-
Notifications
You must be signed in to change notification settings - Fork 12.9k
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
missed(?) optimization with a const array of same item #107208
Comments
Minimal C reproducer produces identical code: https://godbolt.org/z/E97jeGshr |
Indeed, even if it's an LLVM bug, we often keep issues open in this repo to track the Rust reproducer and make sure the upstream change actually fixes it.
If you mean something like https://godbolt.org/z/hn8WMd7Gx, the optimization is simpler in that case because It's harder when the load is from a variable index. LLVM should already handle the case where all bits are the same (though this wouldn't help your reproducer) in |
yep, that's the kind of thing i was thinking.
ah! that makes much more sense; i hadn't thought about the index being a constant in that case. |
i've just realized:
particularly given the C reproducer (thanks alex!), i should go open an issue against |
Yes please! Someone will likely get around to it Eventually but, y'know, |
Fold LoadInst for uniformly initialized constants, even if there are non-constant GEP indices. Goal proof: https://alive2.llvm.org/ce/z/oZtVby Motivated by rust-lang/rust#107208 Differential Revision: https://reviews.llvm.org/D144184
Fold LoadInst for uniformly initialized constants, even if there are non-constant GEP indices. Goal proof: https://alive2.llvm.org/ce/z/oZtVby Motivated by rust-lang/rust#107208 Differential Revision: https://reviews.llvm.org/D144184
Upstream patch: https://reviews.llvm.org/D144445 |
(Assigning to myself to check back after the LLVM 17 upgrade). |
This works now if But not if it's a This appears to be a regression from @scottmcm's changes to replace memcpy with vector load+store in some cases. As a side effect of that change, const arrays now get materialized as explicit vector stores at every use, instead of referencing a global. That seems potentially pretty bad. |
For the case with function pointers: ...when the function pointers are all the same declared function, this was fixed in 1.73: https://godbolt.org/z/WMefYsah4 ...when the function pointers are from closures, it still isn't resolved: https://godbolt.org/z/5baEW4ro8 (it seems like this is because the two closure functions get merged too late) |
Fold LoadInst for uniformly initialized constants, even if there are non-constant GEP indices. Goal proof: https://alive2.llvm.org/ce/z/oZtVby Motivated by rust-lang/rust#107208 Differential Revision: https://reviews.llvm.org/D144184
This looks to be working as expected on release: https://godbolt.org/z/enf7a7Pa3 Can this be closed as is, or should we add a codegen test? |
We should add a codegen test. |
Ok, test in #134852 |
Added a codegen test for optimization with const arrays Closes rust-lang#107208
Rollup merge of rust-lang#134852 - alex:patch-1, r=durin42 Added a codegen test for optimization with const arrays Closes rust-lang#107208
some example code:
compiles to (via 1.66.1, or 1.68.0-nightly from 2023-01-21):
rustc faithfully produces an array of
0x01, 0x01
in the resulting program and loads from it. it seems to me rustc ought to be able to tell that all entries in the array are identical, use that value, and avoid loading from the array in the first place.i'm not entirely sure if this should be an issue against
rust-lang/rust
or bug against LLVM, but i tried looking throughI-slow
for similar reports. so i think starting here is the right call :D thanks!edit: it does seem remarkable that the load is eliminated if there is only one item in the array. that looks to be handled somewhere before generating LLVM IR, so i think this is the right place.
i happened to notice this with LUT being an array of function pointers, included for completeness
compiles to (via 1.66.1, or 1.68.0-nightly from 2023-01-21):
similarly, the array is indexed regardless of the fact that the array's items are all identical. this, in turn, is a minimized form of the original code with dozens of entries in the array. in that case,
LUT
is an associated item on a trait where one implementation happens to result in all function pointers being the same nearly-no-op function.The text was updated successfully, but these errors were encountered: