|
| 1 | +//===-- SPIRVPrepareGlobals.cpp - Prepare IR SPIRV globals ------*- C++ -*-===// |
| 2 | +// |
| 3 | +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | +// See https://llvm.org/LICENSE.txt for license information. |
| 5 | +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 6 | +// |
| 7 | +//===----------------------------------------------------------------------===// |
| 8 | +// |
| 9 | +// The pass transforms IR globals that cannot be trivially mapped to SPIRV |
| 10 | +// into something that is trival to lower. |
| 11 | +// |
| 12 | +//===----------------------------------------------------------------------===// |
| 13 | + |
| 14 | +#include "SPIRV.h" |
| 15 | + |
| 16 | +#include "llvm/IR/Module.h" |
| 17 | + |
| 18 | +using namespace llvm; |
| 19 | + |
| 20 | +namespace { |
| 21 | + |
| 22 | +struct SPIRVPrepareGlobals : public ModulePass { |
| 23 | + static char ID; |
| 24 | + SPIRVPrepareGlobals() : ModulePass(ID) {} |
| 25 | + |
| 26 | + StringRef getPassName() const override { |
| 27 | + return "SPIRV prepare global variables"; |
| 28 | + } |
| 29 | + |
| 30 | + bool runOnModule(Module &M) override; |
| 31 | +}; |
| 32 | + |
| 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 | +} |
| 57 | +char SPIRVPrepareGlobals::ID = 0; |
| 58 | + |
| 59 | +} // namespace |
| 60 | + |
| 61 | +INITIALIZE_PASS(SPIRVPrepareGlobals, "prepare-globals", |
| 62 | + "SPIRV prepare global variables", false, false) |
| 63 | + |
| 64 | +namespace llvm { |
| 65 | +ModulePass *createSPIRVPrepareGlobalsPass() { |
| 66 | + return new SPIRVPrepareGlobals(); |
| 67 | +} |
| 68 | +} // namespace llvm |
0 commit comments