FLANG
LazySymbolTable.h
1//===-- Optimizer/Support/LazySymbolTable.h ---------------------*- C++ -*-===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// Lazy symbol table: build an mlir::SymbolTable only when lookups are needed,
10// and use its map for O(1) lookups instead of repeatedly walking the module
11// (SymbolTable::lookupNearestSymbolFrom is linear in the number of symbols).
12//
13//===----------------------------------------------------------------------===//
14
15#ifndef FORTRAN_OPTIMIZER_SUPPORT_LAZYSYMBOLTABLE_H
16#define FORTRAN_OPTIMIZER_SUPPORT_LAZYSYMBOLTABLE_H
17
18#include "mlir/IR/BuiltinOps.h"
19#include "mlir/IR/SymbolTable.h"
20
21namespace fir {
22
26class LazySymbolTable {
27public:
28 explicit LazySymbolTable(mlir::Operation *op)
29 : module(mlir::isa<mlir::ModuleOp>(op)
30 ? mlir::cast<mlir::ModuleOp>(op)
31 : op->getParentOfType<mlir::ModuleOp>()) {}
32
33 void build() {
34 if (table)
35 return;
36 table = std::make_unique<mlir::SymbolTable>(module);
37 }
38
40 template <typename T>
41 T lookup(llvm::StringRef name) {
42 build();
43 return table->lookup<T>(name);
44 }
45
49 mlir::Operation *lookupSymbol(mlir::SymbolRefAttr attr) {
50 if (!attr)
51 return nullptr;
52 build();
53 return table ? table->lookup(attr.getRootReference().getValue()) : nullptr;
54 }
55
56private:
57 std::unique_ptr<mlir::SymbolTable> table;
58 mlir::ModuleOp module;
59};
60
61} // namespace fir
62
63#endif // FORTRAN_OPTIMIZER_SUPPORT_LAZYSYMBOLTABLE_H
mlir::Operation * lookupSymbol(mlir::SymbolRefAttr attr)
Definition LazySymbolTable.h:49
T lookup(llvm::StringRef name)
Look up a symbol by name. Builds the table on first use.
Definition LazySymbolTable.h:41
Definition AbstractConverter.h:37