LLZK 3.0.0
An open-source IR for Zero Knowledge (ZK) circuits
Loading...
Searching...
No Matches
SymbolLookup.h
Go to the documentation of this file.
1//===-- SymbolLookup.h - Symbol Lookup Functions ----------------*- 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//===----------------------------------------------------------------------===//
14//===----------------------------------------------------------------------===//
15
16#pragma once
17
18#include "llzk/Util/Constants.h"
19
20#include <mlir/IR/BuiltinOps.h>
21#include <mlir/IR/Operation.h>
22#include <mlir/IR/OwningOpRef.h>
23
24#include <llvm/ADT/ArrayRef.h>
25#include <llvm/ADT/StringRef.h>
26
27#include <memory>
28#include <variant>
29#include <vector>
30
31namespace llzk {
32
33template <typename T> class SymbolLookupResult;
34
35using ManagedResources = std::shared_ptr<
36 std::pair<mlir::OwningOpRef<mlir::ModuleOp>, std::unique_ptr<mlir::SymbolTableCollection>>>;
37
39public:
40 SymbolLookupResultUntyped() : op(nullptr) {}
41 SymbolLookupResultUntyped(mlir::Operation *opPtr) : op(opPtr) {}
42
44 : op(other.op), managedResources(other.managedResources),
45 includeSymNameStack(other.includeSymNameStack), namespaceStack(other.namespaceStack) {}
46 template <typename T> SymbolLookupResultUntyped(const SymbolLookupResult<T> &other);
47
49 if (this == &other) {
50 return *this;
51 }
52 this->op = other.op;
53 this->managedResources = other.managedResources;
54 this->includeSymNameStack = other.includeSymNameStack;
55 this->namespaceStack = other.namespaceStack;
56 return *this;
57 }
58 template <typename T> SymbolLookupResultUntyped &operator=(const SymbolLookupResult<T> &other);
59
61 : op(other.op), managedResources(std::move(other.managedResources)),
62 includeSymNameStack(std::move(other.includeSymNameStack)),
63 namespaceStack(std::move(other.namespaceStack)) {
64 other.op = nullptr;
65 }
66 template <typename T> SymbolLookupResultUntyped(SymbolLookupResult<T> &&other);
67
69 if (this != &other) {
70 this->op = other.op;
71 other.op = nullptr;
72 this->managedResources = std::move(other.managedResources);
73 this->includeSymNameStack = std::move(other.includeSymNameStack);
74 this->namespaceStack = std::move(other.namespaceStack);
75 }
76 return *this;
77 }
78 template <typename T> SymbolLookupResultUntyped &operator=(SymbolLookupResult<T> &&other);
79
81 mlir::Operation *operator->();
82 mlir::Operation &operator*();
83 mlir::Operation &operator*() const;
84 mlir::Operation *get();
85 mlir::Operation *get() const;
86
88 operator bool() const;
89
91 std::vector<llvm::StringRef> getIncludeSymNames() const { return includeSymNameStack; }
92
95 llvm::ArrayRef<llvm::StringRef> getNamespace() const { return namespaceStack; }
96
98 bool viaInclude() const { return !includeSymNameStack.empty(); }
99
100 mlir::SymbolTableCollection *getSymbolTableCache() {
101 if (managedResources) {
102 return managedResources->second.get();
103 } else {
104 return nullptr;
105 }
106 }
107
109 bool isManaged() const { return managedResources != nullptr; }
110
112 void manage(
113 mlir::OwningOpRef<mlir::ModuleOp> &&ptr, std::unique_ptr<mlir::SymbolTableCollection> tables
114 );
115
117 void trackIncludeAsName(llvm::StringRef includeOpSymName);
118
120 void pushNamespace(llvm::StringRef symName);
121
123 void prependNamespace(llvm::ArrayRef<llvm::StringRef> ns);
124
125 bool operator==(const SymbolLookupResultUntyped &rhs) const { return op == rhs.op; }
126
127private:
128 mlir::Operation *op;
131 ManagedResources managedResources;
133 std::vector<llvm::StringRef> includeSymNameStack;
136 std::vector<llvm::StringRef> namespaceStack;
137
138 friend class Within;
139};
140
141template <typename T> class SymbolLookupResult {
142public:
143 SymbolLookupResult(SymbolLookupResultUntyped &&innerRes) : inner(std::move(innerRes)) {}
144
147 T operator->() { return llvm::dyn_cast<T>(*inner); }
148 T operator*() { return llvm::dyn_cast<T>(*inner); }
149 const T operator*() const { return llvm::dyn_cast<T>(*inner); }
150 T get() { return llvm::dyn_cast<T>(inner.get()); }
151 T get() const { return llvm::dyn_cast<T>(inner.get()); }
152
153 operator bool() const { return inner && llvm::isa<T>(*inner); }
154
156 std::vector<llvm::StringRef> getIncludeSymNames() const { return inner.getIncludeSymNames(); }
157
160 llvm::ArrayRef<llvm::StringRef> getNamespace() const { return inner.getNamespace(); }
161
163 void prependNamespace(llvm::ArrayRef<llvm::StringRef> ns) { inner.prependNamespace(ns); }
164
166 bool viaInclude() const { return inner.viaInclude(); }
167
168 bool operator==(const SymbolLookupResult<T> &rhs) const { return inner == rhs.inner; }
169
171 bool isManaged() const { return inner.isManaged(); }
172
173private:
175
176 friend class Within;
178};
179
180// These methods' definitions need to be here, after the declaration of SymbolLookupResult<T>
181
182template <typename T>
185
186template <typename T>
189 *this = other.inner;
190 return *this;
191}
192
193template <typename T>
196
197template <typename T>
199 *this = std::move(other.inner);
200 return *this;
201}
202
203class Within {
204public:
206 Within() : from(nullptr) {}
208 Within(mlir::Operation *op) : from(op) { assert(op && "cannot lookup within nullptr"); }
210 Within(SymbolLookupResultUntyped &&res) : from(std::move(res)) {}
212 template <typename T> Within(SymbolLookupResult<T> &&res) : Within(std::move(res.inner)) {}
213
214 Within(const Within &) = delete;
215 Within(Within &&other) noexcept : from(std::move(other.from)) {}
216 Within &operator=(const Within &) = delete;
217 Within &operator=(Within &&) noexcept;
218
219 inline static Within root() { return Within(); }
220
221 mlir::FailureOr<SymbolLookupResultUntyped> lookup(
222 mlir::SymbolTableCollection &tables, mlir::SymbolRefAttr symbol, mlir::Operation *origin,
223 bool reportMissing = true
224 ) &&;
225
226private:
227 std::variant<mlir::Operation *, SymbolLookupResultUntyped> from;
228};
229
230inline mlir::FailureOr<SymbolLookupResultUntyped> lookupSymbolIn(
231 mlir::SymbolTableCollection &tables, mlir::SymbolRefAttr symbol, Within &&lookupWithin,
232 mlir::Operation *origin, bool reportMissing = true
233) {
234 return std::move(lookupWithin).lookup(tables, symbol, origin, reportMissing);
235}
236
237inline mlir::FailureOr<SymbolLookupResultUntyped> lookupTopLevelSymbol(
238 mlir::SymbolTableCollection &tables, mlir::SymbolRefAttr symbol, mlir::Operation *origin,
239 bool reportMissing = true
240) {
241 return Within().lookup(tables, symbol, origin, reportMissing);
242}
243
244template <typename T>
245inline mlir::FailureOr<SymbolLookupResult<T>> lookupSymbolIn(
246 mlir::SymbolTableCollection &tables, mlir::SymbolRefAttr symbol, Within &&lookupWithin,
247 mlir::Operation *origin, bool reportMissing = true
248) {
249 auto found = lookupSymbolIn(tables, symbol, std::move(lookupWithin), origin, reportMissing);
250 if (mlir::failed(found)) {
251 return mlir::failure(); // lookupSymbolIn() already emits a sufficient error message
252 }
253 // Keep a copy of the op ptr in case we need it for displaying diagnostics
254 mlir::Operation *op = found->get();
255 // ... since the untyped result gets moved here into a typed result.
256 SymbolLookupResult<T> ret(std::move(*found));
257 if (!ret) {
258 if (reportMissing) {
259 auto diag = origin->emitError() << "symbol \"" << symbol << "\" references a '"
260 << op->getName() << "' but expected ";
261 if constexpr (requires { T::getOperationName(); }) {
262 diag << "a '" << T::getOperationName() << '\'';
263 } else {
264 diag << "a symbol with the requested interface";
265 }
266 return diag;
267 } else {
268 return mlir::failure();
269 }
270 }
271 return ret;
272}
273
274template <typename T>
275inline mlir::FailureOr<SymbolLookupResult<T>> lookupTopLevelSymbol(
276 mlir::SymbolTableCollection &tables, mlir::SymbolRefAttr symbol, mlir::Operation *origin,
277 bool reportMissing = true
278) {
279 return lookupSymbolIn<T>(tables, symbol, Within(), origin, reportMissing);
280}
281
282} // 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
mlir::SymbolTableCollection * getSymbolTableCache()
bool viaInclude() const
Return 'true' if at least one IncludeOp was traversed to load this result.
std::vector< llvm::StringRef > getIncludeSymNames() const
Return the stack of symbol names from the IncludeOp that were traversed to load this result.
SymbolLookupResultUntyped(SymbolLookupResultUntyped &&other) noexcept
void prependNamespace(llvm::ArrayRef< llvm::StringRef > ns)
Adds the given namespace to the beginning of this result's namespace.
void trackIncludeAsName(llvm::StringRef includeOpSymName)
Adds the symbol name from the IncludeOp that caused the module to be loaded.
SymbolLookupResultUntyped(const SymbolLookupResultUntyped &other)
llvm::ArrayRef< llvm::StringRef > getNamespace() const
Return the stack of symbol names from either IncludeOp or ModuleOp that were traversed to load this r...
bool operator==(const SymbolLookupResultUntyped &rhs) const
void manage(mlir::OwningOpRef< mlir::ModuleOp > &&ptr, std::unique_ptr< mlir::SymbolTableCollection > tables)
Adds a pointer to the set of resources the result has to manage the lifetime of.
bool isManaged() const
True iff the symbol is managed (i.e., loaded via an IncludeOp).
SymbolLookupResultUntyped(mlir::Operation *opPtr)
SymbolLookupResultUntyped & operator=(const SymbolLookupResultUntyped &other)
void pushNamespace(llvm::StringRef symName)
Adds the symbol name from an IncludeOp or ModuleOp where the op is contained.
mlir::Operation * operator->()
Access the internal operation.
SymbolLookupResultUntyped & operator=(SymbolLookupResultUntyped &&other) noexcept
void prependNamespace(llvm::ArrayRef< llvm::StringRef > ns)
Adds the given namespace to the beginning of this result's namespace.
llvm::ArrayRef< llvm::StringRef > getNamespace() const
Return the stack of symbol names from either IncludeOp or ModuleOp that were traversed to load this r...
std::vector< llvm::StringRef > getIncludeSymNames() const
Return the stack of symbol names from the IncludeOp that were traversed to load this result.
const T operator*() const
SymbolLookupResult(SymbolLookupResultUntyped &&innerRes)
bool viaInclude() const
Return 'true' if at least one IncludeOp was traversed to load this result.
friend class SymbolLookupResultUntyped
bool isManaged() const
Return 'true' if the inner resource is managed (i.e., loaded via an IncludeOp).
bool operator==(const SymbolLookupResult< T > &rhs) const
T operator->()
Access the internal operation as type T.
mlir::FailureOr< SymbolLookupResultUntyped > lookup(mlir::SymbolTableCollection &tables, mlir::SymbolRefAttr symbol, mlir::Operation *origin, bool reportMissing=true) &&
Within(const Within &)=delete
Within()
Lookup within the top-level (root) module.
Within(SymbolLookupResultUntyped &&res)
Lookup within the Operation of the given result and transfer managed resources.
Within(Within &&other) noexcept
Within & operator=(const Within &)=delete
Within(mlir::Operation *op)
Lookup within the given Operation (cannot be nullptr).
static Within root()
Within(SymbolLookupResult< T > &&res)
Lookup within the Operation of the given result and transfer managed resources.
mlir::FailureOr< SymbolLookupResultUntyped > lookupTopLevelSymbol(mlir::SymbolTableCollection &tables, mlir::SymbolRefAttr symbol, mlir::Operation *origin, bool reportMissing=true)
mlir::FailureOr< SymbolLookupResultUntyped > lookupSymbolIn(mlir::SymbolTableCollection &tables, mlir::SymbolRefAttr symbol, Within &&lookupWithin, mlir::Operation *origin, bool reportMissing=true)
std::shared_ptr< std::pair< mlir::OwningOpRef< mlir::ModuleOp >, std::unique_ptr< mlir::SymbolTableCollection > > > ManagedResources