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

Nemanja Ivanovic via llvm-commits llvm-commits at lists.llvm.org
Tue Nov 21 09:31:22 PST 2023


nemanjai wrote:

> It seems that we need some discussions on the right semantics possibly in riscv-elf-psabi-doc. A clarification on riscv-asm-manual may be useful as well to help hand-written assembly.

I am certainly not against this.

> In addition, I asked for source code that leads to this code generation, which is not provided.
```
double NaturallyAlignedScalar = 5.;
double accessNaturallyAlignedScalar() {
  // This will demonstrate the sharing of the upper portion.
  return NaturallyAlignedScalar;
}

double NaturallyAlignedArray[4] = { 3., 4., 5., 6. };
double accessNaturallyAlignedArray() {
  // Two halves of first element accessed together,
  // all pieces of all other elements accessed separately.
  return NaturallyAlignedArray[0] + NaturallyAlignedArray[3];
}

double __attribute__((aligned(512))) OveralignedArray[64];
double accessOveralignedArray() {
  // All pieces of the array accessed together.
  return OveralignedArray[0] + OveralignedArray[63];
}

// Compile with clang -O2 --target=riscv32
```

> That answer doesn't resolve my question. This test uses a very small alignment and cannot guarantee `%hi(Var0) == %hi(Var0+4)`. As such, it cannot prove that this is an issue that should be done at the linker side rather than at the compiler side ...

I don't follow the logic here. I don't understand your claim here:
```
IIUC this requires that the compiler can prove %hi(Var0) == %hi(Var0+4),
i.e. Var0%4096 does not reside in the range [0x7fc,0x800).
A necessary but not sufficient condition is that Var0's section's alignment is at least 4096.
```
Why is it necessary that `Var0`'s section's alignment be at least 4096? An object that is aligned at 8 bytes cannot possibly reside in the address range that you mentioned. The only valid addresses for an 8-byte aligned object near the range you mentioned are `0x7f8` and `0x800` and neither is in the range you mentioned.

> (possibly disable this `%hi`-sharing feature).

This would pessimize every access and we would probably need to implement an optimization in the linker to get rid of the redundant `addi`'s. And I am not sure what problem it would solve.

> The description does not say anything about the alignment value 4096 (1<<12).

Please elaborate. I can add anything to the description that you'd like me to add. I just don't understand what you're after.

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


More information about the llvm-commits mailing list