Skip to content

[MLIR][Arith] Invalid SelectOp folding #117554

Closed
@7FM

Description

@7FM

In CIRCT, we have a dialect named hwarith to represent arithmetics that persist signedness information and prevent unintentional under- and overflows.
Using this dialect in combination with arith.select revealed an issue. Take the following example code:

func.func @test(%sel : i1) -> ui1 {
  %trueVal = hwarith.constant 1 : ui1
  %falseVal = hwarith.constant 0 : ui1
  %res = arith.select %sel, %trueVal, %falseVal : ui1
  func.return %res : ui1
}

The SelectOp's folder tries to replace the SelectOp directly with %sel. However, %sel is of type i1 whereas the true- and falseVal values are of type ui1. Consequently, the following crash is caused: error: 'arith.select' op folder produced a value of incorrect type: 'i1', expected: 'ui1'.

The corresponding pattern matching code has a bit-width check for the return type but lacks a signedness check and thus implicitly assumes that the return type is signless (i1):

if (getType().isInteger(1) && matchPattern(adaptor.getTrueValue(), m_One()) &&
matchPattern(adaptor.getFalseValue(), m_Zero()))
return condition;

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugIndicates an unexpected problem or unintended behaviormlir:arith

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions