LLZK 3.0.0
An open-source IR for Zero Knowledge (ZK) circuits
Loading...
Searching...
No Matches
Ops.td
Go to the documentation of this file.
1//===-- Ops.td ---------------------------------------------*- tablegen -*-===//
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#ifndef LLZK_GLOBAL_OPS
11#define LLZK_GLOBAL_OPS
12
13include "llzk/Dialect/Shared/Types.td"
14include "llzk/Dialect/Global/IR/Dialect.td"
15include "llzk/Dialect/Global/IR/OpInterfaces.td"
16include "llzk/Dialect/Function/IR/OpTraits.td"
17
18include "mlir/IR/SymbolInterfaces.td"
19include "mlir/Interfaces/SideEffectInterfaces.td"
20
21class GlobalDialectOp<string mnemonic, list<Trait> traits = []>
22 : Op<GlobalDialect, mnemonic, traits>;
23
24def LLZK_GlobalDefOp
25 : GlobalDialectOp<"def", [HasParent<"mlir::ModuleOp">,
26 DeclareOpInterfaceMethods<SymbolUserOpInterface>,
27 Symbol]> {
28 let summary = "global value";
29 let description = [{
30 Examples:
31
32 ```llzk
33 // Global constant (denoted by "const" modifier) string.
34 global.def const @s : !string.type = "Hello World!"
35
36 // Global variable (i.e., no "const" modifier) with initial value.
37 global.def @b : i1 = false
38
39 // Uninitialized global variable.
40 global.def @a : !array.type<2,2 x i1>
41 ```
42
43 An initializer's typed attributes must exactly match the declared global
44 type. When parsing a felt initializer, an explicitly selected field
45 refines an unspecified declared felt type; conversely, an unspecified
46 felt initializer adopts an explicitly declared field. Thus, an initialized
47 global always stores matching declared and initializer felt types.
48 }];
49
50 let arguments = (ins SymbolNameAttr:$sym_name, UnitAttr:$constant,
51 TypeAttrOf<GlobalDefType>:$type,
52 DefaultValuedAttr<AnyAttr, "nullptr">:$initial_value);
53
54 let hasCustomAssemblyFormat = 1;
55 let hasVerifier = 1;
56
57 let extraClassDeclaration = [{
58 /// Returns true when this def is marked as `const`.
59 inline bool isConstant() { return getConstant(); }
60 }];
61}
62
63class GlobalRefOpBase<string mnemonic, list<Trait> traits = []>
64 : GlobalDialectOp<
65 mnemonic, traits#[DeclareOpInterfaceMethods<GlobalRefOpInterface>,
66 DeclareOpInterfaceMethods<SymbolUserOpInterface>]> {
67 let extraClassDeclaration = [{
68 /// Gets the definition for the `global` referenced in this op.
69 inline ::mlir::FailureOr<SymbolLookupResult<GlobalDefOp>> getGlobalDefOp(::mlir::SymbolTableCollection &tables) {
70 return ::llvm::cast<GlobalRefOpInterface>(getOperation()).getGlobalDefOp(tables);
71 }
72 }];
73}
74
75def LLZK_GlobalReadOp
76 : GlobalRefOpBase<
77 "read", [DeclareOpInterfaceMethods<MemoryEffectsOpInterface>]> {
78 let summary = "read value of a global";
79 let description = [{
80 This operation copies the value of a named global. The result is a value copy,
81 not a pointer or alias to the global's storage. Reads marked `const` must target
82 a global declared with the `const` modifier and thus have no memory effects.
83 }];
84
85 let arguments = (ins UnitAttr:$constant, SymbolRefAttr:$name_ref);
86 let results = (outs GlobalDefType:$val);
87
88 let assemblyFormat = [{
89 (`const` $constant^)? $name_ref `:` type($val) attr-dict
90 }];
91
92 // `constant` was added in version 3. Preserve the v2 bytecode layout, which
93 // contains only `name_ref`, when decoding older bytecode.
94 let useCustomPropertiesEncoding = 1;
95
96 let extraClassDeclaration = [{
97 /// Returns true when this read is marked as `const`.
98 inline bool isConstant() { return getConstant(); }
99 }];
100}
101
102def LLZK_GlobalWriteOp
103 : GlobalRefOpBase<
104 "write", [WitnessGen,
105 MemoryEffects<[MemWrite<GlobalMemoryResource>]>]> {
106 let summary = "write value to a global";
107 let description = [{
108 This operation copies a value to a named global. It does not create a pointer or
109 alias between the source value and the global's storage.
110 Not allowed for globals declared with the "const" modifier.
111 }];
112
113 let arguments = (ins SymbolRefAttr:$name_ref, GlobalDefType:$val);
114
115 let assemblyFormat = [{
116 $name_ref `=` $val `:` type($val) attr-dict
117 }];
118}
119
120#endif // LLZK_GLOBAL_OPS