[lld] [RISCV] Disable gp relaxation if part of object unreachable (PR #72655)

Fangrui Song via llvm-commits llvm-commits at lists.llvm.org
Wed Nov 22 09:20:43 PST 2023


MaskRay wrote:

Thanks for the C source example. It demonstrates the problem and justifies %hi-sharing codegen by the compiler.
This is exactly the information the description can have to clarify the issue.

I think you can edit the description to include the following
```
double NaturallyAlignedScalar = 5.;
double accessNaturallyAlignedScalar() { return NaturallyAlignedScalar; }
//      lui     a1, %hi(NaturallyAlignedScalar)
//      lw      a0, %lo(NaturallyAlignedScalar)(a1)
//      lw      a1, %lo(NaturallyAlignedScalar+4)(a1)

double NaturallyAlignedArray[4] = { 3., 4., 5., 6. };
double accessNaturallyAlignedArray() {
  return NaturallyAlignedArray[0] + NaturallyAlignedArray[3];
}
//      lui     a2, %hi(NaturallyAlignedArray)
//      lw      a0, %lo(NaturallyAlignedArray)(a2)
//      lw      a1, %lo(NaturallyAlignedArray+4)(a2)
//      addi    a3, a2, %lo(NaturallyAlignedArray)
//      lw      a2, 24(a3)
//      lw      a3, 28(a3)
```

For the `accessNaturallyAlignedScalar` codegen, the compiler must prove that `%hi(NaturallyAlignedScalar) = %hi(NaturallyAlignedScalar+4)`, i.e. `NaturallyAlignedScalar%0x800` is out of [0x7fc,0x800).
This requirement is satisfied because `NaturallyAlignedScalar` is aligned by 8.

---

When I commented on [0x7fc,0x800) the first time, I did not think deeply.
The alignment makes the condition satisfied. This analysis is important and should be included in the description to justify that the compiler codegen can reuse `%hi20`.

I have created https://github.com/riscv-non-isa/riscv-elf-psabi-doc/pull/408 to clarify that
multiple R_RISCV_LO12_I/R_RISCV_LO12_S can share one single R_RISCV_HI20.

---

The patch handles
```
// when gp < var
lui     a1, %hi(var)
lw      a0, %lo(var)(a1)
lw      a1, %lo(var+4)(a1)
```

but not
```
// when var < gp
lui     a1, %hi(var+4)
lw      a0, %lo(var+4)(a1)
lw      a1, %lo(var)(a1)
```

We need a test for the latter case as well.



https://github.com/llvm/llvm-project/pull/72655


More information about the llvm-commits mailing list