LLZK 2.1.1
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"
18#include "llzk/Util/Compare.h"
21
22#include <mlir-c/BuiltinAttributes.h>
23#include <mlir-c/Support.h>
24
25#include <mlir/CAPI/AffineMap.h>
26#include <mlir/CAPI/Registration.h>
27#include <mlir/CAPI/Support.h>
28#include <mlir/CAPI/Wrap.h>
29#include <mlir/IR/BuiltinAttributes.h>
30#include <mlir/IR/SymbolTable.h>
31
32#include <llvm/ADT/STLExtras.h>
33
34using namespace mlir;
35using namespace llzk;
36using namespace llzk::component;
37
38// Include the generated CAPI
41
42MLIR_DEFINE_CAPI_DIALECT_REGISTRATION(Struct, llzk__component, StructDialect)
43
44//===----------------------------------------------------------------------===//
45// StructType
46//===----------------------------------------------------------------------===//
47
48MlirType llzkStruct_StructTypeGet(MlirAttribute name) {
49 return wrap(StructType::get(llvm::cast<SymbolRefAttr>(unwrap(name))));
50}
51
52MlirType llzkStruct_StructTypeGetWithArrayAttr(MlirAttribute name, MlirAttribute params) {
53 return wrap(
55 llvm::cast<SymbolRefAttr>(unwrap(name)), llvm::cast<ArrayAttr>(unwrap(params))
56 )
57 );
58}
59
61 MlirAttribute name, intptr_t numParams, MlirAttribute const *params
62) {
63 SmallVector<Attribute> paramsSto;
64 return wrap(
66 llvm::cast<SymbolRefAttr>(unwrap(name)), unwrapList(numParams, params, paramsSto)
67 )
68 );
69}
71 MlirType type, MlirOperation root, LlzkSymbolLookupResult *result
72) {
73 auto structType = mlir::unwrap_cast<StructType>(type);
74 auto *rootOp = unwrap(root);
75 SymbolTableCollection stc;
76 mlir::FailureOr<llzk::SymbolLookupResult<StructDefOp>> lookup =
77 structType.getDefinition(stc, rootOp);
78
79 if (succeeded(lookup)) {
80 // Allocate the result in the heap and store the pointer in the out var.
81 result->ptr = new llzk::SymbolLookupResultUntyped(std::move(*lookup));
82 }
83 return wrap(lookup);
84}
85
87 MlirType type, MlirModule root, LlzkSymbolLookupResult *result
88) {
89 return llzkStructStructTypeGetDefinition(type, mlirModuleGetOperation(root), result);
90}
91
92//===----------------------------------------------------------------------===//
93// StructDefOp
94//===----------------------------------------------------------------------===//
95
96MlirBlock llzkStruct_StructDefOpGetBody(MlirOperation op) {
97 return wrap(llvm::cast<StructDefOp>(unwrap(op)).getBody());
98}
99
100MlirType llzkStruct_StructDefOpGetType(MlirOperation op) {
101 return wrap(llvm::cast<StructDefOp>(unwrap(op)).getType());
102}
103
104MlirType llzkStruct_StructDefOpGetTypeWithParams(MlirOperation op, MlirAttribute attr) {
105 return wrap(llvm::cast<StructDefOp>(unwrap(op)).getType(llvm::cast<ArrayAttr>(unwrap(attr))));
106}
107
108void llzkStruct_StructDefOpGetMemberDefs(MlirOperation op, MlirOperation *dst) {
109 for (auto [offset, member] :
110 llvm::enumerate(llvm::cast<StructDefOp>(unwrap(op)).getMemberDefs())) {
111 dst[offset] = wrap(member);
112 }
113}
114
115intptr_t llzkStruct_StructDefOpGetNumMemberDefs(MlirOperation op) {
116 return llzk::checkedCast<intptr_t>(llvm::cast<StructDefOp>(unwrap(op)).getMemberDefs().size());
117}
118
120 MlirOperation op, intptr_t *strSize, char *(*alloc_string)(size_t)
121) {
122 auto header = llvm::cast<StructDefOp>(unwrap(op)).getHeaderString();
123 *strSize = llzk::checkedCast<intptr_t>(header.size()) + 1; // Plus one because it's a C string.
124 char *dst = alloc_string(*strSize);
125 dst[header.size()] = 0;
126 memcpy(dst, header.data(), header.size());
127 return dst;
128}
129
130void llzkStruct_StructDefOpGetTemplateParamOpNames(MlirOperation op, MlirAttribute *dst) {
131 for (auto [offset, attr] :
132 llvm::enumerate(llvm::cast<StructDefOp>(unwrap(op)).getTemplateParamOpNames())) {
133 dst[offset] = wrap(attr);
134 }
135}
136
139 llvm::cast<StructDefOp>(unwrap(op)).getTemplateParamOpNames().size()
140 );
141}
142
143void llzkStruct_StructDefOpGetTemplateExprOpNames(MlirOperation op, MlirAttribute *dst) {
144 for (auto [offset, attr] :
145 llvm::enumerate(llvm::cast<StructDefOp>(unwrap(op)).getTemplateExprOpNames())) {
146 dst[offset] = wrap(attr);
147 }
148}
149
152 llvm::cast<StructDefOp>(unwrap(op)).getTemplateExprOpNames().size()
153 );
154}
155
156//===----------------------------------------------------------------------===//
157// MemberReadOp
158//===----------------------------------------------------------------------===//
159
161 Struct, MemberReadOp, MlirType memberType, MlirValue component, MlirIdentifier memberName
162) {
163 return wrap(
165 builder, location, unwrap(memberType), unwrap(component), unwrap(memberName)
166 )
167 );
168}
169
171 Struct, MemberReadOp, WithAffineMapDistance, MlirType memberType, MlirValue component,
172 MlirIdentifier memberName, MlirAffineMap map, MlirValueRange mapOperands
173) {
174 SmallVector<Value> mapOperandsSto;
175 auto mapAttr = AffineMapAttr::get(unwrap(map));
176 return wrap(
178 builder, location, unwrap(memberType), unwrap(component), unwrap(memberName), mapAttr,
179 unwrapList(mapOperands.size, mapOperands.values, mapOperandsSto),
180 mapAttr.getAffineMap().getNumDims()
181 )
182 );
183}
184
186 Struct, MemberReadOp, WithTemplateSymbolDistance, MlirType memberType, MlirValue component,
187 MlirIdentifier memberName, MlirStringRef symbol
188) {
189 return wrap(
191 builder, location, unwrap(memberType), unwrap(component), unwrap(memberName),
192 FlatSymbolRefAttr::get(unwrap(builder)->getStringAttr(unwrap(symbol)))
193 )
194 );
195}
196
198 Struct, MemberReadOp, WithLiteralDistance, MlirType memberType, MlirValue component,
199 MlirIdentifier memberName, int64_t distance
200) {
201 return wrap(
203 builder, location, unwrap(memberType), unwrap(component), unwrap(memberName),
204 unwrap(builder)->getIndexAttr(distance)
205 )
206 );
207}
MlirType llzkStruct_StructTypeGet(MlirAttribute name)
Creates a llzk::component::StructType.
Definition Struct.cpp:48
intptr_t llzkStruct_StructDefOpGetNumMemberDefs(MlirOperation op)
Returns the number of MemberDefOp operations defined in this struct.
Definition Struct.cpp:115
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:86
MlirType llzkStruct_StructTypeGetWithArrayAttr(MlirAttribute name, MlirAttribute params)
Creates a llzk::component::StructType with an ArrayAttr as parameters.
Definition Struct.cpp:52
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:143
MlirType llzkStruct_StructTypeGetWithAttrs(MlirAttribute name, intptr_t numParams, MlirAttribute const *params)
Creates a llzk::component::StructType with an array of parameters.
Definition Struct.cpp:60
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:104
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:70
void llzkStruct_StructDefOpGetMemberDefs(MlirOperation op, MlirOperation *dst)
Fills the given array with the MemberDefOp operations inside this struct.
Definition Struct.cpp:108
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:130
MlirBlock llzkStruct_StructDefOpGetBody(MlirOperation op)
Returns the single body Block within the StructDefOp's Region.
Definition Struct.cpp:96
const char * llzkStruct_StructDefOpGetHeaderString(MlirOperation op, intptr_t *strSize, char *(*alloc_string)(size_t))
Returns the header string of the struct.
Definition Struct.cpp:119
MlirType llzkStruct_StructDefOpGetType(MlirOperation op)
Returns the associated StructType to this op using the const params defined by the op.
Definition Struct.cpp:100
intptr_t llzkStruct_StructDefOpGetNumTemplateParamOpNames(MlirOperation op)
Returns the number of poly.param operations defined within this template.
Definition Struct.cpp:137
intptr_t llzkStruct_StructDefOpGetNumTemplateExprOpNames(MlirOperation op)
Returns the number of poly.expr operations defined within this template.
Definition Struct.cpp:150
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
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