1 //===- SPIRVTargetMachine.cpp - Define TargetMachine for SPIR-V -*- 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 // Implements the info about SPIR-V target spec. 10 // 11 //===----------------------------------------------------------------------===// 12 13 #include "SPIRVTargetMachine.h" 14 #include "TargetInfo/SPIRVTargetInfo.h" 15 #include "llvm/CodeGen/TargetLoweringObjectFileImpl.h" 16 #include "llvm/CodeGen/TargetPassConfig.h" 17 #include "llvm/MC/TargetRegistry.h" 18 19 using namespace llvm; 20 21 extern "C" LLVM_EXTERNAL_VISIBILITY void LLVMInitializeSPIRVTarget() { 22 // Register the target. 23 RegisterTargetMachine<SPIRVTargetMachine> X(getTheSPIRV32Target()); 24 RegisterTargetMachine<SPIRVTargetMachine> Y(getTheSPIRV64Target()); 25 } 26 27 static std::string computeDataLayout(const Triple &TT) { 28 std::string DataLayout = "e-m:e"; 29 30 const auto Arch = TT.getArch(); 31 if (Arch == Triple::spirv32) 32 DataLayout += "-p:32:32"; 33 else if (Arch == Triple::spirv64) 34 DataLayout += "-p:64:64"; 35 return DataLayout; 36 } 37 38 static Reloc::Model getEffectiveRelocModel(Optional<Reloc::Model> RM) { 39 if (!RM) 40 return Reloc::PIC_; 41 return *RM; 42 } 43 44 SPIRVTargetMachine::SPIRVTargetMachine(const Target &T, const Triple &TT, 45 StringRef CPU, StringRef FS, 46 const TargetOptions &Options, 47 Optional<Reloc::Model> RM, 48 Optional<CodeModel::Model> CM, 49 CodeGenOpt::Level OL, bool JIT) 50 : LLVMTargetMachine(T, computeDataLayout(TT), TT, CPU, FS, Options, 51 getEffectiveRelocModel(RM), 52 getEffectiveCodeModel(CM, CodeModel::Small), OL), 53 TLOF(std::make_unique<TargetLoweringObjectFileELF>()) { 54 initAsmInfo(); 55 } 56 57 namespace { 58 // SPIR-V Code Generator Pass Configuration Options. 59 class SPIRVPassConfig : public TargetPassConfig { 60 public: 61 SPIRVPassConfig(SPIRVTargetMachine &TM, PassManagerBase &PM) 62 : TargetPassConfig(TM, PM) {} 63 64 SPIRVTargetMachine &getSPIRVTargetMachine() const { 65 return getTM<SPIRVTargetMachine>(); 66 } 67 }; 68 } // namespace 69 70 TargetPassConfig *SPIRVTargetMachine::createPassConfig(PassManagerBase &PM) { 71 return new SPIRVPassConfig(*this, PM); 72 } 73