Skip to content

Add custom target specs for riscv boards to silence the relax warning on all riscv board builds - #5084

Merged
bradjc merged 2 commits into
masterfrom
dev/riscv/custom-target-relax
Aug 21, 2026
Merged

bradjc merged 2 commits into
masterfrom
dev/riscv/custom-target-relax

Conversation

@ppannuto

Copy link
Copy Markdown
Member

Pull Request Overview

All of our riscv board builds print a multi-line warning right now because we enable +relax (for good reason, as it leads to real code size improvements). The warning comes because relax isn't stabilized and the path to general stabilization is a bit fuzzy as far as I can tell—but I don't think it's going anywhere, it's just passing through an llvm intrinsic. The stabilization concerns are more around how to handle 'default behavior' when relax working requires some platform support (setting gp correctly, which Tock does do).

The approach this takes, then, is to create custom targets for riscv boards, because if relax is in the target specs definition, then no warning is emitted; the warning only comes when you use Cargo to turn on relax. In practice, upstream default target specs rarely change, and I had Claude update the toolchain update script to check / update these as-needed.

A better solution would be to pick back up the thread on #4464, since then we could include relax in the upstream specs for the tock target — but that's a much bigger project. This is an immediate cleanup of some real noise in our builds.

Testing Strategy

This pull request was tested by compiling.

TODO or Help Wanted

I'm unsure whether or not we want this, as it's a bit more 'custom' board machinery. I don't think it's too bad, and it would be nice to eliminate the multi-line warning that prints with every riscv board build.

Checklist

  • Ran make prepush.

PR Contents

Documentation

  • Updated the relevant files in /docs and the Book.
  • No documentation updates are required.

AI Use

  • No AI was used in this PR.
  • AI was used in this PR. I have read Tock's AI policy and have properly disclosed my AI use below.

I asked Claude where the warning was coming from and whether we could silence it. We iterated for a while, and this was the best approach I came to.

Here's a summary of some of the other things I forced Claude to try that didn't work. Empirical results, each tested for real:
  • (a) RUSTFLAGS env var: breaks the build — replaces (not merges) the config-file rustflags, dropping Tock's sentinel flag (cfg_tock_buildflagssentinel) that boards/build_scripts checks for; build panics before the warning question is even reachable.
  • (b) .cargo/config.toml via [build.rustflags]: this is master's current, real setup (boards/cargo/riscv_flags.toml) — confirmed the warning still fires on every crate compiled.
  • (b-variant) [target..rustflags]: also breaks — Cargo's rustflags source selection is winner-take-all across config fragments, not merged, so it silently drops tock_flags.toml's sentinel flag too (same panic as (a)). Not viable regardless of the warning.
  • (d) -A unstable-features: does not suppress it. --cap-lints allow does suppress it at the raw rustc level — but Cargo only auto-applies --cap-lints to external/registry dependencies, never to local workspace/path crates (our own kernel/board/arch crates), so this can't be leveraged through normal builds.

Bottom line: No config-file-based mechanism — including PR #5076's --config merge trick — can suppress this warning for Tock's own crates; it's a hard-coded rustc diagnostic with no lint name, confirming the branch's existing commit message claim. The custom JSON target-spec approach (embedding +relax in the target's features field rather than passing it as a -C target-feature flag) remains the only working path, since it doesn't go through the "unstable -C flag" warning at all. Recommend sticking with the current dev/riscv/custom-target-relax design.

@ppannuto
ppannuto force-pushed the dev/riscv/custom-target-relax branch from 872fb05 to 06b9e9a Compare August 17, 2026 18:23
@github-actions github-actions Bot added the WG-OpenTitan In the purview of the OpenTitan working group. label Aug 17, 2026

@bradjc bradjc 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.

This is the absolute best and stupidest PR.

I ran the rust update script and got:

Checking RISC-V custom target specs...
Traceback (most recent call last):
  File "<stdin>", line 4, in <module>
  File "/opt/homebrew/Cellar/[email protected]/3.14.3_1/Frameworks/Python.framework/Versions/3.14/lib/python3.14/json/__init__.py", line 298, in load
    return loads(fp.read(),
        cls=cls, object_hook=object_hook,
        parse_float=parse_float, parse_int=parse_int,
        parse_constant=parse_constant, object_pairs_hook=object_pairs_hook, **kw)
  File "/opt/homebrew/Cellar/[email protected]/3.14.3_1/Frameworks/Python.framework/Versions/3.14/lib/python3.14/json/__init__.py", line 352, in loads
    return _default_decoder.decode(s)
           ~~~~~~~~~~~~~~~~~~~~~~~^^^
  File "/opt/homebrew/Cellar/[email protected]/3.14.3_1/Frameworks/Python.framework/Versions/3.14/lib/python3.14/json/decoder.py", line 345, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
               ~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^
  File "/opt/homebrew/Cellar/[email protected]/3.14.3_1/Frameworks/Python.framework/Versions/3.14/lib/python3.14/json/decoder.py", line 363, in raw_decode
    raise JSONDecodeError("Expecting value", s, err.value) from None
json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)
Traceback (most recent call last):
  File "<stdin>", line 4, in <module>
  File "/opt/homebrew/Cellar/[email protected]/3.14.3_1/Frameworks/Python.framework/Versions/3.14/lib/python3.14/json/__init__.py", line 298, in load
    return loads(fp.read(),
        cls=cls, object_hook=object_hook,
        parse_float=parse_float, parse_int=parse_int,
        parse_constant=parse_constant, object_pairs_hook=object_pairs_hook, **kw)
  File "/opt/homebrew/Cellar/[email protected]/3.14.3_1/Frameworks/Python.framework/Versions/3.14/lib/python3.14/json/__init__.py", line 352, in loads
    return _default_decoder.decode(s)
           ~~~~~~~~~~~~~~~~~~~~~~~^^^
  File "/opt/homebrew/Cellar/[email protected]/3.14.3_1/Frameworks/Python.framework/Versions/3.14/lib/python3.14/json/decoder.py", line 345, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
               ~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^
  File "/opt/homebrew/Cellar/[email protected]/3.14.3_1/Frameworks/Python.framework/Versions/3.14/lib/python3.14/json/decoder.py", line 363, in raw_decode
    raise JSONDecodeError("Expecting value", s, err.value) from None
json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)
Traceback (most recent call last):
  File "<stdin>", line 4, in <module>
  File "/opt/homebrew/Cellar/[email protected]/3.14.3_1/Frameworks/Python.framework/Versions/3.14/lib/python3.14/json/__init__.py", line 298, in load
    return loads(fp.read(),
        cls=cls, object_hook=object_hook,
        parse_float=parse_float, parse_int=parse_int,
        parse_constant=parse_constant, object_pairs_hook=object_pairs_hook, **kw)
  File "/opt/homebrew/Cellar/[email protected]/3.14.3_1/Frameworks/Python.framework/Versions/3.14/lib/python3.14/json/__init__.py", line 352, in loads
    return _default_decoder.decode(s)
           ~~~~~~~~~~~~~~~~~~~~~~~^^^
  File "/opt/homebrew/Cellar/[email protected]/3.14.3_1/Frameworks/Python.framework/Versions/3.14/lib/python3.14/json/decoder.py", line 345, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
               ~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^
  File "/opt/homebrew/Cellar/[email protected]/3.14.3_1/Frameworks/Python.framework/Versions/3.14/lib/python3.14/json/decoder.py", line 363, in raw_decode
    raise JSONDecodeError("Expecting value", s, err.value) from None
json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)

@ppannuto
ppannuto force-pushed the dev/riscv/custom-target-relax branch from 06b9e9a to 663f7c3 Compare August 18, 2026 17:12
@ppannuto

Copy link
Copy Markdown
Member Author

UGH... today in "Pat learns more about the limitations of AI and how closely you have to police these things..."

I ask it to explain how it claimed it "tested the script worked correctly" (and it had reported that it had tested the script because I told it to do that, but exactly how it tested was admittedly opaque as I didn't prompt that...)

I only tested this locally against my own pinned toolchain, which was already installed — I never tested the actual toolchain-bump scenario the script exists for, so I pushed it untested against its real use case.


Cleaned up and actually tested now, sorry

bradjc
bradjc previously approved these changes Aug 18, 2026

@bradjc bradjc 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.

Updated script worked for me.

Comment thread boards/cargo/README.md Resolved
Comment thread boards/Makefile.common Resolved
ppannuto and others added 2 commits August 18, 2026 15:52
…rning

The +relax RISC-V target feature (which emits relaxable relocations for
the linker to perform size-saving relaxation) has no lint code in rustc,
so it cannot be suppressed via -A. Upstream has no active stabilization
effort.

Instead, add custom target JSON specs for each RISC-V variant Tock uses
(riscv32imc, riscv32imac, riscv64imac) with +relax baked into the base
features field. Features in the target spec do not trigger the unstable
warning. Update all RISC-V board cargo configs to reference these specs
by path, and add json-target-spec to riscv_flags.toml to enable the
required Cargo unstable feature.

When updating rust-toolchain.toml, re-run:
  rustc -Z unstable-options --print target-spec-json --target <triple>
for each of the three targets and diff against the checked-in JSON to
catch any upstream changes.

Also fixes two consumers that were bypassing or breaking under the new
.json TARGET value:
- boards/veer_el2_sim/Makefile hardcoded TARGET=riscv32imc-unknown-none-elf
  directly rather than leaving it unset like every other RISC-V board,
  which bypassed the .cargo/config.toml auto-detection in
  Makefile.common and silently opted veer_el2_sim out of the new target
  spec (and +relax).
- Several RISC-V board Makefiles (hifive1, hifive_inventor, arty_e21,
  redboard_redv, qemu_rv32_virt, qemu_rv64_virt,
  opentitan/earlgrey-cw310, esp32-c3-devkitM-1,
  tutorials/qemu_rv32_virt-tutorial,
  configurations/qemu_rv64_virt-test-ci) built their flash/qemu/run/
  install target paths directly as
  $(TOCK_ROOT_DIRECTORY)target/$(TARGET)/..., which breaks once TARGET
  is a relative path to a custom JSON spec: the literal path segment
  gets glued onto target/ instead of resolving to Cargo's actual output
  directory. Makefile.common already computes TARGET_PATH for exactly
  this (strips the .json suffix and any path prefix down to the
  target's basename); switched these rules to use it, matching the
  pattern veer_el2_sim and opentitan's own .PHONY build rules already
  used.

Co-Authored-By: Claude Sonnet 4.6 <[email protected]>
Co-Authored-By: Claude Sonnet 5 <[email protected]>
Tock uses custom target JSON specs (boards/cargo/riscv*.json) rather than
the built-in RISC-V targets so that the +relax feature (which enables LLVM
to emit relaxable relocations for linker relaxation, providing significant
code size savings) can be included in the base target definition. Features
specified via -Ctarget-feature trigger an "unstable feature" warning for
+relax because rustc has not yet stabilized it; features in the target spec
itself do not. There is no active upstream stabilization effort as of 2026;
track progress at rust-lang/rust#109426.

When the toolchain version changes, the upstream target spec JSON may
change too. Extend update_rust_version.sh to automatically compare and
update the three custom RISC-V target spec files in boards/cargo/ using
the new toolchain's --print target-spec-json output (re-adding +relax).

Co-Authored-By: Claude Sonnet 5 <[email protected]>
@ppannuto
ppannuto force-pushed the dev/riscv/custom-target-relax branch from 624b1ac to 277c318 Compare August 18, 2026 22:53

@brghena brghena 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.

The changes themselves are fine. The last question I've got is whether this is too much churn and custom stuff to be worth removing a warning. It's reasonably documented at least.

@ppannuto

Copy link
Copy Markdown
Member Author

I think there is real value in removing this warning — it's a bit of an opaque thing that it prints, and I don't think it's a great first user experience to build a board and have a large warning you have to puzzle through.

There's a lot of warning about potential churn in here, because it does technically exist, but I don't think there will be much churn in practice — targets rarely change, and we won't need to change our infra here until +relax actually stabilizes.

@ppannuto ppannuto added the last-call Final review period for a pull request. label Aug 20, 2026
@bradjc
bradjc added this pull request to the merge queue Aug 21, 2026
Merged via the queue into master with commit 0df7703 Aug 21, 2026
19 of 23 checks passed
@bradjc
bradjc deleted the dev/riscv/custom-target-relax branch August 21, 2026 16:22
@lschuermann lschuermann mentioned this pull request Oct 7, 2026
2 of 5 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

last-call Final review period for a pull request. WG-OpenTitan In the purview of the OpenTitan working group.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants