[all-commits] [llvm/llvm-project] e9d99e: [ELF] Clean up R_RISCV_RELAX code. NFC

Fangrui Song via All-commits all-commits at lists.llvm.org
Fri Jan 26 21:35:36 PST 2024


  Branch: refs/heads/release/18.x
  Home:   https://github.com/llvm/llvm-project
  Commit: e9d99e51834e2bf0b39c23a60f2dba5539edd17b
      https://github.com/llvm/llvm-project/commit/e9d99e51834e2bf0b39c23a60f2dba5539edd17b
  Author: Fangrui Song <i at maskray.me>
  Date:   2024-01-26 (Fri, 26 Jan 2024)

  Changed paths:
    M lld/ELF/Arch/RISCV.cpp

  Log Message:
  -----------
  [ELF] Clean up R_RISCV_RELAX code. NFC

(cherry picked from commit ccb99f221422b8de5e1ae04d3427f15878f7cd93)


  Commit: 3d02473ac538f542fb76c4aff0fb6504398c3f15
      https://github.com/llvm/llvm-project/commit/3d02473ac538f542fb76c4aff0fb6504398c3f15
  Author: Fangrui Song <i at maskray.me>
  Date:   2024-01-26 (Fri, 26 Jan 2024)

  Changed paths:
    M lld/ELF/Relocations.cpp

  Log Message:
  -----------
  [ELF] Fix terminology: TLS optimizations instead of TLS relaxation. NFC

(cherry picked from commit 849951f8759171cb6c74d3ccbcf154506fc1f0ae)


  Commit: e2521eaa1aac43b3216378d529096f08ac98cf14
      https://github.com/llvm/llvm-project/commit/e2521eaa1aac43b3216378d529096f08ac98cf14
  Author: Fangrui Song <i at maskray.me>
  Date:   2024-01-26 (Fri, 26 Jan 2024)

  Changed paths:
    M lld/ELF/Arch/RISCV.cpp
    M lld/ELF/Relocations.cpp
    A lld/test/ELF/riscv-tlsdesc-gd-mixed.s
    A lld/test/ELF/riscv-tlsdesc-relax.s
    A lld/test/ELF/riscv-tlsdesc.s

  Log Message:
  -----------
  [ELF] Implement R_RISCV_TLSDESC for RISC-V

Support
R_RISCV_TLSDESC_HI20/R_RISCV_TLSDESC_LOAD_LO12/R_RISCV_TLSDESC_ADD_LO12/R_RISCV_TLSDESC_CALL.
LOAD_LO12/ADD_LO12/CALL relocations reference a label at the HI20
location, which requires special handling. We save the value of HI20 to
be reused. Two interleaved TLSDESC code sequences, which compilers do
not generate, are unsupported.

For -no-pie/-pie links, TLSDESC to initial-exec or local-exec
optimizations are eligible. Implement the relevant hooks
(R_RELAX_TLS_GD_TO_LE, R_RELAX_TLS_GD_TO_IE): the first two instructions
are converted to NOP while the latter two are converted to a GOT load or
a lui+addi.

The first two instructions, which would be converted to NOP, are removed
instead in the presence of relaxation. Relaxation is eligible as long as
the R_RISCV_TLSDESC_HI20 relocation has a pairing R_RISCV_RELAX,
regardless of whether the following instructions have a R_RISCV_RELAX.
In addition, for the TLSDESC to LE optimization (`lui a0,<hi20>; addi a0,a0,<lo12>`),
`lui` can be removed (i.e. use the short form) if hi20 is 0.

```
// TLSDESC to LE/IE optimization
.Ltlsdesc_hi2:
  auipc a4, %tlsdesc_hi(c)                      # if relax: remove; otherwise, NOP
  load  a5, %tlsdesc_load_lo(.Ltlsdesc_hi2)(a4) # if relax: remove; otherwise, NOP
  addi  a0, a4, %tlsdesc_add_lo(.Ltlsdesc_hi2)  # if LE && !hi20 {if relax: remove; otherwise, NOP}
  jalr  t0, 0(a5), %tlsdesc_call(.Ltlsdesc_hi2)
  add   a0, a0, tp
```

The implementation carefully ensures that an instruction unrelated to
the current TLSDESC code sequence, if immediately follows a removable
instruction (HI20 or LOAD_LO12 OR (LE-specific) ADD_LO12), is not
converted to NOP.

* `riscv64-tlsdesc.s` is inspired by `i386-tlsdesc-gd.s` (https://reviews.llvm.org/D112582).
* `riscv64-tlsdesc-relax.s` tests linker relaxation.
* `riscv-tlsdesc-gd-mixed.s` is inspired by `x86-64-tlsdesc-gd-mixed.s` (https://reviews.llvm.org/D116900).

Link: https://github.com/riscv-non-isa/riscv-elf-psabi-doc/pull/373

Reviewed By: ilovepi

Pull Request: https://github.com/llvm/llvm-project/pull/79239

(cherry picked from commit 1117fdd7c16873eb389e988c6a39ad922bae0fd0)


Compare: https://github.com/llvm/llvm-project/compare/d9e26c223bdb...e2521eaa1aac


More information about the All-commits mailing list