Skip to content

attr: Allow #[used] attribute and its argument variant. - #4837

Open
CohenArthur wants to merge 2 commits into
masterfrom
recognize-used-attr
Open

CohenArthur wants to merge 2 commits into
masterfrom
recognize-used-attr

Conversation

@CohenArthur

@CohenArthur CohenArthur commented Sep 3, 2026

Copy link
Copy Markdown
Member

Closes #4836, but I think it's missing some stuff in the backend to properly handle the attribute. We always mark StaticItems with TREE_USED, not sure if that is enough?

This commit allows recognizing both forms of the #[used] attribute: The basic one, which forbids any argument, and the one gated behind feature(used_with_arg), which allows specifying whether the compiler or linker should be responsible for marking the static item as used.

This does not do anything in the backend for actually marking the item as used even if the compiler decides it should not be used. This is a first step towards handling it as the kernel requires it.

gcc/rust/ChangeLog:

* checks/errors/feature/rust-feature-defs.h (FEATURE_ACTIVE): Add `used_with_arg` feature.
* checks/errors/feature/rust-feature-gate.cc (FeatureGate::check_used_attribute): New function. (FeatureGate::visit): Implement feature gating visits for static items.
* checks/errors/feature/rust-feature-gate.h: Declare them.
* checks/errors/rust-builtin-attribute-checker.cc (used): New handler. (check_valid_attribute_for_item): Handle `used` attribute for static items.
* util/rust-attribute-values.h: Add new attribute value for `used`.
* util/rust-attributes.cc: Likewise.

gcc/testsuite/ChangeLog:

* rust/compile/used-attr1.rs: New test.
* rust/compile/used-attr2.rs: New test.
* rust/compile/used-attr3.rs: New test.

backend: Add proper handling for #[used] attribute.

We now support the full implementation of the #[used] attribute, which
can be used to conserve a static variable even if the compiler deems it
unused. This commit also implements #[used(linker)], which instructs the
linker to not garbage collect the static even if it seems unused.

This behavior is equivalent to the used and retain attributes in C.

gcc/rust/ChangeLog:

* backend/rust-compile-item.cc (handle_used_attr): New function.
(CompileItem::visit): Call it.

gcc/testsuite/ChangeLog:

* rust/compile/used-attr4.rs: New test.

@CohenArthur
CohenArthur requested review from P-E-P and powerboat9 and removed request for powerboat9 September 3, 2026 03:52
@CohenArthur CohenArthur added the rust-for-linux Issue related to the compilation of the Linux kernel and its crates label Sep 3, 2026
Comment on lines +524 to +526
// FIXME: We need to enable this when the compatibility mode is >= 1.60 I guess?
FEATURE_ACTIVE ("used_with_arg", USED_WITH_ARG, "1.60.0", ISSUE_SOME (93798),
EDITION_NONE)

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

This should go in rust-feature-defs-rfl.h for now

@powerboat9

Copy link
Copy Markdown
Collaborator

We always mark StaticItems with TREE_USED, not sure if that is enough?

It might be, could you add a comment around where that's happening which mentions the #[used] attribute, so that doesn't get removed by someone who isn't considering #[used]?

@CohenArthur
CohenArthur force-pushed the recognize-used-attr branch 2 times, most recently from c985dc1 to 36c5afe Compare September 16, 2026 02:58
@CohenArthur

Copy link
Copy Markdown
Member Author

I ended up implementing all three forms of the attribute including the linker one, so we don't need to do anything particular. this last commit may break the CI on older platforms though

@CohenArthur
CohenArthur force-pushed the recognize-used-attr branch 2 times, most recently from 31b188a to 01c3963 Compare September 16, 2026 15:29
This commit allows recognizing both forms of the `#[used]` attribute: The
basic one, which forbids any argument, and the one gated behind
`feature(used_with_arg)`, which allows specifying whether the compiler or
linker should be responsible for marking the static item as used.

This does not do anything in the backend for actually marking the item as
used even if the compiler decides it should not be used. This is a first
step towards handling it as the kernel requires it.

gcc/rust/ChangeLog:

	* checks/errors/feature/rust-feature-defs-rfl.h (FEATURE_ACTIVE): Add `used_with_arg`
	feature.
	* checks/errors/feature/rust-feature-gate.cc (FeatureGate::check_used_attribute):
	New function.
	(FeatureGate::visit): Implement feature gating visits for static items.
	* checks/errors/feature/rust-feature-gate.h: Declare them.
	* checks/errors/rust-builtin-attribute-checker.cc (used): New handler.
	(check_valid_attribute_for_item): Handle `used` attribute for static items.
	* util/rust-attribute-values.h: Add new attribute value for `used`.
	* util/rust-attributes.cc: Likewise.
	* backend/rust-compile-item.cc (CompileItem::visit): Add note about implementing the
	`#[used]` attribute properly in our backend.

gcc/testsuite/ChangeLog:

	* rust/compile/used-attr1.rs: New test.
	* rust/compile/used-attr2.rs: New test.
	* rust/compile/used-attr3.rs: New test.
We now support the full implementation of the #[used] attribute, which
can be used to conserve a static variable even if the compiler deems it
unused. This commit also implements #[used(linker)], which instructs the
linker to not garbage collect the static even if it seems unused.

This behavior is equivalent to the `used` and `retain` attributes in C.

gcc/rust/ChangeLog:

	* backend/rust-compile-item.cc (handle_used_attr): New function.
	(CompileItem::visit): Call it.

gcc/testsuite/ChangeLog:

	* rust/compile/used-attr4.rs: New test.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

rust-for-linux Issue related to the compilation of the Linux kernel and its crates

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Handle used built-in attribute

2 participants