LLZK 2.1.1
An open-source IR for Zero Knowledge (ZK) circuits
Loading...
Searching...
No Matches
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
18#include <llvm/ADT/TypeSwitch.h>
19
20namespace llzk::smt {
21
23template <typename ConcreteType, typename ResultType = void, typename... ExtraArgs>
25public:
26 ResultType dispatchSMTOpVisitor(mlir::Operation *op, ExtraArgs... args) {
27 auto *thisCast = static_cast<ConcreteType *>(this);
28 return mlir::TypeSwitch<mlir::Operation *, ResultType>(op)
29 .template Case<
30 // Constants
32 // Bit-vector arithmetic
35 // Bit-vector bitwise
37 // Other bit-vector ops
39 // Int arithmetic
41 // Core Ops
43 // Variable/symbol declaration
45 // solver interaction
47 // Boolean logic
49 // Arrays
51 // Quantifiers
52 ForallOp, ExistsOp, YieldOp>([&](auto expr) -> ResultType {
53 return thisCast->visitSMTOp(expr, args...);
54 }).Default([&](auto) -> ResultType { return thisCast->visitInvalidSMTOp(op, args...); });
55 }
56
58 ResultType visitInvalidSMTOp(mlir::Operation *op, ExtraArgs... /*args*/) {
59 op->emitOpError("unknown SMT node");
60 abort();
61 }
62
65 ResultType visitUnhandledSMTOp(mlir::Operation * /*op*/, ExtraArgs... /*args*/) {
66 return ResultType();
67 }
68
69#define HANDLE(OPTYPE, OPKIND) \
70 ResultType visitSMTOp(OPTYPE op, ExtraArgs... args) { \
71 return static_cast<ConcreteType *>(this)->visit##OPKIND##SMTOp(op, args...); \
72 }
73
74 // Constants
76 HANDLE(IntConstantOp, Unhandled);
77 HANDLE(BVConstantOp, Unhandled);
78
79 // Bit-vector arithmetic
80 HANDLE(BVNegOp, Unhandled);
81 HANDLE(BVAddOp, Unhandled);
82 HANDLE(BVMulOp, Unhandled);
83 HANDLE(BVURemOp, Unhandled);
84 HANDLE(BVSRemOp, Unhandled);
85 HANDLE(BVSModOp, Unhandled);
86 HANDLE(BVShlOp, Unhandled);
87 HANDLE(BVLShrOp, Unhandled);
88 HANDLE(BVAShrOp, Unhandled);
89 HANDLE(BVUDivOp, Unhandled);
90 HANDLE(BVSDivOp, Unhandled);
91
92 // Bit-vector bitwise operations
93 HANDLE(BVNotOp, Unhandled);
94 HANDLE(BVAndOp, Unhandled);
95 HANDLE(BVOrOp, Unhandled);
96 HANDLE(BVXOrOp, Unhandled);
97
98 // Other bit-vector operations
99 HANDLE(ConcatOp, Unhandled);
100 HANDLE(ExtractOp, Unhandled);
101 HANDLE(RepeatOp, Unhandled);
102 HANDLE(BVCmpOp, Unhandled);
103 HANDLE(BV2IntOp, Unhandled);
104
105 // Int arithmetic
106 HANDLE(IntAddOp, Unhandled);
107 HANDLE(IntMulOp, Unhandled);
108 HANDLE(IntSubOp, Unhandled);
109 HANDLE(IntDivOp, Unhandled);
110 HANDLE(IntModOp, Unhandled);
111
112 HANDLE(IntCmpOp, Unhandled);
113 HANDLE(Int2BVOp, Unhandled);
114
115 HANDLE(EqOp, Unhandled);
116 HANDLE(DistinctOp, Unhandled);
117 HANDLE(IteOp, Unhandled);
118
119 HANDLE(DeclareFunOp, Unhandled);
120 HANDLE(ApplyFuncOp, Unhandled);
121
122 HANDLE(SolverOp, Unhandled);
123 HANDLE(AssertOp, Unhandled);
124 HANDLE(ResetOp, Unhandled);
125 HANDLE(PushOp, Unhandled);
126 HANDLE(PopOp, Unhandled);
127 HANDLE(CheckOp, Unhandled);
128 HANDLE(SetLogicOp, Unhandled);
129
130 // Boolean logic operations
131 HANDLE(NotOp, Unhandled);
132 HANDLE(AndOp, Unhandled);
133 HANDLE(OrOp, Unhandled);
134 HANDLE(XOrOp, Unhandled);
135 HANDLE(ImpliesOp, Unhandled);
136
137 // Array operations
138 HANDLE(ArrayStoreOp, Unhandled);
141
142 // Quantifier operations
143 HANDLE(ForallOp, Unhandled);
144 HANDLE(ExistsOp, Unhandled);
145 HANDLE(YieldOp, Unhandled);
146
147#undef HANDLE
148};
149
151template <typename ConcreteType, typename ResultType = void, typename... ExtraArgs>
153public:
154 ResultType dispatchSMTTypeVisitor(mlir::Type type, ExtraArgs... args) {
155 auto *thisCast = static_cast<ConcreteType *>(this);
156 return mlir::TypeSwitch<mlir::Type, ResultType>(type)
157 .template Case<BoolType, IntType, BitVectorType, ArrayType, SMTFuncType, SortType>(
158 [&](auto expr) -> ResultType { return thisCast->visitSMTType(expr, args...); }
159 )
160 .Default([&](auto) -> ResultType { return thisCast->visitInvalidSMTType(type, args...); });
161 }
162
164 ResultType visitInvalidSMTType(mlir::Type /*type*/, ExtraArgs... /*args*/) { abort(); }
165
168 ResultType visitUnhandledSMTType(mlir::Type /*type*/, ExtraArgs... /*args*/) {
169 return ResultType();
170 }
171
172#define HANDLE(TYPE, KIND) \
173 ResultType visitSMTType(TYPE op, ExtraArgs... args) { \
174 return static_cast<ConcreteType *>(this)->visit##KIND##SMTType(op, args...); \
175 }
176
177 HANDLE(BoolType, Unhandled);
178 HANDLE(IntType, Unhandled);
180 HANDLE(ArrayType, Unhandled);
181 HANDLE(SMTFuncType, Unhandled);
182 HANDLE(SortType, Unhandled);
183
184#undef HANDLE
185};
186
187} // namespace llzk::smt
188
189#endif // MLIR_DIALECT_SMT_IR_SMTVISITORS_H
This helps visit SMT nodes.
Definition SMTVisitors.h:24
HANDLE(IntMulOp, Unhandled)
HANDLE(BVURemOp, Unhandled)
HANDLE(ResetOp, Unhandled)
HANDLE(ForallOp, Unhandled)
HANDLE(ArrayStoreOp, Unhandled)
HANDLE(BVLShrOp, Unhandled)
HANDLE(BVUDivOp, Unhandled)
HANDLE(BoolConstantOp, Unhandled)
HANDLE(BVShlOp, Unhandled)
HANDLE(BVAShrOp, Unhandled)
HANDLE(PushOp, Unhandled)
HANDLE(ImpliesOp, Unhandled)
HANDLE(OrOp, Unhandled)
HANDLE(ExistsOp, Unhandled)
HANDLE(IntAddOp, Unhandled)
HANDLE(IntSubOp, Unhandled)
HANDLE(BVConstantOp, Unhandled)
HANDLE(RepeatOp, Unhandled)
HANDLE(Int2BVOp, Unhandled)
HANDLE(ConcatOp, Unhandled)
HANDLE(SetLogicOp, Unhandled)
HANDLE(AssertOp, Unhandled)
HANDLE(BVMulOp, Unhandled)
ResultType dispatchSMTOpVisitor(mlir::Operation *op, ExtraArgs... args)
Definition SMTVisitors.h:26
ResultType visitUnhandledSMTOp(mlir::Operation *, ExtraArgs...)
This callback is invoked on any SMT operations that are not handled by the concrete visitor.
Definition SMTVisitors.h:65
HANDLE(BV2IntOp, Unhandled)
HANDLE(SolverOp, Unhandled)
ResultType visitInvalidSMTOp(mlir::Operation *op, ExtraArgs...)
This callback is invoked on any non-expression operations.
Definition SMTVisitors.h:58
HANDLE(IntModOp, Unhandled)
HANDLE(BVSRemOp, Unhandled)
HANDLE(BVAddOp, Unhandled)
HANDLE(IntConstantOp, Unhandled)
HANDLE(BVAndOp, Unhandled)
HANDLE(BVXOrOp, Unhandled)
HANDLE(NotOp, Unhandled)
HANDLE(BVNotOp, Unhandled)
HANDLE(IteOp, Unhandled)
HANDLE(EqOp, Unhandled)
HANDLE(CheckOp, Unhandled)
HANDLE(AndOp, Unhandled)
HANDLE(BVSDivOp, Unhandled)
HANDLE(BVNegOp, Unhandled)
HANDLE(ArraySelectOp, Unhandled)
HANDLE(YieldOp, Unhandled)
HANDLE(ExtractOp, Unhandled)
HANDLE(ApplyFuncOp, Unhandled)
HANDLE(DistinctOp, Unhandled)
HANDLE(IntDivOp, Unhandled)
HANDLE(BVCmpOp, Unhandled)
HANDLE(IntCmpOp, Unhandled)
HANDLE(PopOp, Unhandled)
HANDLE(XOrOp, Unhandled)
HANDLE(BVOrOp, Unhandled)
HANDLE(BVSModOp, Unhandled)
HANDLE(DeclareFunOp, Unhandled)
HANDLE(ArrayBroadcastOp, Unhandled)
This helps visit SMT types.
HANDLE(BoolType, Unhandled)
HANDLE(SortType, Unhandled)
HANDLE(IntType, Unhandled)
HANDLE(BitVectorType, Unhandled)
HANDLE(SMTFuncType, Unhandled)
HANDLE(ArrayType, Unhandled)
ResultType visitInvalidSMTType(mlir::Type, ExtraArgs...)
This callback is invoked on any non-expression types.
ResultType dispatchSMTTypeVisitor(mlir::Type type, ExtraArgs... args)
ResultType visitUnhandledSMTType(mlir::Type, ExtraArgs...)
This callback is invoked on any SMT type that are not handled by the concrete visitor.