LLZK 3.0.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"
11
12#include "llzk-c/Support.h"
13
14#include "llzk/Util/Compare.h"
16
17#include <mlir-c/BuiltinAttributes.h>
18
19#include <mlir/CAPI/IR.h>
20#include <mlir/CAPI/Support.h>
21#include <mlir/CAPI/Wrap.h>
22#include <mlir/IR/BuiltinAttributes.h>
23#include <mlir/IR/Iterators.h>
24#include <mlir/IR/SymbolTable.h>
25
26#include <cstdint>
27#include <cstring>
28
29using namespace llzk;
30using namespace mlir;
31
34 delete reinterpret_cast<SymbolLookupResultUntyped *>(result.ptr);
35}
36
41 SymbolLookupResultUntyped *result = reinterpret_cast<SymbolLookupResultUntyped *>(wrapped.ptr);
42 return wrap(result->get());
43}
44
47static mlir::WalkResult unwrap(MlirWalkResult result) {
48 switch (result) {
49 case MlirWalkResultAdvance:
50 return mlir::WalkResult::advance();
51
52 case MlirWalkResultInterrupt:
53 return mlir::WalkResult::interrupt();
54
55 case MlirWalkResultSkip:
56 return mlir::WalkResult::skip();
57 }
58 llvm_unreachable("unknown result in WalkResult::unwrap");
59}
60
62 MlirOperation from, MlirOperationWalkCallback callback, void *userData, MlirWalkOrder walkOrder
63) {
64 switch (walkOrder) {
65 case MlirWalkPreOrder:
66 unwrap(from)->walk<WalkOrder::PreOrder, ReverseIterator>([callback, userData](Operation *op) {
67 return unwrap(callback(wrap(op), userData));
68 });
69 break;
70 case MlirWalkPostOrder:
71 unwrap(from)->walk<WalkOrder::PostOrder, ReverseIterator>([callback, userData](Operation *op) {
72 return unwrap(callback(wrap(op), userData));
73 });
74 }
75}
76
77void llzkSymbolTableInsert(MlirOperation symTableOp, MlirOperation newSymbolOp) {
78 SymbolTableCollection symTables;
79 symTables.getSymbolTable(unwrap(symTableOp)).insert(unwrap(newSymbolOp));
80}
81
82//===----------------------------------------------------------------------===//
83// LlzkAffineMapOperandsBuilder implementation.
84//===----------------------------------------------------------------------===//
85
86namespace {
87template <typename T> void appendElems(T const *src, intptr_t srcSize, T *&dst, intptr_t &dstSize) {
88 assert(srcSize >= 0 && "Negative source size");
89 assert(dstSize >= 0 && "Negative destination size");
90 T *newDst = static_cast<T *>(std::realloc(dst, (srcSize + dstSize) * sizeof(T)));
91 assert(newDst && "Failed to increase the size of buffer");
92 dst = newDst; // this temporary + assert prevents `clang-tidy` warnings
93 std::memcpy(dst + dstSize, src, srcSize * sizeof(T));
94 dstSize += srcSize;
95}
96
97static void maybeDeallocArray(LlzkAffineMapOperandsBuilder *builder) {
98 if (builder->nDimsPerMap >= 0 && builder->dimsPerMap.array) {
99 std::free(builder->dimsPerMap.array);
100 builder->dimsPerMap.array = nullptr;
101 }
102}
103
108static void assertArraysAreInSync(LlzkAffineMapOperandsBuilder *builder) {
109 intptr_t nDimsPerMap = builder->nDimsPerMap < 0
111 : builder->nDimsPerMap;
112 (void)nDimsPerMap; // To silence unused variable warning if the assert below is compiled out.
113 assert(builder->nMapOperands == nDimsPerMap);
114}
115
116} // namespace
117
120 builder.nMapOperands = 0;
121 builder.mapOperands = nullptr;
122 builder.nDimsPerMap = 0;
123 builder.dimsPerMap.array = nullptr;
124
125 return builder;
126}
127
129 if (!builder) {
130 return;
131 }
132 if (builder->mapOperands) {
133 std::free(builder->mapOperands);
134 }
135 maybeDeallocArray(builder);
136 // Reset values to 0/NULL.
138}
139
141 LlzkAffineMapOperandsBuilder *builder, intptr_t n, MlirValueRange const *mapOperands
142) {
143 appendElems(mapOperands, n, builder->mapOperands, builder->nMapOperands);
144}
145
147 LlzkAffineMapOperandsBuilder *builder, intptr_t n, MlirValueRange const *mapOperands,
148 int32_t const *dimsPerMap
149) {
150 assertArraysAreInSync(builder);
151 llzkAffineMapOperandsBuilderAppendOperands(builder, n, mapOperands);
152 llzkAffineMapOperandsBuilderAppendDimCount(builder, n, dimsPerMap);
153 assertArraysAreInSync(builder);
154}
155
157 LlzkAffineMapOperandsBuilder *builder, intptr_t n, int32_t const *dimsPerMap
158) {
160 appendElems(dimsPerMap, n, builder->dimsPerMap.array, builder->nDimsPerMap);
161}
162
164 LlzkAffineMapOperandsBuilder *builder, MlirAttribute attribute
165) {
166 assert(mlirAttributeIsADenseI32Array(attribute) && "Attribute is not a DenseI32Array");
167 maybeDeallocArray(builder);
168 builder->nDimsPerMap = -1;
169 builder->dimsPerMap.attr = attribute;
170}
171
173 if (builder->nDimsPerMap >= 0) {
174 return;
175 }
176 auto attrData = mlir::unwrap_cast<DenseI32ArrayAttr>(builder->dimsPerMap.attr).asArrayRef();
177 size_t realSize = attrData.size() * sizeof(decltype(attrData)::value_type);
178 builder->dimsPerMap.array = static_cast<int32_t *>(std::malloc(realSize));
179 std::memcpy(builder->dimsPerMap.array, attrData.data(), realSize);
180 builder->nDimsPerMap = llzk::checkedCast<intptr_t>(attrData.size());
181}
182
190
192 LlzkAffineMapOperandsBuilder builder, MlirContext context
193) {
194 if (builder.nDimsPerMap < 0) {
195 return builder.dimsPerMap.attr;
196 }
197 return mlirDenseI32ArrayGet(context, builder.nDimsPerMap, builder.dimsPerMap.array);
198}
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:33
void llzkAffineMapOperandsBuilderSetDimsPerMapFromAttr(LlzkAffineMapOperandsBuilder *builder, MlirAttribute attribute)
Sets the number of dimensions per map to the given attribute.
Definition Support.cpp:163
void llzkAffineMapOperandsBuilderConvertDimsPerMapToArray(LlzkAffineMapOperandsBuilder *builder)
Converts the list of dimensions defined as an attribute into an array.
Definition Support.cpp:172
MlirOperation LlzkSymbolLookupResultGetOperation(LlzkSymbolLookupResult wrapped)
Returns the looked up Operation.
Definition Support.cpp:40
void llzkSymbolTableInsert(MlirOperation symTableOp, MlirOperation newSymbolOp)
Inserts newSymbolOp into the symbol table owned by symTableOp.
Definition Support.cpp:77
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:61
void llzkAffineMapOperandsBuilderConvertDimsPerMapToAttr(LlzkAffineMapOperandsBuilder *builder, MlirContext context)
Converts the list of dimensions defined as an array into an attribute.
Definition Support.cpp:183
MlirAttribute llzkAffineMapOperandsBuilderGetDimsPerMapAttr(LlzkAffineMapOperandsBuilder builder, MlirContext context)
Returns the number of dimensions per map represented as an attribute.
Definition Support.cpp:191
void llzkAffineMapOperandsBuilderAppendOperands(LlzkAffineMapOperandsBuilder *builder, intptr_t n, MlirValueRange const *mapOperands)
Appends the value ranges to the list of map operands.
Definition Support.cpp:140
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:156
LlzkAffineMapOperandsBuilder llzkAffineMapOperandsBuilderCreate()
Creates a new struct. The owner is responsible for cleaning the struct.
Definition Support.cpp:118
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:146
void llzkAffineMapOperandsBuilderDestroy(LlzkAffineMapOperandsBuilder *builder)
Destroys the struct releasing its resources.
Definition Support.cpp:128
This file defines methods symbol lookup across LLZK operations and included files.
constexpr T checkedCast(U u) noexcept
Definition Compare.h:94
auto unwrap_cast(auto &from)
Definition Support.h:47
Encapsulates the arguments related to affine maps that are common in operation constructors that supp...
Definition Support.h:94
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:102
MlirValueRange * mapOperands
Definition Support.h:98
int32_t * array
List of dimension counts.
Definition Support.h:107
Owned result of an LLZK symbol lookup.
Definition Support.h:56
void * ptr
raw pointer to the result
Definition Support.h:58
Representation of an mlir::ValueRange.
Definition Support.h:47