LLZK 2.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/IR.h>
20#include <mlir-c/Pass.h>
21
22#include <mlir/CAPI/IR.h>
23#include <mlir/CAPI/Pass.h>
24#include <mlir/CAPI/Registration.h>
25#include <mlir/CAPI/Wrap.h>
26#include <mlir/IR/Attributes.h>
27#include <mlir/IR/BuiltinAttributes.h>
28
29#include <llvm/ADT/SmallVectorExtras.h>
30
31using namespace mlir;
32using namespace llzk;
33using namespace llzk::function;
34
35// Include the generated CAPI
38
39MLIR_DEFINE_CAPI_DIALECT_REGISTRATION(Function, llzk__function, FunctionDialect)
40
41//===----------------------------------------------------------------------===//
42// FuncDefOp
43//===----------------------------------------------------------------------===//
44
45
48 MlirLocation location, MlirStringRef name, MlirType funcType, intptr_t numAttrs,
49 MlirNamedAttribute const *attrs, intptr_t numArgAttrs, 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 });
57 return wrap(
59 unwrap(location), unwrap(name), llvm::cast<FunctionType>(unwrap(funcType)),
60 unwrapList(numAttrs, attrs, attrsSto), unwrappedArgAttrs
61 )
62 );
63}
64
65//===----------------------------------------------------------------------===//
66// CallOp
67//===----------------------------------------------------------------------===//
68
69static auto unwrapCallee(MlirOperation op) { return llvm::cast<FuncDefOp>(unwrap(op)); }
70
71static auto unwrapDims(MlirAttribute attr) { return llvm::cast<DenseI32ArrayAttr>(unwrap(attr)); }
72
73static auto unwrapName(MlirAttribute attr) { return llvm::cast<SymbolRefAttr>(unwrap(attr)); }
74
76 Function, CallOp, intptr_t numResults, MlirType const *results, MlirAttribute name,
77 intptr_t numOperands, MlirValue const *operands
78) {
79 SmallVector<Type> resultsSto;
80 SmallVector<Value> operandsSto;
81 return wrap(
83 builder, location, unwrapList(numResults, results, resultsSto), unwrapName(name),
84 unwrapList(numOperands, operands, operandsSto)
85 )
86 );
87}
88
90 Function, CallOp, ToCallee, MlirOperation callee, intptr_t numOperands,
91 MlirValue const *operands
92) {
93 SmallVector<Value> operandsSto;
94 return wrap(
96 builder, location, unwrapCallee(callee), unwrapList(numOperands, operands, operandsSto)
97 )
98 );
99}
100
102 Function, CallOp, WithMapOperands, intptr_t numResults, MlirType const *results,
103 MlirAttribute name, LlzkAffineMapOperandsBuilder mapOperands, intptr_t numArgOperands,
104 MlirValue const *argOperands
105) {
106 SmallVector<Type> resultsSto;
107 SmallVector<Value> argOperandsSto;
108 MapOperandsHelper<> mapOperandsHelper(mapOperands.nMapOperands, mapOperands.mapOperands);
109 auto numDimsPerMap =
110 llzkAffineMapOperandsBuilderGetDimsPerMapAttr(mapOperands, mlirLocationGetContext(location));
111 return wrap(
113 builder, location, unwrapList(numResults, results, resultsSto), unwrapName(name),
114 *mapOperandsHelper, unwrapDims(numDimsPerMap),
115 unwrapList(numArgOperands, argOperands, argOperandsSto)
116 )
117 );
118}
119
121 Function, CallOp, ToCalleeWithMapOperands, MlirOperation callee,
122 LlzkAffineMapOperandsBuilder mapOperands, intptr_t numArgOperands, MlirValue const *argOperands
123) {
124 SmallVector<Value> argOperandsSto;
125 MapOperandsHelper<> mapOperandsHelper(mapOperands.nMapOperands, mapOperands.mapOperands);
126 auto numDimsPerMap =
127 llzkAffineMapOperandsBuilderGetDimsPerMapAttr(mapOperands, mlirLocationGetContext(location));
128 return wrap(
130 builder, location, unwrapCallee(callee), *mapOperandsHelper, unwrapDims(numDimsPerMap),
131 unwrapList(numArgOperands, argOperands, argOperandsSto)
132 )
133 );
134}
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:47
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