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(mlir::cast<SymbolRefAttr>(unwrap(name))));
42}
43
44MlirType llzkStructTypeGetWithArrayAttr(MlirAttribute name, MlirAttribute params) {
45 return wrap(StructType::get(
46 mlir::cast<SymbolRefAttr>(unwrap(name)), mlir::cast<ArrayAttr>(unwrap(params))
47 ));
48}
49
50MlirType
51llzkStructTypeGetWithAttrs(MlirAttribute name, intptr_t numParams, MlirAttribute const *params) {
52 SmallVector<Attribute> paramsSto;
53 return wrap(StructType::get(
54 mlir::cast<SymbolRefAttr>(unwrap(name)), unwrapList(numParams, params, paramsSto)
55 ));
56}
57
58bool llzkTypeIsAStructType(MlirType type) { return mlir::isa<StructType>(unwrap(type)); }
59
60MlirAttribute llzkStructTypeGetName(MlirType type) {
61 return wrap(mlir::cast<StructType>(unwrap(type)).getNameRef());
62}
63
64MlirAttribute llzkStructTypeGetParams(MlirType type) {
65 return wrap(mlir::cast<StructType>(unwrap(type)).getParams());
66}
67
68//===----------------------------------------------------------------------===//
69// StructDefOp
70//===----------------------------------------------------------------------===//
71
72bool llzkOperationIsAStructDefOp(MlirOperation op) { return mlir::isa<StructDefOp>(unwrap(op)); }
73
74MlirType llzkStructDefOpGetType(MlirOperation op) {
75 return wrap(mlir::cast<StructDefOp>(unwrap(op)).getType());
76}
77
78MlirType llzkStructDefOpGetTypeWithParams(MlirOperation op, MlirAttribute attr) {
79
80 return wrap(mlir::cast<StructDefOp>(unwrap(op)).getType(mlir::cast<ArrayAttr>(unwrap(attr))));
81}
82
83MlirOperation llzkStructDefOpGetFieldDef(MlirOperation op, MlirStringRef name) {
84 Builder builder(unwrap(op)->getContext());
85 return wrap(mlir::cast<StructDefOp>(unwrap(op)).getFieldDef(builder.getStringAttr(unwrap(name))));
86}
87
88void llzkStructDefOpGetFieldDefs(MlirOperation op, MlirOperation *dst) {
89 for (auto [offset, field] : llvm::enumerate(mlir::cast<StructDefOp>(unwrap(op)).getFieldDefs())) {
90 dst[offset] = wrap(field);
91 }
92}
93
94intptr_t llzkStructDefOpGetNumFieldDefs(MlirOperation op) {
95 return static_cast<intptr_t>(mlir::cast<StructDefOp>(unwrap(op)).getFieldDefs().size());
96}
97
98MlirLogicalResult llzkStructDefOpGetHasColumns(MlirOperation op) {
99 return wrap(mlir::cast<StructDefOp>(unwrap(op)).hasColumns());
100}
101
102MlirOperation llzkStructDefOpGetComputeFuncOp(MlirOperation op) {
103 return wrap(mlir::cast<StructDefOp>(unwrap(op)).getComputeFuncOp());
104}
105
106MlirOperation llzkStructDefOpGetConstrainFuncOp(MlirOperation op) {
107 return wrap(mlir::cast<StructDefOp>(unwrap(op)).getConstrainFuncOp());
108}
109
110const char *
111llzkStructDefOpGetHeaderString(MlirOperation op, intptr_t *strSize, char *(*alloc_string)(size_t)) {
112 auto header = mlir::cast<StructDefOp>(unwrap(op)).getHeaderString();
113 *strSize = static_cast<intptr_t>(header.size()) + 1; // Plus one because it's a C string.
114 char *dst = alloc_string(*strSize);
115 dst[header.size()] = 0;
116 memcpy(dst, header.data(), header.size());
117 return dst;
118}
119
120bool llzkStructDefOpGetHasParamName(MlirOperation op, MlirStringRef name) {
121 Builder builder(unwrap(op)->getContext());
122 return mlir::cast<StructDefOp>(unwrap(op)).hasParamNamed(builder.getStringAttr(unwrap(name)));
123}
124
125MlirAttribute llzkStructDefOpGetFullyQualifiedName(MlirOperation op) {
126 return wrap(mlir::cast<StructDefOp>(unwrap(op)).getFullyQualifiedName());
127}
128
129bool llzkStructDefOpGetIsMainComponent(MlirOperation op) {
130 return mlir::cast<StructDefOp>(unwrap(op)).isMainComponent();
131}
132
133//===----------------------------------------------------------------------===//
134// FieldDefOp
135//===----------------------------------------------------------------------===//
136
137bool llzkOperationIsAFieldDefOp(MlirOperation op) { return mlir::isa<FieldDefOp>(unwrap(op)); }
138
139bool llzkFieldDefOpGetHasPublicAttr(MlirOperation op) {
140 return mlir::cast<FieldDefOp>(unwrap(op)).hasPublicAttr();
141}
142
143void llzkFieldDefOpSetPublicAttr(MlirOperation op, bool value) {
144 mlir::cast<FieldDefOp>(unwrap(op)).setPublicAttr(value);
145}
146
147//===----------------------------------------------------------------------===//
148// FieldReadOp
149//===----------------------------------------------------------------------===//
150
152 FieldReadOp, MlirType fieldType, MlirValue component, MlirStringRef name
153) {
154 return wrap(create<FieldReadOp>(
155 builder, location, unwrap(fieldType), unwrap(component),
156 unwrap(builder)->getStringAttr(unwrap(name))
157 ));
158}
159
161 FieldReadOp, WithAffineMapDistance, MlirType fieldType, MlirValue component, MlirStringRef name,
162 MlirAffineMap map, MlirValueRange mapOperands, int32_t numDimsPerMap
163) {
164 SmallVector<Value> mapOperandsSto;
165 auto nameAttr = unwrap(builder)->getStringAttr(unwrap(name));
166 auto mapAttr = AffineMapAttr::get(unwrap(map));
167 return wrap(create<FieldReadOp>(
168 builder, location, unwrap(fieldType), unwrap(component), nameAttr, mapAttr,
169 unwrapList(mapOperands.size, mapOperands.values, mapOperandsSto), numDimsPerMap
170 ));
171}
172
174 FieldReadOp, WithConstParamDistance, MlirType fieldType, MlirValue component,
175 MlirStringRef name, MlirStringRef symbol
176) {
177 auto nameAttr = unwrap(builder)->getStringAttr(unwrap(name));
178 return wrap(create<FieldReadOp>(
179 builder, location, unwrap(fieldType), unwrap(component), nameAttr,
180 FlatSymbolRefAttr::get(unwrap(builder)->getStringAttr(unwrap(symbol)))
181 ));
182}
183
185 FieldReadOp, WithLiteralDistance, MlirType fieldType, MlirValue component, MlirStringRef name,
186 int64_t distance
187) {
188 auto nameAttr = unwrap(builder)->getStringAttr(unwrap(name));
189 return wrap(create<FieldReadOp>(
190 builder, location, unwrap(fieldType), unwrap(component), nameAttr,
191 unwrap(builder)->getI64IntegerAttr(distance)
192 ));
193}
MLIR_DEFINE_CAPI_DIALECT_REGISTRATION(Polymorphic, llzk__polymorphic, llzk::polymorphic::PolymorphicDialect) MlirType llzkTypeVarTypeGet(MlirContext ctx
MlirStringRef name
void llzkFieldDefOpSetPublicAttr(MlirOperation op, bool value)
Sets the public attribute in the given field.
Definition Struct.cpp:143
bool llzkFieldDefOpGetHasPublicAttr(MlirOperation op)
Definition Struct.cpp:139
const char * llzkStructDefOpGetHeaderString(MlirOperation op, intptr_t *strSize, char *(*alloc_string)(size_t))
Returns the header string of the struct.
Definition Struct.cpp:111
void llzkStructDefOpGetFieldDefs(MlirOperation op, MlirOperation *dst)
Fills the given array with the FieldDefOp operations inside this struct.
Definition Struct.cpp:88
MlirAttribute llzkStructDefOpGetFullyQualifiedName(MlirOperation op)
Returns a StringAttr with the fully qualified name of the struct.
Definition Struct.cpp:125
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:78
bool llzkStructDefOpGetHasParamName(MlirOperation op, MlirStringRef name)
Definition Struct.cpp:120
MlirType llzkStructTypeGetWithAttrs(MlirAttribute name, intptr_t numParams, MlirAttribute const *params)
Creates a llzk::component::StructType with an array of parameters.
Definition Struct.cpp:51
MlirAttribute llzkStructTypeGetParams(MlirType type)
Returns the parameter of a llzk::component::StructType as an ArrayAttr.
Definition Struct.cpp:64
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:137
bool llzkStructDefOpGetIsMainComponent(MlirOperation op)
Definition Struct.cpp:129
bool llzkTypeIsAStructType(MlirType type)
Definition Struct.cpp:58
MlirOperation llzkStructDefOpGetFieldDef(MlirOperation op, MlirStringRef name)
Returns the operation that defines the field with the given name, if present.
Definition Struct.cpp:83
MlirOperation llzkStructDefOpGetComputeFuncOp(MlirOperation op)
Returns the FuncDefOp operation that defines the witness computation of the struct.
Definition Struct.cpp:102
MlirAttribute llzkStructTypeGetName(MlirType type)
Returns the fully qualified name of a llzk::component::StructType.
Definition Struct.cpp:60
intptr_t llzkStructDefOpGetNumFieldDefs(MlirOperation op)
Returns the number of FieldDefOp operations defined in this struct.
Definition Struct.cpp:94
bool llzkOperationIsAStructDefOp(MlirOperation op)
Definition Struct.cpp:72
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:106
MlirLogicalResult llzkStructDefOpGetHasColumns(MlirOperation op)
Returns true if the struct has fields marked as columns.
Definition Struct.cpp:98
MlirType llzkStructDefOpGetType(MlirOperation op)
Returns the associated StructType to this op using the const params defined by the op.
Definition Struct.cpp:74
static StructType get(::mlir::SymbolRefAttr structName)
Definition Types.cpp.inc:69
#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