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