[llvm] [LLVM-Tablegen] Pretty Printing Arguments in LLVM Intrinsics (PR #162629)
Dharuni R Acharya via llvm-commits
llvm-commits at lists.llvm.org
Thu Oct 30 23:14:02 PDT 2025
https://github.com/DharuniRAcharya updated https://github.com/llvm/llvm-project/pull/162629
>From c6ce2c3743dfd5cdb55c7d73ef31186e7b9fe31a Mon Sep 17 00:00:00 2001
From: Dharuni R Acharya <dharunira at nvidia.com>
Date: Thu, 9 Oct 2025 10:07:02 +0000
Subject: [PATCH 1/4] [LLVM-Tablegen] Pretty Printing Arguments in LLVM
Intrinsics
This patch adds LLVM infrastructure to support pretty printing arguments of the intrinsics. The motivation is to increase the readability of LLVM intrinsics and facilitate easy modifications and debugging of LLVM IR.
This adds a property ArgInfo<ArgIndex, "argName", "functionName"> to the intrinsic arguments that enables printing self-explanatory inline comment for the arguments.
The addition of pretty print support can provide a simple, low-overhead feature that enhances usability of LLVM intrinsics without disrupting existing workflows.
Link to the RFC:
https://discourse.llvm.org/t/rfc-pretty-printing-immediate-arguments-in-llvm-intrinsics/88536
Signed-off-by: Dharuni R Acharya <dharunira at nvidia.com>
---
llvm/include/llvm/IR/Intrinsics.h | 9 +++
llvm/include/llvm/IR/Intrinsics.td | 8 ++
llvm/include/llvm/IR/IntrinsicsNVVM.td | 9 ++-
llvm/include/llvm/IR/NVVMIntrinsicUtils.h | 48 ++++++++++++
llvm/lib/IR/AsmWriter.cpp | 41 ++++++++--
llvm/lib/IR/Intrinsics.cpp | 11 +++
.../NVPTX/tcgen05-mma-tensor-formatted.ll | 41 ++++++++++
llvm/test/TableGen/intrinsic-arginfo.td | 71 ++++++++++++++++++
.../TableGen/Basic/CodeGenIntrinsics.cpp | 23 ++++++
llvm/utils/TableGen/Basic/CodeGenIntrinsics.h | 16 ++++
.../utils/TableGen/Basic/IntrinsicEmitter.cpp | 75 +++++++++++++++++++
11 files changed, 343 insertions(+), 9 deletions(-)
create mode 100644 llvm/test/CodeGen/NVPTX/tcgen05-mma-tensor-formatted.ll
create mode 100644 llvm/test/TableGen/intrinsic-arginfo.td
diff --git a/llvm/include/llvm/IR/Intrinsics.h b/llvm/include/llvm/IR/Intrinsics.h
index 9577d0141f168..c91fc254ebe11 100644
--- a/llvm/include/llvm/IR/Intrinsics.h
+++ b/llvm/include/llvm/IR/Intrinsics.h
@@ -30,6 +30,8 @@ class LLVMContext;
class Module;
class AttributeList;
class AttributeSet;
+class raw_ostream;
+class Constant;
/// This namespace contains an enum with a value for every intrinsic/builtin
/// function known by LLVM. The enum values are returned by
@@ -81,6 +83,9 @@ namespace Intrinsic {
/// Returns true if the intrinsic can be overloaded.
LLVM_ABI bool isOverloaded(ID id);
+ /// Returns true if the intrinsic has pretty printed immediate arguments.
+ LLVM_ABI bool hasPrettyPrintedArgs(ID id);
+
/// isTargetIntrinsic - Returns true if IID is an intrinsic specific to a
/// certain target. If it is a generic intrinsic false is returned.
LLVM_ABI bool isTargetIntrinsic(ID IID);
@@ -284,6 +289,10 @@ namespace Intrinsic {
/// N.
LLVM_ABI Intrinsic::ID getDeinterleaveIntrinsicID(unsigned Factor);
+ /// Print the argument info for the arguments with ArgInfo.
+ LLVM_ABI void printImmArg(ID IID, unsigned ArgIdx, raw_ostream &OS,
+ const Constant *ImmArgVal);
+
} // namespace Intrinsic
} // namespace llvm
diff --git a/llvm/include/llvm/IR/Intrinsics.td b/llvm/include/llvm/IR/Intrinsics.td
index 4d59ee8676b9e..ebfc0cddec306 100644
--- a/llvm/include/llvm/IR/Intrinsics.td
+++ b/llvm/include/llvm/IR/Intrinsics.td
@@ -87,6 +87,14 @@ class NoUndef<AttrIndex idx> : IntrinsicProperty {
int ArgNo = idx.Value;
}
+// ArgInfo - The specified argument has an argument name and an optional argument printing
+// function for diagnostic output.
+class ArgInfo<AttrIndex idx, string argname, string funcname = ""> : IntrinsicProperty {
+ int ArgNo = idx.Value;
+ string ArgName = argname;
+ string FunctionName = funcname;
+}
+
// NonNull - The return value or specified argument is not null.
class NonNull<AttrIndex idx> : IntrinsicProperty {
int ArgNo = idx.Value;
diff --git a/llvm/include/llvm/IR/IntrinsicsNVVM.td b/llvm/include/llvm/IR/IntrinsicsNVVM.td
index 719181a09f475..026a3f3bcf2ed 100644
--- a/llvm/include/llvm/IR/IntrinsicsNVVM.td
+++ b/llvm/include/llvm/IR/IntrinsicsNVVM.td
@@ -2954,7 +2954,14 @@ foreach sp = [0, 1] in {
defvar nargs = !size(args);
defvar scale_d_imm = ArgIndex<!sub(nargs, 1)>;
defvar scale_d_imm_range = [ImmArg<scale_d_imm>, Range<scale_d_imm, 0, 16>];
- defvar intrinsic_properties = !listconcat(
+
+ // Check if this is the specific llvm.nvvm.tcgen05.mma.tensor intrinsic.
+ defvar is_target_intrinsic = !and(!eq(sp, 0),
+ !eq(space, "tensor"),
+ !eq(scale_d, 0),
+ !eq(ashift, 0));
+
+ defvar base_properties = !listconcat(
mma.common_intr_props,
!if(!eq(scale_d, 1), scale_d_imm_range, []),
[Range<ArgIndex<nargs>, 0, !if(!eq(scale_d, 1), 2, 4)>, // kind
diff --git a/llvm/include/llvm/IR/NVVMIntrinsicUtils.h b/llvm/include/llvm/IR/NVVMIntrinsicUtils.h
index d55100e5e709d..3b4129a7693f1 100644
--- a/llvm/include/llvm/IR/NVVMIntrinsicUtils.h
+++ b/llvm/include/llvm/IR/NVVMIntrinsicUtils.h
@@ -18,8 +18,11 @@
#include <stdint.h>
#include "llvm/ADT/APFloat.h"
+#include "llvm/ADT/APInt.h"
+#include "llvm/IR/Constants.h"
#include "llvm/IR/Intrinsics.h"
#include "llvm/IR/IntrinsicsNVPTX.h"
+#include "llvm/Support/raw_ostream.h"
namespace llvm {
namespace nvvm {
@@ -659,6 +662,51 @@ inline APFloat::roundingMode GetFMARoundingMode(Intrinsic::ID IntrinsicID) {
llvm_unreachable("Invalid FP instrinsic rounding mode for NVVM fma");
}
+inline void printTcgen05MMAKind(raw_ostream &OS, const Constant *ImmArgVal) {
+ if (const ConstantInt *CI = dyn_cast<ConstantInt>(ImmArgVal)) {
+ uint64_t Val = CI->getZExtValue();
+ switch (static_cast<Tcgen05MMAKind>(Val)) {
+ case Tcgen05MMAKind::F16:
+ OS << "f16";
+ return;
+ case Tcgen05MMAKind::TF32:
+ OS << "tf32";
+ return;
+ case Tcgen05MMAKind::F8F6F4:
+ OS << "f8f6f4";
+ return;
+ case Tcgen05MMAKind::I8:
+ OS << "i8";
+ return;
+ }
+ }
+ llvm_unreachable(
+ "printTcgen05MMAKind called with invalid value for immediate argument");
+}
+
+inline void printTcgen05CollectorUsageOp(raw_ostream &OS,
+ const Constant *ImmArgVal) {
+ if (const ConstantInt *CI = dyn_cast<ConstantInt>(ImmArgVal)) {
+ uint64_t Val = CI->getZExtValue();
+ switch (static_cast<Tcgen05CollectorUsageOp>(Val)) {
+ case Tcgen05CollectorUsageOp::DISCARD:
+ OS << "discard";
+ return;
+ case Tcgen05CollectorUsageOp::LASTUSE:
+ OS << "lastuse";
+ return;
+ case Tcgen05CollectorUsageOp::FILL:
+ OS << "fill";
+ return;
+ case Tcgen05CollectorUsageOp::USE:
+ OS << "use";
+ return;
+ }
+ }
+ llvm_unreachable("printTcgen05CollectorUsageOp called with invalid value for "
+ "immediate argument");
+}
+
} // namespace nvvm
} // namespace llvm
#endif // LLVM_IR_NVVMINTRINSICUTILS_H
diff --git a/llvm/lib/IR/AsmWriter.cpp b/llvm/lib/IR/AsmWriter.cpp
index 95d954f6b8174..49697b02c44e3 100644
--- a/llvm/lib/IR/AsmWriter.cpp
+++ b/llvm/lib/IR/AsmWriter.cpp
@@ -53,6 +53,7 @@
#include "llvm/IR/Instruction.h"
#include "llvm/IR/Instructions.h"
#include "llvm/IR/IntrinsicInst.h"
+#include "llvm/IR/Intrinsics.h"
#include "llvm/IR/LLVMContext.h"
#include "llvm/IR/Metadata.h"
#include "llvm/IR/Module.h"
@@ -4581,12 +4582,38 @@ void AssemblyWriter::printInstruction(const Instruction &I) {
Out << ' ';
writeOperand(Operand, false);
Out << '(';
+ bool HasPrettyPrintedArgs =
+ isa<IntrinsicInst>(CI) &&
+ Intrinsic::hasPrettyPrintedArgs(CI->getIntrinsicID());
+
ListSeparator LS;
- for (unsigned op = 0, Eop = CI->arg_size(); op < Eop; ++op) {
- Out << LS;
- writeParamOperand(CI->getArgOperand(op), PAL.getParamAttrs(op));
- }
+ if (HasPrettyPrintedArgs) {
+ Function *CalledFunc = CI->getCalledFunction();
+ auto PrintArgComment = [&](unsigned ArgNo) {
+ const Constant *ConstArg = dyn_cast<Constant>(CI->getArgOperand(ArgNo));
+ if (!ConstArg)
+ return;
+ std::string ArgComment;
+ raw_string_ostream ArgCommentStream(ArgComment);
+ Intrinsic::ID IID = CalledFunc->getIntrinsicID();
+ Intrinsic::printImmArg(IID, ArgNo, ArgCommentStream, ConstArg);
+ if (ArgComment.empty())
+ return;
+ Out << "/* " << ArgComment << " */ ";
+ };
+ for (unsigned ArgNo = 0, NumArgs = CI->arg_size(); ArgNo < NumArgs;
+ ++ArgNo) {
+ Out << LS;
+ PrintArgComment(ArgNo);
+ writeParamOperand(CI->getArgOperand(ArgNo), PAL.getParamAttrs(ArgNo));
+ }
+ } else {
+ for (unsigned op = 0, Eop = CI->arg_size(); op < Eop; ++op) {
+ Out << LS;
+ writeParamOperand(CI->getArgOperand(op), PAL.getParamAttrs(op));
+ }
+ }
// Emit an ellipsis if this is a musttail call in a vararg function. This
// is only to aid readability, musttail calls forward varargs by default.
if (CI->isMustTailCall() && CI->getParent() &&
@@ -5010,12 +5037,10 @@ void AssemblyWriter::printUseLists(const Function *F) {
//===----------------------------------------------------------------------===//
void Function::print(raw_ostream &ROS, AssemblyAnnotationWriter *AAW,
- bool ShouldPreserveUseListOrder,
- bool IsForDebug) const {
+ bool ShouldPreserveUseListOrder, bool IsForDebug) const {
SlotTracker SlotTable(this->getParent());
formatted_raw_ostream OS(ROS);
- AssemblyWriter W(OS, SlotTable, this->getParent(), AAW,
- IsForDebug,
+ AssemblyWriter W(OS, SlotTable, this->getParent(), AAW, IsForDebug,
ShouldPreserveUseListOrder);
W.printFunction(this);
}
diff --git a/llvm/lib/IR/Intrinsics.cpp b/llvm/lib/IR/Intrinsics.cpp
index 526800e217399..859689b9cf168 100644
--- a/llvm/lib/IR/Intrinsics.cpp
+++ b/llvm/lib/IR/Intrinsics.cpp
@@ -32,6 +32,7 @@
#include "llvm/IR/IntrinsicsX86.h"
#include "llvm/IR/IntrinsicsXCore.h"
#include "llvm/IR/Module.h"
+#include "llvm/IR/NVVMIntrinsicUtils.h"
#include "llvm/IR/Type.h"
using namespace llvm;
@@ -601,6 +602,12 @@ bool Intrinsic::isOverloaded(ID id) {
#undef GET_INTRINSIC_OVERLOAD_TABLE
}
+bool Intrinsic::hasPrettyPrintedArgs(ID id){
+#define GET_INTRINSIC_PRETTY_PRINT_TABLE
+#include "llvm/IR/IntrinsicImpl.inc"
+#undef GET_INTRINSIC_PRETTY_PRINT_TABLE
+}
+
/// Table of per-target intrinsic name tables.
#define GET_INTRINSIC_TARGET_DATA
#include "llvm/IR/IntrinsicImpl.inc"
@@ -1142,3 +1149,7 @@ Intrinsic::ID Intrinsic::getDeinterleaveIntrinsicID(unsigned Factor) {
assert(Factor >= 2 && Factor <= 8 && "Unexpected factor");
return InterleaveIntrinsics[Factor - 2].Deinterleave;
}
+
+#define GET_INTRINSIC_PRETTY_PRINT_ARGUMENTS
+#include "llvm/IR/IntrinsicImpl.inc"
+#undef GET_INTRINSIC_PRETTY_PRINT_ARGUMENTS
diff --git a/llvm/test/CodeGen/NVPTX/tcgen05-mma-tensor-formatted.ll b/llvm/test/CodeGen/NVPTX/tcgen05-mma-tensor-formatted.ll
new file mode 100644
index 0000000000000..94d85e954f5da
--- /dev/null
+++ b/llvm/test/CodeGen/NVPTX/tcgen05-mma-tensor-formatted.ll
@@ -0,0 +1,41 @@
+; NOTE: Assertions have been autogenerated by utils/update_llc_test_checks.py UTC_ARGS: --version 6
+; NOTE: This sample test demonstrates the pretty print feature for NVPTX intrinsics
+; RUN: llvm-as < %s | llvm-dis | FileCheck %s
+
+target triple = "nvptx64-nvidia-cuda"
+
+define void @tcgen05_mma_fp16_cta1(ptr addrspace(6) %dtmem, ptr addrspace(6) %atensor, i64 %b, i32 %idesc, i1 %enable_inp_d) {
+ ; CHECK-LABEL: define void @tcgen05_mma_fp16_cta1(
+ ; CHECK: call void @llvm.nvvm.tcgen05.mma.tensor(ptr addrspace(6) %dtmem, ptr addrspace(6) %atensor, i64 %b, i32 %idesc, i1 %enable_inp_d, /* kind=f16 */ i32 0, /* cta_group= */ i32 1, /* collector=discard */ i32 0)
+ call void @llvm.nvvm.tcgen05.mma.tensor(ptr addrspace(6) %dtmem, ptr addrspace(6) %atensor, i64 %b, i32 %idesc, i1 %enable_inp_d, i32 0, i32 1, i32 0)
+
+ ; CHECK: call void @llvm.nvvm.tcgen05.mma.tensor(ptr addrspace(6) %dtmem, ptr addrspace(6) %atensor, i64 %b, i32 %idesc, i1 %enable_inp_d, /* kind=f16 */ i32 0, /* cta_group= */ i32 1, /* collector=lastuse */ i32 1)
+ call void @llvm.nvvm.tcgen05.mma.tensor(ptr addrspace(6) %dtmem, ptr addrspace(6) %atensor, i64 %b, i32 %idesc, i1 %enable_inp_d, i32 0, i32 1, i32 1)
+
+ ; CHECK: call void @llvm.nvvm.tcgen05.mma.tensor(ptr addrspace(6) %dtmem, ptr addrspace(6) %atensor, i64 %b, i32 %idesc, i1 %enable_inp_d, /* kind=f16 */ i32 0, /* cta_group= */ i32 1, /* collector=fill */ i32 2)
+ call void @llvm.nvvm.tcgen05.mma.tensor(ptr addrspace(6) %dtmem, ptr addrspace(6) %atensor, i64 %b, i32 %idesc, i1 %enable_inp_d, i32 0, i32 1, i32 2)
+
+ ; CHECK: call void @llvm.nvvm.tcgen05.mma.tensor(ptr addrspace(6) %dtmem, ptr addrspace(6) %atensor, i64 %b, i32 %idesc, i1 %enable_inp_d, /* kind=f16 */ i32 0, /* cta_group= */ i32 1, /* collector=use */ i32 3)
+ call void @llvm.nvvm.tcgen05.mma.tensor(ptr addrspace(6) %dtmem, ptr addrspace(6) %atensor, i64 %b, i32 %idesc, i1 %enable_inp_d, i32 0, i32 1, i32 3)
+
+ ret void
+}
+
+define void @tcgen05_mma_f8f6f4_cta2(ptr addrspace(6) %dtmem, ptr addrspace(6) %atensor, i64 %b, i32 %idesc, i1 %enable_inp_d) {
+ ; CHECK-LABEL: define void @tcgen05_mma_f8f6f4_cta2(
+ ; CHECK: call void @llvm.nvvm.tcgen05.mma.tensor(ptr addrspace(6) %dtmem, ptr addrspace(6) %atensor, i64 %b, i32 %idesc, i1 %enable_inp_d, /* kind=f8f6f4 */ i32 2, /* cta_group= */ i32 2, /* collector=discard */ i32 0)
+ call void @llvm.nvvm.tcgen05.mma.tensor(ptr addrspace(6) %dtmem, ptr addrspace(6) %atensor, i64 %b, i32 %idesc, i1 %enable_inp_d, i32 2, i32 2, i32 0)
+
+ ; CHECK: call void @llvm.nvvm.tcgen05.mma.tensor(ptr addrspace(6) %dtmem, ptr addrspace(6) %atensor, i64 %b, i32 %idesc, i1 %enable_inp_d, /* kind=f8f6f4 */ i32 2, /* cta_group= */ i32 2, /* collector=lastuse */ i32 1)
+ call void @llvm.nvvm.tcgen05.mma.tensor(ptr addrspace(6) %dtmem, ptr addrspace(6) %atensor, i64 %b, i32 %idesc, i1 %enable_inp_d, i32 2, i32 2, i32 1)
+
+ ; CHECK: call void @llvm.nvvm.tcgen05.mma.tensor(ptr addrspace(6) %dtmem, ptr addrspace(6) %atensor, i64 %b, i32 %idesc, i1 %enable_inp_d, /* kind=f8f6f4 */ i32 2, /* cta_group= */ i32 2, /* collector=fill */ i32 2)
+ call void @llvm.nvvm.tcgen05.mma.tensor(ptr addrspace(6) %dtmem, ptr addrspace(6) %atensor, i64 %b, i32 %idesc, i1 %enable_inp_d, i32 2, i32 2, i32 2)
+
+ ; CHECK: call void @llvm.nvvm.tcgen05.mma.tensor(ptr addrspace(6) %dtmem, ptr addrspace(6) %atensor, i64 %b, i32 %idesc, i1 %enable_inp_d, /* kind=f8f6f4 */ i32 2, /* cta_group= */ i32 2, /* collector=use */ i32 3)
+ call void @llvm.nvvm.tcgen05.mma.tensor(ptr addrspace(6) %dtmem, ptr addrspace(6) %atensor, i64 %b, i32 %idesc, i1 %enable_inp_d, i32 2, i32 2, i32 3)
+
+ ret void
+}
+
+declare void @llvm.nvvm.tcgen05.mma.tensor(ptr addrspace(6), ptr addrspace(6), i64, i32, i1, i32, i32, i32)
diff --git a/llvm/test/TableGen/intrinsic-arginfo.td b/llvm/test/TableGen/intrinsic-arginfo.td
new file mode 100644
index 0000000000000..9a65e2c7da8d6
--- /dev/null
+++ b/llvm/test/TableGen/intrinsic-arginfo.td
@@ -0,0 +1,71 @@
+// RUN: llvm-tblgen -gen-intrinsic-impl -I %p/../../include %s | FileCheck %s
+
+// Test ArgInfo property for pretty-printing intrinsic arguments.
+// This test verifies that TableGen generates the correct pretty-printing code
+// for intrinsics that use the ArgInfo property.
+
+include "llvm/IR/Intrinsics.td"
+
+// Simple intrinsic with two arguments that have ArgInfo.
+def int_dummy_foo_bar : DefaultAttrsIntrinsic<
+ [llvm_i32_ty],
+ [llvm_i32_ty, // data
+ llvm_i32_ty, // mode
+ llvm_i32_ty], // stride
+ [IntrNoMem,
+ ImmArg<ArgIndex<1>>,
+ ArgInfo<ArgIndex<1>, "mode", "printDummyMode">,
+ ArgInfo<ArgIndex<2>, "stride">]>;
+
+// A custom floating point add with rounding and sat mode.
+def int_my_fadd_f32 : DefaultAttrsIntrinsic<
+ [llvm_float_ty],
+ [llvm_float_ty, // a
+ llvm_float_ty, // b
+ llvm_i32_ty, // rounding_mode
+ llvm_i1_ty], // saturation_mode
+ [IntrNoMem,
+ ImmArg<ArgIndex<2>>,
+ ImmArg<ArgIndex<3>>,
+ ArgInfo<ArgIndex<2>, "rounding_mode", "printRoundingMode">,
+ ArgInfo<ArgIndex<3>, "saturation_mode">]>;
+
+// CHECK: #ifdef GET_INTRINSIC_PRETTY_PRINT_TABLE
+// CHECK-NEXT: static constexpr uint8_t PPTable[] = {
+
+// CHECK: #endif // GET_INTRINSIC_PRETTY_PRINT_TABLE
+
+// CHECK: #ifdef GET_INTRINSIC_PRETTY_PRINT_ARGUMENTS
+// CHECK: void Intrinsic::printImmArg(ID IID, unsigned ArgIdx, raw_ostream &OS, const Constant *ImmArgVal) {
+
+// CHECK: case dummy_foo_bar:
+// CHECK-NEXT: switch (ArgIdx) {
+
+// CHECK-NEXT: case 1:
+// CHECK-NEXT: OS << "mode=";
+// CHECK-NEXT: printDummyMode(OS, ImmArgVal);
+// CHECK-NEXT: return;
+
+// CHECK-NEXT: case 2:
+// CHECK-NEXT: OS << "stride=";
+// CHECK-NEXT: return;
+
+// CHECK-NEXT: }
+// CHECK-NEXT: break;
+
+// CHECK: case my_fadd_f32:
+// CHECK-NEXT: switch (ArgIdx) {
+
+// CHECK-NEXT: case 2:
+// CHECK-NEXT: OS << "rounding_mode=";
+// CHECK-NEXT: printRoundingMode(OS, ImmArgVal);
+// CHECK-NEXT: return;
+
+// CHECK-NEXT: case 3:
+// CHECK-NEXT: OS << "saturation_mode=";
+// CHECK-NEXT: return;
+
+// CHECK-NEXT: }
+// CHECK-NEXT: break;
+
+// CHECK: #endif // GET_INTRINSIC_PRETTY_PRINT_ARGUMENTS
diff --git a/llvm/utils/TableGen/Basic/CodeGenIntrinsics.cpp b/llvm/utils/TableGen/Basic/CodeGenIntrinsics.cpp
index cd866469792a2..c863a5f60ca56 100644
--- a/llvm/utils/TableGen/Basic/CodeGenIntrinsics.cpp
+++ b/llvm/utils/TableGen/Basic/CodeGenIntrinsics.cpp
@@ -447,6 +447,14 @@ void CodeGenIntrinsic::setProperty(const Record *R) {
int64_t Lower = R->getValueAsInt("Lower");
int64_t Upper = R->getValueAsInt("Upper");
addArgAttribute(ArgNo, Range, Lower, Upper);
+ } else if (R->isSubClassOf("ArgInfo")) {
+ unsigned ArgNo = R->getValueAsInt("ArgNo");
+ if (ArgNo < 1)
+ PrintFatalError(R->getLoc(),
+ "ArgInfo requires ArgNo >= 1 (0 is return value)");
+ StringRef ArgName = R->getValueAsString("ArgName");
+ StringRef FuncName = R->getValueAsString("FunctionName");
+ addPrettyPrintFunction(ArgNo - 1, ArgName, FuncName);
} else {
llvm_unreachable("Unknown property!");
}
@@ -474,3 +482,18 @@ void CodeGenIntrinsic::addArgAttribute(unsigned Idx, ArgAttrKind AK, uint64_t V,
ArgumentAttributes.resize(Idx + 1);
ArgumentAttributes[Idx].emplace_back(AK, V, V2);
}
+
+void CodeGenIntrinsic::addPrettyPrintFunction(unsigned ArgIdx,
+ StringRef ArgName,
+ StringRef FuncName) {
+ auto It = llvm::find_if(PrettyPrintFunctions, [ArgIdx](const auto &Info) {
+ return Info.ArgIdx == ArgIdx;
+ });
+ if (It != PrettyPrintFunctions.end()) {
+ PrintFatalError(TheDef->getLoc(), "ArgInfo for argument " + Twine(ArgIdx) +
+ " is already defined as '" +
+ It->FuncName + "'");
+ return;
+ }
+ PrettyPrintFunctions.emplace_back(ArgIdx, ArgName, FuncName);
+}
diff --git a/llvm/utils/TableGen/Basic/CodeGenIntrinsics.h b/llvm/utils/TableGen/Basic/CodeGenIntrinsics.h
index 2e86149514f46..a7e51db803006 100644
--- a/llvm/utils/TableGen/Basic/CodeGenIntrinsics.h
+++ b/llvm/utils/TableGen/Basic/CodeGenIntrinsics.h
@@ -149,6 +149,22 @@ struct CodeGenIntrinsic {
void addArgAttribute(unsigned Idx, ArgAttrKind AK, uint64_t V = 0,
uint64_t V2 = 0);
+ /// Structure to store pretty print and argument information.
+ struct PrettyPrintArgInfo {
+ unsigned ArgIdx;
+ StringRef ArgName;
+ StringRef FuncName;
+
+ PrettyPrintArgInfo(unsigned Idx, StringRef Name, StringRef Func)
+ : ArgIdx(Idx), ArgName(Name), FuncName(Func) {}
+ };
+
+ /// Vector that stores ArgInfo (ArgIndex, ArgName, FunctionName).
+ SmallVector<PrettyPrintArgInfo> PrettyPrintFunctions;
+
+ void addPrettyPrintFunction(unsigned ArgIdx, StringRef ArgName,
+ StringRef FuncName);
+
bool hasProperty(enum SDNP Prop) const { return Properties & (1 << Prop); }
/// Goes through all IntrProperties that have IsDefault value set and sets
diff --git a/llvm/utils/TableGen/Basic/IntrinsicEmitter.cpp b/llvm/utils/TableGen/Basic/IntrinsicEmitter.cpp
index 75dffb18fca5a..75be4bb8bf3f7 100644
--- a/llvm/utils/TableGen/Basic/IntrinsicEmitter.cpp
+++ b/llvm/utils/TableGen/Basic/IntrinsicEmitter.cpp
@@ -60,8 +60,12 @@ class IntrinsicEmitter {
raw_ostream &OS);
void EmitIntrinsicToOverloadTable(const CodeGenIntrinsicTable &Ints,
raw_ostream &OS);
+ void EmitIntrinsicToPrettyPrintTable(const CodeGenIntrinsicTable &Ints,
+ raw_ostream &OS);
void EmitGenerator(const CodeGenIntrinsicTable &Ints, raw_ostream &OS);
void EmitAttributes(const CodeGenIntrinsicTable &Ints, raw_ostream &OS);
+ void EmitPrettyPrintArguments(const CodeGenIntrinsicTable &Ints,
+ raw_ostream &OS);
void EmitIntrinsicToBuiltinMap(const CodeGenIntrinsicTable &Ints,
bool IsClang, raw_ostream &OS);
};
@@ -109,6 +113,12 @@ void IntrinsicEmitter::run(raw_ostream &OS, bool Enums) {
// Emit the intrinsic parameter attributes.
EmitAttributes(Ints, OS);
+ // Emit the intrinsic ID -> pretty print table.
+ EmitIntrinsicToPrettyPrintTable(Ints, OS);
+
+ // Emit Pretty Print attribute.
+ EmitPrettyPrintArguments(Ints, OS);
+
// Emit code to translate Clang builtins into LLVM intrinsics.
EmitIntrinsicToBuiltinMap(Ints, true, OS);
@@ -805,6 +815,71 @@ AttributeSet Intrinsic::getFnAttributes(LLVMContext &C, ID id) {{
NoFunctionAttrsID);
}
+void IntrinsicEmitter::EmitIntrinsicToPrettyPrintTable(
+ const CodeGenIntrinsicTable &Ints, raw_ostream &OS) {
+ OS << R"(// Intrinsic ID to pretty print bitset.
+)"
+ R"(#ifdef GET_INTRINSIC_PRETTY_PRINT_TABLE
+static constexpr uint8_t PPTable[] = {
+ 0
+ )";
+ int CountPerLine = 0;
+ for (auto [I, Int] : enumerate(Ints)) {
+ size_t Idx = I + 1;
+
+ if (Idx % 8 == 0) {
+ OS << ", 0";
+ CountPerLine++;
+ if (CountPerLine == 8) {
+ OS << "\n ";
+ CountPerLine = 0;
+ }
+ }
+ if (!Int.PrettyPrintFunctions.empty())
+ OS << " | (1<<" << Idx % 8 << ')';
+ }
+ OS << "\n};\n\n";
+ OS << "return (PPTable[id/8] & (1 << (id%8))) != 0;\n";
+ OS << "#endif // GET_INTRINSIC_PRETTY_PRINT_TABLE\n\n";
+}
+
+void IntrinsicEmitter::EmitPrettyPrintArguments(
+ const CodeGenIntrinsicTable &Ints, raw_ostream &OS) {
+ OS << R"(
+#ifdef GET_INTRINSIC_PRETTY_PRINT_ARGUMENTS
+
+void Intrinsic::printImmArg(ID IID, unsigned ArgIdx, raw_ostream &OS, const Constant *ImmArgVal) {
+ using namespace Intrinsic;
+ switch (IID) {
+)";
+
+ for (const auto &Int : Ints) {
+ if (Int.PrettyPrintFunctions.empty())
+ continue;
+
+ OS << " case " << Int.EnumName << ":\n";
+ OS << " switch (ArgIdx) {\n";
+ for (const auto &Info : Int.PrettyPrintFunctions) {
+ OS << " case " << Info.ArgIdx << ":\n";
+ OS << " OS << \"" << Info.ArgName << "=\";\n";
+ if (!Info.FuncName.empty()) {
+ OS << " ";
+ if (!Int.TargetPrefix.empty())
+ OS << Int.TargetPrefix << "::";
+ OS << Info.FuncName << "(OS, ImmArgVal);\n";
+ }
+ OS << " return;\n";
+ }
+ OS << " }\n";
+ OS << " break;\n";
+ }
+ OS << R"(
+ }
+}
+#endif // GET_INTRINSIC_PRETTY_PRINT_ARGUMENTS
+)";
+}
+
void IntrinsicEmitter::EmitIntrinsicToBuiltinMap(
const CodeGenIntrinsicTable &Ints, bool IsClang, raw_ostream &OS) {
StringRef CompilerName = IsClang ? "Clang" : "MS";
>From 9263626a015949cdfead61ca75922478a72a8abb Mon Sep 17 00:00:00 2001
From: Dharuni R Acharya <dharunira at nvidia.com>
Date: Fri, 24 Oct 2025 08:47:07 +0000
Subject: [PATCH 2/4] ArgInfo with ArgName and ImmArgPrinter
---
llvm/include/llvm/IR/Intrinsics.td | 27 +++++++++++++------
llvm/include/llvm/IR/IntrinsicsNVVM.td | 7 +++++
llvm/test/TableGen/intrinsic-arginfo.td | 8 +++---
.../TableGen/Basic/CodeGenIntrinsics.cpp | 19 +++++++++++--
4 files changed, 47 insertions(+), 14 deletions(-)
diff --git a/llvm/include/llvm/IR/Intrinsics.td b/llvm/include/llvm/IR/Intrinsics.td
index ebfc0cddec306..2b787c0e9a6e3 100644
--- a/llvm/include/llvm/IR/Intrinsics.td
+++ b/llvm/include/llvm/IR/Intrinsics.td
@@ -87,14 +87,6 @@ class NoUndef<AttrIndex idx> : IntrinsicProperty {
int ArgNo = idx.Value;
}
-// ArgInfo - The specified argument has an argument name and an optional argument printing
-// function for diagnostic output.
-class ArgInfo<AttrIndex idx, string argname, string funcname = ""> : IntrinsicProperty {
- int ArgNo = idx.Value;
- string ArgName = argname;
- string FunctionName = funcname;
-}
-
// NonNull - The return value or specified argument is not null.
class NonNull<AttrIndex idx> : IntrinsicProperty {
int ArgNo = idx.Value;
@@ -150,6 +142,25 @@ class Range<AttrIndex idx, int lower, int upper> : IntrinsicProperty {
int Upper = upper;
}
+// ArgProperty - Base class for argument properties that can be specified in ArgInfo.
+class ArgProperty;
+
+// ArgName - Specifies the name of an argument for pretty-printing.
+class ArgName<string name> : ArgProperty {
+ string Name = name;
+}
+
+// ImmArgPrinter - Specifies a custom printer function for immediate arguments.
+class ImmArgPrinter<string funcname> : ArgProperty {
+ string FuncName = funcname;
+}
+
+// ArgInfo - The specified argument has properties defined by a list of ArgProperty objects.
+class ArgInfo<AttrIndex idx, list<ArgProperty> arg_properties> : IntrinsicProperty {
+ int ArgNo = idx.Value;
+ list<ArgProperty> Properties = arg_properties;
+}
+
def IntrNoReturn : IntrinsicProperty;
// Applied by default.
diff --git a/llvm/include/llvm/IR/IntrinsicsNVVM.td b/llvm/include/llvm/IR/IntrinsicsNVVM.td
index 026a3f3bcf2ed..2bb92041fccb3 100644
--- a/llvm/include/llvm/IR/IntrinsicsNVVM.td
+++ b/llvm/include/llvm/IR/IntrinsicsNVVM.td
@@ -2971,6 +2971,13 @@ foreach sp = [0, 1] in {
]
);
+ defvar intrinsic_properties = !if(is_target_intrinsic,
+ !listconcat(base_properties,
+ [ArgInfo<ArgIndex<nargs>, [ArgName<"kind">, ImmArgPrinter<"printTcgen05MMAKind">]>,
+ ArgInfo<ArgIndex<!add(nargs, 1)>, [ArgName<"cta_group">]>,
+ ArgInfo<ArgIndex<!add(nargs, 2)>, [ArgName<"collector">, ImmArgPrinter<"printTcgen05CollectorUsageOp">]>]),
+ base_properties);
+
def mma.record_name:
DefaultAttrsIntrinsicFlags<[], args, flags, intrinsic_properties,
mma.intr_name>;
diff --git a/llvm/test/TableGen/intrinsic-arginfo.td b/llvm/test/TableGen/intrinsic-arginfo.td
index 9a65e2c7da8d6..eab1f5e032bc3 100644
--- a/llvm/test/TableGen/intrinsic-arginfo.td
+++ b/llvm/test/TableGen/intrinsic-arginfo.td
@@ -14,8 +14,8 @@ def int_dummy_foo_bar : DefaultAttrsIntrinsic<
llvm_i32_ty], // stride
[IntrNoMem,
ImmArg<ArgIndex<1>>,
- ArgInfo<ArgIndex<1>, "mode", "printDummyMode">,
- ArgInfo<ArgIndex<2>, "stride">]>;
+ ArgInfo<ArgIndex<1>, [ArgName<"mode">, ImmArgPrinter<"printDummyMode">]>,
+ ArgInfo<ArgIndex<2>, [ArgName<"stride">]>]>;
// A custom floating point add with rounding and sat mode.
def int_my_fadd_f32 : DefaultAttrsIntrinsic<
@@ -27,8 +27,8 @@ def int_my_fadd_f32 : DefaultAttrsIntrinsic<
[IntrNoMem,
ImmArg<ArgIndex<2>>,
ImmArg<ArgIndex<3>>,
- ArgInfo<ArgIndex<2>, "rounding_mode", "printRoundingMode">,
- ArgInfo<ArgIndex<3>, "saturation_mode">]>;
+ ArgInfo<ArgIndex<2>, [ArgName<"rounding_mode">, ImmArgPrinter<"printRoundingMode">]>,
+ ArgInfo<ArgIndex<3>, [ArgName<"saturation_mode">]>]>;
// CHECK: #ifdef GET_INTRINSIC_PRETTY_PRINT_TABLE
// CHECK-NEXT: static constexpr uint8_t PPTable[] = {
diff --git a/llvm/utils/TableGen/Basic/CodeGenIntrinsics.cpp b/llvm/utils/TableGen/Basic/CodeGenIntrinsics.cpp
index c863a5f60ca56..7f605a6d97983 100644
--- a/llvm/utils/TableGen/Basic/CodeGenIntrinsics.cpp
+++ b/llvm/utils/TableGen/Basic/CodeGenIntrinsics.cpp
@@ -452,8 +452,23 @@ void CodeGenIntrinsic::setProperty(const Record *R) {
if (ArgNo < 1)
PrintFatalError(R->getLoc(),
"ArgInfo requires ArgNo >= 1 (0 is return value)");
- StringRef ArgName = R->getValueAsString("ArgName");
- StringRef FuncName = R->getValueAsString("FunctionName");
+ const ListInit *Properties = R->getValueAsListInit("Properties");
+ StringRef ArgName;
+ StringRef FuncName;
+
+ for (const Init *PropInit : Properties->getElements()) {
+ if (const DefInit *PropDef = dyn_cast<DefInit>(PropInit)) {
+ const Record *PropRec = PropDef->getDef();
+
+ if (PropRec->isSubClassOf("ArgName"))
+ ArgName = PropRec->getValueAsString("Name");
+ else if (PropRec->isSubClassOf("ImmArgPrinter"))
+ FuncName = PropRec->getValueAsString("FuncName");
+ else
+ PrintFatalError(PropRec->getLoc(),
+ "Unknown ArgProperty type: " + PropRec->getName());
+ }
+ }
addPrettyPrintFunction(ArgNo - 1, ArgName, FuncName);
} else {
llvm_unreachable("Unknown property!");
>From 51f2df141bfa9727b77d0599d841f51641655584 Mon Sep 17 00:00:00 2001
From: Dharuni R Acharya <dharunira at nvidia.com>
Date: Fri, 24 Oct 2025 10:19:19 +0000
Subject: [PATCH 3/4] Fix Formatting
---
llvm/utils/TableGen/Basic/CodeGenIntrinsics.cpp | 10 +++++-----
1 file changed, 5 insertions(+), 5 deletions(-)
diff --git a/llvm/utils/TableGen/Basic/CodeGenIntrinsics.cpp b/llvm/utils/TableGen/Basic/CodeGenIntrinsics.cpp
index 7f605a6d97983..fae19f57f6d23 100644
--- a/llvm/utils/TableGen/Basic/CodeGenIntrinsics.cpp
+++ b/llvm/utils/TableGen/Basic/CodeGenIntrinsics.cpp
@@ -455,16 +455,16 @@ void CodeGenIntrinsic::setProperty(const Record *R) {
const ListInit *Properties = R->getValueAsListInit("Properties");
StringRef ArgName;
StringRef FuncName;
-
+
for (const Init *PropInit : Properties->getElements()) {
if (const DefInit *PropDef = dyn_cast<DefInit>(PropInit)) {
const Record *PropRec = PropDef->getDef();
-
- if (PropRec->isSubClassOf("ArgName"))
+
+ if (PropRec->isSubClassOf("ArgName"))
ArgName = PropRec->getValueAsString("Name");
- else if (PropRec->isSubClassOf("ImmArgPrinter"))
+ else if (PropRec->isSubClassOf("ImmArgPrinter"))
FuncName = PropRec->getValueAsString("FuncName");
- else
+ else
PrintFatalError(PropRec->getLoc(),
"Unknown ArgProperty type: " + PropRec->getName());
}
>From 8eb999c522ff87f91c22fd9bffcc52ca273ce16a Mon Sep 17 00:00:00 2001
From: Dharuni R Acharya <dharunira at nvidia.com>
Date: Fri, 31 Oct 2025 04:54:17 +0000
Subject: [PATCH 4/4] Emitter Updates
---
llvm/utils/TableGen/Basic/IntrinsicEmitter.cpp | 3 ++-
1 file changed, 2 insertions(+), 1 deletion(-)
diff --git a/llvm/utils/TableGen/Basic/IntrinsicEmitter.cpp b/llvm/utils/TableGen/Basic/IntrinsicEmitter.cpp
index 75be4bb8bf3f7..bac8204de929c 100644
--- a/llvm/utils/TableGen/Basic/IntrinsicEmitter.cpp
+++ b/llvm/utils/TableGen/Basic/IntrinsicEmitter.cpp
@@ -873,7 +873,8 @@ void Intrinsic::printImmArg(ID IID, unsigned ArgIdx, raw_ostream &OS, const Cons
OS << " }\n";
OS << " break;\n";
}
- OS << R"(
+ OS << R"( default:
+ break;
}
}
#endif // GET_INTRINSIC_PRETTY_PRINT_ARGUMENTS
More information about the llvm-commits
mailing list