Skip to content

Commit e2b703f

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

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
@@ -452,24 +452,19 @@ SPIRVType *LLVMToSPIRVBase::transType(Type *T) {
452452
if (T->isArrayTy()) {
453453
// SPIR-V 1.3 s3.32.6: Length is the number of elements in the array.
454454
// It must be at least 1.
455-
if (T->getArrayNumElements() < 1) {
456-
std::string Str;
457-
llvm::raw_string_ostream OS(Str);
458-
OS << *T;
459-
SPIRVCK(T->getArrayNumElements() >= 1, InvalidArraySize, OS.str());
460-
}
455+
const auto ArraySize =
456+
T->getArrayNumElements() ? T->getArrayNumElements() : 1;
461457
Type *ElTy = T->getArrayElementType();
462458
SPIRVType *TransType = BM->addArrayType(
463459
transType(ElTy),
464460
static_cast<SPIRVConstant *>(transValue(
465-
ConstantInt::get(getSizetType(), T->getArrayNumElements(), false),
466-
nullptr)));
461+
ConstantInt::get(getSizetType(), ArraySize, false), nullptr)));
467462
mapType(T, TransType);
468463
if (ElTy->isPointerTy()) {
469464
mapType(
470465
ArrayType::get(TypedPointerType::get(Type::getInt8Ty(*Ctx),
471466
ElTy->getPointerAddressSpace()),
472-
T->getArrayNumElements()),
467+
ArraySize),
473468
TransType);
474469
}
475470
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)