From 7c4cc9e20cb5fb25678dc612feb2388361b7ac83 Mon Sep 17 00:00:00 2001 From: Nick Sarnie Date: Wed, 26 Nov 2025 13:36:51 -0800 Subject: [PATCH 1/2] [SPIRV] Error for zero-length arrays if not a shader Signed-off-by: Nick Sarnie --- llvm/lib/Target/SPIRV/SPIRVGlobalRegistry.cpp | 13 +++++++++---- llvm/test/CodeGen/SPIRV/zero-length-array.ll | 10 ++++++++-- 2 files changed, 17 insertions(+), 6 deletions(-) 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 From 11f3d9d394ba4921b0bf2b8ae0e01055412b6127 Mon Sep 17 00:00:00 2001 From: Nick Sarnie Date: Tue, 2 Dec 2025 07:20:44 -0800 Subject: [PATCH 2/2] address feedback Signed-off-by: Nick Sarnie --- llvm/lib/Target/SPIRV/SPIRVGlobalRegistry.cpp | 9 +++------ llvm/test/CodeGen/SPIRV/zero-length-array.ll | 2 +- 2 files changed, 4 insertions(+), 7 deletions(-) diff --git a/llvm/lib/Target/SPIRV/SPIRVGlobalRegistry.cpp b/llvm/lib/Target/SPIRV/SPIRVGlobalRegistry.cpp index 17af251e20929..a69ab958baf38 100644 --- a/llvm/lib/Target/SPIRV/SPIRVGlobalRegistry.cpp +++ b/llvm/lib/Target/SPIRV/SPIRVGlobalRegistry.cpp @@ -884,13 +884,10 @@ SPIRVType *SPIRVGlobalRegistry::getOpTypeArray(uint32_t NumElems, }); } else { if (!ST.isShader()) { - Function &Fn = MIRBuilder.getMF().getFunction(); - Fn.getContext().diagnose(DiagnosticInfoUnsupported( - Fn, + llvm::reportFatalUsageError( "Runtime arrays are not allowed in non-shader " - "SPIR-V modules", - MIRBuilder.getDebugLoc())); - return ElemType; + "SPIR-V modules"); + return nullptr; } ArrayType = createOpType(MIRBuilder, [&](MachineIRBuilder &MIRBuilder) { return MIRBuilder.buildInstr(SPIRV::OpTypeRuntimeArray) diff --git a/llvm/test/CodeGen/SPIRV/zero-length-array.ll b/llvm/test/CodeGen/SPIRV/zero-length-array.ll index ab3647bb17fa2..cb34529ebfecd 100644 --- a/llvm/test/CodeGen/SPIRV/zero-length-array.ll +++ b/llvm/test/CodeGen/SPIRV/zero-length-array.ll @@ -15,7 +15,7 @@ ; For non-compute, error. -; CHECK-ERR: in function foo void (): Runtime arrays are not allowed in non-shader SPIR-V modules +; CHECK-ERR: LLVM ERROR: Runtime arrays are not allowed in non-shader SPIR-V modules define spir_func void @foo() { entry: