Skip to content

Commit 077cf81

Browse files
aparshin-intelsys_zuul
authored andcommitted
initial support for source/line debug information in VectorCompiler backend
Change-Id: I46f4d51591b54e5d4cdce1596dadca9c3fac7c5d
1 parent 0a70d2e commit 077cf81

File tree

9 files changed

+459
-4
lines changed

9 files changed

+459
-4
lines changed

IGC/VectorCompiler/include/vc/Support/Options.td

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,10 @@ let Flags = [ApiOption] in {
5656
def vc_codegen : Flag<["-"], "vc-codegen">,
5757
HelpText<"Enable vector codegenerator">;
5858

59+
// Generate debug info
60+
def vc_emit_debug : Flag<["-"], "g">,
61+
HelpText<"Enable generation of debug information">;
62+
5963
def optimize : Separate<["-"], "optimize">,
6064
HelpText<"Set optimization level to either 'none' or 'full'">,
6165
MetaVarName<"<level>">;

IGC/VectorCompiler/lib/GenXCodeGen/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ set(CODEGEN_SOURCES
2121
GenXConstants.cpp
2222
GenXCoalescing.cpp
2323
GenXDeadVectorRemoval.cpp
24+
GenXDebugInfo.cpp
2425
GenXDepressurizer.cpp
2526
GenXExtractVectorizer.cpp
2627
GenXFuncPtrsLowering.cpp

IGC/VectorCompiler/lib/GenXCodeGen/FunctionGroup.h

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,18 +78,25 @@ class FunctionGroup {
7878
// group, starting with the head Function.
7979
AssertingVH<Function> &at(unsigned i) { return Functions[i]; }
8080
typedef SmallVectorImpl<AssertingVH<Function>>::iterator iterator;
81+
typedef SmallVectorImpl<AssertingVH<Function>>::const_iterator const_iterator;
8182
iterator begin() { return Functions.begin(); }
8283
iterator end() { return Functions.end(); }
84+
const_iterator begin() const { return Functions.begin(); }
85+
const_iterator end() const { return Functions.end(); }
8386
typedef SmallVectorImpl<AssertingVH<Function>>::reverse_iterator
8487
reverse_iterator;
8588
reverse_iterator rbegin() { return Functions.rbegin(); }
8689
reverse_iterator rend() { return Functions.rend(); }
87-
size_t size() { return Functions.size(); }
90+
size_t size() const { return Functions.size(); }
8891
// accessors
8992
Function *getHead() {
9093
IGC_ASSERT(size());
9194
return *begin();
9295
}
96+
const Function *getHead() const {
97+
IGC_ASSERT(size());
98+
return *begin();
99+
}
93100
StringRef getName() { return getHead()->getName(); }
94101
LLVMContext &getContext() { return getHead()->getContext(); }
95102
Module *getModule() { return getHead()->getParent(); }
@@ -165,9 +172,12 @@ class FunctionGroupAnalysis : public ModulePass {
165172
void replaceFunction(Function *OldF, Function *NewF);
166173
// iterator for FunctionGroups in the analysis
167174
typedef SmallVectorImpl<FunctionGroup *>::iterator iterator;
175+
typedef SmallVectorImpl<FunctionGroup *>::const_iterator const_iterator;
168176
iterator begin() { return iterator(Groups.begin()); }
169177
iterator end() { return iterator(Groups.end()); }
170-
size_t size() { return Groups.size(); }
178+
const_iterator begin() const { return const_iterator(Groups.begin()); }
179+
const_iterator end() const { return const_iterator(Groups.end()); }
180+
size_t size() const { return Groups.size(); }
171181
// addToFunctionGroup : add Function F to FunctionGroup FG
172182
// Using this (rather than calling push_back directly on the FunctionGroup)
173183
// means that the mapping from F to FG will be created, and getGroup() will

IGC/VectorCompiler/lib/GenXCodeGen/GenXCisaBuilder.cpp

Lines changed: 46 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
4444

4545
#include "GenX.h"
4646
#include "GenXBackendConfig.h"
47+
#include "GenXDebugInfo.h"
4748
#include "GenXGotoJoin.h"
4849
#include "GenXIntrinsics.h"
4950
#include "GenXOCLRuntimeInfo.h"
@@ -70,6 +71,7 @@ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
7071
#include "llvm/IR/DiagnosticPrinter.h"
7172
#include "llvm/IR/Dominators.h"
7273
#include "llvm/InitializePasses.h"
74+
#include "llvm/Support/Error.h"
7375
#include "llvm/Support/Path.h"
7476
#include "llvm/Support/Regex.h"
7577
#include "llvm/Support/ScopedPrinter.h"
@@ -101,12 +103,17 @@ using namespace genx;
101103

102104
#define DEBUG_TYPE "GENX_CISA_BUILDER"
103105

104-
static cl::opt<bool> EmitVisa("emit-visa", cl::init(false), cl::Hidden,
105-
cl::desc("Generate Visa instead of fat binary."));
106106
static cl::list<std::string>
107107
FinalizerOpts("finalizer-opts", cl::Hidden, cl::ZeroOrMore,
108108
cl::desc("Additional options for finalizer."));
109109

110+
static cl::opt<bool> EmitVisa("emit-visa", cl::init(false), cl::Hidden,
111+
cl::desc("Generate Visa instead of fat binary."));
112+
113+
static cl::opt<bool> GenerateDebugInfo(
114+
"emit-debug-info", cl::init(false), cl::Hidden,
115+
cl::desc("Generate DWARF debug info for each compiled kernel"));
116+
110117
static cl::opt<std::string> AsmNameOpt("asm-name", cl::init(""), cl::Hidden,
111118
cl::desc("Output assembly code to this file during compilation."));
112119

@@ -549,6 +556,7 @@ class GenXKernelBuilder {
549556
bool NoMask = false;
550557

551558
genx::AlignmentInfo AI;
559+
const Instruction *CurrentInst = nullptr;
552560

553561
public:
554562
FunctionGroup *FG = nullptr;
@@ -1339,6 +1347,7 @@ bool GenXKernelBuilder::buildInstruction(Instruction *Inst) {
13391347
// Make the source location pending, so it is output as vISA FILE and LOC
13401348
// instructions next time an opcode is written.
13411349
const DebugLoc &DL = Inst->getDebugLoc();
1350+
CurrentInst = Inst;
13421351
if (DL) {
13431352
StringRef Filename = DL->getFilename();
13441353
if (Filename != "") {
@@ -4437,11 +4446,13 @@ void GenXKernelBuilder::addDebugInfo() {
44374446
LastFilename = PendingFilename;
44384447
}
44394448
if (PendingLine != LastLine) {
4449+
LLVM_DEBUG(dbgs() << "LOC instruction appended:" << PendingLine << "\n");
44404450
CISA_CALL(Kernel->AppendVISAMiscLOC(PendingLine));
44414451
LastLine = PendingLine;
44424452
PendingLine = 0;
44434453
}
44444454
}
4455+
GM->updateVisaDebugInfo(KernFunc, CurrentInst);
44454456
}
44464457

44474458
void GenXKernelBuilder::emitOptimizationHints() {
@@ -4470,6 +4481,7 @@ void GenXKernelBuilder::emitOptimizationHints() {
44704481
* addLabelInst : add a label instruction for a basic block or join
44714482
*/
44724483
void GenXKernelBuilder::addLabelInst(Value *BB) {
4484+
GM->updateVisaDebugInfo(KernFunc, nullptr);
44734485
// Skip this for now, because we don't know how to patch labels of branches.
44744486
if (0) { // LastLabel >= 0) {
44754487
// There has been no code since the last label, so use the same label
@@ -5614,6 +5626,10 @@ class GenXFinalizer : public ModulePass {
56145626
FunctionGroupAnalysis &FGA, const GenXSubtarget &ST,
56155627
const GenXBackendConfig &BC);
56165628

5629+
void emitDebugInformation(const GenXModule &GM,
5630+
const FunctionGroupAnalysis &FGA,
5631+
const GenXSubtarget &ST);
5632+
56175633
bool runOnModule(Module &M) {
56185634
Ctx = &M.getContext();
56195635

@@ -5634,7 +5650,12 @@ class GenXFinalizer : public ModulePass {
56345650
CISA_CALL(CisaBuilder->Compile("genxir", &ss, EmitVisa));
56355651
if (OCLInfo)
56365652
fillOCLRuntimeInfo(*OCLInfo, GM, FGA, ST, BC);
5653+
56375654
dbgs() << CisaBuilder->GetCriticalMsg();
5655+
5656+
if (GenerateDebugInfo)
5657+
emitDebugInformation(GM, FGA, ST);
5658+
56385659
GM.DestroyCISABuilder();
56395660
GM.DestroyVISAAsmReader();
56405661
Out << ss.str();
@@ -5753,6 +5774,29 @@ void GenXFinalizer::fillOCLRuntimeInfo(GenXOCLRuntimeInfo &OCLInfo,
57535774
freeBlock(GenBin);
57545775
}
57555776
}
5777+
void GenXFinalizer::emitDebugInformation(const GenXModule &GM,
5778+
const FunctionGroupAnalysis &FGA,
5779+
const GenXSubtarget &ST) {
5780+
for (const auto *FG : FGA) {
5781+
const auto *KF = FG->getHead();
5782+
llvm::SmallVector<char, 1000> ElfImage;
5783+
5784+
auto Err =
5785+
genx::generateDebugInfo(ElfImage, GM, *KF, ST.getTargetTriple().str());
5786+
if (Err)
5787+
llvm::report_fatal_error(toString(std::move(Err)));
5788+
5789+
std::error_code EC;
5790+
llvm::raw_fd_ostream OS(("dbg_" + KF->getName() + ".elf").str(), EC);
5791+
if (!EC) {
5792+
OS << StringRef(ElfImage.data(), ElfImage.size());
5793+
OS.close();
5794+
}
5795+
5796+
if (EC)
5797+
llvm::report_fatal_error(EC.message());
5798+
}
5799+
}
57565800

57575801
void GenXModule::clearFinalizerArgs(std::vector<const char*>& Owner) const {
57585802
std::for_each(Owner.begin(), Owner.end(), [](const char* a) { delete []a; });

0 commit comments

Comments
 (0)