LLZK 0.1.0
An open-source IR for Zero Knowledge (ZK) circuits
Loading...
Searching...
No Matches
Function.cpp
Go to the documentation of this file.
1//===-- Function.cpp - Function dialect C API implementation ----*- 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#include "llzk/CAPI/Builder.h"
11#include "llzk/CAPI/Support.h"
14
16#include "llzk-c/Support.h"
17
18#include <mlir/CAPI/IR.h>
19#include <mlir/CAPI/Pass.h>
20#include <mlir/CAPI/Registration.h>
21#include <mlir/CAPI/Wrap.h>
22#include <mlir/IR/Attributes.h>
23#include <mlir/IR/BuiltinAttributes.h>
24
25#include <mlir-c/IR.h>
26#include <mlir-c/Pass.h>
27
28#include <llvm/ADT/SmallVectorExtras.h>
29
30using namespace mlir;
31using namespace llzk;
32using namespace llzk::function;
33
34MLIR_DEFINE_CAPI_DIALECT_REGISTRATION(Function, llzk__function, FunctionDialect)
35
36static NamedAttribute unwrap(MlirNamedAttribute attr) {
37 return NamedAttribute(unwrap(attr.name), unwrap(attr.attribute));
38}
39
40//===----------------------------------------------------------------------===//
41// FuncDefOp
42//===----------------------------------------------------------------------===//
43
47 MlirLocation location, MlirStringRef name, MlirType funcType, intptr_t numAttrs,
48 MlirNamedAttribute const *attrs, intptr_t numArgAttrs, MlirAttribute const *argAttrs
49) {
50 SmallVector<NamedAttribute> attrsSto;
51 SmallVector<Attribute> argAttrsSto;
52 SmallVector<DictionaryAttr> unwrappedArgAttrs =
53 llvm::map_to_vector(unwrapList(numArgAttrs, argAttrs, argAttrsSto), [](auto attr) {
54 return llvm::cast<DictionaryAttr>(attr);
55 });
56 return wrap(
58 unwrap(location), unwrap(name), llvm::cast<FunctionType>(unwrap(funcType)),
59 unwrapList(numAttrs, attrs, attrsSto), unwrappedArgAttrs
60 )
61 );
62}
63
64bool llzkOperationIsAFuncDefOp(MlirOperation op) { return llvm::isa<FuncDefOp>(unwrap(op)); }
65
67 return unwrap_cast<FuncDefOp>(op).hasAllowConstraintAttr();
68}
69
70void llzkFuncDefOpSetAllowConstraintAttr(MlirOperation op, bool value) {
71 unwrap_cast<FuncDefOp>(op).setAllowConstraintAttr(value);
72}
73
75 return unwrap_cast<FuncDefOp>(op).hasAllowWitnessAttr();
76}
77
78void llzkFuncDefOpSetAllowWitnessAttr(MlirOperation op, bool value) {
79 unwrap_cast<FuncDefOp>(op).setAllowWitnessAttr(value);
80}
81
83 return unwrap_cast<FuncDefOp>(op).hasAllowNonNativeFieldOpsAttr();
84}
85
86void llzkFuncDefOpSetAllowNonNativeFieldOpsAttr(MlirOperation op, bool value) {
87 unwrap_cast<FuncDefOp>(op).setAllowNonNativeFieldOpsAttr(value);
88}
89
90bool llzkFuncDefOpGetHasArgIsPub(MlirOperation op, unsigned argNo) {
91 return unwrap_cast<FuncDefOp>(op).hasArgPublicAttr(argNo);
92}
93
94MlirAttribute llzkFuncDefOpGetFullyQualifiedName(MlirOperation op) {
95 return wrap(unwrap_cast<FuncDefOp>(op).getFullyQualifiedName());
96}
97
98bool llzkFuncDefOpGetNameIsCompute(MlirOperation op) {
99 return unwrap_cast<FuncDefOp>(op).nameIsCompute();
100}
101
102bool llzkFuncDefOpGetNameIsConstrain(MlirOperation op) {
103 return unwrap_cast<FuncDefOp>(op).nameIsConstrain();
104}
105
106bool llzkFuncDefOpGetIsInStruct(MlirOperation op) {
107 return unwrap_cast<FuncDefOp>(op).isInStruct();
108}
109
110bool llzkFuncDefOpGetIsStructCompute(MlirOperation op) {
111 return unwrap_cast<FuncDefOp>(op).isStructCompute();
112}
113
114bool llzkFuncDefOpGetIsStructConstrain(MlirOperation op) {
115 return unwrap_cast<FuncDefOp>(op).isStructConstrain();
116}
117
120MlirValue llzkFuncDefOpGetSelfValueFromCompute(MlirOperation op) {
121 return wrap(unwrap_cast<FuncDefOp>(op).getSelfValueFromCompute());
122}
123
126MlirValue llzkFuncDefOpGetSelfValueFromConstrain(MlirOperation op) {
127 return wrap(unwrap_cast<FuncDefOp>(op).getSelfValueFromConstrain());
128}
129
132 return wrap(unwrap_cast<FuncDefOp>(op).getSingleResultTypeOfCompute());
133}
134
135//===----------------------------------------------------------------------===//
136// CallOp
137//===----------------------------------------------------------------------===//
138
139static auto unwrapCallee(MlirOperation op) { return llvm::cast<FuncDefOp>(unwrap(op)); }
140
141static auto unwrapDims(MlirAttribute attr) { return llvm::cast<DenseI32ArrayAttr>(unwrap(attr)); }
142
143static auto unwrapName(MlirAttribute attr) { return llvm::cast<SymbolRefAttr>(unwrap(attr)); }
144
146 CallOp, intptr_t numResults, MlirType const *results, MlirAttribute name, intptr_t numOperands,
147 MlirValue const *operands
148) {
149 SmallVector<Type> resultsSto;
150 SmallVector<Value> operandsSto;
151 return wrap(
153 builder, location, unwrapList(numResults, results, resultsSto), unwrapName(name),
154 unwrapList(numOperands, operands, operandsSto)
155 )
156 );
157}
158
160 CallOp, ToCallee, MlirOperation callee, intptr_t numOperands, MlirValue const *operands
161) {
162 SmallVector<Value> operandsSto;
163 return wrap(
165 builder, location, unwrapCallee(callee), unwrapList(numOperands, operands, operandsSto)
166 )
167 );
168}
169
171 CallOp, WithMapOperands, intptr_t numResults, MlirType const *results, MlirAttribute name,
172 LlzkAffineMapOperandsBuilder mapOperands, intptr_t numArgOperands, MlirValue const *argOperands
173) {
174 SmallVector<Type> resultsSto;
175 SmallVector<Value> argOperandsSto;
176 MapOperandsHelper<> mapOperandsHelper(mapOperands.nMapOperands, mapOperands.mapOperands);
177 auto numDimsPerMap =
178 llzkAffineMapOperandsBuilderGetDimsPerMapAttr(mapOperands, mlirLocationGetContext(location));
179 return wrap(
181 builder, location, unwrapList(numResults, results, resultsSto), unwrapName(name),
182 *mapOperandsHelper, unwrapDims(numDimsPerMap),
183 unwrapList(numArgOperands, argOperands, argOperandsSto)
184 )
185 );
186}
187
189 CallOp, ToCalleeWithMapOperands, MlirOperation callee, LlzkAffineMapOperandsBuilder mapOperands,
190 intptr_t numArgOperands, MlirValue const *argOperands
191) {
192 SmallVector<Value> argOperandsSto;
193 MapOperandsHelper<> mapOperandsHelper(mapOperands.nMapOperands, mapOperands.mapOperands);
194 auto numDimsPerMap =
195 llzkAffineMapOperandsBuilderGetDimsPerMapAttr(mapOperands, mlirLocationGetContext(location));
196 return wrap(
198 builder, location, unwrapCallee(callee), *mapOperandsHelper, unwrapDims(numDimsPerMap),
199 unwrapList(numArgOperands, argOperands, argOperandsSto)
200 )
201 );
202}
203
204bool llzkOperationIsACallOp(MlirOperation op) { return llvm::isa<CallOp>(unwrap(op)); }
205
206MlirType llzkCallOpGetCalleeType(MlirOperation op) {
207 return wrap(unwrap_cast<CallOp>(op).getCalleeType());
208}
209
210bool llzkCallOpGetCalleeIsCompute(MlirOperation op) {
211 return unwrap_cast<CallOp>(op).calleeIsCompute();
212}
213
214bool llzkCallOpGetCalleeIsConstrain(MlirOperation op) {
215 return unwrap_cast<CallOp>(op).calleeIsConstrain();
216}
217
219 return unwrap_cast<CallOp>(op).calleeIsStructCompute();
220}
221
223 return unwrap_cast<CallOp>(op).calleeIsStructConstrain();
224}
225
228MlirValue llzkCallOpGetSelfValueFromCompute(MlirOperation op) {
229 return wrap(unwrap_cast<CallOp>(op).getSelfValueFromCompute());
230}
231
234MlirValue llzkCallOpGetSelfValueFromConstrain(MlirOperation op) {
235 return wrap(unwrap_cast<CallOp>(op).getSelfValueFromConstrain());
236}
237
238MlirType llzkCallOpGetSingleResultTypeOfCompute(MlirOperation op) {
239 return wrap(unwrap_cast<CallOp>(op).getSingleResultTypeOfCompute());
240}
bool llzkFuncDefOpGetHasAllowWitnessAttr(MlirOperation op)
Definition Function.cpp:74
MlirType llzkFuncDefOpGetSingleResultTypeOfCompute(MlirOperation op)
Assuming the function is the compute function returns its StructType result.
Definition Function.cpp:131
MlirType llzkCallOpGetCalleeType(MlirOperation op)
Returns the FunctionType of the callee.
Definition Function.cpp:206
MlirOperation llzkFuncDefOpCreateWithAttrsAndArgAttrs(MlirLocation location, MlirStringRef name, MlirType funcType, intptr_t numAttrs, MlirNamedAttribute const *attrs, intptr_t numArgAttrs, MlirAttribute const *argAttrs)
Creates a FuncDefOp with the given attributes and argument attributes.
Definition Function.cpp:46
bool llzkFuncDefOpGetNameIsConstrain(MlirOperation op)
Definition Function.cpp:102
bool llzkFuncDefOpGetHasAllowNonNativeFieldOpsAttr(MlirOperation op)
Definition Function.cpp:82
bool llzkCallOpGetCalleeIsStructConstrain(MlirOperation op)
Definition Function.cpp:222
bool llzkFuncDefOpGetIsInStruct(MlirOperation op)
Definition Function.cpp:106
MlirValue llzkFuncDefOpGetSelfValueFromCompute(MlirOperation op)
Return the "self" value (i.e.
Definition Function.cpp:120
bool llzkFuncDefOpGetHasArgIsPub(MlirOperation op, unsigned argNo)
Definition Function.cpp:90
bool llzkFuncDefOpGetIsStructCompute(MlirOperation op)
Definition Function.cpp:110
MlirValue llzkFuncDefOpGetSelfValueFromConstrain(MlirOperation op)
Return the "self" value (i.e.
Definition Function.cpp:126
bool llzkCallOpGetCalleeIsConstrain(MlirOperation op)
Definition Function.cpp:214
MlirValue llzkCallOpGetSelfValueFromCompute(MlirOperation op)
Return the "self" value (i.e.
Definition Function.cpp:228
void llzkFuncDefOpSetAllowConstraintAttr(MlirOperation op, bool value)
Sets the allow_constraint attribute in the FuncDefOp operation.
Definition Function.cpp:70
bool llzkCallOpGetCalleeIsStructCompute(MlirOperation op)
Definition Function.cpp:218
MlirType llzkCallOpGetSingleResultTypeOfCompute(MlirOperation op)
Assuming the callee is the compute function, returns its StructType result.
Definition Function.cpp:238
bool llzkFuncDefOpGetIsStructConstrain(MlirOperation op)
Definition Function.cpp:114
bool llzkOperationIsAFuncDefOp(MlirOperation op)
Definition Function.cpp:64
void llzkFuncDefOpSetAllowNonNativeFieldOpsAttr(MlirOperation op, bool value)
Sets the allow_non_native_field_ops attribute in the FuncDefOp operation.
Definition Function.cpp:86
MlirValue llzkCallOpGetSelfValueFromConstrain(MlirOperation op)
Return the "self" value (i.e.
Definition Function.cpp:234
bool llzkCallOpGetCalleeIsCompute(MlirOperation op)
Definition Function.cpp:210
bool llzkOperationIsACallOp(MlirOperation op)
Definition Function.cpp:204
bool llzkFuncDefOpGetNameIsCompute(MlirOperation op)
Definition Function.cpp:98
bool llzkFuncDefOpGetHasAllowConstraintAttr(MlirOperation op)
Definition Function.cpp:66
MlirAttribute llzkFuncDefOpGetFullyQualifiedName(MlirOperation op)
Returns the fully qualified name of the function.
Definition Function.cpp:94
void llzkFuncDefOpSetAllowWitnessAttr(MlirOperation op, bool value)
Sets the allow_witness attribute in the FuncDefOp operation.
Definition Function.cpp:78
MlirAttribute llzkAffineMapOperandsBuilderGetDimsPerMapAttr(LlzkAffineMapOperandsBuilder builder, MlirContext context)
Returns the number of dimensions per map represented as an attribute.
Definition Support.cpp:186
Helper for unwrapping the C arguments for the map operands.
Definition Support.h:36
static FuncDefOp create(::mlir::Location location, ::llvm::StringRef name, ::mlir::FunctionType type, ::llvm::ArrayRef<::mlir::NamedAttribute > attrs={})
#define LLZK_DEFINE_OP_BUILD_METHOD(op,...)
Definition Support.h:27
#define LLZK_DEFINE_SUFFIX_OP_BUILD_METHOD(op, suffix,...)
Definition Support.h:25
mlir::Operation * create(MlirOpBuilder cBuilder, MlirLocation cLocation, Args &&...args)
Creates a new operation using an ODS build method.
Definition Builder.h:41
auto unwrap_cast(auto &from)
Definition Support.h:30
Encapsulates the arguments related to affine maps that are common in operation constructors that supp...
Definition Support.h:105
MlirValueRange * mapOperands
Definition Support.h:109