[Mlir-commits] [mlir] [MLIR][NVVM] Return ISA compiler log via ObjectAttr properties (PR #176697)
llvmlistbot at llvm.org
llvmlistbot at llvm.org
Sun Jan 18 22:35:31 PST 2026
llvmbot wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-mlir-llvm
Author: Zichen Lu (MikaOvO)
<details>
<summary>Changes</summary>
This PR makes the compilation log from ISA compiler available to users by returning it as part of the `gpu::ObjectAttr` properties, following the existing pattern like `LLVMIRToISATimeInMs`.
Currently, the compiler log (which contains useful information such as spill statistics when --verbose is passed) is only accessible in debug builds via `LLVM_DEBUG`. However, there are good reasons to make this information available in release builds as well:
1. Both `ptxas` and `libnvptxcompiler` are publicly available tools/libraries distributed with the CUDA Toolkit. The `--verbose` flag and its output are documented public features, not internal debug information.
2. The verbose output provides valuable insights for users.
---
Full diff: https://github.com/llvm/llvm-project/pull/176697.diff
2 Files Affected:
- (modified) mlir/lib/Target/LLVM/NVVM/Target.cpp (+31-6)
- (modified) mlir/test/Dialect/GPU/module-to-binary-nvvm.mlir (+10)
``````````diff
diff --git a/mlir/lib/Target/LLVM/NVVM/Target.cpp b/mlir/lib/Target/LLVM/NVVM/Target.cpp
index e2588052d7ba6..1c0ccd6be43d7 100644
--- a/mlir/lib/Target/LLVM/NVVM/Target.cpp
+++ b/mlir/lib/Target/LLVM/NVVM/Target.cpp
@@ -232,6 +232,9 @@ class NVPTXSerializer : public SerializeGPUModuleBase {
/// is LLVMIR or ISA.
std::optional<int64_t> getISAToBinaryTimeInMs();
+ /// Get the compiler log from ISA compiler.
+ StringRef getISACompilerLog() const;
+
private:
using TmpFile = std::pair<llvm::SmallString<128>, llvm::FileRemover>;
@@ -253,6 +256,9 @@ class NVPTXSerializer : public SerializeGPUModuleBase {
/// ISA->Binary perf result.
std::optional<int64_t> isaToBinaryTimeInMs;
+
+ /// Compiler log from ptxas or libnvptxcompiler.
+ std::string isaCompilerLog;
};
} // namespace
@@ -285,6 +291,8 @@ std::optional<int64_t> NVPTXSerializer::getISAToBinaryTimeInMs() {
return isaToBinaryTimeInMs;
}
+StringRef NVPTXSerializer::getCompilerLog() const { return isaCompilerLog; }
+
gpu::GPUModuleOp NVPTXSerializer::getOperation() {
return dyn_cast<gpu::GPUModuleOp>(&SerializeGPUModuleBase::getOperation());
}
@@ -484,6 +492,9 @@ NVPTXSerializer::compileToBinary(StringRef ptxCode) {
/*MemoryLimit=*/0,
/*ErrMsg=*/&message))
return emitLogError("`ptxas`");
+
+ if (auto logBuffer = llvm::MemoryBuffer::getFile(logFile->first))
+ isaCompilerLog = (*logBuffer)->getBuffer().str();
#define DEBUG_TYPE "dump-sass"
LLVM_DEBUG({
std::optional<std::string> nvdisasm = findTool("nvdisasm");
@@ -610,21 +621,26 @@ NVPTXSerializer::compileToBinaryNVPTX(StringRef ptxCode) {
SmallVector<char, 0> binary(elfSize, 0);
RETURN_ON_NVPTXCOMPILER_ERROR(
nvPTXCompilerGetCompiledProgram(compiler, (void *)binary.data()));
+
+ RETURN_ON_NVPTXCOMPILER_ERROR(
+ nvPTXCompilerGetInfoLogSize(compiler, &logSize));
+
+ if (logSize != 0) {
+ SmallVector<char> log(logSize + 1, 0);
+ RETURN_ON_NVPTXCOMPILER_ERROR(
+ nvPTXCompilerGetInfoLog(compiler, log.data()));
+ isaCompilerLog = log.data();
+ }
// Dump the log of the compiler, helpful if the verbose flag was passed.
#define DEBUG_TYPE "serialize-to-binary"
LLVM_DEBUG({
- RETURN_ON_NVPTXCOMPILER_ERROR(
- nvPTXCompilerGetInfoLogSize(compiler, &logSize));
if (logSize != 0) {
- SmallVector<char> log(logSize + 1, 0);
- RETURN_ON_NVPTXCOMPILER_ERROR(
- nvPTXCompilerGetInfoLog(compiler, log.data()));
LDBG() << "NVPTX compiler invocation for module: "
<< getOperation().getNameAttr()
<< "\nArguments: " << llvm::interleaved(cmdOpts.second, " ")
<< "\nOutput\n"
- << log.data();
+ << isaCompilerLog;
}
});
#undef DEBUG_TYPE
@@ -747,6 +763,10 @@ NVVMTargetAttrImpl::serializeToObject(Attribute attribute, Operation *module,
if (isaToBinaryTimeInMs.has_value())
module->setAttr("ISAToBinaryTimeInMs",
builder.getI64IntegerAttr(*isaToBinaryTimeInMs));
+ StringRef isaCompilerLog = serializer.getCompilerLog();
+ if (!isaCompilerLog.empty())
+ module->setAttr("ISACompilerLog", builder.getStringAttr(isaCompilerLog));
+
return result;
}
@@ -775,6 +795,11 @@ NVVMTargetAttrImpl::createObject(Attribute attribute, Operation *module,
}
}
+ if (module->hasAttr("ISACompilerLog")) {
+ StringAttr attr = llvm::dyn_cast<StringAttr>(module->getAttr("ISACompilerLog"));
+ properties.push_back(builder.getNamedAttr("ISACompilerLog", attr));
+ }
+
if (!properties.empty())
objectProps = builder.getDictionaryAttr(properties);
diff --git a/mlir/test/Dialect/GPU/module-to-binary-nvvm.mlir b/mlir/test/Dialect/GPU/module-to-binary-nvvm.mlir
index dee680208bdf0..ff53a45bacd2a 100644
--- a/mlir/test/Dialect/GPU/module-to-binary-nvvm.mlir
+++ b/mlir/test/Dialect/GPU/module-to-binary-nvvm.mlir
@@ -2,6 +2,7 @@
// RUN: mlir-opt %s --gpu-module-to-binary="format=llvm" | FileCheck %s
// RUN: mlir-opt %s --gpu-module-to-binary="format=isa" | FileCheck %s -check-prefix=CHECK-ISA
// RUN: mlir-opt %s --gpu-module-to-binary="format=llvm section=__fatbin" | FileCheck %s -check-prefix=CHECK-SECTION
+// RUN: mlir-opt %s --gpu-module-to-binary="format=bin opts=--verbose" | FileCheck %s -check-prefix=CHECK-ISA-LOG
module attributes {gpu.container_module} {
// CHECK-LABEL:gpu.binary @kernel_module1
@@ -37,4 +38,13 @@ module attributes {gpu.container_module} {
llvm.return
}
}
+
+ // CHECK-LABEL:gpu.binary @kernel_module4
+ // CHECK-ISA-LOG:[#gpu.object<#nvvm.target<chip = "sm_70">, properties = {LLVMIRToISATimeInMs = {{[0-9]+}} : i64, ISAToBinaryTimeInMs = {{[0-9]+}} : i64, ISACompilerLog = {{.*}}, O = 2 : i32}, assembly = "{{.*}}">, #gpu.object<#nvvm.target, properties = {LLVMIRToISATimeInMs = {{[0-9]+}} : i64, O = 2 : i32}, assembly = "{{.*}}">]
+ gpu.module @kernel_module4 [#nvvm.target<chip = "sm_70">] {
+ llvm.func @kernel(%arg0: i32, %arg1: !llvm.ptr,
+ %arg2: !llvm.ptr, %arg3: i64, %arg4: i64,
+ %arg5: i64) attributes {gpu.kernel} {
+ llvm.return
+ }
}
``````````
</details>
https://github.com/llvm/llvm-project/pull/176697
More information about the Mlir-commits
mailing list