cornell-zhang / hcl-dialect

HeteroCL-MLIR dialect for accelerator design
https://cornell-zhang.github.io/heterocl/index.html
Other
37 stars 15 forks source link

[LLVM][FixedToInt] Add support for `arith::SelectOp` (Fix #177) #178

Closed zzzDavid closed 1 year ago

zzzDavid commented 1 year ago

Summary

The fixed-to-integer pass did not have support for arith.select operation that has fixed-point type operands and results. This PR added supports for this case.

New test case

Added an IR test case here: hcl-dialect/test/Transforms/datatype/fixedpoint.mlir

  func.func @select_op(%arg0: memref<10x!hcl.Fixed<8, 4>>, %arg1: memref<10x!hcl.Fixed<8, 4>>) attributes {itypes = "__", otypes = ""} {
    affine.for %arg2 = 0 to 10 {
      %0 = affine.load %arg0[%arg2] {from = "tensor_0"} : memref<10x!hcl.Fixed<8, 4>>
      %c0_i32 = arith.constant 0 : i32
      %1 = hcl.fixed_to_fixed(%0) : !hcl.Fixed<8, 4> -> !hcl.Fixed<36, 4>
      %2 = hcl.int_to_fixed(%c0_i32) : i32 -> !hcl.Fixed<36, 4>
      %3 = hcl.cmp_fixed sgt, %1, %2 : !hcl.Fixed<36, 4>
      %4 = affine.load %arg0[%arg2] {from = "tensor_0"} : memref<10x!hcl.Fixed<8, 4>>
      %5 = hcl.int_to_fixed(%c0_i32) : i32 -> !hcl.Fixed<8, 4>
      %6 = arith.select %3, %4, %5 : !hcl.Fixed<8, 4> // CHECK: i8
      affine.store %6, %arg1[%arg2] {to = "tensor_1"} : memref<10x!hcl.Fixed<8, 4>>
    } {loop_name = "x", op_name = "tensor_1"}
    return
  }

This is a fixed-point ReLU operation. I will add the same test in frontend test suite.

zzzDavid commented 1 year ago

MLIR verifier would throw an error message, but I also added a validation step after each function is lowered to throw error for any unsupported operations just to provide more information