| 1 | //===-- DirectXAsmPrinter.cpp - DirectX assembly writer --------*- 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 | // This file contains AsmPrinters for the DirectX backend. |
| 10 | // |
| 11 | //===----------------------------------------------------------------------===// |
| 12 | |
| 13 | #include "DirectXAsmPrinter.h" |
| 14 | #include "TargetInfo/DirectXTargetInfo.h" |
| 15 | #include "llvm/CodeGen/AsmPrinter.h" |
| 16 | #include "llvm/CodeGen/AsmPrinterAnalysis.h" |
| 17 | #include "llvm/CodeGen/MachinePassManager.h" |
| 18 | #include "llvm/IR/GlobalVariable.h" |
| 19 | #include "llvm/MC/MCStreamer.h" |
| 20 | #include "llvm/MC/SectionKind.h" |
| 21 | #include "llvm/MC/TargetRegistry.h" |
| 22 | #include "llvm/Target/TargetLoweringObjectFile.h" |
| 23 | |
| 24 | using namespace llvm; |
| 25 | |
| 26 | #define DEBUG_TYPE "asm-printer" |
| 27 | |
| 28 | namespace { |
| 29 | |
| 30 | // The DXILAsmPrinter is mostly a stub because DXIL is just LLVM bitcode which |
| 31 | // gets embedded into a DXContainer file. |
| 32 | class DXILAsmPrinter : public AsmPrinter { |
| 33 | public: |
| 34 | explicit DXILAsmPrinter(TargetMachine &TM, |
| 35 | std::unique_ptr<MCStreamer> Streamer) |
| 36 | : AsmPrinter(TM, std::move(Streamer)) {} |
| 37 | |
| 38 | StringRef getPassName() const override { return "DXIL Assembly Printer" ; } |
| 39 | void emitGlobalVariable(const GlobalVariable *GV) override; |
| 40 | bool runOnMachineFunction(MachineFunction &MF) override { return false; } |
| 41 | }; |
| 42 | } // namespace |
| 43 | |
| 44 | void DXILAsmPrinter::emitGlobalVariable(const GlobalVariable *GV) { |
| 45 | // If there is no initializer, or no explicit section do nothing |
| 46 | if (!GV->hasInitializer() || GV->hasImplicitSection() || !GV->hasSection()) |
| 47 | return; |
| 48 | // Skip the LLVM metadata |
| 49 | if (GV->getSection() == "llvm.metadata" ) |
| 50 | return; |
| 51 | SectionKind GVKind = TargetLoweringObjectFile::getKindForGlobal(GO: GV, TM); |
| 52 | MCSection *TheSection = getObjFileLowering().SectionForGlobal(GO: GV, Kind: GVKind, TM); |
| 53 | OutStreamer->switchSection(Section: TheSection); |
| 54 | emitGlobalConstant(DL: GV->getDataLayout(), CV: GV->getInitializer()); |
| 55 | } |
| 56 | |
| 57 | extern "C" LLVM_ABI LLVM_EXTERNAL_VISIBILITY void |
| 58 | LLVMInitializeDirectXAsmPrinter() { |
| 59 | RegisterAsmPrinter<DXILAsmPrinter> X(getTheDirectXTarget()); |
| 60 | } |
| 61 | |
| 62 | PreservedAnalyses DXILAsmPrinterBeginPass::run(Module &M, |
| 63 | ModuleAnalysisManager &MAM) { |
| 64 | DXILAsmPrinter &AsmPrinter = static_cast<DXILAsmPrinter &>( |
| 65 | MAM.getResult<AsmPrinterAnalysis>(IR&: M).getPrinter()); |
| 66 | setupModuleAsmPrinter(M, MAM, AsmPrinter); |
| 67 | AsmPrinter.doInitialization(M); |
| 68 | return PreservedAnalyses::all(); |
| 69 | } |
| 70 | |
| 71 | PreservedAnalyses |
| 72 | DXILAsmPrinterPass::run(MachineFunction &MF, |
| 73 | MachineFunctionAnalysisManager &MFAM) { |
| 74 | DXILAsmPrinter &AsmPrinter = static_cast<DXILAsmPrinter &>( |
| 75 | MFAM.getResult<ModuleAnalysisManagerMachineFunctionProxy>(IR&: MF) |
| 76 | .getCachedResult<AsmPrinterAnalysis>(IR&: *MF.getFunction().getParent()) |
| 77 | ->getPrinter()); |
| 78 | setupMachineFunctionAsmPrinter(MFAM, MF, AsmPrinter); |
| 79 | AsmPrinter.runOnMachineFunction(MF); |
| 80 | return PreservedAnalyses::all(); |
| 81 | } |
| 82 | |
| 83 | PreservedAnalyses DXILAsmPrinterEndPass::run(Module &M, |
| 84 | ModuleAnalysisManager &MAM) { |
| 85 | DXILAsmPrinter &AsmPrinter = static_cast<DXILAsmPrinter &>( |
| 86 | MAM.getResult<AsmPrinterAnalysis>(IR&: M).getPrinter()); |
| 87 | setupModuleAsmPrinter(M, MAM, AsmPrinter); |
| 88 | AsmPrinter.doFinalization(M); |
| 89 | return PreservedAnalyses::all(); |
| 90 | } |
| 91 | |