-
Notifications
You must be signed in to change notification settings - Fork 13.8k
Add an attribute to check the number of lanes in a SIMD vector after monomorphization #146667
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?
Add an attribute to check the number of lanes in a SIMD vector after monomorphization #146667
Conversation
This comment has been minimized.
This comment has been minimized.
Hmm, that's not good. I'm probably using the visitor wrong. I'll investigate tomorrow |
I think I understand the goal here, but I'm not sure what exactly one has to do to actually find all the types that could be relevant. Iterating the signature and the locals feels ad-hoc, and also redundant since all arguments and the return value also appear as MIR locals. And then you try to recursively iterate the type and its fields... no idea if that makes sense, sorry. I also don't have the time right now to dig into this. r? types Also Cc @BoxyUwU @lcnr @compiler-errors since this is essentially adding a compiler hack to work around const generics being painful, IIUC. (Would be good to reference an issue with context and prior discussion describing why the |
for background on dropping the lane count traits: rust-lang/portable-simd#364 (comment) |
https://rust-lang.zulipchat.com/#narrow/channel/182449-t-compiler.2Fhelp/topic/mono.20check.20that.20walks.20all.20types/near/539909904 this is the related zulip thread? Going to ignore the fact that I personally feel somewhat iffy about this change and feel in favor of a I think visiting all locals (and their fields) is very... questionable 😅 You'vbe mentioned putting this check into We already have similar errors for arrays, e.g. |
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.
this should indeed get a new-style attribute parser
Adding the I could do this in layout, and perhaps it's more precise there. I think the compromise is that layout errors don't track a source span, but maybe that can be added. |
I like the analogy with "this type is too big for your target". |
If this is a backend limitation, why is the maximum lane count configured by the library in the type? The max size is a hard-coded constant in the compiler, after all. |
This is probably the most relevant zulip thread: https://rust-lang.zulipchat.com/#narrow/channel/257879-project-portable-simd/topic/Post-mono.20errors.20for.20better.20UX A little bit of info in this issue as well, though mostly outdated as we now support non-power-of-two lengths: rust-lang/portable-simd#364 If I were able to somehow embed a const assert rather than a post-mono error I would be much happier with that, but AFAIK that's still not possible |
Unfortunately I tried that in the past (#80652) and failed crater because some crates are using |
It's a nightly-only feature so I am not too bothered by that. EDIT: Looking at that PR, it seems the failure was mostly about non-power-of-2 vectors, not too big vectors?
That seems to be an argument in favor of having this in the compiler. (If we want to accept such non-portability; the max size currently only depends on the target pointer width.) |
For std::simd, we have settled on 64 elements as a reasonable maximum size. With larger vectors, you can see weird unrolling and writing vectors to memory particularly on architectures with smaller vectors. I wouldn't want to enforce 64 elements for the |
only for when we need separate trait impls for each size, when we don't need the trait impls I think 16384 is better since that is actually the hardware size on some RISC-V CPUs. |
Some changes occurred in compiler/rustc_passes/src/check_attr.rs Some changes occurred in compiler/rustc_attr_parsing Some changes occurred in compiler/rustc_hir/src/attrs |
f7e773e
to
9ff49f8
Compare
9ff49f8
to
b79fa8e
Compare
I switched to doing the check in layout calculation and there's no guesswork with making sure you don't miss checking the limit. The layout error is definitely worse (it doesn't even have a span). I tried giving it a span, but the only span available is the struct item, which IMO is also not very useful. Maybe a future change can pipe through a span that points to where the type is actually used. |
b79fa8e
to
83ca8cd
Compare
r=me for the attribute stuff. I don't think I can judge the rest |
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.
maybe add a cross-crate test? idk how easy that is though
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.
yeah, I'm not sure how to do that, or what the criteria for that would be
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.
You use aux crates. A cross crate test seems useful. https://rustc-dev-guide.rust-lang.org/tests/compiletest.html?highlight=Aux#building-auxiliary-crates
I'm happy if someone can approve the layout error generation, and we leave the discussion of whether we should use this attribute or another solution for the RFC process before stabilization |
IIRC the error spans for too big types are much better than what you describe here, so this can probably be fixed by making Directly emitting (I get that this is pre-existing, so this is for a future PR. But you seemed to think that this is somehow fundamental to raising the error during layout generation, when it isn't. We have all the infrastructure needed for better errors there, it looks like someone just cut some corners when adding these SIMD errors.) |
I believe returning the info to the caller in the same way as size errors should be done in this PR |
This comment has been minimized.
This comment has been minimized.
e7207c1
to
2c875a0
Compare
I suspect that was me :) Though at the time (and on stable today) the backends didn't report spans for layout errors. I changed the layout errors to report properly. |
This comment has been minimized.
This comment has been minimized.
|
||
fn main() { | ||
let _a: V<i32, 6> = V([0; 6]); //~ ERROR the SIMD type `V<i32, 6>` has more elements than the limit 4 | ||
} |
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.
why does this test exist? it's a copy of tests/ui/simd/simd-lane-limit-err.rs
, is it not?
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.
because it's testing a different length because repr(simd, packed)
does some unusual stuff with non-powers-of-2
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.
please add a comment explaining that then
please add a cross-crate test and also make sure to track the fact that simd len is now a post-mono error somewhere so that it's mentioned when stabilizing this r=me after that |
I changed the tests to be cross-crate. I figure we'll document the post-mono error in std::simd once we apply the attribute |
Allows std::simd to drop the
LaneCount<N>: SupportedLaneCount
trait and maintain good error messages.r? @RalfJung
cc @workingjubilee @programmerjake