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 mlir::cast<DictionaryAttr>(attr);
54 });
55 return wrap(FuncDefOp::create(
56 unwrap(location), unwrap(name), mlir::cast<FunctionType>(unwrap(funcType)),
57 unwrapList(numAttrs, attrs, attrsSto), unwrappedArgAttrs
58 ));
59}
60
61bool llzkOperationIsAFuncDefOp(MlirOperation op) { return mlir::isa<FuncDefOp>(unwrap(op)); }
62
64 return mlir::unwrap_cast<FuncDefOp>(op).hasAllowConstraintAttr();
65}
66
67void llzkFuncDefOpSetAllowConstraintAttr(MlirOperation op, bool value) {
68 mlir::unwrap_cast<FuncDefOp>(op).setAllowConstraintAttr(value);
69}
70
72 return mlir::unwrap_cast<FuncDefOp>(op).hasAllowWitnessAttr();
73}
74
75void llzkFuncDefOpSetAllowWitnessAttr(MlirOperation op, bool value) {
76 mlir::unwrap_cast<FuncDefOp>(op).setAllowWitnessAttr(value);
77}
78
79bool llzkFuncDefOpGetHasArgIsPub(MlirOperation op, unsigned argNo) {
80 return mlir::unwrap_cast<FuncDefOp>(op).hasArgPublicAttr(argNo);
81}
82
83MlirAttribute llzkFuncDefOpGetFullyQualifiedName(MlirOperation op) {
84 return wrap(mlir::unwrap_cast<FuncDefOp>(op).getFullyQualifiedName());
85}
86
87bool llzkFuncDefOpGetNameIsCompute(MlirOperation op) {
88 return mlir::unwrap_cast<FuncDefOp>(op).nameIsCompute();
89}
90
91bool llzkFuncDefOpGetNameIsConstrain(MlirOperation op) {
92 return mlir::unwrap_cast<FuncDefOp>(op).nameIsConstrain();
93}
94
95bool llzkFuncDefOpGetIsInStruct(MlirOperation op) {
96 return mlir::unwrap_cast<FuncDefOp>(op).isInStruct();
97}
98
99bool llzkFuncDefOpGetIsStructCompute(MlirOperation op) {
100 return mlir::unwrap_cast<FuncDefOp>(op).isStructCompute();
101}
102
103bool llzkFuncDefOpGetIsStructConstrain(MlirOperation op) {
104 return mlir::unwrap_cast<FuncDefOp>(op).isStructConstrain();
105}
106
109MlirValue llzkFuncDefOpGetSelfValueFromCompute(MlirOperation op) {
110 return wrap(mlir::unwrap_cast<FuncDefOp>(op).getSelfValueFromCompute());
111}
112
115MlirValue llzkFuncDefOpGetSelfValueFromConstrain(MlirOperation op) {
116 return wrap(mlir::unwrap_cast<FuncDefOp>(op).getSelfValueFromConstrain());
117}
118
121 return wrap(mlir::unwrap_cast<FuncDefOp>(op).getSingleResultTypeOfCompute());
122}
123
124//===----------------------------------------------------------------------===//
125// CallOp
126//===----------------------------------------------------------------------===//
127
128static auto unwrapCallee(MlirOperation op) { return mlir::cast<FuncDefOp>(unwrap(op)); }
129
130static auto unwrapDims(MlirAttribute attr) { return mlir::cast<DenseI32ArrayAttr>(unwrap(attr)); }
131
132static auto unwrapName(MlirAttribute attr) { return mlir::cast<SymbolRefAttr>(unwrap(attr)); }
133
135 CallOp, intptr_t numResults, MlirType const *results, MlirAttribute name, intptr_t numOperands,
136 MlirValue const *operands
137) {
138 SmallVector<Type> resultsSto;
139 SmallVector<Value> operandsSto;
140 return wrap(create<CallOp>(
141 builder, location, unwrapList(numResults, results, resultsSto), unwrapName(name),
142 unwrapList(numOperands, operands, operandsSto)
143 ));
144}
145
147 CallOp, ToCallee, MlirOperation callee, intptr_t numOperands, MlirValue const *operands
148) {
149 SmallVector<Value> operandsSto;
150 return wrap(create<CallOp>(
151 builder, location, unwrapCallee(callee), unwrapList(numOperands, operands, operandsSto)
152 ));
153}
154
156 CallOp, WithMapOperands, intptr_t numResults, MlirType const *results, MlirAttribute name,
157 intptr_t numMapOperands, MlirValueRange const *mapOperands, MlirAttribute numDimsPerMap,
158 intptr_t numArgOperands, MlirValue const *argOperands
159) {
160 SmallVector<Type> resultsSto;
161 SmallVector<Value> argOperandsSto;
162 MapOperandsHelper<> mapOperandsHelper(numMapOperands, mapOperands);
163 return wrap(create<CallOp>(
164 builder, location, unwrapList(numResults, results, resultsSto), unwrapName(name),
165 *mapOperandsHelper, unwrapDims(numDimsPerMap),
166 unwrapList(numArgOperands, argOperands, argOperandsSto)
167 ));
168}
169
171 CallOp, WithMapOperandsAndDims, intptr_t numResults, MlirType const *results,
172 MlirAttribute name, intptr_t numMapOperands, MlirValueRange const *mapOperands,
173 intptr_t numDimsPermMapLength, int32_t const *numDimsPerMap, intptr_t numArgOperands,
174 MlirValue const *argOperands
175) {
176 SmallVector<Type> resultsSto;
177 SmallVector<Value> argOperandsSto;
178 MapOperandsHelper<> mapOperandsHelper(numMapOperands, mapOperands);
179 return wrap(create<CallOp>(
180 builder, location, unwrapList(numResults, results, resultsSto), unwrapName(name),
181 *mapOperandsHelper, ArrayRef(numDimsPerMap, numDimsPermMapLength),
182 unwrapList(numArgOperands, argOperands, argOperandsSto)
183 ));
184}
185
187 CallOp, ToCalleeWithMapOperands, MlirOperation callee, intptr_t numMapOperands,
188 MlirValueRange const *mapOperands, MlirAttribute numDimsPerMap, intptr_t numArgOperands,
189 MlirValue const *argOperands
190) {
191 SmallVector<Value> argOperandsSto;
192 MapOperandsHelper<> mapOperandsHelper(numMapOperands, mapOperands);
193 return wrap(create<CallOp>(
194 builder, location, unwrapCallee(callee), *mapOperandsHelper, unwrapDims(numDimsPerMap),
195 unwrapList(numArgOperands, argOperands, argOperandsSto)
196 ));
197}
198
200 CallOp, ToCalleeWithMapOperandsAndDims, MlirOperation callee, intptr_t numMapOperands,
201 MlirValueRange const *mapOperands, intptr_t numDimsPermMapLength, int32_t const *numDimsPerMap,
202 intptr_t numArgOperands, MlirValue const *argOperands
203) {
204 SmallVector<Value> argOperandsSto;
205 MapOperandsHelper<> mapOperandsHelper(numMapOperands, mapOperands);
206 return wrap(create<CallOp>(
207 builder, location, unwrapCallee(callee), *mapOperandsHelper,
208 ArrayRef(numDimsPerMap, numDimsPermMapLength),
209 unwrapList(numArgOperands, argOperands, argOperandsSto)
210 ));
211}
212
213bool llzkOperationIsACallOp(MlirOperation op) { return mlir::isa<CallOp>(unwrap(op)); }
214
215MlirType llzkCallOpGetCalleeType(MlirOperation op) {
216 return wrap(mlir::unwrap_cast<CallOp>(op).getCalleeType());
217}
218
219bool llzkCallOpGetCalleeIsCompute(MlirOperation op) {
220 return mlir::unwrap_cast<CallOp>(op).calleeIsCompute();
221}
222
223bool llzkCallOpGetCalleeIsConstrain(MlirOperation op) {
224 return mlir::unwrap_cast<CallOp>(op).calleeIsConstrain();
225}
226
228 return mlir::unwrap_cast<CallOp>(op).calleeIsStructCompute();
229}
230
232 return mlir::unwrap_cast<CallOp>(op).calleeIsStructConstrain();
233}
234
237MlirValue llzkCallOpGetSelfValueFromCompute(MlirOperation op) {
238 return wrap(mlir::unwrap_cast<CallOp>(op).getSelfValueFromCompute());
239}
240
243MlirValue llzkCallOpGetSelfValueFromConstrain(MlirOperation op) {
244 return wrap(mlir::unwrap_cast<CallOp>(op).getSelfValueFromConstrain());
245}
246
247MlirType llzkCallOpGetSingleResultTypeOfCompute(MlirOperation op) {
248 return wrap(mlir::unwrap_cast<CallOp>(op).getSingleResultTypeOfCompute());
249}
bool llzkFuncDefOpGetHasAllowWitnessAttr(MlirOperation op)
Definition Function.cpp:71
MlirType llzkFuncDefOpGetSingleResultTypeOfCompute(MlirOperation op)
Assuming the function is the compute function returns its StructType result.
Definition Function.cpp:120
MlirType llzkCallOpGetCalleeType(MlirOperation op)
Returns the FunctionType of the callee.
Definition Function.cpp:215
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:91
bool llzkCallOpGetCalleeIsStructConstrain(MlirOperation op)
Definition Function.cpp:231
bool llzkFuncDefOpGetIsInStruct(MlirOperation op)
Definition Function.cpp:95
MlirValue llzkFuncDefOpGetSelfValueFromCompute(MlirOperation op)
Return the "self" value (i.e.
Definition Function.cpp:109
bool llzkFuncDefOpGetHasArgIsPub(MlirOperation op, unsigned argNo)
Definition Function.cpp:79
bool llzkFuncDefOpGetIsStructCompute(MlirOperation op)
Definition Function.cpp:99
MlirValue llzkFuncDefOpGetSelfValueFromConstrain(MlirOperation op)
Return the "self" value (i.e.
Definition Function.cpp:115
bool llzkCallOpGetCalleeIsConstrain(MlirOperation op)
Definition Function.cpp:223
MlirValue llzkCallOpGetSelfValueFromCompute(MlirOperation op)
Return the "self" value (i.e.
Definition Function.cpp:237
void llzkFuncDefOpSetAllowConstraintAttr(MlirOperation op, bool value)
Sets the allow_constraint attribute in the FuncDefOp operation.
Definition Function.cpp:67
bool llzkCallOpGetCalleeIsStructCompute(MlirOperation op)
Definition Function.cpp:227
MlirType llzkCallOpGetSingleResultTypeOfCompute(MlirOperation op)
Assuming the callee is the compute function, returns its StructType result.
Definition Function.cpp:247
bool llzkFuncDefOpGetIsStructConstrain(MlirOperation op)
Definition Function.cpp:103
bool llzkOperationIsAFuncDefOp(MlirOperation op)
Definition Function.cpp:61
MlirValue llzkCallOpGetSelfValueFromConstrain(MlirOperation op)
Return the "self" value (i.e.
Definition Function.cpp:243
bool llzkCallOpGetCalleeIsCompute(MlirOperation op)
Definition Function.cpp:219
bool llzkOperationIsACallOp(MlirOperation op)
Definition Function.cpp:213
bool llzkFuncDefOpGetNameIsCompute(MlirOperation op)
Definition Function.cpp:87
bool llzkFuncDefOpGetHasAllowConstraintAttr(MlirOperation op)
Definition Function.cpp:63
MlirAttribute llzkFuncDefOpGetFullyQualifiedName(MlirOperation op)
Returns the fully qualified name of the function.
Definition Function.cpp:83
void llzkFuncDefOpSetAllowWitnessAttr(MlirOperation op, bool value)
Sets the allow_witness attribute in the FuncDefOp operation.
Definition Function.cpp:75
MLIR_DEFINE_CAPI_DIALECT_REGISTRATION(Polymorphic, llzk__polymorphic, llzk::polymorphic::PolymorphicDialect) MlirType llzkTypeVarTypeGet(MlirContext ctx
MlirStringRef name
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