Skip to content

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

Merged
rust-bors[bot] merged 1 commit into
rust-lang:mainfrom
TechnoPorg:riscv-default-relax
Nov 20, 2026
Merged

rust-bors[bot] merged 1 commit into
rust-lang:mainfrom
TechnoPorg:riscv-default-relax

Conversation

@TechnoPorg

@TechnoPorg TechnoPorg commented Oct 15, 2026 •

Copy link
Copy Markdown
Contributor

View all comments

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

This comment has been minimized.

@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
@TechnoPorg

Copy link
Copy Markdown
Contributor Author

@rustbot reroll

@rustbot rustbot assigned chenyukang and unassigned tiif Nov 16, 2026
self.features.contains("+relax"),
"The `+relax` feature should be enabled for RISC-V targets"
);
}

@bjorn3 bjorn3 Nov 19, 2026 •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

There are a bunch of C projects that use -mno-relax for riscv, so I'm a bit hesitant to actually refuse disabling linker relaxation. And besides the consistency check is used for actually nonsensical or invalid combinations of flags, not for something that is just an optimization.

View changes since the review

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Do you think there are any built-in targets where we would want to have it disabled? I went with this PR instead of #163890 as that one was more contentious, but if yes, I would eventually want to return to that PR. If not, as @RalfJung suggested above, anyone needing relaxation off could make a custom target spec where the feature is disabled.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Do these consistency checks no apply to custom targets?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Not all of them, but this one in particular checks that the target kind is TargetKind::Builtin rather than TargetKind::Json.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Yeah the point of this particular check is just to govern our built-in targets. We do hold those to a higher standard than custom targets in some cases, basically as an easy way to consistently enforce a policy for all our own targets.

@TechnoPorg

Copy link
Copy Markdown
Contributor Author

@rustbot r? bjorn3

@rustbot rustbot assigned bjorn3 and unassigned chenyukang Nov 19, 2026
@bjorn3

bjorn3 commented Nov 19, 2026

Copy link
Copy Markdown
Member

(r? is standalone, it doesn't need to mention rustbot)

@rustbot

rustbot commented Nov 19, 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.

@bjorn3

bjorn3 commented Nov 20, 2026

Copy link
Copy Markdown
Member

Unlike --gp-relax this should not be able to cause any issues other than causing linker failures on older linkers. Given that riscv is still relatively new, I think it is fair to assume that people will have a somewhat up to date linker with support for the relax relocs.

@bors r+

@rust-bors

rust-bors Bot commented Nov 20, 2026

Copy link
Copy Markdown
Contributor

📌 Commit f99532a has been approved by bjorn3

It is now in the queue for this repository.

@rust-bors rust-bors Bot added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Nov 20, 2026
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
…uwer

Rollup of 5 pull requests

Successful merges:

 - #157721 (compiletest: Add testing for lint machinery at runtime)
 - #153746 (rustdoc: add emoji icons for unstable and deprecated to search results)
 - #164378 (RISC-V: enable relax target feature by default)
 - #166241 (view-types: better documentation for View variant)
 - #166246 (Add rustdoc-ui tests for (some) malformed doc attributes)
@rust-bors
rust-bors Bot merged commit 95b9b6a into rust-lang:main Nov 20, 2026
14 checks passed
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
@rustbot rustbot added this to the 1.102.0 milestone Nov 20, 2026
github-actions Bot pushed a commit to rust-lang/rustc-dev-guide that referenced this pull request Nov 23, 2026
…uwer

Rollup of 5 pull requests

Successful merges:

 - rust-lang/rust#157721 (compiletest: Add testing for lint machinery at runtime)
 - rust-lang/rust#153746 (rustdoc: add emoji icons for unstable and deprecated to search results)
 - rust-lang/rust#164378 (RISC-V: enable relax target feature by default)
 - rust-lang/rust#166241 (view-types: better documentation for View variant)
 - rust-lang/rust#166246 (Add rustdoc-ui tests for (some) malformed doc attributes)
@TechnoPorg
TechnoPorg deleted the riscv-default-relax branch November 27, 2026 21:35
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. 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.

10 participants