Loading [MathJax]/jax/output/HTML-CSS/config.js
CIRCT 22.0.0git
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
HWVisitors.h
Go to the documentation of this file.
1//===- HWVisitors.h - HW 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 HW IR.
10//
11//===----------------------------------------------------------------------===//
12
13#ifndef CIRCT_DIALECT_HW_HWVISITORS_H
14#define CIRCT_DIALECT_HW_HWVISITORS_H
15
17#include "llvm/ADT/TypeSwitch.h"
18
19namespace circt {
20namespace hw {
21
22/// This helps visit TypeOp nodes.
23template <typename ConcreteType, typename ResultType = void,
24 typename... ExtraArgs>
26public:
27 ResultType dispatchTypeOpVisitor(Operation *op, ExtraArgs... args) {
28 auto *thisCast = static_cast<ConcreteType *>(this);
29 return TypeSwitch<Operation *, ResultType>(op)
30 .template Case<ConstantOp, AggregateConstantOp,
31 // Array operations
32 ArraySliceOp, ArrayCreateOp, ArrayConcatOp, ArrayGetOp,
33 ArrayInjectOp,
34 // Struct operations
35 StructCreateOp, StructExtractOp, StructInjectOp,
36 // Union operations
37 UnionCreateOp, UnionExtractOp,
38 // Cast operation
39 BitcastOp, ParamValueOp,
40 // Enum operations
41 EnumConstantOp, EnumCmpOp>([&](auto expr) -> ResultType {
42 return thisCast->visitTypeOp(expr, args...);
43 })
44 .Default([&](auto expr) -> ResultType {
45 return thisCast->visitInvalidTypeOp(op, args...);
46 });
47 }
48
49 /// This callback is invoked on any non-expression operations.
50 ResultType visitInvalidTypeOp(Operation *op, ExtraArgs... args) {
51 op->emitOpError("unknown HW combinational node");
52 abort();
53 }
54
55 /// This callback is invoked on any combinational operations that are not
56 /// handled by the concrete visitor.
57 ResultType visitUnhandledTypeOp(Operation *op, ExtraArgs... args) {
58 return ResultType();
59 }
60
61#define HANDLE(OPTYPE, OPKIND) \
62 ResultType visitTypeOp(OPTYPE op, ExtraArgs... args) { \
63 return static_cast<ConcreteType *>(this)->visit##OPKIND##TypeOp(op, \
64 args...); \
65 }
66
67 HANDLE(ConstantOp, Unhandled);
68 HANDLE(AggregateConstantOp, Unhandled);
69 HANDLE(BitcastOp, Unhandled);
70 HANDLE(ParamValueOp, Unhandled);
71 HANDLE(StructCreateOp, Unhandled);
72 HANDLE(StructExtractOp, Unhandled);
73 HANDLE(StructInjectOp, Unhandled);
74 HANDLE(UnionCreateOp, Unhandled);
75 HANDLE(UnionExtractOp, Unhandled);
76 HANDLE(ArraySliceOp, Unhandled);
77 HANDLE(ArrayGetOp, Unhandled);
78 HANDLE(ArrayCreateOp, Unhandled);
79 HANDLE(ArrayConcatOp, Unhandled);
80 HANDLE(ArrayInjectOp, Unhandled);
81 HANDLE(EnumCmpOp, Unhandled);
82 HANDLE(EnumConstantOp, Unhandled);
83#undef HANDLE
84};
85
86/// This helps visit TypeOp nodes.
87template <typename ConcreteType, typename ResultType = void,
88 typename... ExtraArgs>
90public:
91 ResultType dispatchStmtVisitor(Operation *op, ExtraArgs... args) {
92 auto *thisCast = static_cast<ConcreteType *>(this);
93 return TypeSwitch<Operation *, ResultType>(op)
94 .template Case<OutputOp, InstanceOp, InstanceChoiceOp, TypeScopeOp,
95 TypedeclOp>([&](auto expr) -> ResultType {
96 return thisCast->visitStmt(expr, args...);
97 })
98 .Default([&](auto expr) -> ResultType {
99 return thisCast->visitInvalidStmt(op, args...);
100 });
101 }
102
103 /// This callback is invoked on any non-expression operations.
104 ResultType visitInvalidStmt(Operation *op, ExtraArgs... args) {
105 op->emitOpError("unknown hw statement");
106 abort();
107 }
108
109 /// This callback is invoked on any combinational operations that are not
110 /// handled by the concrete visitor.
111 ResultType visitUnhandledTypeOp(Operation *op, ExtraArgs... args) {
112 return ResultType();
113 }
114
115 /// This fallback is invoked on any binary node that isn't explicitly handled.
116 /// The default implementation delegates to the 'unhandled' fallback.
117 ResultType visitBinaryTypeOp(Operation *op, ExtraArgs... args) {
118 return static_cast<ConcreteType *>(this)->visitUnhandledTypeOp(op, args...);
119 }
120
121 ResultType visitUnaryTypeOp(Operation *op, ExtraArgs... args) {
122 return static_cast<ConcreteType *>(this)->visitUnhandledTypeOp(op, args...);
123 }
124
125#define HANDLE(OPTYPE, OPKIND) \
126 ResultType visitStmt(OPTYPE op, ExtraArgs... args) { \
127 return static_cast<ConcreteType *>(this)->visit##OPKIND##Stmt(op, \
128 args...); \
129 }
130
131 // Basic nodes.
132 HANDLE(OutputOp, Unhandled);
133 HANDLE(InstanceOp, Unhandled);
134 HANDLE(InstanceChoiceOp, Unhandled);
135 HANDLE(TypeScopeOp, Unhandled);
136 HANDLE(TypedeclOp, Unhandled);
137#undef HANDLE
138};
139
140} // namespace hw
141} // namespace circt
142
143#endif // CIRCT_DIALECT_HW_HWVISITORS_H
This helps visit TypeOp nodes.
Definition HWVisitors.h:89
ResultType visitInvalidStmt(Operation *op, ExtraArgs... args)
This callback is invoked on any non-expression operations.
Definition HWVisitors.h:104
ResultType visitUnaryTypeOp(Operation *op, ExtraArgs... args)
Definition HWVisitors.h:121
HANDLE(InstanceOp, Unhandled)
HANDLE(TypeScopeOp, Unhandled)
ResultType visitUnhandledTypeOp(Operation *op, ExtraArgs... args)
This callback is invoked on any combinational operations that are not handled by the concrete visitor...
Definition HWVisitors.h:111
HANDLE(TypedeclOp, Unhandled)
HANDLE(OutputOp, Unhandled)
HANDLE(InstanceChoiceOp, Unhandled)
ResultType visitBinaryTypeOp(Operation *op, ExtraArgs... args)
This fallback is invoked on any binary node that isn't explicitly handled.
Definition HWVisitors.h:117
ResultType dispatchStmtVisitor(Operation *op, ExtraArgs... args)
Definition HWVisitors.h:91
This helps visit TypeOp nodes.
Definition HWVisitors.h:25
HANDLE(EnumCmpOp, Unhandled)
HANDLE(ParamValueOp, Unhandled)
HANDLE(AggregateConstantOp, Unhandled)
HANDLE(ConstantOp, Unhandled)
HANDLE(UnionExtractOp, Unhandled)
ResultType dispatchTypeOpVisitor(Operation *op, ExtraArgs... args)
Definition HWVisitors.h:27
HANDLE(ArrayGetOp, Unhandled)
HANDLE(ArraySliceOp, Unhandled)
HANDLE(StructExtractOp, Unhandled)
HANDLE(ArrayConcatOp, Unhandled)
HANDLE(ArrayInjectOp, Unhandled)
HANDLE(StructCreateOp, Unhandled)
HANDLE(StructInjectOp, Unhandled)
HANDLE(ArrayCreateOp, Unhandled)
ResultType visitUnhandledTypeOp(Operation *op, ExtraArgs... args)
This callback is invoked on any combinational operations that are not handled by the concrete visitor...
Definition HWVisitors.h:57
ResultType visitInvalidTypeOp(Operation *op, ExtraArgs... args)
This callback is invoked on any non-expression operations.
Definition HWVisitors.h:50
HANDLE(UnionCreateOp, Unhandled)
HANDLE(BitcastOp, Unhandled)
HANDLE(EnumConstantOp, Unhandled)
The InstanceGraph op interface, see InstanceGraphInterface.td for more details.
Definition hw.py:1