-
Notifications
You must be signed in to change notification settings - Fork 13.4k
Change codegen of LLVM intrinsics to be name-based, and add llvm linkage support for bf16(xN)
and i1xN
#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
base: master
Are you sure you want to change the base?
Conversation
Some changes occurred in compiler/rustc_codegen_ssa |
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
Some changes occurred in compiler/rustc_codegen_gcc |
This comment has been minimized.
This comment has been minimized.
x86amx
for i32x256
for AMX intrinsics
x86amx
for i32x256
for AMX intrinsicsx86amx
and i32x256
for AMX intrinsics
This comment has been minimized.
This comment has been minimized.
I think you can use |
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. |
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 |
Reminder, once the PR becomes ready for a review, use |
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
@sayantn Taking the address of an intrinsic is invalid LLVM IR. |
This comment has been minimized.
This comment has been minimized.
This PR modifies cc @jieyouxu |
There was an invalid intrinsic in |
After CI is green, I think we should do a try build and an rustc-perf run, because this modifies quite a bit in the codegen of rust and llvm intrinsics, which might have some performance footprint (I am especially concerned about rust-intrinsics, I got rid of the signature table on grounds that we don't need to do it anymore, but we might still need it as a cache) |
x86amx
x86amx
, bf16(xN)
and i1xN
This comment has been minimized.
This comment has been minimized.
Can anyone help here? IDK what's going on, in my system (x86_64-unknown-linux-gnu), I can actually see the |
Assuming they can stand separately, can we split up the intrinsic changes from the |
@workingjubilee If you mean I should separate the implementation changes of Rust intrinsics into another PR, yeah no problem, but I feel like they go with this PR. I can try separating them into clean commits, but separating them into different PRs will imo not be good. But I don't think I should separate the loosening of ABI check rules for PS: I don't really like that we use the same term for rustc builtins and LLVM builtins, but oh well |
- Remove redundant bitcasts at callsite edit (squash with struct)
Sorry for the repeated force pushes, I refactored my code quite a bit, and restructured the commits |
- Correct usage of invalid intrinsics in tests
As @workingjubilee suggested I will split this PR into 2, this will only add bypasses for |
x86amx
, bf16(xN)
and i1xN
bf16(xN)
and i1xN
TypeKind::Struct if self.type_kind(rust_ty) == TypeKind::Struct => { | ||
let rust_element_tys = self.struct_element_types(rust_ty); | ||
let llvm_element_tys = self.struct_element_types(llvm_ty); | ||
|
||
if rust_element_tys.len() != llvm_element_tys.len() { | ||
return false; | ||
} | ||
|
||
iter::zip(rust_element_tys, llvm_element_tys).all( | ||
|(rust_element_ty, llvm_element_ty)| { | ||
self.equate_ty(rust_element_ty, llvm_element_ty) | ||
}, | ||
) | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
a little more context for why we destructure struct
s - LLVM typically returns literal non-packed structs. So to match them, Rust code often uses repr(packed)
structs. LLVM autoupgrade understands that there is no functional difference between {T, U}
and <{T, U}>
, and auto-injects some destructurings. But as here we are trying to match the types exactly, it would error as packed structs are not "equal" to non-packed ones. Moreover there is no Rust-native way to creating non-packed LLVM structs with the exact fields we want (in general), because Rust might add extra fields to ensure alignment. Which leaves us with the only option being packed structs, which requires this workaround.
It would have been nice if LLVM offered some way to "pack"/"un-pack" structs, i.e. convert between <{T, U}>
and {T, U}
, but as it doesn't, we have no way other than simply destructure and restructure them (which is, kinda good actually, as it lets us make these autocasts "deep"
let name = llvm::get_value_name(llfn); | ||
if name.starts_with(b"llvm.") | ||
&& let Some(intrinsic) = llvm::Intrinsic::lookup(name) | ||
{ | ||
// FIXME: also do this for overloaded intrinsics | ||
if !intrinsic.is_overloaded() { | ||
return; | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do we need to add attributes even if the signature is getting AutoUpgraded (because at this point we can safely assume that the function is getting upgraded, we would have detected it earlier if it was invalid? I assume the sext
/zext
will matter if LLVM is autoupgrading i32
to i1x8
or sth, but cc @nikic to confirm. If they don't matter even for upgradable intrinsics, we can reduce this to just a name check
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
f32 (f32)
due to the Rust signaturePros
Cons
-Zverify-llvm-ir
to it will fail compilation). I would expect this code to not compile at all instead of generating invalid IR.x86amx
type, and (almost) all intrinsics that have vectors ofi1
types) can't be linked to at all. This is a (major?) roadblock in the AMX and AVX512 support in stdarch.-Zverify-llvm-ir
won't complain. Eventually it will error out due to the non-existing function (courtesy of the linker). I don't think this is a behavior we want.What this PR does
LLVMIntrinsicGetType
to directly get the function type of the intrinsic from LLVM.Note
This PR only focuses on non-overloaded intrinsics, overloaded can be done in a future PR
Regardless, the undermentioned functionalities work for all intrinsics
AutoUpgrade
d by LLVM. If not, that means it is an invalid intrinsic, and we error out.Pros
x86amx
and injectingllvm.x86.cast.vector.to.tile
andllvm.x86.cast.tile.to.vector
s in callsite)Note
I don't intend for these bypasses to be permanent (at least the
bf16
andi1
ones, thex86amx
bypass seems inevitable). A better approach will be introducing abf16
type in Rust, and allowingrepr(simd)
withbool
s to get Rust-nativei1xN
s. These are meant to be short-time, as I mentioned, "bypass"es. They shouldn't cause any major breakage even if removed, aslink_llvm_intrinsics
is perma-unstable.This PR adds bypasses for
bf16
(viai16
),bf16xN
(viai16xN
),i1xN
(viaiM
, whereM
is the smallest power of 2 s.t.M >= N
, unlessN <= 4
, where we useM = 8
). This will unblock AVX512-VP2INTERSECT and a lot of bf16 intrinsics in stdarch. This PR also automatically destructures structs if the types don't exactly match (this is required for us to start emitting hard errors on mismmatches).Cons
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
Cons
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, as LLVM adds more mangling to the names.Use the
IITDescriptor
table and the Rust function signatureWe can use the base name to get the
IITDescriptor
s of the corresponding intrinsic, and then manually implement the matching logic based on the Rust signature.Pros
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
llvm.sqrt.bf16
until we havebf16
types in Rust. Because if we are usingu16
s (or any other type) asbf16
s, then the matcher will deduce that the signature isu16 (u16)
notbf16 (bf16)
(which would lead to an error becauseu16
is not a valid type parameter forllvm.sqrt
), even though the intended type parameter is specified in the name.IITDescriptorKind
sThese 2 approaches might give different results for same function. Let's take
The name-based approach will decide that the type parameter is
bf16
, and the LLVM signature isi1 (bf16)
and will inject some bitcasts at callsite.The
IITDescriptor
-based approach will decide that the LLVM signature isi1 (u16)
, and will see that the name given doesn't match the expected name (llvm.is.constant.u16
), and will error out.Other things that this PR does
disables all ABI checks only for the(moved to another PR)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.bitcast
s incg_llvm/builder::check_call
(now renamed ascast_arguments
due to its new counterpartcast_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 CI is green, we would need a try build and a rustc-perf run.
@rustbot label T-compiler A-codegen A-LLVM
r? codegen