[llvm] [SPIR-V] Don't consider a function be a builtin just by checking name (PR #182776)

Dmitry Sidorov via llvm-commits llvm-commits at lists.llvm.org
Sun Feb 22 14:07:16 PST 2026


https://github.com/MrSidims created https://github.com/llvm/llvm-project/pull/182776

If a function has the same name as a (n OpenCL) builtin, but not matching number of arguments to the builtin - consider it to be a user function.

Fixes: https://github.com/llvm/llvm-project/issues/165237

>From 945f00a6bbe6328ac2023340220c27d585f0b06a Mon Sep 17 00:00:00 2001
From: Dmitry Sidorov <Dmitry.Sidorov at amd.com>
Date: Sun, 22 Feb 2026 18:30:09 +0100
Subject: [PATCH] [SPIR-V] Don't consider a function be a builtin just by
 checking name

If a function has the same name as a (n OpenCL) builtin, but not
matching number of arguments to the builtin - consider it to be a user
function.

Fixes: https://github.com/llvm/llvm-project/issues/165237
---
 llvm/lib/Target/SPIRV/SPIRVBuiltins.cpp              | 11 ++++++++---
 .../CodeGen/SPIRV/user-function-with-builtin-name.ll | 12 ++++++++++++
 2 files changed, 20 insertions(+), 3 deletions(-)
 create mode 100644 llvm/test/CodeGen/SPIRV/user-function-with-builtin-name.ll

diff --git a/llvm/lib/Target/SPIRV/SPIRVBuiltins.cpp b/llvm/lib/Target/SPIRV/SPIRVBuiltins.cpp
index 3123a6f9ffe77..e0a9a83f8e19b 100644
--- a/llvm/lib/Target/SPIRV/SPIRVBuiltins.cpp
+++ b/llvm/lib/Target/SPIRV/SPIRVBuiltins.cpp
@@ -3194,9 +3194,14 @@ std::optional<bool> lowerBuiltin(const StringRef DemangledCall,
     return std::nullopt;
   }
 
-  // TODO: check if the provided args meet the builtin requirments.
-  assert(Args.size() >= Call->Builtin->MinNumArgs &&
-         "Too few arguments to generate the builtin");
+  // Check if the provided args meet the builtin requirements. If not, treat
+  // the call as a regular function call rather than crashing.
+  if (Args.size() < Call->Builtin->MinNumArgs) {
+    LLVM_DEBUG(dbgs() << "Too few arguments for builtin " << DemangledCall
+                      << ": expected at least " << Call->Builtin->MinNumArgs
+                      << ", got " << Args.size() << "\n");
+    return std::nullopt;
+  }
   if (Call->Builtin->MaxNumArgs && Args.size() > Call->Builtin->MaxNumArgs)
     LLVM_DEBUG(dbgs() << "More arguments provided than required!\n");
 
diff --git a/llvm/test/CodeGen/SPIRV/user-function-with-builtin-name.ll b/llvm/test/CodeGen/SPIRV/user-function-with-builtin-name.ll
new file mode 100644
index 0000000000000..9d150b8645cda
--- /dev/null
+++ b/llvm/test/CodeGen/SPIRV/user-function-with-builtin-name.ll
@@ -0,0 +1,12 @@
+; RUN: llc -O0 -mtriple=spirv64-unknown-unknown %s -o - | FileCheck %s
+
+; Verify that calling a function, that looks like a builtin, but has no matching
+; number of arguments is treated as user function.
+
+; CHECK: OpFunctionCall %[[#]] %[[#]]
+define spir_func void @test() {
+  call spir_func void @barrier()
+  ret void
+}
+
+declare spir_func void @barrier()



More information about the llvm-commits mailing list