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 - Array 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 2025 Veridise Inc.
6// SPDX-License-Identifier: Apache-2.0
7//
8//===----------------------------------------------------------------------===//
9
11
16
17#include <mlir/IR/DialectImplementation.h>
18
19#include <llvm/ADT/TypeSwitch.h>
20
21// TableGen'd implementation files
23
24#define GET_TYPEDEF_CLASSES
26
27namespace {
28
29class ArrayValueCopyDialectInterface final : public llzk::ValueCopyDialectInterface {
30public:
31 explicit ArrayValueCopyDialectInterface(mlir::Dialect *owner)
32 : ValueCopyDialectInterface(owner) {}
33
34 bool canMaterializeValueCopy(mlir::Type type) const final {
35 auto arrayType = llvm::dyn_cast<llzk::array::ArrayType>(type);
36 return arrayType && (llvm::isa<mlir::NoneType>(arrayType.getElementType()) ||
37 (arrayType.hasStaticShape() &&
38 llzk::canMaterializeValueCopy(arrayType.getElementType())));
39 }
40
41 std::string getValueCopyFailureReason(mlir::Type type) const final {
42 auto arrayType = llvm::dyn_cast<llzk::array::ArrayType>(type);
43 if (!arrayType) {
44 return ValueCopyDialectInterface::getValueCopyFailureReason(type);
45 }
46 if (!arrayType.hasStaticShape()) {
47 return "dynamic or symbolic payload arrays cannot be copied independently; "
48 "flatten or resolve array shapes first";
49 }
50 return "array element copy is unsupported: " +
51 llzk::getValueCopyFailureReason(arrayType.getElementType());
52 }
53
54 mlir::FailureOr<mlir::Value> materializeValueCopy(
55 mlir::OpBuilder &builder, mlir::Location loc, mlir::Value source
56 ) const final {
57 auto arrayType = llvm::dyn_cast<llzk::array::ArrayType>(source.getType());
58 if (!arrayType || !canMaterializeValueCopy(arrayType)) {
59 return mlir::failure();
60 }
61
62 // Shape-only arrays have no element payload that can be independently mutated. Their shape is
63 // fixed by the value, so reusing the SSA value preserves copy semantics even for dynamic or
64 // symbolic shapes.
65 if (llvm::isa<mlir::NoneType>(arrayType.getElementType())) {
66 return source;
67 }
68
69 auto destination = llzk::array::CreateArrayOp::create(builder, loc, arrayType);
70 std::optional<mlir::SmallVector<mlir::ArrayAttr>> indices = arrayType.getSubelementIndices();
71 assert(indices.has_value() && "static arrays must provide concrete element indices");
72 for (mlir::ArrayAttr index : *indices) {
73 mlir::Value read = llzk::array::ArrayAccessOpInterface::genRead(builder, loc, source, index);
74 mlir::FailureOr<mlir::Value> copied = llzk::materializeValueCopy(builder, loc, read);
75 if (mlir::failed(copied)) {
76 return mlir::failure();
77 }
79 builder, loc, destination.getResult(), index, *copied
80 );
81 }
82 return destination.getResult();
83 }
84};
85
86} // namespace
87
88//===------------------------------------------------------------------===//
89// ArrayDialect
90//===------------------------------------------------------------------===//
91
92auto llzk::array::ArrayDialect::initialize() -> void {
93 // clang-format off
94 addOperations<
95 #define GET_OP_LIST
97 >();
98
99 addTypes<
100 #define GET_TYPEDEF_LIST
102 >();
103 // clang-format on
104 addInterfaces<LLZKDialectBytecodeInterface<ArrayDialect>, ArrayValueCopyDialectInterface>();
105}
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
static void genWrite(::mlir::OpBuilder &bldr, ::mlir::Location loc, ::mlir::Value arrayRef, ::mlir::ValueRange indices, ::mlir::Value value)
Create an array.write or array.insert for one concrete element or subarray.
::mlir::Value genRead(::mlir::OpBuilder &bldr, ::mlir::Location loc, ::mlir::Value arrayRef, ::mlir::ValueRange indices)
Create an array.read or array.extract for one concrete element or subarray.
static CreateArrayOp create(::mlir::OpBuilder &builder, ::mlir::Location location, ::llzk::array::ArrayType result, ::mlir::ValueRange elements={})
Definition Ops.cpp.inc:718
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