[Mlir-commits] [mlir] [MLIR][NVVM] Return ISA compiler log via ObjectAttr properties (PR #176697)
Zichen Lu
llvmlistbot at llvm.org
Tue Jan 20 02:09:46 PST 2026
https://github.com/MikaOvO updated https://github.com/llvm/llvm-project/pull/176697
>From 0520a2467f0cf71c1c5fd38417d29acbe38cabe0 Mon Sep 17 00:00:00 2001
From: Zichen Lu <mikaovo2000 at gmail.com>
Date: Tue, 20 Jan 2026 18:09:31 +0800
Subject: [PATCH] [MLIR][NVVM] Return ISA compiler log via ObjectAttr
properties
---
mlir/lib/Target/LLVM/NVVM/Target.cpp | 48 ++++++++++++++++---
.../CUDA/module-to-binary-compiler-log.mlir | 14 ++++++
2 files changed, 55 insertions(+), 7 deletions(-)
create mode 100644 mlir/test/Integration/GPU/CUDA/module-to-binary-compiler-log.mlir
diff --git a/mlir/lib/Target/LLVM/NVVM/Target.cpp b/mlir/lib/Target/LLVM/NVVM/Target.cpp
index e2588052d7ba6..0c4e7820d9257 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::getISACompilerLog() const { return isaCompilerLog; }
+
gpu::GPUModuleOp NVPTXSerializer::getOperation() {
return dyn_cast<gpu::GPUModuleOp>(&SerializeGPUModuleBase::getOperation());
}
@@ -484,6 +492,11 @@ NVPTXSerializer::compileToBinary(StringRef ptxCode) {
/*MemoryLimit=*/0,
/*ErrMsg=*/&message))
return emitLogError("`ptxas`");
+
+ if (target.hasFlag("compiler-diagnostics")) {
+ 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");
@@ -611,21 +624,32 @@ NVPTXSerializer::compileToBinaryNVPTX(StringRef ptxCode) {
RETURN_ON_NVPTXCOMPILER_ERROR(
nvPTXCompilerGetCompiledProgram(compiler, (void *)binary.data()));
+ // Lambda to fetch info log; returns empty vector on failure or no log.
+ auto fetchInfoLog = [&]() -> SmallVector<char> {
+ size_t size = 0;
+ if (nvPTXCompilerGetInfoLogSize(compiler, &size) != NVPTXCOMPILE_SUCCESS ||
+ size == 0)
+ return {};
+ SmallVector<char> log(size + 1, 0);
+ if (nvPTXCompilerGetInfoLog(compiler, log.data()) != NVPTXCOMPILE_SUCCESS)
+ return {};
+ return log;
+ };
+
+ if (target.hasFlag("compiler-diagnostics")) {
+ if (auto log = fetchInfoLog(); !log.empty())
+ 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()));
+ if (auto log = fetchInfoLog(); !log.empty())
LDBG() << "NVPTX compiler invocation for module: "
<< getOperation().getNameAttr()
<< "\nArguments: " << llvm::interleaved(cmdOpts.second, " ")
<< "\nOutput\n"
<< log.data();
- }
});
#undef DEBUG_TYPE
RETURN_ON_NVPTXCOMPILER_ERROR(nvPTXCompilerDestroy(&compiler));
@@ -747,6 +771,10 @@ NVVMTargetAttrImpl::serializeToObject(Attribute attribute, Operation *module,
if (isaToBinaryTimeInMs.has_value())
module->setAttr("ISAToBinaryTimeInMs",
builder.getI64IntegerAttr(*isaToBinaryTimeInMs));
+ StringRef isaCompilerLog = serializer.getISACompilerLog();
+ if (!isaCompilerLog.empty())
+ module->setAttr("ISACompilerLog", builder.getStringAttr(isaCompilerLog));
+
return result;
}
@@ -775,6 +803,12 @@ 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/Integration/GPU/CUDA/module-to-binary-compiler-log.mlir b/mlir/test/Integration/GPU/CUDA/module-to-binary-compiler-log.mlir
new file mode 100644
index 0000000000000..e9eb7a4389ff1
--- /dev/null
+++ b/mlir/test/Integration/GPU/CUDA/module-to-binary-compiler-log.mlir
@@ -0,0 +1,14 @@
+// RUN: mlir-opt %s --gpu-module-to-binary="format=%gpu_compilation_format opts=--verbose" \
+// RUN: | FileCheck %s
+
+module attributes {gpu.container_module} {
+ // CHECK-LABEL: gpu.binary @kernel_module
+ // CHECK: properties = {{{.*}}PtxCompilerLog = {{.*}}
+ gpu.module @kernel_module [#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
+ }
+ }
+}
More information about the Mlir-commits
mailing list