Skip to content

Commit

Permalink
When dumping intermediates, dump how to reproduce the .optimized.ll (
Browse files Browse the repository at this point in the history
…#19633)

This is similar to the upstream `--print-pipeline-passes`, which we
don't implement but is still there as a command-line flag. In our case,
I think it's more useful to integrate it into our existing
dump-intermediates feature. This would need to be done for each target;
this PR only does it for ROCMTarget for now.

Sample output at the start of the dumped `.optimized.ll` file:
```
; To reproduce the .optimized.ll from the .linked.ll, run:
; opt --passes=' [...] elided 4 kilobytes of text [...] '
```

Signed-off-by: Benoit Jacob <jacob.benoit.1@gmail.com>
  • Loading branch information
bjacob authored Jan 8, 2025
1 parent be75a30 commit 7b9aa28
Showing 1 changed file with 18 additions and 5 deletions.
23 changes: 18 additions & 5 deletions compiler/plugins/target/ROCM/ROCMTarget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -175,9 +175,10 @@ static StringRef getABI(IREE::HAL::ExecutableTargetAttr targetAttr) {

static void dumpModuleToPath(StringRef path, StringRef baseName,
StringRef suffix, StringRef extension,
llvm::Module &module) {
llvm::Module &module, StringRef header = {}) {
llvm::SmallVector<char, 0> data;
llvm::raw_svector_ostream ostream(data);
ostream << header;
module.print(ostream, nullptr);
dumpDataToPath(path, baseName, suffix, extension,
StringRef(data.data(), data.size()));
Expand Down Expand Up @@ -295,7 +296,8 @@ class ROCMTargetBackend final : public TargetBackend {
static void optimizeModule(llvm::Module &module,
llvm::TargetMachine &targetMachine,
ArrayRef<std::string> passPlugins,
bool slpVectorization) {
bool slpVectorization,
std::string &outPassesString) {
llvm::LoopAnalysisManager lam;
llvm::FunctionAnalysisManager fam;
llvm::CGSCCAnalysisManager cgam;
Expand Down Expand Up @@ -336,7 +338,11 @@ class ROCMTargetBackend final : public TargetBackend {
mpm.addPass(llvm::VerifierPass());
mpm.addPass(pb.buildPerModuleDefaultPipeline(ol));
mpm.addPass(llvm::VerifierPass());

llvm::raw_string_ostream os(outPassesString);
mpm.printPipeline(os, [&pic](StringRef className) {
auto passName = pic.getPassNameForClassName(className);
return passName.empty() ? className : passName;
});
mpm.run(module, mam);
}

Expand Down Expand Up @@ -566,12 +572,19 @@ class ROCMTargetBackend final : public TargetBackend {
}

// Run LLVM optimization passes.
std::string passesString;
optimizeModule(*llvmModule, *targetMachine, options.passPlugins,
options.slpVectorization);
options.slpVectorization, passesString);
if (!serializationOptions.dumpIntermediatesPath.empty()) {
std::string header = llvm::formatv(R"TXT(
; To reproduce the .optimized.ll from the .linked.ll, run:
; opt --passes='{}'
)TXT",
passesString);
dumpModuleToPath(serializationOptions.dumpIntermediatesPath,
serializationOptions.dumpBaseName, variantOp.getName(),
".optimized.ll", *llvmModule);
".optimized.ll", *llvmModule, header);
}

if (failed(validateFinalizedModule(variantOp, *llvmModule))) {
Expand Down

0 comments on commit 7b9aa28

Please sign in to comment.