|
| 1 | +#include "MCTargetDesc/SPIRVBaseInfo.h" |
| 2 | +#include "MCTargetDesc/SPIRVMCTargetDesc.h" |
| 3 | +#include "SPIRVGlobalRegistry.h" |
| 4 | +#include "SPIRVRegisterInfo.h" |
| 5 | +#include "SPIRVTargetMachine.h" |
| 6 | +#include "llvm/ADT/SmallString.h" |
| 7 | +#include "llvm/CodeGen/GlobalISel/MachineIRBuilder.h" |
| 8 | +#include "llvm/CodeGen/MachineBasicBlock.h" |
| 9 | +#include "llvm/CodeGen/MachineFunction.h" |
| 10 | +#include "llvm/CodeGen/MachineFunctionPass.h" |
| 11 | +#include "llvm/CodeGen/MachineInstr.h" |
| 12 | +#include "llvm/CodeGen/MachineInstrBuilder.h" |
| 13 | +#include "llvm/CodeGen/MachineModuleInfo.h" |
| 14 | +#include "llvm/CodeGen/MachineOperand.h" |
| 15 | +#include "llvm/IR/DebugInfoMetadata.h" |
| 16 | +#include "llvm/IR/Metadata.h" |
| 17 | +#include "llvm/PassRegistry.h" |
| 18 | +#include "llvm/Support/Casting.h" |
| 19 | +#include "llvm/Support/Path.h" |
| 20 | + |
| 21 | +#define DEBUG_TYPE "spirv-nonsemantic-debug-info" |
| 22 | + |
| 23 | +namespace llvm { |
| 24 | +struct SPIRVEmitNonSemanticDI : public MachineFunctionPass { |
| 25 | + static char ID; |
| 26 | + SPIRVTargetMachine *TM; |
| 27 | + SPIRVEmitNonSemanticDI(SPIRVTargetMachine *TM); |
| 28 | + SPIRVEmitNonSemanticDI(); |
| 29 | + |
| 30 | + bool runOnMachineFunction(MachineFunction &MF) override; |
| 31 | + |
| 32 | +private: |
| 33 | + bool IsGlobalDIEmitted = false; |
| 34 | + bool emitGlobalDI(MachineFunction &MF); |
| 35 | +}; |
| 36 | + |
| 37 | +void initializeSPIRVEmitNonSemanticDIPass(PassRegistry &); |
| 38 | + |
| 39 | +FunctionPass *createSPIRVEmitNonSemanticDIPass(SPIRVTargetMachine *TM) { |
| 40 | + return new SPIRVEmitNonSemanticDI(TM); |
| 41 | +} |
| 42 | +} // namespace llvm |
| 43 | + |
| 44 | +using namespace llvm; |
| 45 | + |
| 46 | +INITIALIZE_PASS(SPIRVEmitNonSemanticDI, DEBUG_TYPE, |
| 47 | + "SPIRV NonSemantic.Shader.DebugInfo.100 emitter", false, false) |
| 48 | + |
| 49 | +char SPIRVEmitNonSemanticDI::ID = 0; |
| 50 | + |
| 51 | +SPIRVEmitNonSemanticDI::SPIRVEmitNonSemanticDI(SPIRVTargetMachine *TM) |
| 52 | + : MachineFunctionPass(ID), TM(TM) { |
| 53 | + initializeSPIRVEmitNonSemanticDIPass(*PassRegistry::getPassRegistry()); |
| 54 | +} |
| 55 | + |
| 56 | +SPIRVEmitNonSemanticDI::SPIRVEmitNonSemanticDI() : MachineFunctionPass(ID) { |
| 57 | + initializeSPIRVEmitNonSemanticDIPass(*PassRegistry::getPassRegistry()); |
| 58 | +} |
| 59 | + |
| 60 | +bool SPIRVEmitNonSemanticDI::emitGlobalDI(MachineFunction &MF) { |
| 61 | + // If this MachineFunction doesn't have any BB repeat procedure |
| 62 | + // for the next |
| 63 | + if (MF.begin() == MF.end()) { |
| 64 | + IsGlobalDIEmitted = false; |
| 65 | + return false; |
| 66 | + } |
| 67 | + |
| 68 | + // Required variables to get from metadata search |
| 69 | + LLVMContext *Context; |
| 70 | + SmallString<128> FilePath; |
| 71 | + unsigned SourceLanguage = 0; |
| 72 | + int64_t DwarfVersion = 0; |
| 73 | + int64_t DebugInfoVersion = 0; |
| 74 | + |
| 75 | + // Searching through the Module metadata to find nescessary |
| 76 | + // information like DwarfVersion or SourceLanguage |
| 77 | + { |
| 78 | + const MachineModuleInfo &MMI = |
| 79 | + getAnalysis<MachineModuleInfoWrapperPass>().getMMI(); |
| 80 | + const Module *M = MMI.getModule(); |
| 81 | + Context = &M->getContext(); |
| 82 | + const NamedMDNode *DbgCu = M->getNamedMetadata("llvm.dbg.cu"); |
| 83 | + if (!DbgCu) |
| 84 | + return false; |
| 85 | + for (const auto *Op : DbgCu->operands()) { |
| 86 | + if (const auto *CompileUnit = dyn_cast<DICompileUnit>(Op)) { |
| 87 | + DIFile *File = CompileUnit->getFile(); |
| 88 | + sys::path::append(FilePath, File->getDirectory(), File->getFilename()); |
| 89 | + SourceLanguage = CompileUnit->getSourceLanguage(); |
| 90 | + break; |
| 91 | + } |
| 92 | + } |
| 93 | + const NamedMDNode *ModuleFlags = M->getNamedMetadata("llvm.module.flags"); |
| 94 | + for (const auto *Op : ModuleFlags->operands()) { |
| 95 | + const MDOperand &MaybeStrOp = Op->getOperand(1); |
| 96 | + if (MaybeStrOp.equalsStr("Dwarf Version")) |
| 97 | + DwarfVersion = |
| 98 | + cast<ConstantInt>( |
| 99 | + cast<ConstantAsMetadata>(Op->getOperand(2))->getValue()) |
| 100 | + ->getSExtValue(); |
| 101 | + else if (MaybeStrOp.equalsStr("Debug Info Version")) |
| 102 | + DebugInfoVersion = |
| 103 | + cast<ConstantInt>( |
| 104 | + cast<ConstantAsMetadata>(Op->getOperand(2))->getValue()) |
| 105 | + ->getSExtValue(); |
| 106 | + } |
| 107 | + } |
| 108 | + // NonSemantic.Shader.DebugInfo.100 global DI instruction emitting |
| 109 | + { |
| 110 | + // Required LLVM variables for emitting logic |
| 111 | + const SPIRVInstrInfo *TII = TM->getSubtargetImpl()->getInstrInfo(); |
| 112 | + const SPIRVRegisterInfo *TRI = TM->getSubtargetImpl()->getRegisterInfo(); |
| 113 | + const RegisterBankInfo *RBI = TM->getSubtargetImpl()->getRegBankInfo(); |
| 114 | + SPIRVGlobalRegistry *GR = TM->getSubtargetImpl()->getSPIRVGlobalRegistry(); |
| 115 | + MachineRegisterInfo &MRI = MF.getRegInfo(); |
| 116 | + MachineBasicBlock &MBB = *MF.begin(); |
| 117 | + |
| 118 | + // To correct placement of a OpLabel instruction during SPIRVAsmPrinter |
| 119 | + // emission all new instructions needs to be placed after OpFunction |
| 120 | + // and before first terminator |
| 121 | + MachineIRBuilder MIRBuilder(MBB, MBB.getFirstTerminator()); |
| 122 | + |
| 123 | + // Emit OpString with FilePath which is required by DebugSource |
| 124 | + const Register StrReg = MRI.createVirtualRegister(&SPIRV::IDRegClass); |
| 125 | + MRI.setType(StrReg, LLT::scalar(32)); |
| 126 | + MachineInstrBuilder MIB = MIRBuilder.buildInstr(SPIRV::OpString); |
| 127 | + MIB.addDef(StrReg); |
| 128 | + addStringImm(FilePath, MIB); |
| 129 | + |
| 130 | + const SPIRVType *VoidTy = |
| 131 | + GR->getOrCreateSPIRVType(Type::getVoidTy(*Context), MIRBuilder); |
| 132 | + |
| 133 | + // Emit DebugSource which is required by DebugCompilationUnit |
| 134 | + const Register DebugSourceResIdReg = |
| 135 | + MRI.createVirtualRegister(&SPIRV::IDRegClass); |
| 136 | + MRI.setType(DebugSourceResIdReg, LLT::scalar(32)); |
| 137 | + MIB = MIRBuilder.buildInstr(SPIRV::OpExtInst) |
| 138 | + .addDef(DebugSourceResIdReg) |
| 139 | + .addUse(GR->getSPIRVTypeID(VoidTy)) |
| 140 | + .addImm(static_cast<int64_t>( |
| 141 | + SPIRV::InstructionSet::NonSemantic_Shader_DebugInfo_100)) |
| 142 | + .addImm(SPIRV::NonSemanticExtInst::DebugSource) |
| 143 | + .addUse(StrReg); |
| 144 | + MIB.constrainAllUses(*TII, *TRI, *RBI); |
| 145 | + GR->assignSPIRVTypeToVReg(VoidTy, DebugSourceResIdReg, MF); |
| 146 | + |
| 147 | + const SPIRVType *I32Ty = |
| 148 | + GR->getOrCreateSPIRVType(Type::getInt32Ty(*Context), MIRBuilder); |
| 149 | + |
| 150 | + // Convert DwarfVersion, DebugInfo and SourceLanguage integers to OpConstant |
| 151 | + // instructions required by DebugCompilationUnit |
| 152 | + const Register DwarfVersionReg = |
| 153 | + GR->buildConstantInt(DwarfVersion, MIRBuilder, I32Ty, false); |
| 154 | + const Register DebugInfoVersionReg = |
| 155 | + GR->buildConstantInt(DebugInfoVersion, MIRBuilder, I32Ty, false); |
| 156 | + const Register SourceLanguageReg = |
| 157 | + GR->buildConstantInt(SourceLanguage, MIRBuilder, I32Ty, false); |
| 158 | + |
| 159 | + // Emit DebugCompilationUnit |
| 160 | + const Register DebugCompUnitResIdReg = |
| 161 | + MRI.createVirtualRegister(&SPIRV::IDRegClass); |
| 162 | + MRI.setType(DebugCompUnitResIdReg, LLT::scalar(32)); |
| 163 | + MIB = MIRBuilder.buildInstr(SPIRV::OpExtInst) |
| 164 | + .addDef(DebugCompUnitResIdReg) |
| 165 | + .addUse(GR->getSPIRVTypeID(VoidTy)) |
| 166 | + .addImm(static_cast<int64_t>( |
| 167 | + SPIRV::InstructionSet::NonSemantic_Shader_DebugInfo_100)) |
| 168 | + .addImm(SPIRV::NonSemanticExtInst::DebugCompilationUnit) |
| 169 | + .addUse(DebugInfoVersionReg) |
| 170 | + .addUse(DwarfVersionReg) |
| 171 | + .addUse(DebugSourceResIdReg) |
| 172 | + .addUse(SourceLanguageReg); |
| 173 | + MIB.constrainAllUses(*TII, *TRI, *RBI); |
| 174 | + GR->assignSPIRVTypeToVReg(VoidTy, DebugCompUnitResIdReg, MF); |
| 175 | + } |
| 176 | + return true; |
| 177 | +} |
| 178 | + |
| 179 | +bool SPIRVEmitNonSemanticDI::runOnMachineFunction(MachineFunction &MF) { |
| 180 | + bool Res = false; |
| 181 | + // emitGlobalDI needs to be executed only once to avoid |
| 182 | + // emitting duplicates |
| 183 | + if (!IsGlobalDIEmitted) { |
| 184 | + IsGlobalDIEmitted = true; |
| 185 | + Res = emitGlobalDI(MF); |
| 186 | + } |
| 187 | + return Res; |
| 188 | +} |
0 commit comments