LLZK 2.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
46void mlirOperationReplaceUsesOfWith(MlirOperation op, MlirValue oldValue, MlirValue newValue) {
47 unwrap(op)->replaceUsesOfWith(unwrap(oldValue), unwrap(newValue));
48}
49
51static mlir::WalkResult unwrap(MlirWalkResult result) {
52 switch (result) {
53 case MlirWalkResultAdvance:
54 return mlir::WalkResult::advance();
55
56 case MlirWalkResultInterrupt:
57 return mlir::WalkResult::interrupt();
58
59 case MlirWalkResultSkip:
60 return mlir::WalkResult::skip();
61 }
62 llvm_unreachable("unknown result in WalkResult::unwrap");
63}
64
66 MlirOperation from, MlirOperationWalkCallback callback, void *userData, MlirWalkOrder walkOrder
67) {
68 switch (walkOrder) {
69 case MlirWalkPreOrder:
70 unwrap(from)->walk<WalkOrder::PreOrder, ReverseIterator>([callback, userData](Operation *op) {
71 return unwrap(callback(wrap(op), userData));
72 });
73 break;
74 case MlirWalkPostOrder:
75 unwrap(from)->walk<WalkOrder::PostOrder, ReverseIterator>([callback, userData](Operation *op) {
76 return unwrap(callback(wrap(op), userData));
77 });
78 }
79}
80
81void llzkSymbolTableInsert(MlirOperation symTableOp, MlirOperation newSymbolOp) {
82 SymbolTableCollection symTables;
83 symTables.getSymbolTable(unwrap(symTableOp)).insert(unwrap(newSymbolOp));
84}
85
86//===----------------------------------------------------------------------===//
87// LlzkAffineMapOperandsBuilder implementation.
88//===----------------------------------------------------------------------===//
89
90namespace {
91template <typename T> void appendElems(T const *src, intptr_t srcSize, T *&dst, intptr_t &dstSize) {
92 assert(srcSize >= 0 && "Negative source size");
93 assert(dstSize >= 0 && "Negative destination size");
94 T *newDst = static_cast<T *>(std::realloc(dst, (srcSize + dstSize) * sizeof(T)));
95 assert(newDst && "Failed to increase the size of buffer");
96 dst = newDst; // this temporary + assert prevents `clang-tidy` warnings
97 std::memcpy(dst + dstSize, src, srcSize * sizeof(T));
98 dstSize += srcSize;
99}
100
101static void maybeDeallocArray(LlzkAffineMapOperandsBuilder *builder) {
102 if (builder->nDimsPerMap >= 0 && builder->dimsPerMap.array) {
103 std::free(builder->dimsPerMap.array);
104 builder->dimsPerMap.array = nullptr;
105 }
106}
107
112static void assertArraysAreInSync(LlzkAffineMapOperandsBuilder *builder) {
113 intptr_t nDimsPerMap = builder->nDimsPerMap < 0
115 : builder->nDimsPerMap;
116 (void)nDimsPerMap; // To silence unused variable warning if the assert below is compiled out.
117 assert(builder->nMapOperands == nDimsPerMap);
118}
119
120} // namespace
121
124 builder.nMapOperands = 0;
125 builder.mapOperands = nullptr;
126 builder.nDimsPerMap = 0;
127 builder.dimsPerMap.array = nullptr;
128
129 return builder;
130}
131
133 if (!builder) {
134 return;
135 }
136 if (builder->mapOperands) {
137 std::free(builder->mapOperands);
138 }
139 maybeDeallocArray(builder);
140 // Reset values to 0/NULL.
142}
143
145 LlzkAffineMapOperandsBuilder *builder, intptr_t n, MlirValueRange const *mapOperands
146) {
147 appendElems(mapOperands, n, builder->mapOperands, builder->nMapOperands);
148}
149
151 LlzkAffineMapOperandsBuilder *builder, intptr_t n, MlirValueRange const *mapOperands,
152 int32_t const *dimsPerMap
153) {
154 assertArraysAreInSync(builder);
155 llzkAffineMapOperandsBuilderAppendOperands(builder, n, mapOperands);
156 llzkAffineMapOperandsBuilderAppendDimCount(builder, n, dimsPerMap);
157 assertArraysAreInSync(builder);
158}
159
161 LlzkAffineMapOperandsBuilder *builder, intptr_t n, int32_t const *dimsPerMap
162) {
164 appendElems(dimsPerMap, n, builder->dimsPerMap.array, builder->nDimsPerMap);
165}
166
168 LlzkAffineMapOperandsBuilder *builder, MlirAttribute attribute
169) {
170 assert(mlirAttributeIsADenseI32Array(attribute) && "Attribute is not a DenseI32Array");
171 maybeDeallocArray(builder);
172 builder->nDimsPerMap = -1;
173 builder->dimsPerMap.attr = attribute;
174}
175
177 if (builder->nDimsPerMap >= 0) {
178 return;
179 }
180 auto attrData = mlir::unwrap_cast<DenseI32ArrayAttr>(builder->dimsPerMap.attr).asArrayRef();
181 size_t realSize = attrData.size() * sizeof(decltype(attrData)::value_type);
182 builder->dimsPerMap.array = static_cast<int32_t *>(std::malloc(realSize));
183 std::memcpy(builder->dimsPerMap.array, attrData.data(), realSize);
184 builder->nDimsPerMap = llzk::checkedCast<intptr_t>(attrData.size());
185}
186
194
196 LlzkAffineMapOperandsBuilder builder, MlirContext context
197) {
198 if (builder.nDimsPerMap < 0) {
199 return builder.dimsPerMap.attr;
200 }
201 return mlirDenseI32ArrayGet(context, builder.nDimsPerMap, builder.dimsPerMap.array);
202}
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:167
void llzkAffineMapOperandsBuilderConvertDimsPerMapToArray(LlzkAffineMapOperandsBuilder *builder)
Converts the list of dimensions defined as an attribute into an array.
Definition Support.cpp:176
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:81
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:65
void llzkAffineMapOperandsBuilderConvertDimsPerMapToAttr(LlzkAffineMapOperandsBuilder *builder, MlirContext context)
Converts the list of dimensions defined as an array into an attribute.
Definition Support.cpp:187
MlirAttribute llzkAffineMapOperandsBuilderGetDimsPerMapAttr(LlzkAffineMapOperandsBuilder builder, MlirContext context)
Returns the number of dimensions per map represented as an attribute.
Definition Support.cpp:195
void llzkAffineMapOperandsBuilderAppendOperands(LlzkAffineMapOperandsBuilder *builder, intptr_t n, MlirValueRange const *mapOperands)
Appends the value ranges to the list of map operands.
Definition Support.cpp:144
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:160
void mlirOperationReplaceUsesOfWith(MlirOperation op, MlirValue oldValue, MlirValue newValue)
Note: Duplicated from upstream LLVM. Available in 21.1.8 and later.
Definition Support.cpp:46
LlzkAffineMapOperandsBuilder llzkAffineMapOperandsBuilderCreate()
Creates a new struct. The owner is responsible for cleaning the struct.
Definition Support.cpp:122
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:150
void llzkAffineMapOperandsBuilderDestroy(LlzkAffineMapOperandsBuilder *builder)
Destroys the struct releasing its resources.
Definition Support.cpp:132
This file defines methods symbol lookup across LLZK operations and included files.
constexpr T checkedCast(U u) noexcept
Definition Compare.h:81
auto unwrap_cast(auto &from)
Definition Support.h:51
Encapsulates the arguments related to affine maps that are common in operation constructors that supp...
Definition Support.h:103
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:111
MlirValueRange * mapOperands
Definition Support.h:107
int32_t * array
List of dimension counts.
Definition Support.h:116
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