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

[Conversion] Refactor `fixed-to-integer` lowering pass #199

Closed zzzDavid closed 1 year ago

zzzDavid commented 1 year ago

Description

This PR refactors fixed-to-integer pass. I made three major changes:

  1. Removing the assumption of top-level function args being 64-bit.
  2. Removing the assumption that function arguments are all memref types.
  3. Removing the annotation process of fixed-point type info.

Housekeeping 🧹

New tests

Two new tests from #193 and #194 are added to test/Transforms/datatype/fixedpoint.mlir

Notes for future references

There was an annotation process where each fixed-point operation is tagged with attributes to record its fixed-point type info, such as width, frac, sign. This was necessary because we were converting the operations from top to bottom. For example, if I have:

%0 = hcl.add_fixed %arg0, %arg1: !hcl.Fixed<5, 3>
%1 = hcl.add_fixed %0, %arg2: !hcl.Fixed<5, 3>

By the time we visit the second operation, the first operation is already lowered to arith.addi, and %0 becomes i5, and we don't know the fixed point type info for %0 anymore, i.e., converting the defining operation result type "covers" its users. The annotation process solves this issue by tagging the fixed-point type info as attributes to these operations.

A better way to do this is to just transform the fixed-point operations in a reversed order in each block.

void visitBlock(Block &block) {
  for (auto it = block.rbegin(); it != block.rend(); ++it) {
    Operation &op = *it;
    visitOperation(op);
  }
}

This way, each operation conversion only updates the operation result, i.e. we update user operations first, and the upstream "defining" operations are not affected.