Skip to content

Conversation

@sarnex
Copy link
Member

@sarnex sarnex commented Nov 26, 2025

I had a case where the frontend was generating a zero elem array in non-shader code so it was just crashing in a release build.
Add a real error and make it not crash.

Signed-off-by: Nick Sarnie <nick.sarnie@intel.com>
@sarnex sarnex marked this pull request as ready for review November 26, 2025 22:26
@llvmbot
Copy link
Member

llvmbot commented Nov 26, 2025

@llvm/pr-subscribers-backend-spir-v

Author: Nick Sarnie (sarnex)

Changes

I had a case where the frontend was generating a zero elem array in non-shader code so it was just crashing in a release build.
Add a real error and make it not crash by returning some real value.


Full diff: https://github.com/llvm/llvm-project/pull/169732.diff

2 Files Affected:

  • (modified) llvm/lib/Target/SPIRV/SPIRVGlobalRegistry.cpp (+9-4)
  • (modified) llvm/test/CodeGen/SPIRV/zero-length-array.ll (+8-2)
diff --git a/llvm/lib/Target/SPIRV/SPIRVGlobalRegistry.cpp b/llvm/lib/Target/SPIRV/SPIRVGlobalRegistry.cpp
index 0b89e5f4cf316..17af251e20929 100644
--- a/llvm/lib/Target/SPIRV/SPIRVGlobalRegistry.cpp
+++ b/llvm/lib/Target/SPIRV/SPIRVGlobalRegistry.cpp
@@ -883,10 +883,15 @@ SPIRVType *SPIRVGlobalRegistry::getOpTypeArray(uint32_t NumElems,
           .addUse(NumElementsVReg);
     });
   } else {
-    assert(ST.isShader() && "Runtime arrays are not allowed in non-shader "
-                            "SPIR-V modules.");
-    if (!ST.isShader())
-      return nullptr;
+    if (!ST.isShader()) {
+      Function &Fn = MIRBuilder.getMF().getFunction();
+      Fn.getContext().diagnose(DiagnosticInfoUnsupported(
+          Fn,
+          "Runtime arrays are not allowed in non-shader "
+          "SPIR-V modules",
+          MIRBuilder.getDebugLoc()));
+      return ElemType;
+    }
     ArrayType = createOpType(MIRBuilder, [&](MachineIRBuilder &MIRBuilder) {
       return MIRBuilder.buildInstr(SPIRV::OpTypeRuntimeArray)
           .addDef(createTypeVReg(MIRBuilder))
diff --git a/llvm/test/CodeGen/SPIRV/zero-length-array.ll b/llvm/test/CodeGen/SPIRV/zero-length-array.ll
index 5fd94d25dfd87..ab3647bb17fa2 100644
--- a/llvm/test/CodeGen/SPIRV/zero-length-array.ll
+++ b/llvm/test/CodeGen/SPIRV/zero-length-array.ll
@@ -1,7 +1,9 @@
-; RUN: llc -verify-machineinstrs -O0 -mtriple=spirv-unknown-vulkan-compute %s -o - | FileCheck %s
+; RUN: llc -verify-machineinstrs -O0 -mtriple=spirv-unknown-vulkan-compute < %s | FileCheck %s
 ; RUN: %if spirv-tools %{ llc -O0 -mtriple=spirv-unknown-vulkan-compute %s -o - -filetype=obj | spirv-val %}
 
-; Nothing is generated, but compilation doesn't crash.
+; RUN: not llc -verify-machineinstrs -O0 -mtriple=spirv64-unknown-unknown < %s 2>&1 | FileCheck -check-prefix=CHECK-ERR %s
+
+; For compute, nothing is generated, but compilation doesn't crash.
 ; CHECK: OpName %[[#FOO:]] "foo"
 ; CHECK: OpName %[[#RTM:]] "reg2mem alloca point"
 ; CHECK: %[[#INT:]] = OpTypeInt 32 0
@@ -11,6 +13,10 @@
 ; CHECK-NEXT: OpReturn
 ; CHECK-NEXT: OpFunctionEnd
 
+
+; For non-compute, error.
+; CHECK-ERR: in function foo void (): Runtime arrays are not allowed in non-shader SPIR-V modules
+
 define spir_func void @foo() {
 entry:
   %i = alloca [0 x i32], align 4

"Runtime arrays are not allowed in non-shader "
"SPIR-V modules",
MIRBuilder.getDebugLoc()));
return ElemType;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does diagnose(...) return and continues executing the backend code?

Copy link
Member Author

@sarnex sarnex Dec 1, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, but it properly errors out a bit after this. If we don't do this it still crashes with an error, with this there's just an error. If you know a better way to prevent a crash/assert but still throw an error let me know, I'd prefer that too.

Copy link
Contributor

@maarquitos14 maarquitos14 Dec 2, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would llvm::report_fatal_error be better then? Reporting the error and immediately aborting seems to me like what you're asking for.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm ok if we use llvm::reportFatalUsageError here (llvm::report_fatal_error is deprecated).

We can revisit having proper diagnostics later.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the feedback, I reworked the error to use llvm::reportFatalUsageError in my latest commit

Copy link
Contributor

@maarquitos14 maarquitos14 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I had a case where the frontend was generating a zero elem array in non-shader code so it was just crashing in a release build.
Add a real error and make it not crash by returning some real value.

Can you share/describe the case where you find the problem? #149522 introduced handling for cases with 0-length arrays, if it's similar, we might prefer to extend that mechanism instead.

"Runtime arrays are not allowed in non-shader "
"SPIR-V modules",
MIRBuilder.getDebugLoc()));
return ElemType;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure this is what we want to do. The caller of this function is expecting an OpTypeArray, but we will be returning the ElemType in this case. Can you please explain why that is okay? And why is that better than returning nullptr, which signals that something went wrong?

Copy link
Member Author

@sarnex sarnex Dec 1, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Replied to the same comment in Juan's review of this, thanks.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this feedback should be addressed in my latest commit

Signed-off-by: Nick Sarnie <nick.sarnie@intel.com>
Copy link
Contributor

@jmmartinez jmmartinez left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks good to me. But wait for the other reviewer's approval.

Thanks !

@sarnex
Copy link
Member Author

sarnex commented Dec 2, 2025

Thanks Juan!

@sarnex sarnex requested a review from maarquitos14 December 2, 2025 16:50
@sarnex sarnex merged commit 1a3709c into llvm:main Dec 2, 2025
11 checks passed
kcloudy0717 pushed a commit to kcloudy0717/llvm-project that referenced this pull request Dec 4, 2025
I had a case where the frontend was generating a zero elem array in
non-shader code so it was just crashing in a release build.
Add a real error and make it not crash.

---------

Signed-off-by: Nick Sarnie <nick.sarnie@intel.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants