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"
19include "mlir/Interfaces/SideEffectInterfaces.td"
21class GlobalDialectOp<string mnemonic, list<Trait> traits = []>
22 : Op<GlobalDialect, mnemonic, traits>;
25 : GlobalDialectOp<"def", [HasParent<"mlir::ModuleOp">,
26 DeclareOpInterfaceMethods<SymbolUserOpInterface>,
28 let summary = "global value";
33 // Global constant (denoted by "const" modifier) string.
34 global.def const @s : !string.type = "Hello World!"
36 // Global variable (i.e., no "const" modifier) with initial value.
37 global.def @b : i1 = false
39 // Uninitialized global variable.
40 global.def @a : !array.type<2,2 x i1>
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.
50 let arguments = (ins SymbolNameAttr:$sym_name, UnitAttr:$constant,
51 TypeAttrOf<GlobalDefType>:$type,
52 DefaultValuedAttr<AnyAttr, "nullptr">:$initial_value);
54 let hasCustomAssemblyFormat = 1;
57 let extraClassDeclaration = [{
58 inline bool isConstant() { return getConstant(); }
63class GlobalRefOpBase<string mnemonic, list<Trait> traits = []>
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);
75def LLZK_GlobalReadOp : GlobalRefOpBase<"read", [MemoryEffects<[MemRead]>]> {
76 let summary = "read value of a global";
78 This operation reads the value of a named global.
81 let arguments = (ins SymbolRefAttr:$name_ref);
82 let results = (outs GlobalDefType:$val);
84 let assemblyFormat = [{
85 $name_ref `:` type($val) attr-dict
90 : GlobalRefOpBase<"write", [WitnessGen, MemoryEffects<[MemWrite]>]> {
91 let summary = "write value to a global";
93 This operation writes a value to a named global.
94 Not allowed for globals declared with the "const" modifier.
97 let arguments = (ins SymbolRefAttr:$name_ref, GlobalDefType:$val);
99 let assemblyFormat = [{
100 $name_ref `=` $val `:` type($val) attr-dict
104#endif // LLZK_GLOBAL_OPS