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

miscompilation of trivial but non-canonical arithmetic by AArch64 backend

Open regehr opened this issue 2 years ago • 3 comments

this IR:

define i32 @f(i8 %0) {
  %2 = add i8 %0, 0
  %3 = icmp ugt i8 %2, 1
  %4 = select i1 %3, i32 1, i32 0
  ret i32 %4
}

is currently getting lowered to aarch64 like this:

Johns-MacBook-Pro:build regehr$ ~/llvm-project/for-alive/bin/llc foo.ll -o -
	.section	__TEXT,__text,regular,pure_instructions
	.build_version macos, 14, 0
	.globl	_f                              ; -- Begin function f
	.p2align	2
_f:                                     ; @f
	.cfi_startproc
; %bb.0:
	mov	w0, wzr
	ret
	.cfi_endproc
                                        ; -- End function
.subsections_via_symbols
Johns-MacBook-Pro:build regehr$ 

of course this isn't correct -- for most values of %0, this function should return 1

regehr avatar Mar 11 '24 04:03 regehr

@llvm/issue-subscribers-backend-aarch64

Author: John Regehr (regehr)

this IR: ```llvm define i32 @f(i8 %0) { %2 = add i8 %0, 0 %3 = icmp ugt i8 %2, 1 %4 = select i1 %3, i32 1, i32 0 ret i32 %4 } ```

is currently getting lowered to aarch64 like this:

Johns-MacBook-Pro:build regehr$ ~/llvm-project/for-alive/bin/llc foo.ll -o -
	.section	__TEXT,__text,regular,pure_instructions
	.build_version macos, 14, 0
	.globl	_f                              ; -- Begin function f
	.p2align	2
_f:                                     ; @<!-- -->f
	.cfi_startproc
; %bb.0:
	mov	w0, wzr
	ret
	.cfi_endproc
                                        ; -- End function
.subsections_via_symbols
Johns-MacBook-Pro:build regehr$ 

of course this isn't correct -- for most values of %0, this function should return 1

llvmbot avatar Mar 11 '24 04:03 llvmbot

This is seen on other targets as well. Looks like TypePromotion is doing something incorrect. Bisected to #81690

*** IR Dump Before Type Promotion (type-promotion) ***
define i32 @f(i8 %0) {
  %2 = add i8 %0, 0
  %3 = icmp ugt i8 %2, 1
  %4 = select i1 %3, i32 1, i32 0
  ret i32 %4
}
*** IR Dump After Type Promotion (type-promotion) ***
define i32 @f(i8 %0) {
  %2 = zext i8 %0 to i32
  %3 = add i32 %2, 0
  %4 = icmp ugt i32 %3, -255
  %5 = select i1 %4, i32 1, i32 0
  ret i32 %5

}

cc: @topperc

svs-quic avatar Mar 11 '24 05:03 svs-quic

Closing manually, see above commit.

nikic avatar Mar 13 '24 15:03 nikic