forked from llvm/circt
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHWStubExternalModules.cpp
More file actions
63 lines (56 loc) · 2.23 KB
/
HWStubExternalModules.cpp
File metadata and controls
63 lines (56 loc) · 2.23 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
//===- HWStubExternalModules.cpp - HW Module Stubbing Pass ----------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
//
// This transformation pass converts external modules to empty normal modules.
//
//===----------------------------------------------------------------------===//
#include "circt/Dialect/HW/HWOps.h"
#include "circt/Dialect/SV/SVOps.h"
#include "circt/Dialect/SV/SVPasses.h"
#include "mlir/IR/Builders.h"
#include "mlir/Pass/Pass.h"
namespace circt {
namespace sv {
#define GEN_PASS_DEF_HWSTUBEXTERNALMODULES
#include "circt/Dialect/SV/SVPasses.h.inc"
} // namespace sv
} // namespace circt
using namespace circt;
//===----------------------------------------------------------------------===//
// HWStubExternalModules Pass
//===----------------------------------------------------------------------===//
namespace {
struct HWStubExternalModulesPass
: public circt::sv::impl::HWStubExternalModulesBase<
HWStubExternalModulesPass> {
void runOnOperation() override;
};
} // end anonymous namespace
void HWStubExternalModulesPass::runOnOperation() {
auto topModule = getOperation().getBody();
OpBuilder builder(topModule->getParentOp()->getContext());
builder.setInsertionPointToEnd(topModule);
for (auto &op : llvm::make_early_inc_range(*topModule))
if (auto module = dyn_cast<hw::HWModuleExternOp>(op)) {
hw::ModulePortInfo ports(module.getPortList());
auto nameAttr = module.getNameAttr();
auto newModule = hw::HWModuleOp::create(
builder, module.getLoc(), nameAttr, ports, module.getParameters());
auto outputOp = newModule.getBodyBlock()->getTerminator();
OpBuilder innerBuilder(outputOp);
SmallVector<Value, 8> outputs;
// All output ports need values, use x
for (auto &p : ports.getOutputs()) {
outputs.push_back(
sv::ConstantXOp::create(innerBuilder, outputOp->getLoc(), p.type));
}
outputOp->setOperands(outputs);
// Done with the old module.
module.erase();
}
}