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 return wrap(mlir::cast<StructDefOp>(unwrap(op)).getType(mlir::cast<ArrayAttr>(unwrap(attr))));
80}
81
82MlirOperation llzkStructDefOpGetFieldDef(MlirOperation op, MlirStringRef name) {
83 Builder builder(unwrap(op)->getContext());
84 return wrap(mlir::cast<StructDefOp>(unwrap(op)).getFieldDef(builder.getStringAttr(unwrap(name))));
85}
86
87void llzkStructDefOpGetFieldDefs(MlirOperation op, MlirOperation *dst) {
88 for (auto [offset, field] : llvm::enumerate(mlir::cast<StructDefOp>(unwrap(op)).getFieldDefs())) {
89 dst[offset] = wrap(field);
90 }
91}
92
93intptr_t llzkStructDefOpGetNumFieldDefs(MlirOperation op) {
94 return static_cast<intptr_t>(mlir::cast<StructDefOp>(unwrap(op)).getFieldDefs().size());
95}
96
97MlirLogicalResult llzkStructDefOpGetHasColumns(MlirOperation op) {
98 return wrap(mlir::cast<StructDefOp>(unwrap(op)).hasColumns());
99}
100
101MlirOperation llzkStructDefOpGetComputeFuncOp(MlirOperation op) {
102 return wrap(mlir::cast<StructDefOp>(unwrap(op)).getComputeFuncOp());
103}
104
105MlirOperation llzkStructDefOpGetConstrainFuncOp(MlirOperation op) {
106 return wrap(mlir::cast<StructDefOp>(unwrap(op)).getConstrainFuncOp());
107}
108
109const char *
110llzkStructDefOpGetHeaderString(MlirOperation op, intptr_t *strSize, char *(*alloc_string)(size_t)) {
111 auto header = mlir::cast<StructDefOp>(unwrap(op)).getHeaderString();
112 *strSize = static_cast<intptr_t>(header.size()) + 1; // Plus one because it's a C string.
113 char *dst = alloc_string(*strSize);
114 dst[header.size()] = 0;
115 memcpy(dst, header.data(), header.size());
116 return dst;
117}
118
119bool llzkStructDefOpGetHasParamName(MlirOperation op, MlirStringRef name) {
120 Builder builder(unwrap(op)->getContext());
121 return mlir::cast<StructDefOp>(unwrap(op)).hasParamNamed(builder.getStringAttr(unwrap(name)));
122}
123
124MlirAttribute llzkStructDefOpGetFullyQualifiedName(MlirOperation op) {
125 return wrap(mlir::cast<StructDefOp>(unwrap(op)).getFullyQualifiedName());
126}
127
128bool llzkStructDefOpGetIsMainComponent(MlirOperation op) {
129 return mlir::cast<StructDefOp>(unwrap(op)).isMainComponent();
130}
131
132//===----------------------------------------------------------------------===//
133// FieldDefOp
134//===----------------------------------------------------------------------===//
135
136bool llzkOperationIsAFieldDefOp(MlirOperation op) { return mlir::isa<FieldDefOp>(unwrap(op)); }
137
138bool llzkFieldDefOpGetHasPublicAttr(MlirOperation op) {
139 return mlir::cast<FieldDefOp>(unwrap(op)).hasPublicAttr();
140}
141
142void llzkFieldDefOpSetPublicAttr(MlirOperation op, bool value) {
143 mlir::cast<FieldDefOp>(unwrap(op)).setPublicAttr(value);
144}
145
146//===----------------------------------------------------------------------===//
147// FieldReadOp
148//===----------------------------------------------------------------------===//
149
151 FieldReadOp, MlirType fieldType, MlirValue component, MlirStringRef name
152) {
153 return wrap(create<FieldReadOp>(
154 builder, location, unwrap(fieldType), unwrap(component),
155 unwrap(builder)->getStringAttr(unwrap(name))
156 ));
157}
158
160 FieldReadOp, WithAffineMapDistance, MlirType fieldType, MlirValue component, MlirStringRef name,
161 MlirAffineMap map, MlirValueRange mapOperands, int32_t numDimsPerMap
162) {
163 SmallVector<Value> mapOperandsSto;
164 auto nameAttr = unwrap(builder)->getStringAttr(unwrap(name));
165 auto mapAttr = AffineMapAttr::get(unwrap(map));
166 return wrap(create<FieldReadOp>(
167 builder, location, unwrap(fieldType), unwrap(component), nameAttr, mapAttr,
168 unwrapList(mapOperands.size, mapOperands.values, mapOperandsSto), numDimsPerMap
169 ));
170}
171
173 FieldReadOp, WithConstParamDistance, MlirType fieldType, MlirValue component,
174 MlirStringRef name, MlirStringRef symbol
175) {
176 auto nameAttr = unwrap(builder)->getStringAttr(unwrap(name));
177 return wrap(create<FieldReadOp>(
178 builder, location, unwrap(fieldType), unwrap(component), nameAttr,
179 FlatSymbolRefAttr::get(unwrap(builder)->getStringAttr(unwrap(symbol)))
180 ));
181}
182
184 FieldReadOp, WithLiteralDistance, MlirType fieldType, MlirValue component, MlirStringRef name,
185 int64_t distance
186) {
187 auto nameAttr = unwrap(builder)->getStringAttr(unwrap(name));
188 return wrap(create<FieldReadOp>(
189 builder, location, unwrap(fieldType), unwrap(component), nameAttr,
190 unwrap(builder)->getI64IntegerAttr(distance)
191 ));
192}
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:142
bool llzkFieldDefOpGetHasPublicAttr(MlirOperation op)
Definition Struct.cpp:138
const char * llzkStructDefOpGetHeaderString(MlirOperation op, intptr_t *strSize, char *(*alloc_string)(size_t))
Returns the header string of the struct.
Definition Struct.cpp:110
void llzkStructDefOpGetFieldDefs(MlirOperation op, MlirOperation *dst)
Fills the given array with the FieldDefOp operations inside this struct.
Definition Struct.cpp:87
MlirAttribute llzkStructDefOpGetFullyQualifiedName(MlirOperation op)
Returns a StringAttr with the fully qualified name of the struct.
Definition Struct.cpp:124
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:119
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:136
bool llzkStructDefOpGetIsMainComponent(MlirOperation op)
Definition Struct.cpp:128
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:82
MlirOperation llzkStructDefOpGetComputeFuncOp(MlirOperation op)
Returns the FuncDefOp operation that defines the witness computation of the struct.
Definition Struct.cpp:101
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:93
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:105
MlirLogicalResult llzkStructDefOpGetHasColumns(MlirOperation op)
Returns true if the struct has fields marked as columns.
Definition Struct.cpp:97
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