LLZK 3.0.0
An open-source IR for Zero Knowledge (ZK) circuits
Loading...
Searching...
No Matches
ValueCopy.cpp
Go to the documentation of this file.
1//===-- ValueCopy.cpp - Mutable value-copy materialization ------*- 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
15
16#include <mlir/IR/BuiltinTypes.h>
17
18using namespace mlir;
19
20namespace llzk {
21
22namespace {
23
25static bool isIdentityPreservingCopy(Type type) {
26 // Struct values are identity-bearing handles: copies continue to reference the same component.
27 // Keep concrete immutable types explicit so future mutable types cannot inherit this policy.
28 return llvm::isa<
29 IndexType, IntegerType, felt::FeltType, string::StringType, component::StructType>(type);
30}
31
32static const ValueCopyDialectInterface *getValueCopyInterface(Type type) {
33 return type.getDialect().getRegisteredInterface<ValueCopyDialectInterface>();
34}
35
36} // namespace
37
38bool requiresValueCopy(Type type) { return !isIdentityPreservingCopy(type); }
39
40bool canMaterializeValueCopy(Type type) {
41 if (isIdentityPreservingCopy(type)) {
42 return true;
43 }
44 if (const auto *interface = getValueCopyInterface(type)) {
45 return interface->canMaterializeValueCopy(type);
46 }
47 return false;
48}
49
51 std::string reason;
52 llvm::raw_string_ostream(reason) << "unsupported value-copy type '" << type << '\'';
53 return reason;
54}
55
56std::string getValueCopyFailureReason(Type type) {
57 if (canMaterializeValueCopy(type)) {
58 return {};
59 }
60 if (const auto *interface = getValueCopyInterface(type)) {
61 return interface->getValueCopyFailureReason(type);
62 }
63 std::string reason;
64 llvm::raw_string_ostream(reason) << "unsupported value-copy type '" << type << '\'';
65 return reason;
66}
67
68FailureOr<Value> materializeValueCopy(OpBuilder &builder, Location loc, Value source) {
69 Type type = source.getType();
70 if (isIdentityPreservingCopy(type)) {
71 return source;
72 }
73 if (const auto *interface = getValueCopyInterface(type)) {
74 return interface->materializeValueCopy(builder, loc, source);
75 }
76 return failure();
77}
78
79} // namespace llzk
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
std::string getValueCopyFailureReason(Type type)
Definition ValueCopy.cpp:56
FailureOr< Value > materializeValueCopy(OpBuilder &builder, Location loc, Value source)
Definition ValueCopy.cpp:68
bool requiresValueCopy(Type type)
Definition ValueCopy.cpp:38
bool canMaterializeValueCopy(Type type)
Definition ValueCopy.cpp:40