Repository navigation
Stabilize RISC-V relax target feature - #163890
TechnoPorg wants to merge 1 commit into
Conversation
|
Target features are being changed; ensure all ABI effects are being accounted for cc @RalfJung |
|
r? @TaKO8Ki rustbot has assigned @TaKO8Ki. Use Why was this reviewer chosen?The reviewer was selected based on:
|
|
Error: Only Rust team members can ping teams. Please file an issue on GitHub at triagebot if there's a problem with this bot, or reach out on #triagebot on Zulip. |
|
@rustbot label O-riscv |
|
Error: Label risc-v can only be set by Rust team members Please file an issue on GitHub at triagebot if there's a problem with this bot, or reach out on #triagebot on Zulip. |
It would be helpful to say more explicitly what a "linker relaxation" or a "pointer relaxation" is (and whether those two are the same thing). Currently I think that maybe "which collapses access sequences into a single gp-relative access" is defining "pointer relaxation" kind of on the side as part of a larger sentence but I am not sure -- the text is hard to follow for people that don't already know about linker/pointer relaxations. Keep in mind that the audience for this report includes people that have a high-level view of Rust and its overall soundness story, but that have no idea at all about the details of linking, or risc-v.
This sounds to me like the target feature leads to UB when used incorrectly, so we cannot stabilize it as-is. Target features must be sound to disable and enable and even mix different values within a single program. Anything that affects the ABI cannot be a target feature, at least not without extra care which ensures that it is never set in a way to violate the ABI.
If you directly mess with registers, any UB you see is your own problem. So this is certainly not a valid argument. |
I've updated the report to explain what linker relaxation is and how "global pointer" relaxations specifically work. I can edit it further if it still assumes too much background knowledge or uses too much jargon (https://xkcd.com/2501/).
Yes, if someone provides a symbol named
Fair enough. Are there any similar rules around unmangled symbols (re: the point above about providing
Would this (from the updated report) be your preferred way forward? As an embedded developer whose project doesn't define |
|
Thanks for the extra explanations, that helps. (And yes the XKCD very much applies here. ;)
Yeah, pretty much. If you define an unmangled symbol
Interesting. This does seem sound by the letter of the term, but it also sounds like a quite surprising soundness contract. I don't know enough about this target feature to really judge what the best option here is, I am here mostly to ensure that we don't do anything unsound :) . Figuring out possible solutions needs input from some target/linker experts. Maybe @beetrees or @bjorn3 or @taiki-e can help us here. On which target triple do you want to use this target feature? Given your description I would expect that most of our targets enable the feature but the android ones do not. Enabling it on android would be unsound. However when I randomly picked one of them, riscv64gc-unknown-linux-gnu, it does not seem to be enabled there. So it seems I must still be missing something. |
|
There is also |
If I understand correctly |
|
(due to the ongoing GH outage I can't switch accounts? so replying from my work account)
I'd like to use it on an embedded Currently no Rust targets enable
It seems based on my quick investigation that the flag winds up here: and doesn't do anything beyond toggle that X86 option corresponding to -mrelax-relocations={yes, no} in Clang. The |
|
Oh wow |
The easiest solution then would be to enable "+relaxed" by default where useful, and to turn the target feature into an internal-only feature to make it clear that it's not planned to be stabilized. Or are there targets where it's useful to let users control this? @rustbot ping risc-v |
|
Hey RISC-V Group! This issue has been identified as a good "RISC-V candidate". |
|
Actually that's not the same as the RISC-V team listed on the target page. Very confusing. So I guess to ping the target maintainers I need to @almindor |
That feels like a reasonable step to me. Alternatively, depending on the outcome of this discussion, we could just add it by default and leave it as unstable so that the door remains open. I suppose we might just exclude all RISC-V targets listed as supporting the SCS sanitizer (which is the other use of x3 I'm aware of) (https://github.com/rust-lang/rust/blob/main/src/doc/unstable-book/src/compiler-flags/sanitizer.md#risc-v-64-family). |
|
Hey! Not an expert on linker relaxation etc., but from my side I can say that the |
|
I've been thinking about this more, and I while I think in any case it's good to enable The reason I say this is because one of the main use cases in this discussion is embedded projects, which I think we all agree benefit from the reduced code size, but may or may not define Another thing I should have noted earlier is that Rust inline assembly already forbids using |
|
Apparently the target feature is not enabled by default in clang, despite the same arguments applying there -- do you know why? What does GCC do? |
|
My understanding is that it is enabled by default in Clang as per: It is also enabled by default in GCC: https://gcc.gnu.org/onlinedocs/gcc-16.2.0/gcc/RISC-V-Options.html |
|
That seems like a good reason to also enable it by default on our side then. |
|
That sounds reasonable, and I can make a PR enabling it by default shortly. Should I repurpose this one to keep the conversation history, or open a new PR? |
|
I think a new PR is better. The discussion here will seem entirely unrelated to the PR otherwise.^^ |
…bjorn3 RISC-V: enable relax target feature by default This PR adds `+relax` to the default target features for all RISC-V targets to enable emission of linker relaxations by LLVM. It does not enable "global pointer" relaxations; see rust-lang#163890 for more context and discussion on how those relaxations work. `relax` is then marked as an internal-only target feature so that it can't be modified with `-Ctarget-feature=±relax` or used with `#[cfg(target_feature = "relax")]`, `#[target_feature = "relax"]`, none of which make sense. When building wild for `riscv64gc-unknown-linux-gnu`, it reduces binary size by 1.2%. When building my work project for `riscv32i-unknown-none-elf`, it reduces binary size by 1.9%, or by 2.1% if global pointer relaxations are enabled with `--relax-gp`. This is an alternative to rust-lang#163890 which will supersede that one if merged. cc @RalfJung, @romancardenas, @bjorn3 as participants on the previous thread. r? compiler
…bjorn3 RISC-V: enable relax target feature by default This PR adds `+relax` to the default target features for all RISC-V targets to enable emission of linker relaxations by LLVM. It does not enable "global pointer" relaxations; see rust-lang#163890 for more context and discussion on how those relaxations work. `relax` is then marked as an internal-only target feature so that it can't be modified with `-Ctarget-feature=±relax` or used with `#[cfg(target_feature = "relax")]`, `#[target_feature = "relax"]`, none of which make sense. When building wild for `riscv64gc-unknown-linux-gnu`, it reduces binary size by 1.2%. When building my work project for `riscv32i-unknown-none-elf`, it reduces binary size by 1.9%, or by 2.1% if global pointer relaxations are enabled with `--relax-gp`. This is an alternative to rust-lang#163890 which will supersede that one if merged. cc @RalfJung, @romancardenas, @bjorn3 as participants on the previous thread. r? compiler
Rollup merge of #164378 - TechnoPorg:riscv-default-relax, r=bjorn3 RISC-V: enable relax target feature by default This PR adds `+relax` to the default target features for all RISC-V targets to enable emission of linker relaxations by LLVM. It does not enable "global pointer" relaxations; see #163890 for more context and discussion on how those relaxations work. `relax` is then marked as an internal-only target feature so that it can't be modified with `-Ctarget-feature=±relax` or used with `#[cfg(target_feature = "relax")]`, `#[target_feature = "relax"]`, none of which make sense. When building wild for `riscv64gc-unknown-linux-gnu`, it reduces binary size by 1.2%. When building my work project for `riscv32i-unknown-none-elf`, it reduces binary size by 1.9%, or by 2.1% if global pointer relaxations are enabled with `--relax-gp`. This is an alternative to #163890 which will supersede that one if merged. cc @RalfJung, @romancardenas, @bjorn3 as participants on the previous thread. r? compiler
|
Now that #164378 has merged, my need for this PR has been met. If anyone else still has a reason for wanting this to be stabilized that the target spec discussion in the other PR doesn't cover, please do share it here! |
RISC-V: enable relax target feature by default This PR adds `+relax` to the default target features for all RISC-V targets to enable emission of linker relaxations by LLVM. It does not enable "global pointer" relaxations; see rust-lang/rust#163890 for more context and discussion on how those relaxations work. `relax` is then marked as an internal-only target feature so that it can't be modified with `-Ctarget-feature=±relax` or used with `#[cfg(target_feature = "relax")]`, `#[target_feature = "relax"]`, none of which make sense. When building wild for `riscv64gc-unknown-linux-gnu`, it reduces binary size by 1.2%. When building my work project for `riscv32i-unknown-none-elf`, it reduces binary size by 1.9%, or by 2.1% if global pointer relaxations are enabled with `--relax-gp`. This is an alternative to rust-lang/rust#163890 which will supersede that one if merged. cc @RalfJung, @romancardenas, @bjorn3 as participants on the previous thread. r? compiler
View all comments
Stabilization report
Summary
This stabilizes the
relaxtarget feature for RISC-V, both RV32 and RV64. It enables LLVM emission of linker relaxations for improved runtime performance and smaller binary size.It was added in #109860 and has seen no movement until recently due to concerns about global pointer relaxation, which are covered below.
Tracking:
Reference PRs:
cc @rust-lang/lang @rust-lang/lang-advisors
What is stabilized
Enabling the
relaxtarget feature in RUSTFLAGS will no longer emit a warning (and eventually a hard error, see #162235).This also enables use of
#[target_feature = "relax]and#[cfg(target_feature = "relax")].What isn't stabilized
relaxis not enabled by default on any targetsrelaxis enabled (see Global pointer relaxation)--relax-gpflag to all linker invocations on RISC-V targets, but that is not supported across all linkers and can safely be done later--relaxor--no-relax). This distinction is covered in the reference PR; unfortunately, the LLVM feature description does not make it clear when using--print target-features.Design
Reference
The reference PR linked above adds the feature to the list of supported RISC-V (32- and 64-bit) target features.
Answers to unresolved questions
Global pointer relaxation
The RISC-V ELF ABI defines a number of linker relaxations, most of which are harmless (function call relaxation, zero-page relaxation, etc). A linker relaxation is an optimization made by the linker once memory addresses have been resolved and there is enough information to "relax" overly conservative assumptions made about memory layout at compile time. For example, consider the zero-page relaxation example from the ELF ABI link above:
This original code would have been emitted because the compiler did not know the actual address the load would be resolved to. However, since during linking, the address ended up close to 0x00 (in this example, the address is exactly zero), it can be simplified to just one load, relative to the zero page:
This is perfectly fine because
x0must be hardwired to return zero, so this ends up loading from the exact same address, but with one less instruction."Global pointer" relaxations—a kind of relaxation that uses the so-called "global pointer" symbol—are the one exception to this harmlessness.
What is the global pointer?
The RISC-V ELF ABI reserves register
x3for the address of the__global_pointer$symbol. This can then be used for relative accesses of items within +-2KiB of the symbol, either explicitly or via the "global pointer" relaxation. Similarly to how the zero-page relaxation example above collapses a load to a single instruction relative to0x00, the "global pointer" relaxation collapses load/store sequences into a single instruction relative to the global pointer. This requires that the address of the__global_pointer$symbol be loaded intox3(aliased asgpbelow).Why might this be problematic?
Some targets choose not to follow the ABI: for example, Android and Fuchsia both use
x3for the shadow call stack instead (thanks Google...). This means that code compiled with gp-relative accesses will violently segfault whengpis used for something else.Keeping
relaxas unstable and only enabling it in target specs where we know the startup code initializes x3 properly would entirely sidestep this concern, since as @RalfJung has noted, it's the fault of the application developer if they cause UB by messing with registers on their own.Why is this actually fine?
Whether or not gp relaxations are applied is entirely up to the linker. Behaviour differs between LLD and GNU ld: the former defaults to
--no-relax-gp, while the latter defaults to--relax-gp. At present, I know of no other linkers which support these flags or global pointer relaxations in general.However, neither will apply gp relaxations if the
__global_pointer$symbol is not defined in the input. I interpret this to mean that if an application provides the symbol, it's their responsibility to properly initializex3, and it's not our fault if they mix and match ABI compliance.Will GP relaxation be applied?
__global_pointer$not provided__global_pointer$providedBetween LLD defaulting to not performing these relaxations and no linker performs the relaxation if the symbol is not present, I believe this question can be considered resolved.
Post-RFC changes
None.
Key points
See Global pointer relaxation.
Nightly extensions
None.
Doors closed
Stabilizing
relaxas a target feature means we cannot stabilize it later as a separate flag as discussed below.Feedback
Call for testing
Not issued.
Nightly use
Many RISC-V projects, mostly embedded, enable this already due to the binary size improvements:
relaxwarning on all riscv board builds tock/tock#5084Implementation
Major parts
This was implemented in #109860.
It only needs to be forwarded to LLVM, so the implementation is a single line at:
rust/compiler/rustc_target/src/target_features.rs
Line 705 in b57eb9a
LLVM will then emit relaxations in object files, which are applied by the linker. For most RISC-V targets, this is either LLD or GNU ld. mold and wild also support RISC-V relaxations apart from global pointer relaxations.
Coverage
The meaningful part of this feature is implemented in LLVM, where there are tests for RISC-V linker relaxations LLVM and LLD: https://github.com/search?q=repo%3Allvm%2Fllvm-project+relax+test+riscv&type=code.
Outstanding bugs
--relax-gpin order to encounter it.Outstanding FIXMEs
None.
Tool changes
None.
Breaking changes
None.
Type system, opsem
cc @rust-lang/opsem
Compile-time checks
Global pointer relaxation can unfortunately not be checked at compile time by
rustc. Any hypothetical static analysis to determine whether the x3 register is properly initialized would need to go in the linker.There is otherwise nothing that needs to be checked at compile-time.
Type system rules
N/A
Sound by default?
Unsound if a project defines the
__global_pointer$symbol, enables this target feature, and passes--relax-gp(default for GNU ld) without also initializing thex3register to the address of the symbol.This also requires the
tpregister to be initialized as per the RISC-V ELF ABI in order to apply thread pointer-relative relaxations to TLS variables. However, this needs to be done anyways for TLS to work.Apart from gp, there are no ABI concerns. See https://docs.riscv.org/reference/abi/v1.0/riscv-elf-linker-relaxation.html.
Breaks the AM?
The above combination of symbols and features will lead to undefined behaviour.
Common interactions
Temporaries
None.
Drop order
N/A
Pre-expansion / post-expansion
N/A
Edition hygiene
N/A
SemVer implications
N/A
Exposing other features
None.
History
Acknowledgments
__global_pointer$is defined.Open items
As mentioned in #162772 (comment), this is not like other target features in that it only affects codegen and ABI (via gp), and is not relevant to CPU/ISA features. As such, it feels unusual to stabilize it as a target feature. An alternative might be to stabilize it as a new flag
-Crelax, but doing so feels invasive for a RISCV-specific detail.Being able to use
#[target_feature = "relax"]is not entirely unreasonable, since one may wish to toggle relaxation for a single function only, but#[cfg(target_feature = "relax")]feels odd, since enabling relaxation should not affect how Rust code is written.cc @mati865 as the reviewer of my previous PR
@rustbot ping risc-v
No LLMs were used to write this PR.