-
Notifications
You must be signed in to change notification settings - Fork 13.6k
[SPIRV] Emitting DebugSource, DebugCompileUnit #97558
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
e57e140
[SPIRV] Added new stub pass for NonSemantic DI
bwlodarcz 9bc08c4
[SPIRV] Emitting DebugSource, DebugCompileUnit
bwlodarcz 4e85f0a
Fixes after CR
bwlodarcz 058a77d
Add assignSPIRVTypeToVReg
bwlodarcz 1dd89b7
Pass moved to the end
bwlodarcz 4690df0
Adding braces and comments
bwlodarcz 66d3d18
Added test
bwlodarcz f0435af
Fix comment typo
bwlodarcz 383acdd
Fix after review plus mir test
bwlodarcz f889d35
Fixed spirv-val OpLabel issue
bwlodarcz 9f38912
Updated SPIRVUsage.rst
bwlodarcz ab017fb
Replacement of getMMI method
bwlodarcz a5882eb
Format fix
bwlodarcz 62e1342
Added command line option for DI emission
bwlodarcz 910ec71
Additional fixes
bwlodarcz 9aae9c3
Change from id to iid in CHECK-MIR
bwlodarcz 7c90529
Change s32 to s64 in test
bwlodarcz 3443716
verify-machineinstrs compliance
bwlodarcz 0d252ae
Addition to comment
bwlodarcz File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,188 @@ | ||
#include "MCTargetDesc/SPIRVBaseInfo.h" | ||
#include "MCTargetDesc/SPIRVMCTargetDesc.h" | ||
#include "SPIRVGlobalRegistry.h" | ||
#include "SPIRVRegisterInfo.h" | ||
#include "SPIRVTargetMachine.h" | ||
#include "llvm/ADT/SmallString.h" | ||
#include "llvm/CodeGen/GlobalISel/MachineIRBuilder.h" | ||
#include "llvm/CodeGen/MachineBasicBlock.h" | ||
#include "llvm/CodeGen/MachineFunction.h" | ||
#include "llvm/CodeGen/MachineFunctionPass.h" | ||
#include "llvm/CodeGen/MachineInstr.h" | ||
#include "llvm/CodeGen/MachineInstrBuilder.h" | ||
#include "llvm/CodeGen/MachineModuleInfo.h" | ||
#include "llvm/CodeGen/MachineOperand.h" | ||
#include "llvm/IR/DebugInfoMetadata.h" | ||
#include "llvm/IR/Metadata.h" | ||
#include "llvm/PassRegistry.h" | ||
#include "llvm/Support/Casting.h" | ||
#include "llvm/Support/Path.h" | ||
|
||
#define DEBUG_TYPE "spirv-nonsemantic-debug-info" | ||
|
||
bwlodarcz marked this conversation as resolved.
Show resolved
Hide resolved
|
||
namespace llvm { | ||
struct SPIRVEmitNonSemanticDI : public MachineFunctionPass { | ||
static char ID; | ||
SPIRVTargetMachine *TM; | ||
SPIRVEmitNonSemanticDI(SPIRVTargetMachine *TM); | ||
SPIRVEmitNonSemanticDI(); | ||
|
||
bool runOnMachineFunction(MachineFunction &MF) override; | ||
|
||
private: | ||
bool IsGlobalDIEmitted = false; | ||
bool emitGlobalDI(MachineFunction &MF); | ||
}; | ||
|
||
void initializeSPIRVEmitNonSemanticDIPass(PassRegistry &); | ||
|
||
FunctionPass *createSPIRVEmitNonSemanticDIPass(SPIRVTargetMachine *TM) { | ||
return new SPIRVEmitNonSemanticDI(TM); | ||
} | ||
} // namespace llvm | ||
|
||
using namespace llvm; | ||
|
||
INITIALIZE_PASS(SPIRVEmitNonSemanticDI, DEBUG_TYPE, | ||
"SPIRV NonSemantic.Shader.DebugInfo.100 emitter", false, false) | ||
|
||
char SPIRVEmitNonSemanticDI::ID = 0; | ||
|
||
SPIRVEmitNonSemanticDI::SPIRVEmitNonSemanticDI(SPIRVTargetMachine *TM) | ||
: MachineFunctionPass(ID), TM(TM) { | ||
initializeSPIRVEmitNonSemanticDIPass(*PassRegistry::getPassRegistry()); | ||
} | ||
|
||
SPIRVEmitNonSemanticDI::SPIRVEmitNonSemanticDI() : MachineFunctionPass(ID) { | ||
initializeSPIRVEmitNonSemanticDIPass(*PassRegistry::getPassRegistry()); | ||
} | ||
|
||
bool SPIRVEmitNonSemanticDI::emitGlobalDI(MachineFunction &MF) { | ||
// If this MachineFunction doesn't have any BB repeat procedure | ||
// for the next | ||
if (MF.begin() == MF.end()) { | ||
IsGlobalDIEmitted = false; | ||
return false; | ||
} | ||
|
||
// Required variables to get from metadata search | ||
LLVMContext *Context; | ||
SmallString<128> FilePath; | ||
unsigned SourceLanguage = 0; | ||
int64_t DwarfVersion = 0; | ||
int64_t DebugInfoVersion = 0; | ||
|
||
// Searching through the Module metadata to find nescessary | ||
// information like DwarfVersion or SourceLanguage | ||
{ | ||
const MachineModuleInfo &MMI = | ||
getAnalysis<MachineModuleInfoWrapperPass>().getMMI(); | ||
const Module *M = MMI.getModule(); | ||
Context = &M->getContext(); | ||
const NamedMDNode *DbgCu = M->getNamedMetadata("llvm.dbg.cu"); | ||
if (!DbgCu) | ||
return false; | ||
for (const auto *Op : DbgCu->operands()) { | ||
if (const auto *CompileUnit = dyn_cast<DICompileUnit>(Op)) { | ||
DIFile *File = CompileUnit->getFile(); | ||
sys::path::append(FilePath, File->getDirectory(), File->getFilename()); | ||
SourceLanguage = CompileUnit->getSourceLanguage(); | ||
break; | ||
} | ||
} | ||
const NamedMDNode *ModuleFlags = M->getNamedMetadata("llvm.module.flags"); | ||
for (const auto *Op : ModuleFlags->operands()) { | ||
const MDOperand &MaybeStrOp = Op->getOperand(1); | ||
if (MaybeStrOp.equalsStr("Dwarf Version")) | ||
DwarfVersion = | ||
cast<ConstantInt>( | ||
cast<ConstantAsMetadata>(Op->getOperand(2))->getValue()) | ||
->getSExtValue(); | ||
else if (MaybeStrOp.equalsStr("Debug Info Version")) | ||
DebugInfoVersion = | ||
cast<ConstantInt>( | ||
cast<ConstantAsMetadata>(Op->getOperand(2))->getValue()) | ||
->getSExtValue(); | ||
} | ||
} | ||
// NonSemantic.Shader.DebugInfo.100 global DI instruction emitting | ||
{ | ||
// Required LLVM variables for emitting logic | ||
const SPIRVInstrInfo *TII = TM->getSubtargetImpl()->getInstrInfo(); | ||
const SPIRVRegisterInfo *TRI = TM->getSubtargetImpl()->getRegisterInfo(); | ||
const RegisterBankInfo *RBI = TM->getSubtargetImpl()->getRegBankInfo(); | ||
SPIRVGlobalRegistry *GR = TM->getSubtargetImpl()->getSPIRVGlobalRegistry(); | ||
MachineRegisterInfo &MRI = MF.getRegInfo(); | ||
MachineBasicBlock &MBB = *MF.begin(); | ||
|
||
// To correct placement of a OpLabel instruction during SPIRVAsmPrinter | ||
// emission all new instructions needs to be placed after OpFunction | ||
// and before first terminator | ||
MachineIRBuilder MIRBuilder(MBB, MBB.getFirstTerminator()); | ||
|
||
// Emit OpString with FilePath which is required by DebugSource | ||
const Register StrReg = MRI.createVirtualRegister(&SPIRV::IDRegClass); | ||
MRI.setType(StrReg, LLT::scalar(32)); | ||
MachineInstrBuilder MIB = MIRBuilder.buildInstr(SPIRV::OpString); | ||
bwlodarcz marked this conversation as resolved.
Show resolved
Hide resolved
|
||
MIB.addDef(StrReg); | ||
addStringImm(FilePath, MIB); | ||
|
||
const SPIRVType *VoidTy = | ||
GR->getOrCreateSPIRVType(Type::getVoidTy(*Context), MIRBuilder); | ||
|
||
// Emit DebugSource which is required by DebugCompilationUnit | ||
const Register DebugSourceResIdReg = | ||
bwlodarcz marked this conversation as resolved.
Show resolved
Hide resolved
|
||
MRI.createVirtualRegister(&SPIRV::IDRegClass); | ||
MRI.setType(DebugSourceResIdReg, LLT::scalar(32)); | ||
MIB = MIRBuilder.buildInstr(SPIRV::OpExtInst) | ||
.addDef(DebugSourceResIdReg) | ||
.addUse(GR->getSPIRVTypeID(VoidTy)) | ||
.addImm(static_cast<int64_t>( | ||
SPIRV::InstructionSet::NonSemantic_Shader_DebugInfo_100)) | ||
.addImm(SPIRV::NonSemanticExtInst::DebugSource) | ||
.addUse(StrReg); | ||
MIB.constrainAllUses(*TII, *TRI, *RBI); | ||
GR->assignSPIRVTypeToVReg(VoidTy, DebugSourceResIdReg, MF); | ||
|
||
const SPIRVType *I32Ty = | ||
GR->getOrCreateSPIRVType(Type::getInt32Ty(*Context), MIRBuilder); | ||
|
||
// Convert DwarfVersion, DebugInfo and SourceLanguage integers to OpConstant | ||
// instructions required by DebugCompilationUnit | ||
const Register DwarfVersionReg = | ||
bwlodarcz marked this conversation as resolved.
Show resolved
Hide resolved
|
||
GR->buildConstantInt(DwarfVersion, MIRBuilder, I32Ty, false); | ||
const Register DebugInfoVersionReg = | ||
GR->buildConstantInt(DebugInfoVersion, MIRBuilder, I32Ty, false); | ||
const Register SourceLanguageReg = | ||
GR->buildConstantInt(SourceLanguage, MIRBuilder, I32Ty, false); | ||
|
||
// Emit DebugCompilationUnit | ||
const Register DebugCompUnitResIdReg = | ||
MRI.createVirtualRegister(&SPIRV::IDRegClass); | ||
MRI.setType(DebugCompUnitResIdReg, LLT::scalar(32)); | ||
MIB = MIRBuilder.buildInstr(SPIRV::OpExtInst) | ||
.addDef(DebugCompUnitResIdReg) | ||
.addUse(GR->getSPIRVTypeID(VoidTy)) | ||
.addImm(static_cast<int64_t>( | ||
SPIRV::InstructionSet::NonSemantic_Shader_DebugInfo_100)) | ||
.addImm(SPIRV::NonSemanticExtInst::DebugCompilationUnit) | ||
.addUse(DebugInfoVersionReg) | ||
.addUse(DwarfVersionReg) | ||
.addUse(DebugSourceResIdReg) | ||
.addUse(SourceLanguageReg); | ||
MIB.constrainAllUses(*TII, *TRI, *RBI); | ||
GR->assignSPIRVTypeToVReg(VoidTy, DebugCompUnitResIdReg, MF); | ||
} | ||
return true; | ||
} | ||
|
||
bool SPIRVEmitNonSemanticDI::runOnMachineFunction(MachineFunction &MF) { | ||
bool Res = false; | ||
// emitGlobalDI needs to be executed only once to avoid | ||
// emitting duplicates | ||
if (!IsGlobalDIEmitted) { | ||
IsGlobalDIEmitted = true; | ||
Res = emitGlobalDI(MF); | ||
} | ||
return Res; | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.