[llvm] [LLVM][Intrinsics] Add Default arguments support for Intrinsics in TableGen (PR #198557)
via llvm-commits
llvm-commits at lists.llvm.org
Tue May 19 08:53:54 PDT 2026
llvmorg-github-actions[bot] wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-backend-nvptx
Author: Varad Rahul Kamthe (varadk27)
<details>
<summary>Changes</summary>
This patch adds LLVM infrastructure to support default argument values for intrinsics, modeled after C++ default arguments.
The motivation is to simplify the evolution of intrinsic signatures by removing the hand-written AutoUpgrade boilerplate that intrinsic authors must repeat for every new trailing parameter.
This feature adds a TableGen property `DefaultIntArg<ArgIndex<N>, value>` that lets an intrinsic author declare a default value alongside an `ImmArg` parameter. Defaults are applied through two complementary paths: AutoUpgrade fills missing trailing arguments when older `.ll` or `.bc` IR is loaded, and `IRBuilder::CreateIntrinsic` fills them when C++ code constructs intrinsic calls programmatically.
Link to the RFC, where this feature was discussed:
---
Patch is 29.27 KiB, truncated to 20.00 KiB below, full version: https://github.com/llvm/llvm-project/pull/198557.diff
18 Files Affected:
- (modified) llvm/include/llvm/IR/Intrinsics.h (+9)
- (modified) llvm/include/llvm/IR/Intrinsics.td (+10)
- (modified) llvm/include/llvm/IR/IntrinsicsNVVM.td (+49)
- (modified) llvm/lib/IR/AutoUpgrade.cpp (+92)
- (modified) llvm/lib/IR/IRBuilder.cpp (+33-2)
- (modified) llvm/lib/IR/Intrinsics.cpp (+8)
- (added) llvm/test/CodeGen/NVPTX/default-arg-cp-async-bulk-dummy.ll (+14)
- (added) llvm/test/CodeGen/NVPTX/default-arg-explicit-value-preserved.ll (+13)
- (added) llvm/test/CodeGen/NVPTX/default-arg-move-i32-dummy.ll (+14)
- (added) llvm/test/CodeGen/NVPTX/default-arg-multiple-trailing.ll (+14)
- (added) llvm/test/CodeGen/NVPTX/default-arg-nanosleep-dummy.ll (+14)
- (added) llvm/test/CodeGen/NVPTX/default-arg-partial-fill.ll (+14)
- (added) llvm/test/CodeGen/NVPTX/default-arg-single-val.ll (+15)
- (added) llvm/test/CodeGen/NVPTX/default-arg-tcgen05-fence-dummy.ll (+14)
- (modified) llvm/unittests/IR/IRBuilderTest.cpp (+64)
- (modified) llvm/utils/TableGen/Basic/CodeGenIntrinsics.cpp (+49)
- (modified) llvm/utils/TableGen/Basic/CodeGenIntrinsics.h (+5)
- (modified) llvm/utils/TableGen/Basic/IntrinsicEmitter.cpp (+57)
``````````diff
diff --git a/llvm/include/llvm/IR/Intrinsics.h b/llvm/include/llvm/IR/Intrinsics.h
index af97285fcee70..40fc0544b6600 100644
--- a/llvm/include/llvm/IR/Intrinsics.h
+++ b/llvm/include/llvm/IR/Intrinsics.h
@@ -91,6 +91,15 @@ LLVM_ABI bool isTriviallyScalarizable(ID id);
/// Returns true if the intrinsic has pretty printed immediate arguments.
LLVM_ABI bool hasPrettyPrintedArgs(ID id);
+/// Returns true if the intrinsic has any arguments with default values.
+/// Used by AutoUpgrade to decide whether to call getDefaultArgValue().
+LLVM_ABI bool hasDefaultArgs(ID id);
+
+/// Returns the default value for argument ArgIdx of intrinsic IID.
+/// Returns std::nullopt if that argument has no default.
+/// Supported types: all integer widths (i1/i8/i16/i32/i64).
+LLVM_ABI std::optional<int64_t> getDefaultArgValue(ID IID, unsigned ArgIdx);
+
/// 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);
diff --git a/llvm/include/llvm/IR/Intrinsics.td b/llvm/include/llvm/IR/Intrinsics.td
index 993ddd7e33701..2cb2ba79c3c48 100644
--- a/llvm/include/llvm/IR/Intrinsics.td
+++ b/llvm/include/llvm/IR/Intrinsics.td
@@ -180,6 +180,16 @@ class ArgInfo<ArgIndex idx, list<ArgProperty> arg_properties> : IntrinsicPropert
list<ArgProperty> Properties = arg_properties;
}
+// DefaultIntArg - Attach an integer default value to an intrinsic argument.
+// The argument must be at the END of the parameter list.
+// Supported param types: llvm_i1_ty (use 0/1 or true/false), llvm_i8_ty, llvm_i16_ty, llvm_i32_ty,
+// llvm_i64_ty.
+// Write any integer literal as the value.
+class DefaultIntArg<ArgIndex idx, int val> : IntrinsicProperty {
+ int ArgNo = idx.Value; // 1-based internally; parser subtracts 1
+ int DefaultVal = val;
+}
+
def IntrNoReturn : IntrinsicProperty;
// Applied by default.
diff --git a/llvm/include/llvm/IR/IntrinsicsNVVM.td b/llvm/include/llvm/IR/IntrinsicsNVVM.td
index cc245d4e17f5c..61ad479308f7c 100644
--- a/llvm/include/llvm/IR/IntrinsicsNVVM.td
+++ b/llvm/include/llvm/IR/IntrinsicsNVVM.td
@@ -1301,6 +1301,37 @@ let TargetPrefix = "nvvm" in {
DefaultAttrsIntrinsic<[], [llvm_i32_ty],
[IntrConvergent, IntrNoMem, IntrHasSideEffects]>;
+ //
+ // Test intrinsic for default-arg feature: three required i32 args plus a
+ // fourth i32 with DefaultIntArg = 255.
+ //
+ def int_nvvm_test_add3 : NVVMBuiltin,
+ NVVMPureIntrinsic<[llvm_i32_ty],
+ [llvm_i32_ty, llvm_i32_ty, llvm_i32_ty, llvm_i32_ty],
+ [ImmArg<ArgIndex<3>>,
+ DefaultIntArg<ArgIndex<3>, 255>]>;
+
+ //
+ // Test intrinsic for default-arg feature: four required i32 args plus two
+ // trailing args (i64, i32) with DefaultIntArg = 50 and 14.
+ //
+ def int_nvvm_test_add4 : NVVMBuiltin,
+ NVVMPureIntrinsic<[llvm_i32_ty],
+ [llvm_i32_ty, llvm_i32_ty, llvm_i32_ty, llvm_i32_ty,
+ llvm_i64_ty, llvm_i32_ty],
+ [ImmArg<ArgIndex<4>>, ImmArg<ArgIndex<5>>,
+ DefaultIntArg<ArgIndex<4>, 50>,
+ DefaultIntArg<ArgIndex<5>, 14>]>;
+
+ //
+ // Dummy based on int_nvvm_nanosleep. Adds i1 flag with default = false.
+ //
+ def int_nvvm_nanosleep_dummy : NVVMBuiltin,
+ DefaultAttrsIntrinsic<[], [llvm_i32_ty, llvm_i1_ty],
+ [IntrConvergent, IntrNoMem, IntrHasSideEffects,
+ ImmArg<ArgIndex<1>>,
+ DefaultIntArg<ArgIndex<1>, false>]>;
+
//
// Performance Monitor Events (pm events) intrinsics
//
@@ -2064,6 +2095,12 @@ def int_nvvm_cp_async_bulk_commit_group : Intrinsic<[]>;
def int_nvvm_cp_async_bulk_wait_group :
Intrinsic<[], [llvm_i32_ty], [ImmArg<ArgIndex<0>>]>;
+// Dummy based on cp_async_bulk_wait_group. Adds i1 flag with default = false.
+def int_nvvm_cp_async_bulk_wait_group_dummy :
+ Intrinsic<[], [llvm_i32_ty, llvm_i1_ty],
+ [ImmArg<ArgIndex<1>>,
+ DefaultIntArg<ArgIndex<1>, false>]>;
+
def int_nvvm_cp_async_bulk_wait_group_read :
Intrinsic<[], [llvm_i32_ty], [ImmArg<ArgIndex<0>>]>;
@@ -2198,6 +2235,12 @@ let IntrProperties = [IntrNoMem] in {
def int_nvvm_move_ptr : DefaultAttrsIntrinsic<[llvm_anyptr_ty], [llvm_anyptr_ty]>;
}
+// Dummy based on int_nvvm_move_i32. Adds i64 second arg with default = 99.
+def int_nvvm_move_i32_dummy :
+ DefaultAttrsIntrinsic<[llvm_i32_ty], [llvm_i32_ty, llvm_i64_ty],
+ [ImmArg<ArgIndex<1>>,
+ DefaultIntArg<ArgIndex<1>, 99>]>;
+
// For getting the handle from a texture or surface variable
def int_nvvm_texsurf_handle
: NVVMPureIntrinsic<[llvm_i64_ty], [llvm_metadata_ty, llvm_anyptr_ty]>;
@@ -3180,6 +3223,12 @@ def int_nvvm_tcgen05_fence_before_thread_sync : Intrinsic<[], [],
def int_nvvm_tcgen05_fence_after_thread_sync : Intrinsic<[], [],
[IntrNoMem, IntrHasSideEffects]>;
+// Dummy based on tcgen05_fence. Two i32 args + i1 flag with default = false.
+def int_nvvm_tcgen05_fence_dummy :
+ Intrinsic<[], [llvm_i32_ty, llvm_i32_ty, llvm_i1_ty],
+ [IntrConvergent, ImmArg<ArgIndex<2>>,
+ DefaultIntArg<ArgIndex<2>, false>]>;
+
// Tcgen05 cp intrinsics
foreach cta_group = ["cg1", "cg2"] in {
foreach src_fmt = ["", "b6x16_p32", "b4x16_p64"] in {
diff --git a/llvm/lib/IR/AutoUpgrade.cpp b/llvm/lib/IR/AutoUpgrade.cpp
index 5fed947f9314d..52c97f16d4095 100644
--- a/llvm/lib/IR/AutoUpgrade.cpp
+++ b/llvm/lib/IR/AutoUpgrade.cpp
@@ -1278,6 +1278,36 @@ static bool convertIntrinsicValidType(StringRef Name,
return false;
}
+static bool upgradeDeclWithDefaultArgs(Function *F, Function *&NewFn) {
+ // Look up the intrinsic ID by full name, e.g. "llvm.nvvm.add3.i32"
+ Intrinsic::ID IID = Intrinsic::lookupIntrinsicID(F->getName());
+ if (IID == Intrinsic::not_intrinsic)
+ return false;
+
+ // Fast path: this intrinsic has no default args annotated in .td
+ if (!Intrinsic::hasDefaultArgs(IID))
+ return false;
+
+ if (Intrinsic::isOverloaded(IID))
+ return false;
+
+ // Get the canonical full declaration for this intrinsic.
+ Function *FullDecl = Intrinsic::getOrInsertDeclaration(F->getParent(), IID);
+
+ // If the existing declaration already has all args, nothing to upgrade
+ if (F->arg_size() >= FullDecl->arg_size())
+ return false;
+
+ // Verify every missing trailing arg has a default in the table
+ for (unsigned Idx = F->arg_size(); Idx < FullDecl->arg_size(); ++Idx) {
+ if (!Intrinsic::getDefaultArgValue(IID, Idx))
+ return false;
+ }
+
+ NewFn = FullDecl;
+ return true;
+}
+
static bool upgradeIntrinsicFunction1(Function *F, Function *&NewFn,
bool CanUpgradeDebugIntrinsicsToRecords) {
assert(F && "Illegal to upgrade a non-existent Function.");
@@ -1896,6 +1926,9 @@ static bool upgradeIntrinsicFunction1(Function *F, Function *&NewFn,
// to both detect an intrinsic which needs upgrading, and to provide the
// upgraded form of the intrinsic. We should perhaps have two separate
// functions for this.
+ if (upgradeDeclWithDefaultArgs(F, NewFn))
+ return true;
+
return false;
}
@@ -4978,6 +5011,60 @@ static Value *upgradeConvertIntrinsicCall(StringRef Name, CallBase *CI,
return nullptr;
}
+static bool upgradeCallWithDefaultArgs(CallBase *CI, Function *NewFn,
+ IRBuilder<> &Builder) {
+ Intrinsic::ID IID = NewFn->getIntrinsicID();
+
+ // Fast path: this intrinsic has no default args in the table.
+ if (!Intrinsic::hasDefaultArgs(IID))
+ return false;
+
+ unsigned OldArgCount = CI->arg_size();
+ unsigned NewArgCount = NewFn->arg_size();
+
+ // If the caller already supplied all arguments (or more), nothing to do.
+ // This mirrors C++ semantics: an explicitly-passed value is never overridden.
+ if (OldArgCount >= NewArgCount)
+ return false;
+
+ // Start with the existing arguments from the old call.
+ SmallVector<Value *, 8> NewArgs(CI->args());
+
+ // Fill in each missing trailing argument from the table.
+ FunctionType *NewFT = NewFn->getFunctionType();
+ for (unsigned Idx = OldArgCount; Idx < NewArgCount; ++Idx) {
+ std::optional<int64_t> DefaultVal = Intrinsic::getDefaultArgValue(IID, Idx);
+
+ // If any missing arg has no entry in the table, give up.
+ if (!DefaultVal)
+ return false;
+
+ Type *ParamTy = NewFT->getParamType(Idx);
+
+ // Only integer types are supported (i1, i8, i16, i32, i64).
+ if (!ParamTy->isIntegerTy())
+ return false;
+
+ NewArgs.push_back(
+ ConstantInt::get(ParamTy, static_cast<uint64_t>(*DefaultVal)));
+ }
+
+ // Preserve operand bundles by creating the call with them.
+ SmallVector<OperandBundleDef, 1> OpBundles;
+ CI->getOperandBundlesAsDefs(OpBundles);
+ CallInst *NewCall = Builder.CreateCall(NewFn, NewArgs, OpBundles);
+
+ NewCall->takeName(CI);
+ NewCall->setCallingConv(CI->getCallingConv());
+ NewCall->copyMetadata(*CI);
+ if (auto *OldCI = dyn_cast<CallInst>(CI))
+ NewCall->setTailCallKind(OldCI->getTailCallKind());
+
+ CI->replaceAllUsesWith(NewCall);
+ CI->eraseFromParent();
+ return true;
+}
+
/// Upgrade a call to an old intrinsic. All argument and return casting must be
/// provided to seamlessly integrate with existing context.
void llvm::UpgradeIntrinsicCall(CallBase *CI, Function *NewFn) {
@@ -5083,6 +5170,11 @@ void llvm::UpgradeIntrinsicCall(CallBase *CI, Function *NewFn) {
CallInst *NewCall = nullptr;
switch (NewFn->getIntrinsicID()) {
default: {
+ // Last resort: try the data-driven default-arg upgrade.
+ // Handles any intrinsic annotated with DefaultIntArg
+ // in its .td definition, without needing a dedicated case.
+ if (upgradeCallWithDefaultArgs(CI, NewFn, Builder))
+ return;
DefaultCase();
return;
}
diff --git a/llvm/lib/IR/IRBuilder.cpp b/llvm/lib/IR/IRBuilder.cpp
index 7cdcb60a78aa3..2f978277717de 100644
--- a/llvm/lib/IR/IRBuilder.cpp
+++ b/llvm/lib/IR/IRBuilder.cpp
@@ -909,6 +909,33 @@ CallInst *IRBuilderBase::CreateGCGetPointerOffset(Value *DerivedPtr,
{DerivedPtr}, {}, Name);
}
+// Fill missing trailing arguments from the default-arg table.
+// Returns true if Args was extended; false if no fill was applied (e.g.
+// the intrinsic has no declared defaults, all args were already supplied,
+// or a missing arg has no default in the table).
+static bool fillDefaultArgs(Function *Fn, SmallVectorImpl<Value *> &Args) {
+ Intrinsic::ID IID = Fn->getIntrinsicID();
+ if (IID == Intrinsic::not_intrinsic || !Intrinsic::hasDefaultArgs(IID))
+ return false;
+
+ unsigned Expected = Fn->arg_size();
+ unsigned Supplied = Args.size();
+ if (Supplied >= Expected)
+ return false;
+
+ FunctionType *FT = Fn->getFunctionType();
+ for (unsigned Idx = Supplied; Idx < Expected; ++Idx) {
+ std::optional<int64_t> Default = Intrinsic::getDefaultArgValue(IID, Idx);
+ if (!Default)
+ return false;
+ Type *ParamTy = FT->getParamType(Idx);
+ if (!ParamTy->isIntegerTy())
+ return false;
+ Args.push_back(ConstantInt::get(ParamTy, static_cast<uint64_t>(*Default)));
+ }
+ return true;
+}
+
CallInst *IRBuilderBase::CreateUnaryIntrinsic(Intrinsic::ID ID, Value *V,
FMFSource FMFSource,
const Twine &Name) {
@@ -935,7 +962,9 @@ CallInst *IRBuilderBase::CreateIntrinsic(Intrinsic::ID ID,
ArrayRef<OperandBundleDef> OpBundles) {
Module *M = BB->getModule();
Function *Fn = Intrinsic::getOrInsertDeclaration(M, ID, OverloadTypes);
- return createCallHelper(Fn, Args, Name, FMFSource, OpBundles);
+ SmallVector<Value *, 8> FilledArgs(Args);
+ fillDefaultArgs(Fn, FilledArgs);
+ return createCallHelper(Fn, FilledArgs, Name, FMFSource, OpBundles);
}
CallInst *IRBuilderBase::CreateIntrinsic(Type *RetTy, Intrinsic::ID ID,
@@ -945,7 +974,9 @@ CallInst *IRBuilderBase::CreateIntrinsic(Type *RetTy, Intrinsic::ID ID,
Module *M = BB->getModule();
SmallVector<Type *> ArgTys = llvm::map_to_vector(Args, &Value::getType);
Function *Fn = Intrinsic::getOrInsertDeclaration(M, ID, RetTy, ArgTys);
- return createCallHelper(Fn, Args, Name, FMFSource);
+ SmallVector<Value *, 8> FilledArgs(Args);
+ fillDefaultArgs(Fn, FilledArgs);
+ return createCallHelper(Fn, FilledArgs, Name, FMFSource);
}
CallInst *IRBuilderBase::CreateConstrainedFPBinOp(
diff --git a/llvm/lib/IR/Intrinsics.cpp b/llvm/lib/IR/Intrinsics.cpp
index 008d823315156..bec585e362992 100644
--- a/llvm/lib/IR/Intrinsics.cpp
+++ b/llvm/lib/IR/Intrinsics.cpp
@@ -639,6 +639,11 @@ bool Intrinsic::hasPrettyPrintedArgs(ID id){
#include "llvm/IR/IntrinsicImpl.inc"
}
+bool Intrinsic::hasDefaultArgs(ID id){
+#define GET_INTRINSIC_DEFAULT_ARG_TABLE
+#include "llvm/IR/IntrinsicImpl.inc"
+}
+
/// Table of per-target intrinsic name tables.
#define GET_INTRINSIC_TARGET_DATA
#include "llvm/IR/IntrinsicImpl.inc"
@@ -1337,3 +1342,6 @@ Intrinsic::ID Intrinsic::getDeinterleaveIntrinsicID(unsigned Factor) {
#define GET_INTRINSIC_PRETTY_PRINT_ARGUMENTS
#include "llvm/IR/IntrinsicImpl.inc"
+
+#define GET_INTRINSIC_DEFAULT_ARG_VALUES
+#include "llvm/IR/IntrinsicImpl.inc"
diff --git a/llvm/test/CodeGen/NVPTX/default-arg-cp-async-bulk-dummy.ll b/llvm/test/CodeGen/NVPTX/default-arg-cp-async-bulk-dummy.ll
new file mode 100644
index 0000000000000..7df6494b3a40a
--- /dev/null
+++ b/llvm/test/CodeGen/NVPTX/default-arg-cp-async-bulk-dummy.ll
@@ -0,0 +1,14 @@
+; RUN: opt -S < %s | FileCheck %s
+;
+; Plain Intrinsic<[]> — void-returning, cp_async_bulk style.
+; Old decl: 1 arg (i32). New: 2 args. Arg[1] = i1, default = false.
+
+declare void @llvm.nvvm.cp.async.bulk.wait.group.dummy(i32)
+
+define void @test_cp_async_bulk_wait_group_dummy(i32 %n) {
+entry:
+ call void @llvm.nvvm.cp.async.bulk.wait.group.dummy(i32 %n)
+ ret void
+}
+; CHECK-LABEL: @test_cp_async_bulk_wait_group_dummy
+; CHECK: call void @llvm.nvvm.cp.async.bulk.wait.group.dummy(i32 %n, i1 false)
diff --git a/llvm/test/CodeGen/NVPTX/default-arg-explicit-value-preserved.ll b/llvm/test/CodeGen/NVPTX/default-arg-explicit-value-preserved.ll
new file mode 100644
index 0000000000000..f8510e7b893b3
--- /dev/null
+++ b/llvm/test/CodeGen/NVPTX/default-arg-explicit-value-preserved.ll
@@ -0,0 +1,13 @@
+; RUN: opt -S < %s | FileCheck %s
+;
+; The 4-arg call passes an explicit value. It must be preserved.
+
+declare i32 @llvm.nvvm.test.add3(i32, i32, i32, i32)
+
+define i32 @test_explicit_value_preserved(i32 %a, i32 %b, i32 %c) {
+entry:
+ %r = call i32 @llvm.nvvm.test.add3(i32 %a, i32 %b, i32 %c, i32 42)
+ ret i32 %r
+}
+; CHECK-LABEL: @test_explicit_value_preserved
+; CHECK: call i32 @llvm.nvvm.test.add3(i32 %a, i32 %b, i32 %c, i32 42)
diff --git a/llvm/test/CodeGen/NVPTX/default-arg-move-i32-dummy.ll b/llvm/test/CodeGen/NVPTX/default-arg-move-i32-dummy.ll
new file mode 100644
index 0000000000000..cfc7e0b26a25a
--- /dev/null
+++ b/llvm/test/CodeGen/NVPTX/default-arg-move-i32-dummy.ll
@@ -0,0 +1,14 @@
+; RUN: opt -S < %s | FileCheck %s
+;
+; DefaultAttrsIntrinsic with return type (no NVVMBuiltin).
+; Old decl: 1 arg (i32). New: 2 args. Arg[1] = i64, default = 99.
+
+declare i32 @llvm.nvvm.move.i32.dummy(i32)
+
+define i32 @test_move_i32_dummy(i32 %a) {
+entry:
+ %r = call i32 @llvm.nvvm.move.i32.dummy(i32 %a)
+ ret i32 %r
+}
+; CHECK-LABEL: @test_move_i32_dummy
+; CHECK: call i32 @llvm.nvvm.move.i32.dummy(i32 %a, i64 99)
diff --git a/llvm/test/CodeGen/NVPTX/default-arg-multiple-trailing.ll b/llvm/test/CodeGen/NVPTX/default-arg-multiple-trailing.ll
new file mode 100644
index 0000000000000..c048c79c5d92f
--- /dev/null
+++ b/llvm/test/CodeGen/NVPTX/default-arg-multiple-trailing.ll
@@ -0,0 +1,14 @@
+; RUN: opt -S < %s | FileCheck %s
+;
+; test_add4 has 6 params; last two have defaults (i64 50, i32 14).
+; Old declaration has only 4 args; both missing defaults are inserted.
+
+declare i32 @llvm.nvvm.test.add4(i32, i32, i32, i32)
+
+define i32 @test_multiple_trailing_defaults(i32 %a, i32 %b, i32 %c, i32 %d) {
+entry:
+ %r = call i32 @llvm.nvvm.test.add4(i32 %a, i32 %b, i32 %c, i32 %d)
+ ret i32 %r
+}
+; CHECK-LABEL: @test_multiple_trailing_defaults
+; CHECK: call i32 @llvm.nvvm.test.add4(i32 %a, i32 %b, i32 %c, i32 %d, i64 50, i32 14)
diff --git a/llvm/test/CodeGen/NVPTX/default-arg-nanosleep-dummy.ll b/llvm/test/CodeGen/NVPTX/default-arg-nanosleep-dummy.ll
new file mode 100644
index 0000000000000..95b6f46dfa9fd
--- /dev/null
+++ b/llvm/test/CodeGen/NVPTX/default-arg-nanosleep-dummy.ll
@@ -0,0 +1,14 @@
+; RUN: opt -S < %s | FileCheck %s
+;
+; NVVMBuiltin + DefaultAttrsIntrinsic (void-returning).
+; Old decl: 1 arg (i32). New: 2 args. Arg[1] = i1, default = false.
+
+declare void @llvm.nvvm.nanosleep.dummy(i32)
+
+define void @test_nanosleep_dummy(i32 %ns) {
+entry:
+ call void @llvm.nvvm.nanosleep.dummy(i32 %ns)
+ ret void
+}
+; CHECK-LABEL: @test_nanosleep_dummy
+; CHECK: call void @llvm.nvvm.nanosleep.dummy(i32 %ns, i1 false)
diff --git a/llvm/test/CodeGen/NVPTX/default-arg-partial-fill.ll b/llvm/test/CodeGen/NVPTX/default-arg-partial-fill.ll
new file mode 100644
index 0000000000000..db296f5a1aeda
--- /dev/null
+++ b/llvm/test/CodeGen/NVPTX/default-arg-partial-fill.ll
@@ -0,0 +1,14 @@
+; RUN: opt -S < %s | FileCheck %s
+;
+; Old decl has 5 args (index 5 missing). Arg[4] passed explicitly (i64 99),
+; must NOT be overridden. Arg[5] filled with default i32 14.
+
+declare i32 @llvm.nvvm.test.add4(i32, i32, i32, i32, i64)
+
+define i32 @test_partial_fill(i32 %a, i32 %b, i32 %c, i32 %d) {
+entry:
+ %r = call i32 @llvm.nvvm.test.add4(i32 %a, i32 %b, i32 %c, i32 %d, i64 99)
+ ret i32 %r
+}
+; CHECK-LABEL: @test_partial_fill
+; CHECK: call i32 @llvm.nvvm.test.add4(i32 %a, i32 %b, i32 %c, i32 %d, i64 99, i32 14)
diff --git a/llvm/test/CodeGen/NVPTX/default-arg-single-val.ll b/llvm/test/CodeGen/NVPTX/default-arg-single-val.ll
new file mode 100644
index 0000000000000..62074e02cbfaa
--- /dev/null
+++ b/llvm/test/CodeGen/NVPTX/default-arg-single-val.ll
@@ -0,0 +1,15 @@
+; RUN: opt -S < %s | FileCheck %s
+;
+; Verifies that AutoUpgrade correctly fills in a missing trailing argument
+; with the value declared via DefaultIntArg in the intrinsic's TableGen
+; definition.
+
+declare i32 @llvm.nvvm.test.add3(i32, i32, i32)
+
+define i32 @test_default_single_val(i32 %a, i32 %b, i32 %c) {
+entry:
+ %r = call i32 @llvm.nvvm.test.add3(i32 %a, i32 %b, i32 %c)
+ ret i32 %r
+}
+; CHECK-LABEL: @test_default_single_val
+; CHECK: call i32 @llvm.nvvm.test.add3(i32 %a, i32 %b, i32 %c, i32 255)
diff --git a/llvm/test/CodeGen/NVPTX/default-arg-tcgen05-fence-dummy.ll b/llvm/test/CodeGen/NVPTX/default-arg-tcgen05-fence-dummy.ll
new file mode 100644
index 0000000000000..81f0ad507bd42
--- /dev/null
+++ b/llvm/test/CodeGen/NVPTX/default-arg-tcgen05-fence-dummy.ll
@@ -0,0 +1,14 @@
+; RUN: opt -S < %s | FileCheck %s
+;
+; Plain Intrinsic<[]>, tcgen05 style.
+; Old decl: 2 args (i32, i32). New: 3 args. Arg[2] = i1, default = false.
+
+declare void @llvm.nvvm.tcgen05.fence.dummy(i32, i32)
+
+define void @test_tcgen05_fence_dummy(i32 %flags, i32 %token) {
+entry:
+ call void @llvm.nvvm.tcgen05.fence.dummy(i32 %flags, i32 %token)
+ ret void
+}
+; CHECK-LABEL: @test_tcgen05_fence_dummy
+; CHECK: call void @llvm.nvvm.tcgen05.fence.dummy(i32 %flags, i32 %token, i1 false)
diff --git a/llvm/unittests/IR/IRBuilderTest.cpp b/llvm/unittests/IR/IRBuilderTest.cpp
index b4f1c97f03aca..a4a27d661551d 100644
--- a/llvm/unittests/IR/IRBuilderTest.cpp
+++ b/llvm/unittests/IR/IRBuilderTest.cpp
@@ -15,6 +15,7 @@
#include "llvm/IR/Function.h"
#include "llvm/IR/IntrinsicInst.h"
#include "llvm/IR/IntrinsicsAArch64.h"
+#include "llvm/IR/IntrinsicsNVPTX.h"
#include "llvm/IR/LLVMContext.h"
#include "llvm/IR/MDBuilder.h"
#include "llvm/IR/Module.h"
@@ -160,6 +161,69 @@ TEST_F(IRBuilderTest, Intrinsics) {
EXPECT_EQ(II->getIntrinsicID(), Intrinsic::set_rounding);
}
+TEST_F(IRBuilderTest, IntrinsicWithDefaultArgs) {
+ IRBuilder<> Builder(BB);
+
+ // Use test_add3, which has 4 params with a default on the trailing i32.
+ Value *A = Builder.getInt32(1);
+ Value *B = Builder.getInt32(2);...
[truncated]
``````````
</details>
https://github.com/llvm/llvm-project/pull/198557
More information about the llvm-commits
mailing list