CIRCT  19.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 "PassDetail.h"
14 #include "circt/Dialect/HW/HWOps.h"
16 #include "mlir/IR/Builders.h"
17 
18 using namespace circt;
19 
20 //===----------------------------------------------------------------------===//
21 // HWStubExternalModules Pass
22 //===----------------------------------------------------------------------===//
23 
24 namespace {
25 struct HWStubExternalModulesPass
26  : public sv::HWStubExternalModulesBase<HWStubExternalModulesPass> {
27  void runOnOperation() override;
28 };
29 } // end anonymous namespace
30 
31 void HWStubExternalModulesPass::runOnOperation() {
32  auto topModule = getOperation().getBody();
33  OpBuilder builder(topModule->getParentOp()->getContext());
34  builder.setInsertionPointToEnd(topModule);
35 
36  for (auto &op : llvm::make_early_inc_range(*topModule))
37  if (auto module = dyn_cast<hw::HWModuleExternOp>(op)) {
38  hw::ModulePortInfo ports(module.getPortList());
39  auto nameAttr = module.getNameAttr();
40  auto newModule = builder.create<hw::HWModuleOp>(
41  module.getLoc(), nameAttr, ports, module.getParameters());
42  auto outputOp = newModule.getBodyBlock()->getTerminator();
43  OpBuilder innerBuilder(outputOp);
44  SmallVector<Value, 8> outputs;
45  // All output ports need values, use x
46  for (auto &p : ports.getOutputs()) {
47  outputs.push_back(
48  innerBuilder.create<sv::ConstantXOp>(outputOp->getLoc(), p.type));
49  }
50  outputOp->setOperands(outputs);
51 
52  // Done with the old module.
53  module.erase();
54  }
55 }
56 
58  return std::make_unique<HWStubExternalModulesPass>();
59 }
llvm::SmallVector< StringAttr > outputs
Builder builder
def type(self)
Definition: hw.py:191
std::unique_ptr< mlir::Pass > createHWStubExternalModulesPass()
The InstanceGraph op interface, see InstanceGraphInterface.td for more details.
Definition: DebugAnalysis.h:21