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

[ImportVerilog]Dedup module Op #7245

Merged
merged 26 commits into from
Jul 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
46b542b
[ImportVerilog]Dedup module Op
mingzheTerapines Jun 27, 2024
799c179
Merge branch 'llvm:main' into mingzhe-DedupOpt
mingzheTerapines Jun 28, 2024
5e31646
Merge branch 'llvm:main' into mingzhe-DedupOpt
mingzheTerapines Jul 3, 2024
eb3440d
Avoid duplicate mapping
mingzheTerapines Jul 3, 2024
ce1cd2a
clang-tidy
mingzheTerapines Jul 3, 2024
30bf53c
Merge branch 'llvm:main' into mingzhe-DedupOpt
mingzheTerapines Jul 4, 2024
c2bd781
Merge branch 'llvm:main' into mingzhe-DedupOpt
mingzheTerapines Jul 8, 2024
e833e47
Merge branch 'llvm:main' into mingzhe-DedupOpt
mingzheTerapines Jul 9, 2024
86f2a00
Merge remote-tracking branch 'upstream/main' into mingzhe-DedupOpt
mingzheTerapines Jul 10, 2024
3624ef7
Fix tests
mingzheTerapines Jul 10, 2024
47a5f64
Fix tests
mingzheTerapines Jul 10, 2024
ffa0759
Merge branch 'mingzhe-DedupOpt' of https://github.com/Terapines/circt…
mingzheTerapines Jul 10, 2024
8b83371
populate the portsBySyntaxNode once initially when lowering the module
mingzheTerapines Jul 10, 2024
f50714e
populate the portsBySyntaxNode once initially when lowering the module
mingzheTerapines Jul 10, 2024
6b0d974
Merge branch 'mingzhe-DedupOpt' of https://github.com/Terapines/circt…
mingzheTerapines Jul 10, 2024
cef04c7
Merge branch 'llvm:main' into mingzhe-DedupOpt
mingzheTerapines Jul 12, 2024
6ce87ea
Merge branch 'llvm:main' into mingzhe-DedupOpt
mingzheTerapines Jul 15, 2024
0ef43fc
Update lib/Conversion/ImportVerilog/Structure.cpp
mingzheTerapines Jul 15, 2024
d6959a9
Imporve
mingzheTerapines Jul 15, 2024
4288cdb
Merge branch 'llvm:main' into mingzhe-DedupOpt
mingzheTerapines Jul 16, 2024
058b88f
Imporve logic
mingzheTerapines Jul 16, 2024
591c609
Imporve logic
mingzheTerapines Jul 16, 2024
7638e39
Merge branch 'mingzhe-DedupOpt' of https://github.com/Terapines/circt…
mingzheTerapines Jul 16, 2024
b26888f
Merge branch 'llvm:main' into mingzhe-DedupOpt
mingzheTerapines Jul 17, 2024
0c08a97
Merge branch 'llvm:main' into mingzhe-DedupOpt
mingzheTerapines Jul 18, 2024
5c7d0c1
Set test unnested
mingzheTerapines Jul 18, 2024
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
2 changes: 2 additions & 0 deletions lib/Conversion/ImportVerilog/ImportVerilogInternals.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ struct PortLowering {
struct ModuleLowering {
moore::SVModuleOp op;
SmallVector<PortLowering> ports;
DenseMap<const slang::syntax::SyntaxNode *, const slang::ast::PortSymbol *>
portsBySyntaxNode;
};

/// A helper class to facilitate the conversion from a Slang AST to MLIR
Expand Down
64 changes: 64 additions & 0 deletions lib/Conversion/ImportVerilog/Structure.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,10 @@ struct MemberVisitor {
// connection for the port.
if (!expr) {
auto *port = con->port.as_if<PortSymbol>();
if (auto *existingPort =
moduleLowering->portsBySyntaxNode.lookup(port->getSyntax()))
port = existingPort;

switch (port->direction) {
case slang::ast::ArgumentDirection::In: {
auto refType = moore::RefType::get(
Expand Down Expand Up @@ -191,6 +195,9 @@ struct MemberVisitor {
: context.convertLvalueExpression(*expr);
if (!value)
return failure();
if (auto *existingPort =
moduleLowering->portsBySyntaxNode.lookup(con->port.getSyntax()))
port = existingPort;
portValues.insert({port, value});
continue;
}
Expand All @@ -206,6 +213,9 @@ struct MemberVisitor {
unsigned offset = 0;
auto i32 = moore::IntType::getInt(context.getContext(), 32);
for (const auto *port : llvm::reverse(multiPort->ports)) {
if (auto *existingPort = moduleLowering->portsBySyntaxNode.lookup(
con->port.getSyntax()))
port = existingPort;
unsigned width = port->getType().getBitWidth();
auto index = builder.create<moore::ConstantOp>(loc, i32, offset);
auto sliceType = context.convertType(port->getType());
Expand Down Expand Up @@ -470,7 +480,56 @@ ModuleLowering *
Context::convertModuleHeader(const slang::ast::InstanceBodySymbol *module) {
using slang::ast::ArgumentDirection;
using slang::ast::MultiPortSymbol;
using slang::ast::ParameterSymbol;
using slang::ast::PortSymbol;
using slang::ast::TypeParameterSymbol;

auto parameters = module->parameters;
bool hasModuleSame = false;
// If there is already exist a module that has the same name with this
// module ,has the same parent scope and has the same parameters we can
// define this module is a duplicate module
for (auto const &existingModule : modules) {
if (module->getDeclaringDefinition() ==
existingModule.getFirst()->getDeclaringDefinition()) {
auto moduleParameters = existingModule.getFirst()->parameters;
hasModuleSame = true;
for (auto it1 = parameters.begin(), it2 = moduleParameters.begin();
it1 != parameters.end() && it2 != moduleParameters.end();
it1++, it2++) {
// Parameters size different
if (it1 == parameters.end() || it2 == moduleParameters.end()) {
hasModuleSame = false;
break;
}
const auto *para1 = (*it1)->symbol.as_if<ParameterSymbol>();
const auto *para2 = (*it2)->symbol.as_if<ParameterSymbol>();
// Parameters kind different
if ((para1 == nullptr) ^ (para2 == nullptr)) {
hasModuleSame = false;
break;
}
// Compare ParameterSymbol
if (para1 != nullptr) {
hasModuleSame = para1->getValue() == para2->getValue();
}
// Compare TypeParameterSymbol
if (para1 == nullptr) {
auto para1Type = convertType(
(*it1)->symbol.as<TypeParameterSymbol>().getTypeAlias());
auto para2Type = convertType(
(*it2)->symbol.as<TypeParameterSymbol>().getTypeAlias());
hasModuleSame = para1Type == para2Type;
}
if (!hasModuleSame)
break;
}
if (hasModuleSame) {
module = existingModule.first;
break;
}
}
}

auto &slot = modules[module];
if (slot)
Expand Down Expand Up @@ -553,6 +612,11 @@ Context::convertModuleHeader(const slang::ast::InstanceBodySymbol *module) {

// Schedule the body to be lowered.
moduleWorklist.push(module);

// Map duplicate port by Syntax
for (const auto &port : lowering.ports)
lowering.portsBySyntaxNode.insert({port.ast.getSyntax(), &port.ast});

return &lowering;
}

Expand Down
32 changes: 32 additions & 0 deletions test/Conversion/ImportVerilog/basic.sv
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,38 @@ module Empty;
; // empty member
endmodule


module DedupA(input wire a,
input wire b,
output wire [3:0] c);
endmodule

module DedupB #(parameter p = 32)
(input wire a,
input wire b,
output wire [3:0] c);
endmodule

// CHECK-LABEL: moore.module private @DedupA(in %a : !moore.l1, in %b : !moore.l1, out c : !moore.l4) {
// CHECK-LABEL: moore.module private @DedupB(in %a : !moore.l1, in %b : !moore.l1, out c : !moore.l4) {
// CHECK-LABEL: moore.module private @DedupB_0(in %a : !moore.l1, in %b : !moore.l1, out c : !moore.l4) {
// CHECK-LABEL: moore.module @Dedup
module Dedup;
wire [3:0] a;
wire [3:0] b;
wire [3:0] c;
// CHECK-LABEL: moore.instance "insA" @DedupA
DedupA insA(.a(a), .c(c));
// CHECK-LABEL: moore.instance "insB" @DedupA
DedupA insB(.b(b), .c(c));
// CHECK-LABEL: moore.instance "insC" @DedupB
DedupB insC(.c(c));
// CHECK-LABEL: moore.instance "insD" @DedupB
DedupB insD(.a(a), .b(b), .c(c));
// CHECK-LABEL: moore.instance "insE" @DedupB_0
DedupB #(8) insE(.c(c));
endmodule

// CHECK-LABEL: moore.module @NestedA() {
// CHECK: moore.instance "NestedB" @NestedB
// CHECK: }
Expand Down
Loading