@@ -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." ));
106106static 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+
110117static 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
553561public:
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
44474458void GenXKernelBuilder::emitOptimizationHints () {
@@ -4470,6 +4481,7 @@ void GenXKernelBuilder::emitOptimizationHints() {
44704481 * addLabelInst : add a label instruction for a basic block or join
44714482 */
44724483void 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
57575801void GenXModule::clearFinalizerArgs (std::vector<const char *>& Owner) const {
57585802 std::for_each (Owner.begin (), Owner.end (), [](const char * a) { delete [] a; });
0 commit comments