-
Notifications
You must be signed in to change notification settings - Fork 305
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
mingzheTerapines
wants to merge
21
commits into
llvm:main
Choose a base branch
from
Terapines:mingzhe-PassDedup
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
21 commits
Select commit
Hold shift + click to select a range
5d532d8
Infra
mingzheTerapines eac9de4
[Moore][Deuplicate] Dedup module op
mingzheTerapines 9121116
Debug
mingzheTerapines 379a371
Infra
mingzheTerapines 5180932
Merge branch 'llvm:main' into mingzhe-PassDedup
mingzheTerapines ef38e04
Complete
mingzheTerapines 8b028d3
Remove symName compare
mingzheTerapines 1e6295f
Fix some bugs
mingzheTerapines f3d73ba
Merge branch 'llvm:main' into mingzhe-PassDedup
mingzheTerapines bff113c
clang-tidy
mingzheTerapines d892267
Dedup pass test
mingzheTerapines 85a0a59
Fix operation type selection: svmoduleop -> builtinOP
mingzheTerapines 2fdc7fe
Add annotation
mingzheTerapines aeabbb4
Fix clang
mingzheTerapines bb81cf9
Add annotation
mingzheTerapines 8a03a9d
Remove useless include
mingzheTerapines 15a11c2
Merge branch 'llvm:main' into mingzhe-PassDedup
mingzheTerapines fdb03ae
Merge branch 'llvm:main' into mingzhe-PassDedup
mingzheTerapines 88afafa
Merge branch 'llvm:main' into mingzhe-PassDedup
mingzheTerapines 32ec719
Merge branch 'llvm:main' into mingzhe-PassDedup
mingzheTerapines da9635a
Merge branch 'llvm:main' into mingzhe-PassDedup
mingzheTerapines File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
add_circt_dialect_library(CIRCTMooreTransforms | ||
SimplifyProcedures.cpp | ||
Dedup.cpp | ||
|
||
|
||
DEPENDS | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.