LLZK 2.0.0
An open-source IR for Zero Knowledge (ZK) circuits
Loading...
Searching...
No Matches
POD.cpp
Go to the documentation of this file.
1//===-- POD.cpp - POD 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 2026 Project LLZK
6// SPDX-License-Identifier: Apache-2.0
7//
8//===----------------------------------------------------------------------===//
9
10#include "llzk-c/Dialect/POD.h"
11
12#include "llzk-c/Support.h"
13
14#include "llzk/CAPI/Builder.h"
15#include "llzk/CAPI/Support.h"
18
19#include <mlir-c/IR.h>
20
21#include <mlir/CAPI/IR.h>
22#include <mlir/CAPI/Registration.h>
23#include <mlir/CAPI/Support.h>
24#include <mlir/CAPI/Wrap.h>
25#include <mlir/IR/Attributes.h>
26#include <mlir/IR/Diagnostics.h>
27#include <mlir/Support/LLVM.h>
28
29#include <llvm/ADT/STLExtras.h>
30#include <llvm/ADT/SmallVectorExtras.h>
31
32#include <cstdint>
33
34using namespace mlir;
35using namespace llzk;
36using namespace llzk::pod;
37
38// Include the generated CAPI
42
43MLIR_DEFINE_CAPI_DIALECT_REGISTRATION(POD, llzk__pod, PODDialect)
44
45namespace {
46
47static SmallVector<RecordValue>
48fromRawRecordValues(intptr_t nValues, LlzkRecordValue const *values) {
49 return llvm::map_to_vector(ArrayRef(values, nValues), [](const auto &record) {
50 return RecordValue {.name = unwrap(record.name), .value = unwrap(record.value)};
51 });
52}
53
54} // namespace
55
56//===----------------------------------------------------------------------===//
57// RecordAttr
58//===----------------------------------------------------------------------===//
59
60MlirAttribute llzkPod_RecordAttrGetInferredContext(MlirIdentifier name, MlirType type) {
61 auto t = unwrap(type);
62 return wrap(RecordAttr::get(t.getContext(), unwrap(name), t));
63}
64
65//===----------------------------------------------------------------------===//
66// PodType
67//===----------------------------------------------------------------------===//
68
70 MlirContext context, intptr_t nRecords, LlzkRecordValue const *records
71) {
72 auto initialValues = fromRawRecordValues(nRecords, records);
73 return wrap(PodType::fromInitialValues(unwrap(context), initialValues));
74}
75
76void llzkPod_PodTypeGetRecords(MlirType type, MlirAttribute *dst) {
77 auto records = unwrap_cast<PodType>(type).getRecords();
78 MutableArrayRef<MlirAttribute> dstRef(dst, records.size());
79 llvm::transform(records, dstRef.begin(), [](auto record) { return wrap(record); });
80}
81
82namespace {
83
84static MlirType
85lookupRecordImpl(PodType type, StringRef name, llvm::function_ref<InFlightDiagnostic()> emitError) {
86 auto attr = type.getRecord(name, emitError);
87 if (failed(attr)) {
88 return MlirType {.ptr = nullptr};
89 }
90 return wrap(*attr);
91}
92
93} // namespace
94
95MlirType llzkPod_PodTypeLookupRecord(MlirType type, MlirStringRef name) {
96 auto pod = unwrap_cast<PodType>(type);
97 return lookupRecordImpl(pod, unwrap(name), [pod] {
98 auto *ctx = pod.getContext();
99 return ctx->getDiagEngine().emit(Builder(ctx).getUnknownLoc(), DiagnosticSeverity::Error);
100 });
101}
102
103MlirType
104llzkPod_PodTypeLookupRecordWithinLocation(MlirType type, MlirStringRef name, MlirLocation loc) {
105 auto pod = unwrap_cast<PodType>(type);
106 return lookupRecordImpl(pod, unwrap(name), [pod, loc] {
107 return pod.getContext()->getDiagEngine().emit(unwrap(loc), DiagnosticSeverity::Error);
108 });
109}
110
111MlirType
112llzkPod_PodTypeLookupRecordWithinOperation(MlirType type, MlirStringRef name, MlirOperation op) {
113 return lookupRecordImpl(unwrap_cast<PodType>(type), unwrap(name), [op] {
114 return unwrap(op)->emitError();
115 });
116}
117
118//===----------------------------------------------------------------------===//
119// NewPodOp
120//===----------------------------------------------------------------------===//
121
123 Pod, NewPodOp, InferredFromInitialValues, intptr_t nValues, LlzkRecordValue const *values
124) {
125 auto recordValues = fromRawRecordValues(nValues, values);
126 return wrap(create<NewPodOp>(builder, location, recordValues));
127}
128
130 Pod, NewPodOp, MlirType type, intptr_t nValues, LlzkRecordValue const *values
131) {
132 auto recordValues = fromRawRecordValues(nValues, values);
133 return wrap(create<NewPodOp>(builder, location, unwrap_cast<PodType>(type), recordValues));
134}
135
137 Pod, NewPodOp, WithMapOperands, MlirType type, intptr_t nValues, LlzkRecordValue const *values,
139) {
140 auto recordValues = fromRawRecordValues(nValues, values);
141 MapOperandsHelper<> mapOps(mapOperands.nMapOperands, mapOperands.mapOperands);
142 auto numDimsPerMap =
143 llzkAffineMapOperandsBuilderGetDimsPerMapAttr(mapOperands, mlirLocationGetContext(location));
144 return wrap(
146 builder, location, unwrap_cast<PodType>(type), *mapOps,
147 unwrap_cast<DenseI32ArrayAttr>(numDimsPerMap)
148 )
149 );
150}
void llzkPod_PodTypeGetRecords(MlirType type, MlirAttribute *dst)
Writes the records into the given array that must have been previously allocated with enough space.
Definition POD.cpp:76
MlirType llzkPod_PodTypeLookupRecord(MlirType type, MlirStringRef name)
Lookups a record type by name.
Definition POD.cpp:95
MlirAttribute llzkPod_RecordAttrGetInferredContext(MlirIdentifier name, MlirType type)
Creates a new llzk::pod::RecordAttr using the MlirContext of the given type.
Definition POD.cpp:60
MlirType llzkPod_PodTypeLookupRecordWithinLocation(MlirType type, MlirStringRef name, MlirLocation loc)
Lookups a record type by name.
Definition POD.cpp:104
MlirType llzkPod_PodTypeLookupRecordWithinOperation(MlirType type, MlirStringRef name, MlirOperation op)
Lookups a record type by name.
Definition POD.cpp:112
MlirType llzkPod_PodTypeGetFromInitialValues(MlirContext context, intptr_t nRecords, LlzkRecordValue const *records)
Creates an llzk::pod::PodType using a list of values for inferring the records.
Definition POD.cpp:69
MlirAttribute llzkAffineMapOperandsBuilderGetDimsPerMapAttr(LlzkAffineMapOperandsBuilder builder, MlirContext context)
Returns the number of dimensions per map represented as an attribute.
Definition Support.cpp:195
Helper for unwrapping the C arguments for the map operands.
Definition Support.h:61
::llvm::FailureOr<::mlir::Type > getRecord(::llvm::StringRef name, ::llvm::function_ref<::mlir::InFlightDiagnostic()>) const
Searches a record by name.
Definition Types.cpp:50
static PodType fromInitialValues(::mlir::MLIRContext *ctx, InitializedRecords init)
Creates a new type from a set of initialized records.
Definition Types.cpp:42
#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
mlir::Operation * create(MlirOpBuilder cBuilder, MlirLocation cLocation, Args &&...args)
Creates a new operation using an ODS build method.
Definition Builder.h:41
mlir::Location getUnknownLoc(mlir::MLIRContext *context)
Definition Builders.h:23
auto unwrap_cast(auto &from)
Definition Support.h:51
Encapsulates the arguments related to affine maps that are common in operation constructors that supp...
Definition Support.h:103
MlirValueRange * mapOperands
Definition Support.h:107
Information needed to define a pod RecordAttr given a name and a value that will be stored in the rec...
Definition POD.h:47