MLIR  21.0.0git
SMTVisitors.h
Go to the documentation of this file.
1 //===- SMTVisitors.h - SMT 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 the SMT IR.
10 //
11 //===----------------------------------------------------------------------===//
12 
13 #ifndef MLIR_DIALECT_SMT_IR_SMTVISITORS_H
14 #define MLIR_DIALECT_SMT_IR_SMTVISITORS_H
15 
17 #include "llvm/ADT/TypeSwitch.h"
18 
19 namespace mlir {
20 namespace smt {
21 
22 /// This helps visit SMT nodes.
23 template <typename ConcreteType, typename ResultType = void,
24  typename... ExtraArgs>
25 class SMTOpVisitor {
26 public:
27  ResultType dispatchSMTOpVisitor(Operation *op, ExtraArgs... args) {
28  auto *thisCast = static_cast<ConcreteType *>(this);
30  .template Case<
31  // Constants
32  BoolConstantOp, IntConstantOp, BVConstantOp,
33  // Bit-vector arithmetic
34  BVNegOp, BVAddOp, BVMulOp, BVURemOp, BVSRemOp, BVSModOp, BVShlOp,
35  BVLShrOp, BVAShrOp, BVUDivOp, BVSDivOp,
36  // Bit-vector bitwise
37  BVNotOp, BVAndOp, BVOrOp, BVXOrOp,
38  // Other bit-vector ops
39  ConcatOp, ExtractOp, RepeatOp, BVCmpOp, BV2IntOp,
40  // Int arithmetic
41  IntAddOp, IntMulOp, IntSubOp, IntDivOp, IntModOp, IntCmpOp,
42  Int2BVOp,
43  // Core Ops
44  EqOp, DistinctOp, IteOp,
45  // Variable/symbol declaration
46  DeclareFunOp, ApplyFuncOp,
47  // solver interaction
48  SolverOp, AssertOp, ResetOp, PushOp, PopOp, CheckOp, SetLogicOp,
49  // Boolean logic
50  NotOp, AndOp, OrOp, XOrOp, ImpliesOp,
51  // Arrays
52  ArrayStoreOp, ArraySelectOp, ArrayBroadcastOp,
53  // Quantifiers
54  ForallOp, ExistsOp, YieldOp>([&](auto expr) -> ResultType {
55  return thisCast->visitSMTOp(expr, args...);
56  })
57  .Default([&](auto expr) -> ResultType {
58  return thisCast->visitInvalidSMTOp(op, args...);
59  });
60  }
61 
62  /// This callback is invoked on any non-expression operations.
63  ResultType visitInvalidSMTOp(Operation *op, ExtraArgs... args) {
64  op->emitOpError("unknown SMT node");
65  abort();
66  }
67 
68  /// This callback is invoked on any SMT operations that are not
69  /// handled by the concrete visitor.
70  ResultType visitUnhandledSMTOp(Operation *op, ExtraArgs... args) {
71  return ResultType();
72  }
73 
74 #define HANDLE(OPTYPE, OPKIND) \
75  ResultType visitSMTOp(OPTYPE op, ExtraArgs... args) { \
76  return static_cast<ConcreteType *>(this)->visit##OPKIND##SMTOp(op, \
77  args...); \
78  }
79 
80  // Constants
81  HANDLE(BoolConstantOp, Unhandled);
82  HANDLE(IntConstantOp, Unhandled);
83  HANDLE(BVConstantOp, Unhandled);
84 
85  // Bit-vector arithmetic
86  HANDLE(BVNegOp, Unhandled);
87  HANDLE(BVAddOp, Unhandled);
88  HANDLE(BVMulOp, Unhandled);
89  HANDLE(BVURemOp, Unhandled);
90  HANDLE(BVSRemOp, Unhandled);
91  HANDLE(BVSModOp, Unhandled);
92  HANDLE(BVShlOp, Unhandled);
93  HANDLE(BVLShrOp, Unhandled);
94  HANDLE(BVAShrOp, Unhandled);
95  HANDLE(BVUDivOp, Unhandled);
96  HANDLE(BVSDivOp, Unhandled);
97 
98  // Bit-vector bitwise operations
99  HANDLE(BVNotOp, Unhandled);
100  HANDLE(BVAndOp, Unhandled);
101  HANDLE(BVOrOp, Unhandled);
102  HANDLE(BVXOrOp, Unhandled);
103 
104  // Other bit-vector operations
105  HANDLE(ConcatOp, Unhandled);
106  HANDLE(ExtractOp, Unhandled);
107  HANDLE(RepeatOp, Unhandled);
108  HANDLE(BVCmpOp, Unhandled);
109  HANDLE(BV2IntOp, Unhandled);
110 
111  // Int arithmetic
112  HANDLE(IntAddOp, Unhandled);
113  HANDLE(IntMulOp, Unhandled);
114  HANDLE(IntSubOp, Unhandled);
115  HANDLE(IntDivOp, Unhandled);
116  HANDLE(IntModOp, Unhandled);
117 
118  HANDLE(IntCmpOp, Unhandled);
119  HANDLE(Int2BVOp, Unhandled);
120 
121  HANDLE(EqOp, Unhandled);
122  HANDLE(DistinctOp, Unhandled);
123  HANDLE(IteOp, Unhandled);
124 
125  HANDLE(DeclareFunOp, Unhandled);
126  HANDLE(ApplyFuncOp, Unhandled);
127 
128  HANDLE(SolverOp, Unhandled);
129  HANDLE(AssertOp, Unhandled);
130  HANDLE(ResetOp, Unhandled);
131  HANDLE(PushOp, Unhandled);
132  HANDLE(PopOp, Unhandled);
133  HANDLE(CheckOp, Unhandled);
134  HANDLE(SetLogicOp, Unhandled);
135 
136  // Boolean logic operations
137  HANDLE(NotOp, Unhandled);
138  HANDLE(AndOp, Unhandled);
139  HANDLE(OrOp, Unhandled);
140  HANDLE(XOrOp, Unhandled);
141  HANDLE(ImpliesOp, Unhandled);
142 
143  // Array operations
144  HANDLE(ArrayStoreOp, Unhandled);
145  HANDLE(ArraySelectOp, Unhandled);
146  HANDLE(ArrayBroadcastOp, Unhandled);
147 
148  // Quantifier operations
149  HANDLE(ForallOp, Unhandled);
150  HANDLE(ExistsOp, Unhandled);
151  HANDLE(YieldOp, Unhandled);
152 
153 #undef HANDLE
154 };
155 
156 /// This helps visit SMT types.
157 template <typename ConcreteType, typename ResultType = void,
158  typename... ExtraArgs>
160 public:
161  ResultType dispatchSMTTypeVisitor(Type type, ExtraArgs... args) {
162  auto *thisCast = static_cast<ConcreteType *>(this);
163  return TypeSwitch<Type, ResultType>(type)
164  .template Case<BoolType, IntType, BitVectorType, ArrayType, SMTFuncType,
165  SortType>([&](auto expr) -> ResultType {
166  return thisCast->visitSMTType(expr, args...);
167  })
168  .Default([&](auto expr) -> ResultType {
169  return thisCast->visitInvalidSMTType(type, args...);
170  });
171  }
172 
173  /// This callback is invoked on any non-expression types.
174  ResultType visitInvalidSMTType(Type type, ExtraArgs... args) { abort(); }
175 
176  /// This callback is invoked on any SMT type that are not
177  /// handled by the concrete visitor.
178  ResultType visitUnhandledSMTType(Type type, ExtraArgs... args) {
179  return ResultType();
180  }
181 
182 #define HANDLE(TYPE, KIND) \
183  ResultType visitSMTType(TYPE op, ExtraArgs... args) { \
184  return static_cast<ConcreteType *>(this)->visit##KIND##SMTType(op, \
185  args...); \
186  }
187 
188  HANDLE(BoolType, Unhandled);
189  HANDLE(IntegerType, Unhandled);
190  HANDLE(BitVectorType, Unhandled);
191  HANDLE(ArrayType, Unhandled);
192  HANDLE(SMTFuncType, Unhandled);
193  HANDLE(SortType, Unhandled);
194 
195 #undef HANDLE
196 };
197 
198 } // namespace smt
199 } // namespace mlir
200 
201 #endif // MLIR_DIALECT_SMT_IR_SMTVISITORS_H
Operation is the basic unit of execution within MLIR.
Definition: Operation.h:88
InFlightDiagnostic emitOpError(const Twine &message={})
Emit an error with the op name prefixed, like "'dim' op " which is convenient for verifiers.
Definition: Operation.cpp:673
Instances of the Type class are uniqued, have an immutable identifier and an optional mutable compone...
Definition: Types.h:74
This helps visit SMT nodes.
Definition: SMTVisitors.h:25
HANDLE(BoolConstantOp, Unhandled)
ResultType visitUnhandledSMTOp(Operation *op, ExtraArgs... args)
This callback is invoked on any SMT operations that are not handled by the concrete visitor.
Definition: SMTVisitors.h:70
HANDLE(BVConstantOp, Unhandled)
HANDLE(BVSRemOp, Unhandled)
HANDLE(IntConstantOp, Unhandled)
HANDLE(BV2IntOp, Unhandled)
HANDLE(NotOp, Unhandled)
HANDLE(YieldOp, Unhandled)
HANDLE(BVNotOp, Unhandled)
HANDLE(IntMulOp, Unhandled)
HANDLE(BVAddOp, Unhandled)
ResultType visitInvalidSMTOp(Operation *op, ExtraArgs... args)
This callback is invoked on any non-expression operations.
Definition: SMTVisitors.h:63
HANDLE(RepeatOp, Unhandled)
HANDLE(BVXOrOp, Unhandled)
HANDLE(XOrOp, Unhandled)
HANDLE(BVLShrOp, Unhandled)
ResultType dispatchSMTOpVisitor(Operation *op, ExtraArgs... args)
Definition: SMTVisitors.h:27
HANDLE(SolverOp, Unhandled)
HANDLE(BVUDivOp, Unhandled)
HANDLE(PushOp, Unhandled)
HANDLE(SetLogicOp, Unhandled)
HANDLE(ExtractOp, Unhandled)
HANDLE(IntCmpOp, Unhandled)
HANDLE(Int2BVOp, Unhandled)
HANDLE(EqOp, Unhandled)
HANDLE(BVCmpOp, Unhandled)
HANDLE(BVSModOp, Unhandled)
HANDLE(BVNegOp, Unhandled)
HANDLE(IntAddOp, Unhandled)
HANDLE(IntSubOp, Unhandled)
HANDLE(BVShlOp, Unhandled)
HANDLE(BVSDivOp, Unhandled)
HANDLE(BVOrOp, Unhandled)
HANDLE(ImpliesOp, Unhandled)
HANDLE(BVMulOp, Unhandled)
HANDLE(BVURemOp, Unhandled)
HANDLE(CheckOp, Unhandled)
HANDLE(PopOp, Unhandled)
HANDLE(ArraySelectOp, Unhandled)
HANDLE(IteOp, Unhandled)
HANDLE(DistinctOp, Unhandled)
HANDLE(ArrayBroadcastOp, Unhandled)
HANDLE(BVAndOp, Unhandled)
HANDLE(AssertOp, Unhandled)
HANDLE(DeclareFunOp, Unhandled)
HANDLE(OrOp, Unhandled)
HANDLE(ApplyFuncOp, Unhandled)
HANDLE(AndOp, Unhandled)
HANDLE(IntDivOp, Unhandled)
HANDLE(ResetOp, Unhandled)
HANDLE(ConcatOp, Unhandled)
HANDLE(ArrayStoreOp, Unhandled)
HANDLE(IntModOp, Unhandled)
HANDLE(ForallOp, Unhandled)
HANDLE(ExistsOp, Unhandled)
HANDLE(BVAShrOp, Unhandled)
This helps visit SMT types.
Definition: SMTVisitors.h:159
ResultType visitInvalidSMTType(Type type, ExtraArgs... args)
This callback is invoked on any non-expression types.
Definition: SMTVisitors.h:174
HANDLE(BoolType, Unhandled)
ResultType dispatchSMTTypeVisitor(Type type, ExtraArgs... args)
Definition: SMTVisitors.h:161
HANDLE(IntegerType, Unhandled)
HANDLE(ArrayType, Unhandled)
HANDLE(SMTFuncType, Unhandled)
ResultType visitUnhandledSMTType(Type type, ExtraArgs... args)
This callback is invoked on any SMT type that are not handled by the concrete visitor.
Definition: SMTVisitors.h:178
HANDLE(BitVectorType, Unhandled)
HANDLE(SortType, Unhandled)
Include the generated interface declarations.