-
Notifications
You must be signed in to change notification settings - Fork 15.4k
[SPIRV] Error for zero-length arrays if not a shader #169732
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
Signed-off-by: Nick Sarnie <nick.sarnie@intel.com>
|
@llvm/pr-subscribers-backend-spir-v Author: Nick Sarnie (sarnex) ChangesI 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. Full diff: https://github.com/llvm/llvm-project/pull/169732.diff 2 Files Affected:
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; |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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
maarquitos14
left a comment
There was a problem hiding this 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; |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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>
jmmartinez
left a comment
There was a problem hiding this 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 !
|
Thanks Juan! |
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>
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.