LLZK 3.0.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
11
12#include "llzk-c/Support.h"
13
14#include "llzk/CAPI/Builder.h"
15#include "llzk/CAPI/Support.h"
18
19#include <mlir-c/BuiltinAttributes.h>
20#include <mlir-c/IR.h>
21#include <mlir-c/Pass.h>
22
23#include <mlir/CAPI/IR.h>
24#include <mlir/CAPI/Pass.h>
25#include <mlir/CAPI/Registration.h>
26#include <mlir/CAPI/Wrap.h>
27#include <mlir/IR/Attributes.h>
28#include <mlir/IR/BuiltinAttributes.h>
29
30#include <llvm/ADT/SmallVectorExtras.h>
31
32using namespace mlir;
33using namespace llzk;
34using namespace llzk::function;
35
36// Include the generated CAPI
39
40MLIR_DEFINE_CAPI_DIALECT_REGISTRATION(Function, llzk__function, FunctionDialect)
41
42//===----------------------------------------------------------------------===//
43// FuncDefOp
44//===----------------------------------------------------------------------===//
45
47 Function, FuncDefOp, WithAttrsAndArgAttrs, MlirStringRef name, MlirType funcType,
48 intptr_t numAttrs, MlirNamedAttribute const *attrs, intptr_t numArgAttrs,
49 MlirAttribute const *argAttrs
50) {
51 SmallVector<NamedAttribute> attrsSto;
52 SmallVector<Attribute> argAttrsSto;
53 SmallVector<DictionaryAttr> unwrappedArgAttrs =
54 llvm::map_to_vector(unwrapList(numArgAttrs, argAttrs, argAttrsSto), [](auto attr) {
55 return llvm::cast<DictionaryAttr>(attr);
56 });
58 builder, wrap(
60 builder, location, unwrap(name), llvm::cast<FunctionType>(unwrap(funcType)),
61 unwrapList(numAttrs, attrs, attrsSto), unwrappedArgAttrs
62 )
63 )
64 );
65}
66
68 Function, FuncDefOp, WithAttrs, MlirStringRef name, MlirType funcType, intptr_t numAttrs,
69 MlirNamedAttribute const *attrs
70) {
71 return llzkFunction_FuncDefOpBuildWithAttrsAndArgAttrs(
72 builder, location, name, funcType, numAttrs, attrs, /*numArgAttrs=*/0, /*argAttrs=*/NULL
73 );
74}
75
77 Function, FuncDefOp, WithArgAttrs, MlirStringRef name, MlirType funcType, intptr_t numArgAttrs,
78 MlirAttribute const *argAttrs
79) {
80 return llzkFunction_FuncDefOpBuildWithAttrsAndArgAttrs(
81 builder, location, name, funcType, /*numAttrs=*/0, /*attrs=*/NULL, numArgAttrs, argAttrs
82 );
83}
84
86 Function, FuncDefOp, WithoutAttrs, MlirStringRef name, MlirType funcType
87) {
88 return llzkFunction_FuncDefOpBuildWithAttrs(
89 builder, location, name, funcType, /*numAttrs=*/0, /*attrs=*/NULL
90 );
91}
92
93MlirAttribute llzkFunction_FuncDefOpGetArgNameAttr(MlirOperation op, unsigned index) {
94 std::optional<StringAttr> argNameAttr = llvm::cast<FuncDefOp>(unwrap(op)).getArgNameAttr(index);
95 return wrap(argNameAttr ? Attribute(*argNameAttr) : Attribute());
96}
97
98void llzkFunction_FuncDefOpSetArgNameAttr(MlirOperation op, unsigned index, MlirAttribute attr) {
99 llvm::cast<FuncDefOp>(unwrap(op)).setArgNameAttr(index, llvm::cast<StringAttr>(unwrap(attr)));
100}
101
102void llzkFunction_FuncDefOpSetArgName(MlirOperation op, unsigned index, MlirStringRef name) {
103 llvm::cast<FuncDefOp>(unwrap(op)).setArgName(index, unwrap(name));
104}
105
106MlirAttribute llzkFunction_FuncDefOpGetResNameAttr(MlirOperation op, unsigned index) {
107 std::optional<StringAttr> resNameAttr = llvm::cast<FuncDefOp>(unwrap(op)).getResNameAttr(index);
108 return wrap(resNameAttr ? Attribute(*resNameAttr) : Attribute());
109}
110
111void llzkFunction_FuncDefOpSetResNameAttr(MlirOperation op, unsigned index, MlirAttribute attr) {
112 llvm::cast<FuncDefOp>(unwrap(op)).setResNameAttr(index, llvm::cast<StringAttr>(unwrap(attr)));
113}
114
115void llzkFunction_FuncDefOpSetResName(MlirOperation op, unsigned index, MlirStringRef name) {
116 llvm::cast<FuncDefOp>(unwrap(op)).setResName(index, unwrap(name));
117}
118
119//===----------------------------------------------------------------------===//
120// CallOp
121//===----------------------------------------------------------------------===//
122
123static auto unwrapCallee(MlirOperation op) { return llvm::cast<FuncDefOp>(unwrap(op)); }
124
125static auto unwrapDims(MlirAttribute attr) { return llvm::cast<DenseI32ArrayAttr>(unwrap(attr)); }
126
127static auto unwrapName(MlirAttribute attr) { return llvm::cast<SymbolRefAttr>(unwrap(attr)); }
128
130 Function, CallOp, intptr_t numResults, MlirType const *results, MlirAttribute name,
131 intptr_t numOperands, MlirValue const *operands
132) {
133 SmallVector<Type> resultsSto;
134 SmallVector<Value> operandsSto;
135 return mlirOpBuilderInsert(
136 builder, wrap(
138 builder, location, unwrapList(numResults, results, resultsSto),
139 unwrapName(name), unwrapList(numOperands, operands, operandsSto)
140 )
141 )
142 );
143}
144
146 Function, CallOp, ToCallee, MlirOperation callee, intptr_t numOperands,
147 MlirValue const *operands
148) {
149 SmallVector<Value> operandsSto;
150 return mlirOpBuilderInsert(
151 builder, wrap(
153 builder, location, unwrapCallee(callee),
154 unwrapList(numOperands, operands, operandsSto)
155 )
156 )
157 );
158}
159
161 Function, CallOp, WithMapOperands, intptr_t numResults, MlirType const *results,
162 MlirAttribute name, LlzkAffineMapOperandsBuilder mapOperands, intptr_t numArgOperands,
163 MlirValue const *argOperands
164) {
165 SmallVector<Type> resultsSto;
166 SmallVector<Value> argOperandsSto;
167 MapOperandsHelper<> mapOperandsHelper(mapOperands.nMapOperands, mapOperands.mapOperands);
168 auto numDimsPerMap =
169 llzkAffineMapOperandsBuilderGetDimsPerMapAttr(mapOperands, mlirLocationGetContext(location));
170 return mlirOpBuilderInsert(
171 builder, wrap(
173 builder, location, unwrapList(numResults, results, resultsSto),
174 unwrapName(name), *mapOperandsHelper, unwrapDims(numDimsPerMap),
175 unwrapList(numArgOperands, argOperands, argOperandsSto)
176 )
177 )
178 );
179}
180
182 Function, CallOp, ToCalleeWithMapOperands, MlirOperation callee,
183 LlzkAffineMapOperandsBuilder mapOperands, intptr_t numArgOperands, MlirValue const *argOperands
184) {
185 SmallVector<Value> argOperandsSto;
186 MapOperandsHelper<> mapOperandsHelper(mapOperands.nMapOperands, mapOperands.mapOperands);
187 auto numDimsPerMap =
188 llzkAffineMapOperandsBuilderGetDimsPerMapAttr(mapOperands, mlirLocationGetContext(location));
189 return mlirOpBuilderInsert(
190 builder,
191 wrap(
193 builder, location, unwrapCallee(callee), *mapOperandsHelper,
194 unwrapDims(numDimsPerMap), unwrapList(numArgOperands, argOperands, argOperandsSto)
195 )
196 )
197 );
198}
199
201 Function, CallOp, WithTemplateParams, intptr_t numResults, MlirType const *results,
202 MlirAttribute name, intptr_t numTemplateParams, MlirAttribute const *templateParams,
203 intptr_t numArgOperands, MlirValue const *argOperands
204) {
205 SmallVector<Type> resultsSto;
206 SmallVector<Value> argOperandsSto;
207 SmallVector<Attribute> templateParamsSto;
208 return mlirOpBuilderInsert(
209 builder, wrap(
211 builder, location, unwrapList(numResults, results, resultsSto),
212 unwrapName(name), unwrapList(numArgOperands, argOperands, argOperandsSto),
213 unwrapList(numTemplateParams, templateParams, templateParamsSto)
214 )
215 )
216 );
217}
218
220 Function, CallOp, ToCalleeWithTemplateParams, MlirOperation callee, intptr_t numTemplateParams,
221 MlirAttribute const *templateParams, intptr_t numArgOperands, MlirValue const *argOperands
222) {
223 SmallVector<Value> argOperandsSto;
224 SmallVector<Attribute> templateParamsSto;
225 return mlirOpBuilderInsert(
226 builder, wrap(
228 builder, location, unwrapCallee(callee),
229 unwrapList(numArgOperands, argOperands, argOperandsSto),
230 unwrapList(numTemplateParams, templateParams, templateParamsSto)
231 )
232 )
233 );
234}
MlirOperation mlirOpBuilderInsert(MlirOpBuilder builder, MlirOperation op)
Inserts op at the current insertion point of builder and returns it.
Definition Builder.cpp:167
MlirAttribute llzkFunction_FuncDefOpGetResNameAttr(MlirOperation op, unsigned index)
Returns the function.res_name StringAttr for the result at the given index, or null if the result has...
Definition Function.cpp:106
void llzkFunction_FuncDefOpSetResName(MlirOperation op, unsigned index, MlirStringRef name)
Sets the function.res_name attribute for the result at the given index from a string value.
Definition Function.cpp:115
void llzkFunction_FuncDefOpSetArgNameAttr(MlirOperation op, unsigned index, MlirAttribute attr)
Sets the function.arg_name attribute for the argument at the given index.
Definition Function.cpp:98
MlirAttribute llzkFunction_FuncDefOpGetArgNameAttr(MlirOperation op, unsigned index)
Returns the function.arg_name StringAttr for the argument at the given index, or null if the argument...
Definition Function.cpp:93
void llzkFunction_FuncDefOpSetResNameAttr(MlirOperation op, unsigned index, MlirAttribute attr)
Sets the function.res_name attribute for the result at the given index.
Definition Function.cpp:111
void llzkFunction_FuncDefOpSetArgName(MlirOperation op, unsigned index, MlirStringRef name)
Sets the function.arg_name attribute for the argument at the given index from a string value.
Definition Function.cpp:102
MlirAttribute llzkAffineMapOperandsBuilderGetDimsPerMapAttr(LlzkAffineMapOperandsBuilder builder, MlirContext context)
Returns the number of dimensions per map represented as an attribute.
Definition Support.cpp:195
Helper for unwrapping the C arguments for the map operands.
Definition Support.h:61
#define LLZK_DEFINE_OP_BUILD_METHOD(dialect, op,...)
Definition Support.h:31
#define LLZK_DEFINE_SUFFIX_OP_BUILD_METHOD(dialect, op, suffix,...)
Definition Support.h:27
mlir::Operation * create(MlirOpBuilder cBuilder, MlirLocation cLocation, Args &&...args)
Creates a new operation using an ODS build method.
Definition Builder.h:41
Encapsulates the arguments related to affine maps that are common in operation constructors that supp...
Definition Support.h:103
MlirValueRange * mapOperands
Definition Support.h:107