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

Miscompile: select-to-and transformation exposes poison from trunc nsw to i1

Open AIC33145 opened this issue 3 months ago • 3 comments

Description

We observed a semantic difference after optimization when a select is transformed into an and.

The issue involves trunc nsw with an i1 destination. For the input %c = false and %x = 1, the original IR returns false, while the optimized IR produces poison https://godbolt.org/z/b5cfTsYaz .

Original IR

define i1 @test_logical_and_trunc_nuw(
    i1 %c,
    i8 noundef range(i8 0, 2) %x) {
  %trunc = trunc nsw i8 %x to i1
  %result = select i1 %c, i1 %trunc, i1 false
  ret i1 %result
}

Optimized IR

define i1 @test_logical_and_trunc_nuw(
    i1 %c,
    i8 noundef range(i8 0, 2) %x) local_unnamed_addr {
  %trunc = trunc nuw nsw i8 %x to i1
  %result = and i1 %c, %trunc
  ret i1 %result
}

The addition of nuw is valid for the parameter range and does not appear to be the direct cause of the problem. The semantic difference comes from replacing select with and.

Problematic input

%c = false
%x = 1

For this input:

%trunc = trunc nsw i8 1 to i1

produces poison.

The retained bit is 1, while all seven truncated bits are 0. Therefore, the truncated bits are not equal to the top bit of the truncation result, which violates the nsw condition.

In the original IR:

select i1 false, i1 poison, i1 false

returns false, because select does not propagate poison from its non-selected operand.

In the optimized IR:

and i1 false, i1 poison

produces poison, because and propagates poison from its operands.

Therefore:

Original:  false
Optimized: poison

This appears to violate refinement and makes the select-to-and transformation unsound when the selected value may be poison.

LangRef question

We checked the LLVM Language Reference for trunc. It explicitly permits:

%r = trunc nsw <ty> %value to <ty2>
%r = trunc nuw nsw <ty> %value to <ty2>

The type constraints only require integer types and a source type wider than the destination type. We could not find any restriction that prohibits nsw when the destination type is i1.

The current LangRef states:

If the nsw keyword is present, and any of the truncated bits are not the same as the top bit of the truncation result, the result is a poison value.

Under this wording, trunc nsw i8 1 to i1 is a valid instruction whose result is poison.

Could you clarify the intended resolution?

Based on the current LangRef wording, this looks like an optimizer bug rather than invalid input IR.

If nsw is not intended to be supported for truncation to i1, should the LangRef and verifier be updated to explicitly reject or restrict it?

AIC33145 avatar Sep 02 '26 05:09 AIC33145

Hi, I'd like to take this issue!

Chennesxu avatar Sep 02 '26 07:09 Chennesxu

Bisected to #198178; CC @andjo403 @nikic

efriedma-quic avatar Sep 02 '26 18:09 efriedma-quic

nice catch yes there is missing a check that nsw is not present in https://github.com/llvm/llvm-project/pull/198178 and alive agree https://alive2.llvm.org/ce/z/-uWmKd

andjo403 avatar Sep 02 '26 20:09 andjo403