CIRCT  19.0.0git
HWToSV.cpp
Go to the documentation of this file.
1 //===- HWToSV.cpp - HW To SV 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 SV Conversion Pass Implementation.
10 //
11 //===----------------------------------------------------------------------===//
12 
14 #include "circt/Dialect/HW/HWOps.h"
15 #include "circt/Dialect/SV/SVOps.h"
16 #include "mlir/Pass/Pass.h"
17 #include "mlir/Transforms/DialectConversion.h"
18 #include "llvm/ADT/TypeSwitch.h"
19 
20 namespace circt {
21 #define GEN_PASS_DEF_LOWERHWTOSV
22 #include "circt/Conversion/Passes.h.inc"
23 } // namespace circt
24 
25 using namespace mlir;
26 using namespace circt;
27 using namespace hw;
28 using namespace sv;
29 
30 static sv::EventControl hwToSvEventControl(hw::EventControl ec) {
31  switch (ec) {
32  case hw::EventControl::AtPosEdge:
33  return sv::EventControl::AtPosEdge;
34  case hw::EventControl::AtNegEdge:
35  return sv::EventControl::AtNegEdge;
36  case hw::EventControl::AtEdge:
37  return sv::EventControl::AtEdge;
38  }
39  llvm_unreachable("Unknown event control kind");
40 }
41 
42 namespace {
43 struct HWToSVPass : public circt::impl::LowerHWToSVBase<HWToSVPass> {
44  void runOnOperation() override;
45 };
46 
47 struct TriggeredOpConversionPattern : public OpConversionPattern<TriggeredOp> {
49 
50  LogicalResult
51  matchAndRewrite(TriggeredOp op, OpAdaptor operands,
52  ConversionPatternRewriter &rewriter) const override {
53  auto alwaysOp = rewriter.create<AlwaysOp>(
54  op.getLoc(),
55  llvm::SmallVector<sv::EventControl>{hwToSvEventControl(op.getEvent())},
56  llvm::SmallVector<Value>{op.getTrigger()});
57  rewriter.mergeBlocks(op.getBodyBlock(), alwaysOp.getBodyBlock(),
58  operands.getInputs());
59  rewriter.eraseOp(op);
60  return success();
61  }
62 };
63 
64 } // namespace
65 
66 void HWToSVPass::runOnOperation() {
67  MLIRContext &context = getContext();
68  hw::HWModuleOp module = getOperation();
69 
70  ConversionTarget target(context);
71  RewritePatternSet patterns(&context);
72 
73  target.addIllegalOp<TriggeredOp>();
74  target.addLegalDialect<sv::SVDialect>();
75 
76  patterns.add<TriggeredOpConversionPattern>(&context);
77 
78  if (failed(applyPartialConversion(module, target, std::move(patterns))))
79  signalPassFailure();
80 }
81 
82 //===----------------------------------------------------------------------===//
83 // HW to SV Conversion Pass
84 //===----------------------------------------------------------------------===//
85 
86 std::unique_ptr<OperationPass<hw::HWModuleOp>> circt::createLowerHWToSVPass() {
87  return std::make_unique<HWToSVPass>();
88 }
static sv::EventControl hwToSvEventControl(hw::EventControl ec)
Definition: HWToSV.cpp:30
The InstanceGraph op interface, see InstanceGraphInterface.td for more details.
Definition: DebugAnalysis.h:21
std::unique_ptr< mlir::OperationPass< hw::HWModuleOp > > createLowerHWToSVPass()
Definition: HWToSV.cpp:86
Definition: hw.py:1
Definition: sv.py:1