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