Skip to content

Commit 81cf7d2

Browse files
committed
Fix location emission
1 parent 2661fa3 commit 81cf7d2

File tree

10 files changed

+103
-82
lines changed

10 files changed

+103
-82
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -271,3 +271,6 @@ gallery/how_to/work_with_microtvm/micro_tvmc.py
271271

272272
# Printed TIR code on disk
273273
*.tir
274+
275+
# GDB history file
276+
.gdb_history

src/printer/tir_text_printer_debug.cc

Lines changed: 43 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,25 +25,64 @@
2525

2626
#include "tir_text_printer_debug.h"
2727

28+
#include <optional>
2829
#include <string>
2930

3031
#include "text_printer.h"
3132

3233
namespace tvm {
3334
namespace tir {
3435

35-
std::string span_text(const Span& span) {
36+
std::optional<std::string> span_text(const Span& span) {
3637
if (!span.defined()) {
37-
return "missing";
38+
return std::nullopt;
39+
}
40+
41+
std::string source("main.tir");
42+
if (span->source_name.defined() && span->source_name->name.get()) {
43+
source = span->source_name->name;
3844
}
39-
std::string source("file");
4045
return source + ":" + std::to_string(span->line) + ":" + std::to_string(span->column);
4146
}
4247

48+
template <typename ObjectPtr>
49+
void add_all_relevant_lines(const std::vector<std::tuple<const ObjectPtr*, size_t>>& data,
50+
size_t current_line, Doc* output) {
51+
ICHECK(output) << "output must be a valid Doc";
52+
for (const auto& item : data) {
53+
if (std::get<1>(item) != current_line - 1) {
54+
// Item is not relevant for this line, skip it
55+
continue;
56+
}
57+
58+
// Print out the item's span info if present
59+
auto text = span_text(std::get<0>(item)->span);
60+
if (text.has_value()) {
61+
*output << *text;
62+
} else {
63+
*output << "missing";
64+
}
65+
*output << ", ";
66+
}
67+
}
68+
4369
Doc TIRTextPrinterDebug::NewLine() {
4470
current_line_ += 1;
4571

46-
return TIRTextPrinter::NewLine();
72+
if (!show_spans_) {
73+
return TIRTextPrinter::NewLine();
74+
}
75+
76+
Doc output;
77+
78+
output << " [";
79+
80+
add_all_relevant_lines(exprs_by_line_, current_line_, &output);
81+
add_all_relevant_lines(stmts_by_line_, current_line_, &output);
82+
83+
output << "]" << TIRTextPrinter::NewLine();
84+
85+
return output;
4786
}
4887

4988
#define X(TypeName) \

src/printer/tir_text_printer_debug.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,8 @@ namespace tir {
3737

3838
class TIRTextPrinterDebug : public TIRTextPrinter {
3939
public:
40-
TIRTextPrinterDebug() : TIRTextPrinter(false, &meta_), current_line_(1) {}
40+
explicit TIRTextPrinterDebug(bool show_spans)
41+
: TIRTextPrinter(false, &meta_), current_line_(1), show_spans_(show_spans) {}
4142

4243
std::vector<std::tuple<const PrimExprNode*, size_t>> GetExprsByLine() const {
4344
return exprs_by_line_;
@@ -61,6 +62,9 @@ class TIRTextPrinterDebug : public TIRTextPrinter {
6162
// Line that the printer is currently printing
6263
size_t current_line_;
6364

65+
// Whether to include spans relevant to each line before a newline or not
66+
bool show_spans_;
67+
6468
// Record of all stmts and exprs and their corresponding line
6569
std::vector<std::tuple<const StmtNode*, size_t>> stmts_by_line_;
6670
std::vector<std::tuple<const PrimExprNode*, size_t>> exprs_by_line_;

src/target/llvm/codegen_cpu.cc

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -186,11 +186,16 @@ void CodeGenCPU::Init(const std::string& module_name, LLVMTarget* llvm_target, b
186186
llvm::DISubprogram* CodeGenCPU::CreateDebugFunction(const PrimFunc& f) {
187187
#if TVM_LLVM_VERSION >= 50
188188
llvm::SmallVector<llvm::Metadata*, 4> paramTys;
189+
190+
paramTys.push_back(GetDebugType(f->ret_type));
191+
for (const auto& param : f->params) {
192+
paramTys.push_back(GetDebugType(GetType(param)));
193+
}
194+
189195
auto* DIFunctionTy = dbg_info_->di_builder_->createSubroutineType(
190196
dbg_info_->di_builder_->getOrCreateTypeArray(paramTys));
191197

192-
// TODO(driazati): add the right argument info to the function
193-
bool local_to_unit = false;
198+
bool local_to_unit = llvm::GlobalVariable::isLocalLinkage(llvm::GlobalValue::InternalLinkage);
194199

195200
#if TVM_LLVM_VERSION >= 80
196201
auto SPFlags = llvm::DISubprogram::toSPFlags(local_to_unit, /*IsDefinition=*/true,
@@ -207,14 +212,15 @@ llvm::DISubprogram* CodeGenCPU::CreateDebugFunction(const PrimFunc& f) {
207212
/*Flags=*/llvm::DINode::FlagPrototyped, /*isOptimized=*/true);
208213
#endif
209214
return DIFunction;
215+
#else
216+
return nullptr;
210217
#endif
211218
}
212219

213220
void CodeGenCPU::AddFunction(const PrimFunc& f) {
214221
#if TVM_LLVM_VERSION >= 50
215222
di_subprogram_ = CreateDebugFunction(f);
216223
#endif
217-
218224
EmitDebugLocation(f->span);
219225
CodeGenLLVM::AddFunction(f);
220226
if (f_tvm_register_system_symbol_ != nullptr) {
@@ -272,6 +278,9 @@ void CodeGenCPU::AddDebugInformation(PrimFunc f_tir, llvm::Function* f_llvm) {
272278
#endif
273279
}
274280

281+
llvm::DIType* CodeGenCPU::GetDebugType(const Type& ty_tir) {
282+
return GetDebugType(ty_tir, GetLLVMType(ty_tir));
283+
}
275284
llvm::DIType* CodeGenCPU::GetDebugType(const Type& ty_tir, llvm::Type* ty_llvm) {
276285
if (ty_llvm == t_void_) {
277286
return nullptr;
@@ -952,7 +961,6 @@ llvm::Value* CodeGenCPU::CreateCallPacked(const CallNode* op, bool use_string_lo
952961
}
953962

954963
llvm::Value* CodeGenCPU::CreateCallTracePacked(const CallNode* op) {
955-
EmitDebugLocation(op);
956964
ICHECK_EQ(op->args.size(), 6U);
957965
PackedCall pc = MakeCallPackedLowered(op->args, op->dtype, op->args[3].as<IntImmNode>()->value,
958966
op->args[4].as<IntImmNode>()->value, true);
@@ -1388,7 +1396,6 @@ void CodeGenCPU::AddStartupFunction() {
13881396
}
13891397

13901398
llvm::Value* CodeGenCPU::CreateIntrinsic(const CallNode* op) {
1391-
EmitDebugLocation(op);
13921399
if (op->op.same_as(builtin::tvm_call_packed_lowered())) {
13931400
return CreateCallPacked(op, true /* use_string_lookup */);
13941401
} else if (op->op.same_as(builtin::tvm_call_trace_packed_lowered())) {

src/target/llvm/codegen_cpu.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,7 @@ class CodeGenCPU : public CodeGenLLVM {
195195

196196
// Get the DWARF type corresponding to the LLVM type |ty|. The current API in practice only
197197
// generates |int32|, and |int8*|.
198+
llvm::DIType* GetDebugType(const Type& ty_tir);
198199
llvm::DIType* GetDebugType(const Type& ty_tir, llvm::Type* ty_llvm);
199200
// Adds the DWARF debug information for |function| to |dbg_info_|.
200201
void AddDebugInformation(PrimFunc f_tir, llvm::Function* f_llvm);

0 commit comments

Comments
 (0)