@@ -161,7 +161,9 @@ class ASRToLLVMVisitor : public ASR::BaseVisitor<ASRToLLVMVisitor>
161
161
std::unique_ptr<llvm::Module> module;
162
162
std::unique_ptr<llvm::IRBuilder<>> builder;
163
163
Platform platform;
164
- bool enable_debug_info;
164
+ bool emit_debug_info;
165
+ std::string infile;
166
+ bool emit_debug_line_column;
165
167
Allocator &al;
166
168
167
169
llvm::Value *tmp;
@@ -239,11 +241,15 @@ class ASRToLLVMVisitor : public ASR::BaseVisitor<ASRToLLVMVisitor>
239
241
llvm::DIFile *debug_Unit;
240
242
241
243
ASRToLLVMVisitor (Allocator &al, llvm::LLVMContext &context, Platform platform,
242
- bool enable_debug_info, diag::Diagnostics &diagnostics) :
244
+ bool emit_debug_info, std::string infile, bool emit_debug_line_column,
245
+ diag::Diagnostics &diagnostics) :
243
246
diag{diagnostics},
244
247
context (context),
245
248
builder (std::make_unique<llvm::IRBuilder<>>(context)),
246
- platform{platform}, enable_debug_info{enable_debug_info},
249
+ platform{platform},
250
+ emit_debug_info{emit_debug_info},
251
+ infile{infile},
252
+ emit_debug_line_column{emit_debug_line_column},
247
253
al{al},
248
254
prototype_only (false ),
249
255
llvm_utils (std::make_unique<LLVMUtils>(context, builder.get())),
@@ -369,11 +375,24 @@ class ASRToLLVMVisitor : public ASR::BaseVisitor<ASRToLLVMVisitor>
369
375
}
370
376
}
371
377
378
+ void debug_get_line_column (const uint32_t &loc_first,
379
+ uint32_t &line, uint32_t &column) {
380
+ LocationManager lm;
381
+ lm.in_filename = infile;
382
+ lm.init_simple (LFortran::read_file (infile));
383
+ lm.pos_to_linecol (lm.output_to_input_pos (loc_first, false ), line, column);
384
+ }
385
+
372
386
template <typename T>
373
387
void debug_emit_loc (const T &x) {
374
388
Location loc = x.base .base .loc ;
375
- uint64_t line = loc.first ;
376
- uint64_t column = 0 ;
389
+ uint32_t line, column;
390
+ if (emit_debug_line_column) {
391
+ debug_get_line_column (loc.first , line, column);
392
+ } else {
393
+ line = loc.first ;
394
+ column = 0 ;
395
+ }
377
396
builder->SetCurrentDebugLocation (
378
397
llvm::DILocation::get (debug_current_scope->getContext (),
379
398
line, column, debug_current_scope));
@@ -385,8 +404,12 @@ class ASRToLLVMVisitor : public ASR::BaseVisitor<ASRToLLVMVisitor>
385
404
debug_CU->getFilename (),
386
405
debug_CU->getDirectory ());
387
406
llvm::DIScope *FContext = debug_Unit;
388
- uint64_t LineNo, ScopeLine;
389
- LineNo = ScopeLine = 0 ;
407
+ uint32_t line, column;
408
+ if (emit_debug_line_column) {
409
+ debug_get_line_column (x.base .base .loc .first , line, column);
410
+ } else {
411
+ line = 0 ;
412
+ }
390
413
std::string fn_debug_name = x.m_name ;
391
414
llvm::DIBasicType *return_type_info = nullptr ;
392
415
if constexpr (std::is_same_v<T, ASR::Function_t>){
@@ -405,7 +428,7 @@ class ASRToLLVMVisitor : public ASR::BaseVisitor<ASRToLLVMVisitor>
405
428
DBuilder->getOrCreateTypeArray (return_type_info));
406
429
SP = DBuilder->createFunction (
407
430
FContext, fn_debug_name, llvm::StringRef (), debug_Unit,
408
- LineNo , return_type, ScopeLine,
431
+ line , return_type, 0 , // TODO: ScopeLine
409
432
llvm::DINode::FlagPrototyped,
410
433
llvm::DISubprogram::SPFlagDefinition);
411
434
debug_current_scope = SP;
@@ -1147,10 +1170,10 @@ class ASRToLLVMVisitor : public ASR::BaseVisitor<ASRToLLVMVisitor>
1147
1170
module = std::make_unique<llvm::Module>(" LFortran" , context);
1148
1171
module->setDataLayout (" " );
1149
1172
1150
- if (enable_debug_info ) {
1173
+ if (emit_debug_info ) {
1151
1174
DBuilder = std::make_unique<llvm::DIBuilder>(*module);
1152
1175
debug_CU = DBuilder->createCompileUnit (
1153
- llvm::dwarf::DW_LANG_C, DBuilder->createFile (" xxexpr.py " , " /yy/ " ),
1176
+ llvm::dwarf::DW_LANG_C, DBuilder->createFile (infile , " . " ),
1154
1177
" LPython Compiler" , false , " " , 0 );
1155
1178
}
1156
1179
@@ -2249,7 +2272,7 @@ class ASRToLLVMVisitor : public ASR::BaseVisitor<ASRToLLVMVisitor>
2249
2272
llvm::Function::ExternalLinkage, " main" , module.get ());
2250
2273
llvm::BasicBlock *BB = llvm::BasicBlock::Create (context,
2251
2274
" .entry" , F);
2252
- if (enable_debug_info ) {
2275
+ if (emit_debug_info ) {
2253
2276
llvm::DISubprogram *SP;
2254
2277
debug_emit_function (x, SP);
2255
2278
F->setSubprogram (SP);
@@ -2266,7 +2289,7 @@ class ASRToLLVMVisitor : public ASR::BaseVisitor<ASRToLLVMVisitor>
2266
2289
dict_api_sc->set_is_dict_present (is_dict_present_copy_sc);
2267
2290
2268
2291
// Finalize the debug info.
2269
- if (enable_debug_info ) DBuilder->finalize ();
2292
+ if (emit_debug_info ) DBuilder->finalize ();
2270
2293
}
2271
2294
2272
2295
/*
@@ -2599,20 +2622,26 @@ class ASRToLLVMVisitor : public ASR::BaseVisitor<ASRToLLVMVisitor>
2599
2622
}
2600
2623
}
2601
2624
llvm::AllocaInst *ptr = builder->CreateAlloca (type, nullptr , v->m_name );
2602
- if (enable_debug_info ) {
2625
+ if (emit_debug_info ) {
2603
2626
// Reset the debug location
2604
2627
builder->SetCurrentDebugLocation (nullptr );
2605
- uint64_t LineNo = v->base .base .loc .first ;
2628
+ uint32_t line, column;
2629
+ if (emit_debug_line_column) {
2630
+ debug_get_line_column (v->base .base .loc .first , line, column);
2631
+ } else {
2632
+ line = v->base .base .loc .first ;
2633
+ column = 0 ;
2634
+ }
2606
2635
std::string type_name;
2607
2636
uint32_t type_size, type_encoding;
2608
2637
get_type_debug_info (v->m_type , type_name, type_size,
2609
2638
type_encoding);
2610
2639
llvm::DILocalVariable *debug_var = DBuilder->createParameterVariable (
2611
- debug_current_scope, v->m_name , ++debug_arg_count, debug_Unit, LineNo ,
2640
+ debug_current_scope, v->m_name , ++debug_arg_count, debug_Unit, line ,
2612
2641
DBuilder->createBasicType (type_name, type_size, type_encoding), true );
2613
2642
DBuilder->insertDeclare (ptr, debug_var, DBuilder->createExpression (),
2614
2643
llvm::DILocation::get (debug_current_scope->getContext (),
2615
- LineNo , 0 , debug_current_scope), builder->GetInsertBlock ());
2644
+ line , 0 , debug_current_scope), builder->GetInsertBlock ());
2616
2645
}
2617
2646
2618
2647
if ( ASR::is_a<ASR::Struct_t>(*v->m_type ) ) {
@@ -3183,7 +3212,7 @@ class ASRToLLVMVisitor : public ASR::BaseVisitor<ASRToLLVMVisitor>
3183
3212
dict_api_sc->set_is_dict_present (is_dict_present_copy_sc);
3184
3213
3185
3214
// Finalize the debug info.
3186
- if (enable_debug_info ) DBuilder->finalize ();
3215
+ if (emit_debug_info ) DBuilder->finalize ();
3187
3216
}
3188
3217
3189
3218
void instantiate_function (const ASR::Function_t &x){
@@ -3218,19 +3247,19 @@ class ASRToLLVMVisitor : public ASR::BaseVisitor<ASRToLLVMVisitor>
3218
3247
llvm::Function::ExternalLinkage, fn_name, module.get ());
3219
3248
3220
3249
// Add Debugging information to the LLVM function F
3221
- if (enable_debug_info ) {
3250
+ if (emit_debug_info ) {
3222
3251
debug_emit_function (x, SP);
3223
3252
F->setSubprogram (SP);
3224
3253
}
3225
3254
} else {
3226
3255
uint32_t old_h = llvm_symtab_fn_names[fn_name];
3227
3256
F = llvm_symtab_fn[old_h];
3228
- if (enable_debug_info ) {
3257
+ if (emit_debug_info ) {
3229
3258
SP = (llvm::DISubprogram*) llvm_symtab_fn_discope[old_h];
3230
3259
}
3231
3260
}
3232
3261
llvm_symtab_fn[h] = F;
3233
- if (enable_debug_info ) llvm_symtab_fn_discope[h] = SP;
3262
+ if (emit_debug_info ) llvm_symtab_fn_discope[h] = SP;
3234
3263
3235
3264
// Instantiate (pre-declare) all nested interfaces
3236
3265
for (auto &item : x.m_symtab ->get_scope ()) {
@@ -3427,12 +3456,12 @@ class ASRToLLVMVisitor : public ASR::BaseVisitor<ASRToLLVMVisitor>
3427
3456
parent_function = &x;
3428
3457
parent_function_hash = h;
3429
3458
llvm::Function* F = llvm_symtab_fn[h];
3430
- if (enable_debug_info ) debug_current_scope = llvm_symtab_fn_discope[h];
3459
+ if (emit_debug_info ) debug_current_scope = llvm_symtab_fn_discope[h];
3431
3460
proc_return = llvm::BasicBlock::Create (context, " return" );
3432
3461
llvm::BasicBlock *BB = llvm::BasicBlock::Create (context,
3433
3462
" .entry" , F);
3434
3463
builder->SetInsertPoint (BB);
3435
- if (enable_debug_info ) debug_emit_loc (x);
3464
+ if (emit_debug_info ) debug_emit_loc (x);
3436
3465
declare_args (x, *F);
3437
3466
declare_local_vars (x);
3438
3467
}
@@ -3715,7 +3744,7 @@ class ASRToLLVMVisitor : public ASR::BaseVisitor<ASRToLLVMVisitor>
3715
3744
}
3716
3745
3717
3746
void visit_Assignment (const ASR::Assignment_t &x) {
3718
- if (enable_debug_info ) debug_emit_loc (x);
3747
+ if (emit_debug_info ) debug_emit_loc (x);
3719
3748
if ( x.m_overloaded ) {
3720
3749
this ->visit_stmt (*x.m_overloaded );
3721
3750
return ;
@@ -6014,7 +6043,7 @@ class ASRToLLVMVisitor : public ASR::BaseVisitor<ASRToLLVMVisitor>
6014
6043
}
6015
6044
6016
6045
void visit_SubroutineCall (const ASR::SubroutineCall_t &x) {
6017
- if (enable_debug_info ) debug_emit_loc (x);
6046
+ if (emit_debug_info ) debug_emit_loc (x);
6018
6047
if ( ASRUtils::is_intrinsic_optimization (x.m_name ) ) {
6019
6048
ASR::Function_t* routine = ASR::down_cast<ASR::Function_t>(
6020
6049
ASRUtils::symbol_get_past_external (x.m_name ));
@@ -6356,12 +6385,14 @@ Result<std::unique_ptr<LLVMModule>> asr_to_llvm(ASR::TranslationUnit_t &asr,
6356
6385
diag::Diagnostics &diagnostics,
6357
6386
llvm::LLVMContext &context, Allocator &al,
6358
6387
LCompilers::PassManager& pass_manager,
6359
- CompilerOptions &co, const std::string &run_fn)
6388
+ CompilerOptions &co, const std::string &run_fn,
6389
+ const std::string &infile)
6360
6390
{
6361
6391
#if LLVM_VERSION_MAJOR >= 15
6362
6392
context.setOpaquePointers (false );
6363
6393
#endif
6364
- ASRToLLVMVisitor v (al, context, co.platform , co.arg_g , diagnostics);
6394
+ ASRToLLVMVisitor v (al, context, co.platform , co.emit_debug_info , infile,
6395
+ co.emit_debug_line_column , diagnostics);
6365
6396
LCompilers::PassOptions pass_options;
6366
6397
pass_options.run_fun = run_fn;
6367
6398
pass_options.always_run = false ;
0 commit comments