CIRCT  20.0.0git
IbisAddOperatorLibrary.cpp
Go to the documentation of this file.
1 //===- IbisAddOperatorLibraryPass.cpp -------------------------------------===//
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 
11 #include "mlir/Pass/Pass.h"
12 
19 #include "mlir/Transforms/GreedyPatternRewriteDriver.h"
20 
22 
23 #include "mlir/IR/ImplicitLocOpBuilder.h"
24 
25 #include <iterator>
26 
27 namespace circt {
28 namespace ibis {
29 #define GEN_PASS_DEF_IBISADDOPERATORLIBRARY
30 #include "circt/Dialect/Ibis/IbisPasses.h.inc"
31 } // namespace ibis
32 } // namespace circt
33 
34 using namespace mlir;
35 using namespace circt;
36 using namespace ibis;
37 
38 namespace {
39 
40 struct AddOperatorLibraryPass
41  : public circt::ibis::impl::IbisAddOperatorLibraryBase<
42  AddOperatorLibraryPass> {
43  void runOnOperation() override;
44 };
45 
46 // TODO @mortbopet: once we have c++20, change this to a templated lambda to
47 // avoid passing the builder around.
48 template <typename TOp>
49 void addOperator(ImplicitLocOpBuilder &b, int latency) {
50  b.create<ssp::OperatorTypeOp>(
51  b.getStringAttr(TOp::getOperationName()),
52  b.getArrayAttr({b.getAttr<ssp::LatencyAttr>(latency)}));
53 }
54 
55 } // anonymous namespace
56 
57 void AddOperatorLibraryPass::runOnOperation() {
58  auto b = ImplicitLocOpBuilder::atBlockBegin(getOperation().getLoc(),
59  getOperation().getBody());
60  auto opLib = b.create<ssp::OperatorLibraryOp>();
61  opLib.setSymNameAttr(b.getStringAttr(kIbisOperatorLibName));
62  b.setInsertionPointToStart(opLib.getBodyBlock());
63 
64  // Provide definitions for some comb ops - just latency properties for now.
65 
66  // Arithmetic operators
67  addOperator<comb::AddOp>(b, 1);
68  addOperator<comb::SubOp>(b, 1);
69  addOperator<comb::MulOp>(b, 2);
70  addOperator<comb::ModSOp>(b, 2);
71  addOperator<comb::ModUOp>(b, 2);
72 
73  // Boolean operators
74  addOperator<comb::AndOp>(b, 0);
75  addOperator<comb::OrOp>(b, 0);
76  addOperator<comb::XorOp>(b, 0);
77 
78  // Comparison
79  addOperator<comb::ICmpOp>(b, 1);
80 
81  // Shift
82  addOperator<comb::ShlOp>(b, 0);
83  addOperator<comb::ShrUOp>(b, 0);
84  addOperator<comb::ShrSOp>(b, 1);
85 }
86 
88  return std::make_unique<AddOperatorLibraryPass>();
89 }
static constexpr const char * kIbisOperatorLibName
Definition: IbisOps.h:36
std::unique_ptr< mlir::Pass > createAddOperatorLibraryPass()
The InstanceGraph op interface, see InstanceGraphInterface.td for more details.
Definition: DebugAnalysis.h:21