[llvm] [NVPTX] Improve error diagnostic when handling unknown intrinsics (PR #191194)

Antonio Frighetto via llvm-commits llvm-commits at lists.llvm.org
Tue Apr 14 01:35:08 PDT 2026


https://github.com/antoniofrighetto updated https://github.com/llvm/llvm-project/pull/191194

>From 507a57d97f45026872907be8051063eb3fc51251 Mon Sep 17 00:00:00 2001
From: Antonio Frighetto <me at antoniofrighetto.com>
Date: Thu, 9 Apr 2026 15:04:00 +0200
Subject: [PATCH 1/4] [NVPTX] Fail gracefully when handling unknown intrinsics

Following up on #146726, it may be desirable to gracefully fail the
compilation and let the user know the final PTX generated may be
invalid in presence of unknown NVVM intrinsics, previously not lowered.
---
 llvm/lib/Target/NVPTX/NVPTXAsmPrinter.cpp    |  7 +++++++
 llvm/test/CodeGen/NVPTX/unknown-intrinsic.ll | 11 +++++++++++
 2 files changed, 18 insertions(+)
 create mode 100644 llvm/test/CodeGen/NVPTX/unknown-intrinsic.ll

diff --git a/llvm/lib/Target/NVPTX/NVPTXAsmPrinter.cpp b/llvm/lib/Target/NVPTX/NVPTXAsmPrinter.cpp
index 573671de03ab9..3b3a88ab2ff73 100644
--- a/llvm/lib/Target/NVPTX/NVPTXAsmPrinter.cpp
+++ b/llvm/lib/Target/NVPTX/NVPTXAsmPrinter.cpp
@@ -639,6 +639,13 @@ void NVPTXAsmPrinter::emitDeclarations(const Module &M, raw_ostream &O) {
         continue;
       if (F.getIntrinsicID())
         continue;
+      // If we happen to have an intrinsic previously not lowered, i.e.,
+      // unrecognized intrinsics, we should let the user know that the final
+      // declaration emitted may lead to illegal PTX.
+      if (F.isIntrinsic())
+        report_fatal_error("unknown intrinsic '" + F.getName() +
+                           "' is not supported by this version of the NVPTX "
+                           "backend; may result in invalid PTX.");
       emitDeclaration(&F, O);
       continue;
     }
diff --git a/llvm/test/CodeGen/NVPTX/unknown-intrinsic.ll b/llvm/test/CodeGen/NVPTX/unknown-intrinsic.ll
new file mode 100644
index 0000000000000..d750fc9a6a626
--- /dev/null
+++ b/llvm/test/CodeGen/NVPTX/unknown-intrinsic.ll
@@ -0,0 +1,11 @@
+; NOTE: Assertions have been autogenerated by utils/update_llc_test_checks.py UTC_ARGS: --version 6
+; RUN: not --crash llc -march=nvptx64 -mcpu=sm_80 < %s 2>&1 | FileCheck %s
+
+; CHECK: LLVM ERROR: unknown intrinsic 'llvm.nvvm.unknown.intrinsic' is not supported by this version of the NVPTX backend
+
+declare float @llvm.nvvm.unknown.intrinsic(float)
+
+define float @caller(float %x) {
+  %r = call float @llvm.nvvm.unknown.intrinsic(float %x)
+  ret float %r
+}

>From c921cea60479190c6dc065396352a435eb567c25 Mon Sep 17 00:00:00 2001
From: Antonio Frighetto <me at antoniofrighetto.com>
Date: Thu, 9 Apr 2026 15:54:15 +0200
Subject: [PATCH 2/4] !fixup use emitError

---
 llvm/lib/Target/NVPTX/NVPTXAsmPrinter.cpp    | 11 +++++++----
 llvm/test/CodeGen/NVPTX/unknown-intrinsic.ll |  4 ++--
 2 files changed, 9 insertions(+), 6 deletions(-)

diff --git a/llvm/lib/Target/NVPTX/NVPTXAsmPrinter.cpp b/llvm/lib/Target/NVPTX/NVPTXAsmPrinter.cpp
index 3b3a88ab2ff73..782b0c9e68c5d 100644
--- a/llvm/lib/Target/NVPTX/NVPTXAsmPrinter.cpp
+++ b/llvm/lib/Target/NVPTX/NVPTXAsmPrinter.cpp
@@ -642,10 +642,13 @@ void NVPTXAsmPrinter::emitDeclarations(const Module &M, raw_ostream &O) {
       // If we happen to have an intrinsic previously not lowered, i.e.,
       // unrecognized intrinsics, we should let the user know that the final
       // declaration emitted may lead to illegal PTX.
-      if (F.isIntrinsic())
-        report_fatal_error("unknown intrinsic '" + F.getName() +
-                           "' is not supported by this version of the NVPTX "
-                           "backend; may result in invalid PTX.");
+      if (F.isIntrinsic()) {
+        F.getContext().emitError(
+            "unknown intrinsic '" + F.getName() +
+            "' is not supported by this version of the NVPTX "
+            "backend; may result in invalid PTX.");
+        continue;
+      }
       emitDeclaration(&F, O);
       continue;
     }
diff --git a/llvm/test/CodeGen/NVPTX/unknown-intrinsic.ll b/llvm/test/CodeGen/NVPTX/unknown-intrinsic.ll
index d750fc9a6a626..58de82dc03744 100644
--- a/llvm/test/CodeGen/NVPTX/unknown-intrinsic.ll
+++ b/llvm/test/CodeGen/NVPTX/unknown-intrinsic.ll
@@ -1,7 +1,7 @@
 ; NOTE: Assertions have been autogenerated by utils/update_llc_test_checks.py UTC_ARGS: --version 6
-; RUN: not --crash llc -march=nvptx64 -mcpu=sm_80 < %s 2>&1 | FileCheck %s
+; RUN: not llc -march=nvptx64 -mcpu=sm_80 < %s 2>&1 | FileCheck %s
 
-; CHECK: LLVM ERROR: unknown intrinsic 'llvm.nvvm.unknown.intrinsic' is not supported by this version of the NVPTX backend
+; CHECK: error: unknown intrinsic 'llvm.nvvm.unknown.intrinsic' is not supported by this version of the NVPTX backend
 
 declare float @llvm.nvvm.unknown.intrinsic(float)
 

>From 2c24b5ef10a6e25c884fb9f47ab96f0948ebb3d9 Mon Sep 17 00:00:00 2001
From: Antonio Frighetto <me at antoniofrighetto.com>
Date: Mon, 13 Apr 2026 18:25:01 +0200
Subject: [PATCH 3/4] !fixup use DiagnosticInfoUnsupported, add in lowercall
 too

---
 llvm/lib/Target/NVPTX/NVPTXAsmPrinter.cpp    | 14 +++++------
 llvm/lib/Target/NVPTX/NVPTXISelLowering.cpp  | 26 ++++++++++++--------
 llvm/test/CodeGen/NVPTX/unknown-intrinsic.ll |  3 ++-
 3 files changed, 25 insertions(+), 18 deletions(-)

diff --git a/llvm/lib/Target/NVPTX/NVPTXAsmPrinter.cpp b/llvm/lib/Target/NVPTX/NVPTXAsmPrinter.cpp
index 782b0c9e68c5d..f3372399e447e 100644
--- a/llvm/lib/Target/NVPTX/NVPTXAsmPrinter.cpp
+++ b/llvm/lib/Target/NVPTX/NVPTXAsmPrinter.cpp
@@ -639,14 +639,14 @@ void NVPTXAsmPrinter::emitDeclarations(const Module &M, raw_ostream &O) {
         continue;
       if (F.getIntrinsicID())
         continue;
-      // If we happen to have an intrinsic previously not lowered, i.e.,
-      // unrecognized intrinsics, we should let the user know that the final
-      // declaration emitted may lead to illegal PTX.
+      // An unrecognized intrinsic would produce an invalid PTX declaration. Let
+      // the user know that, and skip it.
       if (F.isIntrinsic()) {
-        F.getContext().emitError(
-            "unknown intrinsic '" + F.getName() +
-            "' is not supported by this version of the NVPTX "
-            "backend; may result in invalid PTX.");
+        LLVMContext &Ctx = F.getContext();
+        Ctx.diagnose(DiagnosticInfoUnsupported(
+            F, Twine("unknown intrinsic '") + F.getName() +
+                   "' is not supported by this version of the NVPTX "
+                   "backend; may result in invalid PTX."));
         continue;
       }
       emitDeclaration(&F, O);
diff --git a/llvm/lib/Target/NVPTX/NVPTXISelLowering.cpp b/llvm/lib/Target/NVPTX/NVPTXISelLowering.cpp
index a5fd0a8724762..ddf1cedbf729f 100644
--- a/llvm/lib/Target/NVPTX/NVPTXISelLowering.cpp
+++ b/llvm/lib/Target/NVPTX/NVPTXISelLowering.cpp
@@ -1324,15 +1324,6 @@ static Align getArgumentAlignment(const CallBase *CB, Type *Ty, unsigned Idx,
   return DL.getABITypeAlign(Ty);
 }
 
-static bool shouldConvertToIndirectCall(const CallBase *CB,
-                                        const GlobalAddressSDNode *Func) {
-  if (!Func)
-    return false;
-  if (auto *CalleeFunc = dyn_cast<Function>(Func->getGlobal()))
-    return CB->getFunctionType() != CalleeFunc->getFunctionType();
-  return false;
-}
-
 static MachinePointerInfo refinePtrAS(SDValue &Ptr, SelectionDAG &DAG,
                                       const DataLayout &DL,
                                       const TargetLowering &TL) {
@@ -1655,9 +1646,12 @@ SDValue NVPTXTargetLowering::LowerCall(TargetLowering::CallLoweringInfo &CLI,
   }
 
   const auto *Func = dyn_cast<GlobalAddressSDNode>(Callee.getNode());
+  const auto *CalleeF = Func ? dyn_cast<Function>(Func->getGlobal()) : nullptr;
+
   // If the type of the callsite does not match that of the function, convert
   // the callsite to an indirect call.
-  const bool ConvertToIndirectCall = shouldConvertToIndirectCall(CB, Func);
+  const bool ConvertToIndirectCall =
+      CalleeF && CB->getFunctionType() != CalleeF->getFunctionType();
 
   // Both indirect calls and libcalls have nullptr Func. In order to distinguish
   // between them we must rely on the call site value which is valid for
@@ -1696,6 +1690,18 @@ SDValue NVPTXTargetLowering::LowerCall(TargetLowering::CallLoweringInfo &CLI,
     CallPrereqs.push_back(PrototypeDeclare);
   }
 
+  const bool IsUnknownIntrinsic =
+      CalleeF && CalleeF->isIntrinsic() &&
+      CalleeF->getIntrinsicID() == Intrinsic::not_intrinsic;
+  if (IsUnknownIntrinsic) {
+    DAG.getContext()->diagnose(DiagnosticInfoUnsupported(
+        DAG.getMachineFunction().getFunction(),
+        "call to unknown intrinsic '" + CalleeF->getName() +
+            "' is not supported by this version of the NVPTX backend; may "
+            "produce invalid PTX.",
+        dl.getDebugLoc()));
+  }
+
   const unsigned Proto = IsIndirectCall ? UniqueCallSite : 0;
   const unsigned NumArgs =
       std::min<unsigned>(CLI.NumFixedArgs + 1, Args.size());
diff --git a/llvm/test/CodeGen/NVPTX/unknown-intrinsic.ll b/llvm/test/CodeGen/NVPTX/unknown-intrinsic.ll
index 58de82dc03744..27daf216b1012 100644
--- a/llvm/test/CodeGen/NVPTX/unknown-intrinsic.ll
+++ b/llvm/test/CodeGen/NVPTX/unknown-intrinsic.ll
@@ -1,7 +1,8 @@
 ; NOTE: Assertions have been autogenerated by utils/update_llc_test_checks.py UTC_ARGS: --version 6
 ; RUN: not llc -march=nvptx64 -mcpu=sm_80 < %s 2>&1 | FileCheck %s
 
-; CHECK: error: unknown intrinsic 'llvm.nvvm.unknown.intrinsic' is not supported by this version of the NVPTX backend
+; CHECK: error: {{.*}}call to unknown intrinsic 'llvm.nvvm.unknown.intrinsic' is not supported by this version of the NVPTX backend
+; CHECK: error: {{.*}}unknown intrinsic 'llvm.nvvm.unknown.intrinsic' is not supported by this version of the NVPTX backend
 
 declare float @llvm.nvvm.unknown.intrinsic(float)
 

>From 65011a5eff4ce4bd4faef26d758786295e0f4033 Mon Sep 17 00:00:00 2001
From: Antonio Frighetto <me at antoniofrighetto.com>
Date: Tue, 14 Apr 2026 10:34:34 +0200
Subject: [PATCH 4/4] !fixup unify msg

---
 llvm/lib/Target/NVPTX/NVPTXISelLowering.cpp | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/llvm/lib/Target/NVPTX/NVPTXISelLowering.cpp b/llvm/lib/Target/NVPTX/NVPTXISelLowering.cpp
index ddf1cedbf729f..f5ee93cdc8760 100644
--- a/llvm/lib/Target/NVPTX/NVPTXISelLowering.cpp
+++ b/llvm/lib/Target/NVPTX/NVPTXISelLowering.cpp
@@ -1698,7 +1698,7 @@ SDValue NVPTXTargetLowering::LowerCall(TargetLowering::CallLoweringInfo &CLI,
         DAG.getMachineFunction().getFunction(),
         "call to unknown intrinsic '" + CalleeF->getName() +
             "' is not supported by this version of the NVPTX backend; may "
-            "produce invalid PTX.",
+            "result in invalid PTX.",
         dl.getDebugLoc()));
   }
 



More information about the llvm-commits mailing list