[Mlir-commits] [mlir] [MLIR][NVVM] Return ISA compiler log via ObjectAttr properties (PR #176697)

Zichen Lu llvmlistbot at llvm.org
Tue Jan 20 01:22:41 PST 2026


https://github.com/MikaOvO updated https://github.com/llvm/llvm-project/pull/176697

>From c3bae8ba2595ceb69031e9249e025cc9d463e65c Mon Sep 17 00:00:00 2001
From: Zichen Lu <mikaovo2000 at gmail.com>
Date: Mon, 19 Jan 2026 14:28:32 +0800
Subject: [PATCH] [MLIR][NVVM] Return ISA compiler log via ObjectAttr
 properties

---
 mlir/lib/Target/LLVM/NVVM/Target.cpp          | 48 ++++++++++++++++---
 .../Dialect/GPU/module-to-binary-nvvm.mlir    | 10 ++++
 2 files changed, 51 insertions(+), 7 deletions(-)

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/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
+    }
 }



More information about the Mlir-commits mailing list