Skip to content

Commit 496f9a7

Browse files
author
Jeff Niu
committed
[mlir][analysis] Add an analysis for preserving symbol tables
This patch adds a `SymbolTableAnalysis` that can be used with the analysis manager. It contains a symbol table collection. This analysis allows symbol tables to be preserved across passes so that they do not need to be recomputed. The analysis assumes it remains valid because most transformations automatically keep symbol tables up-to-date using its `insert` and `erase` methods. Reviewed By: rriddle Differential Revision: https://reviews.llvm.org/D139666
1 parent a22f145 commit 496f9a7

File tree

1 file changed

+54
-0
lines changed

1 file changed

+54
-0
lines changed
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
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.
24+
SymbolTableAnalysis(Operation *op)
25+
: topLevelSymbolTable(symbolTables.getSymbolTable(op)) {}
26+
27+
/// Get the symbol table collection.
28+
SymbolTableCollection &getSymbolTables() { return symbolTables; }
29+
30+
/// Get the top-level symbol table.
31+
SymbolTable &getTopLevelSymbolTable() { return topLevelSymbolTable; }
32+
33+
/// Get the top-level operation.
34+
template <typename OpT>
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.
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

0 commit comments

Comments
 (0)