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

Miscompilation at O3 #100298

Closed
dtcxzyw opened this issue Jul 24, 2024 · 1 comment · Fixed by #100316
Closed

Miscompilation at O3 #100298

dtcxzyw opened this issue Jul 24, 2024 · 1 comment · Fixed by #100316

Comments

@dtcxzyw
Copy link
Member

dtcxzyw commented Jul 24, 2024

#include "csmith.h"
uint32_t a;
int16_t c;
void d() {
  for (a = -15; a >= 6; a = safe_add_func_uint16_t_u_u(a, 9))
    ;
  c = safe_lshift_func_int16_t_s_u(safe_lshift_func_uint16_t_u_s(a,14), 0);
}
int main() {
  d();
  printf("%d\n", c);
  return 0;
}
dtcxzyw@dtcxzyw:~/WorkSpace/Projects/compilers/LLVM/llvm-build$ gcc -O0 test.c -fsanitize=undefined -I/usr/include/csmith -w
dtcxzyw@dtcxzyw:~/WorkSpace/Projects/compilers/LLVM/llvm-build$ ./a.out 
-16384
dtcxzyw@dtcxzyw:~/WorkSpace/Projects/compilers/LLVM/llvm-build$ bin/clang -O3 test.c -I/usr/include/csmith -w
dtcxzyw@dtcxzyw:~/WorkSpace/Projects/compilers/LLVM/llvm-build$ ./a.out 
0

llvm version: 2d6ff0c

@dtcxzyw
Copy link
Member Author

dtcxzyw commented Jul 24, 2024

Reduced testcase: https://alive2.llvm.org/ce/z/9fExrA

; bin/opt -passes=instcombine test.ll -S
define i16 @src() {
entry:
  br label %for.inc

for.inc:
  %storemerge6 = phi i32 [ -15, %entry ], [ %conv1, %for.inc ]
  %add.i = add nsw i32 %storemerge6, 9
  %conv1 = and i32 %add.i, 65535
  %cmp = icmp ugt i32 %conv1, 5
  br i1 %cmp, label %for.inc, label %for.end

for.end:
  %conv2 = trunc nuw nsw i32 %conv1 to i16
  %cmp3.i = icmp ugt i32 %conv1, 3
  %0 = shl nuw i16 %conv2, 14
  %cond.in.i = select i1 %cmp3.i, i16 %conv2, i16 %0
  ret i16 %cond.in.i
}

dtcxzyw added a commit that referenced this issue Jul 24, 2024
…es (#100316)

See the following case:
```
define i16 @pr100298() {
entry:
  br label %for.inc

for.inc:
  %indvar = phi i32 [ -15, %entry ], [ %mask, %for.inc ]
  %add = add nsw i32 %indvar, 9
  %mask = and i32 %add, 65535
  %cmp1 = icmp ugt i32 %mask, 5
  br i1 %cmp1, label %for.inc, label %for.end

for.end:
  %conv = trunc i32 %add to i16
  %cmp2 = icmp ugt i32 %mask, 3
  %shl = shl nuw i16 %conv, 14
  %res = select i1 %cmp2, i16 %conv, i16 %shl
  ret i16 %res
}
```

When computing knownbits of `%shl` with `%cmp2=false`, we cannot use
this condition in the analysis of `%mask (%for.inc -> %for.inc)`.
 
Fixes #100298.
llvmbot pushed a commit to llvmbot/llvm-project that referenced this issue Jul 24, 2024
…es (llvm#100316)

See the following case:
```
define i16 @pr100298() {
entry:
  br label %for.inc

for.inc:
  %indvar = phi i32 [ -15, %entry ], [ %mask, %for.inc ]
  %add = add nsw i32 %indvar, 9
  %mask = and i32 %add, 65535
  %cmp1 = icmp ugt i32 %mask, 5
  br i1 %cmp1, label %for.inc, label %for.end

for.end:
  %conv = trunc i32 %add to i16
  %cmp2 = icmp ugt i32 %mask, 3
  %shl = shl nuw i16 %conv, 14
  %res = select i1 %cmp2, i16 %conv, i16 %shl
  ret i16 %res
}
```

When computing knownbits of `%shl` with `%cmp2=false`, we cannot use
this condition in the analysis of `%mask (%for.inc -> %for.inc)`.

Fixes llvm#100298.

(cherry picked from commit 59eae91)
tru pushed a commit to llvmbot/llvm-project that referenced this issue Jul 24, 2024
…es (llvm#100316)

See the following case:
```
define i16 @pr100298() {
entry:
  br label %for.inc

for.inc:
  %indvar = phi i32 [ -15, %entry ], [ %mask, %for.inc ]
  %add = add nsw i32 %indvar, 9
  %mask = and i32 %add, 65535
  %cmp1 = icmp ugt i32 %mask, 5
  br i1 %cmp1, label %for.inc, label %for.end

for.end:
  %conv = trunc i32 %add to i16
  %cmp2 = icmp ugt i32 %mask, 3
  %shl = shl nuw i16 %conv, 14
  %res = select i1 %cmp2, i16 %conv, i16 %shl
  ret i16 %res
}
```

When computing knownbits of `%shl` with `%cmp2=false`, we cannot use
this condition in the analysis of `%mask (%for.inc -> %for.inc)`.

Fixes llvm#100298.

(cherry picked from commit 59eae91)
yuxuanchen1997 pushed a commit that referenced this issue Jul 25, 2024
…es (#100316)

Summary:
See the following case:
```
define i16 @pr100298() {
entry:
  br label %for.inc

for.inc:
  %indvar = phi i32 [ -15, %entry ], [ %mask, %for.inc ]
  %add = add nsw i32 %indvar, 9
  %mask = and i32 %add, 65535
  %cmp1 = icmp ugt i32 %mask, 5
  br i1 %cmp1, label %for.inc, label %for.end

for.end:
  %conv = trunc i32 %add to i16
  %cmp2 = icmp ugt i32 %mask, 3
  %shl = shl nuw i16 %conv, 14
  %res = select i1 %cmp2, i16 %conv, i16 %shl
  ret i16 %res
}
```

When computing knownbits of `%shl` with `%cmp2=false`, we cannot use
this condition in the analysis of `%mask (%for.inc -> %for.inc)`.
 
Fixes #100298.

Test Plan: 

Reviewers: 

Subscribers: 

Tasks: 

Tags: 


Differential Revision: https://phabricator.intern.facebook.com/D60250567
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants