Skip to content

Commit

Permalink
[Moore] Add AssignedVarOp and MergeAssignments pass.
Browse files Browse the repository at this point in the history
  • Loading branch information
hailongSun2000 committed Jul 3, 2024
1 parent 2ec4a1c commit c8c19f6
Show file tree
Hide file tree
Showing 8 changed files with 123 additions and 1 deletion.
14 changes: 13 additions & 1 deletion include/circt/Dialect/Moore/MooreOps.td
Original file line number Diff line number Diff line change
Expand Up @@ -249,11 +249,23 @@ def NetOp : MooreOp<"net", [
);
let results = (outs Res<RefType, "", [MemAlloc]>:$result);
let assemblyFormat = [{
``custom<ImplicitSSAName>($name) $kind ($assignment^)? attr-dict
`` custom<ImplicitSSAName>($name) $kind ($assignment^)? attr-dict
`:` type($result)
}];
}

def AssignedVarOp : MooreOp<"assigned_variable", [
DeclareOpInterfaceMethods<OpAsmOpInterface, ["getAsmResultNames"]>,
TypesMatchWith<"input value and variable types match",
"result", "input", "cast<RefType>($_self).getNestedType()">
]> {
let arguments = (ins StrAttr:$name, UnpackedType:$input);
let results = (outs Res<RefType, "", [MemAlloc]>:$result);
let assemblyFormat = [{
`` custom<ImplicitSSAName>($name) $input attr-dict `:` type($result)
}];
}

def ReadOp : MooreOp<"read", [
DeclareOpInterfaceMethods<PromotableMemOpInterface>,
TypesMatchWith<"input and result types match",
Expand Down
1 change: 1 addition & 0 deletions include/circt/Dialect/Moore/MoorePasses.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ namespace moore {
#define GEN_PASS_DECL
#include "circt/Dialect/Moore/MoorePasses.h.inc"

std::unique_ptr<mlir::Pass> createMergeAssignmentsPass();
std::unique_ptr<mlir::Pass> createSimplifyProceduresPass();
std::unique_ptr<mlir::Pass> createLowerConcatRefPass();

Expand Down
10 changes: 10 additions & 0 deletions include/circt/Dialect/Moore/MoorePasses.td
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,16 @@

include "mlir/Pass/PassBase.td"

def MergeAssignments : Pass<"moore-merge-assignments", "moore::SVModuleOp"> {
let summary = "Merge declarations and assignments";
let description = [{
Find easy uses of declaration and assignment and merge them into
assigned_variable. That allows the MooreToCore lowering to look for
assigned_variable to create an hw.wire.
}];
let constructor = "circt::moore::createMergeAssignmentsPass()";
}

def SimplifyProcedures : Pass<"moore-simplify-procedures", "moore::SVModuleOp"> {
let summary = "Simplify procedures";
let description = [{
Expand Down
8 changes: 8 additions & 0 deletions lib/Dialect/Moore/MooreOps.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,14 @@ void NetOp::getAsmResultNames(OpAsmSetValueNameFn setNameFn) {
setNameFn(getResult(), getName());
}

//===----------------------------------------------------------------------===//
// AssignedVarOp
//===----------------------------------------------------------------------===//

void AssignedVarOp::getAsmResultNames(OpAsmSetValueNameFn setNameFn) {
setNameFn(getResult(), getName());
}

//===----------------------------------------------------------------------===//
// ConstantOp
//===----------------------------------------------------------------------===//
Expand Down
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
LowerConcatRef.cpp
MergeAssignments.cpp
SimplifyProcedures.cpp


Expand Down
68 changes: 68 additions & 0 deletions lib/Dialect/Moore/Transforms/MergeAssignments.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
//===- MergeAssignments.cpp - Merge declaration and assignment ------------===//
//
// 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 defines the MergeAssignments pass.
// Find easy uses of declaration and assignments and merge them into
// assigned_variable. Easy use represents the declaration doesn't be performed
// bit slice. Like bit [127:0] b; assign b = 128'b0;
//
//===----------------------------------------------------------------------===//

#include "circt/Dialect/Moore/MooreOps.h"
#include "circt/Dialect/Moore/MoorePasses.h"
#include "mlir/IR/Dialect.h"
#include "llvm/ADT/MapVector.h"
#include "llvm/ADT/TypeSwitch.h"

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

using namespace circt;
using namespace moore;

namespace {
struct MergeAssignmentsPass
: public circt::moore::impl::MergeAssignmentsBase<MergeAssignmentsPass> {
void runOnOperation() override;
};
} // namespace

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

// Only collect the easy declaration and its value at module level.
static void collectAssignmets(SVModuleOp moduleOp,
llvm::MapVector<Value, Value> &assignments) {
moduleOp->walk([&](ContinuousAssignOp op) {
if (auto varOp = op.getDst().getDefiningOp<VariableOp>())
assignments[varOp] = op.getSrc();
});
}

void MergeAssignmentsPass::runOnOperation() {
OpBuilder builder(&getContext());

// Use to collect the easy declaration and its value.
llvm::MapVector<Value, Value> assignments;

collectAssignmets(getOperation(), assignments);
for (auto assignment : assignments) {
auto varName =
assignment.first.getDefiningOp()->getAttrOfType<StringAttr>("name");

builder.setInsertionPointAfterValue(assignment.first);
builder.create<AssignedVarOp>(assignment.first.getLoc(),
assignment.first.getType(), varName,
assignment.second);
}
}
21 changes: 21 additions & 0 deletions test/Dialect/Moore/merge-assignments.mlir
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// RUN: circt-opt --moore-merge-assignments %s | FileCheck %s

// CHECK-LABEL: moore.module @Foo()
moore.module @Foo() {
// CHECK: %a = moore.variable : <i32>
// CHECK: %a_0 = moore.assigned_variable name "a" %0 : <i32>
%a = moore.variable : <i32>

// CHECK: %b = moore.variable : <l32>
// CHECK: %b_1 = moore.assigned_variable name "b" %2 : <l32>
%b = moore.variable : <l32>

%0 = moore.constant 32 : i32
moore.assign %a, %0 : i32
%1 = moore.constant 32 : i32
%2 = moore.conversion %1 : !moore.i32 -> !moore.l32
moore.assign %b, %2 : l32
moore.output

}

1 change: 1 addition & 0 deletions tools/circt-verilog/circt-verilog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,7 @@ static LogicalResult populateMooreTransforms(mlir::PassManager &pm) {
modulePM.addPass(moore::createLowerConcatRefPass());
modulePM.addPass(moore::createSimplifyProceduresPass());
pm.addPass(mlir::createMem2Reg());
modulePM.addPass(moore::createMergeAssignmentsPass());
// TODO: like dedup pass.

return success();
Expand Down

0 comments on commit c8c19f6

Please sign in to comment.