Skip to content

Stabilize RISC-V relax target feature - #163890

Closed
TechnoPorg wants to merge 1 commit into
rust-lang:mainfrom
TechnoPorg:push-pvolwsksspmk
Closed

TechnoPorg wants to merge 1 commit into
rust-lang:mainfrom
TechnoPorg:push-pvolwsksspmk

Conversation

@TechnoPorg

@TechnoPorg TechnoPorg commented Oct 6, 2026 •

Copy link
Copy Markdown
Contributor

View all comments

Stabilization report

Summary

This stabilizes the relax target 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 relax target feature in RUSTFLAGS will no longer emit a warning (and eventually a hard error, see #162235).

# accepted after this PR
RUSTFLAGS="-Ctarget-feature=+relax" cargo build

This also enables use of #[target_feature = "relax] and #[cfg(target_feature = "relax")].

What isn't stabilized

  • relax is not enabled by default on any targets
    • This is easily done in the future by adjusting the target spec, and was possible before this PR
  • Global pointer relaxations are not automatically enabled when relax is enabled (see Global pointer relaxation)
    • This could be done by prepending the --relax-gp flag to all linker invocations on RISC-V targets, but that is not supported across all linkers and can safely be done later
  • This does not control whether or not the linker will apply the emitted relaxations (--relax or --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:

# original assembly
# lui = "load upper immediate". Here, place the immediate value "0" into bits [31:12] of destination register t0, and zero the lower twelve bits
# The R_RISCV_* comments after the instruction are relocations. The part that matters here is that LLVM will emit specific relocations with the relax feature enabled that tell the linker to possibly perform relaxation here 
    lui t0, 0       # R_RISCV_HI20 (symbol), R_RISCV_RELAX
# lw = "load word". Here, load the contents of address t0 + offset "0" into destination register t1
    lw t1, 0(t0)    # R_RISCV_LO12_I (symbol), R_RISCV_RELAX

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:

# relaxed assembly
    lw t1, <address-of-symbol>(x0)

This is perfectly fine because x0 must 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 x3 for 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 to 0x00, 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 into x3 (aliased as gp below).

# original assembly
    lui t0, 0       # R_RISCV_HI20 (symbol), R_RISCV_RELAX
    lw t1, 0(t0)    # R_RISCV_LO12_I (symbol), R_RISCV_RELAX
# relaxed assembly
# in this example, the global pointer symbol is close enough to our hypothetical target address 0x0 that 0x0 can be expressed as relative to gp
    lw t1, <gp-offset-for-symbol>(gp)

Why might this be problematic?

Some targets choose not to follow the ABI: for example, Android and Fuchsia both use x3 for the shadow call stack instead (thanks Google...). This means that code compiled with gp-relative accesses will violently segfault when gp is used for something else.

Keeping relax as 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 initialize x3, and it's not our fault if they mix and match ABI compliance.

Will GP relaxation be applied?

--no-relax-gp --relax-gp
Symbol named __global_pointer$ not provided No No
Symbol named __global_pointer$ provided No Yes

Between 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 relax as 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:

Implementation

Major parts

This was implemented in #109860.

It only needs to be forwarded to LLVM, so the implementation is a single line at:

("relax", Unstable(sym::riscv_target_feature), &[]),

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

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 the x3 register to the address of the symbol.

This also requires the tp register 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

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.

@rustbot

rustbot commented Oct 6, 2026

Copy link
Copy Markdown
Collaborator

Target features are being changed; ensure all ABI effects are being accounted for

cc @RalfJung

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Oct 6, 2026
@rustbot

rustbot commented Oct 6, 2026

Copy link
Copy Markdown
Collaborator

r? @TaKO8Ki

rustbot has assigned @TaKO8Ki.
They will have a look at your PR within the next two weeks and either review your PR or reassign to another reviewer.

Use r? to explicitly pick a reviewer

Why was this reviewer chosen?

The reviewer was selected based on:

  • Owners of files modified in this PR: compiler
  • compiler expanded to 77 candidates
  • Random selection from 18 candidates

@rustbot

rustbot commented Oct 6, 2026

Copy link
Copy Markdown
Collaborator

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.

@TechnoPorg

TechnoPorg commented Oct 6, 2026 •

Copy link
Copy Markdown
Contributor Author

@rustbot label O-riscv

@rustbot

rustbot commented Oct 6, 2026

Copy link
Copy Markdown
Collaborator

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.

@rustbot rustbot added the O-riscv Target: RISC-V architecture label Oct 6, 2026
@RalfJung

RalfJung commented Oct 7, 2026 •

Copy link
Copy Markdown
Member

What is the global pointer?

The RISC-V ELF ABI reserves register x3 for 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, which collapses access sequences into a single gp-relative access. LLVM automatically emits global pointer relaxations when relax is enabled.

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.

Why might this be problematic?

Some targets choose not to follow the ABI: for example, Android and Fuchsia both use x3 for the shadow call stack instead (thanks Google...). This means that code compiled with gp-relative accesses will violently segfault when gp is used for something else.

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.

Notably, keeping relax as unstable and only enabling it in target specs does not sidestep this concern, since it's entirely up to the application developer to provide/initialize gp or not as they see fit.

If you directly mess with registers, any UB you see is your own problem. So this is certainly not a valid argument.

@TechnoPorg

TechnoPorg commented Oct 7, 2026 •

Copy link
Copy Markdown
Contributor Author

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.

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/).

This sounds to me like the target feature leads to UB when used incorrectly, so we cannot stabilize it as-is.

Yes, if someone provides a symbol named __global_pointer, does not initialize x3 with the address of that symbol, and enables this target feature, it will lead to UB.

If you directly mess with registers, any UB you see is your own problem. So this is certainly not a valid argument.

Fair enough. Are there any similar rules around unmangled symbols (re: the point above about providing __global_pointer)?

Keeping relax as 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 below, it's the fault of the application developer if they cause UB by messing with registers on their own.

Would this (from the updated report) be your preferred way forward? As an embedded developer whose project doesn't define __global_pointer, I would be disappointed to see relax made inaccessible for my (sound) use case, but it's certainly better than keeping it inaccessible to everyone.

@RalfJung

RalfJung commented Oct 7, 2026 •

Copy link
Copy Markdown
Member

Thanks for the extra explanations, that helps. (And yes the XKCD very much applies here. ;)

Fair enough. Are there any similar rules around unmangled symbols (re: the point above about providing __global_pointer)?

Yeah, pretty much. If you define an unmangled symbol memcmp and do some arbitrary nonsense in there (or even just make it always return 0), that can also cause UB.

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 initialize x3, and it's not our fault if they mix and match ABI compliance.

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.

@RalfJung

RalfJung commented Oct 7, 2026

Copy link
Copy Markdown
Member

There is also -Zrelax-elf-relocations, does that have anything to do with this?

@bjorn3

bjorn3 commented Oct 7, 2026

Copy link
Copy Markdown
Member

There is also -Zrelax-elf-relocations, does that have anything to do with this?

--relax-gp is a linker relaxation that is only valid in some cases. -Zrelax-elf-relocations enables all linker relaxations that are always valid (for example on riscv a R_RISCV_RELAX reloc allows the linker to replace auipc+jalr with a single jal when the callee is within +/- 1MiB)

On which target triple do you want to use this target feature?

If I understand correctly --relax-gp is only really useful on embedded targets.

@maya-qorsa

maya-qorsa commented Oct 7, 2026 •

Copy link
Copy Markdown

(due to the ongoing GH outage I can't switch accounts? so replying from my work account)

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.

I'd like to use it on an embedded riscv32i-unknown-none-elf project.

Currently no Rust targets enable relax by default. There was a suggestion years ago to do so (#t-compiler/risc-v > Should `-C target-feature=+relax` become the default?), but it didn't go anywhere until I recently started taking a look at this. It should be OK to do so, and in fact clang and gcc both default to -mrelax. Although we're all working on Rust in order to not follow the decisions made in the C/C++ ecosystem, it is helpful to see that they haven't run into problems doing so.

There is also -Zrelax-elf-relocations, does that have anything to do with this?

--relax-gp is a linker relaxation that is only valid in some cases. -Zrelax-elf-relocations enables all linker relaxations that are always valid (for example on riscv a R_RISCV_RELAX reloc allows the linker to replace auipc+jalr with a single jal when the callee is within +/- 1MiB)

It seems based on my quick investigation that the flag winds up here:

Options.MCOptions.X86RelaxRelocations = RelaxELFRelocations;

and doesn't do anything beyond toggle that X86 option corresponding to -mrelax-relocations={yes, no} in Clang. The -mrelax-relocations=no help text reads "Disable x86 relax relocations".

@RalfJung

RalfJung commented Oct 7, 2026

Copy link
Copy Markdown
Member

Oh wow -Zrelax-elf-relocations only does something on x86? Wow, we should do something about that on the docs side at the very least. But we can then ignore it in this discussion, thanks for figuring that out.

@RalfJung

RalfJung commented Oct 7, 2026

Copy link
Copy Markdown
Member

Currently no Rust targets enable relax by default. There was a suggestion years ago to do so (#t-compiler/risc-v > Should -C target-feature=+relax become the default?), but it didn't go anywhere until I recently started taking a look at this. It should be OK to do so, and in fact clang and gcc both default to -mrelax. Although we're all working on Rust in order to not follow the decisions made in the C/C++ ecosystem, it is helpful to see that they haven't run into problems doing so.

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

@rustbot

rustbot commented Oct 7, 2026

Copy link
Copy Markdown
Collaborator

Hey RISC-V Group! This issue has been identified as a good "RISC-V candidate".
In case it's useful, here are some instructions for tackling these sorts of
issues. Maybe take a look?
Thanks! <3

cc @denisvasilik @kraj @sweiglbosker @TechnoPorg

@RalfJung

RalfJung commented Oct 7, 2026

Copy link
Copy Markdown
Member

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
@dkhayes117
@romancardenas
@MabezDev
@jessebraham
@rmsyn

@maya-qorsa

maya-qorsa commented Oct 7, 2026 •

Copy link
Copy Markdown

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?

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).

@romancardenas

Copy link
Copy Markdown
Contributor

Hey! Not an expert on linker relaxation etc., but from my side I can say that the riscv-rt runtime maintained by the Rust Embedded's RISC-V team properly initializes x3 and defines __global_pointer. Thus, providing relaxation support for baremetal targets would allow users of riscv-rt to benefit from these optimizations. Nonetheless, for people that would like to opt this out, proper documentation would be needed.

@TechnoPorg

Copy link
Copy Markdown
Contributor Author

I've been thinking about this more, and I while I think in any case it's good to enable +relax by default in RISC-V targets, I don't see a reason not to do so for all targets (in which case the feature could just as well be stable?).

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 __global_pointer$. The known problematic targets I mentioned above (Android and Fuchsia) don't define __global_pointer$ in startup code either, so in that way are no different from such embedded targets, and don't introduce UB unless the user independently defines __global_pointer$ and sets --relax-gp on LLD. Also as far as I can tell, LLVM/Clang support SCS on Linux (https://clang.llvm.org/docs/ShadowCallStack.html), and so not enabling +relax for anything with SCS would rule out the most downloaded RISC-V targets (download stats).

Another thing I should have noted earlier is that Rust inline assembly already forbids using x3 as an operand, but unfortunately doesn't complain if it shows up on its own in the asm body.

@RalfJung

Copy link
Copy Markdown
Member

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?

@RalfJung

Copy link
Copy Markdown
Member

That seems like a good reason to also enable it by default on our side then.
Then we'd only have to stabilize this if there is a need for -Ctarget-feature=-relax, which I guess is a flag that clang and GCC have but so far there hasn't been a good motivation for having it in Rust I think?

@TechnoPorg

Copy link
Copy Markdown
Contributor Author

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?

@RalfJung

RalfJung commented Oct 15, 2026 •

Copy link
Copy Markdown
Member

I think a new PR is better. The discussion here will seem entirely unrelated to the PR otherwise.^^

JonathanBrouwer added a commit to JonathanBrouwer/rust that referenced this pull request Nov 20, 2026
…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
JonathanBrouwer added a commit to JonathanBrouwer/rust that referenced this pull request Nov 20, 2026
…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
rust-bors Bot pushed a commit that referenced this pull request Nov 20, 2026
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
@TechnoPorg

Copy link
Copy Markdown
Contributor Author

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!

@TechnoPorg TechnoPorg closed this Nov 20, 2026
@rustbot rustbot removed the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Nov 20, 2026
@TechnoPorg
TechnoPorg deleted the push-pvolwsksspmk branch November 20, 2026 16:59
github-actions Bot pushed a commit to rust-lang/rustc-dev-guide that referenced this pull request Nov 23, 2026
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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

O-riscv Target: RISC-V architecture T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

7 participants