[mlir][analysis] Add an analysis for preserving symbol tables
authorJeff Niu <jeff@modular.com>
Thu, 8 Dec 2022 20:22:40 +0000 (12:22 -0800)
committerJeff Niu <jeff@modular.com>
Sun, 8 Jan 2023 21:16:16 +0000 (13:16 -0800)
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

mlir/include/mlir/Analysis/SymbolTableAnalysis.h [new file with mode: 0644]

diff --git a/mlir/include/mlir/Analysis/SymbolTableAnalysis.h b/mlir/include/mlir/Analysis/SymbolTableAnalysis.h
new file mode 100644 (file)
index 0000000..145627e
--- /dev/null
@@ -0,0 +1,54 @@
+//===- SymbolTableAnalysis.h - Analysis for cached symbol tables --*- C++ -*-=//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef MLIR_ANALYSIS_SYMBOLTABLEANALYSIS_H
+#define MLIR_ANALYSIS_SYMBOLTABLEANALYSIS_H
+
+#include "mlir/IR/SymbolTable.h"
+#include "mlir/Pass/AnalysisManager.h"
+
+namespace mlir {
+/// This is a simple analysis that contains a symbol table collection and, for
+/// simplicity, a reference to the top-level symbol table. This allows symbol
+/// tables to be preserved across passes. Most often, symbol tables are
+/// automatically kept up-to-date via the `insert` and `erase` functions.
+class SymbolTableAnalysis {
+public:
+  /// Create the symbol table analysis at the provided top-level operation and
+  /// instantiate the symbol table of the top-level operation.
+  SymbolTableAnalysis(Operation *op)
+      : topLevelSymbolTable(symbolTables.getSymbolTable(op)) {}
+
+  /// Get the symbol table collection.
+  SymbolTableCollection &getSymbolTables() { return symbolTables; }
+
+  /// Get the top-level symbol table.
+  SymbolTable &getTopLevelSymbolTable() { return topLevelSymbolTable; }
+
+  /// Get the top-level operation.
+  template <typename OpT>
+  OpT getTopLevelOp() {
+    return cast<OpT>(topLevelSymbolTable.getOp());
+  }
+
+  /// Symbol tables are kept up-to-date by passes. Assume that the analysis
+  /// remains valid.
+  bool isInvalidated(const AnalysisManager::PreservedAnalyses &pa) {
+    return false;
+  }
+
+private:
+  /// The symbol table collection containing cached symbol tables for all nested
+  /// symbol table operations.
+  SymbolTableCollection symbolTables;
+  /// The symbol table of the top-level operation.
+  SymbolTable &topLevelSymbolTable;
+};
+} // namespace mlir
+
+#endif // MLIR_ANALYSIS_SYMBOLTABLEANALYSIS_H