LLZK 2.0.0
An open-source IR for Zero Knowledge (ZK) circuits
Loading...
Searching...
No Matches
Versioning.h
Go to the documentation of this file.
1//===-- Versioning.h --------------------------------------------*- 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 <mlir/Bytecode/BytecodeImplementation.h>
11
12namespace llzk {
13
16static constexpr auto kV1ConstParamsAttr = "llzk.v1_const_params";
17
18struct LLZKDialectVersion : public mlir::DialectVersion {
19 static const LLZKDialectVersion &CurrentVersion();
20
21 static mlir::FailureOr<LLZKDialectVersion> read(mlir::DialectBytecodeReader &reader);
22
24 LLZKDialectVersion(uint64_t majorV, uint64_t minorV, uint64_t patchV)
25 : majorVersion(majorV), minorVersion(minorV), patchVersion(patchV) {}
26
27 void write(mlir::DialectBytecodeWriter &writer) const;
28
29 std::string str() const;
30
31 std::strong_ordering operator<=>(const LLZKDialectVersion &other) const;
32
33 bool operator==(const LLZKDialectVersion &other) const { return std::is_eq(operator<=>(other)); }
34
36};
37
39template <typename DialectTy>
40struct LLZKDialectBytecodeInterface : public mlir::BytecodeDialectInterface {
41
42 LLZKDialectBytecodeInterface(mlir::Dialect *dia) : mlir::BytecodeDialectInterface(dia) {}
43
45 void writeVersion(mlir::DialectBytecodeWriter &writer) const override {
46 auto versionOr = writer.getDialectVersion<DialectTy>();
47 // Check if a target version to emit was specified on the writer configs.
48 if (mlir::succeeded(versionOr)) {
49 reinterpret_cast<const LLZKDialectVersion *>(*versionOr)->write(writer);
50 } else {
51 // Otherwise, write the current version
53 }
54 }
55
58 std::unique_ptr<mlir::DialectVersion>
59 readVersion(mlir::DialectBytecodeReader &reader) const override {
60 auto versionOr = LLZKDialectVersion::read(reader);
61 if (mlir::failed(versionOr)) {
62 return nullptr;
63 }
64 return std::make_unique<LLZKDialectVersion>(std::move(*versionOr));
65 }
66
71 mlir::LogicalResult
72 upgradeFromVersion(mlir::Operation *rootOp, const mlir::DialectVersion &version) const final {
73 const auto &requested = static_cast<const LLZKDialectVersion &>(version);
74 const auto &current = LLZKDialectVersion::CurrentVersion();
75 if (requested == current) {
76 return mlir::success(); // versions match, nothing to do
77 }
78 if (requested > current) {
79 return rootOp->emitError().append(
80 "Cannot upgrade from current version ", current.str(), " to future version ",
81 requested.str()
82 );
83 }
84 return upgradeFromVersion(rootOp, current, requested);
85 }
86
87 virtual mlir::LogicalResult upgradeFromVersion(
88 mlir::Operation *rootOp, const LLZKDialectVersion &current,
89 const LLZKDialectVersion &requested
90 ) const {
91 assert(requested < current && "pre-condition of upgradeFromVersion not met");
92 // Default implementation does nothing. Dialects that have breaking changes between versions
93 // should override this method to perform the necessary upgrade transformations.
94 return mlir::success();
95 }
96};
97
98} // namespace llzk
virtual mlir::LogicalResult upgradeFromVersion(mlir::Operation *rootOp, const LLZKDialectVersion &current, const LLZKDialectVersion &requested) const
Definition Versioning.h:87
mlir::LogicalResult upgradeFromVersion(mlir::Operation *rootOp, const mlir::DialectVersion &version) const final
Hook invoked for each custom dialect after parsing is completed if a version directive was present an...
Definition Versioning.h:72
std::unique_ptr< mlir::DialectVersion > readVersion(mlir::DialectBytecodeReader &reader) const override
Read the version of this dialect from the provided reader and return it as a unique_ptr to a dialect ...
Definition Versioning.h:59
void writeVersion(mlir::DialectBytecodeWriter &writer) const override
Writes the current version of the LLZK-lib to the given writer.
Definition Versioning.h:45
LLZKDialectBytecodeInterface(mlir::Dialect *dia)
Definition Versioning.h:42
static const LLZKDialectVersion & CurrentVersion()
LLZKDialectVersion(uint64_t majorV, uint64_t minorV, uint64_t patchV)
Definition Versioning.h:24
bool operator==(const LLZKDialectVersion &other) const
Definition Versioning.h:33
std::strong_ordering operator<=>(const LLZKDialectVersion &other) const
static mlir::FailureOr< LLZKDialectVersion > read(mlir::DialectBytecodeReader &reader)
void write(mlir::DialectBytecodeWriter &writer) const
std::string str() const