19#include <mlir/IR/BuiltinOps.h>
20#include <mlir/IR/Operation.h>
21#include <mlir/IR/OwningOpRef.h>
29lookupSymbolRec(SymbolTableCollection &tables, SymbolRefAttr symbol, Operation *symTableOp) {
33 SmallVector<Operation *, 4> symbolsFound;
34 if (succeeded(tables.lookupSymbolIn(symTableOp, symbol, symbolsFound))) {
36 for (
auto it = symbolsFound.rbegin(); it != symbolsFound.rend(); ++it) {
39 ret.trackIncludeAsName(op->getAttrOfType<StringAttr>(SymbolTable::getSymbolAttrName()));
47 if (Operation *rootOp = tables.lookupSymbolIn(symTableOp, symbol.getRootReference())) {
48 if (
IncludeOp rootOpInc = llvm::dyn_cast<IncludeOp>(rootOp)) {
49 FailureOr<OwningOpRef<ModuleOp>> otherMod = rootOpInc.openModule();
50 if (succeeded(otherMod)) {
55 SymbolTableCollection external;
58 result.manage(std::move(*otherMod), std::move(external));
59 result.trackIncludeAsName(rootOpInc.getSymName());
63 }
else if (ModuleOp rootOpMod = llvm::dyn_cast<ModuleOp>(rootOp)) {
84SymbolLookupResultUntyped::operator bool()
const {
return op !=
nullptr; }
88 OwningOpRef<ModuleOp> &&ptr, SymbolTableCollection &&tables
93 if (!managedResources) {
94 managedResources = std::make_shared<std::pair<OwningOpRef<ModuleOp>, SymbolTableCollection>>(
95 std::make_pair(std::move(ptr), std::move(tables))
102 includeSymNameStack.push_back(includeOpSymName);
110 if (
this != &other) {
111 from = std::move(other.from);
117 SymbolTableCollection &tables, SymbolRefAttr symbol, Operation *origin,
bool reportMissing
122 SymbolTableCollection *cachedTablesForRes = priorRes->getSymbolTableCache();
123 if (!cachedTablesForRes) {
124 cachedTablesForRes = &tables;
126 if (
auto found = lookupSymbolRec(*cachedTablesForRes, symbol, priorRes->op)) {
127 assert(!found.managedResources &&
"should not have loaded additional modules");
130 assert(found.includeSymNameStack.empty() &&
"should not have loaded additional modules");
132 found.managedResources = std::move(priorRes->managedResources);
133 found.includeSymNameStack = std::move(priorRes->includeSymNameStack);
138 Operation *lookupFrom = std::get<Operation *>(this->from);
144 lookupFrom =
root.value();
146 if (
auto found = lookupSymbolRec(tables, symbol, lookupFrom)) {
152 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 trackIncludeAsName(llvm::StringRef includeOpSymName)
Adds the symbol name from the IncludeOp that caused the module to be loaded.
mlir::Operation & operator*()
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 specifies the IR language name.
FailureOr< ModuleOp > getRootModule(Operation *from)
mlir::SymbolRefAttr getTailAsSymbolRefAttr(mlir::SymbolRefAttr symbol)
Return SymbolRefAttr like the one given but with the root/head element removed.