LLZK 0.1.0
Veridise's ZK Language IR
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
14struct LLZKDialectVersion : public mlir::DialectVersion {
15 static const LLZKDialectVersion &CurrentVersion();
16
17 static mlir::FailureOr<LLZKDialectVersion> read(mlir::DialectBytecodeReader &reader);
18
20 LLZKDialectVersion(uint64_t majorV, uint64_t minorV, uint64_t patchV)
21 : majorVersion(majorV), minorVersion(minorV), patchVersion(patchV) {}
22
23 void write(mlir::DialectBytecodeWriter &writer) const;
24
25 std::string str() const;
26
27 std::strong_ordering operator<=>(const LLZKDialectVersion &other) const;
28
29 bool operator==(const LLZKDialectVersion &other) const { return std::is_eq(operator<=>(other)); }
30
32};
33
35template <typename DialectTy>
36struct LLZKDialectBytecodeInterface : public mlir::BytecodeDialectInterface {
37
38 LLZKDialectBytecodeInterface(mlir::Dialect *dialect) : mlir::BytecodeDialectInterface(dialect) {}
39
41 void writeVersion(mlir::DialectBytecodeWriter &writer) const override {
42 auto versionOr = writer.getDialectVersion<DialectTy>();
43 // Check if a target version to emit was specified on the writer configs.
44 if (mlir::succeeded(versionOr)) {
45 reinterpret_cast<const LLZKDialectVersion *>(*versionOr)->write(writer);
46 } else {
47 // Otherwise, write the current version
49 }
50 }
51
54 std::unique_ptr<mlir::DialectVersion> readVersion(mlir::DialectBytecodeReader &reader
55 ) const override {
56 auto versionOr = LLZKDialectVersion::read(reader);
57 if (mlir::failed(versionOr)) {
58 return nullptr;
59 }
60 return std::make_unique<LLZKDialectVersion>(std::move(*versionOr));
61 }
62
67 mlir::LogicalResult upgradeFromVersion(
68 mlir::Operation *topLevelOp, const mlir::DialectVersion &version
69 ) const override {
70 auto llzkVersion = reinterpret_cast<const LLZKDialectVersion *>(&version);
71 if (!llzkVersion) {
72 return mlir::success();
73 }
74 const auto &current = LLZKDialectVersion::CurrentVersion();
75 if (*llzkVersion > current) {
76 topLevelOp->emitError(
77 mlir::Twine("Cannot upgrade from current version ") + current.str() +
78 " to future version " + llzkVersion->str()
79 );
80 return mlir::failure();
81 }
82 if (*llzkVersion == current) {
83 // No work to do, versions match.
84 return mlir::success();
85 }
86 // NOTE: This is the point at which upgrade functionality should be added
87 // for backwards compatibility.
88 topLevelOp->emitWarning(
89 mlir::Twine("LLZK version ") + llzkVersion->str() + " is older than current version " +
90 current.str() + " and no upgrade methods have been implemented. Proceed with caution."
91 );
92 return mlir::failure();
93 }
94};
95
96} // namespace llzk
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:54
mlir::LogicalResult upgradeFromVersion(mlir::Operation *topLevelOp, const mlir::DialectVersion &version) const override
Hook invoked after parsing completed, if a version directive was present and included an entry for th...
Definition Versioning.h:67
LLZKDialectBytecodeInterface(mlir::Dialect *dialect)
Definition Versioning.h:38
void writeVersion(mlir::DialectBytecodeWriter &writer) const override
Writes the current version of the LLZK-lib to the given writer.
Definition Versioning.h:41
static const LLZKDialectVersion & CurrentVersion()
LLZKDialectVersion(uint64_t majorV, uint64_t minorV, uint64_t patchV)
Definition Versioning.h:20
bool operator==(const LLZKDialectVersion &other) const
Definition Versioning.h:29
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