Skip to content

Commit 9cfd729

Browse files
[Backport to llvm_release_170] Translate LLVM-IR zero-length arrays to 1-length arrays in SPIR-V (#3546)
Backport of PR #2743 into `llvm_release_170`. All commits applied cleanly. Co-authored-by: Lorenc Bushi <[email protected]>
1 parent ffb846b commit 9cfd729

File tree

3 files changed

+8
-12
lines changed

3 files changed

+8
-12
lines changed

lib/SPIRV/SPIRVWriter.cpp

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -377,24 +377,19 @@ SPIRVType *LLVMToSPIRVBase::transType(Type *T) {
377377
if (T->isArrayTy()) {
378378
// SPIR-V 1.3 s3.32.6: Length is the number of elements in the array.
379379
// It must be at least 1.
380-
if (T->getArrayNumElements() < 1) {
381-
std::string Str;
382-
llvm::raw_string_ostream OS(Str);
383-
OS << *T;
384-
SPIRVCK(T->getArrayNumElements() >= 1, InvalidArraySize, OS.str());
385-
}
380+
const auto ArraySize =
381+
T->getArrayNumElements() ? T->getArrayNumElements() : 1;
386382
Type *ElTy = T->getArrayElementType();
387383
SPIRVType *TransType = BM->addArrayType(
388384
transType(ElTy),
389385
static_cast<SPIRVConstant *>(transValue(
390-
ConstantInt::get(getSizetType(), T->getArrayNumElements(), false),
391-
nullptr)));
386+
ConstantInt::get(getSizetType(), ArraySize, false), nullptr)));
392387
mapType(T, TransType);
393388
if (ElTy->isPointerTy()) {
394389
mapType(
395390
ArrayType::get(TypedPointerType::get(Type::getInt8Ty(*Ctx),
396391
ElTy->getPointerAddressSpace()),
397-
T->getArrayNumElements()),
392+
ArraySize),
398393
TransType);
399394
}
400395
return TransType;

lib/SPIRV/libSPIRV/SPIRVErrorEnum.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ _SPIRV_OP(InvalidMemoryModel, "Expects 0-3.")
1010
_SPIRV_OP(InvalidFunctionControlMask, "")
1111
_SPIRV_OP(InvalidBuiltinSetName, "Expects OpenCL.std.")
1212
_SPIRV_OP(InvalidFunctionCall, "Unexpected llvm intrinsic:\n")
13-
_SPIRV_OP(InvalidArraySize, "Array size must be at least 1:")
1413
_SPIRV_OP(InvalidBitWidth, "Invalid bit width in input:")
1514
_SPIRV_OP(InvalidModule, "Invalid SPIR-V module:")
1615
_SPIRV_OP(InvalidLlvmModule, "Invalid LLVM module:")
Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
; RUN: llvm-as %s -o %t.bc
2-
; RUN: not llvm-spirv %t.bc -o %t.spv 2>&1 | FileCheck %s
2+
; RUN: llvm-spirv %t.bc -o %t.spv
3+
; RUN: spirv-dis %t.spv | FileCheck %s
34

4-
; CHECK: InvalidArraySize: Array size must be at least 1: [0 x i32]
5+
; CHECK: [[REGISTER:%[a-zA-Z0-9_]+]] = OpConstant %uint 1
6+
; CHECK: OpTypeArray %uint [[REGISTER]]
57

68
source_filename = "test.cl"
79
target datalayout = "e-p:32:32-i64:64-v16:16-v24:32-v32:32-v48:64-v96:128-v192:256-v256:256-v512:512-v1024:1024"

0 commit comments

Comments
 (0)