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 - Global value 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
12#include "InitializerUtils.h"
13
16
17// TableGen'd implementation files
19
20using namespace mlir;
21using namespace llzk;
22
23//===------------------------------------------------------------------===//
24// GlobalDialect
25//===------------------------------------------------------------------===//
26
27namespace {
28
29class GlobalDialectBytecodeInterface : public LLZKDialectBytecodeInterface<global::GlobalDialect> {
30 using Base = LLZKDialectBytecodeInterface<global::GlobalDialect>;
31
32public:
33 using Base::Base;
34
35 LogicalResult upgradeFromVersion(
36 Operation *root, const LLZKDialectVersion & /*current*/, const LLZKDialectVersion &requested
37 ) const final {
38 auto res = root->walk([](global::GlobalDefOp global) -> WalkResult {
39 if (Attribute initialValue = global.getInitialValueAttr()) {
40 auto errFn = [context = initialValue.getContext()] {
42 };
43 FailureOr<global::NormalizedGlobalInitializer> normalized =
44 global::normalizeGlobalInitializer(global.getType(), initialValue, errFn);
45 if (failed(normalized)) {
46 return global.emitError(
47 "contains a legacy initializer that is incompatible with its declared type"
48 );
49 }
50 global.setInitialValueAttr(normalized->value);
51 global.setTypeAttr(TypeAttr::get(normalized->type));
52 }
53 return WalkResult::advance();
54 });
55 if (res.wasInterrupted()) {
56 return failure();
57 }
58
59 // Before version 3, global reads did not carry a `constant` property.
60 // Reads of const definitions are equivalent to the new `global.read const`
61 // form, so upgrade them to preserve their effect-free semantics.
62 if (requested.majorVersion < 3) {
63 SymbolTableCollection tables;
64 res = root->walk([&tables](global::GlobalReadOp read) -> WalkResult {
65 auto globalRef = llvm::cast<global::GlobalRefOpInterface>(read.getOperation());
66 FailureOr<SymbolLookupResult<global::GlobalDefOp>> target =
67 globalRef.getGlobalDefOp(tables);
68 if (succeeded(target) && target->get().isConstant()) {
69 read.setConstant(true);
70 }
71 return WalkResult::advance();
72 });
73 }
74 return failure(res.wasInterrupted());
75 }
76};
77
78} // namespace
79
80auto global::GlobalDialect::initialize() -> void {
81 // clang-format off
82 addOperations<
83 #define GET_OP_LIST
85 >();
86 // clang-format on
87 addInterfaces<GlobalDialectBytecodeInterface>();
88}
static InFlightDiagnosticWrapper createSilent(mlir::MLIRContext *ctx)
Construct a silent diagnostic that does nothing when appended to or reported.
Definition ErrorHelper.h:77
void setInitialValueAttr(::mlir::Attribute attr)
Definition Ops.h.inc:316
::mlir::Type getType()
Definition Ops.cpp.inc:400
void setTypeAttr(::mlir::TypeAttr attr)
Definition Ops.h.inc:311
::mlir::Attribute getInitialValueAttr()
Definition Ops.h.inc:296
void setConstant(bool attrValue)
Definition Ops.cpp.inc:880
mlir::FailureOr< NormalizedGlobalInitializer > normalizeGlobalInitializer(mlir::Type type, mlir::Attribute value, EmitErrorFn emitError)
Normalize unambiguous initializer representations and their declared type.
This implements the bytecode interface for the LLZK dialect.
Definition Versioning.h:69