LLZK 0.1.0
An open-source IR for Zero Knowledge (ZK) circuits
Loading...
Searching...
No Matches
Typing.cpp
Go to the documentation of this file.
1//===-- Typing.cpp - C API for llzk types -----------------------*- 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
11
12#include "llzk-c/Typing.h"
13
14#include <mlir/CAPI/IR.h>
15#include <mlir/CAPI/Support.h>
16#include <mlir/CAPI/Wrap.h>
17
18using namespace llzk;
19using namespace mlir;
20
21void llzkAssertValidAttrForParamOfType(MlirAttribute attr) {
23}
24
25bool llzkIsValidType(MlirType type) { return isValidType(unwrap(type)); }
26
27bool llzkIsValidColumnType(MlirType type, MlirOperation op) {
28 SymbolTableCollection sc;
29 return isValidColumnType(unwrap(type), sc, unwrap(op));
30}
31
32bool llzkIsValidGlobalType(MlirType type) { return isValidGlobalType(unwrap(type)); }
33
34bool llzkIsValidEmitEqType(MlirType type) { return isValidEmitEqType(unwrap(type)); }
35
36bool llzkIsValidConstReadType(MlirType type) { return isValidConstReadType(unwrap(type)); }
37
38bool llzkIsValidArrayElemType(MlirType type) { return isValidArrayElemType(unwrap(type)); }
39
40bool llzkIsValidArrayType(MlirType type) { return isValidArrayType(unwrap(type)); }
41
42bool llzkIsConcreteType(MlirType type, bool allowStructParams) {
43 return isConcreteType(unwrap(type), allowStructParams);
44}
45
46bool llzkHasAffineMapAttr(MlirType type) { return hasAffineMapAttr(unwrap(type)); }
47
49 intptr_t numLhsParams, MlirAttribute const *lhsParams, intptr_t numRhsParams,
50 MlirAttribute const *rhsParams
51) {
52 SmallVector<Attribute> lhsSto, rhsSto;
53 return typeParamsUnify(
54 unwrapList(numLhsParams, lhsParams, lhsSto), unwrapList(numRhsParams, rhsParams, rhsSto)
55 );
56}
57
58bool llzkArrayAttrTypeParamsUnify(MlirAttribute lhsParams, MlirAttribute rhsParams) {
59 return typeParamsUnify(
60 llvm::cast<ArrayAttr>(unwrap(lhsParams)), llvm::cast<ArrayAttr>(unwrap(rhsParams))
61 );
62}
63
65 MlirType lhs, MlirType rhs, intptr_t nRhsReversePrefix, MlirStringRef const *rhsReversePrefix
66) {
67 SmallVector<StringRef> rhsReversePrefixSto;
68 return llzk::typesUnify(
69 unwrap(lhs), unwrap(rhs), unwrapList(nRhsReversePrefix, rhsReversePrefix, rhsReversePrefixSto)
70 );
71}
72
74 MlirType oldTy, MlirType newTy, bool (*knownOldToNew)(MlirType, MlirType, void *),
75 void *userData
76) {
78 unwrap(oldTy), unwrap(newTy), [knownOldToNew, userData](auto lhs, auto rhs) {
79 return knownOldToNew(wrap(lhs), wrap(rhs), userData);
80 }
81 );
82}
83
84MlirAttribute llzkForceIntAttrType(MlirAttribute attr, MlirLocation loc) {
85 auto emitErrorFn = [&loc]() { return InFlightDiagnosticWrapper(mlir::emitError(unwrap(loc))); };
86 FailureOr<Attribute> forced = forceIntAttrType(unwrap(attr), emitErrorFn);
87 return wrap(succeeded(forced) ? *forced : nullptr);
88}
bool llzkIsConcreteType(MlirType type, bool allowStructParams)
Return false if the type contains any of the following:
Definition Typing.cpp:42
bool llzkIsValidArrayType(MlirType type)
Checks if the type is a LLZK Array and it also contains a valid LLZK type.
Definition Typing.cpp:40
bool llzkIsValidGlobalType(MlirType type)
valid types: isValidType() - {TypeVarType} - {types with variable parameters}
Definition Typing.cpp:32
MlirAttribute llzkForceIntAttrType(MlirAttribute attr, MlirLocation loc)
Convert any IntegerAttr with a type other than IndexType to use IndexType.
Definition Typing.cpp:84
bool llzkIsValidType(MlirType type)
valid types: {I1, Index, String, FeltType, StructType, ArrayType, TypeVarType}
Definition Typing.cpp:25
bool llzkIsValidConstReadType(MlirType type)
valid types: {I1, Index, FeltType, TypeVarType}
Definition Typing.cpp:36
bool llzkHasAffineMapAttr(MlirType type)
Return true iff the given type contains an AffineMapAttr.
Definition Typing.cpp:46
bool llzkArrayAttrTypeParamsUnify(MlirAttribute lhsParams, MlirAttribute rhsParams)
Return true iff the two ArrayAttr instances containing StructType or ArrayType parameters are equival...
Definition Typing.cpp:58
bool llzkIsMoreConcreteUnification(MlirType oldTy, MlirType newTy, bool(*knownOldToNew)(MlirType, MlirType, void *), void *userData)
Return true iff the types unify and newTy is "more concrete" than oldTy.
Definition Typing.cpp:73
bool llzkTypesUnify(MlirType lhs, MlirType rhs, intptr_t nRhsReversePrefix, MlirStringRef const *rhsReversePrefix)
Return true iff the two Type instances are equivalent or could be equivalent after full instantiation...
Definition Typing.cpp:64
bool llzkTypeParamsUnify(intptr_t numLhsParams, MlirAttribute const *lhsParams, intptr_t numRhsParams, MlirAttribute const *rhsParams)
Return true iff the two ArrayRef instances containing StructType or ArrayType parameters are equivale...
Definition Typing.cpp:48
void llzkAssertValidAttrForParamOfType(MlirAttribute attr)
This function asserts that the given Attribute kind is legal within the LLZK types that can contain A...
Definition Typing.cpp:21
bool llzkIsValidArrayElemType(MlirType type)
valid types: isValidType() - {ArrayType}
Definition Typing.cpp:38
bool llzkIsValidEmitEqType(MlirType type)
valid types: isValidType() - {String, StructType} (excluded via any type parameter nesting)
Definition Typing.cpp:34
bool llzkIsValidColumnType(MlirType type, MlirOperation op)
valid types: {FeltType, StructType (with columns), ArrayType (that contains a valid column type)}
Definition Typing.cpp:27
Wrapper around InFlightDiagnostic that can either be a regular InFlightDiagnostic or a special versio...
Definition ErrorHelper.h:26
void assertValidAttrForParamOfType(Attribute attr)
FailureOr< Attribute > forceIntAttrType(Attribute attr, EmitErrorFn emitError)
bool isValidArrayType(Type type)
bool isConcreteType(Type type, bool allowStructParams)
bool isValidArrayElemType(Type type)
bool isValidGlobalType(Type type)
bool isValidColumnType(Type type, SymbolTableCollection &symbolTable, Operation *op)
bool isValidEmitEqType(Type type)
bool isValidType(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 isMoreConcreteUnification(Type oldTy, Type newTy, llvm::function_ref< bool(Type oldTy, Type newTy)> knownOldToNew)
bool hasAffineMapAttr(Type type)
bool isValidConstReadType(Type type)