Skip to content

Commit fbf70ce

Browse files
[Backport to 15] Translate LLVM-IR zero-length arrays to 1-length arrays in SPIR-V (#3549)
Manual backport of #2743 Co-authored-by: Lorenc Bushi <lorenc.bushi@intel.com>
1 parent 105869e commit fbf70ce

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
@@ -384,18 +384,14 @@ SPIRVType *LLVMToSPIRVBase::transType(Type *T) {
384384
if (T->isArrayTy()) {
385385
// SPIR-V 1.3 s3.32.6: Length is the number of elements in the array.
386386
// It must be at least 1.
387-
if (T->getArrayNumElements() < 1) {
388-
std::string Str;
389-
llvm::raw_string_ostream OS(Str);
390-
OS << *T;
391-
SPIRVCK(T->getArrayNumElements() >= 1, InvalidArraySize, OS.str());
392-
}
393-
return mapType(T, BM->addArrayType(
394-
transType(T->getArrayElementType()),
395-
static_cast<SPIRVConstant *>(transValue(
396-
ConstantInt::get(getSizetType(),
397-
T->getArrayNumElements(), false),
398-
nullptr))));
387+
const auto ArraySize =
388+
T->getArrayNumElements() ? T->getArrayNumElements() : 1;
389+
return mapType(
390+
T,
391+
BM->addArrayType(
392+
transType(T->getArrayElementType()),
393+
static_cast<SPIRVConstant *>(transValue(
394+
ConstantInt::get(getSizetType(), ArraySize, false), nullptr))));
399395
}
400396

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

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)