LLZK 0.1.0
Veridise's ZK Language IR
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
17#include <mlir/CAPI/IR.h>
18#include <mlir/CAPI/Pass.h>
19#include <mlir/CAPI/Registration.h>
20#include <mlir/CAPI/Wrap.h>
21#include <mlir/IR/Attributes.h>
22#include <mlir/IR/BuiltinAttributes.h>
23
24#include <mlir-c/IR.h>
25#include <mlir-c/Pass.h>
26
27#include <llvm/ADT/SmallVectorExtras.h>
28
29using namespace llzk::function;
30using namespace mlir;
31using namespace llzk;
32
34
35static NamedAttribute unwrap(MlirNamedAttribute attr) {
36 return NamedAttribute(unwrap(attr.name), unwrap(attr.attribute));
37}
38
39//===----------------------------------------------------------------------===//
40// FuncDefOp
41//===----------------------------------------------------------------------===//
42
46 MlirLocation location, MlirStringRef name, MlirType funcType, intptr_t numAttrs,
47 MlirNamedAttribute const *attrs, intptr_t numArgAttrs, MlirAttribute const *argAttrs
48) {
49 SmallVector<NamedAttribute> attrsSto;
50 SmallVector<Attribute> argAttrsSto;
51 SmallVector<DictionaryAttr> unwrappedArgAttrs =
52 llvm::map_to_vector(unwrapList(numArgAttrs, argAttrs, argAttrsSto), [](auto attr) {
53 return llvm::cast<DictionaryAttr>(attr);
54 });
55 return wrap(
57 unwrap(location), unwrap(name), llvm::cast<FunctionType>(unwrap(funcType)),
58 unwrapList(numAttrs, attrs, attrsSto), unwrappedArgAttrs
59 )
60 );
61}
62
63bool llzkOperationIsAFuncDefOp(MlirOperation op) { return llvm::isa<FuncDefOp>(unwrap(op)); }
64
66 return mlir::unwrap_cast<FuncDefOp>(op).hasAllowConstraintAttr();
67}
68
69void llzkFuncDefOpSetAllowConstraintAttr(MlirOperation op, bool value) {
70 mlir::unwrap_cast<FuncDefOp>(op).setAllowConstraintAttr(value);
71}
72
74 return mlir::unwrap_cast<FuncDefOp>(op).hasAllowWitnessAttr();
75}
76
77void llzkFuncDefOpSetAllowWitnessAttr(MlirOperation op, bool value) {
78 mlir::unwrap_cast<FuncDefOp>(op).setAllowWitnessAttr(value);
79}
80
81bool llzkFuncDefOpGetHasArgIsPub(MlirOperation op, unsigned argNo) {
82 return mlir::unwrap_cast<FuncDefOp>(op).hasArgPublicAttr(argNo);
83}
84
85MlirAttribute llzkFuncDefOpGetFullyQualifiedName(MlirOperation op) {
86 return wrap(mlir::unwrap_cast<FuncDefOp>(op).getFullyQualifiedName());
87}
88
89bool llzkFuncDefOpGetNameIsCompute(MlirOperation op) {
90 return mlir::unwrap_cast<FuncDefOp>(op).nameIsCompute();
91}
92
93bool llzkFuncDefOpGetNameIsConstrain(MlirOperation op) {
94 return mlir::unwrap_cast<FuncDefOp>(op).nameIsConstrain();
95}
96
97bool llzkFuncDefOpGetIsInStruct(MlirOperation op) {
98 return mlir::unwrap_cast<FuncDefOp>(op).isInStruct();
99}
100
101bool llzkFuncDefOpGetIsStructCompute(MlirOperation op) {
102 return mlir::unwrap_cast<FuncDefOp>(op).isStructCompute();
103}
104
105bool llzkFuncDefOpGetIsStructConstrain(MlirOperation op) {
106 return mlir::unwrap_cast<FuncDefOp>(op).isStructConstrain();
107}
108
111MlirValue llzkFuncDefOpGetSelfValueFromCompute(MlirOperation op) {
112 return wrap(mlir::unwrap_cast<FuncDefOp>(op).getSelfValueFromCompute());
113}
114
117MlirValue llzkFuncDefOpGetSelfValueFromConstrain(MlirOperation op) {
118 return wrap(mlir::unwrap_cast<FuncDefOp>(op).getSelfValueFromConstrain());
119}
120
123 return wrap(mlir::unwrap_cast<FuncDefOp>(op).getSingleResultTypeOfCompute());
124}
125
126//===----------------------------------------------------------------------===//
127// CallOp
128//===----------------------------------------------------------------------===//
129
130static auto unwrapCallee(MlirOperation op) { return llvm::cast<FuncDefOp>(unwrap(op)); }
131
132static auto unwrapDims(MlirAttribute attr) { return llvm::cast<DenseI32ArrayAttr>(unwrap(attr)); }
133
134static auto unwrapName(MlirAttribute attr) { return llvm::cast<SymbolRefAttr>(unwrap(attr)); }
135
137 CallOp, intptr_t numResults, MlirType const *results, MlirAttribute name, intptr_t numOperands,
138 MlirValue const *operands
139) {
140 SmallVector<Type> resultsSto;
141 SmallVector<Value> operandsSto;
142 return wrap(
144 builder, location, unwrapList(numResults, results, resultsSto), unwrapName(name),
145 unwrapList(numOperands, operands, operandsSto)
146 )
147 );
148}
149
151 CallOp, ToCallee, MlirOperation callee, intptr_t numOperands, MlirValue const *operands
152) {
153 SmallVector<Value> operandsSto;
154 return wrap(
156 builder, location, unwrapCallee(callee), unwrapList(numOperands, operands, operandsSto)
157 )
158 );
159}
160
162 CallOp, WithMapOperands, intptr_t numResults, MlirType const *results, MlirAttribute name,
163 intptr_t numMapOperands, MlirValueRange const *mapOperands, MlirAttribute numDimsPerMap,
164 intptr_t numArgOperands, MlirValue const *argOperands
165) {
166 SmallVector<Type> resultsSto;
167 SmallVector<Value> argOperandsSto;
168 MapOperandsHelper<> mapOperandsHelper(numMapOperands, mapOperands);
169 return wrap(
171 builder, location, unwrapList(numResults, results, resultsSto), unwrapName(name),
172 *mapOperandsHelper, unwrapDims(numDimsPerMap),
173 unwrapList(numArgOperands, argOperands, argOperandsSto)
174 )
175 );
176}
177
179 CallOp, WithMapOperandsAndDims, intptr_t numResults, MlirType const *results,
180 MlirAttribute name, intptr_t numMapOperands, MlirValueRange const *mapOperands,
181 intptr_t numDimsPermMapLength, int32_t const *numDimsPerMap, intptr_t numArgOperands,
182 MlirValue const *argOperands
183) {
184 SmallVector<Type> resultsSto;
185 SmallVector<Value> argOperandsSto;
186 MapOperandsHelper<> mapOperandsHelper(numMapOperands, mapOperands);
187 return wrap(
189 builder, location, unwrapList(numResults, results, resultsSto), unwrapName(name),
190 *mapOperandsHelper, ArrayRef(numDimsPerMap, numDimsPermMapLength),
191 unwrapList(numArgOperands, argOperands, argOperandsSto)
192 )
193 );
194}
195
197 CallOp, ToCalleeWithMapOperands, MlirOperation callee, intptr_t numMapOperands,
198 MlirValueRange const *mapOperands, MlirAttribute numDimsPerMap, intptr_t numArgOperands,
199 MlirValue const *argOperands
200) {
201 SmallVector<Value> argOperandsSto;
202 MapOperandsHelper<> mapOperandsHelper(numMapOperands, mapOperands);
203 return wrap(
205 builder, location, unwrapCallee(callee), *mapOperandsHelper, unwrapDims(numDimsPerMap),
206 unwrapList(numArgOperands, argOperands, argOperandsSto)
207 )
208 );
209}
210
212 CallOp, ToCalleeWithMapOperandsAndDims, MlirOperation callee, intptr_t numMapOperands,
213 MlirValueRange const *mapOperands, intptr_t numDimsPermMapLength, int32_t const *numDimsPerMap,
214 intptr_t numArgOperands, MlirValue const *argOperands
215) {
216 SmallVector<Value> argOperandsSto;
217 MapOperandsHelper<> mapOperandsHelper(numMapOperands, mapOperands);
218 return wrap(
220 builder, location, unwrapCallee(callee), *mapOperandsHelper,
221 ArrayRef(numDimsPerMap, numDimsPermMapLength),
222 unwrapList(numArgOperands, argOperands, argOperandsSto)
223 )
224 );
225}
226
227bool llzkOperationIsACallOp(MlirOperation op) { return llvm::isa<CallOp>(unwrap(op)); }
228
229MlirType llzkCallOpGetCalleeType(MlirOperation op) {
230 return wrap(mlir::unwrap_cast<CallOp>(op).getCalleeType());
231}
232
233bool llzkCallOpGetCalleeIsCompute(MlirOperation op) {
234 return mlir::unwrap_cast<CallOp>(op).calleeIsCompute();
235}
236
237bool llzkCallOpGetCalleeIsConstrain(MlirOperation op) {
238 return mlir::unwrap_cast<CallOp>(op).calleeIsConstrain();
239}
240
242 return mlir::unwrap_cast<CallOp>(op).calleeIsStructCompute();
243}
244
246 return mlir::unwrap_cast<CallOp>(op).calleeIsStructConstrain();
247}
248
251MlirValue llzkCallOpGetSelfValueFromCompute(MlirOperation op) {
252 return wrap(mlir::unwrap_cast<CallOp>(op).getSelfValueFromCompute());
253}
254
257MlirValue llzkCallOpGetSelfValueFromConstrain(MlirOperation op) {
258 return wrap(mlir::unwrap_cast<CallOp>(op).getSelfValueFromConstrain());
259}
260
261MlirType llzkCallOpGetSingleResultTypeOfCompute(MlirOperation op) {
262 return wrap(mlir::unwrap_cast<CallOp>(op).getSingleResultTypeOfCompute());
263}
bool llzkFuncDefOpGetHasAllowWitnessAttr(MlirOperation op)
Definition Function.cpp:73
MlirType llzkFuncDefOpGetSingleResultTypeOfCompute(MlirOperation op)
Assuming the function is the compute function returns its StructType result.
Definition Function.cpp:122
MlirType llzkCallOpGetCalleeType(MlirOperation op)
Returns the FunctionType of the callee.
Definition Function.cpp:229
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:45
bool llzkFuncDefOpGetNameIsConstrain(MlirOperation op)
Definition Function.cpp:93
bool llzkCallOpGetCalleeIsStructConstrain(MlirOperation op)
Definition Function.cpp:245
bool llzkFuncDefOpGetIsInStruct(MlirOperation op)
Definition Function.cpp:97
MlirValue llzkFuncDefOpGetSelfValueFromCompute(MlirOperation op)
Return the "self" value (i.e.
Definition Function.cpp:111
bool llzkFuncDefOpGetHasArgIsPub(MlirOperation op, unsigned argNo)
Definition Function.cpp:81
bool llzkFuncDefOpGetIsStructCompute(MlirOperation op)
Definition Function.cpp:101
MlirValue llzkFuncDefOpGetSelfValueFromConstrain(MlirOperation op)
Return the "self" value (i.e.
Definition Function.cpp:117
bool llzkCallOpGetCalleeIsConstrain(MlirOperation op)
Definition Function.cpp:237
MlirValue llzkCallOpGetSelfValueFromCompute(MlirOperation op)
Return the "self" value (i.e.
Definition Function.cpp:251
void llzkFuncDefOpSetAllowConstraintAttr(MlirOperation op, bool value)
Sets the allow_constraint attribute in the FuncDefOp operation.
Definition Function.cpp:69
bool llzkCallOpGetCalleeIsStructCompute(MlirOperation op)
Definition Function.cpp:241
MlirType llzkCallOpGetSingleResultTypeOfCompute(MlirOperation op)
Assuming the callee is the compute function, returns its StructType result.
Definition Function.cpp:261
bool llzkFuncDefOpGetIsStructConstrain(MlirOperation op)
Definition Function.cpp:105
bool llzkOperationIsAFuncDefOp(MlirOperation op)
Definition Function.cpp:63
MlirValue llzkCallOpGetSelfValueFromConstrain(MlirOperation op)
Return the "self" value (i.e.
Definition Function.cpp:257
bool llzkCallOpGetCalleeIsCompute(MlirOperation op)
Definition Function.cpp:233
bool llzkOperationIsACallOp(MlirOperation op)
Definition Function.cpp:227
bool llzkFuncDefOpGetNameIsCompute(MlirOperation op)
Definition Function.cpp:89
bool llzkFuncDefOpGetHasAllowConstraintAttr(MlirOperation op)
Definition Function.cpp:65
MlirAttribute llzkFuncDefOpGetFullyQualifiedName(MlirOperation op)
Returns the fully qualified name of the function.
Definition Function.cpp:85
void llzkFuncDefOpSetAllowWitnessAttr(MlirOperation op, bool value)
Sets the allow_witness attribute in the FuncDefOp operation.
Definition Function.cpp:77
MLIR_DEFINE_CAPI_DIALECT_REGISTRATION(Polymorphic, llzk__polymorphic, llzk::polymorphic::PolymorphicDialect) MlirType llzkTypeVarTypeGet(MlirContext ctx
MlirStringRef name
Definition Poly.cpp:48
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