CIRCT 20.0.0git
Loading...
Searching...
No Matches
Visitor.h
Go to the documentation of this file.
1//===- Handshake/Visitors.h - Handshake Dialect Visitors --------*- C++ -*-===//
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 file defines visitors that make it easier to work with Handshake IR.
10//
11//===----------------------------------------------------------------------===//
12
13#ifndef CIRCT_DIALECT_HANDSHAKE_VISITORS_H
14#define CIRCT_DIALECT_HANDSHAKE_VISITORS_H
15
17#include "mlir/Dialect/Arith/IR/Arith.h"
18#include "mlir/Dialect/Func/IR/FuncOps.h"
19#include "llvm/ADT/TypeSwitch.h"
20
21namespace circt {
22namespace handshake {
23
24/// HandshakeVisitor is a visitor for handshake nodes.
25template <typename ConcreteType, typename ResultType = void,
26 typename... ExtraArgs>
28public:
29 ResultType dispatchHandshakeVisitor(Operation *op, ExtraArgs... args) {
30 auto *thisCast = static_cast<ConcreteType *>(this);
31 return TypeSwitch<Operation *, ResultType>(op)
32 .template Case<
33 // Handshake nodes.
34 BranchOp, BufferOp, ConditionalBranchOp, ConstantOp, ControlMergeOp,
35 ForkOp, FuncOp, InstanceOp, JoinOp, LazyForkOp, LoadOp, MemoryOp,
36 ExternalMemoryOp, MergeOp, MuxOp, ReturnOp, SinkOp, SourceOp,
37 StoreOp, SyncOp, PackOp, UnpackOp>([&](auto opNode) -> ResultType {
38 return thisCast->visitHandshake(opNode, args...);
39 })
40 .Default([&](auto opNode) -> ResultType {
41 return thisCast->visitInvalidOp(op, args...);
42 });
43 }
44
45 /// This callback is invoked on any invalid operations.
46 ResultType visitInvalidOp(Operation *op, ExtraArgs... args) {
47 op->emitOpError("is unsupported operation");
48 abort();
49 }
50
51 /// This callback is invoked on any operations that are not handled by the
52 /// concrete visitor.
53 ResultType visitUnhandledOp(Operation *op, ExtraArgs... args) {
54 return ResultType();
55 }
56
57#define HANDLE(OPTYPE) \
58 ResultType visitHandshake(OPTYPE op, ExtraArgs... args) { \
59 return static_cast<ConcreteType *>(this)->visitUnhandledOp(op, args...); \
60 }
61
62 // Handshake nodes.
63 HANDLE(BranchOp);
64 HANDLE(BufferOp);
65 HANDLE(ConditionalBranchOp);
66 HANDLE(ConstantOp);
67 HANDLE(ControlMergeOp);
68 HANDLE(ForkOp);
69 HANDLE(FuncOp);
70 HANDLE(InstanceOp);
71 HANDLE(JoinOp);
72 HANDLE(LazyForkOp);
73 HANDLE(LoadOp);
74 HANDLE(MemoryOp);
75 HANDLE(ExternalMemoryOp);
76 HANDLE(MergeOp);
77 HANDLE(MuxOp);
78 HANDLE(ReturnOp);
79 HANDLE(SinkOp);
80 HANDLE(SourceOp);
81 HANDLE(StoreOp);
82 HANDLE(SyncOp);
83 HANDLE(PackOp);
84 HANDLE(UnpackOp);
85#undef HANDLE
86};
87
88} // namespace handshake
89} // namespace circt
90
91namespace mlir {
92
93/// StdExprVisitor is a visitor for standard expression nodes.
94template <typename ConcreteType, typename ResultType = void,
95 typename... ExtraArgs>
97public:
98 ResultType dispatchStdExprVisitor(Operation *op, ExtraArgs... args) {
99 auto *thisCast = static_cast<ConcreteType *>(this);
100 return TypeSwitch<Operation *, ResultType>(op)
101 .template Case<
102 arith::IndexCastOp, arith::ExtUIOp, arith::ExtSIOp, arith::TruncIOp,
103 // Integer binary expressions.
104 arith::CmpIOp, arith::AddIOp, arith::SubIOp, arith::MulIOp,
105 arith::DivSIOp, arith::RemSIOp, arith::DivUIOp, arith::RemUIOp,
106 arith::XOrIOp, arith::AndIOp, arith::OrIOp, arith::ShLIOp,
107 arith::ShRSIOp, arith::ShRUIOp, arith::SelectOp>(
108 [&](auto opNode) -> ResultType {
109 return thisCast->visitStdExpr(opNode, args...);
110 })
111 .Default([&](auto opNode) -> ResultType {
112 return thisCast->visitInvalidOp(op, args...);
113 });
114 }
115
116 /// This callback is invoked on any invalid operations.
117 ResultType visitInvalidOp(Operation *op, ExtraArgs... args) {
118 op->emitOpError("is unsupported operation");
119 abort();
120 }
121
122 /// This callback is invoked on any operations that are not handled by the
123 /// concrete visitor.
124 ResultType visitUnhandledOp(Operation *op, ExtraArgs... args) {
125 return ResultType();
126 }
127
128#define HANDLE(OPTYPE) \
129 ResultType visitStdExpr(OPTYPE op, ExtraArgs... args) { \
130 return static_cast<ConcreteType *>(this)->visitUnhandledOp(op, args...); \
131 }
132
133 HANDLE(arith::IndexCastOp);
134 HANDLE(arith::ExtSIOp);
135 HANDLE(arith::ExtUIOp);
136 HANDLE(arith::TruncIOp);
137
138 // Integer binary expressions.
139 HANDLE(arith::CmpIOp);
140 HANDLE(arith::AddIOp);
141 HANDLE(arith::SubIOp);
142 HANDLE(arith::MulIOp);
143 HANDLE(arith::DivSIOp);
144 HANDLE(arith::RemSIOp);
145 HANDLE(arith::DivUIOp);
146 HANDLE(arith::RemUIOp);
147 HANDLE(arith::XOrIOp);
148 HANDLE(arith::AndIOp);
149 HANDLE(arith::OrIOp);
150 HANDLE(arith::ShLIOp);
151 HANDLE(arith::ShRSIOp);
152 HANDLE(arith::ShRUIOp);
153#undef HANDLE
154};
155
156} // namespace mlir
157
158#endif // CIRCT_DIALECT_HANDSHAKE_VISITORS_H
HandshakeVisitor is a visitor for handshake nodes.
Definition Visitor.h:27
ResultType visitUnhandledOp(Operation *op, ExtraArgs... args)
This callback is invoked on any operations that are not handled by the concrete visitor.
Definition Visitor.h:53
ResultType visitInvalidOp(Operation *op, ExtraArgs... args)
This callback is invoked on any invalid operations.
Definition Visitor.h:46
ResultType dispatchHandshakeVisitor(Operation *op, ExtraArgs... args)
Definition Visitor.h:29
StdExprVisitor is a visitor for standard expression nodes.
Definition Visitor.h:96
ResultType visitInvalidOp(Operation *op, ExtraArgs... args)
This callback is invoked on any invalid operations.
Definition Visitor.h:117
ResultType visitUnhandledOp(Operation *op, ExtraArgs... args)
This callback is invoked on any operations that are not handled by the concrete visitor.
Definition Visitor.h:124
HANDLE(arith::AndIOp)
HANDLE(arith::ShLIOp)
HANDLE(arith::ShRSIOp)
HANDLE(arith::SubIOp)
HANDLE(arith::MulIOp)
HANDLE(arith::ShRUIOp)
HANDLE(arith::XOrIOp)
HANDLE(arith::AddIOp)
HANDLE(arith::RemSIOp)
HANDLE(arith::DivSIOp)
HANDLE(arith::DivUIOp)
HANDLE(arith::RemUIOp)
HANDLE(arith::ExtUIOp)
ResultType dispatchStdExprVisitor(Operation *op, ExtraArgs... args)
Definition Visitor.h:98
HANDLE(arith::IndexCastOp)
HANDLE(arith::TruncIOp)
HANDLE(arith::OrIOp)
HANDLE(arith::ExtSIOp)
HANDLE(arith::CmpIOp)
The InstanceGraph op interface, see InstanceGraphInterface.td for more details.