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
44 let arguments = (ins SymbolNameAttr:$sym_name, UnitAttr:$constant,
45 TypeAttrOf<GlobalDefType>:$type,
46 DefaultValuedAttr<AnyAttr, "nullptr">:$initial_value);
47
48 let assemblyFormat = [{
49 (`const` $constant^)?
50 $sym_name `:` $type
51 `` custom<GlobalInitialValue>($initial_value, ref($type))
52 attr-dict
53 }];
54
55 let hasVerifier = 1;
56
57 let extraClassDeclaration = [{
58 inline bool isConstant() { return getConstant(); }
59
60 private:
61 static ::mlir::ParseResult parseGlobalInitialValue(::mlir::OpAsmParser &parser,
62 ::mlir::Attribute &initialValue, ::mlir::TypeAttr typeAttr
63 );
64
65 static void printGlobalInitialValue(::mlir::OpAsmPrinter &printer, GlobalDefOp op,
66 ::mlir::Attribute initialValue, ::mlir::TypeAttr typeAttr
67 );
68 }];
69}
70
71class GlobalRefOpBase<string mnemonic, list<Trait> traits = []>
72 : GlobalDialectOp<
73 mnemonic, traits#[DeclareOpInterfaceMethods<GlobalRefOpInterface>,
74 DeclareOpInterfaceMethods<SymbolUserOpInterface>]> {
75 let extraClassDeclaration = [{
76 /// Gets the definition for the `global` referenced in this op.
77 inline ::mlir::FailureOr<SymbolLookupResult<GlobalDefOp>> getGlobalDefOp(::mlir::SymbolTableCollection &tables) {
78 return ::llvm::cast<GlobalRefOpInterface>(getOperation()).getGlobalDefOp(tables);
79 }
80 }];
81}
82
83def LLZK_GlobalReadOp : GlobalRefOpBase<"read", [MemoryEffects<[MemRead]>]> {
84 let summary = "read value of a global";
85 let description = [{
86 This operation reads the value of a named global.
87 }];
88
89 let arguments = (ins SymbolRefAttr:$name_ref);
90 let results = (outs GlobalDefType:$val);
91
92 let assemblyFormat = [{
93 $name_ref `:` type($val) attr-dict
94 }];
95}
96
97def LLZK_GlobalWriteOp
98 : GlobalRefOpBase<"write", [WitnessGen, MemoryEffects<[MemWrite]>]> {
99 let summary = "write value to a global";
100 let description = [{
101 This operation writes a value to a named global.
102 Not allowed for globals declared with the "const" modifier.
103 }];
104
105 let arguments = (ins SymbolRefAttr:$name_ref, GlobalDefType:$val);
106
107 let assemblyFormat = [{
108 $name_ref `=` $val `:` type($val) attr-dict
109 }];
110}
111
112#endif // LLZK_GLOBAL_OPS