LLVM Bugzilla is read-only and represents the historical archive of all LLVM issues filled before November 26, 2021. Use github to submit LLVM bugs

Bug 49661 - wrong code at -O2 and -O3 on x86_64-linux-gnu
Summary: wrong code at -O2 and -O3 on x86_64-linux-gnu
Status: RESOLVED FIXED
Alias: None
Product: libraries
Classification: Unclassified
Component: Scalar Optimizations (show other bugs)
Version: trunk
Hardware: PC All
: P enhancement
Assignee: Unassigned LLVM Bugs
URL:
Keywords:
: 50253 (view as bug list)
Depends on:
Blocks:
 
Reported: 2021-03-20 10:11 PDT by Zhendong Su
Modified: 2021-05-20 09:39 PDT (History)
6 users (show)

See Also:
Fixed By Commit(s): f34311c4024d


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Zhendong Su 2021-03-20 10:11:25 PDT
[520] % clangtk -v
clang version 13.0.0 (https://github.com/llvm/llvm-project.git f860187ea6e9b30e1ecf74784f0af0e0c9ecc01c)
Target: x86_64-unknown-linux-gnu
Thread model: posix
InstalledDir: /local/suz-local/opfuzz/bin
Found candidate GCC installation: /usr/lib/gcc/i686-linux-gnu/8
Found candidate GCC installation: /usr/lib/gcc/x86_64-linux-gnu/6
Found candidate GCC installation: /usr/lib/gcc/x86_64-linux-gnu/6.5.0
Found candidate GCC installation: /usr/lib/gcc/x86_64-linux-gnu/7
Found candidate GCC installation: /usr/lib/gcc/x86_64-linux-gnu/7.5.0
Found candidate GCC installation: /usr/lib/gcc/x86_64-linux-gnu/8
Selected GCC installation: /usr/lib/gcc/x86_64-linux-gnu/7.5.0
Candidate multilib: .;@m64
Candidate multilib: 32;@m32
Candidate multilib: x32;@mx32
Selected multilib: .;@m64
[521] % 
[521] % clangtk -Os small.c; ./a.out
[522] % 
[522] % clangtk -O2 small.c
[523] % ./a.out
Segmentation fault
[524] % 
[524] % cat small.c
static int *a[2][3][3], b, c;
int main() {
  for (c = 0; c < 2; c++)
    for (b = 0; b < 2; b++)
      a[1][c][b] = a[0][0][0];
  return 0;
}
Comment 1 Simon Pilgrim 2021-03-21 05:24:44 PDT
https://gcc.godbolt.org/z/xTv9PG

Seems to be an alignment issue

@_ZL1a.1 = internal unnamed_addr global [3 x [3 x i32*]] zeroinitializer, align 16

define dso_local i32 @main() local_unnamed_addr #0 {
  store <2 x i32*> zeroinitializer, <2 x i32*>* bitcast ([3 x [3 x i32*]]* @_ZL1a.1 to <2 x i32*>*), align 16
  store <2 x i32*> zeroinitializer, <2 x i32*>* bitcast (i32** getelementptr inbounds ([3 x [3 x i32*]], [3 x [3 x i32*]]* @_ZL1a.1, i64 0, i64 1, i64 0) to <2 x i32*>*), align 16 ; THIS ISN'T 16-BYTE ALIGNED 
  ret i32 0
}
Comment 2 Sanjay Patel 2021-05-14 12:28:33 PDT
This looks the same as bug 50253, but not making it a duplicate yet since I don't know anything about -globalopt.

Before that pass, we have:

@c = internal unnamed_addr global i32 0, align 4
@b = internal unnamed_addr global i32 0, align 4
@a = internal unnamed_addr global [2 x [3 x [3 x i32*]]] zeroinitializer, align 16

; Function Attrs: nofree norecurse nosync nounwind ssp uwtable
define i32 @main() local_unnamed_addr #0 {
entry:
  %0 = load i32*, i32** getelementptr inbounds ([2 x [3 x [3 x i32*]]], [2 x [3 x [3 x i32*]]]* @a, i64 0, i64 0, i64 0, i64 0), align 16, !tbaa !5
  store i32* %0, i32** getelementptr inbounds ([2 x [3 x [3 x i32*]]], [2 x [3 x [3 x i32*]]]* @a, i64 0, i64 1, i64 0, i64 0), align 8, !tbaa !5
  store i32* %0, i32** getelementptr inbounds ([2 x [3 x [3 x i32*]]], [2 x [3 x [3 x i32*]]]* @a, i64 0, i64 1, i64 0, i64 1), align 16, !tbaa !5
  store i32* %0, i32** getelementptr inbounds ([2 x [3 x [3 x i32*]]], [2 x [3 x [3 x i32*]]]* @a, i64 0, i64 1, i64 1, i64 0), align 16, !tbaa !5
  store i32* %0, i32** getelementptr inbounds ([2 x [3 x [3 x i32*]]], [2 x [3 x [3 x i32*]]]* @a, i64 0, i64 1, i64 1, i64 1), align 8, !tbaa !5
  store i32 2, i32* @c, align 4, !tbaa !9
  store i32 2, i32* @b, align 4, !tbaa !9
  ret i32 0
}

And after:

@a.1 = internal unnamed_addr global [3 x [3 x i32*]] zeroinitializer, align 16

; Function Attrs: nofree norecurse nosync nounwind ssp uwtable
define i32 @main() local_unnamed_addr #0 {
entry:
  %b = alloca i32, align 4
  store i32 0, i32* %b, align 4
  %c = alloca i32, align 4
  store i32 0, i32* %c, align 4
  store i32* null, i32** getelementptr inbounds ([3 x [3 x i32*]], [3 x [3 x i32*]]* @a.1, i32 0, i64 0, i64 0), align 8, !tbaa !5
  store i32* null, i32** getelementptr inbounds ([3 x [3 x i32*]], [3 x [3 x i32*]]* @a.1, i32 0, i64 0, i64 1), align 16, !tbaa !5
  store i32* null, i32** getelementptr inbounds ([3 x [3 x i32*]], [3 x [3 x i32*]]* @a.1, i32 0, i64 1, i64 0), align 16, !tbaa !5
  store i32* null, i32** getelementptr inbounds ([3 x [3 x i32*]], [3 x [3 x i32*]]* @a.1, i32 0, i64 1, i64 1), align 8, !tbaa !5
  store i32 2, i32* %c, align 4, !tbaa !9
  store i32 2, i32* %b, align 4, !tbaa !9
  ret i32 0
}
Comment 3 Sanjay Patel 2021-05-15 06:09:48 PDT
https://reviews.llvm.org/D102552
Comment 4 Sanjay Patel 2021-05-20 09:36:40 PDT
Should be fixed with:
https://reviews.llvm.org/rGf34311c4024d
Comment 5 Sanjay Patel 2021-05-20 09:39:24 PDT
*** Bug 50253 has been marked as a duplicate of this bug. ***