MLIR  16.0.0git
SymbolPrivatize.cpp
Go to the documentation of this file.
1 //===- SymbolPrivatize.cpp - Pass to mark symbols private -----------------===//
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 // This file implements an pass that marks all symbols as private unless
10 // excluded.
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #include "PassDetail.h"
15 #include "mlir/IR/SymbolTable.h"
16 #include "mlir/Transforms/Passes.h"
17 
18 using namespace mlir;
19 
20 namespace {
21 struct SymbolPrivatize : public SymbolPrivatizeBase<SymbolPrivatize> {
22  explicit SymbolPrivatize(ArrayRef<std::string> excludeSymbols);
23  LogicalResult initialize(MLIRContext *context) override;
24  void runOnOperation() override;
25 
26  /// Symbols whose visibility won't be changed.
27  DenseSet<StringAttr> excludedSymbols;
28 };
29 } // namespace
30 
31 SymbolPrivatize::SymbolPrivatize(llvm::ArrayRef<std::string> excludeSymbols) {
32  exclude = excludeSymbols;
33 }
34 
35 LogicalResult SymbolPrivatize::initialize(MLIRContext *context) {
36  for (const std::string &symbol : exclude)
37  excludedSymbols.insert(StringAttr::get(context, symbol));
38  return success();
39 }
40 
41 void SymbolPrivatize::runOnOperation() {
42  for (Region &region : getOperation()->getRegions()) {
43  for (Block &block : region) {
44  for (Operation &op : block) {
45  auto symbol = dyn_cast<SymbolOpInterface>(op);
46  if (!symbol)
47  continue;
48  if (!excludedSymbols.contains(symbol.getNameAttr()))
49  symbol.setVisibility(SymbolTable::Visibility::Private);
50  }
51  }
52  }
53 }
54 
55 std::unique_ptr<Pass>
57  return std::make_unique<SymbolPrivatize>(exclude);
58 }
Include the generated interface declarations.
This class contains a list of basic blocks and a link to the parent operation it is attached to...
Definition: Region.h:26
Operation is a basic unit of execution within MLIR.
Definition: Operation.h:28
Block represents an ordered list of Operations.
Definition: Block.h:29
LogicalResult success(bool isSuccess=true)
Utility function to generate a LogicalResult.
Definition: LogicalResult.h:56
This class represents an efficient way to signal success or failure.
Definition: LogicalResult.h:26
The symbol is private and may only be referenced by SymbolRefAttrs local to the operations within the...
std::unique_ptr< Pass > createSymbolPrivatizePass(ArrayRef< std::string > excludeSymbols={})
Creates a pass which marks top-level symbol operations as private unless listed in excludeSymbols...
MLIRContext is the top-level object for a collection of MLIR operations.
Definition: MLIRContext.h:55