Skip to content

RISC-V: enable relax target feature by default - #164378

Open
TechnoPorg wants to merge 2 commits into
rust-lang:mainfrom
TechnoPorg:riscv-default-relax
Open

TechnoPorg wants to merge 2 commits into
rust-lang:mainfrom
TechnoPorg:riscv-default-relax

Conversation

@TechnoPorg

@TechnoPorg TechnoPorg commented Oct 15, 2026 •

Copy link
Copy Markdown
Contributor

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

@rustbot

rustbot commented Oct 15, 2026

Copy link
Copy Markdown
Collaborator

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

cc @RalfJung

These commits modify compiler targets.
(See the Target Tier Policy.)

@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 15, 2026
@TechnoPorg

Copy link
Copy Markdown
Contributor Author

I'm not sure whether it would make sense to enable this in global_llvm_features instead of touching every individual RISC-V target. The argument I had for doing it this way is that if someone desperately needs to turn off linker relaxation, they can write a custom target spec without it.

@RalfJung

Copy link
Copy Markdown
Member

I would suggest adding a check in compiler/rustc_target/src/spec/consistency.rs that required "relax" to be present in all our riscv targets. (That check can distinguish between JSON targets and built-in targets.)

@RalfJung

Copy link
Copy Markdown
Member

@Amanieu

Amanieu commented Oct 18, 2026

Copy link
Copy Markdown
Member

The relax feature mainly existed to support linkers which at the time didn't support linker relaxation. But these days I believe this is supported by all major linkers, so there's no point in disabling this any more.

@sanchuanhehe

Copy link
Copy Markdown
Contributor

Typo: Missing comma in RISC-V target features

File: compiler/rustc_target/src/spec/targets/riscv64gc_unknown_redox.rs⁠

There seems to be a missing comma in the feature string:

base.features = "+m,+a,+f,+d,+c+relax".into();

It should be:

base.features = "+m,+a,+f,+d,+c,+relax".into();

Otherwise, +c+relax will be parsed as a single invalid feature instead of two separate features.

@rustbot

rustbot commented Oct 18, 2026

Copy link
Copy Markdown
Collaborator

Target consistency checks are being changed

cc @RalfJung

@rustbot

rustbot commented Oct 18, 2026

Copy link
Copy Markdown
Collaborator

This PR was rebased onto a different main commit. Here's a range-diff highlighting what actually changed.

Rebasing is a normal part of keeping PRs up to date, so no action is needed—this note is just to help reviewers.

@romancardenas romancardenas left a comment •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@TechnoPorg

Copy link
Copy Markdown
Contributor Author

@rustbot reroll

@rustbot rustbot assigned tiif and unassigned dingxiangfei2009 Nov 2, 2026

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

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.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

8 participants