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 { LHS, RHS };
149static inline mlir::raw_ostream &operator<<(mlir::raw_ostream &os, const Side &val) {
150 switch (val) {
151 case Side::LHS:
152 os << "LHS";
153 break;
154 case Side::RHS:
155 os << "RHS";
156 break;
157 }
158 return os;
159}
160
161inline Side reverse(Side in) {
162 switch (in) {
163 case Side::LHS:
164 return Side::RHS;
165 case Side::RHS:
166 return Side::LHS;
167 }
168 return in;
169}
170
171} // namespace llzk
172
173namespace llvm {
174template <> struct DenseMapInfo<llzk::Side> {
175 using T = llzk::Side;
176 static unsigned getHashValue(const T &val) {
177 using UT = std::underlying_type_t<T>;
178 return llvm::DenseMapInfo<UT>::getHashValue(static_cast<UT>(val));
179 }
180 static bool isEqual(const T &lhs, const T &rhs) { return lhs == rhs; }
181};
182} // namespace llvm
183
184namespace llzk {
185
186bool isDynamic(mlir::IntegerAttr intAttr);
187
195
198uint64_t computeEmitEqCardinality(mlir::Type type);
199
208using UnificationMap = mlir::DenseMap<std::pair<mlir::SymbolRefAttr, Side>, mlir::Attribute>;
209
213 const mlir::ArrayRef<mlir::Attribute> &lhsParams,
214 const mlir::ArrayRef<mlir::Attribute> &rhsParams, UnificationMap *unifications = nullptr
215);
216
220 const mlir::ArrayAttr &lhsParams, const mlir::ArrayAttr &rhsParams,
221 UnificationMap *unifications = nullptr
222);
223
228 mlir::ArrayRef<llvm::StringRef> rhsReversePrefix = {}, UnificationMap *unifications = nullptr
229);
230
235 mlir::ArrayRef<llvm::StringRef> rhsReversePrefix = {}, UnificationMap *unifications = nullptr
236);
237
241 pod::PodType lhs, pod::PodType rhs, mlir::ArrayRef<llvm::StringRef> rhsReversePrefix = {},
242 UnificationMap *unifications = nullptr
243);
244
248 mlir::FunctionType lhs, mlir::FunctionType rhs,
249 mlir::ArrayRef<llvm::StringRef> rhsReversePrefix = {}, UnificationMap *unifications = nullptr
250);
251
255 mlir::Type lhs, mlir::Type rhs, mlir::ArrayRef<llvm::StringRef> rhsReversePrefix = {},
256 UnificationMap *unifications = nullptr
257);
258
261template <typename Iter1, typename Iter2>
262inline bool typeListsUnify(
263 Iter1 lhs, Iter2 rhs, mlir::ArrayRef<llvm::StringRef> rhsReversePrefix = {},
264 UnificationMap *unifications = nullptr
265) {
266 return (lhs.size() == rhs.size()) &&
267 std::equal(lhs.begin(), lhs.end(), rhs.begin(), [&](mlir::Type a, mlir::Type b) {
268 return typesUnify(a, b, rhsReversePrefix, unifications);
269 });
270}
271
272template <typename Iter1, typename Iter2>
274 Iter1 lhs, Iter2 rhs, mlir::ArrayRef<llvm::StringRef> rhsReversePrefix = {},
275 UnificationMap *unifications = nullptr
276) {
277 return lhs.size() == 1 && rhs.size() == 1 &&
278 typesUnify(lhs.front(), rhs.front(), rhsReversePrefix, unifications);
279}
280
289 mlir::Type oldTy, mlir::Type newTy,
290 llvm::function_ref<bool(mlir::Type oldTy, mlir::Type newTy)> knownOldToNew = nullptr
291);
292
293template <typename TypeClass> inline TypeClass getIfSingleton(mlir::TypeRange types) {
294 return (types.size() == 1) ? llvm::dyn_cast<TypeClass>(types.front()) : nullptr;
295}
296
297template <typename TypeClass> inline TypeClass getAtIndex(mlir::TypeRange types, size_t index) {
298 return (types.size() > index) ? llvm::dyn_cast<TypeClass>(types[index]) : nullptr;
299}
300
302mlir::FailureOr<mlir::IntegerAttr> forceIntType(mlir::IntegerAttr attr, EmitErrorFn emitError);
303
305mlir::FailureOr<mlir::Attribute> forceIntAttrType(mlir::Attribute attr, EmitErrorFn emitError);
306
308mlir::FailureOr<llvm::SmallVector<mlir::Attribute>>
309forceIntAttrTypes(llvm::ArrayRef<mlir::Attribute> attrList, EmitErrorFn emitError);
310
312mlir::LogicalResult verifyIntAttrType(EmitErrorFn emitError, mlir::Attribute in);
313
315mlir::LogicalResult verifyAffineMapAttrType(EmitErrorFn emitError, mlir::Attribute in);
316
318mlir::LogicalResult verifyStructTypeParams(EmitErrorFn emitError, mlir::ArrayAttr params);
319
321mlir::LogicalResult
322verifyArrayDimSizes(EmitErrorFn emitError, mlir::ArrayRef<mlir::Attribute> dimensionSizes);
323
325mlir::LogicalResult verifyArrayType(
326 EmitErrorFn emitError, mlir::Type elementType, mlir::ArrayRef<mlir::Attribute> dimensionSizes
327);
328
334mlir::LogicalResult verifySubArrayType(
335 EmitErrorFn emitError, array::ArrayType arrayType, array::ArrayType subArrayType
336);
337
341mlir::LogicalResult verifySubArrayOrElementType(
342 EmitErrorFn emitError, array::ArrayType arrayType, mlir::Type subArrayOrElemType
343);
344
347bool isFeltOrSimpleFeltAggregate(mlir::Type ty);
348
352bool isValidMainSignalType(mlir::Type pType);
353
354} // 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:262
bool isConcreteType(Type type, bool allowStructParams)
bool isValidArrayElemType(Type type)
TypeClass getIfSingleton(mlir::TypeRange types)
Definition TypeHelper.h:293
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:273
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:208
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:297
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:161
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:180
static unsigned getHashValue(const T &val)
Definition TypeHelper.h:176