Skip to content

add extract_bits and deposit_bits intrinsics - #164876

Open
malezjaa wants to merge 1 commit into
rust-lang:mainfrom
malezjaa:add-pext-and-pdep-intrinsics
Open

malezjaa wants to merge 1 commit into
rust-lang:mainfrom
malezjaa:add-pext-and-pdep-intrinsics

Conversation

@malezjaa

@malezjaa malezjaa commented Oct 24, 2026 •

Copy link
Copy Markdown
Contributor

View all comments

Tracking issue: #149069

LLVM 23 introduced generic intrinsics for pext (extract_bits) and pdep (deposit_bits)

@rustbot

rustbot commented Oct 24, 2026

Copy link
Copy Markdown
Collaborator

Some changes occurred to the intrinsics. Make sure the CTFE / Miri interpreter
gets adapted for the changes, if necessary.

cc @rust-lang/miri, @RalfJung, @oli-obk, @lcnr

Any special-casing of Miri in the standard library requires review.

cc @rust-lang/miri

⚠️ #[miri::intrinsic_fallback_is_spec] must only be used if the function actively checks for all UB cases,
and explores the possible non-determinism of the intrinsic.

cc @rust-lang/miri

@rustbot rustbot added A-LLVM Area: Code generation parts specific to LLVM. Both correctness bugs and optimization-related issues. 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. T-libs Relevant to the library team, which will review and decide on the PR/issue. labels Oct 24, 2026
@rustbot

rustbot commented Oct 24, 2026

Copy link
Copy Markdown
Collaborator

r? @JohnTitor

rustbot has assigned @JohnTitor.
They will have a look at your PR within the next two weeks and either review your PR or reassign to another reviewer.

Use r? to explicitly pick a reviewer

Why was this reviewer chosen?

The reviewer was selected based on:

  • Owners of files modified in this PR: libs
  • libs expanded to 37 candidates
  • Random selection from ChrisDenton, JohnTitor, Mark-Simulacrum, thomcc

@rust-log-analyzer

This comment has been minimized.

@RalfJung RalfJung added the llm-assisted An LLM-assisted PR as defined by the LLM policy. Requires ahead-of-time consent by assignee. label Oct 25, 2026
@RalfJung

Copy link
Copy Markdown
Member

Tests were made by AI, but I understand how they work.

FWIW our policy disallows code generation, and there is no exception for tests. For those you're supposed to have a pre-arranged reviewer who agreed to reviewing LLM code.

@malezjaa

Copy link
Copy Markdown
Contributor Author

Oh right, I'll close this and open a new pr with tests made by me If that's okay

@malezjaa malezjaa closed this Oct 25, 2026
@rustbot rustbot removed the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Oct 25, 2026
@RalfJung

Copy link
Copy Markdown
Member

You can also update this PR. Or you can make it a draft and ask for a reviewer on Zulip. Whatever you prefer.

@malezjaa malezjaa reopened this Oct 25, 2026
@rustbot rustbot added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Oct 25, 2026
Comment thread library/core/src/intrinsics/mod.rs Outdated
#[unstable(feature = "uint_gather_scatter_bits", issue = "149069")]
#[rustc_const_unstable(feature = "uint_gather_scatter_bits", issue = "149069")]
#[miri::intrinsic_fallback_is_spec]
pub const fn pext<T: [const] fallback::Pext>(x: T, mask: T) -> T {

@RalfJung RalfJung Oct 25, 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.

These names are extremely opaque and make little sense to me.

Please name intrinsics more like the Rust operation than their often obscure LLVM name. You can mention the LLVM name in the docs.

View changes since the review

@malezjaa
malezjaa force-pushed the add-pext-and-pdep-intrinsics branch from b457daf to 50266b1 Compare October 25, 2026 12:28
@malezjaa malezjaa changed the title add pext and pdep intrinsics add extract_bits and deposit_bits intrinsics Oct 25, 2026
@rust-log-analyzer

This comment has been minimized.

@malezjaa
malezjaa force-pushed the add-pext-and-pdep-intrinsics branch from 50266b1 to 3e22e48 Compare October 25, 2026 13:16
@JohnTitor

Copy link
Copy Markdown
Member

r? RalfJung

@rustbot rustbot assigned RalfJung and unassigned JohnTitor Nov 3, 2026
@rustbot

rustbot commented Nov 3, 2026

Copy link
Copy Markdown
Collaborator

RalfJung is not on the review rotation at the moment.
They may take a while to respond.

@RalfJung RalfJung left a comment •

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.

This seems reasonable, modulo some nits.
@rust-lang/wg-llvm any concerns about using these new LLVM intrinsics?

@rustbot author

View changes since this review

let pair = self.insert_value(pair, high, 1);
pair
}
sym::deposit_bits | sym::extract_bits if llvm_version >= (23, 0, 0) => {

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.

Why are we only using this for LLVM >= 23? Please add a comment.

Comment thread library/core/src/num/imp/int_bits.rs Outdated

#[inline(always)]
pub(in crate::num) const fn extract_impl(mut x: $U, sparse: $U) -> $U {
pub(crate) const fn extract_impl(mut x: $U, sparse: $U) -> $U {

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.

If these are now only used for the fallback impl, they should be moved to intrinsics/fallback.

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.

Is this test doing anything useful? We already have the other test ensuring that the LLVM intrinsic gets generated.

Comment thread library/core/src/intrinsics/mod.rs Outdated
/// This corresponds to `@llvm.pext.*`.
#[rustc_intrinsic]
#[rustc_nounwind]
#[unstable(feature = "uint_gather_scatter_bits", issue = "149069")]

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.

Intrinsic will not be stabilized so they should not have a separate feature gate.

@rustbot rustbot added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Nov 4, 2026
@rustbot

rustbot commented Nov 4, 2026

Copy link
Copy Markdown
Collaborator

Reminder, once the PR becomes ready for a review, use @rustbot ready.

@nikic

nikic commented Nov 4, 2026

Copy link
Copy Markdown
Contributor

@rust-lang/wg-llvm any concerns about using these new LLVM intrinsics?

Did you confirm that they produce good codegen, esp. on LLVM 23? For the case where they get expanded, that is.

@RalfJung

RalfJung commented Nov 4, 2026

Copy link
Copy Markdown
Member

For the case where they get expanded, that is.

I don't know what you mean by that.

@nikic

nikic commented Nov 4, 2026

Copy link
Copy Markdown
Contributor

When there is no native instruction

@RalfJung

RalfJung commented Nov 4, 2026

Copy link
Copy Markdown
Member

Which architectures would that affect?
There is an asm test but it runs on x86 and doesn't seem to do much.

@nikic

nikic commented Nov 4, 2026

Copy link
Copy Markdown
Contributor

You'd want to look at something like x86 without bmi2.

Just trying the 32-bit case, we have current Rust codegen: https://rust.godbolt.org/z/4snnWPhKP

And the LLVM intrinsic expansion: https://llvm.godbolt.org/z/bx6TKW6hf

It looks like Rust's current fallback produces less instructions, it's ~180 total vs ~250 total.

Doesn't look like this changes substantially in LLVM 24 either.

@RalfJung

RalfJung commented Nov 4, 2026

Copy link
Copy Markdown
Member

So... this PR is a regression for such targets?

Any suggestions for what to do? We could in principle gate the use of the intrinsic on target & target features, and use the fallback body otherwise, just like it is already used on older LLVM.

@nikic

nikic commented Nov 4, 2026

Copy link
Copy Markdown
Contributor

It looks like there is an LLVM PR to improve the expansion: llvm/llvm-project#227292

I'd probably just wait until that gets in.

@malezjaa
malezjaa force-pushed the add-pext-and-pdep-intrinsics branch 2 times, most recently from 8d64e63 to 935f428 Compare November 5, 2026 16:13
@rustbot

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

@malezjaa

malezjaa commented Nov 5, 2026

Copy link
Copy Markdown
Contributor Author

@rustbot ready

@rustbot rustbot removed the S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. label Nov 5, 2026
@rustbot

rustbot commented Nov 5, 2026

Copy link
Copy Markdown
Collaborator

Error: shortcut handler unexpectedly failed in this comment: failed to remove Label { name: "S-waiting-on-author" }

Please file an issue on GitHub at triagebot if there's a problem with this bot, or reach out on #triagebot on Zulip.

@rust-log-analyzer

This comment has been minimized.

@malezjaa
malezjaa force-pushed the add-pext-and-pdep-intrinsics branch from 935f428 to 3ead23f Compare November 5, 2026 16:23

@RalfJung RalfJung left a comment •

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.

This LGTM from the compiler implementation side, but I cannot comment on when we should actually start using the attribute wrt LLVM support.

r? @nikic

View changes since this review

let pair = self.insert_value(pair, high, 1);
pair
}
// FIXME(llvm): These intrinsics were introduced in LLVM 23.

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.

Suggested change
// FIXME(llvm): These intrinsics were introduced in LLVM 23.
// Checking LLVM version as these intrinsics were introduced in LLVM 23.

Comment thread library/core/src/intrinsics/mod.rs Outdated
Comment on lines +3728 to +3729
#[unstable(feature = "core_intrinsics", issue = "none")]
#[rustc_const_unstable(feature = "core_intrinsics", issue = "none")]

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.

You don't need #[unstable] here, the const attribute is enough.

@rustbot rustbot assigned nikic and unassigned RalfJung Nov 5, 2026
@RalfJung

RalfJung commented Nov 5, 2026

Copy link
Copy Markdown
Member

r? @nikic

@rustbot

rustbot commented Nov 5, 2026

Copy link
Copy Markdown
Collaborator

Requested reviewer is already assigned to this pull request.

Please choose another assignee.

@malezjaa
malezjaa force-pushed the add-pext-and-pdep-intrinsics branch from 3ead23f to 375326e Compare November 5, 2026 20:12
@malezjaa

malezjaa commented Nov 5, 2026

Copy link
Copy Markdown
Contributor Author

Updated. Thank you for you review :D

@rust-bors

rust-bors Bot commented Nov 10, 2026

Copy link
Copy Markdown
Contributor

☔ The latest upstream changes (presumably #165760) made this pull request unmergeable. Please resolve the merge conflicts by rebasing.

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

A-LLVM Area: Code generation parts specific to LLVM. Both correctness bugs and optimization-related issues. llm-assisted An LLM-assisted PR as defined by the LLM policy. Requires ahead-of-time consent by assignee. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. T-libs Relevant to the library team, which will review and decide on the PR/issue.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants