globalopt miscompilation with i32/i64 mismatch in GEP indices
| Bugzilla Link | 11352 |
| Resolution | FIXED |
| Resolved on | Nov 26, 2019 10:03 |
| Version | trunk |
| OS | Linux |
| Attachments | IR which globalopt miscompiles |
| CC | @efriedma-quic,@ekatz,@nlewycky |
Extended Description
This code, built by clang, is incorrectly optimized when targeting x86_64-linux:
int xs[] = { 1, xs[0] };
The relevant part of the generated IR looks like this:
@xs = global [2 x i32] zeroinitializer, align 4
define internal void @__cxx_global_var_init() nounwind section ".text.startup" { entry: store i32 1, i32* getelementptr inbounds ([2 x i32]* @xs, i64 0, i64 0) %0 = load i32* getelementptr inbounds ([2 x i32]* @xs, i32 0, i64 0), align 4 store i32 %0, i32* getelementptr inbounds ([2 x i32]* @xs, i64 0, i64 1) ret void }
Note that the GEPs don't have consistent types for their indices. This leads globalopt to believe that the first store and the load do not alias, so it uses the value 0 for %0. This results in:
@xs = global [2 x i32] [i32 1, i32 0], align 4
mentioned in issue llvm/llvm-bugzilla-archive#12060
mentioned in issue llvm/llvm-bugzilla-archive#12060
Fixed in llvm v3.1, and the consistency of GEP indices types is fixed in llvm v3.8.
Not really. I added a hack that makes it much less likely to occur, but the fundamental problem is still there.
Nick: I think this has been fixed for a while, right?
*** Bug llvm/llvm-bugzilla-archive#12060 has been marked as a duplicate of this bug. ***
I think you're right in C++98, but there is a sequenced-before relationship in C++11 (see [dcl.init.list]p4). One of the fun changes for generalized initializer lists :)
I'm not sure your example is actually valid C++ (because there isn't a sequence point, IIRC). That said, yes, globalopt's EvaluateFunction is wrong.