Skip to content

Commit dccbf1e

Browse files
committed
[SPIRV] Support -fembed-bitcode=marker for non-shader modules
-fembed-bitcode=marker gets lowered as a [0 x i8] zeroinitialized global variable. This is interpreted as a runtime-array and is not supported in non-shaders. To work around this, we replace the [0 x i8] by a zeroinitialized single-element array.
1 parent 750eec6 commit dccbf1e

File tree

2 files changed

+47
-1
lines changed

2 files changed

+47
-1
lines changed

llvm/lib/Target/SPIRV/SPIRVEmitIntrinsics.cpp

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,8 @@ class SPIRVEmitIntrinsics
236236

237237
Instruction *buildLogicalAccessChainFromGEP(GetElementPtrInst &GEP);
238238

239+
bool promoteEmbddedBitcodeMarker(Module &M) const;
240+
239241
public:
240242
static char ID;
241243
SPIRVEmitIntrinsics(SPIRVTargetMachine *TM = nullptr)
@@ -3007,9 +3009,43 @@ void SPIRVEmitIntrinsics::parseFunDeclarations(Module &M) {
30073009
}
30083010
}
30093011

3012+
bool SPIRVEmitIntrinsics::promoteEmbddedBitcodeMarker(Module &M) const {
3013+
const SPIRVSubtarget *STI = TM->getSubtargetImpl();
3014+
if (STI->isShader())
3015+
return false;
3016+
3017+
GlobalVariable *EmbeddedBitcode = M.getNamedGlobal("llvm.embedded.module");
3018+
if (!EmbeddedBitcode)
3019+
return false;
3020+
3021+
ArrayType *AT = cast<ArrayType>(EmbeddedBitcode->getValueType());
3022+
if (AT->getNumElements() != 0)
3023+
return false;
3024+
3025+
// When compiling with -fembed-bitcode=marker, LLVM generates a [0 x i8]
3026+
// zeroinitialized global variable containing the bitcode. This results in an
3027+
// assert outside of shaders. As a workaround, we replace this global with a
3028+
// zeroinitialized [1 x i8].
3029+
ArrayType *AT1 = ArrayType::get(AT->getElementType(), 1);
3030+
Constant *ZeroInit = Constant::getNullValue(AT1);
3031+
GlobalVariable *NewEmbeddedBitcode = new GlobalVariable(
3032+
AT1, EmbeddedBitcode->isConstant(), EmbeddedBitcode->getLinkage(),
3033+
ZeroInit, "", EmbeddedBitcode->getThreadLocalMode(),
3034+
EmbeddedBitcode->getAddressSpace(),
3035+
EmbeddedBitcode->isExternallyInitialized());
3036+
NewEmbeddedBitcode->setSection(NewEmbeddedBitcode->getSection());
3037+
NewEmbeddedBitcode->takeName(EmbeddedBitcode);
3038+
3039+
M.insertGlobalVariable(NewEmbeddedBitcode);
3040+
EmbeddedBitcode->replaceAllUsesWith(NewEmbeddedBitcode);
3041+
EmbeddedBitcode->eraseFromParent();
3042+
return true;
3043+
}
3044+
30103045
bool SPIRVEmitIntrinsics::runOnModule(Module &M) {
30113046
bool Changed = false;
30123047

3048+
Changed |= promoteEmbddedBitcodeMarker(M);
30133049
parseFunDeclarations(M);
30143050
insertConstantsForFPFastMathDefault(M);
30153051

llvm/test/CodeGen/SPIRV/fembed-bitcode-marker.ll

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,21 @@
1-
; XFAIL: *
21
; RUN: llc -verify-machineinstrs -mtriple=spirv-unknown-unknown %s -o - | FileCheck %s
32
; RUN: %if spirv-tools %{ llc -mtriple=spirv-unknown-unknown %s -o - -filetype=obj | spirv-val %}
43

54
@llvm.embedded.module = private constant [0 x i8] zeroinitializer, section ".llvmbc", align 1
65
@llvm.cmdline = private constant [5 x i8] c"-cc1\00", section ".llvmcmd", align 1
76
@llvm.compiler.used = appending global [2 x ptr] [ptr @llvm.embedded.module, ptr @llvm.cmdline], section "llvm.metadata"
87

8+
; CHECK-DAG: OpName [[FOO:%[0-9]+]] "foo"
9+
; CHECK-DAG: OpName [[MODULE:%[0-9]+]] "llvm.embedded.module"
10+
; CHECK-DAG: [[INT8:%[0-9]+]] = OpTypeInt 8 0
11+
; CHECK-DAG: [[INT32:%[0-9]+]] = OpTypeInt 32 0
12+
; CHECK-DAG: [[CONST_1_32:%[0-9]+]] = OpConstant [[INT32]] 1
13+
; CHECK-DAG: [[ARRAY_INT8x1:%[0-9]+]] = OpTypeArray [[INT8]] [[CONST_1_32]]
14+
; CHECK-DAG: [[POINTER:%[0-9]+]] = OpTypePointer Function [[ARRAY_INT8x1]]
15+
; CHECK-DAG: [[EMBEDDED_MODULE_INIT:%[0-9]+]] = OpConstantNull [[ARRAY_INT8x1]]
16+
; CHECK: [[FOO]] = OpFunction {{.*}} None {{.*}}
17+
; CHECK-DAG: {{%[0-9]+}} = OpVariable [[POINTER]] Function [[EMBEDDED_MODULE_INIT]]
18+
919
define spir_kernel void @foo() {
1020
entry:
1121
ret void

0 commit comments

Comments
 (0)