19#include <mlir/IR/BuiltinOps.h>
20#include <mlir/IR/Operation.h>
21#include <mlir/IR/OwningOpRef.h>
23#include <llvm/ADT/STLExtras.h>
24#include <llvm/ADT/StringRef.h>
25#include <llvm/Support/Debug.h>
27#define DEBUG_TYPE "llzk-symbol-lookup"
31using namespace include;
34SymbolLookupResultUntyped
35lookupSymbolRec(SymbolTableCollection &tables, SymbolRefAttr symbol, Operation *symTableOp) {
39 SmallVector<Operation *, 4> symbolsFound;
40 if (succeeded(tables.lookupSymbolIn(symTableOp, symbol, symbolsFound))) {
41 SymbolLookupResultUntyped ret(symbolsFound.back());
42 for (
auto it = symbolsFound.rbegin(); it != symbolsFound.rend(); ++it) {
45 auto symName = op->getAttrOfType<StringAttr>(SymbolTable::getSymbolAttrName());
46 ret.pushNamespace(symName);
47 if (!llvm::isa<ModuleOp>(op)) {
48 LLVM_DEBUG({ llvm::dbgs() <<
"[lookupSymbolRec] tracking op as include\n"; });
50 ret.trackIncludeAsName(symName);
59 if (Operation *rootOp = tables.lookupSymbolIn(symTableOp, symbol.getRootReference())) {
60 if (IncludeOp rootOpInc = llvm::dyn_cast<IncludeOp>(rootOp)) {
61 FailureOr<OwningOpRef<ModuleOp>> otherMod = rootOpInc.openModule();
62 if (succeeded(otherMod)) {
67 SymbolTableCollection external;
70 result.manage(std::move(*otherMod), std::move(external));
71 auto symName = rootOpInc.getSymName();
72 result.pushNamespace(symName);
73 result.trackIncludeAsName(symName);
77 }
else if (ModuleOp rootOpMod = llvm::dyn_cast<ModuleOp>(rootOp)) {
82 return SymbolLookupResultUntyped();
98SymbolLookupResultUntyped::operator bool()
const {
return op !=
nullptr; }
102 OwningOpRef<ModuleOp> &&ptr, SymbolTableCollection &&tables
107 if (!managedResources) {
108 managedResources = std::make_shared<std::pair<OwningOpRef<ModuleOp>, SymbolTableCollection>>(
109 std::make_pair(std::move(ptr), std::move(tables))
116 includeSymNameStack.push_back(includeOpSymName);
120 namespaceStack.push_back(symName);
124 std::vector<llvm::StringRef> newNamespace = ns;
125 newNamespace.insert(newNamespace.end(), namespaceStack.begin(), namespaceStack.end());
126 namespaceStack = newNamespace;
134 if (
this != &other) {
135 from = std::move(other.from);
141 SymbolTableCollection &tables, SymbolRefAttr symbol, Operation *origin,
bool reportMissing
146 SymbolTableCollection *cachedTablesForRes = priorRes->getSymbolTableCache();
147 if (!cachedTablesForRes) {
148 cachedTablesForRes = &tables;
150 if (
auto found = lookupSymbolRec(*cachedTablesForRes, symbol, priorRes->op)) {
151 assert(!found.managedResources &&
"should not have loaded additional modules");
154 assert(found.includeSymNameStack.empty() &&
"should not have loaded additional modules");
156 found.managedResources = std::move(priorRes->managedResources);
157 found.includeSymNameStack = std::move(priorRes->includeSymNameStack);
162 Operation *lookupFrom = std::get<Operation *>(this->from);
168 lookupFrom =
root.value();
170 if (
auto found = lookupSymbolRec(tables, symbol, lookupFrom)) {
176 return origin->emitOpError() <<
"references unknown symbol \"" << symbol <<
'"';
This file defines methods symbol lookup across LLZK operations and included files.
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.
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.
mlir::Operation & operator*()
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.
mlir::FailureOr< SymbolLookupResultUntyped > lookup(mlir::SymbolTableCollection &tables, mlir::SymbolRefAttr symbol, mlir::Operation *origin, bool reportMissing=true) &&
Within()
Lookup within the top-level (root) module.
Within & operator=(const Within &)=delete
constexpr char LANG_ATTR_NAME[]
Name of the attribute on the top-level ModuleOp that identifies the ModuleOp as the root module and s...
FailureOr< ModuleOp > getRootModule(Operation *from)
mlir::SymbolRefAttr getTailAsSymbolRefAttr(mlir::SymbolRefAttr symbol)
Return SymbolRefAttr like the one given but with the root/head element removed.