Skip to content

Add an optional llvm = argument to rustc_intrinsic. - #164544

Open
veluca93 wants to merge 1 commit into
rust-lang:mainfrom
veluca93:rustc_llvm_intrinsic
Open

veluca93 wants to merge 1 commit into
rust-lang:mainfrom
veluca93:rustc_llvm_intrinsic

Conversation

@veluca93

@veluca93 veluca93 commented Oct 18, 2026 •

Copy link
Copy Markdown
Contributor

View all comments

This is meant for usage in core::arch, to define the platform-specific LLVM intrinsics that correspond to each intrinsic.

LLM usage: I used an LLM to point me at places in the code from which to start the implementation, but I did not generate any code. The main non-trivial input from the LLM was to prevent inlining of callees annotated with rustc_llvm_intrinsic. Code has been since re-designed following @bjorn3's and @RalfJung's proposals so there's no trace of any LLM input left :-)

r? RalfJ

@rustbot

rustbot commented Oct 18, 2026

Copy link
Copy Markdown
Collaborator

Some changes occurred in compiler/rustc_attr_ir

cc @jdonszelmann, @JonathanBrouwer

Some changes occurred in compiler/rustc_attr_parsing

cc @jdonszelmann, @JonathanBrouwer

Some changes occurred to MIR optimizations

cc @rust-lang/wg-mir-opt

Some changes occurred in compiler/rustc_passes/src/check_attr.rs

cc @jdonszelmann, @JonathanBrouwer

@rustbot rustbot added A-attributes Area: Attributes (`#[…]`, `#![…]`) 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. labels Oct 18, 2026
@rustbot

rustbot commented Oct 18, 2026

Copy link
Copy Markdown
Collaborator

Error: Failed to set assignee to RalfJ: invalid assignee

Note: Only org members with at least the repository "read" role, users with write permissions, or people who have commented on the PR may be assigned.

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

@veluca93

Copy link
Copy Markdown
Contributor Author

r? RalfJung

@rustbot

rustbot commented Oct 18, 2026

Copy link
Copy Markdown
Collaborator

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

@tgross35

Copy link
Copy Markdown
Member

Cc @folkertdev

@veluca93
veluca93 force-pushed the rustc_llvm_intrinsic branch from 39047e6 to a0ad71b Compare October 19, 2026 07:09
@bjorn3

bjorn3 commented Oct 19, 2026

Copy link
Copy Markdown
Member

Can you check that all places in the compiler that check for extern "llvm-intrinsic" have been adjusted as appropriate?

@veluca93

Copy link
Copy Markdown
Contributor Author

Can you check that all places in the compiler that check for extern "llvm-intrinsic" have been adjusted as appropriate?

I gave a look, and I think so, but I am definitely not knowledgeable enough of compiler internals to be sure.

@veluca93
veluca93 marked this pull request as draft October 23, 2026 15:39
@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 Oct 23, 2026
@veluca93
veluca93 force-pushed the rustc_llvm_intrinsic branch 2 times, most recently from ebce41f to 90416b5 Compare October 28, 2026 12:15
@veluca93
veluca93 marked this pull request as ready for review October 28, 2026 12:15
@rustbot

rustbot commented Oct 28, 2026

Copy link
Copy Markdown
Collaborator

rustc_codegen_gcc is developed in its own repository. If possible, consider making this change to rust-lang/rustc_codegen_gcc instead.

cc @antoyo, @GuillaumeGomez

Some changes occurred to constck

cc @fee1-dead

Some changes occurred to the CTFE machinery

cc @RalfJung, @oli-obk, @lcnr

rustc_codegen_cranelift is developed in its own repository. If possible, consider making this change to rust-lang/rustc_codegen_cranelift instead.

cc @bjorn3

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

This comment has been minimized.

@veluca93
veluca93 force-pushed the rustc_llvm_intrinsic branch from 90416b5 to c906c98 Compare October 28, 2026 19:45
@rustbot

rustbot commented Oct 28, 2026

Copy link
Copy Markdown
Collaborator

clippy is developed in its own repository. If possible, consider making this change to rust-lang/rust-clippy instead.

cc @rust-lang/clippy

@rustbot rustbot added the T-clippy Relevant to the Clippy team. label Oct 28, 2026
@veluca93

veluca93 commented Nov 6, 2026

Copy link
Copy Markdown
Contributor Author

I haven't looked too much at the implementation, just the surface area. This looks pretty mechanical (why doesn't GH show a version of git diff --color-moved) but I'm a bit behind on reviews, so perhaps

r? folkertdev

FYI after applying Ralf's suggestion git/github is much happier with the diff, and shows it as a change with a size much more representative of the actual size :-)

@rustbot

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

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

I guess you're right, this isn't actually that bad. I'll yoink it back.

r? me

View changes since this review

Comment thread compiler/rustc_middle/src/ty/util.rs Resolved
Comment thread compiler/rustc_middle/src/ty/util.rs Resolved

// Intrinsics are language primitives, not regular calls, so treat them separately.
if let Some(intrinsic) = tcx.intrinsic(callee) {
// Non-backend intrinsics are language primitives, not regular calls, so treat them separately.

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.

I think my idea was that backend intrinsics would always have a fallback body, but that might not necessarily be the case so... what do you think should happen here?

I think that as soon as this lands, we may as well start switching extern "llvm-intrinsic" blocks over - none of those have fallbacks.

Please include a test making sure we enforce constness matching up too.

@rustbot rustbot assigned tgross35 and unassigned folkertdev Nov 6, 2026
@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 6, 2026
@rustbot

rustbot commented Nov 6, 2026

Copy link
Copy Markdown
Collaborator

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

@veluca93
veluca93 force-pushed the rustc_llvm_intrinsic branch from e294fce to e18a91c Compare November 6, 2026 10:17
@veluca93

veluca93 commented Nov 6, 2026

Copy link
Copy Markdown
Contributor Author

@rustbot ready

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

This comment has been minimized.

@veluca93
veluca93 force-pushed the rustc_llvm_intrinsic branch from e18a91c to f28292b Compare November 6, 2026 10:26
@rust-log-analyzer

This comment has been minimized.

@veluca93
veluca93 force-pushed the rustc_llvm_intrinsic branch from f28292b to 40c3e33 Compare November 6, 2026 10:35
Comment on lines +5 to +13
#[rustc_intrinsic(llvm = "llvm.fshl.i64")]
fn fshl(a: i64, b: i64, sh: i64) -> i64 {
(((a as i128) << 64) | (b as i128) >> sh) as i64
}

fn main() {
const { fshl(1, 2, 3) };
//~^ ERROR: cannot call non-const function `fshl` in constants [E0015]
}

@RalfJung RalfJung Nov 6, 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.

Not sure if this is worth a test, it's just standard const checking. Would be very surprising if we allowed calling a function that's not marked as const.

More interesting is ensuring that const stability checks work properly. You could extend tests/ui/consts/const-unstable-intrinsic.rs to also check backend intrinsics.

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.

Removed, and added more tests to the other file.

@RalfJung

RalfJung commented Nov 6, 2026

Copy link
Copy Markdown
Member

Btw the PR title and description seem to still refer to an earlier version of this PR.

@veluca93
veluca93 force-pushed the rustc_llvm_intrinsic branch from 40c3e33 to 62cfa98 Compare November 6, 2026 14:34
@rustbot

rustbot commented Nov 6, 2026

Copy link
Copy Markdown
Collaborator

miri is developed in its own repository. If the Miri part of this change can be broken out, consider making this change to rust-lang/miri instead. However, if Miri needs adjusting for rustc changes, just ignore this message.

cc @rust-lang/miri

@veluca93 veluca93 changed the title Add a rustc_llvm_intrinsic attribute. Add an optional llvm = argument to rustc_intrinsic. Nov 6, 2026
@veluca93
veluca93 force-pushed the rustc_llvm_intrinsic branch from 62cfa98 to e6d0471 Compare November 6, 2026 15:06

impl ValidityRequirement {
pub fn from_intrinsic(intrinsic: Symbol) -> Option<Self> {
pub fn from_intrinsic(intrinsic: Intrinsic) -> Option<Self> {

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

This is strictly about Rust intrinsics so I don't think it makes sense to change the type here.

View changes since the review

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

I was surprised that the interpreter didn't need any patch here. It turns out that the interpreter has relied on the item_name rather than the name recorded in the intrinsic data, which is fine today as the two are equal, but is wrong after this PR.

So this code

let intrinsic_name = this.tcx.item_name(instance.def_id());

and this
let intrinsic_name = ecx.tcx.item_name(instance.def_id());

will need adjustment.

View changes since the review

@RalfJung RalfJung Nov 7, 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's some logic in this file that seems wrong now

let intrinsic = fx.tcx.item_name(instance.def_id());

View changes since the review

@RalfJung RalfJung Nov 7, 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's logic in this crate that seems wrong now

let name = tcx.item_name(instance.def_id());

View changes since the review

Comment on lines +8 to +12
#[unstable(feature = "unstable", issue = "42")]
#[rustc_intrinsic(llvm = "llvm.fshl.i64")]
pub const unsafe fn fake_size_of_val_llvm<T>(x: *const T) -> usize {
0
}

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

Please keep the test consistent, and have no fallback body here.

I'd suggest calling it llvm_fshl or so, that seems less confusing.

View changes since the review

Comment on lines +22 to +24
pub const unsafe fn fake_align_of_val_llvm<T>(x: *const T) -> usize {
0
}

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

Same here, please remove the fallback body (and pick a better name)

View changes since the review

Comment on lines +54 to +56
pub const unsafe fn fake_align_of_val_llvm<T>(x: *const T) -> usize {
0
}

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

also remove this fallback body (and pick a better name)

View changes since the review

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-attributes Area: Attributes (`#[…]`, `#![…]`) 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-clippy Relevant to the Clippy team. 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.

8 participants