|
| 1 | +//=== CompilerGeneratedNames.cpp - convert special symbols in global names ===// |
| 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 | +#include "flang/Optimizer/Dialect/FIRDialect.h" |
| 10 | +#include "flang/Optimizer/Dialect/FIROps.h" |
| 11 | +#include "flang/Optimizer/Dialect/FIROpsSupport.h" |
| 12 | +#include "flang/Optimizer/Support/InternalNames.h" |
| 13 | +#include "flang/Optimizer/Transforms/Passes.h" |
| 14 | +#include "mlir/IR/Attributes.h" |
| 15 | +#include "mlir/IR/SymbolTable.h" |
| 16 | +#include "mlir/Pass/Pass.h" |
| 17 | + |
| 18 | +namespace fir { |
| 19 | +#define GEN_PASS_DEF_COMPILERGENERATEDNAMESCONVERSION |
| 20 | +#include "flang/Optimizer/Transforms/Passes.h.inc" |
| 21 | +} // namespace fir |
| 22 | + |
| 23 | +using namespace mlir; |
| 24 | + |
| 25 | +namespace { |
| 26 | + |
| 27 | +class CompilerGeneratedNamesConversionPass |
| 28 | + : public fir::impl::CompilerGeneratedNamesConversionBase< |
| 29 | + CompilerGeneratedNamesConversionPass> { |
| 30 | +public: |
| 31 | + using CompilerGeneratedNamesConversionBase< |
| 32 | + CompilerGeneratedNamesConversionPass>:: |
| 33 | + CompilerGeneratedNamesConversionBase; |
| 34 | + |
| 35 | + mlir::ModuleOp getModule() { return getOperation(); } |
| 36 | + void runOnOperation() override; |
| 37 | +}; |
| 38 | +} // namespace |
| 39 | + |
| 40 | +void CompilerGeneratedNamesConversionPass::runOnOperation() { |
| 41 | + auto op = getOperation(); |
| 42 | + auto *context = &getContext(); |
| 43 | + |
| 44 | + llvm::DenseMap<mlir::StringAttr, mlir::FlatSymbolRefAttr> remappings; |
| 45 | + for (auto &funcOrGlobal : op->getRegion(0).front()) { |
| 46 | + if (llvm::isa<mlir::func::FuncOp>(funcOrGlobal) || |
| 47 | + llvm::isa<fir::GlobalOp>(funcOrGlobal)) { |
| 48 | + auto symName = funcOrGlobal.getAttrOfType<mlir::StringAttr>( |
| 49 | + mlir::SymbolTable::getSymbolAttrName()); |
| 50 | + auto deconstructedName = fir::NameUniquer::deconstruct(symName); |
| 51 | + if (deconstructedName.first != fir::NameUniquer::NameKind::NOT_UNIQUED && |
| 52 | + !fir::NameUniquer::isExternalFacingUniquedName(deconstructedName)) { |
| 53 | + std::string newName = |
| 54 | + fir::NameUniquer::replaceSpecialSymbols(symName.getValue().str()); |
| 55 | + if (newName != symName) { |
| 56 | + auto newAttr = mlir::StringAttr::get(context, newName); |
| 57 | + mlir::SymbolTable::setSymbolName(&funcOrGlobal, newAttr); |
| 58 | + auto newSymRef = mlir::FlatSymbolRefAttr::get(newAttr); |
| 59 | + remappings.try_emplace(symName, newSymRef); |
| 60 | + } |
| 61 | + } |
| 62 | + } |
| 63 | + } |
| 64 | + |
| 65 | + if (remappings.empty()) |
| 66 | + return; |
| 67 | + |
| 68 | + // Update all uses of the functions and globals that have been renamed. |
| 69 | + op.walk([&remappings](mlir::Operation *nestedOp) { |
| 70 | + llvm::SmallVector<std::pair<mlir::StringAttr, mlir::SymbolRefAttr>> updates; |
| 71 | + for (const mlir::NamedAttribute &attr : nestedOp->getAttrDictionary()) |
| 72 | + if (auto symRef = llvm::dyn_cast<mlir::SymbolRefAttr>(attr.getValue())) |
| 73 | + if (auto remap = remappings.find(symRef.getRootReference()); |
| 74 | + remap != remappings.end()) |
| 75 | + updates.emplace_back(std::pair<mlir::StringAttr, mlir::SymbolRefAttr>{ |
| 76 | + attr.getName(), mlir::SymbolRefAttr(remap->second)}); |
| 77 | + for (auto update : updates) |
| 78 | + nestedOp->setAttr(update.first, update.second); |
| 79 | + }); |
| 80 | +} |
0 commit comments