Skip to content

Commit

Permalink
[Moore] Fix the stacking fault caused by cast<Variable> and remove un…
Browse files Browse the repository at this point in the history
…used headers. (#7219)
  • Loading branch information
hailongSun2000 authored Jun 20, 2024
1 parent 13ba02f commit c7f4557
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 24 deletions.
4 changes: 2 additions & 2 deletions include/circt/Dialect/Moore/MoorePasses.td
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@

include "mlir/Pass/PassBase.td"

def SimplifyProcedures : Pass<"simplify-procedures", "moore::SVModuleOp"> {
def SimplifyProcedures : Pass<"moore-simplify-procedures", "moore::SVModuleOp"> {
let summary = "Simplify procedures";
let description = [{
Cause we want to introduce mem2reg in the moore dialect to eliminate the
Because we want to introduce mem2reg in the moore dialect to eliminate the
local temporary variables, if the local variabels exist in the procedure
body, it can be promoted by mem2reg. But global/module-level variables
don't be promoted. So this pass is aimed at inserting a local "shadow"
Expand Down
41 changes: 20 additions & 21 deletions lib/Dialect/Moore/Transforms/SimplifyProcedures.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,6 @@

#include "circt/Dialect/Moore/MooreOps.h"
#include "circt/Dialect/Moore/MoorePasses.h"
#include "circt/Dialect/Moore/MooreTypes.h"
#include "mlir/IR/Builders.h"
#include "mlir/Pass/Pass.h"
#include "llvm/Support/Casting.h"
#include "llvm/Support/raw_ostream.h"

namespace circt {
namespace moore {
Expand Down Expand Up @@ -61,23 +56,27 @@ void SimplifyProceduresPass::runOnOperation() {
if (!users.contains(user))
users.insert(user);

auto varOp = cast<VariableOp>(nestedOp.getOperand(0).getDefiningOp());
auto varName = builder.getStringAttr("local_" + varOp.getName());
auto resultType = varOp.getResult().getType();
builder.setInsertionPointToStart(procedureOp.getBody());
auto readOp = builder.create<ReadOp>(
nestedOp.getLoc(), cast<RefType>(resultType).getNestedType(),
varOp.getResult());
auto newVarOp = builder.create<VariableOp>(nestedOp.getLoc(),
resultType, varName, readOp);
builder.clearInsertionPoint();
// Because the operand of moore.event_wait is net.
if (auto varOp = llvm::dyn_cast_or_null<VariableOp>(
nestedOp.getOperand(0).getDefiningOp())) {
auto varName =
builder.getStringAttr(Twine("local_") + varOp.getName());
auto resultType = varOp.getResult().getType();
builder.setInsertionPointToStart(procedureOp.getBody());
auto readOp = builder.create<ReadOp>(
nestedOp.getLoc(), cast<RefType>(resultType).getNestedType(),
varOp.getResult());
auto newVarOp = builder.create<VariableOp>(
nestedOp.getLoc(), resultType, varName, readOp);
builder.clearInsertionPoint();

// Replace the users of the global variable with a corresponding
// "shadow" variable.
for (auto *user : users) {
user->replaceUsesOfWith(user->getOperand(0), newVarOp);
if (isa<BlockingAssignOp>(user))
assignOps.insert(user);
// Replace the users of the global variable with a corresponding
// "shadow" variable.
for (auto *user : users) {
user->replaceUsesOfWith(user->getOperand(0), newVarOp);
if (isa<BlockingAssignOp>(user))
assignOps.insert(user);
}
}
}

Expand Down
2 changes: 1 addition & 1 deletion test/Dialect/Moore/simplify-procedures.mlir
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// RUN: circt-opt --simplify-procedures %s | FileCheck %s
// RUN: circt-opt --moore-simplify-procedures %s | FileCheck %s

// CHECK-LABEL: moore.module @Foo()
moore.module @Foo() {
Expand Down

0 comments on commit c7f4557

Please sign in to comment.