llvm-project icon indicating copy to clipboard operation
llvm-project copied to clipboard

globalopt miscompilation with i32/i64 mismatch in GEP indices

Open ec04fc15-fa35-46f2-80e1-5d271f2ef708 opened this issue 15 years ago • 8 comments

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

llvmbot avatar Nov 26 '21 16:11 llvmbot

Fixed in llvm v3.1, and the consistency of GEP indices types is fixed in llvm v3.8.

ekatz avatar Nov 26 '19 18:11 ekatz

Not really. I added a hack that makes it much less likely to occur, but the fundamental problem is still there.

nlewycky avatar Dec 04 '12 11:12 nlewycky

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. ***

llvmbot avatar Feb 21 '12 23:02 llvmbot

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.

efriedma-quic avatar Nov 10 '11 01:11 efriedma-quic