llvm-project/mlir/lib/Transforms/SymbolPrivatize.cpp
Michele Scuttari 2be8af8f0e
[MLIR] Update pass declarations to new autogenerated files
The patch introduces the required changes to update the pass declarations and definitions to use the new autogenerated files and allow dropping the old infrastructure.

Reviewed By: mehdi_amini, rriddle

Differential Review: https://reviews.llvm.org/D132838
2022-08-30 21:56:31 +02:00

66 lines
2.0 KiB
C++

//===- SymbolPrivatize.cpp - Pass to mark symbols private -----------------===//
//
// 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
//
//===----------------------------------------------------------------------===//
//
// This file implements an pass that marks all symbols as private unless
// excluded.
//
//===----------------------------------------------------------------------===//
#include "mlir/Transforms/Passes.h"
#include "mlir/IR/SymbolTable.h"
namespace mlir {
#define GEN_PASS_DEF_SYMBOLPRIVATIZEPASS
#include "mlir/Transforms/Passes.h.inc"
} // namespace mlir
using namespace mlir;
namespace {
struct SymbolPrivatizePass
: public impl::SymbolPrivatizePassBase<SymbolPrivatizePass> {
explicit SymbolPrivatizePass(ArrayRef<std::string> excludeSymbols);
LogicalResult initialize(MLIRContext *context) override;
void runOnOperation() override;
/// Symbols whose visibility won't be changed.
DenseSet<StringAttr> excludedSymbols;
};
} // namespace
SymbolPrivatizePass::SymbolPrivatizePass(
llvm::ArrayRef<std::string> excludeSymbols) {
exclude = excludeSymbols;
}
LogicalResult SymbolPrivatizePass::initialize(MLIRContext *context) {
for (const std::string &symbol : exclude)
excludedSymbols.insert(StringAttr::get(context, symbol));
return success();
}
void SymbolPrivatizePass::runOnOperation() {
for (Region &region : getOperation()->getRegions()) {
for (Block &block : region) {
for (Operation &op : block) {
auto symbol = dyn_cast<SymbolOpInterface>(op);
if (!symbol)
continue;
if (!excludedSymbols.contains(symbol.getNameAttr()))
symbol.setVisibility(SymbolTable::Visibility::Private);
}
}
}
}
std::unique_ptr<Pass>
mlir::createSymbolPrivatizePass(ArrayRef<std::string> exclude) {
return std::make_unique<SymbolPrivatizePass>(exclude);
}