cornell-zhang / hcl-dialect

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

[LLVM] Semi-affine expressions (module by non-const) are not supported #53

Closed chhzh123 closed 2 years ago

chhzh123 commented 2 years ago

Not sure why the following program cannot be executed on LLVM backend. (See test case)

#map0 = affine_map<(d0)[s0] -> (d0 mod s0)>
#map1 = affine_map<(d0)[s0] -> (d0 floordiv s0)>
module {
  func @top(%arg0: memref<10x20x30xi32>) -> memref<10x20x30xi32> attributes {extra_itypes = "s", extra_otypes = "s"} {
    %0 = memref.alloc() {name = "B"} : memref<10x20x30xi32>
    %1 = memref.alloc() {name = "C"} : memref<10x20x30xi32>
    %c10 = arith.constant 10 : index
    %c20 = arith.constant 20 : index
    affine.for %arg1 = 0 to 200 {
      affine.for %arg2 = 0 to 30 {
        %2 = affine.apply #map0(%arg1)[%c20]
        %3 = affine.apply #map1(%arg1)[%c20]
        %4 = affine.load %arg0[%3, %2, %arg2] {from = "A"} : memref<10x20x30xi32>
        %c2_i32 = arith.constant 2 : i32
        %5 = arith.muli %4, %c2_i32 : i32
        affine.store %5, %0[%3, %2, %arg2] {to = "B"} : memref<10x20x30xi32>
        %6 = affine.load %0[%3, %2, %arg2] {from = "B"} : memref<10x20x30xi32>
        %c1_i32 = arith.constant 1 : i32
        %7 = arith.addi %6, %c1_i32 : i32
        affine.store %7, %1[%3, %2, %arg2] {to = "C"} : memref<10x20x30xi32>
      } {loop_name = "mm"}
    } {loop_name = "ii_jj_fused", stage_name = "C"}
    return %1 : memref<10x20x30xi32>
  }
}

It gives the following error, but %c20 is indeed a constant.

loc("-":11:14): error: semi-affine expressions (modulo by non-const) are not supported
loc("-":11:14): error: failed to legalize operation 'affine.apply'
loc("-":4:3): error: cannot be converted to LLVM IR: missing `LLVMTranslationDialectInterface` registration for dialect for op: builtin.func
chhzh123 commented 2 years ago

This is because the LLVM lowering pass does not support symbolic constant, so we need to change %2 = affine.apply #map0(%arg1)[%c20] into %2 = affine.apply #map0(%arg1) with #map0 = affine_map<(d0) -> (d0 mod 20)>.