LLZK 0.1.0
Veridise's ZK Language IR
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
10#include "llzk/CAPI/Builder.h"
11#include "llzk/CAPI/Support.h"
17
19
20#include <mlir/CAPI/AffineMap.h>
21#include <mlir/CAPI/Registration.h>
22#include <mlir/CAPI/Support.h>
23#include <mlir/CAPI/Wrap.h>
24#include <mlir/IR/BuiltinAttributes.h>
25
26#include <mlir-c/Support.h>
27
28#include <llvm/ADT/STLExtras.h>
29
30using namespace llzk;
31using namespace mlir;
32using namespace llzk::component;
33
35
36//===----------------------------------------------------------------------===//
37// StructType
38//===----------------------------------------------------------------------===//
39
40MlirType llzkStructTypeGet(MlirAttribute name) {
41 return wrap(StructType::get(llvm::cast<SymbolRefAttr>(unwrap(name))));
42}
43
44MlirType llzkStructTypeGetWithArrayAttr(MlirAttribute name, MlirAttribute params) {
45 return wrap(
47 llvm::cast<SymbolRefAttr>(unwrap(name)), llvm::cast<ArrayAttr>(unwrap(params))
48 )
49 );
50}
51
52MlirType
53llzkStructTypeGetWithAttrs(MlirAttribute name, intptr_t numParams, MlirAttribute const *params) {
54 SmallVector<Attribute> paramsSto;
55 return wrap(
57 llvm::cast<SymbolRefAttr>(unwrap(name)), unwrapList(numParams, params, paramsSto)
58 )
59 );
60}
61
62bool llzkTypeIsAStructType(MlirType type) { return llvm::isa<StructType>(unwrap(type)); }
63
64MlirAttribute llzkStructTypeGetName(MlirType type) {
65 return wrap(llvm::cast<StructType>(unwrap(type)).getNameRef());
66}
67
68MlirAttribute llzkStructTypeGetParams(MlirType type) {
69 return wrap(llvm::cast<StructType>(unwrap(type)).getParams());
70}
71
72//===----------------------------------------------------------------------===//
73// StructDefOp
74//===----------------------------------------------------------------------===//
75
76bool llzkOperationIsAStructDefOp(MlirOperation op) { return llvm::isa<StructDefOp>(unwrap(op)); }
77
78MlirRegion llzkStructDefOpGetBodyRegion(MlirOperation op) {
79 return wrap(&llvm::cast<StructDefOp>(unwrap(op)).getBodyRegion());
80}
81
82MlirBlock llzkStructDefOpGetBody(MlirOperation op) {
83 return wrap(llvm::cast<StructDefOp>(unwrap(op)).getBody());
84}
85
86MlirType llzkStructDefOpGetType(MlirOperation op) {
87 return wrap(llvm::cast<StructDefOp>(unwrap(op)).getType());
88}
89
90MlirType llzkStructDefOpGetTypeWithParams(MlirOperation op, MlirAttribute attr) {
91 return wrap(llvm::cast<StructDefOp>(unwrap(op)).getType(llvm::cast<ArrayAttr>(unwrap(attr))));
92}
93
94MlirOperation llzkStructDefOpGetFieldDef(MlirOperation op, MlirStringRef name) {
95 Builder builder(unwrap(op)->getContext());
96 return wrap(llvm::cast<StructDefOp>(unwrap(op)).getFieldDef(builder.getStringAttr(unwrap(name))));
97}
98
99void llzkStructDefOpGetFieldDefs(MlirOperation op, MlirOperation *dst) {
100 for (auto [offset, field] : llvm::enumerate(llvm::cast<StructDefOp>(unwrap(op)).getFieldDefs())) {
101 dst[offset] = wrap(field);
102 }
103}
104
105intptr_t llzkStructDefOpGetNumFieldDefs(MlirOperation op) {
106 return static_cast<intptr_t>(llvm::cast<StructDefOp>(unwrap(op)).getFieldDefs().size());
107}
108
109MlirLogicalResult llzkStructDefOpGetHasColumns(MlirOperation op) {
110 return wrap(llvm::cast<StructDefOp>(unwrap(op)).hasColumns());
111}
112
113MlirOperation llzkStructDefOpGetComputeFuncOp(MlirOperation op) {
114 return wrap(llvm::cast<StructDefOp>(unwrap(op)).getComputeFuncOp());
115}
116
117MlirOperation llzkStructDefOpGetConstrainFuncOp(MlirOperation op) {
118 return wrap(llvm::cast<StructDefOp>(unwrap(op)).getConstrainFuncOp());
119}
120
121const char *
122llzkStructDefOpGetHeaderString(MlirOperation op, intptr_t *strSize, char *(*alloc_string)(size_t)) {
123 auto header = llvm::cast<StructDefOp>(unwrap(op)).getHeaderString();
124 *strSize = static_cast<intptr_t>(header.size()) + 1; // Plus one because it's a C string.
125 char *dst = alloc_string(*strSize);
126 dst[header.size()] = 0;
127 memcpy(dst, header.data(), header.size());
128 return dst;
129}
130
131bool llzkStructDefOpGetHasParamName(MlirOperation op, MlirStringRef name) {
132 Builder builder(unwrap(op)->getContext());
133 return llvm::cast<StructDefOp>(unwrap(op)).hasParamNamed(builder.getStringAttr(unwrap(name)));
134}
135
136MlirAttribute llzkStructDefOpGetFullyQualifiedName(MlirOperation op) {
137 return wrap(llvm::cast<StructDefOp>(unwrap(op)).getFullyQualifiedName());
138}
139
140bool llzkStructDefOpGetIsMainComponent(MlirOperation op) {
141 return llvm::cast<StructDefOp>(unwrap(op)).isMainComponent();
142}
143
144//===----------------------------------------------------------------------===//
145// FieldDefOp
146//===----------------------------------------------------------------------===//
147
148bool llzkOperationIsAFieldDefOp(MlirOperation op) { return llvm::isa<FieldDefOp>(unwrap(op)); }
149
150bool llzkFieldDefOpGetHasPublicAttr(MlirOperation op) {
151 return llvm::cast<FieldDefOp>(unwrap(op)).hasPublicAttr();
152}
153
154void llzkFieldDefOpSetPublicAttr(MlirOperation op, bool value) {
155 llvm::cast<FieldDefOp>(unwrap(op)).setPublicAttr(value);
156}
157
158//===----------------------------------------------------------------------===//
159// FieldReadOp
160//===----------------------------------------------------------------------===//
161
163 FieldReadOp, MlirType fieldType, MlirValue component, MlirStringRef name
164) {
165 return wrap(
167 builder, location, unwrap(fieldType), unwrap(component),
168 unwrap(builder)->getStringAttr(unwrap(name))
169 )
170 );
171}
172
174 FieldReadOp, WithAffineMapDistance, MlirType fieldType, MlirValue component, MlirStringRef name,
175 MlirAffineMap map, MlirValueRange mapOperands, int32_t numDimsPerMap
176) {
177 SmallVector<Value> mapOperandsSto;
178 auto nameAttr = unwrap(builder)->getStringAttr(unwrap(name));
179 auto mapAttr = AffineMapAttr::get(unwrap(map));
180 return wrap(
182 builder, location, unwrap(fieldType), unwrap(component), nameAttr, mapAttr,
183 unwrapList(mapOperands.size, mapOperands.values, mapOperandsSto), numDimsPerMap
184 )
185 );
186}
187
189 FieldReadOp, WithConstParamDistance, MlirType fieldType, MlirValue component,
190 MlirStringRef name, MlirStringRef symbol
191) {
192 auto nameAttr = unwrap(builder)->getStringAttr(unwrap(name));
193 return wrap(
195 builder, location, unwrap(fieldType), unwrap(component), nameAttr,
196 FlatSymbolRefAttr::get(unwrap(builder)->getStringAttr(unwrap(symbol)))
197 )
198 );
199}
200
202 FieldReadOp, WithLiteralDistance, MlirType fieldType, MlirValue component, MlirStringRef name,
203 int64_t distance
204) {
205 auto nameAttr = unwrap(builder)->getStringAttr(unwrap(name));
206 return wrap(
208 builder, location, unwrap(fieldType), unwrap(component), nameAttr,
209 unwrap(builder)->getI64IntegerAttr(distance)
210 )
211 );
212}
MLIR_DEFINE_CAPI_DIALECT_REGISTRATION(Polymorphic, llzk__polymorphic, llzk::polymorphic::PolymorphicDialect) MlirType llzkTypeVarTypeGet(MlirContext ctx
MlirStringRef name
Definition Poly.cpp:48
void llzkFieldDefOpSetPublicAttr(MlirOperation op, bool value)
Sets the public attribute in the given field.
Definition Struct.cpp:154
bool llzkFieldDefOpGetHasPublicAttr(MlirOperation op)
Definition Struct.cpp:150
const char * llzkStructDefOpGetHeaderString(MlirOperation op, intptr_t *strSize, char *(*alloc_string)(size_t))
Returns the header string of the struct.
Definition Struct.cpp:122
void llzkStructDefOpGetFieldDefs(MlirOperation op, MlirOperation *dst)
Fills the given array with the FieldDefOp operations inside this struct.
Definition Struct.cpp:99
MlirAttribute llzkStructDefOpGetFullyQualifiedName(MlirOperation op)
Returns a StringAttr with the fully qualified name of the struct.
Definition Struct.cpp:136
MlirType llzkStructDefOpGetTypeWithParams(MlirOperation op, MlirAttribute attr)
Returns the associated StructType to this op using the given const params instead of the parameters d...
Definition Struct.cpp:90
bool llzkStructDefOpGetHasParamName(MlirOperation op, MlirStringRef name)
Definition Struct.cpp:131
MlirType llzkStructTypeGetWithAttrs(MlirAttribute name, intptr_t numParams, MlirAttribute const *params)
Creates a llzk::component::StructType with an array of parameters.
Definition Struct.cpp:53
MlirAttribute llzkStructTypeGetParams(MlirType type)
Returns the parameter of a llzk::component::StructType as an ArrayAttr.
Definition Struct.cpp:68
MlirType llzkStructTypeGetWithArrayAttr(MlirAttribute name, MlirAttribute params)
Creates a llzk::component::StructType with an ArrayAttr as parameters.
Definition Struct.cpp:44
bool llzkOperationIsAFieldDefOp(MlirOperation op)
Definition Struct.cpp:148
bool llzkStructDefOpGetIsMainComponent(MlirOperation op)
Definition Struct.cpp:140
bool llzkTypeIsAStructType(MlirType type)
Definition Struct.cpp:62
MlirOperation llzkStructDefOpGetFieldDef(MlirOperation op, MlirStringRef name)
Returns the operation that defines the field with the given name, if present.
Definition Struct.cpp:94
MlirOperation llzkStructDefOpGetComputeFuncOp(MlirOperation op)
Returns the FuncDefOp operation that defines the witness computation of the struct.
Definition Struct.cpp:113
MlirBlock llzkStructDefOpGetBody(MlirOperation op)
Returns the single body Block within the StructDefOp's Region.
Definition Struct.cpp:82
MlirRegion llzkStructDefOpGetBodyRegion(MlirOperation op)
Returns the single body Region of the StructDefOp.
Definition Struct.cpp:78
MlirAttribute llzkStructTypeGetName(MlirType type)
Returns the fully qualified name of a llzk::component::StructType.
Definition Struct.cpp:64
intptr_t llzkStructDefOpGetNumFieldDefs(MlirOperation op)
Returns the number of FieldDefOp operations defined in this struct.
Definition Struct.cpp:105
bool llzkOperationIsAStructDefOp(MlirOperation op)
Definition Struct.cpp:76
MlirType llzkStructTypeGet(MlirAttribute name)
Creates a llzk::component::StructType.
Definition Struct.cpp:40
MlirOperation llzkStructDefOpGetConstrainFuncOp(MlirOperation op)
Returns the FuncDefOp operation that defines the constraints of the struct.
Definition Struct.cpp:117
MlirLogicalResult llzkStructDefOpGetHasColumns(MlirOperation op)
Returns true if the struct has fields marked as columns.
Definition Struct.cpp:109
MlirType llzkStructDefOpGetType(MlirOperation op)
Returns the associated StructType to this op using the const params defined by the op.
Definition Struct.cpp:86
static StructType get(::mlir::SymbolRefAttr structName)
Definition Types.cpp.inc:79
#define LLZK_DEFINE_OP_BUILD_METHOD(op,...)
Definition Support.h:27
#define LLZK_DEFINE_SUFFIX_OP_BUILD_METHOD(op, suffix,...)
Definition Support.h:25
mlir::Operation * create(MlirOpBuilder cBuilder, MlirLocation cLocation, Args &&...args)
Creates a new operation using an ODS build method.
Definition Builder.h:41
MlirValue const * values
Definition Support.h:56
intptr_t size
Definition Support.h:57