1//===-- Ops.td ---------------------------------------------*- tablegen -*-===//
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
8//===----------------------------------------------------------------------===//
10#ifndef LLZK_GLOBAL_OPS
11#define LLZK_GLOBAL_OPS
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"
18include "mlir/IR/SymbolInterfaces.td"
20class GlobalDialectOp<string mnemonic, list<Trait> traits = []>
21 : Op<GlobalDialect, mnemonic, traits>;
24 : GlobalDialectOp<"def", [HasParent<"mlir::ModuleOp">,
25 DeclareOpInterfaceMethods<SymbolUserOpInterface>,
27 let summary = "global value";
32 // Global constant (denoted by "const" modifier) string.
33 global.def const @s : !string.type = "Hello World!"
35 // Global variable (i.e. no "const" modifier) with initial value.
36 global.def @b : i1 = false
38 // Uninitialized global variable.
39 global.def @a : !array.type<2,2 x i1>
43 let arguments = (ins SymbolNameAttr:$sym_name, UnitAttr:$constant,
44 TypeAttrOf<GlobalDefType>:$type,
45 DefaultValuedAttr<AnyAttr, "nullptr">:$initial_value);
47 let assemblyFormat = [{
50 `` custom<GlobalInitialValue>($initial_value, ref($type))
56 let extraClassDeclaration = [{
57 static ::mlir::ParseResult parseGlobalInitialValue(::mlir::OpAsmParser &parser,
58 ::mlir::Attribute &initialValue, ::mlir::TypeAttr typeAttr
60 static void printGlobalInitialValue(::mlir::OpAsmPrinter &printer, GlobalDefOp op,
61 ::mlir::Attribute initialValue, ::mlir::TypeAttr typeAttr
64 inline bool isConstant() { return getConstant(); }
68class GlobalRefOpBase<string mnemonic, list<Trait> traits = []>
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);
80def LLZK_GlobalReadOp : GlobalRefOpBase<"read"> {
81 let summary = "read value of a global";
83 This operation reads the value of a named global.
86 let arguments = (ins SymbolRefAttr:$name_ref);
87 let results = (outs GlobalDefType:$val);
89 let assemblyFormat = [{
90 $name_ref `:` type($val) attr-dict
94def LLZK_GlobalWriteOp : GlobalRefOpBase<"write", [WitnessGen]> {
95 let summary = "write value to a global";
97 This operation writes a value to a named global.
98 Not allowed for globals declared with the "const" modifier.
101 let arguments = (ins SymbolRefAttr:$name_ref, GlobalDefType:$val);
103 let assemblyFormat = [{
104 $name_ref `=` $val `:` type($val) attr-dict
108#endif // LLZK_GLOBAL_OPS