Skip to content

Commit 2bde884

Browse files
[Backport to 14] Translate LLVM-IR zero-length arrays to 1-length arrays in SPIR-V (#3550)
Manual backport of #2743 Co-authored-by: Lorenc Bushi <lorenc.bushi@intel.com>
1 parent 2156d9d commit 2bde884

File tree

3 files changed

+12
-15
lines changed

3 files changed

+12
-15
lines changed

lib/SPIRV/SPIRVWriter.cpp

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -385,18 +385,14 @@ SPIRVType *LLVMToSPIRVBase::transType(Type *T) {
385385
if (T->isArrayTy()) {
386386
// SPIR-V 1.3 s3.32.6: Length is the number of elements in the array.
387387
// It must be at least 1.
388-
if (T->getArrayNumElements() < 1) {
389-
std::string Str;
390-
llvm::raw_string_ostream OS(Str);
391-
OS << *T;
392-
SPIRVCK(T->getArrayNumElements() >= 1, InvalidArraySize, OS.str());
393-
}
394-
return mapType(T, BM->addArrayType(
395-
transType(T->getArrayElementType()),
396-
static_cast<SPIRVConstant *>(transValue(
397-
ConstantInt::get(getSizetType(),
398-
T->getArrayNumElements(), false),
399-
nullptr))));
388+
const auto ArraySize =
389+
T->getArrayNumElements() ? T->getArrayNumElements() : 1;
390+
return mapType(
391+
T,
392+
BM->addArrayType(
393+
transType(T->getArrayElementType()),
394+
static_cast<SPIRVConstant *>(transValue(
395+
ConstantInt::get(getSizetType(), ArraySize, false), nullptr))));
400396
}
401397

402398
if (T->isStructTy() && !T->isSized()) {

lib/SPIRV/libSPIRV/SPIRVErrorEnum.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ _SPIRV_OP(InvalidMemoryModel, "Expects 0-3.")
77
_SPIRV_OP(InvalidFunctionControlMask, "")
88
_SPIRV_OP(InvalidBuiltinSetName, "Expects OpenCL.std.")
99
_SPIRV_OP(InvalidFunctionCall, "Unexpected llvm intrinsic:\n")
10-
_SPIRV_OP(InvalidArraySize, "Array size must be at least 1:")
1110
_SPIRV_OP(InvalidBitWidth, "Invalid bit width in input:")
1211
_SPIRV_OP(InvalidModule, "Invalid SPIR-V module:")
1312
_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)