Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 9 additions & 4 deletions llvm/lib/Target/SPIRV/SPIRVGlobalRegistry.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
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

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

}
ArrayType = createOpType(MIRBuilder, [&](MachineIRBuilder &MIRBuilder) {
return MIRBuilder.buildInstr(SPIRV::OpTypeRuntimeArray)
.addDef(createTypeVReg(MIRBuilder))
Expand Down
10 changes: 8 additions & 2 deletions llvm/test/CodeGen/SPIRV/zero-length-array.ll
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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
Expand Down