Skip to content

Commit 0e5ddeb

Browse files
committed
[SPIRV][SPIRVPrepareGlobals] Convert llvm.embedded.module from a 0-element array to a 1-element array
When compiling with -fembed-bitcode-marker, Clang inserts a placeholder for the bitcode. This placeholder is a [0 x i8] array, which we cannot represent in SPIRV. For AMD flavoured SPIRV, we extend the llvm.embedded.module global to a zeroinitialized 1-element array.
1 parent 292aba7 commit 0e5ddeb

File tree

2 files changed

+29
-2
lines changed

2 files changed

+29
-2
lines changed

llvm/lib/Target/SPIRV/SPIRVPrepareGlobals.cpp

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313

1414
#include "SPIRV.h"
1515

16+
#include "llvm/IR/Module.h"
17+
1618
using namespace llvm;
1719

1820
namespace {
@@ -28,7 +30,30 @@ struct SPIRVPrepareGlobals : public ModulePass {
2830
bool runOnModule(Module &M) override;
2931
};
3032

31-
bool SPIRVPrepareGlobals::runOnModule(Module &M) { return false; }
33+
bool tryExtendLLVMBitcodeMarker(GlobalVariable &Bitcode) {
34+
assert(Bitcode.getName() == "llvm.embedded.module");
35+
36+
ArrayType *AT = cast<ArrayType>(Bitcode.getValueType());
37+
if (AT->getNumElements() != 0)
38+
return false;
39+
40+
ArrayType *AT1 = ArrayType::get(AT->getElementType(), 1);
41+
Constant *OneEltInit = Constant::getNullValue(AT1);
42+
Bitcode.replaceInitializer(OneEltInit);
43+
return true;
44+
}
45+
46+
bool SPIRVPrepareGlobals::runOnModule(Module &M) {
47+
const bool IsAMD = M.getTargetTriple().getVendor() == Triple::AMD;
48+
if (!IsAMD)
49+
return false;
50+
51+
bool Changed = false;
52+
if (GlobalVariable *Bitcode = M.getNamedGlobal("llvm.embedded.module"))
53+
Changed |= tryExtendLLVMBitcodeMarker(*Bitcode);
54+
55+
return Changed;
56+
}
3257
char SPIRVPrepareGlobals::ID = 0;
3358

3459
} // namespace

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1+
; Expanding the bitcode marker works only for AMD at the moment
12
; RUN: not llc -verify-machineinstrs -mtriple=spirv-unknown-unknown %s -o -
2-
; RUN: not llc -verify-machineinstrs -mtriple=spirv64-amd-amdhsa %s -o -
3+
; RUN: llc -verify-machineinstrs -mtriple=spirv64-amd-amdhsa %s -o - | FileCheck %s
4+
; RUN: %if spirv-tools %{ llc -mtriple=spirv64-amd-amdhsa %s -o - -filetype=obj | spirv-val %}
35
;
46
; Verify that we lower the embedded bitcode
57

0 commit comments

Comments
 (0)