[PATCH] D113741: [RFC][DwarfDebug][AsmPrinter] Support emitting function-local declaration for a lexical block

ChenZheng via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Sun Dec 26 19:02:13 PST 2021


shchenz added a comment.

To continue with a more accurate fix, one example for your reference. (The behavior is changed with/without this reversion):

  int main() {      // <====== function scope
    int x = 3;
    int num = 6;
    printf("%d\n", x);
    if (num) {   // <======= lexical block 1
      static int x = 45;
      printf("%d\n", x);
      if (x) {   // <======= lexical block 2
        int x = 145;
        printf("%d\n", x);
      }
    }
    return 0;
  }

Without this patch:

  <1><2a>: Abbrev Number: 2 (DW_TAG_subprogram)
     <2b>   DW_AT_low_pc      : 0x10010680
     <33>   DW_AT_high_pc     : 0xd4
     <37>   DW_AT_frame_base  : 1 byte block: 6f         (DW_OP_reg31 (r31))
     <39>   DW_AT_name        : (indirect string, offset: 0xcb): main
     <3f>   DW_AT_type        : <0x94>
     <43>   DW_AT_external    : 1
  <2><43>: Abbrev Number: 3 (DW_TAG_variable)
     <44>   DW_AT_location    : 3 byte block: 91 f0 0    (DW_OP_fbreg: 112)
     <48>   DW_AT_name        : (indirect string, offset: 0xd4): x
     <4e>   DW_AT_type        : <0x94>
  <2><52>: Abbrev Number: 3 (DW_TAG_variable)
     <53>   DW_AT_location    : 3 byte block: 91 ec 0    (DW_OP_fbreg: 108)
     <57>   DW_AT_name        : (indirect string, offset: 0xd0): num
     <5d>   DW_AT_type        : <0x94>
  <2><61>: Abbrev Number: 4 (DW_TAG_lexical_block)     <<<<<<<<<<this should be for lexical block 2, so its DIE level should be 3
     <62>   DW_AT_low_pc      : 0x1001070c
     <6a>   DW_AT_high_pc     : 0x20
  <3><6e>: Abbrev Number: 3 (DW_TAG_variable)             <<<<<<<<<this is for x=145 and nested in lexical block2, so its DIE level should be 4
     <6f>   DW_AT_location    : 3 byte block: 91 e8 0    (DW_OP_fbreg: 104)
     <73>   DW_AT_name        : (indirect string, offset: 0xd4): x
     <79>   DW_AT_type        : <0x94>
  <3><7d>: Abbrev Number: 0
  <2><7e>: Abbrev Number: 5 (DW_TAG_variable)              <<<<<<<<<<this is for static int x = 45, so there should be another lexical block DIE for lexical block 1, and its DIE level should be 2.
     <7f>   DW_AT_name        : (indirect string, offset: 0xd4): x
     <83>   DW_AT_type        : <0x94>
     <89>   DW_AT_location    : 9 byte block: 3 e8 a 3 10 0 0 0 0        (DW_OP_addr: 10030ae8)
  <2><93>: Abbrev Number: 0
  <1><94>: Abbrev Number: 6 (DW_TAG_base_type)
     <95>   DW_AT_name        : (indirect string, offset: 0x0): int
     <99>   DW_AT_encoding    : 5        (signed)
     <9a>   DW_AT_byte_size   : 4
  <1><9b>: Abbrev Number: 0

And we get wrong result for static number `x` at line 7. In debugger, it is printed as 3.

With this patch:

  0x0000002a:   DW_TAG_subprogram
                  DW_AT_low_pc    (0x0000000010010680)
                  DW_AT_high_pc   (0x0000000010010754)
                  DW_AT_frame_base        (DW_OP_reg31 X31)
                  DW_AT_name      ("main")
                  DW_AT_type      (0x00000094 "int")
                  DW_AT_external  (true)
  
  0x00000043:     DW_TAG_variable
                    DW_AT_name    ("x")          <<<<<<<<<<<<we need a lexical block for this definition
                    DW_AT_type    (0x00000094 "int")
                    DW_AT_location        (DW_OP_addr 0x10030ae8)
  
  0x00000058:     DW_TAG_variable
                    DW_AT_location        (DW_OP_fbreg +112)
                    DW_AT_name    ("x")
                    DW_AT_type    (0x00000094 "int")
  
  0x00000067:     DW_TAG_variable
                    DW_AT_location        (DW_OP_fbreg +108)
                    DW_AT_name    ("num")
                    DW_AT_type    (0x00000094 "int")
  
  0x00000076:     DW_TAG_lexical_block
                    DW_AT_low_pc  (0x000000001001070c)
                    DW_AT_high_pc (0x000000001001072c)
  
  0x00000083:       DW_TAG_variable
                      DW_AT_location      (DW_OP_fbreg +104)
                      DW_AT_name  ("x")
                      DW_AT_type  (0x00000094 "int")
  
  0x00000092:       NULL

Now we get wrong result for local variable `x` at line 3. In debugger, it is printed as `45`. (The DIE for the local variable `x` is overridden by the static variable `x` in  lexical block 1).


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D113741



More information about the llvm-commits mailing list