1 //===- SymbolTableAnalysis.h - Analysis for cached symbol tables --*- 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 #ifndef MLIR_ANALYSIS_SYMBOLTABLEANALYSIS_H 10 #define MLIR_ANALYSIS_SYMBOLTABLEANALYSIS_H 11 12 #include "mlir/IR/SymbolTable.h" 13 #include "mlir/Pass/AnalysisManager.h" 14 15 namespace mlir { 16 /// This is a simple analysis that contains a symbol table collection and, for 17 /// simplicity, a reference to the top-level symbol table. This allows symbol 18 /// tables to be preserved across passes. Most often, symbol tables are 19 /// automatically kept up-to-date via the `insert` and `erase` functions. 20 class SymbolTableAnalysis { 21 public: 22 /// Create the symbol table analysis at the provided top-level operation and 23 /// instantiate the symbol table of the top-level operation. SymbolTableAnalysis(Operation * op)24 SymbolTableAnalysis(Operation *op) 25 : topLevelSymbolTable(symbolTables.getSymbolTable(op)) {} 26 27 /// Get the symbol table collection. getSymbolTables()28 SymbolTableCollection &getSymbolTables() { return symbolTables; } 29 30 /// Get the top-level symbol table. getTopLevelSymbolTable()31 SymbolTable &getTopLevelSymbolTable() { return topLevelSymbolTable; } 32 33 /// Get the top-level operation. 34 template <typename OpT> getTopLevelOp()35 OpT getTopLevelOp() { 36 return cast<OpT>(topLevelSymbolTable.getOp()); 37 } 38 39 /// Symbol tables are kept up-to-date by passes. Assume that the analysis 40 /// remains valid. isInvalidated(const AnalysisManager::PreservedAnalyses & pa)41 bool isInvalidated(const AnalysisManager::PreservedAnalyses &pa) { 42 return false; 43 } 44 45 private: 46 /// The symbol table collection containing cached symbol tables for all nested 47 /// symbol table operations. 48 SymbolTableCollection symbolTables; 49 /// The symbol table of the top-level operation. 50 SymbolTable &topLevelSymbolTable; 51 }; 52 } // namespace mlir 53 54 #endif // MLIR_ANALYSIS_SYMBOLTABLEANALYSIS_H 55