LLZK 3.0.0
An open-source IR for Zero Knowledge (ZK) circuits
Loading...
Searching...
No Matches
Dialect.cpp
Go to the documentation of this file.
1//===-- Dialect.cpp - POD dialect 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
11
17
18#include <mlir/IR/Builders.h>
19#include <mlir/IR/DialectImplementation.h>
20
21#include <llvm/ADT/TypeSwitch.h>
22
23// TableGen'd implementation files
25
26#define GET_TYPEDEF_CLASSES
28
29// Need a complete declaration of storage classes for below
30#define GET_ATTRDEF_CLASSES
32
33using namespace mlir;
34using namespace llzk;
35using namespace llzk::pod;
36
37namespace {
38
39class PODValueCopyDialectInterface final : public ValueCopyDialectInterface {
40public:
41 explicit PODValueCopyDialectInterface(Dialect *owner) : ValueCopyDialectInterface(owner) {}
42
43 bool canMaterializeValueCopy(Type type) const final {
44 auto podType = llvm::dyn_cast<PodType>(type);
45 if (!podType) {
46 return false;
47 }
48 // Copy eligibility is type-based and must also hold for block arguments and read results.
49 // Until copies can recover their instantiation groups, do not synthesize invalid pod.new ops.
50 SmallVector<AffineMapAttr> maps;
51 collectPodMapAttrs(podType, maps);
52 if (!maps.empty()) {
53 return false;
54 }
55 return llvm::all_of(podType.getRecords(), [](RecordAttr record) {
56 return llzk::canMaterializeValueCopy(record.getType());
57 });
58 }
59
60 std::string getValueCopyFailureReason(Type type) const final {
61 auto podType = llvm::dyn_cast<PodType>(type);
62 if (!podType) {
64 }
65 for (RecordAttr record : podType.getRecords()) {
66 if (!llzk::canMaterializeValueCopy(record.getType())) {
67 return "POD record '" + record.getName().getValue().str() +
68 "': " + llzk::getValueCopyFailureReason(record.getType());
69 }
70 }
71 return "POD copy requires affine-map instantiation operands that cannot be recovered";
72 }
73
74 FailureOr<Value>
75 materializeValueCopy(OpBuilder &builder, Location loc, Value source) const final {
76 auto podType = llvm::dyn_cast<PodType>(source.getType());
77 if (!podType || !canMaterializeValueCopy(podType)) {
78 return failure();
79 }
80
81 auto destination = NewPodOp::create(builder, loc, podType);
82 for (RecordAttr record : podType.getRecords()) {
83 auto read = ReadPodOp::create(builder, loc, record.getType(), source, record.getName());
84 FailureOr<Value> copied = llzk::materializeValueCopy(builder, loc, read.getResult());
85 if (failed(copied)) {
86 return failure();
87 }
88 WritePodOp::create(builder, loc, destination.getResult(), record.getName(), *copied);
89 }
90 return destination.getResult();
91 }
92};
93
94} // namespace
95
96//===------------------------------------------------------------------===//
97// PODDialect
98//===------------------------------------------------------------------===//
99
100auto PODDialect::initialize() -> void {
101 // clang-format off
102 addOperations<
103 #define GET_OP_LIST
105 >();
106
107 addTypes<
108 #define GET_TYPEDEF_LIST
110 >();
111
112 addAttributes<
113 #define GET_ATTRDEF_LIST
115 >();
116
117 // clang-format on
118 addInterfaces<LLZKDialectBytecodeInterface<PODDialect>, PODValueCopyDialectInterface>();
119}
Apache License January AND DISTRIBUTION Definitions License shall mean the terms and conditions for and distribution as defined by Sections through of this document Licensor shall mean the copyright owner or entity authorized by the copyright owner that is granting the License Legal Entity shall mean the union of the acting entity and all other entities that control are controlled by or are under common control with that entity For the purposes of this definition control direct or to cause the direction or management of such whether by contract or including but not limited to software source documentation source
Definition LICENSE.txt:28
Dialect hook for materializing independent storage for mutable value types.
Definition ValueCopy.h:25
virtual std::string getValueCopyFailureReason(mlir::Type type) const
Explain why this dialect cannot copy type. Called only after support is rejected.
Definition ValueCopy.cpp:50
static NewPodOp create(::mlir::OpBuilder &builder, ::mlir::Location location, ::llzk::pod::InitializedRecords initialValues={})
Definition Ops.cpp.inc:466
static ReadPodOp create(::mlir::OpBuilder &builder, ::mlir::Location location, ::mlir::Type result, ::mlir::Value pod_ref, ::mlir::StringAttr record_name)
Definition Ops.cpp.inc:784
static WritePodOp create(::mlir::OpBuilder &builder, ::mlir::Location location, ::mlir::Value pod_ref, ::mlir::StringAttr record_name, ::mlir::Value value)
Definition Ops.cpp.inc:1186
void collectPodMapAttrs(Type type, SmallVector< AffineMapAttr > &mapAttrs)
Definition Ops.cpp:279
std::string getValueCopyFailureReason(Type type)
Definition ValueCopy.cpp:56
FailureOr< Value > materializeValueCopy(OpBuilder &builder, Location loc, Value source)
Definition ValueCopy.cpp:68
bool canMaterializeValueCopy(Type type)
Definition ValueCopy.cpp:40