18 #include "mlir/IR/Threading.h"
19 #include "mlir/Pass/Pass.h"
23 #define GEN_PASS_DEF_DROPCONST
24 #include "circt/Dialect/FIRRTL/Passes.h.inc"
28 using namespace circt;
29 using namespace firrtl;
39 if (
auto base = type_dyn_cast<FIRRTLBaseType>(type)) {
43 if (
auto refType = type_dyn_cast<RefType>(type)) {
44 if (
auto converted =
convertType(refType.getType()))
53 class DropConstPass :
public circt::firrtl::impl::DropConstBase<DropConstPass> {
54 void runOnOperation()
override {
57 auto moduleLikes = getOperation().getOps<FModuleLike>();
58 for (
auto mod : moduleLikes) {
60 SmallVector<Attribute> portTypes;
61 portTypes.reserve(mod.getNumPorts());
62 bool convertedAny =
false;
63 llvm::transform(mod.getPortTypes(), std::back_inserter(portTypes),
64 [&](Attribute type) -> Attribute {
65 if (auto convertedType =
66 convertType(cast<TypeAttr>(type).getValue())) {
68 return TypeAttr::get(convertedType);
73 mod->setAttr(FModuleLike::getPortTypesAttrName(),
79 mlir::parallelForEach(
81 llvm::make_filter_range(moduleLikes, llvm::IsaPred<FModuleOp>),
84 module->walk([](Operation *op) {
85 if (
auto constCastOp = dyn_cast<ConstCastOp>(op)) {
87 constCastOp.getResult().replaceAllUsesWith(
88 constCastOp.getInput());
94 for (
auto ®ion : op->getRegions())
95 for (
auto &block : region.getBlocks())
96 for (
auto argument : block.getArguments())
97 if (
auto convertedType =
convertType(argument.getType()))
98 argument.setType(convertedType);
100 for (
auto result : op->getResults())
101 if (
auto convertedType =
convertType(result.getType()))
102 result.setType(convertedType);
106 markAnalysesPreserved<InstanceGraph>();
112 return std::make_unique<DropConstPass>();
static FIRRTLBaseType convertType(FIRRTLBaseType type)
Returns null type if no conversion is needed.
FIRRTLBaseType getAllConstDroppedType()
Return this type with a 'const' modifiers dropped.
Direction get(bool isOutput)
Returns an output direction if isOutput is true, otherwise returns an input direction.
std::unique_ptr< mlir::Pass > createDropConstPass()
The InstanceGraph op interface, see InstanceGraphInterface.td for more details.