Skip to content

Change codegen of LLVM intrinsics to be name-based, and add llvm linkage support for x86amx #140763

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 5 commits into
base: master
Choose a base branch
from

Conversation

sayantn
Copy link
Contributor

@sayantn sayantn commented May 7, 2025

This PR changes how LLVM intrinsics are codegen

Explanation of the changes

Current procedure

This is the same for all functions, LLVM intrinsics are not treated specially

  • We get the LLVM Type of a function simply using the argument types. For example, the following function
    #[link_name = "llvm.sqrt.f32"]
    fn sqrtf32(a: f32) -> f32;
    will have LLVM type simply f32 (f32) due to the Rust signature

Pros

  • Simpler to implement, no extra complexity involved due to LLVM intrinsics

Cons

  • LLVM intrinsics have a well-defined signature, completely defined by their name (and if it is overloaded, the type parameters). So, this process of converting Rust signatures to LLVM signatures may not work, for example the following code generates LLVM IR without any problem
    #[link_name = "llvm.sqrt.f32"]
    fn sqrtf32(a: i32) -> f32;
    but the generated LLVM IR is invalid, because it has wrong signature for the intrinsic (Godbolt, adding -Zverify-llvm-ir to it will fail compilation). I would expect this code to not compile at all instead of generating invalid IR.
  • LLVM intrinsics that have types in their signature that can't be accessed from Rust (notable examples are the AMX intrinsics that have the x86amx type, and (almost) all intrinsics that have vectors of i1 types) can't be linked to at all. This is a (major?) roadblock in the AMX and AVX512 support in stdarch.

What this PR does

  • When linking to non-overloaded intrinsics, we use the function LLVMIntrinsicGetType to directly get the function type of the intrinsic from LLVM.
  • We then use this LLVM definition to verify the Rust signature, and emit a proper error if it doesn't match, instead of silently emitting invalid IR.

This PR only focuses on non-overloaded intrinsics, overloaded can be done in a future PR

Pros

  • This helps a lot in the implementation of #[rust_intrinsic] functions, we don't have to manually hardcode/compute the LLVM signature of the intrinsics. This reduces a lot of complexity in the implementation of Rust intrinsics (bonus: now is_val_statically_known is properly implemented for all immediate operands)

  • It is now not possible (or at least, it would require significantly more leaps and bounds) to introduce invalid IR using non-overloaded LLVM intrinsics.

  • As we are now doing the matching of Rust signatures to LLVM intrinsics ourselves, we can now add bypasses to enable linking to such non-Rust types (e.g. matching 8192-bit vectors to x86amx and injecting llvm.x86.cast.vector.to.tile and llvm.x86.cast.tile.to.vectors in callsite)

    Also, small clarification, I don't intend for these bypasses to be permanent (at least the bf16 and i1 ones, the x86amx bypass seems inevitable). A better approach will be introducing a bf16 type in Rust, and allowing repr(simd) with bools to get Rust-native i1xNs. These are meant to be short-time, as I mentioned, "bypass"es. They shouldn't cause any major breakage even if removed, as link_llvm_intrinsics is perma-unstable.

    This PR adds bypasses for x86amx (via 8192-bit vectors), bf16 (via i16) and bf16xN (via i16xN). This will unblock AMX, and a lot of bf16 intrinsics in stdarch.

Cons

  • It's not possible to do this to all LLVM intrinsics, magic AutoUpgrade code in LLVM sometimes replaces old and removed intrinsics with new and shiny ones. One particular example is llvm.x86.avx2.vperm2i128, this intrinsic has since been completely removed from the IntrinsicsX86.td file. Instead LLVM changes this to shufflevector instructions (Godbolt).
    There is a silver lining here though, LLVM does expose a function to check if an intrinsic call can be "upgrade"d (llvm::UpgradeIntrinsicFunction), we can probably use this.
  • This only works for non-overloaded intrinsics (at least for now). Improving this to work with overloaded intrinsics too will involve significantly more work.

Possible ways to extend this to overloaded intrinsics (future)

Parse the mangled intrinsic name to get the type parameters

LLVM has a stable mangling of intrinsic names with type parameters (in LLVMIntrinsicCopyOverloadedName2), so we can parse the name to get the type parameters, and then just do the same thing.

Pros

  • For most intrinsics, this will work perfectly, and is a easy way to do this.

Cons

  • The LLVM mangling is not perfectly reversible. When we have TargetExt types or identified structs, their name is a part of the mangling, making it impossible to reverse. Even more complexities arise when there are unnamed identified structs.

Use the IITDescriptor table and the Rust function signature

We can use the base name to get the IITDescriptors of the corresponding intrinsic, and then manually implement the matching logic based on the Rust signature.

Pros

  • Doesn't have the above mentioned limitation of the parsing approach, has correct behavior even when there are identified structs and TargetExt types. Also, fun fact, Rust exports all struct types as literal structs (unless it is emitting LLVM IR, then it always uses named identified structs, with mangled names)

Cons

  • Doesn't actually use the type parameters in the name, only uses the base name and the Rust signature to get the llvm signature (although we can check that it is the correct name). It means there would be no way to (for example) link against llvm.sqrt.bf16 until we have bf16 types in Rust. Because if we are using u16s (or any other type) as bf16s, then the matcher will deduce that the signature is u16 (u16) not bf16 (bf16) (which would lead to an error because u16 is not a valid type parameter for llvm.sqrt), even though the intended type parameter is specified in the name.
  • Much more complex, and hard to maintain as LLVM gets new IITDescriptorKinds

Other things that this PR does

  • disables all ABI checks only for the unadjusted ABI to facilitate the implementation of AMX (otherwise passing 8192-bit vectors to the intrinsic won't be allowed). This is "safe" because this ABI is only used to link to LLVM intrinsics, and passing vectors of any lengths to LLVM intrinsics is fine, because they don't exist in machine level.
  • Removes unnecessary bitcasts in cg_llvm/builder::check_call (now renamed as cast_arguments due to its new counterpart cast_return). This was old code from when Rust used to pass non-erased lifetimes to LLVM.

Reviews are welcome, as this is my first time actually contributing to rustc

After the design is finalized, we would need a try build, a rustc-perf run and a crater run.

@rustbot label T-compiler A-codegen A-LLVM
r? codegen

@rustbot rustbot added 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. O-x86_64 Target: x86-64 processors (like x86_64-*) (also known as amd64 and x64) labels May 7, 2025
@rustbot
Copy link
Collaborator

rustbot commented May 8, 2025

Some changes occurred in compiler/rustc_codegen_ssa

cc @WaffleLapkin

@sayantn

This comment has been minimized.

@rust-log-analyzer

This comment has been minimized.

@rust-log-analyzer

This comment has been minimized.

@rustbot
Copy link
Collaborator

rustbot commented May 8, 2025

Some changes occurred in compiler/rustc_codegen_gcc

cc @antoyo, @GuillaumeGomez

@rust-log-analyzer

This comment has been minimized.

@sayantn sayantn changed the title Add auto-bitcasts from/to x86amx and i32x256 for AMX intrinsics Add auto-bitcasts from/to x86amx for i32x256 for AMX intrinsics May 8, 2025
@sayantn sayantn changed the title Add auto-bitcasts from/to x86amx for i32x256 for AMX intrinsics Add auto-bitcasts between x86amx and i32x256 for AMX intrinsics May 8, 2025
@sayantn

This comment has been minimized.

@dianqk
Copy link
Member

dianqk commented May 9, 2025

I think you can use LLVMGetIntrinsicDeclaration, LLVMGetIntrinsicDeclaration or some functions in Intrinsic.h in declare_raw_fn, as a reference: https://github.com/llvm/llvm-project/blob/d35ad58859c97521edab7b2eddfa9fe6838b9a5e/llvm/lib/AsmParser/LLParser.cpp#L330-L335.

@sayantn
Copy link
Contributor Author

sayantn commented May 9, 2025

That can be used to improve performance, I am not really focusing on performance in this PR. I want to currently emphasize the correctness of the codegen.

@sayantn
Copy link
Contributor Author

sayantn commented May 9, 2025

Oh wait, I probably misunderstood your comment, you meant using the llvm declaration by itself. Yeah, that would be better, thanks for the info. I will update the impl when I get the chance

@dianqk
Copy link
Member

dianqk commented May 15, 2025

Oh wait, I probably misunderstood your comment, you meant using the llvm declaration by itself. Yeah, that would be better, thanks for the info. I will update the impl when I get the chance

I think you can just focus on non-overloaded functions for this PR. Overloaded functions and type checking that checking Rust function signatures using LLVM defined can be subsequent PRs.

@rustbot author

@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 May 15, 2025
@rustbot
Copy link
Collaborator

rustbot commented May 15, 2025

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

@sayantn

This comment has been minimized.

@rust-log-analyzer

This comment has been minimized.

@sayantn sayantn marked this pull request as draft May 19, 2025 07:23
@nikic
Copy link
Contributor

nikic commented May 19, 2025

@sayantn Taking the address of an intrinsic is invalid LLVM IR.

@rust-log-analyzer

This comment has been minimized.

@rust-log-analyzer

This comment has been minimized.

Copy link
Contributor

@nikic nikic left a comment

Choose a reason for hiding this comment

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

We should not parse mangled intrinsic names.

I think there was a previous version of this PR that did something more reasonable.

@sayantn
Copy link
Contributor Author

sayantn commented May 24, 2025

@nikic I am not parsing the mangled names, it's just the link names. LLVM doesn't irreversibly mangle intrinsic names unless there are non-literal unnamed struct types in the signature (I got this from the implementation of Intrinsic::getName), and the doc for mangleTypeStr says the it returns a stable mangling. Rust only creates non-literal struct types if we are emitting llvm-ir, and also their names are subject to unspecified mangling, so ppl can't stably link against llvm intrinsics that have named struct type parameters (that's why I left that part unhandled). Also, if the parsing fails, we just revert back to the old implementation.

I don't get why this is problematic (I had to do this parsing by hand because LLVM doesn't expose anything for this purpose)

Also, the previous version used heuristics for detecting when x86amx is a parameter, which is not exactly reliable

@dianqk
Copy link
Member

dianqk commented May 24, 2025

r? nikic (Since you've already started the review, and you know more as well.)

@rustbot rustbot assigned nikic and unassigned dianqk May 24, 2025
@sayantn
Copy link
Contributor Author

sayantn commented May 24, 2025

@nikic I currently have one more possible implementation in mind - manually analyzing all the IITDescriptors to match the signature (which will also give us the type parameters). This is essentially recreating the functionality of Intrinsic::matchIntrinsicSignature (we can't use that because we don't have the type parameters). This implementation will be quite a bit longer, but will be more flexible (and probably more performant).

What do you think about this?

@rustbot rustbot added A-codegen Area: Code generation A-LLVM Area: Code generation parts specific to LLVM. Both correctness bugs and optimization-related issues. and removed O-x86_64 Target: x86-64 processors (like x86_64-*) (also known as amd64 and x64) labels May 24, 2025
@sayantn sayantn changed the title Change codegen of LLVM intrinsics to be name-based, and add llvm linkage support for x86amx, bf16 and i1 Change codegen of LLVM intrinsics to be name-based, and add llvm linkage support for x86amx May 24, 2025
@rust-log-analyzer

This comment has been minimized.

Add support fot `bf16` linking
@sayantn sayantn requested a review from nikic May 25, 2025 20:11
@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 May 25, 2025
@sayantn
Copy link
Contributor Author

sayantn commented May 25, 2025

I have currently changed this to just ignore overloaded intrinsics, we need to find a way to make this work with overloaded functions in the future.

@sayantn sayantn marked this pull request as ready for review May 25, 2025 20:13
@rust-log-analyzer
Copy link
Collaborator

The job x86_64-gnu-llvm-19 failed! Check out the build log: (web) (plain)

Click to see the possible cause of the failure (guessed by this bot)
#19 exporting to docker image format
#19 sending tarball 20.5s done
#19 DONE 26.1s
##[endgroup]
Setting extra environment values for docker:  --env ENABLE_GCC_CODEGEN=1 --env GCC_EXEC_PREFIX=/usr/lib/gcc/
[CI_JOB_NAME=x86_64-gnu-llvm-19]
[CI_JOB_NAME=x86_64-gnu-llvm-19]
debug: `DISABLE_CI_RUSTC_IF_INCOMPATIBLE` configured.
---
sccache: Listening on address 127.0.0.1:4226
##[group]Configure the build
configure: processing command line
configure: 
configure: build.configure-args := ['--build=x86_64-unknown-linux-gnu', '--llvm-root=/usr/lib/llvm-19', '--enable-llvm-link-shared', '--set', 'rust.randomize-layout=true', '--set', 'rust.thin-lto-import-instr-limit=10', '--set', 'build.print-step-timings', '--enable-verbose-tests', '--set', 'build.metrics', '--enable-verbose-configure', '--enable-sccache', '--disable-manage-submodules', '--enable-locked-deps', '--enable-cargo-native-static', '--set', 'rust.codegen-units-std=1', '--set', 'dist.compression-profile=balanced', '--dist-compression-formats=xz', '--set', 'rust.lld=false', '--disable-dist-src', '--release-channel=nightly', '--enable-debug-assertions', '--enable-overflow-checks', '--enable-llvm-assertions', '--set', 'rust.verify-llvm-ir', '--set', 'rust.codegen-backends=llvm,cranelift,gcc', '--set', 'llvm.static-libstdcpp', '--set', 'gcc.download-ci-gcc=true', '--enable-new-symbol-mangling']
configure: build.build          := x86_64-unknown-linux-gnu
configure: target.x86_64-unknown-linux-gnu.llvm-config := /usr/lib/llvm-19/bin/llvm-config
configure: llvm.link-shared     := True
configure: rust.randomize-layout := True
configure: rust.thin-lto-import-instr-limit := 10
---
   Compiling generic-array v0.14.7
error: failed to run custom build command for `libc v0.2.172`

Caused by:
  process didn't exit successfully: `/checkout/obj/build/x86_64-unknown-linux-gnu/stage1-rustc/release/build/libc-df26e3c22d08add9/build-script-build` (signal: 11, SIGSEGV: invalid memory reference)
  --- stdout
  cargo:rerun-if-changed=build.rs
warning: build failed, waiting for other jobs to finish...
error: failed to run custom build command for `proc-macro2 v1.0.95`

Caused by:
  process didn't exit successfully: `/checkout/obj/build/x86_64-unknown-linux-gnu/stage1-rustc/release/build/proc-macro2-da0658483962ceb7/build-script-build` (signal: 11, SIGSEGV: invalid memory reference)
[RUSTC-TIMING] bitflags test:false 0.882
[RUSTC-TIMING] build_script_build test:false 0.232
[RUSTC-TIMING] build_script_build test:false 0.457
[RUSTC-TIMING] autocfg test:false 0.932
[RUSTC-TIMING] build_script_build test:false 0.213

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-codegen Area: Code generation 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.

5 participants