LLZK 2.1.1
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
46
49 MlirLocation location, MlirStringRef name, MlirType funcType, intptr_t numAttrs,
50 MlirNamedAttribute const *attrs, intptr_t numArgAttrs, MlirAttribute const *argAttrs
51) {
52 SmallVector<NamedAttribute> attrsSto;
53 SmallVector<Attribute> argAttrsSto;
54 SmallVector<DictionaryAttr> unwrappedArgAttrs =
55 llvm::map_to_vector(unwrapList(numArgAttrs, argAttrs, argAttrsSto), [](auto attr) {
56 return llvm::cast<DictionaryAttr>(attr);
57 });
58 return wrap(
60 unwrap(location), unwrap(name), llvm::cast<FunctionType>(unwrap(funcType)),
61 unwrapList(numAttrs, attrs, attrsSto), unwrappedArgAttrs
62 )
63 );
64}
65
66bool llzkFunction_FuncDefOpHasArgNameAttr(MlirOperation op, unsigned index) {
67 return llvm::cast<FuncDefOp>(unwrap(op)).hasArgName(index);
68}
69
70MlirAttribute llzkFunction_FuncDefOpGetArgNameAttr(MlirOperation op, unsigned index) {
71 std::optional<StringAttr> argNameAttr = llvm::cast<FuncDefOp>(unwrap(op)).getArgNameAttr(index);
72 return wrap(argNameAttr ? Attribute(*argNameAttr) : Attribute());
73}
74
75void llzkFunction_FuncDefOpSetArgNameAttr(MlirOperation op, unsigned index, MlirAttribute attr) {
76 llvm::cast<FuncDefOp>(unwrap(op)).setArgNameAttr(index, llvm::cast<StringAttr>(unwrap(attr)));
77}
78
79void llzkFunction_FuncDefOpSetArgName(MlirOperation op, unsigned index, MlirStringRef name) {
80 llvm::cast<FuncDefOp>(unwrap(op)).setArgName(index, unwrap(name));
81}
82
83//===----------------------------------------------------------------------===//
84// CallOp
85//===----------------------------------------------------------------------===//
86
87static auto unwrapCallee(MlirOperation op) { return llvm::cast<FuncDefOp>(unwrap(op)); }
88
89static auto unwrapDims(MlirAttribute attr) { return llvm::cast<DenseI32ArrayAttr>(unwrap(attr)); }
90
91static auto unwrapName(MlirAttribute attr) { return llvm::cast<SymbolRefAttr>(unwrap(attr)); }
92
94 Function, CallOp, intptr_t numResults, MlirType const *results, MlirAttribute name,
95 intptr_t numOperands, MlirValue const *operands
96) {
97 SmallVector<Type> resultsSto;
98 SmallVector<Value> operandsSto;
99 return wrap(
101 builder, location, unwrapList(numResults, results, resultsSto), unwrapName(name),
102 unwrapList(numOperands, operands, operandsSto)
103 )
104 );
105}
106
108 Function, CallOp, ToCallee, MlirOperation callee, intptr_t numOperands,
109 MlirValue const *operands
110) {
111 SmallVector<Value> operandsSto;
112 return wrap(
114 builder, location, unwrapCallee(callee), unwrapList(numOperands, operands, operandsSto)
115 )
116 );
117}
118
120 Function, CallOp, WithMapOperands, intptr_t numResults, MlirType const *results,
121 MlirAttribute name, LlzkAffineMapOperandsBuilder mapOperands, intptr_t numArgOperands,
122 MlirValue const *argOperands
123) {
124 SmallVector<Type> resultsSto;
125 SmallVector<Value> argOperandsSto;
126 MapOperandsHelper<> mapOperandsHelper(mapOperands.nMapOperands, mapOperands.mapOperands);
127 auto numDimsPerMap =
128 llzkAffineMapOperandsBuilderGetDimsPerMapAttr(mapOperands, mlirLocationGetContext(location));
129 return wrap(
131 builder, location, unwrapList(numResults, results, resultsSto), unwrapName(name),
132 *mapOperandsHelper, unwrapDims(numDimsPerMap),
133 unwrapList(numArgOperands, argOperands, argOperandsSto)
134 )
135 );
136}
137
139 Function, CallOp, ToCalleeWithMapOperands, MlirOperation callee,
140 LlzkAffineMapOperandsBuilder mapOperands, intptr_t numArgOperands, MlirValue const *argOperands
141) {
142 SmallVector<Value> argOperandsSto;
143 MapOperandsHelper<> mapOperandsHelper(mapOperands.nMapOperands, mapOperands.mapOperands);
144 auto numDimsPerMap =
145 llzkAffineMapOperandsBuilderGetDimsPerMapAttr(mapOperands, mlirLocationGetContext(location));
146 return wrap(
148 builder, location, unwrapCallee(callee), *mapOperandsHelper, unwrapDims(numDimsPerMap),
149 unwrapList(numArgOperands, argOperands, argOperandsSto)
150 )
151 );
152}
153
155 Function, CallOp, WithTemplateParams, intptr_t numResults, MlirType const *results,
156 MlirAttribute name, intptr_t numTemplateParams, MlirAttribute const *templateParams,
157 intptr_t numArgOperands, MlirValue const *argOperands
158) {
159 SmallVector<Type> resultsSto;
160 SmallVector<Value> argOperandsSto;
161 SmallVector<Attribute> templateParamsSto;
162 return wrap(
164 builder, location, unwrapList(numResults, results, resultsSto), unwrapName(name),
165 unwrapList(numArgOperands, argOperands, argOperandsSto),
166 unwrapList(numTemplateParams, templateParams, templateParamsSto)
167 )
168 );
169}
170
172 Function, CallOp, ToCalleeWithTemplateParams, MlirOperation callee, intptr_t numTemplateParams,
173 MlirAttribute const *templateParams, intptr_t numArgOperands, MlirValue const *argOperands
174) {
175 SmallVector<Value> argOperandsSto;
176 SmallVector<Attribute> templateParamsSto;
177 return wrap(
179 builder, location, unwrapCallee(callee),
180 unwrapList(numArgOperands, argOperands, argOperandsSto),
181 unwrapList(numTemplateParams, templateParams, templateParamsSto)
182 )
183 );
184}
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:75
bool llzkFunction_FuncDefOpHasArgNameAttr(MlirOperation op, unsigned index)
Returns true iff the argument at the given index has a function.arg_name attribute.
Definition Function.cpp:66
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:70
MlirOperation llzkFunction_FuncDefOpCreateWithAttrsAndArgAttrs(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:48
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:79
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
static FuncDefOp create(::mlir::Location location, ::llvm::StringRef name, ::mlir::FunctionType type, ::llvm::ArrayRef<::mlir::NamedAttribute > attrs={})
#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