Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Moore][Dedup] Dedup module op #7190

Draft
wants to merge 21 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions include/circt/Dialect/Moore/MoorePasses.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ namespace moore {
#include "circt/Dialect/Moore/MoorePasses.h.inc"

std::unique_ptr<mlir::Pass> createSimplifyProceduresPass();
std::unique_ptr<mlir::Pass> createDedupPass();

/// Generate the code for registering passes.
#define GEN_PASS_REGISTRATION
Expand Down
36 changes: 36 additions & 0 deletions include/circt/Dialect/Moore/MoorePasses.td
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,40 @@ def SimplifyProcedures : Pass<"moore-simplify-procedures", "moore::SVModuleOp">
let constructor = "circt::moore::createSimplifyProceduresPass()";
}

def Dedup : Pass<"moore-dedup", "mlir::ModuleOp"> {
let summary = "Deduplicate modules which are generated by same instance in SV";
let description = [{
This pass detects modules which are structurally equivalent and removes the
duplicate module by replacing all instances of one with the other.
This pass is only suitable for software simulation. Different symbolName
modules relate to different hardware units in hardware simulation. So this pass
is not recommended in hardware simulation.
For example,
```
module {
moore.module @top() {
%a = moore.net wire : <l4>
%0 = moore.read %a : l4
moore.instance "insA" @NestedA(a: %0: !moore.l4) -> ()
%1 = moore.read %a : l4
--- moore.instance "insB" @NestedA_0(a: %1: !moore.l4) -> ()
+++ moore.instance "insB" @NestedA(a: %1: !moore.l4) -> ()
moore.output
}
--- moore.module @NestedA_0(in %a : !moore.l4) {
--- %a_0 = moore.net name "a" wire : <l4>
--- moore.assign %a_0, %a : l4
--- moore.output
--- }
moore.module @NestedA(in %a : !moore.l4) {
%a_0 = moore.net name "a" wire : <l4>
moore.assign %a_0, %a : l4
moore.output
}
}
```
Comment on lines +40 to +63
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You are right. I may remove this.

}];
let constructor = "circt::moore::createDedupPass()";
}

#endif // CIRCT_DIALECT_MOORE_MOOREPASSES_TD
1 change: 1 addition & 0 deletions lib/Dialect/Moore/Transforms/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
add_circt_dialect_library(CIRCTMooreTransforms
SimplifyProcedures.cpp
Dedup.cpp


DEPENDS
Expand Down
87 changes: 87 additions & 0 deletions lib/Dialect/Moore/Transforms/Dedup.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
//===- Dedup.cpp - Moore module deduping --------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
//
// This file implements moore module deduplication.
//
//===----------------------------------------------------------------------===//

#include "circt/Dialect/Moore/MooreOps.h"
#include "circt/Dialect/Moore/MoorePasses.h"

namespace circt {
namespace moore {
#define GEN_PASS_DEF_DEDUP
#include "circt/Dialect/Moore/MoorePasses.h.inc"
} // namespace moore
} // namespace circt

using namespace circt;
using namespace moore;

namespace {
class DedupPass : public circt::moore::impl::DedupBase<DedupPass> {
// This table contains information to determine module module uniqueness.
using ModuleInfo = struct ModuleStruct {
SmallVector<Type> portTypes;
SmallVector<Type> inputTypes;
SmallVector<Type> outputTypes;
};
friend bool operator==(const ModuleInfo &lhs, const ModuleInfo &rhs) {
return lhs.portTypes == rhs.portTypes && lhs.inputTypes == rhs.inputTypes &&
lhs.outputTypes == rhs.outputTypes;
}

// This table records Op name and Op info
using ModuleInfoTable = DenseMap<mlir::StringAttr, ModuleInfo>;
ModuleInfoTable moduleInfoTable;

// This table records old module name and equiplance module name to update
using Symbol2Symbol = DenseMap<mlir::StringAttr, mlir::StringAttr>;
Symbol2Symbol replaceTable;

void runOnOperation() override;
};
} // namespace

std::unique_ptr<mlir::Pass> circt::moore::createDedupPass() {
return std::make_unique<DedupPass>();
}

void DedupPass::runOnOperation() {
// Do equiplance and record in replacTable
// Dedup already exist module op
getOperation()->walk([&](SVModuleOp moduleOp) {
// Define uniqueness
auto moduleName = moduleOp.getSymNameAttr();
auto moduleType = moduleOp.getModuleType();
ModuleInfo moduleInfo = {moduleType.getPortTypes(),
moduleType.getInputTypes(),
moduleType.getOutputTypes()};

// Compare and record to replacetable
// erase this op if there is a equiplance
for (const auto &existModuleInfo : moduleInfoTable) {
if (existModuleInfo.second == moduleInfo) {
moduleOp->erase();
replaceTable.insert({moduleName, existModuleInfo.first});
return WalkResult::advance();
}
}
moduleInfoTable[moduleName] = moduleInfo;
return WalkResult::advance();
});

// Referring to replacetable, replace instance's module name
getOperation()->walk([&](InstanceOp instanceOp) {
auto instanceName = instanceOp.getModuleNameAttr().getAttr();
if (replaceTable.lookup(instanceName)) {
instanceOp.setModuleName(replaceTable[instanceName]);
}
return WalkResult::advance();
});
}
31 changes: 31 additions & 0 deletions test/Dialect/Moore/dedup.mlir
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// RUN: circt-opt --moore-dedup %s | FileCheck %s

// CHECK-LABEL: moore.module @Foo()
moore.module @Foo() {
%a = moore.net wire : <l4>
%0 = moore.read %a : l4
// CHECK: moore.instance "insA" @NestedA
moore.instance "insA" @NestedA(a: %0: !moore.l4) -> ()
%1 = moore.read %a : l4
// CHECK: moore.instance "insB" @NestedA
moore.instance "insB" @NestedA_0(a: %1: !moore.l4) -> ()
moore.output
}
// CHECK: moore.module @NestedA
moore.module @NestedA(in %a : !moore.l4) {
%a_0 = moore.net name "a" wire : <l4>
moore.assign %a_0, %a : l4
moore.output
}
// CHECK-NOT: moore.module @NestedA_0
moore.module @NestedA_0(in %a : !moore.l4) {
%a_0 = moore.net name "a" wire : <l4>
moore.assign %a_0, %a : l4
moore.output
}
// CHECK-NOT: moore.module @NestedA_1
moore.module @NestedA_1(in %a : !moore.l4) {
%a_0 = moore.net name "a" wire : <l4>
moore.assign %a_0, %a : l4
moore.output
}
Loading