CIRCT  20.0.0git
HWStubExternalModules.cpp
Go to the documentation of this file.
1 //===- HWStubExternalModules.cpp - HW Module Stubbing Pass ----------------===//
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 transformation pass converts external modules to empty normal modules.
10 //
11 //===----------------------------------------------------------------------===//
12 
13 #include "circt/Dialect/HW/HWOps.h"
14 #include "circt/Dialect/SV/SVOps.h"
16 #include "mlir/IR/Builders.h"
17 #include "mlir/Pass/Pass.h"
18 
19 namespace circt {
20 namespace sv {
21 #define GEN_PASS_DEF_HWSTUBEXTERNALMODULES
22 #include "circt/Dialect/SV/SVPasses.h.inc"
23 } // namespace sv
24 } // namespace circt
25 
26 using namespace circt;
27 //===----------------------------------------------------------------------===//
28 // HWStubExternalModules Pass
29 //===----------------------------------------------------------------------===//
30 
31 namespace {
32 struct HWStubExternalModulesPass
33  : public circt::sv::impl::HWStubExternalModulesBase<
34  HWStubExternalModulesPass> {
35  void runOnOperation() override;
36 };
37 } // end anonymous namespace
38 
39 void HWStubExternalModulesPass::runOnOperation() {
40  auto topModule = getOperation().getBody();
41  OpBuilder builder(topModule->getParentOp()->getContext());
42  builder.setInsertionPointToEnd(topModule);
43 
44  for (auto &op : llvm::make_early_inc_range(*topModule))
45  if (auto module = dyn_cast<hw::HWModuleExternOp>(op)) {
46  hw::ModulePortInfo ports(module.getPortList());
47  auto nameAttr = module.getNameAttr();
48  auto newModule = builder.create<hw::HWModuleOp>(
49  module.getLoc(), nameAttr, ports, module.getParameters());
50  auto outputOp = newModule.getBodyBlock()->getTerminator();
51  OpBuilder innerBuilder(outputOp);
52  SmallVector<Value, 8> outputs;
53  // All output ports need values, use x
54  for (auto &p : ports.getOutputs()) {
55  outputs.push_back(
56  innerBuilder.create<sv::ConstantXOp>(outputOp->getLoc(), p.type));
57  }
58  outputOp->setOperands(outputs);
59 
60  // Done with the old module.
61  module.erase();
62  }
63 }
64 
66  return std::make_unique<HWStubExternalModulesPass>();
67 }
def type(self)
Definition: hw.py:325
std::unique_ptr< mlir::Pass > createHWStubExternalModulesPass()
The InstanceGraph op interface, see InstanceGraphInterface.td for more details.
Definition: DebugAnalysis.h:21
Definition: sv.py:1