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