LLZK 0.1.0
Veridise's ZK Language IR
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/StringRef.h>
25
26#include <vector>
27
28namespace llzk {
29
31 std::shared_ptr<std::pair<mlir::OwningOpRef<mlir::ModuleOp>, mlir::SymbolTableCollection>>;
32
34public:
35 SymbolLookupResultUntyped() : op(nullptr) {}
36 SymbolLookupResultUntyped(mlir::Operation *op) : op(op) {}
37
39 mlir::Operation *operator->();
40 mlir::Operation &operator*();
41 mlir::Operation &operator*() const;
42 mlir::Operation *get();
43 mlir::Operation *get() const;
44
46 operator bool() const;
47
49 std::vector<llvm::StringRef> getIncludeSymNames() const { return includeSymNameStack; }
50
52 bool viaInclude() const { return !includeSymNameStack.empty(); }
53
54 mlir::SymbolTableCollection *getSymbolTableCache() {
55 if (managedResources) {
56 return &managedResources->second;
57 } else {
58 return nullptr;
59 }
60 }
61
63 void manage(mlir::OwningOpRef<mlir::ModuleOp> &&ptr, mlir::SymbolTableCollection &&tables);
64
66 void trackIncludeAsName(llvm::StringRef includeOpSymName);
67
68 bool operator==(const SymbolLookupResultUntyped &rhs) const { return op == rhs.op; }
69
70private:
71 mlir::Operation *op;
74 ManagedResources managedResources;
76 std::vector<llvm::StringRef> includeSymNameStack;
77
78 friend class Within;
79};
80
81template <typename T> class SymbolLookupResult {
82public:
83 SymbolLookupResult(SymbolLookupResultUntyped &&inner) : inner(std::move(inner)) {}
84
87 T operator->() { return llvm::dyn_cast<T>(*inner); }
88 T operator*() { return llvm::dyn_cast<T>(*inner); }
89 const T operator*() const { return llvm::dyn_cast<T>(*inner); }
90 T get() { return llvm::dyn_cast<T>(inner.get()); }
91 T get() const { return llvm::dyn_cast<T>(inner.get()); }
92
93 operator bool() const { return inner && llvm::isa<T>(*inner); }
94
96 std::vector<llvm::StringRef> getIncludeSymNames() const { return inner.getIncludeSymNames(); }
97
99 bool viaInclude() const { return inner.viaInclude(); }
100
101 bool operator==(const SymbolLookupResult<T> &rhs) const { return inner == rhs.inner; }
102
103private:
105
106 friend class Within;
107};
108
109class Within {
110public:
112 Within() : from(nullptr) {}
114 Within(mlir::Operation *op) : from(op) { assert(op && "cannot lookup within nullptr"); }
116 Within(SymbolLookupResultUntyped &&res) : from(std::move(res)) {}
118 template <typename T> Within(SymbolLookupResult<T> &&res) : Within(std::move(res.inner)) {}
119
120 Within(const Within &) = delete;
121 Within(Within &&other) : from(std::move(other.from)) {}
122 Within &operator=(const Within &) = delete;
124
125 inline static Within root() { return Within(); }
126
127 mlir::FailureOr<SymbolLookupResultUntyped> lookup(
128 mlir::SymbolTableCollection &tables, mlir::SymbolRefAttr symbol, mlir::Operation *origin,
129 bool reportMissing = true
130 ) &&;
131
132private:
133 std::variant<mlir::Operation *, SymbolLookupResultUntyped> from;
134};
135
136inline mlir::FailureOr<SymbolLookupResultUntyped> lookupSymbolIn(
137 mlir::SymbolTableCollection &tables, mlir::SymbolRefAttr symbol, Within &&lookupWithin,
138 mlir::Operation *origin, bool reportMissing = true
139) {
140 return std::move(lookupWithin).lookup(tables, symbol, origin, reportMissing);
141}
142
143inline mlir::FailureOr<SymbolLookupResultUntyped> lookupTopLevelSymbol(
144 mlir::SymbolTableCollection &tables, mlir::SymbolRefAttr symbol, mlir::Operation *origin,
145 bool reportMissing = true
146) {
147 return Within().lookup(tables, symbol, origin, reportMissing);
148}
149
150template <typename T>
151inline mlir::FailureOr<SymbolLookupResult<T>> lookupSymbolIn(
152 mlir::SymbolTableCollection &tables, mlir::SymbolRefAttr symbol, Within &&lookupWithin,
153 mlir::Operation *origin, bool reportMissing = true
154) {
155 auto found = lookupSymbolIn(tables, symbol, std::move(lookupWithin), origin, reportMissing);
156 if (mlir::failed(found)) {
157 return mlir::failure(); // lookupSymbolIn() already emits a sufficient error message
158 }
159 // Keep a copy of the op ptr in case we need it for displaying diagnostics
160 mlir::Operation *op = found->get();
161 // ... since the untyped result gets moved here into a typed result.
162 SymbolLookupResult<T> ret(std::move(*found));
163 if (!ret) {
164 if (reportMissing) {
165 return origin->emitError() << "symbol \"" << symbol << "\" references a '" << op->getName()
166 << "' but expected a '" << T::getOperationName() << '\'';
167 } else {
168 return mlir::failure();
169 }
170 }
171 return ret;
172}
173
174template <typename T>
175inline mlir::FailureOr<SymbolLookupResult<T>> lookupTopLevelSymbol(
176 mlir::SymbolTableCollection &tables, mlir::SymbolRefAttr symbol, mlir::Operation *origin,
177 bool reportMissing = true
178) {
179 return lookupSymbolIn<T>(tables, symbol, Within(), origin, reportMissing);
180}
181
182} // 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.
void manage(mlir::OwningOpRef< mlir::ModuleOp > &&ptr, mlir::SymbolTableCollection &&tables)
Adds a pointer to the set of resources the result has to manage the lifetime of.
std::vector< llvm::StringRef > getIncludeSymNames() const
Return the stack of symbol names from the IncludeOp that were traversed to load this result.
void trackIncludeAsName(llvm::StringRef includeOpSymName)
Adds the symbol name from the IncludeOp that caused the module to be loaded.
bool operator==(const SymbolLookupResultUntyped &rhs) const
SymbolLookupResultUntyped(mlir::Operation *op)
mlir::Operation * operator->()
Access the internal operation.
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
bool viaInclude() const
Return 'true' if at least one IncludeOp was traversed to load this result.
bool operator==(const SymbolLookupResult< T > &rhs) const
T operator->()
Access the internal operation as type T.
SymbolLookupResult(SymbolLookupResultUntyped &&inner)
Within(Within &&other)
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 & 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)
std::shared_ptr< std::pair< mlir::OwningOpRef< mlir::ModuleOp >, mlir::SymbolTableCollection > > ManagedResources
mlir::FailureOr< SymbolLookupResultUntyped > lookupSymbolIn(mlir::SymbolTableCollection &tables, mlir::SymbolRefAttr symbol, Within &&lookupWithin, mlir::Operation *origin, bool reportMissing=true)