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

miscompile due to instcombine #53252

Closed
regehr opened this issue Jan 17, 2022 · 3 comments
Closed

miscompile due to instcombine #53252

regehr opened this issue Jan 17, 2022 · 3 comments

Comments

@regehr
Copy link
Contributor

regehr commented Jan 17, 2022

https://alive2.llvm.org/ce/z/w-iYiR

so if we compile this function:

define i32 @t1_ult_slt_0(i32 %x, i32 %replacement_low, i32 %replacement_high) {
  %t0 = icmp slt i32 %x, 0
  %t1 = select i1 %t0, i32 %replacement_low, i32 %replacement_high
  %t2 = icmp ult i32 %x, 65536
  %1 = xor i1 %t2, true
  %r = select i1 %1, i32 %x, i32 %t1
  ret i32 %r
}

alongside this test driver:

#include <stdio.h>

unsigned t1_ult_slt_0(unsigned a, unsigned b, unsigned c);

int main(void) {
  printf("%u\n", t1_ult_slt_0(2, 0, 1));
}

the output is 1

optimizing the function using instcombine we get this:

regehr@john-home:~/Downloads$ opt -instcombine foo.ll -S -o -
; ModuleID = 'foo.ll'
source_filename = "foo.ll"

define i32 @t1_ult_slt_0(i32 %x, i32 %replacement_low, i32 %replacement_high) {
  %1 = icmp slt i32 %x, 0
  %2 = icmp sgt i32 %x, 65535
  %3 = select i1 %1, i32 %replacement_low, i32 %x
  %4 = select i1 %2, i32 %replacement_high, i32 %3
  ret i32 %4
}
regehr@john-home:~/Downloads$ 

compiling the optimized function, using the same test driver, results in 2 being printed

found by @Hatsunespica's test generater + Alive2
cc @nunoplopes

@nunoplopes
Copy link
Member

cc @rotateright @nikic @LebedevRI

@rickyz
Copy link
Member

rickyz commented Feb 14, 2022

Sent out https://reviews.llvm.org/D119690 to fix this.

rotateright pushed a commit that referenced this issue Apr 26, 2022
This are baseline tests for D119690 ( #53252 )

Differential Revision: https://reviews.llvm.org/D119689
@fhahn
Copy link
Contributor

fhahn commented Apr 28, 2022

Thanks!

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

5 participants