Skip to content

[tools] LLVM Advisor - compilation wrapper with artifact collection and analysis #147451

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 9 commits into
base: main
Choose a base branch
from
Draft
15 changes: 15 additions & 0 deletions llvm/tools/llvm-advisor/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
cmake_minimum_required(VERSION 3.18)

set(LLVM_TOOL_LLVM_ADVISOR_BUILD_DEFAULT ON)
set(LLVM_REQUIRE_EXE_NAMES llvm-advisor)

add_subdirectory(src)

# Set the executable name
set_target_properties(llvm-advisor PROPERTIES
OUTPUT_NAME llvm-advisor)

# Install the binary
install(TARGETS llvm-advisor
RUNTIME DESTINATION bin
COMPONENT llvm-advisor)
7 changes: 7 additions & 0 deletions llvm/tools/llvm-advisor/config/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"outputDir": ".llvm-advisor",
"verbose": false,
"keepTemps": false,
"runProfiler": true,
"timeout": 60
}
35 changes: 35 additions & 0 deletions llvm/tools/llvm-advisor/src/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# Gather all .cpp sources in this directory tree
file(GLOB_RECURSE LLVM_ADVISOR_SOURCES CONFIGURE_DEPENDS
${CMAKE_CURRENT_SOURCE_DIR}/*.cpp
)

# Define the executable target
add_llvm_tool(llvm-advisor
${LLVM_ADVISOR_SOURCES}
)

# Link required LLVM libraries
target_link_libraries(llvm-advisor PRIVATE
LLVMSupport
LLVMCore
LLVMIRReader
LLVMBitWriter
LLVMRemarks
LLVMProfileData
)

# Set include directories
target_include_directories(llvm-advisor PRIVATE
${CMAKE_CURRENT_SOURCE_DIR}
)

# Install the Python view module alongside the binary
install(DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/../view/
DESTINATION ${CMAKE_INSTALL_BINDIR}/view
FILES_MATCHING
PATTERN "*.py"
PATTERN "*.html"
PATTERN "*.css"
PATTERN "*.js"
PATTERN "*.md"
)
64 changes: 64 additions & 0 deletions llvm/tools/llvm-advisor/src/Config/AdvisorConfig.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
#include "AdvisorConfig.h"
#include "llvm/Support/FileSystem.h"
#include "llvm/Support/JSON.h"
#include "llvm/Support/MemoryBuffer.h"
#include "llvm/Support/Path.h"

namespace llvm {
namespace advisor {

AdvisorConfig::AdvisorConfig() {
// Use relative path as default, will be resolved by CompilationManager
OutputDir_ = ".llvm-advisor";
}

Expected<bool> AdvisorConfig::loadFromFile(const std::string &path) {
auto BufferOrError = MemoryBuffer::getFile(path);
if (!BufferOrError) {
return createStringError(BufferOrError.getError(),
"Cannot read config file");
}

auto Buffer = std::move(*BufferOrError);
Expected<json::Value> JsonOrError = json::parse(Buffer->getBuffer());
if (!JsonOrError) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In general, get familiar with:
https://llvm.org/docs/CodingStandards.html

return JsonOrError.takeError();
}

auto &Json = *JsonOrError;
auto *Obj = Json.getAsObject();
if (!Obj) {
return createStringError(std::make_error_code(std::errc::invalid_argument),
"Config file must contain JSON object");
}

if (auto outputDirOpt = Obj->getString("outputDir"); outputDirOpt) {
OutputDir_ = outputDirOpt->str();
}

if (auto verboseOpt = Obj->getBoolean("verbose"); verboseOpt) {
Verbose_ = *verboseOpt;
}

if (auto keepTempsOpt = Obj->getBoolean("keepTemps"); keepTempsOpt) {
KeepTemps_ = *keepTempsOpt;
}

if (auto runProfileOpt = Obj->getBoolean("runProfiler"); runProfileOpt) {
RunProfiler_ = *runProfileOpt;
}

if (auto timeoutOpt = Obj->getInteger("timeout"); timeoutOpt) {
TimeoutSeconds_ = static_cast<int>(*timeoutOpt);
}

return true;
}

std::string AdvisorConfig::getToolPath(const std::string &tool) const {
// For now, just return the tool name and rely on PATH
return tool;
}

} // namespace advisor
} // namespace llvm
41 changes: 41 additions & 0 deletions llvm/tools/llvm-advisor/src/Config/AdvisorConfig.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
#ifndef LLVM_ADVISOR_CONFIG_H
#define LLVM_ADVISOR_CONFIG_H

#include "llvm/Support/Error.h"
#include <string>

namespace llvm {
namespace advisor {

class AdvisorConfig {
public:
AdvisorConfig();

Expected<bool> loadFromFile(const std::string &path);

void setOutputDir(const std::string &dir) { OutputDir_ = dir; }
void setVerbose(bool verbose) { Verbose_ = verbose; }
void setKeepTemps(bool keep) { KeepTemps_ = keep; }
void setRunProfiler(bool run) { RunProfiler_ = run; }
void setTimeout(int seconds) { TimeoutSeconds_ = seconds; }

const std::string &getOutputDir() const { return OutputDir_; }
bool getVerbose() const { return Verbose_; }
bool getKeepTemps() const { return KeepTemps_; }
bool getRunProfiler() const { return RunProfiler_; }
int getTimeout() const { return TimeoutSeconds_; }

std::string getToolPath(const std::string &tool) const;

private:
std::string OutputDir_;
bool Verbose_ = false;
bool KeepTemps_ = false;
bool RunProfiler_ = true;
int TimeoutSeconds_ = 60;
};

} // namespace advisor
} // namespace llvm

#endif
52 changes: 52 additions & 0 deletions llvm/tools/llvm-advisor/src/Core/BuildContext.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
#ifndef LLVM_ADVISOR_BUILD_CONTEXT_H
#define LLVM_ADVISOR_BUILD_CONTEXT_H

#include <map>
#include <string>
#include <vector>

namespace llvm {
namespace advisor {

enum class BuildPhase {
Unknown,
Preprocessing,
Compilation,
Assembly,
Linking,
Archiving,
CMakeConfigure,
CMakeBuild,
MakefileBuild
};

enum class BuildTool {
Unknown,
Clang,
GCC,
LLVM_Tools,
CMake,
Make,
Ninja,
Linker,
Archiver
};

struct BuildContext {
BuildPhase phase;
BuildTool tool;
std::string workingDirectory;
std::string outputDirectory;
std::vector<std::string> inputFiles;
std::vector<std::string> outputFiles;
std::vector<std::string> expectedGeneratedFiles;
std::map<std::string, std::string> metadata;
bool hasOffloading = false;
bool hasDebugInfo = false;
bool hasOptimization = false;
};

} // namespace advisor
} // namespace llvm

#endif
109 changes: 109 additions & 0 deletions llvm/tools/llvm-advisor/src/Core/BuildExecutor.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
#include "BuildExecutor.h"
#include "llvm/Support/FileSystem.h"
#include "llvm/Support/Path.h"
#include "llvm/Support/Program.h"
#include "llvm/Support/raw_ostream.h"

namespace llvm {
namespace advisor {

BuildExecutor::BuildExecutor(const AdvisorConfig &config) : config_(config) {}

Expected<int> BuildExecutor::execute(const std::string &compiler,
const std::vector<std::string> &args,
BuildContext &buildContext,
const std::string &tempDir) {
auto instrumentedArgs = instrumentCompilerArgs(args, buildContext, tempDir);

auto compilerPath = sys::findProgramByName(compiler);
if (!compilerPath) {
return createStringError(
std::make_error_code(std::errc::no_such_file_or_directory),
"Compiler not found: " + compiler);
}

std::vector<StringRef> execArgs;
execArgs.push_back(compiler);
for (const auto &arg : instrumentedArgs) {
execArgs.push_back(arg);
}

if (config_.getVerbose()) {
outs() << "Executing: " << compiler;
for (const auto &arg : instrumentedArgs) {
outs() << " " << arg;
}
outs() << "\n";
}

return sys::ExecuteAndWait(*compilerPath, execArgs);
}

std::vector<std::string>
BuildExecutor::instrumentCompilerArgs(const std::vector<std::string> &args,
BuildContext &buildContext,
const std::string &tempDir) {

std::vector<std::string> result = args;
std::set<std::string> existingFlags;

// Scan existing flags to avoid duplication
for (const auto &arg : args) {
if (arg.find("-g") == 0)
existingFlags.insert("debug");
if (arg.find("-fsave-optimization-record") != std::string::npos)
existingFlags.insert("remarks");
if (arg.find("-fprofile-instr-generate") != std::string::npos)
existingFlags.insert("profile");
}

// Add debug info if not present
if (existingFlags.find("debug") == existingFlags.end()) {
result.push_back("-g");
}

// Add optimization remarks with proper redirection
if (existingFlags.find("remarks") == existingFlags.end()) {
result.push_back("-fsave-optimization-record");
result.push_back("-foptimization-record-file=" + tempDir +
"/remarks.opt.yaml");
buildContext.expectedGeneratedFiles.push_back(tempDir +
"/remarks.opt.yaml");
} else {
// If user already specified remarks, find and redirect the file
bool foundFileFlag = false;
for (auto &arg : result) {
if (arg.find("-foptimization-record-file=") != std::string::npos) {
// Extract filename and redirect to temp
StringRef existingPath = StringRef(arg).substr(26);
StringRef filename = sys::path::filename(existingPath);
arg = "-foptimization-record-file=" + tempDir + "/" + filename.str();
buildContext.expectedGeneratedFiles.push_back(tempDir + "/" +
filename.str());
foundFileFlag = true;
break;
}
}
// If no explicit file specified, add our own
if (!foundFileFlag) {
result.push_back("-foptimization-record-file=" + tempDir +
"/remarks.opt.yaml");
buildContext.expectedGeneratedFiles.push_back(tempDir +
"/remarks.opt.yaml");
}
}

// Add profiling if enabled and not present, redirect to temp directory
if (config_.getRunProfiler() &&
existingFlags.find("profile") == existingFlags.end()) {
result.push_back("-fprofile-instr-generate=" + tempDir +
"/profile.profraw");
result.push_back("-fcoverage-mapping");
buildContext.expectedGeneratedFiles.push_back(tempDir + "/profile.profraw");
}

return result;
}

} // namespace advisor
} // namespace llvm
34 changes: 34 additions & 0 deletions llvm/tools/llvm-advisor/src/Core/BuildExecutor.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#ifndef LLVM_ADVISOR_BUILD_EXECUTOR_H
#define LLVM_ADVISOR_BUILD_EXECUTOR_H

#include "../Config/AdvisorConfig.h"
#include "BuildContext.h"
#include "llvm/Support/Error.h"
#include <set>
#include <string>
#include <vector>

namespace llvm {
namespace advisor {

class BuildExecutor {
public:
BuildExecutor(const AdvisorConfig &config);

Expected<int> execute(const std::string &compiler,
const std::vector<std::string> &args,
BuildContext &buildContext, const std::string &tempDir);

private:
std::vector<std::string>
instrumentCompilerArgs(const std::vector<std::string> &args,
BuildContext &buildContext,
const std::string &tempDir);

const AdvisorConfig &config_;
};

} // namespace advisor
} // namespace llvm

#endif
Loading