LLZK 0.1.0
Veridise's ZK Language IR
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"
19
20class GlobalDialectOp<string mnemonic, list<Trait> traits = []>
21 : Op<GlobalDialect, mnemonic, traits>;
22
23def LLZK_GlobalDefOp
24 : GlobalDialectOp<"def", [HasParent<"mlir::ModuleOp">,
25 DeclareOpInterfaceMethods<SymbolUserOpInterface>,
26 Symbol]> {
27 let summary = "global value";
28 let description = [{
29 Examples:
30
31 ```llzk
32 // Global constant (denoted by "const" modifier) string.
33 global.def const @s : !string.type = "Hello World!"
34
35 // Global variable (i.e., no "const" modifier) with initial value.
36 global.def @b : i1 = false
37
38 // Uninitialized global variable.
39 global.def @a : !array.type<2,2 x i1>
40 ```
41 }];
42
43 let arguments = (ins SymbolNameAttr:$sym_name, UnitAttr:$constant,
44 TypeAttrOf<GlobalDefType>:$type,
45 DefaultValuedAttr<AnyAttr, "nullptr">:$initial_value);
46
47 let assemblyFormat = [{
48 (`const` $constant^)?
49 $sym_name `:` $type
50 `` custom<GlobalInitialValue>($initial_value, ref($type))
51 attr-dict
52 }];
53
54 let hasVerifier = 1;
55
56 let extraClassDeclaration = [{
57 inline bool isConstant() { return getConstant(); }
58
59 private:
60 static ::mlir::ParseResult parseGlobalInitialValue(::mlir::OpAsmParser &parser,
61 ::mlir::Attribute &initialValue, ::mlir::TypeAttr typeAttr
62 );
63
64 static void printGlobalInitialValue(::mlir::OpAsmPrinter &printer, GlobalDefOp op,
65 ::mlir::Attribute initialValue, ::mlir::TypeAttr typeAttr
66 );
67 }];
68}
69
70class GlobalRefOpBase<string mnemonic, list<Trait> traits = []>
71 : GlobalDialectOp<
72 mnemonic, traits#[DeclareOpInterfaceMethods<GlobalRefOpInterface>,
73 DeclareOpInterfaceMethods<SymbolUserOpInterface>]> {
74 let extraClassDeclaration = [{
75 /// Gets the definition for the `global` referenced in this op.
76 inline ::mlir::FailureOr<SymbolLookupResult<GlobalDefOp>> getGlobalDefOp(::mlir::SymbolTableCollection &tables) {
77 return ::llvm::cast<GlobalRefOpInterface>(getOperation()).getGlobalDefOp(tables);
78 }
79 }];
80}
81
82def LLZK_GlobalReadOp : GlobalRefOpBase<"read"> {
83 let summary = "read value of a global";
84 let description = [{
85 This operation reads the value of a named global.
86 }];
87
88 let arguments = (ins SymbolRefAttr:$name_ref);
89 let results = (outs GlobalDefType:$val);
90
91 let assemblyFormat = [{
92 $name_ref `:` type($val) attr-dict
93 }];
94}
95
96def LLZK_GlobalWriteOp : GlobalRefOpBase<"write", [WitnessGen]> {
97 let summary = "write value to a global";
98 let description = [{
99 This operation writes a value to a named global.
100 Not allowed for globals declared with the "const" modifier.
101 }];
102
103 let arguments = (ins SymbolRefAttr:$name_ref, GlobalDefType:$val);
104
105 let assemblyFormat = [{
106 $name_ref `=` $val `:` type($val) attr-dict
107 }];
108}
109
110#endif // LLZK_GLOBAL_OPS