CIRCT 23.0.0git
Loading...
Searching...
No Matches
HWToSystemC.cpp
Go to the documentation of this file.
1//===- HWToSystemC.cpp - HW To SystemC Conversion 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 is the main HW to SystemC Conversion Pass Implementation.
10//
11//===----------------------------------------------------------------------===//
12
17#include "mlir/Dialect/EmitC/IR/EmitC.h"
18#include "mlir/IR/BuiltinDialect.h"
19#include "mlir/Pass/Pass.h"
20#include "mlir/Transforms/DialectConversion.h"
21#include "llvm/ADT/TypeSwitch.h"
22
23namespace circt {
24#define GEN_PASS_DEF_CONVERTHWTOSYSTEMC
25#include "circt/Conversion/Passes.h.inc"
26} // namespace circt
27
28using namespace mlir;
29using namespace circt;
30using namespace hw;
31using namespace systemc;
32
33//===----------------------------------------------------------------------===//
34// Operation Conversion Patterns
35//===----------------------------------------------------------------------===//
36
37namespace {
38
39/// This works on each HW module, creates corresponding SystemC modules, moves
40/// the body of the module into the new SystemC module by splitting up the body
41/// into field declarations, initializations done in a newly added systemc.ctor,
42/// and internal methods to be registered in the constructor.
43struct ConvertHWModule : public OpConversionPattern<HWModuleOp> {
44 using OpConversionPattern::OpConversionPattern;
45
46 LogicalResult
47 matchAndRewrite(HWModuleOp module, OpAdaptor adaptor,
48 ConversionPatternRewriter &rewriter) const override {
49 // Parameterized modules are supported yet.
50 if (!module.getParameters().empty())
51 return emitError(module->getLoc(), "module parameters not supported yet");
52
53 auto ports = module.getPortList();
54 if (llvm::any_of(ports, [](auto &port) { return port.isInOut(); }))
55 return emitError(module->getLoc(), "inout arguments not supported yet");
56
57 // Create the SystemC module.
58 for (size_t i = 0; i < ports.size(); ++i)
59 ports[i].type = typeConverter->convertType(ports[i].type);
60
61 auto scModule = SCModuleOp::create(rewriter, module.getLoc(),
62 module.getNameAttr(), ports);
63 auto *outputOp = module.getBodyBlock()->getTerminator();
64 scModule.setVisibility(module.getVisibility());
65
66 auto portAttrs = module.getAllPortAttrs();
67 if (!portAttrs.empty())
68 scModule.setAllArgAttrs(portAttrs);
69
70 // Create a systemc.func operation inside the module after the ctor.
71 // TODO: implement logic to extract a better name and properly unique it.
72 rewriter.setInsertionPointToStart(scModule.getBodyBlock());
73 auto scFunc = SCFuncOp::create(rewriter, module.getLoc(),
74 rewriter.getStringAttr("innerLogic"));
75
76 // Inline the HW module body into the systemc.func body.
77 // TODO: do some dominance analysis to detect use-before-def and cycles in
78 // the use chain, which are allowed in graph regions but not in SSACFG
79 // regions, and when possible fix them.
80 scFunc.getBodyBlock()->erase();
81 Region &scFuncBody = scFunc.getBody();
82 rewriter.inlineRegionBefore(module.getBody(), scFuncBody, scFuncBody.end());
83
84 // Register the systemc.func inside the systemc.ctor
85 rewriter.setInsertionPointToStart(
86 scModule.getOrCreateCtor(rewriter).getBodyBlock());
87 MethodOp::create(rewriter, scModule.getLoc(), scFunc.getHandle());
88
89 // Register the sensitivities of above SC_METHOD registration.
90 SmallVector<Value> sensitivityValues(
91 llvm::make_filter_range(scModule.getArguments(), [](BlockArgument arg) {
92 return !isa<OutputType>(arg.getType());
93 }));
94 if (!sensitivityValues.empty())
95 SensitiveOp::create(rewriter, scModule.getLoc(), sensitivityValues);
96
97 // Move the block arguments of the systemc.func (that we got from the
98 // hw.module) to the systemc.module
99 rewriter.setInsertionPointToStart(scFunc.getBodyBlock());
100 auto portsLocal = module.getPortList();
101 for (size_t i = 0, e = scFunc.getRegion().getNumArguments(); i < e; ++i) {
102 auto inputRead = SignalReadOp::create(rewriter, scFunc.getLoc(),
103 scModule.getArgument(i))
104 .getResult();
105 auto converted = typeConverter->materializeSourceConversion(
106 rewriter, scModule.getLoc(), portsLocal[i].type, inputRead);
107 scFuncBody.getArgument(0).replaceAllUsesWith(converted);
108 scFuncBody.eraseArgument(0);
109 }
110
111 // Erase the HW module.
112 rewriter.eraseOp(module);
113
114 SmallVector<Value> outPorts;
115 for (auto val : scModule.getArguments()) {
116 if (isa<OutputType>(val.getType()))
117 outPorts.push_back(val);
118 }
119
120 rewriter.setInsertionPoint(outputOp);
121 for (auto args : llvm::zip(outPorts, outputOp->getOperands())) {
122 Value portValue = std::get<0>(args);
123 auto converted = typeConverter->materializeTargetConversion(
124 rewriter, scModule.getLoc(), getSignalBaseType(portValue.getType()),
125 std::get<1>(args));
126 SignalWriteOp::create(rewriter, outputOp->getLoc(), portValue, converted);
127 }
128
129 // Erase the HW OutputOp.
130 outputOp->dropAllReferences();
131 rewriter.eraseOp(outputOp);
132
133 return success();
134 }
135};
136
137/// Convert hw.instance operations to systemc.instance.decl and a
138/// systemc.instance.bind_port operation for each port in the constructor. Also
139/// insert the necessary intermediate signals and write or read their state in
140/// the update function accordingly.
141class ConvertInstance : public OpConversionPattern<InstanceOp> {
142 using OpConversionPattern::OpConversionPattern;
143
144private:
145 template <typename PortTy>
146 LogicalResult
147 collectPortInfo(ValueRange ports, ArrayAttr portNames,
148 SmallVector<systemc::ModuleType::PortInfo> &portInfo) const {
149 for (auto inPort : llvm::zip(ports, portNames)) {
150 Type ty = std::get<0>(inPort).getType();
151 systemc::ModuleType::PortInfo info;
152
153 if (isa<hw::InOutType>(ty))
154 return failure();
155
156 info.type = typeConverter->convertType(PortTy::get(ty));
157 info.name = cast<StringAttr>(std::get<1>(inPort));
158 portInfo.push_back(info);
159 }
160
161 return success();
162 }
163
164public:
165 LogicalResult
166 matchAndRewrite(InstanceOp instanceOp, OpAdaptor adaptor,
167 ConversionPatternRewriter &rewriter) const override {
168 // Make sure the parent is already converted such that we already have a
169 // constructor and update function to insert operations into.
170 auto scModule = instanceOp->getParentOfType<SCModuleOp>();
171 if (!scModule)
172 return rewriter.notifyMatchFailure(instanceOp,
173 "parent was not an SCModuleOp");
174
175 // Track the insertion points for the different places we need to insert
176 // operations while continuing to use the active pattern rewriter.
177 auto ctor = scModule.getOrCreateCtor(rewriter);
178 OpBuilder::InsertPoint stateInsertPt(ctor->getBlock(),
179 Block::iterator(ctor.getOperation()));
180 OpBuilder::InsertPoint initInsertPt(ctor.getBodyBlock(),
181 ctor.getBodyBlock()->end());
182
183 // Collect the port types and names of the instantiated module and convert
184 // them to appropriate systemc types.
185 SmallVector<systemc::ModuleType::PortInfo> portInfo;
186 if (failed(collectPortInfo<InputType>(adaptor.getInputs(),
187 adaptor.getArgNames(), portInfo)) ||
188 failed(collectPortInfo<OutputType>(instanceOp->getResults(),
189 adaptor.getResultNames(), portInfo)))
190 return instanceOp->emitOpError("inout ports not supported");
191
192 Location loc = instanceOp->getLoc();
193 auto instanceName = instanceOp.getInstanceNameAttr();
194 auto instModuleName = instanceOp.getModuleNameAttr();
195
196 // Declare the instance.
197 rewriter.restoreInsertionPoint(stateInsertPt);
198 auto instDecl = InstanceDeclOp::create(rewriter, loc, instanceName,
199 instModuleName, portInfo);
200
201 // Bind the input ports.
202 for (size_t i = 0, numInputs = adaptor.getInputs().size(); i < numInputs;
203 ++i) {
204 Value input = adaptor.getInputs()[i];
205 auto portId = rewriter.getIndexAttr(i);
206 StringAttr signalName = rewriter.getStringAttr(
207 instanceName.getValue() + "_" + portInfo[i].name.getValue());
208
209 if (auto readOp = input.getDefiningOp<SignalReadOp>()) {
210 // Use the read channel directly without adding an
211 // intermediate signal.
212 rewriter.restoreInsertionPoint(initInsertPt);
213 BindPortOp::create(rewriter, loc, instDecl, portId, readOp.getInput());
214 continue;
215 }
216
217 // Otherwise, create an intermediate signal to bind the instance port to.
218 Type sigType = SignalType::get(getSignalBaseType(portInfo[i].type));
219 rewriter.restoreInsertionPoint(stateInsertPt);
220 Value channel = SignalOp::create(rewriter, loc, sigType, signalName);
221 rewriter.restoreInsertionPoint(initInsertPt);
222 BindPortOp::create(rewriter, loc, instDecl, portId, channel);
223 rewriter.setInsertionPoint(instanceOp);
224 SignalWriteOp::create(rewriter, loc, channel, input);
225 }
226
227 // Bind the output ports.
228 for (size_t i = 0, numOutputs = instanceOp->getNumResults(); i < numOutputs;
229 ++i) {
230 size_t numInputs = adaptor.getInputs().size();
231 Value output = instanceOp->getResult(i);
232 auto portId = rewriter.getIndexAttr(i + numInputs);
233 StringAttr signalName =
234 rewriter.getStringAttr(instanceName.getValue() + "_" +
235 portInfo[i + numInputs].name.getValue());
236
237 if (output.hasOneUse()) {
238 if (auto writeOp = dyn_cast<SignalWriteOp>(*output.user_begin())) {
239 // Use the channel written to directly. When there are multiple
240 // channels this value is written to or it is used somewhere else, we
241 // cannot shortcut it and have to insert an intermediate value because
242 // we cannot insert multiple bind statements for one submodule port.
243 // It is also necessary to bind it to an intermediate signal when it
244 // has no uses as every port has to be bound to a channel.
245 rewriter.restoreInsertionPoint(initInsertPt);
246 BindPortOp::create(rewriter, loc, instDecl, portId,
247 writeOp.getDest());
248 writeOp->erase();
249 continue;
250 }
251 }
252
253 // Otherwise, create an intermediate signal.
254 Type sigType =
255 SignalType::get(getSignalBaseType(portInfo[i + numInputs].type));
256 rewriter.restoreInsertionPoint(stateInsertPt);
257 Value channel = SignalOp::create(rewriter, loc, sigType, signalName);
258 rewriter.restoreInsertionPoint(initInsertPt);
259 BindPortOp::create(rewriter, loc, instDecl, portId, channel);
260 rewriter.setInsertionPoint(instanceOp);
261 auto instOut = SignalReadOp::create(rewriter, loc, channel);
262 output.replaceAllUsesWith(instOut);
263 }
264
265 rewriter.eraseOp(instanceOp);
266 return success();
267 }
268};
269
270} // namespace
271
272//===----------------------------------------------------------------------===//
273// Conversion Infrastructure
274//===----------------------------------------------------------------------===//
275
276static void populateLegality(ConversionTarget &target) {
277 target.addIllegalDialect<HWDialect>();
278 target.addLegalDialect<mlir::BuiltinDialect>();
279 target.addLegalDialect<systemc::SystemCDialect>();
280 target.addLegalDialect<comb::CombDialect>();
281 target.addLegalDialect<emitc::EmitCDialect>();
282 target.addLegalOp<hw::ConstantOp>();
283}
284
285static void populateOpConversion(RewritePatternSet &patterns,
286 TypeConverter &typeConverter) {
287 patterns.add<ConvertHWModule, ConvertInstance>(typeConverter,
288 patterns.getContext());
289}
290
291static void populateTypeConversion(TypeConverter &converter) {
292 converter.addConversion([](Type type) { return type; });
293 converter.addConversion([&](SignalType type) {
294 return SignalType::get(converter.convertType(type.getBaseType()));
295 });
296 converter.addConversion([&](InputType type) {
297 return InputType::get(converter.convertType(type.getBaseType()));
298 });
299 converter.addConversion([&](systemc::InOutType type) {
300 return systemc::InOutType::get(converter.convertType(type.getBaseType()));
301 });
302 converter.addConversion([&](OutputType type) {
303 return OutputType::get(converter.convertType(type.getBaseType()));
304 });
305 converter.addConversion([](IntegerType type) -> Type {
306 auto bw = type.getIntOrFloatBitWidth();
307 if (bw == 1)
308 return type;
309
310 if (bw <= 64) {
311 if (type.isSigned())
312 return systemc::IntType::get(type.getContext(), bw);
313
314 return UIntType::get(type.getContext(), bw);
315 }
316
317 if (bw <= 512) {
318 if (type.isSigned())
319 return BigIntType::get(type.getContext(), bw);
320
321 return BigUIntType::get(type.getContext(), bw);
322 }
323
324 return BitVectorType::get(type.getContext(), bw);
325 });
326
327 converter.addSourceMaterialization(
328 [](OpBuilder &builder, Type type, ValueRange values, Location loc) {
329 assert(values.size() == 1);
330 auto op = ConvertOp::create(builder, loc, type, values[0]);
331 return op.getResult();
332 });
333
334 converter.addTargetMaterialization(
335 [](OpBuilder &builder, Type type, ValueRange values, Location loc) {
336 assert(values.size() == 1);
337 auto op = ConvertOp::create(builder, loc, type, values[0]);
338 return op.getResult();
339 });
340}
341
342//===----------------------------------------------------------------------===//
343// HW to SystemC Conversion Pass
344//===----------------------------------------------------------------------===//
345
346namespace {
347struct HWToSystemCPass
348 : public circt::impl::ConvertHWToSystemCBase<HWToSystemCPass> {
349 void runOnOperation() override;
350};
351} // namespace
352
353/// Create a HW to SystemC dialects conversion pass.
354std::unique_ptr<OperationPass<ModuleOp>> circt::createConvertHWToSystemCPass() {
355 return std::make_unique<HWToSystemCPass>();
356}
357
358/// This is the main entrypoint for the HW to SystemC conversion pass.
359void HWToSystemCPass::runOnOperation() {
360 MLIRContext &context = getContext();
361 ModuleOp module = getOperation();
362
363 // Create the include operation here to have exactly one 'systemc' include at
364 // the top instead of one per module.
365 OpBuilder builder(module.getRegion());
366 emitc::IncludeOp::create(builder, module->getLoc(), "systemc.h", true);
367
368 ConversionTarget target(context);
369 TypeConverter typeConverter;
370 RewritePatternSet patterns(&context);
371 populateLegality(target);
372 populateTypeConversion(typeConverter);
373 populateOpConversion(patterns, typeConverter);
374
375 if (failed(applyFullConversion(module, target, std::move(patterns))))
376 signalPassFailure();
377}
assert(baseType &&"element must be base type")
static std::unique_ptr< Context > context
static void populateLegality(ConversionTarget &target)
static void populateOpConversion(RewritePatternSet &patterns, TypeConverter &typeConverter)
static void populateTypeConversion(TypeConverter &converter)
static BigIntType get(MLIRContext *context, unsigned width)
static BigUIntType get(MLIRContext *context, unsigned width)
static BitVectorType get(MLIRContext *context, unsigned width)
static IntType get(MLIRContext *context, unsigned width)
void info(Twine message)
Definition LSPUtils.cpp:20
Type getSignalBaseType(Type type)
Get the type wrapped by a signal or port (in, inout, out) type.
The InstanceGraph op interface, see InstanceGraphInterface.td for more details.
std::unique_ptr< mlir::OperationPass< mlir::ModuleOp > > createConvertHWToSystemCPass()
Create a HW to SystemC dialects conversion pass.
Definition hw.py:1