CIRCT  19.0.0git
DropConst.cpp
Go to the documentation of this file.
1 //===- DropConst.cpp - Check and remove const types -------------*- C++ -*-===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file defines the DropConst pass.
10 //
11 //===----------------------------------------------------------------------===//
12 
18 #include "mlir/IR/Threading.h"
19 #include "mlir/Pass/Pass.h"
20 
21 namespace circt {
22 namespace firrtl {
23 #define GEN_PASS_DEF_DROPCONST
24 #include "circt/Dialect/FIRRTL/Passes.h.inc"
25 } // namespace firrtl
26 } // namespace circt
27 
28 using namespace circt;
29 using namespace firrtl;
30 
31 /// Returns null type if no conversion is needed.
33  auto nonConstType = type.getAllConstDroppedType();
34  return nonConstType != type ? nonConstType : FIRRTLBaseType{};
35 }
36 
37 /// Returns null type if no conversion is needed.
38 static Type convertType(Type type) {
39  if (auto base = type_dyn_cast<FIRRTLBaseType>(type)) {
40  return convertType(base);
41  }
42 
43  if (auto refType = type_dyn_cast<RefType>(type)) {
44  if (auto converted = convertType(refType.getType()))
45  return RefType::get(converted, refType.getForceable(),
46  refType.getLayer());
47  }
48 
49  return {};
50 }
51 
52 namespace {
53 class DropConstPass : public circt::firrtl::impl::DropConstBase<DropConstPass> {
54  void runOnOperation() override {
55 
56  // Update signatures of all module-likes.
57  auto moduleLikes = getOperation().getOps<FModuleLike>();
58  for (auto mod : moduleLikes) {
59  //// Update the module signature with non-'const' ports
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())) {
67  convertedAny = true;
68  return TypeAttr::get(convertedType);
69  }
70  return type;
71  });
72  if (convertedAny)
73  mod->setAttr(FModuleLike::getPortTypesAttrName(),
74  ArrayAttr::get(mod.getContext(), portTypes));
75  };
76 
77  // Rewrite module bodies in parallel.
78  // Filter on FModuleOp specifically as there's no "hasBody()".
79  mlir::parallelForEach(
80  &getContext(),
81  llvm::make_filter_range(moduleLikes, llvm::IsaPred<FModuleOp>),
82  [](auto module) {
83  // Convert the module body if present
84  module->walk([](Operation *op) {
85  if (auto constCastOp = dyn_cast<ConstCastOp>(op)) {
86  // Remove any `ConstCastOp`, replacing results with inputs
87  constCastOp.getResult().replaceAllUsesWith(
88  constCastOp.getInput());
89  constCastOp->erase();
90  return;
91  }
92 
93  // Convert any block arguments
94  for (auto &region : 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);
99 
100  for (auto result : op->getResults())
101  if (auto convertedType = convertType(result.getType()))
102  result.setType(convertedType);
103  });
104  });
105 
106  markAnalysesPreserved<InstanceGraph>();
107  }
108 };
109 } // namespace
110 
111 std::unique_ptr<mlir::Pass> circt::firrtl::createDropConstPass() {
112  return std::make_unique<DropConstPass>();
113 }
static FIRRTLBaseType convertType(FIRRTLBaseType type)
Returns null type if no conversion is needed.
Definition: DropConst.cpp:32
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.
Definition: CalyxOps.cpp:54
std::unique_ptr< mlir::Pass > createDropConstPass()
Definition: DropConst.cpp:111
The InstanceGraph op interface, see InstanceGraphInterface.td for more details.
Definition: DebugAnalysis.h:21