diff --git a/flang/include/flang/Optimizer/Analysis/TBAAForest.h b/flang/include/flang/Optimizer/Analysis/TBAAForest.h index 86030f0be26ae..b69e50bbe05c7 100644 --- a/flang/include/flang/Optimizer/Analysis/TBAAForest.h +++ b/flang/include/flang/Optimizer/Analysis/TBAAForest.h @@ -9,9 +9,11 @@ #ifndef FORTRAN_OPTIMIZER_ANALYSIS_TBAA_FOREST_H #define FORTRAN_OPTIMIZER_ANALYSIS_TBAA_FOREST_H +#include "flang/Optimizer/Dialect/FIROpsSupport.h" #include "mlir/Dialect/Func/IR/FuncOps.h" #include "mlir/Dialect/LLVMIR/LLVMAttrs.h" #include "mlir/Dialect/LLVMIR/LLVMDialect.h" +#include "mlir/IR/Attributes.h" #include "mlir/IR/MLIRContext.h" #include "llvm/ADT/DenseMap.h" #include @@ -82,6 +84,12 @@ class TBAAForrest { return getFuncTree(func.getSymNameAttr()); } inline const TBAATree &operator[](mlir::LLVM::LLVMFuncOp func) { + // the external name conversion pass may rename some functions. Their old + // name must be used so that we add to the tbaa tree added in the FIR pass + mlir::Attribute attr = func->getAttr(getInternalFuncNameAttrName()); + if (attr) { + return getFuncTree(attr.cast()); + } return getFuncTree(func.getSymNameAttr()); } diff --git a/flang/include/flang/Optimizer/Dialect/FIROpsSupport.h b/flang/include/flang/Optimizer/Dialect/FIROpsSupport.h index 00c06d0cbe619..977949e399a53 100644 --- a/flang/include/flang/Optimizer/Dialect/FIROpsSupport.h +++ b/flang/include/flang/Optimizer/Dialect/FIROpsSupport.h @@ -93,6 +93,12 @@ static constexpr llvm::StringRef getInternalProcedureAttrName() { return "fir.internal_proc"; } +/// Attribute containing the original name of a function from before the +/// ExternalNameConverision pass runs +static constexpr llvm::StringRef getInternalFuncNameAttrName() { + return "fir.internal_name"; +} + /// Does the function, \p func, have a host-associations tuple argument? /// Some internal procedures may have access to host procedure variables. bool hasHostAssociationArgument(mlir::func::FuncOp func); diff --git a/flang/lib/Optimizer/Transforms/ExternalNameConversion.cpp b/flang/lib/Optimizer/Transforms/ExternalNameConversion.cpp index e967a8f19d53a..221e93ff85e18 100644 --- a/flang/lib/Optimizer/Transforms/ExternalNameConversion.cpp +++ b/flang/lib/Optimizer/Transforms/ExternalNameConversion.cpp @@ -9,11 +9,13 @@ #include "flang/Common/Fortran.h" #include "flang/Optimizer/Dialect/FIRDialect.h" #include "flang/Optimizer/Dialect/FIROps.h" +#include "flang/Optimizer/Dialect/FIROpsSupport.h" #include "flang/Optimizer/Support/InternalNames.h" #include "flang/Optimizer/Transforms/Passes.h" #include "mlir/Dialect/LLVMIR/LLVMDialect.h" #include "mlir/Dialect/OpenACC/OpenACC.h" #include "mlir/Dialect/OpenMP/OpenMPDialect.h" +#include "mlir/IR/Attributes.h" #include "mlir/IR/SymbolTable.h" #include "mlir/Pass/Pass.h" #include "mlir/Transforms/DialectConversion.h" @@ -75,7 +77,8 @@ struct MangleNameOnFuncOp : public mlir::OpRewritePattern { mlir::PatternRewriter &rewriter) const override { mlir::LogicalResult ret = success(); rewriter.startRootUpdate(op); - auto result = fir::NameUniquer::deconstruct(op.getSymName()); + llvm::StringRef oldName = op.getSymName(); + auto result = fir::NameUniquer::deconstruct(oldName); if (fir::NameUniquer::isExternalFacingUniquedName(result)) { auto newSymbol = rewriter.getStringAttr(mangleExternalName(result, appendUnderscore)); @@ -86,6 +89,9 @@ struct MangleNameOnFuncOp : public mlir::OpRewritePattern { op.setSymNameAttr(newSymbol); mlir::SymbolTable::setSymbolName(op, newSymbol); + + op->setAttr(fir::getInternalFuncNameAttrName(), + mlir::StringAttr::get(op->getContext(), oldName)); } updateEarlyOutliningParentName(op, appendUnderscore); diff --git a/flang/test/Fir/external-mangling.fir b/flang/test/Fir/external-mangling.fir index 5e7cb978838e4..06e1e2e412295 100644 --- a/flang/test/Fir/external-mangling.fir +++ b/flang/test/Fir/external-mangling.fir @@ -49,8 +49,8 @@ func.func private @_QPbar2(!fir.ref) // LLVMIR-UNDER: llvm.mlir.global common @a_(dense<0> : vector<4xi8>) {{.*}} : !llvm.array<4 x i8> // LLVMIR-UNDER: llvm.mlir.global common @__BLNK__(dense<0> : vector<4xi8>) {{.*}} : !llvm.array<4 x i8> -// LLVMIR-UNDER: llvm.func @bar_(!llvm.ptr) attributes {sym_visibility = "private"} -// LLVMIR-UNDER: llvm.func @bar2_(!llvm.ptr) attributes {sym_visibility = "private"} +// LLVMIR-UNDER: llvm.func @bar_(!llvm.ptr) attributes {fir.internal_name = "_QPbar", sym_visibility = "private"} +// LLVMIR-UNDER: llvm.func @bar2_(!llvm.ptr) attributes {fir.internal_name = "_QPbar2", sym_visibility = "private"} // LLVMIR-NOUNDER: %{{.*}} = llvm.mlir.addressof @a : !llvm.ptr // LLVMIR-NOUNDER: %{{.*}} = llvm.mlir.addressof @__BLNK__ : !llvm.ptr @@ -59,8 +59,8 @@ func.func private @_QPbar2(!fir.ref) // LLVMIR-NOUNDER: llvm.mlir.global common @a(dense<0> : vector<4xi8>) {{.*}} : !llvm.array<4 x i8> // LLVMIR-NOUNDER: llvm.mlir.global common @__BLNK__(dense<0> : vector<4xi8>) {{.*}} : !llvm.array<4 x i8> -// LLVMIR-NOUNDER: llvm.func @bar(!llvm.ptr) attributes {sym_visibility = "private"} -// LLVMIR-NOUNDER: llvm.func @bar2(!llvm.ptr) attributes {sym_visibility = "private"} +// LLVMIR-NOUNDER: llvm.func @bar(!llvm.ptr) attributes {fir.internal_name = "_QPbar", sym_visibility = "private"} +// LLVMIR-NOUNDER: llvm.func @bar2(!llvm.ptr) attributes {fir.internal_name = "_QPbar2", sym_visibility = "private"} // ----- @@ -96,5 +96,5 @@ func.func @_QPwriteindex_omp_outline_0() attributes {omp.outline_parent_name = " return } -// CHECK-UNDER: attributes {omp.outline_parent_name = "writeindex_"} -// CHECK-NOUNDER: attributes {omp.outline_parent_name = "writeindex"} +// CHECK-UNDER: attributes {fir.internal_name = "_QPwriteindex_omp_outline_0", omp.outline_parent_name = "writeindex_"} +// CHECK-NOUNDER: attributes {fir.internal_name = "_QPwriteindex_omp_outline_0", omp.outline_parent_name = "writeindex"}