[PATCH] D147506: [DebugLine] save one debug line entry for empty prologue

David Blaikie via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Wed Apr 26 15:39:53 PDT 2023


dblaikie added a comment.

In D147506#4298969 <https://reviews.llvm.org/D147506#4298969>, @shchenz wrote:

>   Like maybe recordSourceLine raises a flag and beginInstruction checks the flag, and if the flag is raised, it calls Asm.OutStreamer->emitDwarfLocDirective and lowers the flag. Then any redundant line directives aren't emitted, they are just overwritten.
>
> Hi @dblaikie Sorry, I don't quite understand the method you suggested. Could you please share more details? And I am also sorry for the long comment...
>
> I also tried a way in `recordSourceLine()` (See the code snippet at last), it can handle empty prologue case, but it can not handle non-empty prologue case now and I can not find a better way to fix this.
>
> The truncated input for non-empty prologue case:
>
>   ; Function Attrs: noinline nounwind optnone uwtable
>   define dso_local signext i32 @main() #0 !dbg !10 {
>   entry:
>     %retval = alloca i32, align 4  ;; prologue
>     store i32 0, ptr %retval, align 4 ;;prologue
>     ret i32 0, !dbg !15
>   }
>
>
>
>   main:                                   # @main
>   .Lfunc_begin0:
>   	.file	0 "empty_prologue" "1.c" md5 0xd1035b543709f23158df094b6d49742b
>   	.cfi_startproc
>   # %bb.0:                                # %entry
>   	li 3, 0
>   	stw 3, -12(1)
>   .Ltmp0:
>   	.loc	0 2 0                           # 1.c:2:0     ######now the first line entry of the file is put after prologue instructions.
>   	.loc	0 3 3 prologue_end              # 1.c:3:3
>   	li 3, 0
>   	blr
>
> The issue is for assembly code generation, we must emit the `.loc` once the `recordSourceLine()` is called, we can not delay it otherwise the `.loc` will be emitted in the wrong place like above case shows. `AsmPrinter::emitFunctionBody()` emits one instruction in each iteration while it iterates the instructions in a BB. The debug handler `DwarfDebug::beginInstruction()` must keep pace with `AsmPrinter::emitFunctionBody()`. Otherwise the `.loc` can not be emitted in the right place. I thought we can not change how we write the assembly file, it has to be written line by line? That is, in above example, after `AsmPrinter::emitFunctionBody()` emits prologue instructions `li` and `stw`, when `recordSourceLine()` tries to emit the first `.loc`, it can not reset the file position to the front of prologue, right?

Right, I think the solution would have to coordinate with changes to `DwarfDebug::beginInstruction` per my comment here:

> Like maybe recordSourceLine raises a flag and beginInstruction checks the flag, and if the flag is raised, it calls Asm.OutStreamer->emitDwarfLocDirective and lowers the flag. Then any redundant line directives aren't emitted, they are just overwritten.

So `recordSourceLine` wouldn't call `emitDwarfLocDirective` - it would only set some members to represent the line info.
Then `DwarfDebug::beginInstruction` would check if the line info was present - if it's present, it would call `emitDwarfLocDirective` and then zero/null/nullopt the line info, so it wouldn't get re-emitted on the next instruction.

That way if two recordSourceLine calls happen with no instruction between them, no `emitDwarfLocDirective` is called - but on the next insntruction that's emitted, the source line info would be emitted at that point.

  recordSourceLine() {
    this->lineInfo = {line, file};
  }
  beginInstruction() {
    if (this->lineInfo) {
      emitDwarfLocDirective(this->lineInfo)
      this->lineInfo = std::nullopt;
    }
    ...
  }

Basically that. (very rough on the code - I know it's more than just file/line, etc... but a rough idea at least)


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D147506/new/

https://reviews.llvm.org/D147506



More information about the llvm-commits mailing list