-
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
Enable noalias annotations #54878
Comments
I’m still working on figuring out the underlying issue. The interesting ticket is #54462. |
Using @nagisa's minimal reproduction: Minimised test case with no unsafe code (make sure to compile with 1 codegen unit!):fn linidx(row: usize, col: usize) -> usize {
row * 1 + col * 3
}
fn swappy() -> [f32; 12] {
let mut mat = [1.0f32, 5.0, 9.0, 2.0, 6.0, 10.0, 3.0, 7.0, 11.0, 4.0, 8.0, 12.0];
for i in 0..2 {
for j in i+1..3 {
if mat[linidx(j, 3)] > mat[linidx(i, 3)] {
for k in 0..4 {
let (x, rest) = mat.split_at_mut(linidx(i, k) + 1);
let a = x.last_mut().unwrap();
let b = rest.get_mut(linidx(j, k) - linidx(i, k) - 1).unwrap();
::std::mem::swap(a, b);
}
}
}
}
mat
}
fn main() {
let mat = swappy();
assert_eq!([9.0, 5.0, 1.0, 10.0, 6.0, 2.0, 11.0, 7.0, 3.0, 12.0, 8.0, 4.0], mat);
} I was able to bisect LLVM's optimization passes to find the one causing the error. Running this command results in a working executeable (replace
While running this command results in a broken executable (the `assert_eq`` fails):
For this file, optimization |
Bisecting LLVM revisions (using llvmlab bisect) narrows it down to r305936-r305938, presumably r305938: [BasicAA] Use MayAlias instead of PartialAlias for fallback. Note that this is a pretty old change, from June 2017. Edit: Looking at the commit description, it seems likely that the bug existed prior to that, but was masked by BasicAA preventing later alias passes from running, which is what the commit fixed. The case involves checking aliasing between a pair of Edit2: Also, passing |
From a look at the pre-GVN IR, I feel like the root cause here might be in loop unrolling, depending on whether my understanding of how LLVM aliasing annotations work is correct. Consider a code like
where In LLVM IR this would go something like:
If we run this through
Note how all four copies of the loop use aliasing metadata over the same aliasing domain. Instead of being noalias within a single iteration, it's noalias across the whole function. Finally,
And this will result in incorrect results if It's possible to reproduce this issue from C with the following code:
With Clang 6.0 this prints |
I reduced it to a simple C test case (compile at -O3 and -O0 and compare output): #include <stdlib.h>
#include <stdio.h>
#include <assert.h>
__attribute__((always_inline))
static inline void copy(int *restrict a, int *restrict b) {
assert(a != b);
*b = *a;
*a = 7;
}
__attribute__((noinline))
void floppy(int mat[static 2], size_t idxs[static 3]) {
for (int i = 0; i < 3; i++) {
copy(&mat[i%2], &mat[idxs[i]]);
}
}
int main() {
int mat[3] = {10, 20};
size_t idxs[3] = {1, 0, 1};
floppy(mat, idxs);
printf("%d %d\n", mat[0], mat[1]);
} Note that if you remove What's happening is:
for (int i = 0; i < 3; i++) {
mat[idxs[i]] = mat[i%2]; mat[i%2] = 7;
}
mat[idxs[0]] = mat[0]; mat[0] = 7; /* from copy(&mat[0%2], &mat[idxs[0]]) */
mat[idxs[1]] = mat[1]; mat[1] = 7; /* from copy(&mat[1%2], &mat[idxs[1]]) */
mat[idxs[2]] = mat[0]; mat[0] = 7; /* from copy(&mat[2%2], &mat[idxs[2]]) */
But Well, it has to do with the way %8 = load i32, i32* %0, align 4, !tbaa !8, !alias.scope !10, !noalias !13
store i32 %8, i32* %7, align 4, !tbaa !8, !alias.scope !13, !noalias !10
store i32 7, i32* %0, align 4, !tbaa !8, !alias.scope !10, !noalias !13 Normally, if a function is inlined multiple times, each copy gets its own unique IDs for alias.scope and noalias, indicating that each call represents its own 'inequality' relationship* between the pair of arguments marked However, in this case, first the function is inlined into the loop, then the inlined code is duplicated when the loop is unrolled – and this duplication does not change the IDs. Because of this, LLVM thinks none of the Amazingly, GCC also miscompiles this, with different output. (clang and GCC at -O0 both output * It's a bit more complicated than that, but in this case, since |
Heh, looks like I raced with @nikic to find the same explanation. Their test case is slightly nicer :) |
That's some really great timing ^^ We reached the same conclusion with nearly the same reduced test case at the same time :) To fix this, probably something along the lines of https://github.com/llvm-mirror/llvm/blob/54d4881c352796b18bfe7314662a294754e3a752/lib/Transforms/Utils/InlineFunction.cpp#L801 needs to be also be done in LoopUnrollPass. |
I've submitted an LLVM bug report for this issue at https://bugs.llvm.org/show_bug.cgi?id=39282. |
And – just mentioning this for completeness – I submitted a bug report to GCC since it also miscompiled my C test case: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=87609 |
Triage: If I'm reading this correctly, the LLVM fix was accepted (https://reviews.llvm.org/D9375). I'm unsure what that means for actually merging into LLVM, or when that happened; someone more knowledgeable about LLVM's revision process should check if the issue is fixed now (and for what versions). |
It's not merged, the review process was a bit odd and the person who OK'ed it don't review patches anymore. |
There's been a call for testing with the "full restrict" patch set. It would probably be valuable if someone tried out re-enabling noalias in Rust on top of an llvm that has this patch set applied. |
A fix for the LLVM bug has now landed. |
Do we know yet what kind of performance improvement is likely to result from this? |
@arzg When noalias annotations were first disabled in 2015 it resulted in between 0-5% increased runtime in various benchmarks. A lot can change in five years, but that seems like a decent ballpark estimate of what sort of performance might result from turning this back on. (Note that because of the nature of this brand of optimization, certain hot loops could actually benefit much more than this.) |
@jrmuizel Could you link the announcement? I dont see anything on the review board of the LLVM patch. I would also expect miscompilations due to the nasty pointer provenance. |
@matu3ba the patch that fixed it is here https://reviews.llvm.org/D92887 |
Now that LLVM 12 has branched, can we test and benchmark this? Or do we have to wait for its release? |
@Muximize I tried compiling rustc with the LLVM master branch yesterday and ran into compilation errors. rustc will need to add support for the newer version of LLVM before we can benchmark anything. The fact that LLVM 12 has branched doesn't really mean all that much; rustc routinely pins its dependency to arbitrary points on the LLVM master branch whenever it desires some new LLVM feature: see https://github.com/rust-lang/llvm-project |
The upgrade to LLVM 12 is currently in progress, but it will take a while until all regressions are sorted out. Currently it's not possible to build stage2 due to https://bugs.llvm.org/show_bug.cgi?id=48888. |
Seems like the issue has already been fixed. |
Is there any option to turn on the mtuable-noalias using cargo? I tried cargo +nightly build --release -Z mutable-noalias=yes and it said no such option. |
Try |
Now it worked, thank you for the quick response. |
The LLVM update is happening in #81451 |
It seems like #63818 needs to be resolved before this can be enabled by default. |
As #63818 is unlikely to be solved soon, could we at least re-enable Not knowing much about the compiler internals, I'm not sure either about how complicated this (hopefully temporary) solution would be. Finding cycles in a graph will have compile time impacts, but we could restrict it to |
@cbeuw I think you can do the transitive closure computation if you're restricted to safe Rust, but I have the feeling it might be undecidable in unsafe Rust. My thinking is: since you could have an arbitrary pointer somewhere, you could have a pointer that's part of the transitive closure and which happens to sometiemes point to the struct itself. And to answer whether or not that is the case if given arbitrary unsafe code as the base seems to me like it should be undecidable. |
Sound self-referential types must be |
Self-referential types may be hidden. Instead a crate could export an opaque type containing a box of the self-referential type. In this case it is not necessary for soundness for the self-referential type to be |
Fair. But all this is a hack anyway, so it might still be a reasonable trade-off. |
Enable mutable noalias for LLVM >= 12 Enable mutable noalias by default on LLVM 12, as previously known miscompiles have been resolved. Now it's time to find the next one ;) * The `-Z mutable-noalias` option no longer has an explicit default and accepts `-Z mutable-noalias=yes` and `-Z mutable-noalias=no` to override the LLVM version based default behavior. * The decision on whether to apply the noalias attribute is moved into rustc_codegen_llvm. rustc_middle only provides us with the necessary information to make the decision. * `noalias` is not emitted for types that are `!Unpin`, as a heuristic for self-referential structures (see rust-lang#54878 and rust-lang#63818).
Noalias annotations have been re-enabled in #82834. Place your bets on when it will get reverted ;) |
This issue tracks the undoing of the
-Zmutable-noalias=no
default introduced in #54639 on account of a bug in LLVM. cc @nagisa(Deja vu?)
The text was updated successfully, but these errors were encountered: