diff --git a/include/circt/Dialect/Moore/MoorePasses.td b/include/circt/Dialect/Moore/MoorePasses.td index 977c477b2317..c3031ae5b683 100644 --- a/include/circt/Dialect/Moore/MoorePasses.td +++ b/include/circt/Dialect/Moore/MoorePasses.td @@ -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" diff --git a/lib/Dialect/Moore/Transforms/SimplifyProcedures.cpp b/lib/Dialect/Moore/Transforms/SimplifyProcedures.cpp index c4896cf09613..41eab61ed3b1 100644 --- a/lib/Dialect/Moore/Transforms/SimplifyProcedures.cpp +++ b/lib/Dialect/Moore/Transforms/SimplifyProcedures.cpp @@ -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 { @@ -61,23 +56,27 @@ void SimplifyProceduresPass::runOnOperation() { if (!users.contains(user)) users.insert(user); - auto varOp = cast(nestedOp.getOperand(0).getDefiningOp()); - auto varName = builder.getStringAttr("local_" + varOp.getName()); - auto resultType = varOp.getResult().getType(); - builder.setInsertionPointToStart(procedureOp.getBody()); - auto readOp = builder.create( - nestedOp.getLoc(), cast(resultType).getNestedType(), - varOp.getResult()); - auto newVarOp = builder.create(nestedOp.getLoc(), - resultType, varName, readOp); - builder.clearInsertionPoint(); + // Because the operand of moore.event_wait is net. + if (auto varOp = llvm::dyn_cast_or_null( + 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( + nestedOp.getLoc(), cast(resultType).getNestedType(), + varOp.getResult()); + auto newVarOp = builder.create( + 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(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(user)) + assignOps.insert(user); + } } } diff --git a/test/Dialect/Moore/simplify-procedures.mlir b/test/Dialect/Moore/simplify-procedures.mlir index 217bea07a460..acf21e623e67 100644 --- a/test/Dialect/Moore/simplify-procedures.mlir +++ b/test/Dialect/Moore/simplify-procedures.mlir @@ -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() {