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

[Datatype] FixedType unable to represent global memref with value #15

Closed zzzDavid closed 2 years ago

zzzDavid commented 2 years ago

In MLIR code we can have constant global memrefs to store input data for testing purpose. For example, in mm.mlir testing example: https://github.com/cornell-zhang/hcl-dialect-prototype/blob/main/test/Translation/mm.mlir, we use memref.global to store input data, which is fed to the kernel function during global

We have defined our fixed and ufixed types, they work with memref and our custom fixed-point arithmetic operations. However, it doesn't work when I try to initialize a global memref with values:

module {
  memref.global "private" @gv0 : memref<4x4x!hcl.Fixed<32, 2>> = dense<[[1.0, 2.0, 3.0, 4.0], [1.0, 2.0, 3.0, 4.0], [1.0, 2.0, 3.0, 4.0], [1.0, 2.0, 3.0, 4.0]]>

  func @top(%arg0: memref<10x!hcl.Fixed<32, 2>>, %arg1: memref<10x!hcl.Fixed<32, 2>>) -> memref<10x!hcl.Fixed<32, 2>> {
    %0 = memref.alloc() {name = "compute_2"} : memref<10x!hcl.Fixed<32, 2>>
    affine.for %arg2 = 0 to 10 {
      %1 = affine.load %arg0[%arg2] {from = "compute_0"} : memref<10x!hcl.Fixed<32, 2>>
      %2 = affine.load %arg1[%arg2] {from = "compute_1"} : memref<10x!hcl.Fixed<32, 2>>
      %3 = "hcl.add_fixed"(%1, %2) : (!hcl.Fixed<32, 2>, !hcl.Fixed<32, 2>) -> !hcl.Fixed<32, 2>
      affine.store %3, %0[%arg2] {to = "compute_2"} : memref<10x!hcl.Fixed<32, 2>>
    } {loop_name = "x", stage_name = "compute_2"}
    return %0 : memref<10x!hcl.Fixed<32, 2>>
  }
}

The error message is:

hcl-opt: /home/nz264/shared/llvm-project-14.0.0.rc1/mlir/lib/Parser/Token.cpp:82: std::string mlir::Token::getStringValue() const: Assertion `getKind() == string || (getKind() == at_identifier && getSpelling()[1] == '"')' failed.
Aborted

This is not necessarily an issue, as we can use integer memrefs to test the kernel function. I'm just curious what we need to add in order to make this kind of global memref work with custom types.

chhzh123 commented 2 years ago

I think you need to add some interface like MemRefElementTypeInterface that enables the FixedTypes to be passed in memref.

zzzDavid commented 2 years ago

Constant global memref for fixed-point type is supported in #33, therefore, closing this issue.