Skip to content
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

WRONG code. FE:@llvm.lifetime.end? DeadStoreElim? #99887

Open
JonPsson1 opened this issue Jul 22, 2024 · 3 comments
Open

WRONG code. FE:@llvm.lifetime.end? DeadStoreElim? #99887

JonPsson1 opened this issue Jul 22, 2024 · 3 comments

Comments

@JonPsson1
Copy link
Contributor

This program:

int printf(const char *, ...);

long RES;
unsigned ****c;
unsigned *****volatile P5 = &c;
static unsigned ****P4;

unsigned **P2a, **P2b;

int main() {
  {
    unsigned ***P3 = &P2a;
    for (unsigned IV = -8; IV; IV = IV + 4) {
      unsigned ***i = &P2b;
      long *P = &RES;
      *P5 = &i;
      *P = ( (P4 = &P3) == *P5 );
    }
  }
  printf("%d\n", RES);
}

should print '0', which it does, except if compiled with -Os on SystemZ:

clang -march=z15 -Os wrong0.i -o a.out -w; ./a.out
1

If I remove the extra curly braces around the loop, 0 is printed also with -Os. I see from the front-end only one slight difference with the braces in a block:

for.end:                                          ; pred        for.end:                                          ; pred
                                                           >      call void @llvm.lifetime.end.p0(i64 8, ptr %P3) #3
  %6 = load i64, ptr @RES, align 8, !tbaa !10                     %6 = load i64, ptr @RES, align 8, !tbaa !10
  %call = call signext i32 (ptr, ...) @printf(ptr nounde          %call = call signext i32 (ptr, ...) @printf(ptr nounde
  call void @llvm.lifetime.end.p0(i64 8, ptr %P3) #3       <
  %7 = load i32, ptr %retval, align 4                             %7 = load i32, ptr %retval, align 4
  ret i32 %7                                                      ret i32 %7
}

Many passes later, the next change then is after DSE where a store now has been removed:

; *** IR Dump After DSEPass on main ***                         ; *** IR Dump After DSEPass on main ***
; Function Attrs: nounwind optsize                              ; Function Attrs: nounwind optsize
define dso_local signext i32 @main() local_unnamed_addr         define dso_local signext i32 @main() local_unnamed_addr 
entry:                                                          entry:
  %P3 = alloca ptr, align 8                                       %P3 = alloca ptr, align 8
  %i = alloca ptr, align 8                                        %i = alloca ptr, align 8
  call void @llvm.lifetime.start.p0(i64 8, ptr nonnull %          call void @llvm.lifetime.start.p0(i64 8, ptr nonnull %
  store ptr @P2a, ptr %P3, align 8, !tbaa !4               <
  br label %for.body                                              br label %for.body

@dtcxzyw
Copy link
Member

dtcxzyw commented Jul 22, 2024

IIRC it is a known issue :( As a workaround, you can compile it with -no-stack-coloring:

dtcxzyw@dtcxzyw:~/WorkSpace/Projects/compilers/LLVM/llvm-build$ clang -O0 tmp.c -w
dtcxzyw@dtcxzyw:~/WorkSpace/Projects/compilers/LLVM/llvm-build$ ./a.out 
0
dtcxzyw@dtcxzyw:~/WorkSpace/Projects/compilers/LLVM/llvm-build$ clang -Os tmp.c -w
dtcxzyw@dtcxzyw:~/WorkSpace/Projects/compilers/LLVM/llvm-build$ ./a.out 
1
dtcxzyw@dtcxzyw:~/WorkSpace/Projects/compilers/LLVM/llvm-build$ clang -Os tmp.c -w -mllvm -no-stack-coloring
dtcxzyw@dtcxzyw:~/WorkSpace/Projects/compilers/LLVM/llvm-build$ ./a.out 
0

@RSilicon
Copy link
Contributor

IIRC it is a known issue :( As a workaround, you can compile it with -no-stack-coloring:

dtcxzyw@dtcxzyw:~/WorkSpace/Projects/compilers/LLVM/llvm-build$ clang -O0 tmp.c -w
dtcxzyw@dtcxzyw:~/WorkSpace/Projects/compilers/LLVM/llvm-build$ ./a.out 
0
dtcxzyw@dtcxzyw:~/WorkSpace/Projects/compilers/LLVM/llvm-build$ clang -Os tmp.c -w
dtcxzyw@dtcxzyw:~/WorkSpace/Projects/compilers/LLVM/llvm-build$ ./a.out 
1
dtcxzyw@dtcxzyw:~/WorkSpace/Projects/compilers/LLVM/llvm-build$ clang -Os tmp.c -w -mllvm -no-stack-coloring
dtcxzyw@dtcxzyw:~/WorkSpace/Projects/compilers/LLVM/llvm-build$ ./a.out 
0

yeah, it's putting i and P3 at the same address
which they can't be

@dtcxzyw
Copy link
Member

dtcxzyw commented Jul 22, 2024

Related issue: #45725

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants