define i8 @known_nonneg_maybe_zero_sgt(i8 %inl, i8 %y) {
%mask = and i8 %y, 127
%or = or i8 %inl, %mask
%cmp = icmp sgt i8 %inl, -1
%s = select i1 %cmp, i8 1, i8 %or
ret i8 %s
}
; ModuleID = '/data/tmp/tmplev036be/orig.ll'
source_filename = "/data/tmp/tmplev036be/orig.ll"
define i8 @known_nonneg_maybe_zero_sgt(i8 %inl, i8 %y) {
%mask = and i8 %y, 127
%or = or i8 %inl, %mask
%s = call i8 @llvm.smin.i8(i8 %or, i8 1)
ret i8 %s
}
; Function Attrs: nocallback nocreateundeforpoison nofree nosync nounwind speculatable willreturn memory(none)
declare i8 @llvm.smin.i8(i8, i8) #0
attributes #0 = { nocallback nocreateundeforpoison nofree nosync nounwind speculatable willreturn memory(none) }
----------------------------------------
define i8 @known_nonneg_maybe_zero_sgt(i8 %inl, i8 %y) {
#0:
%mask = and i8 %y, 127
%or = or i8 %inl, %mask
%cmp = icmp sgt i8 %inl, 255
%s = select i1 %cmp, i8 1, i8 %or
ret i8 %s
}
=>
define i8 @known_nonneg_maybe_zero_sgt(i8 %inl, i8 %y) {
#0:
%mask = and i8 %y, 127
%or = or i8 %inl, %mask
%s = smin i8 %or, 1
ret i8 %s
}
Transformation doesn't verify!
ERROR: Target is more poisonous than source
Example:
i8 %inl = #x00 (0)
i8 %y = poison
Source:
i8 %mask = poison
i8 %or = poison
i1 %cmp = #x1 (1)
i8 %s = #x01 (1)
Target:
i8 %mask = poison
i8 %or = poison
i8 %s = poison
Source value: #x01 (1)
Target value: poison
Summary:
0 correct transformations
1 incorrect transformations
0 failed-to-prove transformations
0 Alive2 errorsThe difference is real: program exit code is the returned i8. Original returns 1 for arguments (0,0); optimized returns 0 after folding to smin(or,1). This is a defined input, not relying on poison.
define i8 @known_nonneg_maybe_zero_sgt(i8 %inl, i8 %y) {
%mask = and i8 %y, 127
%or = or i8 %inl, %mask
%cmp = icmp sgt i8 %inl, -1
%s = select i1 %cmp, i8 1, i8 %or
ret i8 %s
}
define i8 @main(i32 %argc, ptr %argv) {
entry:
%r = call i8 @known_nonneg_maybe_zero_sgt(i8 0, i8 0)
ret i8 %r
}
; ModuleID = '/data/tmp/tmpvswnjkuv/orig.ll'
source_filename = "/data/tmp/tmpvswnjkuv/orig.ll"
define i8 @known_nonneg_maybe_zero_sgt(i8 %inl, i8 %y) {
%mask = and i8 %y, 127
%or = or i8 %inl, %mask
%s = call i8 @llvm.smin.i8(i8 %or, i8 1)
ret i8 %s
}
; Function Attrs: nocallback nocreateundeforpoison nofree nosync nounwind speculatable willreturn memory(none)
declare i8 @llvm.smin.i8(i8, i8) #0
attributes #0 = { nocallback nocreateundeforpoison nofree nosync nounwind speculatable willreturn memory(none) }
define i8 @main(i32 %argc, ptr %argv) {
entry:
%r = call i8 @known_nonneg_maybe_zero_sgt(i8 0, i8 0)
ret i8 %r
}
Confirmed as bug by agent.
Original Output: {'timed_out': False, 'return_code': 1, 'stdout': '', 'stderr': '', 'executor': 'llubi'}
Transformed Output: {'timed_out': False, 'return_code': 0, 'stdout': '', 'stderr': '', 'executor': 'llubi'}
define <2 x i8> @vector_const_one(<2 x i8> %a, <2 x i8> %y) {
%mask = and <2 x i8> %y, <i8 127, i8 127>
%or = or <2 x i8> %a, %mask
%cmp = icmp sge <2 x i8> %a, zeroinitializer
%s = select <2 x i1> %cmp, <2 x i8> <i8 1, i8 1>, <2 x i8> %or
ret <2 x i8> %s
}
; ModuleID = '/data/tmp/tmpybm38bcw/orig.ll'
source_filename = "/data/tmp/tmpybm38bcw/orig.ll"
define <2 x i8> @vector_const_one(<2 x i8> %a, <2 x i8> %y) {
%mask = and <2 x i8> %y, splat (i8 127)
%or = or <2 x i8> %a, %mask
%s = call <2 x i8> @llvm.smin.v2i8(<2 x i8> %or, <2 x i8> splat (i8 1))
ret <2 x i8> %s
}
; Function Attrs: nocallback nocreateundeforpoison nofree nosync nounwind speculatable willreturn memory(none)
declare <2 x i8> @llvm.smin.v2i8(<2 x i8>, <2 x i8>) #0
attributes #0 = { nocallback nocreateundeforpoison nofree nosync nounwind speculatable willreturn memory(none) }
----------------------------------------
define <2 x i8> @vector_const_one(<2 x i8> %a, <2 x i8> %y) {
#0:
%mask = and <2 x i8> %y, { 127, 127 }
%or = or <2 x i8> %a, %mask
%cmp = icmp sge <2 x i8> %a, { 0, 0 }
%s = select <2 x i1> %cmp, <2 x i8> { 1, 1 }, <2 x i8> %or
ret <2 x i8> %s
}
=>
define <2 x i8> @vector_const_one(<2 x i8> %a, <2 x i8> %y) {
#0:
%mask = and <2 x i8> %y, { 127, 127 }
%or = or <2 x i8> %a, %mask
%s = smin <2 x i8> %or, { 1, 1 }
ret <2 x i8> %s
}
Transformation doesn't verify!
ERROR: Target is more poisonous than source
Example:
<2 x i8> %a = < #x00 (0), #x00 (0) >
<2 x i8> %y = < poison, #x03 (3) >
Source:
<2 x i8> %mask = < poison, #x03 (3) >
<2 x i8> %or = < poison, #x03 (3) >
<2 x i1> %cmp = < #x1 (1), #x1 (1) >
<2 x i8> %s = < #x01 (1), #x01 (1) >
Target:
<2 x i8> %mask = < poison, #x03 (3) >
<2 x i8> %or = < poison, #x03 (3) >
<2 x i8> %s = < poison, #x01 (1) >
Source value: < #x01 (1), #x01 (1) >
Target value: < poison, #x01 (1) >
Summary:
0 correct transformations
1 incorrect transformations
0 failed-to-prove transformations
0 Alive2 errorsdefine i8 @or_disjoint_suppressed(i8 %a) {
%or = or disjoint i8 %a, 1
%cmp = icmp sgt i8 %a, -1
%s = select i1 %cmp, i8 0, i8 %or
ret i8 %s
}
; ModuleID = '/data/tmp/tmpnyyc0_jr/orig.ll'
source_filename = "/data/tmp/tmpnyyc0_jr/orig.ll"
define i8 @or_disjoint_suppressed(i8 %a) {
%or = or disjoint i8 %a, 1
%s = call i8 @llvm.smin.i8(i8 %or, i8 0)
ret i8 %s
}
; Function Attrs: nocallback nocreateundeforpoison nofree nosync nounwind speculatable willreturn memory(none)
declare i8 @llvm.smin.i8(i8, i8) #0
attributes #0 = { nocallback nocreateundeforpoison nofree nosync nounwind speculatable willreturn memory(none) }
----------------------------------------
define i8 @or_disjoint_suppressed(i8 %a) {
#0:
%or = or disjoint i8 %a, 1
%cmp = icmp sgt i8 %a, 255
%s = select i1 %cmp, i8 0, i8 %or
ret i8 %s
}
=>
define i8 @or_disjoint_suppressed(i8 %a) {
#0:
%or = or disjoint i8 %a, 1
%s = smin i8 %or, 0
ret i8 %s
}
Transformation doesn't verify!
ERROR: Target is more poisonous than source
Example:
i8 %a = #x01 (1)
Source:
i8 %or = poison
i1 %cmp = #x1 (1)
i8 %s = #x00 (0)
Target:
i8 %or = poison
i8 %s = poison
Source value: #x00 (0)
Target value: poison
Summary:
0 correct transformations
1 incorrect transformations
0 failed-to-prove transformations
0 Alive2 errorsBug Triggering Analysis:
The new InstCombine fold matches this pattern because the predicate is icmp sgt %inl, -1, the selected non-constant arm is %or = or %inl, %mask, the compared value %inl is operand 0 of the OR, the constant arm is 1, and %mask = and %y, 127 is known nonnegative by known-bits analysis.
InstCombine rewrites the function to:
%mask = and i8 %y, 127
%or = or i8 %inl, %mask
%s = call i8 @llvm.smin.i8(i8 %or, i8 1)
ret i8 %s
This is not equivalent. For the fully defined input %inl = 0, %y = 0, the source computes %mask = 0, %or = 0, %cmp = true, and the select returns 1. The optimized form computes smin(0, 1) = 0. The difftest confirmed this concrete execution difference: original returned exit code 1, transformed returned exit code 0.
Alive2 also reports the transformation as incorrect. Its counterexample uses poison %y, showing an additional poison-suppression problem: when %cmp is true, the source select suppresses poison from the %or arm and returns the defined constant 1, while the transformed smin unconditionally consumes %or and becomes poison.
Fix Weakness Analysis:
The patch only checks isKnownNonNegative(Mask, SQ). That precondition is sufficient for the constant arm 0: when %inl is nonnegative, %inl | Mask is nonnegative, so smin(%or, 0) equals 0. However, it is insufficient for the constant arm 1: equivalence requires %inl | Mask >= 1 whenever the constant arm is selected. The current code does not prove that. A known-nonnegative mask may still be zero, and %inl may also be zero on the nonnegative side of the comparison, so %or may be 0.
The weakness is not limited to this scalar case. The same issue appears for vector lanes, and a separate Alive2 test showed that or disjoint can introduce another unsoundness by making a poison-generating OR unconditionally used by smin even when the original select chose the constant arm. The minimal concrete defined-value bug above is sufficient to demonstrate that the fix is over-broad for ConstVal == 1.