LLZK 3.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
12#include <llvm/ADT/APInt.h>
13
14#include <limits>
15
16namespace llzk {
17
20static constexpr auto kV1ConstParamsAttr = "llzk.v1_const_params";
21
22struct LLZKDialectVersion : public mlir::DialectVersion {
23 static const LLZKDialectVersion &CurrentVersion();
24
25 static mlir::FailureOr<LLZKDialectVersion> read(mlir::DialectBytecodeReader &reader);
26
28 LLZKDialectVersion(uint64_t majorV, uint64_t minorV, uint64_t patchV)
29 : majorVersion(majorV), minorVersion(minorV), patchVersion(patchV) {}
30
31 void write(mlir::DialectBytecodeWriter &writer) const;
32
33 std::string str() const;
34
35 std::strong_ordering operator<=>(const LLZKDialectVersion &other) const;
36
37 bool operator==(const LLZKDialectVersion &other) const { return std::is_eq(operator<=>(other)); }
38
40};
41
44inline void writeAPInt(mlir::DialectBytecodeWriter &writer, const llvm::APInt &value) {
45 writer.writeVarInt(value.getBitWidth());
46 writer.writeAPIntWithKnownWidth(value);
47}
48
50inline mlir::FailureOr<llvm::APInt> readAPInt(mlir::DialectBytecodeReader &reader) {
51 uint64_t bitWidth;
52 if (mlir::failed(reader.readVarInt(bitWidth))) {
53 return mlir::failure();
54 }
55 if (bitWidth > std::numeric_limits<unsigned>::max()) {
56 return reader.emitError("APInt bit width too large");
57 }
58 return reader.readAPIntWithKnownWidth(static_cast<unsigned>(bitWidth));
59}
60
62template <typename DialectTy>
63struct LLZKDialectBytecodeInterface : public mlir::BytecodeDialectInterface {
64
65 LLZKDialectBytecodeInterface(mlir::Dialect *dia) : mlir::BytecodeDialectInterface(dia) {}
66
68 void writeVersion(mlir::DialectBytecodeWriter &writer) const override {
69 auto versionOr = writer.getDialectVersion<DialectTy>();
70 // Check if a target version to emit was specified on the writer configs.
71 if (mlir::succeeded(versionOr)) {
72 reinterpret_cast<const LLZKDialectVersion *>(*versionOr)->write(writer);
73 } else {
74 // Otherwise, write the current version
76 }
77 }
78
81 std::unique_ptr<mlir::DialectVersion>
82 readVersion(mlir::DialectBytecodeReader &reader) const override {
83 auto versionOr = LLZKDialectVersion::read(reader);
84 if (mlir::failed(versionOr)) {
85 return nullptr;
86 }
87 return std::make_unique<LLZKDialectVersion>(std::move(*versionOr));
88 }
89
94 mlir::LogicalResult
95 upgradeFromVersion(mlir::Operation *rootOp, const mlir::DialectVersion &version) const final {
96 const auto &requested = static_cast<const LLZKDialectVersion &>(version);
97 const auto &current = LLZKDialectVersion::CurrentVersion();
98 if (requested == current) {
99 return mlir::success(); // versions match, nothing to do
100 }
101 if (requested > current) {
102 return rootOp->emitError().append(
103 "Cannot upgrade from current version ", current.str(), " to future version ",
104 requested.str()
105 );
106 }
107 return upgradeFromVersion(rootOp, current, requested);
108 }
109
110 virtual mlir::LogicalResult upgradeFromVersion(
111 mlir::Operation * /*rootOp*/, const LLZKDialectVersion &current,
112 const LLZKDialectVersion &requested
113 ) const {
114 assert(requested < current && "pre-condition of upgradeFromVersion not met");
115 // Default implementation does nothing. Dialects that have breaking changes between versions
116 // should override this method to perform the necessary upgrade transformations.
117 return mlir::success();
118 }
119};
120
121} // namespace llzk
void writeAPInt(mlir::DialectBytecodeWriter &writer, const llvm::APInt &value)
Write an APInt with its bit width, so the bytecode reader can use MLIR's native APInt payload encodin...
Definition Versioning.h:44
mlir::FailureOr< llvm::APInt > readAPInt(mlir::DialectBytecodeReader &reader)
Read an APInt written by writeAPInt.
Definition Versioning.h:50
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:95
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:82
virtual mlir::LogicalResult upgradeFromVersion(mlir::Operation *, const LLZKDialectVersion &current, const LLZKDialectVersion &requested) const
Definition Versioning.h:110
void writeVersion(mlir::DialectBytecodeWriter &writer) const override
Writes the current version of the LLZK-lib to the given writer.
Definition Versioning.h:68
LLZKDialectBytecodeInterface(mlir::Dialect *dia)
Definition Versioning.h:65
static const LLZKDialectVersion & CurrentVersion()
LLZKDialectVersion(uint64_t majorV, uint64_t minorV, uint64_t patchV)
Definition Versioning.h:28
bool operator==(const LLZKDialectVersion &other) const
Definition Versioning.h:37
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