[llvm] [SPIRV] Do not use OpTypeRuntimeArray in Kernel env. (PR #149522)

Dmitry Sidorov via llvm-commits llvm-commits at lists.llvm.org
Mon Jul 21 07:30:31 PDT 2025


================
@@ -828,9 +828,11 @@ SPIRVType *SPIRVGlobalRegistry::getOpTypeArray(uint32_t NumElems,
          "Invalid array element type");
   SPIRVType *SpvTypeInt32 = getOrCreateSPIRVIntegerType(32, MIRBuilder);
   SPIRVType *ArrayType = nullptr;
-  if (NumElems != 0) {
-    Register NumElementsVReg =
-        buildConstantInt(NumElems, MIRBuilder, SpvTypeInt32, EmitIR);
+  const SPIRVSubtarget &ST =
+      cast<SPIRVSubtarget>(MIRBuilder.getMF().getSubtarget());
+  if (NumElems != 0 || !ST.isShader()) {
+    Register NumElementsVReg = buildConstantInt(
+        NumElems ? NumElems : 1, MIRBuilder, SpvTypeInt32, EmitIR);
----------------
MrSidims wrote:

I understand overall concern and agree, that it's probably better to solve in the frontend. But examples like https://godbolt.org/z/zaTT7nTvr are already UB, hence the compiler is free to do whatever it wants during codegen.

Basically (IMHO) everything, that looks like
`%elem = getelementptr [0 x i32], ptr %array, i32 0, i32 1`
can be changed to
`%elem = getelementptr [1 x i32], ptr %array, i32 0, i32 1`
because the first GEP is UB.

https://github.com/llvm/llvm-project/pull/149522


More information about the llvm-commits mailing list