Skip to content

Commit

Permalink
Remove typeswitch
Browse files Browse the repository at this point in the history
Add func.func Dialect
Disable caonicalize function for local variable
  • Loading branch information
mingzheTerapines committed Aug 2, 2024
1 parent ab3ed8f commit db8f16b
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 49 deletions.
2 changes: 1 addition & 1 deletion include/circt/Dialect/Moore/MooreDialect.td
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ def MooreDialect : Dialect {
void printType(Type, DialectAsmPrinter &) const override;
}];
let useDefaultTypePrinterParser = 0;
let dependentDialects = ["hw::HWDialect"];
let dependentDialects = ["hw::HWDialect", "mlir::func::FuncDialect"];
}

#endif // CIRCT_DIALECT_MOORE_MOOREDIALECT
1 change: 1 addition & 0 deletions include/circt/Dialect/Moore/MooreOps.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
#include "circt/Dialect/HW/HWTypes.h"
#include "circt/Dialect/Moore/MooreDialect.h"
#include "circt/Dialect/Moore/MooreTypes.h"
#include "mlir/Dialect/Func/IR/FuncOps.h"
#include "mlir/IR/RegionKindInterface.h"
#include "mlir/Interfaces/ControlFlowInterfaces.h"
#include "mlir/Interfaces/InferTypeOpInterface.h"
Expand Down
1 change: 1 addition & 0 deletions lib/Dialect/Moore/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ add_circt_dialect_library(CIRCTMoore
CIRCTHW
CIRCTSupport
MLIRIR
MLIRFuncDialect
MLIRInferTypeOpInterface
MLIRMemorySlotInterfaces
)
Expand Down
1 change: 1 addition & 0 deletions lib/Dialect/Moore/MooreDialect.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

#include "circt/Dialect/HW/HWDialect.h"
#include "circt/Dialect/Moore/MooreOps.h"
#include "mlir/Dialect/Func/IR/FuncOps.h"

using namespace circt;
using namespace circt::moore;
Expand Down
107 changes: 59 additions & 48 deletions lib/Dialect/Moore/MooreOps.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ using namespace circt;
using namespace circt::moore;
using namespace mlir;

static ArrayRef<StructLikeMember> getStructMembers(Type type);

//===----------------------------------------------------------------------===//
// SVModuleOp
//===----------------------------------------------------------------------===//
Expand Down Expand Up @@ -289,54 +291,55 @@ VariableOp::handlePromotionComplete(const MemorySlot &slot, Value defaultValue,

LogicalResult VariableOp::canonicalize(VariableOp op,
::mlir::PatternRewriter &rewriter) {
if (!(isa<SVModuleOp>(op->getParentOp()) ||
isa<func::FuncOp>(op->getParentOp())))
return failure();
auto members = getStructMembers(op.getType().getNestedType());
if (!members.empty()) {
SmallVector<Value> createFields;
if (auto initial = op.getInitial()) {
auto addressOp = rewriter.create<AddressOp>(
op.getLoc(), RefType::get(cast<UnpackedType>(initial.getType())),
initial);
for (const auto &member : members) {
auto field = rewriter.create<StructExtractOp>(
op->getLoc(), cast<UnpackedType>(member.type), member.name,
addressOp);
createFields.push_back(field);
}
} else {
for (const auto &member : members) {
// todo: support 4-domain value
auto field = rewriter.create<ConstantOp>(op->getLoc(),
cast<IntType>(member.type), 0);
createFields.push_back(field);
}
}
auto value = rewriter.create<StructCreateOp>(
op->getLoc(), op.getType().getNestedType(), createFields);
rewriter.replaceOpWithNewOp<AddressOp>(op, RefType::get(value.getType()),
value);
return success();
}

return TypeSwitch<Type, LogicalResult>(op.getType().getNestedType())
.Case<StructType, UnpackedStructType>([&op, &rewriter](auto &type) {
SmallVector<Value> createFields;
if (auto initial = op.getInitial()) {
auto addressOp = rewriter.create<AddressOp>(
op.getLoc(), RefType::get(cast<UnpackedType>(initial.getType())),
initial);
for (const auto &member : type.getMembers()) {
auto field = rewriter.create<StructExtractOp>(
op->getLoc(), cast<UnpackedType>(member.type), member.name,
addressOp);
createFields.push_back(field);
}
} else {
for (const auto &member : type.getMembers()) {
// todo: support 4-domain value
auto field = rewriter.create<ConstantOp>(
op->getLoc(), cast<IntType>(member.type), 0);
createFields.push_back(field);
}
}
auto value = rewriter.create<StructCreateOp>(
op->getLoc(), op.getType().getNestedType(), createFields);
rewriter.replaceOpWithNewOp<AddressOp>(
op, RefType::get(value.getType()), value);
return success();
})
.Default([&op, &rewriter](auto &) {
Value initial;
for (auto *user : op->getUsers())
if (isa<ContinuousAssignOp>(user) &&
(user->getOperand(0) == op.getResult())) {
// Don't canonicalize the multiple continuous assignment to the same
// variable.
if (initial)
return failure();
initial = user->getOperand(1);
}

if (initial) {
rewriter.replaceOpWithNewOp<AssignedVarOp>(op, op.getType(),
op.getNameAttr(), initial);
return success();
}

Value initial;
for (auto *user : op->getUsers())
if (isa<ContinuousAssignOp>(user) &&
(user->getOperand(0) == op.getResult())) {
// Don't canonicalize the multiple continuous assignment to the same
// variable.
if (initial)
return failure();
});
initial = user->getOperand(1);
}

if (initial) {
rewriter.replaceOpWithNewOp<AssignedVarOp>(op, op.getType(),
op.getNameAttr(), initial);
return success();
}

return failure();
}

SmallVector<DestructurableMemorySlot> VariableOp::getDestructurableSlots() {
Expand Down Expand Up @@ -560,7 +563,6 @@ static ArrayRef<StructLikeMember> getStructMembers(Type type) {
return structType.getMembers();
if (auto structType = dyn_cast<UnpackedStructType>(type))
return structType.getMembers();
assert(0 && "expected StructType or UnpackedStructType");
return {};
}

Expand Down Expand Up @@ -708,6 +710,9 @@ OpFoldResult StructInjectOp::fold(FoldAdaptor adaptor) {

LogicalResult StructInjectOp::canonicalize(StructInjectOp op,
PatternRewriter &rewriter) {
if (!(isa<SVModuleOp>(op->getParentOp()) ||
isa<func::FuncOp>(op->getParentOp())))
return failure();
auto members = getStructMembers(op.getType());

// Chase a chain of `struct_inject` ops, with an optional final
Expand Down Expand Up @@ -906,9 +911,12 @@ DeletionKind BlockingAssignOp::removeBlockingUses(

LogicalResult BlockingAssignOp::canonicalize(BlockingAssignOp op,
PatternRewriter &rewriter) {
if (!(isa<SVModuleOp>(op->getParentOp()) ||
isa<func::FuncOp>(op->getParentOp())))
return failure();
if (auto refOp = op.getDst().getDefiningOp<moore::StructExtractRefOp>()) {
auto input = refOp.getInput();
if (isa<moore::SVModuleOp>(input.getDefiningOp()->getParentOp())) {
if (isa<SVModuleOp>(input.getDefiningOp()->getParentOp())) {
auto value = rewriter.create<ReadOp>(
op->getLoc(), cast<RefType>(input.getType()).getNestedType(), input);
auto newOp = rewriter.create<moore::StructInjectOp>(
Expand Down Expand Up @@ -967,6 +975,9 @@ ReadOp::removeBlockingUses(const MemorySlot &slot,
}

LogicalResult ReadOp::canonicalize(ReadOp op, PatternRewriter &rewriter) {
if (!(isa<SVModuleOp>(op->getParentOp()) ||
isa<func::FuncOp>(op->getParentOp())))
return failure();
if (auto addr = op.getInput().getDefiningOp<AddressOp>()) {
auto value = addr.getInput();
op.replaceAllUsesWith(value);
Expand Down

0 comments on commit db8f16b

Please sign in to comment.