LLZK 3.0.0
An open-source IR for Zero Knowledge (ZK) circuits
Loading...
Searching...
No Matches
TypeHelper.h
Go to the documentation of this file.
1//===-- TypeHelper.h --------------------------------------------*- C++ -*-===//
2//
3// Part of the LLZK Project, under the Apache License v2.0.
4// See LICENSE.txt for license information.
5// Copyright 2025 Veridise Inc.
6// SPDX-License-Identifier: Apache-2.0
7//
8//===----------------------------------------------------------------------===//
9
10#pragma once
11
13
14#include <mlir/IR/OpImplementation.h>
15#include <mlir/IR/Operation.h>
16#include <mlir/IR/SymbolTable.h>
17
18#include <llvm/ADT/ArrayRef.h>
19#include <llvm/ADT/DenseMap.h>
20#include <llvm/ADT/StringRef.h>
21
22namespace llzk {
23
24// Forward declarations
25namespace component {
26class StructType;
27} // namespace component
28namespace array {
29class ArrayType;
30} // namespace array
31namespace pod {
32class PodType;
33} // namespace pod
34
39class BuildShortTypeString {
40 std::string ret;
41 llvm::raw_string_ostream ss;
42
43 BuildShortTypeString() : ret(), ss(ret) {}
44 BuildShortTypeString &append(mlir::Type);
45 BuildShortTypeString &append(mlir::ArrayRef<mlir::Attribute>);
46 BuildShortTypeString &append(mlir::Attribute);
47
48 void appendSymRef(mlir::SymbolRefAttr);
49 void appendSymName(mlir::StringRef);
50
51public:
53 static inline std::string from(mlir::Type type) {
54 return BuildShortTypeString().append(type).ret;
55 }
56
58 static inline std::string from(mlir::Attribute attr) {
59 return BuildShortTypeString().append(attr).ret;
60 }
61
64 static inline std::string from(mlir::ArrayRef<mlir::Attribute> attrs) {
65 return BuildShortTypeString().append(attrs).ret;
66 }
67};
68
69// This function asserts that the given Attribute kind is legal within the LLZK types that can
70// contain Attribute parameters (i.e., ArrayType, StructType, and TypeVarType). This should be used
71// in any function that examines the attribute parameters within parameterized LLZK types to ensure
72// that the function handles all possible cases properly, especially if more legal attributes are
73// added in the future. Throw a fatal error if anything illegal is found, indicating that the caller
74// of this function should be updated.
75void assertValidAttrForParamOfType(mlir::Attribute attr);
76
78bool isValidType(mlir::Type type);
79
83 mlir::Type type, mlir::SymbolTableCollection &symbolTable, mlir::Operation *op
84);
85
87bool isValidGlobalType(mlir::Type type);
88
90bool isValidEmitEqType(mlir::Type type);
91
93bool isValidConstReadType(mlir::Type type);
94
96bool isValidArrayElemType(mlir::Type type);
97
99bool isValidArrayType(mlir::Type type);
100
106bool isConcreteType(mlir::Type type, bool allowStructParams = true);
107
114bool isTypeVarFreeType(mlir::Type type);
115
117enum class AttrConcreteness : std::uint8_t {
121};
122
130AttrConcreteness classifyAttrConcreteness(mlir::Attribute attr, bool allowStructParams = true);
131
133inline bool isConcreteStructParamAttr(mlir::Attribute attr, bool allowStructParams = true) {
134 return classifyAttrConcreteness(attr, allowStructParams) == AttrConcreteness::Concrete;
135}
136
137inline mlir::LogicalResult checkValidType(EmitErrorFn emitError, mlir::Type type) {
138 if (!isValidType(type)) {
139 return emitError() << "expected a valid LLZK type but found " << type;
140 } else {
141 return mlir::success();
142 }
143}
144
146bool hasAffineMapAttr(mlir::Type type);
147
148enum class Side : std::uint8_t { EMPTY = 0, LHS, RHS, TOMB };
149static inline mlir::raw_ostream &operator<<(mlir::raw_ostream &os, const Side &val) {
150 switch (val) {
151 case Side::EMPTY:
152 os << "EMPTY";
153 break;
154 case Side::TOMB:
155 os << "TOMB";
156 break;
157 case Side::LHS:
158 os << "LHS";
159 break;
160 case Side::RHS:
161 os << "RHS";
162 break;
163 }
164 return os;
165}
166
167inline Side reverse(Side in) {
168 switch (in) {
169 case Side::LHS:
170 return Side::RHS;
171 case Side::RHS:
172 return Side::LHS;
173 default:
174 return in;
175 }
176}
177
178} // namespace llzk
179
180namespace llvm {
181template <> struct DenseMapInfo<llzk::Side> {
182 using T = llzk::Side;
183 static inline T getEmptyKey() { return T::EMPTY; }
184 static inline T getTombstoneKey() { return T::TOMB; }
185 static unsigned getHashValue(const T &val) {
186 using UT = std::underlying_type_t<T>;
187 return llvm::DenseMapInfo<UT>::getHashValue(static_cast<UT>(val));
188 }
189 static bool isEqual(const T &lhs, const T &rhs) { return lhs == rhs; }
190};
191} // namespace llvm
192
193namespace llzk {
194
195bool isDynamic(mlir::IntegerAttr intAttr);
196
204
207uint64_t computeEmitEqCardinality(mlir::Type type);
208
217using UnificationMap = mlir::DenseMap<std::pair<mlir::SymbolRefAttr, Side>, mlir::Attribute>;
218
222 const mlir::ArrayRef<mlir::Attribute> &lhsParams,
223 const mlir::ArrayRef<mlir::Attribute> &rhsParams, UnificationMap *unifications = nullptr
224);
225
229 const mlir::ArrayAttr &lhsParams, const mlir::ArrayAttr &rhsParams,
230 UnificationMap *unifications = nullptr
231);
232
237 mlir::ArrayRef<llvm::StringRef> rhsReversePrefix = {}, UnificationMap *unifications = nullptr
238);
239
244 mlir::ArrayRef<llvm::StringRef> rhsReversePrefix = {}, UnificationMap *unifications = nullptr
245);
246
250 pod::PodType lhs, pod::PodType rhs, mlir::ArrayRef<llvm::StringRef> rhsReversePrefix = {},
251 UnificationMap *unifications = nullptr
252);
253
257 mlir::FunctionType lhs, mlir::FunctionType rhs,
258 mlir::ArrayRef<llvm::StringRef> rhsReversePrefix = {}, UnificationMap *unifications = nullptr
259);
260
264 mlir::Type lhs, mlir::Type rhs, mlir::ArrayRef<llvm::StringRef> rhsReversePrefix = {},
265 UnificationMap *unifications = nullptr
266);
267
270template <typename Iter1, typename Iter2>
271inline bool typeListsUnify(
272 Iter1 lhs, Iter2 rhs, mlir::ArrayRef<llvm::StringRef> rhsReversePrefix = {},
273 UnificationMap *unifications = nullptr
274) {
275 return (lhs.size() == rhs.size()) &&
276 std::equal(lhs.begin(), lhs.end(), rhs.begin(), [&](mlir::Type a, mlir::Type b) {
277 return typesUnify(a, b, rhsReversePrefix, unifications);
278 });
279}
280
281template <typename Iter1, typename Iter2>
283 Iter1 lhs, Iter2 rhs, mlir::ArrayRef<llvm::StringRef> rhsReversePrefix = {},
284 UnificationMap *unifications = nullptr
285) {
286 return lhs.size() == 1 && rhs.size() == 1 &&
287 typesUnify(lhs.front(), rhs.front(), rhsReversePrefix, unifications);
288}
289
298 mlir::Type oldTy, mlir::Type newTy,
299 llvm::function_ref<bool(mlir::Type oldTy, mlir::Type newTy)> knownOldToNew = nullptr
300);
301
302template <typename TypeClass> inline TypeClass getIfSingleton(mlir::TypeRange types) {
303 return (types.size() == 1) ? llvm::dyn_cast<TypeClass>(types.front()) : nullptr;
304}
305
306template <typename TypeClass> inline TypeClass getAtIndex(mlir::TypeRange types, size_t index) {
307 return (types.size() > index) ? llvm::dyn_cast<TypeClass>(types[index]) : nullptr;
308}
309
311mlir::FailureOr<mlir::IntegerAttr> forceIntType(mlir::IntegerAttr attr, EmitErrorFn emitError);
312
314mlir::FailureOr<mlir::Attribute> forceIntAttrType(mlir::Attribute attr, EmitErrorFn emitError);
315
317mlir::FailureOr<llvm::SmallVector<mlir::Attribute>>
318forceIntAttrTypes(llvm::ArrayRef<mlir::Attribute> attrList, EmitErrorFn emitError);
319
321mlir::LogicalResult verifyIntAttrType(EmitErrorFn emitError, mlir::Attribute in);
322
324mlir::LogicalResult verifyAffineMapAttrType(EmitErrorFn emitError, mlir::Attribute in);
325
327mlir::LogicalResult verifyStructTypeParams(EmitErrorFn emitError, mlir::ArrayAttr params);
328
330mlir::LogicalResult
331verifyArrayDimSizes(EmitErrorFn emitError, mlir::ArrayRef<mlir::Attribute> dimensionSizes);
332
334mlir::LogicalResult verifyArrayType(
335 EmitErrorFn emitError, mlir::Type elementType, mlir::ArrayRef<mlir::Attribute> dimensionSizes
336);
337
343mlir::LogicalResult verifySubArrayType(
344 EmitErrorFn emitError, array::ArrayType arrayType, array::ArrayType subArrayType
345);
346
350mlir::LogicalResult verifySubArrayOrElementType(
351 EmitErrorFn emitError, array::ArrayType arrayType, mlir::Type subArrayOrElemType
352);
353
356bool isFeltOrSimpleFeltAggregate(mlir::Type ty);
357
361bool isValidMainSignalType(mlir::Type pType);
362
363} // namespace llzk
static std::string from(mlir::Attribute attr)
Return a brief string representation of one LLZK type parameter attribute.
Definition TypeHelper.h:58
static std::string from(mlir::ArrayRef< mlir::Attribute > attrs)
Return a brief string representation of the attribute list from a parameterized type.
Definition TypeHelper.h:64
static std::string from(mlir::Type type)
Return a brief string representation of the given LLZK type.
Definition TypeHelper.h:53
LogicalResult verifyAffineMapAttrType(EmitErrorFn emitError, Attribute in)
void assertValidAttrForParamOfType(Attribute attr)
LogicalResult verifySubArrayType(EmitErrorFn emitError, ArrayType arrayType, ArrayType subArrayType)
Determine if the subArrayType is a valid subarray of arrayType.
FailureOr< Attribute > forceIntAttrType(Attribute attr, EmitErrorFn emitError)
uint64_t computeEmitEqCardinality(Type type)
bool isValidArrayType(Type type)
LogicalResult verifyIntAttrType(EmitErrorFn emitError, Attribute in)
bool typeListsUnify(Iter1 lhs, Iter2 rhs, mlir::ArrayRef< llvm::StringRef > rhsReversePrefix={}, UnificationMap *unifications=nullptr)
Return true iff the two lists of Type instances are equivalent or could be equivalent after full inst...
Definition TypeHelper.h:271
bool isConcreteType(Type type, bool allowStructParams)
bool isValidArrayElemType(Type type)
TypeClass getIfSingleton(mlir::TypeRange types)
Definition TypeHelper.h:302
bool isValidGlobalType(Type type)
AttrConcreteness
Concreteness classification for an argument to a parameterized struct type.
Definition TypeHelper.h:117
FailureOr< IntegerAttr > forceIntType(IntegerAttr attr, EmitErrorFn emitError)
bool singletonTypeListsUnify(Iter1 lhs, Iter2 rhs, mlir::ArrayRef< llvm::StringRef > rhsReversePrefix={}, UnificationMap *unifications=nullptr)
Definition TypeHelper.h:282
bool structTypesUnify(StructType lhs, StructType rhs, ArrayRef< StringRef > rhsReversePrefix, UnificationMap *unifications)
LogicalResult verifyArrayType(EmitErrorFn emitError, Type elementType, ArrayRef< Attribute > dimensionSizes)
bool isFeltOrSimpleFeltAggregate(Type ty)
LogicalResult verifySubArrayOrElementType(EmitErrorFn emitError, ArrayType arrayType, Type subArrayOrElemType)
bool isValidColumnType(Type type, SymbolTableCollection &symbolTable, Operation *op)
bool isValidMainSignalType(Type pType)
mlir::DenseMap< std::pair< mlir::SymbolRefAttr, Side >, mlir::Attribute > UnificationMap
Optional result from type unifications.
Definition TypeHelper.h:217
llvm::function_ref< InFlightDiagnosticWrapper()> EmitErrorFn
Callback to produce an error diagnostic.
FailureOr< SmallVector< Attribute > > forceIntAttrTypes(ArrayRef< Attribute > attrList, EmitErrorFn emitError)
AttrConcreteness classifyAttrConcreteness(Attribute attr, bool allowStructParams)
Interval operator<<(const Interval &lhs, const Interval &rhs)
bool podTypesUnify(PodType lhs, PodType rhs, ArrayRef< StringRef > rhsReversePrefix, UnificationMap *unifications)
ArrayType flattenArrayElementType(ArrayType outerArrTy, Type elementType)
bool isValidEmitEqType(Type type)
TypeClass getAtIndex(mlir::TypeRange types, size_t index)
Definition TypeHelper.h:306
bool isValidType(Type type)
bool arrayTypesUnify(ArrayType lhs, ArrayType rhs, ArrayRef< StringRef > rhsReversePrefix, UnificationMap *unifications)
bool isDynamic(IntegerAttr intAttr)
Side reverse(Side in)
Definition TypeHelper.h:167
bool isTypeVarFreeType(Type type)
bool typesUnify(Type lhs, Type rhs, ArrayRef< StringRef > rhsReversePrefix, UnificationMap *unifications)
bool typeParamsUnify(const ArrayRef< Attribute > &lhsParams, const ArrayRef< Attribute > &rhsParams, UnificationMap *unifications)
bool isConcreteStructParamAttr(mlir::Attribute attr, bool allowStructParams=true)
Return true if attr is a concrete argument for a parameterized struct type.
Definition TypeHelper.h:133
bool isMoreConcreteUnification(Type oldTy, Type newTy, llvm::function_ref< bool(Type oldTy, Type newTy)> knownOldToNew)
bool functionTypesUnify(FunctionType lhs, FunctionType rhs, ArrayRef< StringRef > rhsReversePrefix, UnificationMap *unifications)
LogicalResult verifyStructTypeParams(EmitErrorFn emitError, ArrayAttr params)
bool hasAffineMapAttr(Type type)
mlir::LogicalResult checkValidType(EmitErrorFn emitError, mlir::Type type)
Definition TypeHelper.h:137
bool isValidConstReadType(Type type)
LogicalResult verifyArrayDimSizes(EmitErrorFn emitError, ArrayRef< Attribute > dimensionSizes)
static bool isEqual(const T &lhs, const T &rhs)
Definition TypeHelper.h:189
static unsigned getHashValue(const T &val)
Definition TypeHelper.h:185