Skip to content

Commit

Permalink
[mlir][analysis] Add an analysis for preserving symbol tables
Browse files Browse the repository at this point in the history
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
  • Loading branch information
Mogball committed Jan 8, 2023
1 parent a22f145 commit 496f9a7
Showing 1 changed file with 54 additions and 0 deletions.
54 changes: 54 additions & 0 deletions mlir/include/mlir/Analysis/SymbolTableAnalysis.h
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 496f9a7

Please sign in to comment.