Skip to content

Commit 92fb60f

Browse files
committed
[MLIR][NVVM] Add dumpISA and dumpMachineISA Flags
Currently, dumping virtual and machine-level ISA is restricted to debug builds, making it unavailable in release builds. However, the ability to view these ISAs can be valuable even in release builds or with production compilers. For instance, `nvcc` provides similar functionality, making it a useful reference. This PR introduces `dumpISA` and `dumpMachineISA` flags to the `GpuModuleToBinaryPass`. Additionally, it adds `dump-ptx` and `dump-sass` flags to the `GPUToNVVMPipelineOptions`.
1 parent a3cd269 commit 92fb60f

File tree

11 files changed

+66
-29
lines changed

11 files changed

+66
-29
lines changed

mlir/include/mlir/Dialect/GPU/IR/CompilationInterfaces.h

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,8 @@ class TargetOptions {
5252
StringRef toolkitPath = {}, ArrayRef<std::string> linkFiles = {},
5353
StringRef cmdOptions = {},
5454
CompilationTarget compilationTarget = getDefaultCompilationTarget(),
55-
function_ref<SymbolTable *()> getSymbolTableCallback = {});
55+
function_ref<SymbolTable *()> getSymbolTableCallback = {},
56+
bool dumpISA = false, bool dumpMachineISA = false);
5657

5758
/// Returns the typeID.
5859
TypeID getTypeID() const;
@@ -66,6 +67,12 @@ class TargetOptions {
6667
/// Returns the command line options.
6768
StringRef getCmdOptions() const;
6869

70+
/// Returns the dump-isa command line options.
71+
bool getDumpISA() const;
72+
73+
/// Returns the dump-machine-isa command line options.
74+
bool getDumpMachineISA() const;
75+
6976
/// Returns a tokenization of the command line options.
7077
std::pair<llvm::BumpPtrAllocator, SmallVector<const char *>>
7178
tokenizeCmdOptions() const;
@@ -90,7 +97,8 @@ class TargetOptions {
9097
TypeID typeID, StringRef toolkitPath = {},
9198
ArrayRef<std::string> linkFiles = {}, StringRef cmdOptions = {},
9299
CompilationTarget compilationTarget = getDefaultCompilationTarget(),
93-
function_ref<SymbolTable *()> getSymbolTableCallback = {});
100+
function_ref<SymbolTable *()> getSymbolTableCallback = {},
101+
bool dumpISA = false, bool dumpMachineISA = false);
94102

95103
/// Path to the target toolkit.
96104
std::string toolkitPath;
@@ -102,6 +110,12 @@ class TargetOptions {
102110
/// process.
103111
std::string cmdOptions;
104112

113+
/// An optional flag to dump generated ISA.
114+
bool dumpISA = false;
115+
116+
/// An optional flag to dump generated and disassembled machine ISA.
117+
bool dumpMachineISA = false;
118+
105119
/// Compilation process target format.
106120
CompilationTarget compilationTarget;
107121

mlir/include/mlir/Dialect/GPU/Pipelines/Passes.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,12 @@ struct GPUToNVVMPipelineOptions
5353
"Whether to use the bareptr calling convention on the host (warning "
5454
"this should be false until the GPU layering is fixed)"),
5555
llvm::cl::init(false)};
56+
PassOptions::Option<bool> dumpPtx{
57+
*this, "dump-ptx", llvm::cl::desc("Dumps PTX code to the error output"),
58+
llvm::cl::init(false)};
59+
PassOptions::Option<bool> dumpSass{
60+
*this, "dump-sass", llvm::cl::desc("Dumps SASS code to the error output"),
61+
llvm::cl::init(false)};
5662
};
5763

5864
//===----------------------------------------------------------------------===//

mlir/include/mlir/Dialect/GPU/Transforms/Passes.td

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,15 @@ def GpuModuleToBinaryPass
9595
Option<"cmdOptions", "opts", "std::string", [{""}],
9696
"Command line options to pass to the tools.">,
9797
Option<"compilationTarget", "format", "std::string", [{"fatbin"}],
98-
"The target representation of the compilation process.">
98+
"The target representation of the compilation process.">,
99+
Option<"dumpISA", "dump-isa", "bool",
100+
/*default=*/"false",
101+
"Dumps generated ISA to the error output.">,
102+
Option<"dumpMachineISA", "dump-machine-isa", "bool",
103+
/*default=*/"false",
104+
"Dumps the generated machine-level ISA to the error output. "
105+
"If the generated ISA is virtual, it instead dumps the"
106+
"machine-level equivalent.">
99107
];
100108
}
101109

mlir/include/mlir/Target/LLVM/NVVM/Utils.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ class SerializeGPUModuleBase : public LLVM::ModuleToObject {
5454
LogicalResult appendStandardLibs();
5555

5656
/// Loads the bitcode files in `fileList`.
57-
virtual std::optional<SmallVector<std::unique_ptr<llvm::Module>>>
57+
std::optional<SmallVector<std::unique_ptr<llvm::Module>>>
5858
loadBitcodeFiles(llvm::Module &module) override;
5959

6060
protected:

mlir/lib/Dialect/GPU/IR/GPUDialect.cpp

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2302,18 +2302,26 @@ KernelMetadataAttr KernelTableAttr::lookup(StringAttr key) const {
23022302
TargetOptions::TargetOptions(
23032303
StringRef toolkitPath, ArrayRef<std::string> linkFiles,
23042304
StringRef cmdOptions, CompilationTarget compilationTarget,
2305-
function_ref<SymbolTable *()> getSymbolTableCallback)
2305+
function_ref<SymbolTable *()> getSymbolTableCallback, bool dumpISA,
2306+
bool dumpMachineISA)
23062307
: TargetOptions(TypeID::get<TargetOptions>(), toolkitPath, linkFiles,
2307-
cmdOptions, compilationTarget, getSymbolTableCallback) {}
2308+
cmdOptions, compilationTarget, getSymbolTableCallback,
2309+
dumpISA, dumpMachineISA) {}
23082310

23092311
TargetOptions::TargetOptions(
23102312
TypeID typeID, StringRef toolkitPath, ArrayRef<std::string> linkFiles,
23112313
StringRef cmdOptions, CompilationTarget compilationTarget,
2312-
function_ref<SymbolTable *()> getSymbolTableCallback)
2314+
function_ref<SymbolTable *()> getSymbolTableCallback, bool dumpISA,
2315+
bool dumpMachineISA)
23132316
: toolkitPath(toolkitPath.str()), linkFiles(linkFiles),
2314-
cmdOptions(cmdOptions.str()), compilationTarget(compilationTarget),
2317+
cmdOptions(cmdOptions.str()), dumpISA(dumpISA),
2318+
dumpMachineISA(dumpMachineISA), compilationTarget(compilationTarget),
23152319
getSymbolTableCallback(getSymbolTableCallback), typeID(typeID) {}
23162320

2321+
bool TargetOptions::getDumpISA() const { return dumpISA; }
2322+
2323+
bool TargetOptions::getDumpMachineISA() const { return dumpMachineISA; }
2324+
23172325
TypeID TargetOptions::getTypeID() const { return typeID; }
23182326

23192327
StringRef TargetOptions::getToolkitPath() const { return toolkitPath; }

mlir/lib/Dialect/GPU/Pipelines/GPUToNVVMPipeline.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,8 @@ void buildHostPostPipeline(OpPassManager &pm,
9595

9696
GpuModuleToBinaryPassOptions gpuModuleToBinaryPassOptions;
9797
gpuModuleToBinaryPassOptions.compilationTarget = options.cubinFormat;
98+
gpuModuleToBinaryPassOptions.dumpISA = options.dumpPtx;
99+
gpuModuleToBinaryPassOptions.dumpMachineISA = options.dumpSass;
98100
pm.addPass(createGpuModuleToBinaryPass(gpuModuleToBinaryPassOptions));
99101
pm.addPass(createConvertMathToLLVMPass());
100102
pm.addPass(createCanonicalizerPass());

mlir/lib/Dialect/GPU/Transforms/ModuleToBinary.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ void GpuModuleToBinaryPass::runOnOperation() {
7070
};
7171

7272
TargetOptions targetOptions(toolkitPath, linkFiles, cmdOptions, *targetFormat,
73-
lazyTableBuilder);
73+
lazyTableBuilder, dumpISA, dumpMachineISA);
7474
if (failed(transformGpuModulesToBinaries(
7575
getOperation(), OffloadingLLVMTranslationAttrInterface(nullptr),
7676
targetOptions)))

mlir/lib/Target/LLVM/NVVM/Target.cpp

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -292,8 +292,8 @@ NVPTXSerializer::compileToBinary(const std::string &ptxCode) {
292292
return std::nullopt;
293293
TmpFile cubinFile;
294294
if (createFatbin) {
295-
Twine cubinFilename = ptxFile->first + ".cubin";
296-
cubinFile = TmpFile(cubinFilename.str(), llvm::FileRemover(cubinFilename));
295+
std::string cubinFilename = (ptxFile->first + ".cubin").str();
296+
cubinFile = TmpFile(cubinFilename, llvm::FileRemover(cubinFilename));
297297
} else {
298298
cubinFile.first = binaryFile->first;
299299
}
@@ -402,8 +402,8 @@ NVPTXSerializer::compileToBinary(const std::string &ptxCode) {
402402
/*MemoryLimit=*/0,
403403
/*ErrMsg=*/&message))
404404
return emitLogError("`ptxas`");
405-
#define DEBUG_TYPE "dump-sass"
406-
LLVM_DEBUG({
405+
406+
if (targetOptions.getDumpMachineISA()) {
407407
std::optional<std::string> nvdisasm = findTool("nvdisasm");
408408
SmallVector<StringRef> nvdisasmArgs(
409409
{StringRef("nvdisasm"), StringRef(cubinFile.first)});
@@ -417,11 +417,10 @@ NVPTXSerializer::compileToBinary(const std::string &ptxCode) {
417417
llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>> logBuffer =
418418
llvm::MemoryBuffer::getFile(logFile->first);
419419
if (logBuffer && !(*logBuffer)->getBuffer().empty()) {
420-
llvm::dbgs() << "Output:\n" << (*logBuffer)->getBuffer() << "\n";
421-
llvm::dbgs().flush();
420+
llvm::errs() << "Output:\n" << (*logBuffer)->getBuffer() << "\n";
421+
llvm::errs().flush();
422422
}
423-
});
424-
#undef DEBUG_TYPE
423+
}
425424

426425
// Invoke `fatbin`.
427426
message.clear();
@@ -572,12 +571,13 @@ NVPTXSerializer::moduleToObject(llvm::Module &llvmModule) {
572571
getOperation().emitError() << "Failed translating the module to ISA.";
573572
return std::nullopt;
574573
}
575-
#define DEBUG_TYPE "serialize-to-isa"
576-
LLVM_DEBUG({
577-
llvm::dbgs() << "PTX for module: " << getOperation().getNameAttr() << "\n";
578-
llvm::dbgs() << *serializedISA << "\n";
579-
llvm::dbgs().flush();
580-
});
574+
if (targetOptions.getDumpISA()) {
575+
llvm::errs() << "// Generated PTX for module: "
576+
<< getOperation().getNameAttr() << "\n";
577+
llvm::errs() << *serializedISA << "\n";
578+
llvm::errs().flush();
579+
}
580+
581581
#undef DEBUG_TYPE
582582

583583
// Return PTX if the compilation target is `assembly`.

mlir/lib/Target/LLVM/ROCDL/Target.cpp

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -430,13 +430,12 @@ std::optional<SmallVector<char, 0>> SerializeGPUModuleBase::moduleToObjectImpl(
430430
getOperation().emitError() << "failed translating the module to ISA";
431431
return std::nullopt;
432432
}
433-
#define DEBUG_TYPE "serialize-to-isa"
434-
LLVM_DEBUG({
433+
if (targetOptions.getDumpISA()) {
435434
llvm::dbgs() << "ISA for module: "
436435
<< cast<gpu::GPUModuleOp>(getOperation()).getNameAttr() << "\n"
437436
<< *serializedISA << "\n";
438-
});
439-
#undef DEBUG_TYPE
437+
}
438+
440439
// Return ISA assembly code if the compilation target is assembly.
441440
if (targetOptions.getCompilationTarget() == gpu::CompilationTarget::Assembly)
442441
return SmallVector<char, 0>(serializedISA->begin(), serializedISA->end());

mlir/test/Integration/GPU/CUDA/dump-ptx.mlir

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// RUN: mlir-opt %s \
2-
// RUN: | mlir-opt -gpu-lower-to-nvvm-pipeline -debug-only=serialize-to-isa \
2+
// RUN: | mlir-opt -gpu-lower-to-nvvm-pipeline="dump-ptx=true" \
33
// RUN: 2>&1 | FileCheck %s
44

55
// CHECK: Generated by LLVM NVPTX Back-End

0 commit comments

Comments
 (0)