Skip to content

intrinsic: Add black_box intrinsic#4456

Draft
Pasta-coder wants to merge 1 commit intoRust-GCC:masterfrom
Pasta-coder:black_box
Draft

intrinsic: Add black_box intrinsic#4456
Pasta-coder wants to merge 1 commit intoRust-GCC:masterfrom
Pasta-coder:black_box

Conversation

@Pasta-coder
Copy link
Contributor

This patch implements the black_box intrinsic, which is used to prevent the compiler from optimizing away a piece of code. It maps to an empty volatile inline assembly block with a memory clobber and a general input constraint, matching the behavior expected by the standard library.

Addresses #3372

gcc/rust/ChangeLog:

* backend/rust-compile-intrinsic.cc (black_box_handler): New handler.

gcc/testsuite/ChangeLog:

* rust/compile/intrinsic-black-box.rs: New test.
* rust/execute/intrinsic-black-box.rs: New test.

This patch implements the `black_box` intrinsic, which is used to prevent
the compiler from optimizing away a piece of code. It maps to an empty
volatile inline assembly block with a memory clobber and a general input
constraint, matching the behavior expected by the standard library.

Addresses Rust-GCC#3372

gcc/rust/ChangeLog:

	* backend/rust-compile-intrinsic.cc (black_box_handler): New handler.

gcc/testsuite/ChangeLog:

	* rust/compile/intrinsic-black-box.rs: New test.
	* rust/execute/intrinsic-black-box.rs: New test.

Signed-off-by: Jayant Chauhan <0001jayant@gmail.com>
@P-E-P
Copy link
Member

P-E-P commented Mar 3, 2026

I don't think this was already available with rust 1.49 ?

@Pasta-coder Pasta-coder marked this pull request as draft March 3, 2026 15:01
@Pasta-coder
Copy link
Contributor Author

While black_box was stabilized in std::hint in 1.66, it has existed as a core compiler-internal intrinsic since 2019 (see RFC #2360 and tracking issue #64102).

This implementation follows the same "infrastructure parity" logic as the existing assume and likely handlers in rust-compile-intrinsic.cc. Although their stable library wrappers (like assert_unchecked in 1.81) appeared in much later Rust versions, the underlying intrinsics were 1.49-era requirements for the test crate and core library optimization barriers.

@P-E-P
Copy link
Member

P-E-P commented Mar 4, 2026

I'm not sure I'm following you here, black box was indeed available in 1.49 but what I meant really was "did it exist as an intrinsic yet ?".
As you can see here black box was then implemented using inline assembly https://github.com/rust-lang/rust/blob/1.49.0/library/core/src/hint.rs#L159

@Pasta-coder
Copy link
Contributor Author

yeah , you are right . i just gazed over that file . disregard my comment .

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants