-
Notifications
You must be signed in to change notification settings - Fork 15.4k
[mlir][gpu] gpu-module-to-binary: add option to dump intermediate files
#170016
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
base: main
Are you sure you want to change the base?
Changes from 4 commits
8ce37a2
b102b57
526849e
30abee7
64a0a28
4ba95d3
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -17,6 +17,12 @@ | |
|
|
||
| #include "llvm/ADT/STLExtras.h" | ||
| #include "llvm/ADT/StringSwitch.h" | ||
| #include "llvm/Support/DebugLog.h" | ||
| #include "llvm/Support/FileSystem.h" | ||
| #include "llvm/Support/Path.h" | ||
| #include "llvm/Support/ToolOutputFile.h" | ||
|
|
||
| #define DEBUG_TYPE "gpu-module-to-binary" | ||
|
|
||
| using namespace mlir; | ||
| using namespace mlir::gpu; | ||
|
|
@@ -26,6 +32,27 @@ namespace mlir { | |
| #include "mlir/Dialect/GPU/Transforms/Passes.h.inc" | ||
| } // namespace mlir | ||
|
|
||
| static void dumpToFile(StringRef dumpDir, const llvm::Twine &filename, | ||
| function_ref<void(llvm::raw_ostream &)> writeContent) { | ||
| if (dumpDir.empty()) | ||
| return; | ||
|
|
||
| llvm::SmallString<128> path(dumpDir); | ||
| llvm::sys::path::append(path, filename); | ||
|
|
||
| std::error_code ec; | ||
| llvm::ToolOutputFile output(path, ec, llvm::sys::fs::OF_None); | ||
| if (ec) { | ||
| LDBG() << "Failed to create file '" << path << "': " << ec.message() | ||
| << "\n"; | ||
|
||
| return; | ||
| } | ||
|
|
||
| writeContent(output.os()); | ||
| output.keep(); | ||
kuhar marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| LDBG() << "Dumped intermediate to: " << path << "\n"; | ||
| } | ||
|
|
||
| namespace { | ||
| class GpuModuleToBinaryPass | ||
| : public impl::GpuModuleToBinaryPassBase<GpuModuleToBinaryPass> { | ||
|
|
@@ -64,8 +91,42 @@ void GpuModuleToBinaryPass::runOnOperation() { | |
| SmallVector<Attribute> librariesToLink; | ||
| for (const std::string &path : linkFiles) | ||
| librariesToLink.push_back(StringAttr::get(&getContext(), path)); | ||
|
|
||
| // Create dump directory if specified. | ||
| if (!dumpIntermediates.empty()) { | ||
| if (std::error_code ec = | ||
| llvm::sys::fs::create_directories(dumpIntermediates)) { | ||
| getOperation()->emitError() << "Failed to create dump directory '" | ||
| << dumpIntermediates << "': " << ec.message(); | ||
joker-eph marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| return signalPassFailure(); | ||
| } | ||
| } | ||
|
|
||
| // Create callbacks for dumping intermediate artifacts if requested. | ||
| auto initialIRCallback = [&](llvm::Module &mod) { | ||
| dumpToFile(dumpIntermediates, mod.getName() + ".initial.ll", | ||
| [&](llvm::raw_ostream &os) { mod.print(os, nullptr); }); | ||
| }; | ||
|
|
||
| auto linkedIRCallback = [&](llvm::Module &mod) { | ||
| dumpToFile(dumpIntermediates, mod.getName() + ".linked.ll", | ||
| [&](llvm::raw_ostream &os) { mod.print(os, nullptr); }); | ||
| }; | ||
|
|
||
| auto optimizedIRCallback = [&](llvm::Module &mod) { | ||
| dumpToFile(dumpIntermediates, mod.getName() + ".opt.ll", | ||
| [&](llvm::raw_ostream &os) { mod.print(os, nullptr); }); | ||
| }; | ||
|
|
||
| auto isaCallback = [&](StringRef isa) { | ||
| dumpToFile(dumpIntermediates, "kernel.isa", | ||
| [&](llvm::raw_ostream &os) { os << isa; }); | ||
| }; | ||
|
|
||
| TargetOptions targetOptions(toolkitPath, librariesToLink, cmdOptions, | ||
| elfSection, *targetFormat, lazyTableBuilder); | ||
| elfSection, *targetFormat, lazyTableBuilder, | ||
| initialIRCallback, linkedIRCallback, | ||
| optimizedIRCallback, isaCallback); | ||
| if (failed(transformGpuModulesToBinaries( | ||
| getOperation(), OffloadingLLVMTranslationAttrInterface(nullptr), | ||
| targetOptions))) | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| // REQUIRES: host-supports-nvptx | ||
| // RUN: rm -rf %t | ||
| // RUN: mlir-opt %s --gpu-module-to-binary='format=isa dump-intermediates=%t' | FileCheck %s | ||
| // RUN: test -f %t/kernel_module.initial.ll | ||
| // RUN: test -f %t/kernel_module.linked.ll | ||
| // RUN: test -f %t/kernel_module.opt.ll | ||
| // RUN: test -f %t/kernel.isa | ||
|
|
||
| module attributes {gpu.container_module} { | ||
| // CHECK-LABEL: gpu.binary @kernel_module | ||
|
|
||
| gpu.module @kernel_module [#nvvm.target<chip = "sm_70">] { | ||
| llvm.func @kernel(%arg0: f32) { | ||
| llvm.return | ||
| } | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| // REQUIRES: host-supports-amdgpu | ||
| // RUN: rm -rf %t | ||
| // RUN: mlir-opt %s --gpu-module-to-binary='format=isa dump-intermediates=%t' | FileCheck %s | ||
| // RUN: test -f %t/kernel_module.initial.ll | ||
| // RUN: test -f %t/kernel_module.linked.ll | ||
| // RUN: test -f %t/kernel_module.opt.ll | ||
| // RUN: test -f %t/kernel.isa | ||
|
|
||
| module attributes {gpu.container_module} { | ||
| // CHECK-LABEL: gpu.binary @kernel_module | ||
|
|
||
| gpu.module @kernel_module [#rocdl.target<chip = "gfx942">] { | ||
| llvm.func @kernel(%arg0: f32) { | ||
| llvm.return | ||
| } | ||
| } | ||
| } |
Uh oh!
There was an error while loading. Please reload this page.