LLZK 0.1.0
An open-source IR for Zero Knowledge (ZK) circuits
Loading...
Searching...
No Matches
SymbolHelper.h
Go to the documentation of this file.
1//===-- SymbolHelper.h ------------------------------------------*- C++ -*-===//
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#pragma once
11
13
14#include <mlir/Interfaces/CallInterfaces.h>
15
16#include <ranges>
17
18namespace llzk {
19
20namespace component {
21class StructType;
22class StructDefOp;
23class MemberDefOp;
24} // namespace component
25
26namespace function {
27class FuncDefOp;
28} // namespace function
29
30llvm::SmallVector<mlir::StringRef> getNames(mlir::SymbolRefAttr ref);
31llvm::SmallVector<mlir::FlatSymbolRefAttr> getPieces(mlir::SymbolRefAttr ref);
32
34inline mlir::FlatSymbolRefAttr
35getFlatSymbolRefAttr(mlir::MLIRContext *context, const mlir::Twine &twine) {
36 return mlir::FlatSymbolRefAttr::get(mlir::StringAttr::get(context, twine));
37}
38
40inline mlir::SymbolRefAttr asSymbolRefAttr(mlir::StringAttr root, mlir::SymbolRefAttr tail) {
41 return mlir::SymbolRefAttr::get(root, getPieces(tail));
42}
43
45inline mlir::SymbolRefAttr asSymbolRefAttr(llvm::ArrayRef<mlir::FlatSymbolRefAttr> path) {
46 return mlir::SymbolRefAttr::get(path.front().getAttr(), path.drop_front());
47}
48
50inline mlir::SymbolRefAttr asSymbolRefAttr(std::vector<mlir::FlatSymbolRefAttr> path) {
51 return asSymbolRefAttr(llvm::ArrayRef<mlir::FlatSymbolRefAttr>(path));
52}
53
55inline mlir::SymbolRefAttr getTailAsSymbolRefAttr(mlir::SymbolRefAttr symbol) {
56 return asSymbolRefAttr(symbol.getNestedReferences());
57}
58
60inline mlir::SymbolRefAttr getPrefixAsSymbolRefAttr(mlir::SymbolRefAttr symbol) {
61 return mlir::SymbolRefAttr::get(
62 symbol.getRootReference(), symbol.getNestedReferences().drop_back()
63 );
64}
65
67mlir::SymbolRefAttr replaceLeaf(mlir::SymbolRefAttr orig, mlir::FlatSymbolRefAttr newLeaf);
68inline mlir::SymbolRefAttr replaceLeaf(mlir::SymbolRefAttr orig, mlir::StringAttr newLeaf) {
69 return replaceLeaf(orig, mlir::FlatSymbolRefAttr::get(newLeaf));
70}
71inline mlir::SymbolRefAttr replaceLeaf(mlir::SymbolRefAttr orig, const mlir::Twine &newLeaf) {
72 return replaceLeaf(orig, mlir::StringAttr::get(orig.getContext(), newLeaf));
73}
74
76mlir::SymbolRefAttr appendLeaf(mlir::SymbolRefAttr orig, mlir::FlatSymbolRefAttr newLeaf);
77inline mlir::SymbolRefAttr appendLeaf(mlir::SymbolRefAttr orig, mlir::StringAttr newLeaf) {
78 return appendLeaf(orig, mlir::FlatSymbolRefAttr::get(newLeaf));
79}
80inline mlir::SymbolRefAttr appendLeaf(mlir::SymbolRefAttr orig, const mlir::Twine &newLeaf) {
81 return appendLeaf(orig, mlir::StringAttr::get(orig.getContext(), newLeaf));
82}
83
86mlir::SymbolRefAttr appendLeafName(mlir::SymbolRefAttr orig, const mlir::Twine &newLeafSuffix);
87
90mlir::FailureOr<mlir::ModuleOp> getRootModule(mlir::Operation *from);
91mlir::FailureOr<mlir::SymbolRefAttr>
92getPathFromRoot(mlir::SymbolOpInterface to, mlir::ModuleOp *foundRoot = nullptr);
93mlir::FailureOr<mlir::SymbolRefAttr>
94getPathFromRoot(component::StructDefOp &to, mlir::ModuleOp *foundRoot = nullptr);
95mlir::FailureOr<mlir::SymbolRefAttr>
96getPathFromRoot(component::MemberDefOp &to, mlir::ModuleOp *foundRoot = nullptr);
97mlir::FailureOr<mlir::SymbolRefAttr>
98getPathFromRoot(function::FuncDefOp &to, mlir::ModuleOp *foundRoot = nullptr);
99
102mlir::FailureOr<mlir::ModuleOp> getTopRootModule(mlir::Operation *from);
103mlir::FailureOr<mlir::SymbolRefAttr>
104getPathFromTopRoot(mlir::SymbolOpInterface to, mlir::ModuleOp *foundRoot = nullptr);
105mlir::FailureOr<mlir::SymbolRefAttr>
106getPathFromTopRoot(component::StructDefOp &to, mlir::ModuleOp *foundRoot = nullptr);
107mlir::FailureOr<mlir::SymbolRefAttr>
108getPathFromTopRoot(component::MemberDefOp &to, mlir::ModuleOp *foundRoot = nullptr);
109mlir::FailureOr<mlir::SymbolRefAttr>
110getPathFromTopRoot(function::FuncDefOp &to, mlir::ModuleOp *foundRoot = nullptr);
111
116mlir::FailureOr<llzk::component::StructType> getMainInstanceType(mlir::Operation *lookupFrom);
117
122mlir::FailureOr<SymbolLookupResult<llzk::component::StructDefOp>>
123getMainInstanceDef(mlir::SymbolTableCollection &symbolTable, mlir::Operation *lookupFrom);
124
130template <typename T>
131inline mlir::FailureOr<SymbolLookupResult<T>>
132resolveCallable(mlir::SymbolTableCollection &symbolTable, mlir::CallOpInterface call) {
133 mlir::CallInterfaceCallable callable = call.getCallableForCallee();
134 if (auto symbolVal = llvm::dyn_cast<mlir::Value>(callable)) {
135 return SymbolLookupResult<T>(symbolVal.getDefiningOp());
136 }
137
138 // If the callable isn't a value, lookup the symbol reference.
139 // We first try to resolve in the nearest symbol table, as per the default
140 // MLIR behavior. If the resulting operation is not found, we will then
141 // use the LLZK lookup helpers.
142 auto symbolRef = llvm::cast<mlir::SymbolRefAttr>(callable);
143 mlir::Operation *op = symbolTable.lookupNearestSymbolFrom(call.getOperation(), symbolRef);
144
145 if (op) {
146 return SymbolLookupResult<T>(std::move(op));
147 }
148 // Otherwise, use the top-level lookup.
149 return lookupTopLevelSymbol<T>(symbolTable, symbolRef, call.getOperation());
150}
151
152template <typename T>
153inline mlir::FailureOr<SymbolLookupResult<T>> resolveCallable(mlir::CallOpInterface call) {
154 mlir::SymbolTableCollection symbolTable;
155 return resolveCallable<T>(symbolTable, call);
156}
157
159mlir::LogicalResult verifyParamOfType(
160 mlir::SymbolTableCollection &tables, mlir::SymbolRefAttr param, mlir::Type structOrArrayType,
161 mlir::Operation *origin
162);
163
166mlir::LogicalResult verifyParamsOfType(
167 mlir::SymbolTableCollection &tables, mlir::ArrayRef<mlir::Attribute> tyParams,
168 mlir::Type structOrArrayType, mlir::Operation *origin
169);
170
172mlir::FailureOr<component::StructDefOp> verifyStructTypeResolution(
173 mlir::SymbolTableCollection &tables, component::StructType ty, mlir::Operation *origin
174);
175
177mlir::LogicalResult
178verifyTypeResolution(mlir::SymbolTableCollection &tables, mlir::Operation *origin, mlir::Type type);
179
181template <std::ranges::input_range Range>
182mlir::LogicalResult verifyTypeResolution(
183 mlir::SymbolTableCollection &tables, mlir::Operation *origin, const Range &types
184) {
185 // Check all before returning to present all applicable type errors in one compilation.
186 bool failed = false;
187 for (const auto &t : types) {
188 failed |= mlir::failed(verifyTypeResolution(tables, origin, t));
189 }
190 return mlir::LogicalResult::failure(failed);
191}
192
193} // namespace llzk
Apache License January AND DISTRIBUTION Definitions License shall mean the terms and conditions for and distribution as defined by Sections through of this document Licensor shall mean the copyright owner or entity authorized by the copyright owner that is granting the License Legal Entity shall mean the union of the acting entity and all other entities that control are controlled by or are under common control with that entity For the purposes of this definition control direct or to cause the direction or management of such whether by contract or including but not limited to software source documentation and configuration files Object form shall mean any form resulting from mechanical transformation or translation of a Source including but not limited to compiled object generated and conversions to other media types Work shall mean the work of whether in Source or Object made available under the as indicated by a copyright notice that is included in or attached to the whether in Source or Object that is based or other modifications as a an original work of authorship For the purposes of this Derivative Works shall not include works that remain separable from
Definition LICENSE.txt:45
This file defines methods symbol lookup across LLZK operations and included files.
mlir::SymbolRefAttr getPrefixAsSymbolRefAttr(mlir::SymbolRefAttr symbol)
Return SymbolRefAttr like the one given but with the leaf/final element removed.
SymbolRefAttr appendLeafName(SymbolRefAttr orig, const Twine &newLeafSuffix)
mlir::FlatSymbolRefAttr getFlatSymbolRefAttr(mlir::MLIRContext *context, const mlir::Twine &twine)
Construct a FlatSymbolRefAttr with the given content.
mlir::FailureOr< SymbolLookupResultUntyped > lookupTopLevelSymbol(mlir::SymbolTableCollection &tables, mlir::SymbolRefAttr symbol, mlir::Operation *origin, bool reportMissing=true)
FailureOr< StructType > getMainInstanceType(Operation *lookupFrom)
llvm::SmallVector< StringRef > getNames(SymbolRefAttr ref)
FailureOr< ModuleOp > getRootModule(Operation *from)
SymbolRefAttr appendLeaf(SymbolRefAttr orig, FlatSymbolRefAttr newLeaf)
SymbolRefAttr replaceLeaf(SymbolRefAttr orig, FlatSymbolRefAttr newLeaf)
FailureOr< StructDefOp > verifyStructTypeResolution(SymbolTableCollection &tables, StructType ty, Operation *origin)
mlir::FailureOr< SymbolLookupResult< T > > resolveCallable(mlir::SymbolTableCollection &symbolTable, mlir::CallOpInterface call)
Based on mlir::CallOpInterface::resolveCallable, but using LLZK lookup helpers.
FailureOr< ModuleOp > getTopRootModule(Operation *from)
LogicalResult verifyTypeResolution(SymbolTableCollection &tables, Operation *origin, Type ty)
LogicalResult verifyParamsOfType(SymbolTableCollection &tables, ArrayRef< Attribute > tyParams, Type parameterizedType, Operation *origin)
mlir::SymbolRefAttr asSymbolRefAttr(mlir::StringAttr root, mlir::SymbolRefAttr tail)
Build a SymbolRefAttr that prepends tail with root, i.e., root::tail.
mlir::SymbolRefAttr getTailAsSymbolRefAttr(mlir::SymbolRefAttr symbol)
Return SymbolRefAttr like the one given but with the root/head element removed.
FailureOr< SymbolLookupResult< StructDefOp > > getMainInstanceDef(SymbolTableCollection &symbolTable, Operation *lookupFrom)
FailureOr< SymbolRefAttr > getPathFromTopRoot(SymbolOpInterface to, ModuleOp *foundRoot)
LogicalResult verifyParamOfType(SymbolTableCollection &tables, SymbolRefAttr param, Type parameterizedType, Operation *origin)
llvm::SmallVector< FlatSymbolRefAttr > getPieces(SymbolRefAttr ref)
FailureOr< SymbolRefAttr > getPathFromRoot(SymbolOpInterface to, ModuleOp *foundRoot)