Skip to content

Conversation

@bjorn3
Copy link
Member

@bjorn3 bjorn3 commented Nov 5, 2025

LLVM intrinsics have weird requirements like requiring the fake "unadjusted" abi, not being callable through function pointers and for all codegen backends other than cg_llvm requiring special cases to redirect them to the correct backend specific intrinsic (or directly codegen their implementation inline without any intrinsic call). By splitting the LLVM intrinsic handling it becomes easier for backends to special case them and should in the future allow getting rid of the abi calculation for extern "unadjusted" in favor of computing the correct abi directly in the backend without depending on the exact way cg_ssa lowers types.

Intrinsics only need a fraction of the functionality offered by
BuilderMethods::call and in particular don't need the FnAbi to be
computed other than (currently) as step towards computing the function
value type.
@rustbot
Copy link
Collaborator

rustbot commented Nov 5, 2025

Some changes occurred in compiler/rustc_codegen_gcc

cc @antoyo, @GuillaumeGomez

Some changes occurred in compiler/rustc_codegen_ssa

cc @WaffleLapkin

@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. labels Nov 5, 2025
@rustbot
Copy link
Collaborator

rustbot commented Nov 5, 2025

r? @SparrowLii

rustbot has assigned @SparrowLii.
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

@bjorn3
Copy link
Member Author

bjorn3 commented Nov 5, 2025

cc @sayantn This should make #140763 much less intrusive for the non-intrinsic call handling and should allow you to localize all changes to cg_llvm without having to touch cg_ssa and cg_gcc.

@sayantn
Copy link
Contributor

sayantn commented Nov 5, 2025

this is awesome, thanks ❤️. I tested compiling stdarch with this branch (for x86_64-unknown-linux-gnu, aarch64-unknown-linux-gnu and riscv64gc-unknown-linux-gnu targets), it doesn't generate any errors

@bjorn3
Copy link
Member Author

bjorn3 commented Nov 5, 2025

Thanks for testing! I hadn't thought about testing this on stdarch. Did you also test compiling the stdarch tests? Otherwise no llvm intrinsic calls are actually getting codegened I think due to all #[inline(always)].

@sayantn
Copy link
Contributor

sayantn commented Nov 5, 2025

yes, I did cargo build -p core_arch --tests, which will compile all intrinsics in x86 (due to all intrinsics having corresponding tests), I am not so sure about aarch64, there can be some intrinsics that are not tested.

@sayantn
Copy link
Contributor

sayantn commented Nov 10, 2025

@bjorn3 could you see if it's possible to split the function declaration of LLVM intrinsics too - after inspecting my PR I noticed that it modifies declaration more than calling. Thanks

@bjorn3
Copy link
Member Author

bjorn3 commented Nov 10, 2025

I figured I would do that after this PR gets merged, but I can do it somewhere in the next couple of days too. In this PR if it hasn't been reviewed by then and otherwise in a separate PR.

This moves all LLVM intrinsic handling out of the regular call path for
cg_gcc and makes it easier to hook into this code for future cg_llvm
changes.
@bjorn3
Copy link
Member Author

bjorn3 commented Nov 10, 2025

Something like this @sayantn?

@antoyo @GuillaumeGomez would you mind reviewing the cg_gcc changes?

@sayantn
Copy link
Contributor

sayantn commented Nov 10, 2025

Thanks, It's nice, but do we need to cache the intrinsic instances? get_fn should be called only when declaring an intrinsic, and that's mostly one-time for an intrinsic

@antoyo
Copy link
Contributor

antoyo commented Nov 10, 2025

@antoyo @GuillaumeGomez would you mind reviewing the cg_gcc changes?

This does look good to me, but I would need to run the tests of cg_gcc to confirm.
@GuillaumeGomez: Is there an easy way to run all of cg_gcc's tests in the Rust repo?

@bjorn3
Copy link
Member Author

bjorn3 commented Nov 10, 2025

It's nice, but do we need to cache the intrinsic instances?

codegen_llvm_intrinsic_call is called once per intrinsic call, not once per intrinsic declaration.

@GuillaumeGomez
Copy link
Member

@GuillaumeGomez: Is there an easy way to run all of cg_gcc's tests in the Rust repo?

./x.py test --test-codegen-backend=gcc should do it.

@antoyo
Copy link
Contributor

antoyo commented Nov 10, 2025

./x.py test --test-codegen-backend=gcc should do it.

Isn't that only running UI tests?

@GuillaumeGomez
Copy link
Member

No, any testsuite with this argument will use the GCC backend.

@antoyo
Copy link
Contributor

antoyo commented Nov 10, 2025

No, any testsuite with this argument will use the GCC backend.

I meant that this won't run the stdarch tests or other tests we have in the CI of the cg_gcc repo.
We need a test that uses LLVM intrinsics and I'm not sure if those we can run in the Rust repo has any.

@bjorn3
Copy link
Member Author

bjorn3 commented Nov 10, 2025

Maybe you could build rustc with cg_gcc as default backend and then manually run the stdarch test suite using it?

@sayantn
Copy link
Contributor

sayantn commented Nov 10, 2025

Ah sorry, the current design looks nice. I will test it on stdarch locally when I get some time

@antoyo
Copy link
Contributor

antoyo commented Nov 10, 2025

Maybe you could build rustc with cg_gcc as default backend and then manually run the stdarch test suite using it?

I tried to build your branch locally and it seems it broke a stage-2 build with cg_gcc.
I get this error:

libgccjit.so: error: : bitcast with types of different sizes
input expression (size: 8):
 <integer_cst 0x7f9ff9560348 type <integer_type 0x7f9ff954b498 unsigned char> constant visited 0>
requested type (size: 32):
 <integer_type 0x7f9ff954bc78 int public SI
    size <integer_cst 0x7f9ff9560468 type <integer_type 0x7f9ff954b690 bitsizetype> constant 32>
    unit-size <integer_cst 0x7f9ff9560480 type <integer_type 0x7f9ff954b5e8 sizetype> constant 4>
    align:32 warn_if_not_align:0 symtab:0 alias-set -1 canonical-type 0x7f9ff954bc78 precision:32 min <integer_cst 0x7f9ff9560420 -2147483648> max <integer_cst 0x7f9ff9560438 2147483647>
    pointer_to_this <pointer_type 0x7f9ff95691f8>>

@bjorn3
Copy link
Member Author

bjorn3 commented Nov 10, 2025

Does it work with the last commit reverted?

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants