LLZK 3.0.0
An open-source IR for Zero Knowledge (ZK) circuits
Loading...
Searching...
No Matches
Struct.cpp
Go to the documentation of this file.
1//===-- Struct.cpp - Struct 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/CAPI/Builder.h"
13#include "llzk/CAPI/Support.h"
19#include "llzk/Util/Compare.h"
22
23#include <mlir-c/BuiltinAttributes.h>
24#include <mlir-c/Pass.h>
25#include <mlir-c/Support.h>
26
27#include <mlir/CAPI/AffineMap.h>
28#include <mlir/CAPI/Pass.h>
29#include <mlir/CAPI/Registration.h>
30#include <mlir/CAPI/Support.h>
31#include <mlir/CAPI/Wrap.h>
32#include <mlir/IR/BuiltinAttributes.h>
33#include <mlir/IR/SymbolTable.h>
34
35#include <llvm/ADT/STLExtras.h>
36
37using namespace mlir;
38using namespace llzk;
39using namespace llzk::component;
40
41static inline void registerLLZKStructTransformationPasses() { registerTransformationPasses(); }
42
43// Include the generated CAPI
47
48MLIR_DEFINE_CAPI_DIALECT_REGISTRATION(Struct, llzk__component, StructDialect)
49
50//===----------------------------------------------------------------------===//
51// StructType
52//===----------------------------------------------------------------------===//
53
54MlirType llzkStruct_StructTypeGet(MlirAttribute name) {
55 return wrap(StructType::get(llvm::cast<SymbolRefAttr>(unwrap(name))));
56}
57
58MlirType llzkStruct_StructTypeGetWithArrayAttr(MlirAttribute name, MlirAttribute params) {
59 return wrap(
61 llvm::cast<SymbolRefAttr>(unwrap(name)), llvm::cast<ArrayAttr>(unwrap(params))
62 )
63 );
64}
65
67 MlirAttribute name, intptr_t numParams, MlirAttribute const *params
68) {
69 SmallVector<Attribute> paramsSto;
70 return wrap(
72 llvm::cast<SymbolRefAttr>(unwrap(name)), unwrapList(numParams, params, paramsSto)
73 )
74 );
75}
77 MlirType type, MlirOperation root, LlzkSymbolLookupResult *result
78) {
79 auto structType = mlir::unwrap_cast<StructType>(type);
80 auto *rootOp = unwrap(root);
81 SymbolTableCollection stc;
82 mlir::FailureOr<llzk::SymbolLookupResult<StructDefOp>> lookup =
83 structType.getDefinition(stc, rootOp);
84
85 if (succeeded(lookup)) {
86 // Allocate the result in the heap and store the pointer in the out var.
87 result->ptr = new llzk::SymbolLookupResultUntyped(std::move(*lookup));
88 }
89 return wrap(lookup);
90}
91
93 MlirType type, MlirModule root, LlzkSymbolLookupResult *result
94) {
95 return llzkStructStructTypeGetDefinition(type, mlirModuleGetOperation(root), result);
96}
97
98//===----------------------------------------------------------------------===//
99// StructDefOp
100//===----------------------------------------------------------------------===//
101
102MlirBlock llzkStruct_StructDefOpGetBody(MlirOperation op) {
103 return wrap(llvm::cast<StructDefOp>(unwrap(op)).getBody());
104}
105
106MlirType llzkStruct_StructDefOpGetType(MlirOperation op) {
107 return wrap(llvm::cast<StructDefOp>(unwrap(op)).getType());
108}
109
110MlirType llzkStruct_StructDefOpGetTypeWithParams(MlirOperation op, MlirAttribute attr) {
111 return wrap(llvm::cast<StructDefOp>(unwrap(op)).getType(llvm::cast<ArrayAttr>(unwrap(attr))));
112}
113
114void llzkStruct_StructDefOpGetMemberDefs(MlirOperation op, MlirOperation *dst) {
115 for (auto [offset, member] :
116 llvm::enumerate(llvm::cast<StructDefOp>(unwrap(op)).getMemberDefs())) {
117 dst[offset] = wrap(member);
118 }
119}
120
121intptr_t llzkStruct_StructDefOpGetNumMemberDefs(MlirOperation op) {
122 return llzk::checkedCast<intptr_t>(llvm::cast<StructDefOp>(unwrap(op)).getMemberDefs().size());
123}
124
126 MlirOperation op, intptr_t *strSize, char *(*alloc_string)(size_t)
127) {
128 auto header = llvm::cast<StructDefOp>(unwrap(op)).getHeaderString();
129 *strSize = llzk::checkedCast<intptr_t>(header.size()) + 1; // Plus one because it's a C string.
130 char *dst = alloc_string(*strSize);
131 dst[header.size()] = 0;
132 memcpy(dst, header.data(), header.size());
133 return dst;
134}
135
136void llzkStruct_StructDefOpGetTemplateParamOpNames(MlirOperation op, MlirAttribute *dst) {
137 for (auto [offset, attr] :
138 llvm::enumerate(llvm::cast<StructDefOp>(unwrap(op)).getTemplateParamOpNames())) {
139 dst[offset] = wrap(attr);
140 }
141}
142
145 llvm::cast<StructDefOp>(unwrap(op)).getTemplateParamOpNames().size()
146 );
147}
148
149void llzkStruct_StructDefOpGetTemplateExprOpNames(MlirOperation op, MlirAttribute *dst) {
150 for (auto [offset, attr] :
151 llvm::enumerate(llvm::cast<StructDefOp>(unwrap(op)).getTemplateExprOpNames())) {
152 dst[offset] = wrap(attr);
153 }
154}
155
158 llvm::cast<StructDefOp>(unwrap(op)).getTemplateExprOpNames().size()
159 );
160}
161
162//===----------------------------------------------------------------------===//
163// MemberDefOp
164//===----------------------------------------------------------------------===//
165
167 Struct, MemberDefOp, MlirStringRef name, MlirType type, bool isSignal, bool isColumn
168) {
169 return mlirOpBuilderInsert(
170 builder,
171 wrap(create<MemberDefOp>(builder, location, unwrap(name), unwrap(type), isSignal, isColumn))
172 );
173}
174
176 Struct, MemberDefOp, WithAttrs, MlirAttribute name, MlirAttribute type, bool isSignal,
177 bool isColumn
178) {
179 return mlirOpBuilderInsert(
180 builder, wrap(
182 builder, location, llvm::cast<StringAttr>(unwrap(name)),
183 llvm::cast<TypeAttr>(unwrap(type)), isSignal, isColumn
184 )
185 )
186 );
187}
188
190 Struct, MemberDefOp, WithNamedAttrs, intptr_t numAttrs, MlirNamedAttribute const *attrs,
191 bool isSignal, bool isColumn
192) {
193 SmallVector<NamedAttribute> attrsSto;
194 return mlirOpBuilderInsert(
195 builder, wrap(
197 builder, location, unwrapList(numAttrs, attrs, attrsSto), isSignal, isColumn
198 )
199 )
200 );
201}
202
204 return llvm::cast<MemberDefOp>(unwrap(op)).getColumn();
205}
206
207void llzkStruct_MemberDefOpSetColumnValue(MlirOperation op, bool newValue) {
208 llvm::cast<MemberDefOp>(unwrap(op)).setColumn(newValue);
209}
210
212 return llvm::cast<MemberDefOp>(unwrap(op)).getSignal();
213}
214
215void llzkStruct_MemberDefOpSetSignalValue(MlirOperation op, bool newValue) {
216 llvm::cast<MemberDefOp>(unwrap(op)).setSignal(newValue);
217}
218
219//===----------------------------------------------------------------------===//
220// MemberReadOp
221//===----------------------------------------------------------------------===//
222
224 Struct, MemberReadOp, MlirType memberType, MlirValue component, MlirIdentifier memberName
225) {
226 return mlirOpBuilderInsert(
227 builder, wrap(
229 builder, location, unwrap(memberType), unwrap(component), unwrap(memberName)
230 )
231 )
232 );
233}
234
236 Struct, MemberReadOp, WithAffineMapDistance, MlirType memberType, MlirValue component,
237 MlirIdentifier memberName, MlirAffineMap map, MlirValueRange mapOperands
238) {
239 SmallVector<Value> mapOperandsSto;
240 auto mapAttr = AffineMapAttr::get(unwrap(map));
241 return mlirOpBuilderInsert(
242 builder, wrap(
244 builder, location, unwrap(memberType), unwrap(component), unwrap(memberName),
245 mapAttr, unwrapList(mapOperands.size, mapOperands.values, mapOperandsSto),
246 mapAttr.getAffineMap().getNumDims()
247 )
248 )
249 );
250}
251
253 Struct, MemberReadOp, WithTemplateSymbolDistance, MlirType memberType, MlirValue component,
254 MlirIdentifier memberName, MlirStringRef symbol
255) {
256 return mlirOpBuilderInsert(
257 builder, wrap(
259 builder, location, unwrap(memberType), unwrap(component), unwrap(memberName),
260 FlatSymbolRefAttr::get(unwrap(builder)->getStringAttr(unwrap(symbol)))
261 )
262 )
263 );
264}
265
267 Struct, MemberReadOp, WithLiteralDistance, MlirType memberType, MlirValue component,
268 MlirIdentifier memberName, int64_t distance
269) {
270 return mlirOpBuilderInsert(
271 builder, wrap(
273 builder, location, unwrap(memberType), unwrap(component), unwrap(memberName),
274 unwrap(builder)->getIndexAttr(distance)
275 )
276 )
277 );
278}
MlirOperation mlirOpBuilderInsert(MlirOpBuilder builder, MlirOperation op)
Inserts op at the current insertion point of builder and returns it.
Definition Builder.cpp:167
MlirType llzkStruct_StructTypeGet(MlirAttribute name)
Creates a llzk::component::StructType.
Definition Struct.cpp:54
intptr_t llzkStruct_StructDefOpGetNumMemberDefs(MlirOperation op)
Returns the number of MemberDefOp operations defined in this struct.
Definition Struct.cpp:121
void llzkStruct_MemberDefOpSetSignalValue(MlirOperation op, bool newValue)
Adds or removes the unit signal attribute according to newValue.
Definition Struct.cpp:215
MlirLogicalResult llzkStructStructTypeGetDefinitionFromModule(MlirType type, MlirModule root, LlzkSymbolLookupResult *result)
Lookups the definition Operation of the given StructType using the given Module as root for the looku...
Definition Struct.cpp:92
MlirType llzkStruct_StructTypeGetWithArrayAttr(MlirAttribute name, MlirAttribute params)
Creates a llzk::component::StructType with an ArrayAttr as parameters.
Definition Struct.cpp:58
void llzkStruct_StructDefOpGetTemplateExprOpNames(MlirOperation op, MlirAttribute *dst)
If this struct.def is within a poly.template, add names of all poly.expr within the poly....
Definition Struct.cpp:149
MlirType llzkStruct_StructTypeGetWithAttrs(MlirAttribute name, intptr_t numParams, MlirAttribute const *params)
Creates a llzk::component::StructType with an array of parameters.
Definition Struct.cpp:66
MlirType llzkStruct_StructDefOpGetTypeWithParams(MlirOperation op, MlirAttribute attr)
Returns the associated StructType to this op using the given const params instead of the parameters d...
Definition Struct.cpp:110
MlirLogicalResult llzkStructStructTypeGetDefinition(MlirType type, MlirOperation root, LlzkSymbolLookupResult *result)
Lookups the definition Operation of the given StructType using the given Operation as root for the lo...
Definition Struct.cpp:76
void llzkStruct_StructDefOpGetMemberDefs(MlirOperation op, MlirOperation *dst)
Fills the given array with the MemberDefOp operations inside this struct.
Definition Struct.cpp:114
void llzkStruct_StructDefOpGetTemplateParamOpNames(MlirOperation op, MlirAttribute *dst)
If this struct.def is within a poly.template, add names of all poly.param within the poly....
Definition Struct.cpp:136
MlirBlock llzkStruct_StructDefOpGetBody(MlirOperation op)
Returns the single body Block within the StructDefOp's Region.
Definition Struct.cpp:102
const char * llzkStruct_StructDefOpGetHeaderString(MlirOperation op, intptr_t *strSize, char *(*alloc_string)(size_t))
Returns the header string of the struct.
Definition Struct.cpp:125
MlirType llzkStruct_StructDefOpGetType(MlirOperation op)
Returns the associated StructType to this op using the const params defined by the op.
Definition Struct.cpp:106
intptr_t llzkStruct_StructDefOpGetNumTemplateParamOpNames(MlirOperation op)
Returns the number of poly.param operations defined within this template.
Definition Struct.cpp:143
bool llzkStruct_MemberDefOpGetColumnValue(MlirOperation op)
Returns whether this member supports offset table accesses.
Definition Struct.cpp:203
void llzkStruct_MemberDefOpSetColumnValue(MlirOperation op, bool newValue)
Adds or removes the unit column attribute according to newValue.
Definition Struct.cpp:207
intptr_t llzkStruct_StructDefOpGetNumTemplateExprOpNames(MlirOperation op)
Returns the number of poly.expr operations defined within this template.
Definition Struct.cpp:156
bool llzkStruct_MemberDefOpGetSignalValue(MlirOperation op)
Returns whether this member is stored as a witness signal.
Definition Struct.cpp:211
This file defines methods symbol lookup across LLZK operations and included files.
static StructType get(::mlir::SymbolRefAttr structName)
Definition Types.cpp.inc:79
#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
void registerTransformationPasses()
constexpr T checkedCast(U u) noexcept
Definition Compare.h:81
mlir::Operation * create(MlirOpBuilder cBuilder, MlirLocation cLocation, Args &&...args)
Creates a new operation using an ODS build method.
Definition Builder.h:41
auto unwrap_cast(auto &from)
Definition Support.h:51
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
MlirValue const * values
Pointer to the first value in the range.
Definition Support.h:49
intptr_t size
Number of values in the range.
Definition Support.h:51