Skip to content

Commit

Permalink
Fit new version of struct
Browse files Browse the repository at this point in the history
  • Loading branch information
mingzheTerapines committed Jul 31, 2024
1 parent 3b4ab12 commit ab3ed8f
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 12 deletions.
1 change: 1 addition & 0 deletions include/circt/Dialect/Moore/MooreOps.td
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,7 @@ def ReadOp : MooreOp<"read", [
let assemblyFormat = [{
$input attr-dict `:` type($input)
}];
let hasCanonicalizeMethod = true;
}

def AddressOp : MooreOp<"address", [
Expand Down
18 changes: 16 additions & 2 deletions lib/Dialect/Moore/MooreOps.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -909,11 +909,15 @@ LogicalResult BlockingAssignOp::canonicalize(BlockingAssignOp op,
if (auto refOp = op.getDst().getDefiningOp<moore::StructExtractRefOp>()) {
auto input = refOp.getInput();
if (isa<moore::SVModuleOp>(input.getDefiningOp()->getParentOp())) {
auto value = rewriter.create<ReadOp>(
op->getLoc(), cast<RefType>(input.getType()).getNestedType(), input);
auto newOp = rewriter.create<moore::StructInjectOp>(
op->getLoc(), input.getType(), input, refOp.getFieldNameAttr(),
op->getLoc(), value.getType(), value, refOp.getFieldNameAttr(),
op.getSrc());
auto newOpAddress = rewriter.create<moore::AddressOp>(
op.getLoc(), RefType::get(newOp.getType()), newOp);
rewriter.replaceOpUsesWithIf(
input.getDefiningOp(), newOp->getResults(),
input.getDefiningOp(), newOpAddress->getResults(),
[&op](OpOperand &operand) {
return !operand.getOwner()->isBeforeInBlock(op);
});
Expand Down Expand Up @@ -962,6 +966,16 @@ ReadOp::removeBlockingUses(const MemorySlot &slot,
return DeletionKind::Delete;
}

LogicalResult ReadOp::canonicalize(ReadOp op, PatternRewriter &rewriter) {
if (auto addr = op.getInput().getDefiningOp<AddressOp>()) {
auto value = addr.getInput();
op.replaceAllUsesWith(value);
op.erase();
return success();
}
return failure();
}

//===----------------------------------------------------------------------===//
// TableGen generated logic.
//===----------------------------------------------------------------------===//
Expand Down
17 changes: 7 additions & 10 deletions test/Dialect/Moore/canonicalizers.mlir
Original file line number Diff line number Diff line change
Expand Up @@ -70,20 +70,17 @@ func.func @StructInjectFold1(%arg0: !moore.struct<{a: i32, b: i32}>) -> (!moore.
}

// CHECK-LABEL: moore.module @structExtractRefLower2Inject
moore.module @structExtractRefLower2Inject(out a : !moore.ref<struct<{a: i32, b: i32}>>) {
moore.module @structExtractRefLower2Inject(out a : !moore.struct<{a: i32, b: i32}>) {
// CHECK: %0 = moore.constant 0 : i32
// CHECK: %1 = moore.constant 0 : i32
// CHECK: %2 = moore.struct_create %0, %1 : !moore.i32, !moore.i32 -> struct<{a: i32, b: i32}>
// CHECK: %3 = moore.address %2 : <struct<{a: i32, b: i32}>>
// CHECK: %4 = moore.constant 1 : i32
// CHECK: %5 = moore.struct_inject %3, "a", %4 : !moore.ref<struct<{a: i32, b: i32}>>
// CHECK: moore.output %5 : !moore.ref<struct<{a: i32, b: i32}>>
// CHECK: %1 = moore.constant 1 : i32
// CHECK: %2 = moore.struct_create %1, %0 : !moore.i32, !moore.i32 -> struct<{a: i32, b: i32}>
// CHECK: moore.output %2 : !moore.struct<{a: i32, b: i32}>
%ii = moore.variable : <struct<{a: i32, b: i32}>>
%0 = moore.struct_extract_ref %ii, "a" : <struct<{a: i32, b: i32}>> -> <i32>
%1 = moore.constant 1 : i32
%2 = moore.conversion %1 : !moore.i32 -> !moore.i32
moore.blocking_assign %0, %2 : i32
moore.output %ii : !moore.ref<struct<{a: i32, b: i32}>>
moore.blocking_assign %0, %1 : i32
%2 = moore.read %ii : <struct<{a: i32, b: i32}>>
moore.output %2 : !moore.struct<{a: i32, b: i32}>
}

// CHECK-LABEL: func.func @StructInjectFold2
Expand Down

0 comments on commit ab3ed8f

Please sign in to comment.