LLZK 0.1.0
An open-source IR for Zero Knowledge (ZK) circuits
Loading...
Searching...
No Matches
Support.cpp
Go to the documentation of this file.
1//===-- Support.cpp - C API general utilities ---------------------*- 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/Support.h"
12
13#include "llzk-c/Support.h"
14
15#include <mlir/CAPI/IR.h>
16#include <mlir/CAPI/Support.h>
17#include <mlir/CAPI/Wrap.h>
18#include <mlir/IR/BuiltinAttributes.h>
19#include <mlir/IR/Iterators.h>
20
21#include <mlir-c/BuiltinAttributes.h>
22
23#include <cstdint>
24#include <cstring>
25
26using namespace llzk;
27using namespace mlir;
28
31 delete reinterpret_cast<SymbolLookupResultUntyped *>(result.ptr);
32}
33
38 SymbolLookupResultUntyped *result = reinterpret_cast<SymbolLookupResultUntyped *>(wrapped.ptr);
39 return wrap(result->get());
40}
41
43void mlirOperationReplaceUsesOfWith(MlirOperation op, MlirValue oldValue, MlirValue newValue) {
44 unwrap(op)->replaceUsesOfWith(unwrap(oldValue), unwrap(newValue));
45}
46
48static mlir::WalkResult unwrap(MlirWalkResult result) {
49 switch (result) {
50 case MlirWalkResultAdvance:
51 return mlir::WalkResult::advance();
52
53 case MlirWalkResultInterrupt:
54 return mlir::WalkResult::interrupt();
55
56 case MlirWalkResultSkip:
57 return mlir::WalkResult::skip();
58 }
59 llvm_unreachable("unknown result in WalkResult::unwrap");
60}
61
63 MlirOperation from, MlirOperationWalkCallback callback, void *userData, MlirWalkOrder walkOrder
64) {
65 switch (walkOrder) {
66 case MlirWalkPreOrder:
67 unwrap(from)->walk<WalkOrder::PreOrder, ReverseIterator>([callback, userData](Operation *op) {
68 return unwrap(callback(wrap(op), userData));
69 });
70 break;
71 case MlirWalkPostOrder:
72 unwrap(from)->walk<WalkOrder::PostOrder, ReverseIterator>([callback, userData](Operation *op) {
73 return unwrap(callback(wrap(op), userData));
74 });
75 }
76}
77
78//===----------------------------------------------------------------------===//
79// LlzkAffineMapOperandsBuilder implementation.
80//===----------------------------------------------------------------------===//
81
82namespace {
83template <typename T> void appendElems(T const *src, intptr_t srcSize, T *&dst, intptr_t &dstSize) {
84 assert(srcSize >= 0 && "Negative source size");
85 assert(dstSize >= 0 && "Negative destination size");
86 dst = static_cast<T *>(std::realloc(dst, (srcSize + dstSize) * sizeof(T)));
87 assert(dst && "Failed to increase the size of buffer");
88 std::memcpy(dst + dstSize, src, srcSize * sizeof(T));
89 dstSize += srcSize;
90}
91
92static void maybeDeallocArray(LlzkAffineMapOperandsBuilder *builder) {
93 if (builder->nDimsPerMap >= 0 && builder->dimsPerMap.array) {
94 std::free(builder->dimsPerMap.array);
95 builder->dimsPerMap.array = nullptr;
96 }
97}
98
103static void assertArraysAreInSync(LlzkAffineMapOperandsBuilder *builder) {
104 intptr_t nDimsPerMap = builder->nDimsPerMap < 0
106 : builder->nDimsPerMap;
107 (void)nDimsPerMap; // To silence unused variable warning if the assert below is compiled out.
108 assert(builder->nMapOperands == nDimsPerMap);
109}
110
111} // namespace
112
115 builder.nMapOperands = 0;
116 builder.mapOperands = nullptr;
117 builder.nDimsPerMap = 0;
118 builder.dimsPerMap.array = nullptr;
119
120 return builder;
121}
122
124 if (!builder) {
125 return;
126 }
127 if (builder->mapOperands) {
128 std::free(builder->mapOperands);
129 }
130 maybeDeallocArray(builder);
131 // Reset values to 0/NULL.
133}
134
136 LlzkAffineMapOperandsBuilder *builder, intptr_t n, MlirValueRange const *mapOperands
137) {
138 appendElems(mapOperands, n, builder->mapOperands, builder->nMapOperands);
139}
140
142 LlzkAffineMapOperandsBuilder *builder, intptr_t n, MlirValueRange const *mapOperands,
143 int32_t const *dimsPerMap
144) {
145 assertArraysAreInSync(builder);
146 llzkAffineMapOperandsBuilderAppendOperands(builder, n, mapOperands);
147 llzkAffineMapOperandsBuilderAppendDimCount(builder, n, dimsPerMap);
148 assertArraysAreInSync(builder);
149}
150
152 LlzkAffineMapOperandsBuilder *builder, intptr_t n, int32_t const *dimsPerMap
153) {
155 appendElems(dimsPerMap, n, builder->dimsPerMap.array, builder->nDimsPerMap);
156}
157
159 LlzkAffineMapOperandsBuilder *builder, MlirAttribute attribute
160) {
161 assert(mlirAttributeIsADenseI32Array(attribute) && "Attribute is not a DenseI32Array");
162 maybeDeallocArray(builder);
163 builder->nDimsPerMap = -1;
164 builder->dimsPerMap.attr = attribute;
165}
166
168 if (builder->nDimsPerMap >= 0) {
169 return;
170 }
171 auto attrData = mlir::unwrap_cast<DenseI32ArrayAttr>(builder->dimsPerMap.attr).asArrayRef();
172 size_t realSize = attrData.size() * sizeof(decltype(attrData)::value_type);
173 builder->dimsPerMap.array = static_cast<int32_t *>(std::malloc(realSize));
174 std::memcpy(builder->dimsPerMap.array, attrData.data(), realSize);
175 builder->nDimsPerMap = static_cast<intptr_t>(attrData.size());
176}
177
185
187 LlzkAffineMapOperandsBuilder builder, MlirContext context
188) {
189 if (builder.nDimsPerMap < 0) {
190 return builder.dimsPerMap.attr;
191 }
192 return mlirDenseI32ArrayGet(context, builder.nDimsPerMap, builder.dimsPerMap.array);
193}
Apache License January AND DISTRIBUTION Definitions License shall mean the terms and conditions for and distribution as defined by Sections through of this document Licensor shall mean the copyright owner or entity authorized by the copyright owner that is granting the License Legal Entity shall mean the union of the acting entity and all other entities that control are controlled by or are under common control with that entity For the purposes of this definition control direct or to cause the direction or management of such whether by contract or including but not limited to software source documentation and configuration files Object form shall mean any form resulting from mechanical transformation or translation of a Source including but not limited to compiled object generated and conversions to other media types Work shall mean the work of whether in Source or Object made available under the as indicated by a copyright notice that is included in or attached to the whether in Source or Object that is based or other modifications as a an original work of authorship For the purposes of this Derivative Works shall not include works that remain separable from
Definition LICENSE.txt:45
void llzkSymbolLookupResultDestroy(LlzkSymbolLookupResult result)
Destroys the lookup result, releasing its resources.
Definition Support.cpp:30
void llzkAffineMapOperandsBuilderSetDimsPerMapFromAttr(LlzkAffineMapOperandsBuilder *builder, MlirAttribute attribute)
Sets the number of dimensions per map to the given attribute.
Definition Support.cpp:158
void llzkAffineMapOperandsBuilderConvertDimsPerMapToArray(LlzkAffineMapOperandsBuilder *builder)
Converts the list of dimensions defined as an attribute into an array.
Definition Support.cpp:167
MlirOperation LlzkSymbolLookupResultGetOperation(LlzkSymbolLookupResult wrapped)
Returns the looked up Operation.
Definition Support.cpp:37
void mlirOperationWalkReverse(MlirOperation from, MlirOperationWalkCallback callback, void *userData, MlirWalkOrder walkOrder)
Walks operation op in walkOrder, with operations at the same nesting level traversed in reverse order...
Definition Support.cpp:62
void llzkAffineMapOperandsBuilderConvertDimsPerMapToAttr(LlzkAffineMapOperandsBuilder *builder, MlirContext context)
Converts the list of dimensions defined as an array into an attribute.
Definition Support.cpp:178
MlirAttribute llzkAffineMapOperandsBuilderGetDimsPerMapAttr(LlzkAffineMapOperandsBuilder builder, MlirContext context)
Returns the number of dimensions per map represented as an attribute.
Definition Support.cpp:186
void llzkAffineMapOperandsBuilderAppendOperands(LlzkAffineMapOperandsBuilder *builder, intptr_t n, MlirValueRange const *mapOperands)
Appends the value ranges to the list of map operands.
Definition Support.cpp:135
void llzkAffineMapOperandsBuilderAppendDimCount(LlzkAffineMapOperandsBuilder *builder, intptr_t n, int32_t const *dimsPerMap)
Appends a dimension count to the list of dimensions per map.
Definition Support.cpp:151
void mlirOperationReplaceUsesOfWith(MlirOperation op, MlirValue oldValue, MlirValue newValue)
Note: Duplicated from upstream LLVM. Available in 21.1.8 and later.
Definition Support.cpp:43
LlzkAffineMapOperandsBuilder llzkAffineMapOperandsBuilderCreate()
Creates a new struct. The owner is responsible for cleaning the struct.
Definition Support.cpp:113
void llzkAffineMapOperandsBuilderAppendOperandsWithDimCount(LlzkAffineMapOperandsBuilder *builder, intptr_t n, MlirValueRange const *mapOperands, int32_t const *dimsPerMap)
Appends the value ranges to the list of map operands and indicates how many of these operands are dim...
Definition Support.cpp:141
void llzkAffineMapOperandsBuilderDestroy(LlzkAffineMapOperandsBuilder *builder)
Destroys the struct releasing its resources.
Definition Support.cpp:123
This file defines methods symbol lookup across LLZK operations and included files.
auto unwrap_cast(auto &from)
Definition Support.h:30
Encapsulates the arguments related to affine maps that are common in operation constructors that supp...
Definition Support.h:105
union LlzkAffineMapOperandsBuilder::@126363357317107207232312003203325156066212176243 dimsPerMap
intptr_t nDimsPerMap
Set to a negative number to indicate that dimsPerMap.attr must be used instead of dimsPerMap....
Definition Support.h:113
MlirValueRange * mapOperands
Definition Support.h:109
int32_t * array
List of dimension counts.
Definition Support.h:118