Skip to content

Commit ba3d024

Browse files
authored
[flang] Record the original name of a function during ExternalNameCoversion (#74065)
We pass TBAA alias information with separate TBAA trees per function (to prevent incorrect alias information after inlining). These TBAA trees are identified by a unique string per function. Naturally, we use the mangled name of the function. TBAA tags are added in two places: during a dedicated pass relatively early (structured control flow makes fir::AliasAnalysis more accurate), then again during CodeGen (when implied box loads and stores become visible). In between these two passes, the ExternalNameConversion pass changes the name of some functions. These functions with changed names previously ended up with separate TBAA trees from the TBAA tags pass and from CodeGen - leading LLVM to think that all data accesses alias with all descriptor accesses. This patch solves this by storing the original name of a function in an attribute during the ExternalNameConversion pass, and using the name from that attribute when creating TBAA trees during CodeGen.
1 parent 3e7ca05 commit ba3d024

File tree

4 files changed

+27
-7
lines changed

4 files changed

+27
-7
lines changed

flang/include/flang/Optimizer/Analysis/TBAAForest.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,11 @@
99
#ifndef FORTRAN_OPTIMIZER_ANALYSIS_TBAA_FOREST_H
1010
#define FORTRAN_OPTIMIZER_ANALYSIS_TBAA_FOREST_H
1111

12+
#include "flang/Optimizer/Dialect/FIROpsSupport.h"
1213
#include "mlir/Dialect/Func/IR/FuncOps.h"
1314
#include "mlir/Dialect/LLVMIR/LLVMAttrs.h"
1415
#include "mlir/Dialect/LLVMIR/LLVMDialect.h"
16+
#include "mlir/IR/Attributes.h"
1517
#include "mlir/IR/MLIRContext.h"
1618
#include "llvm/ADT/DenseMap.h"
1719
#include <string>
@@ -82,6 +84,12 @@ class TBAAForrest {
8284
return getFuncTree(func.getSymNameAttr());
8385
}
8486
inline const TBAATree &operator[](mlir::LLVM::LLVMFuncOp func) {
87+
// the external name conversion pass may rename some functions. Their old
88+
// name must be used so that we add to the tbaa tree added in the FIR pass
89+
mlir::Attribute attr = func->getAttr(getInternalFuncNameAttrName());
90+
if (attr) {
91+
return getFuncTree(attr.cast<mlir::StringAttr>());
92+
}
8593
return getFuncTree(func.getSymNameAttr());
8694
}
8795

flang/include/flang/Optimizer/Dialect/FIROpsSupport.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,12 @@ static constexpr llvm::StringRef getInternalProcedureAttrName() {
9393
return "fir.internal_proc";
9494
}
9595

96+
/// Attribute containing the original name of a function from before the
97+
/// ExternalNameConverision pass runs
98+
static constexpr llvm::StringRef getInternalFuncNameAttrName() {
99+
return "fir.internal_name";
100+
}
101+
96102
/// Does the function, \p func, have a host-associations tuple argument?
97103
/// Some internal procedures may have access to host procedure variables.
98104
bool hasHostAssociationArgument(mlir::func::FuncOp func);

flang/lib/Optimizer/Transforms/ExternalNameConversion.cpp

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,13 @@
99
#include "flang/Common/Fortran.h"
1010
#include "flang/Optimizer/Dialect/FIRDialect.h"
1111
#include "flang/Optimizer/Dialect/FIROps.h"
12+
#include "flang/Optimizer/Dialect/FIROpsSupport.h"
1213
#include "flang/Optimizer/Support/InternalNames.h"
1314
#include "flang/Optimizer/Transforms/Passes.h"
1415
#include "mlir/Dialect/LLVMIR/LLVMDialect.h"
1516
#include "mlir/Dialect/OpenACC/OpenACC.h"
1617
#include "mlir/Dialect/OpenMP/OpenMPDialect.h"
18+
#include "mlir/IR/Attributes.h"
1719
#include "mlir/IR/SymbolTable.h"
1820
#include "mlir/Pass/Pass.h"
1921
#include "mlir/Transforms/DialectConversion.h"
@@ -75,7 +77,8 @@ struct MangleNameOnFuncOp : public mlir::OpRewritePattern<mlir::func::FuncOp> {
7577
mlir::PatternRewriter &rewriter) const override {
7678
mlir::LogicalResult ret = success();
7779
rewriter.startRootUpdate(op);
78-
auto result = fir::NameUniquer::deconstruct(op.getSymName());
80+
llvm::StringRef oldName = op.getSymName();
81+
auto result = fir::NameUniquer::deconstruct(oldName);
7982
if (fir::NameUniquer::isExternalFacingUniquedName(result)) {
8083
auto newSymbol =
8184
rewriter.getStringAttr(mangleExternalName(result, appendUnderscore));
@@ -86,6 +89,9 @@ struct MangleNameOnFuncOp : public mlir::OpRewritePattern<mlir::func::FuncOp> {
8689

8790
op.setSymNameAttr(newSymbol);
8891
mlir::SymbolTable::setSymbolName(op, newSymbol);
92+
93+
op->setAttr(fir::getInternalFuncNameAttrName(),
94+
mlir::StringAttr::get(op->getContext(), oldName));
8995
}
9096

9197
updateEarlyOutliningParentName(op, appendUnderscore);

flang/test/Fir/external-mangling.fir

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,8 @@ func.func private @_QPbar2(!fir.ref<f32>)
4949

5050
// LLVMIR-UNDER: llvm.mlir.global common @a_(dense<0> : vector<4xi8>) {{.*}} : !llvm.array<4 x i8>
5151
// LLVMIR-UNDER: llvm.mlir.global common @__BLNK__(dense<0> : vector<4xi8>) {{.*}} : !llvm.array<4 x i8>
52-
// LLVMIR-UNDER: llvm.func @bar_(!llvm.ptr) attributes {sym_visibility = "private"}
53-
// LLVMIR-UNDER: llvm.func @bar2_(!llvm.ptr) attributes {sym_visibility = "private"}
52+
// LLVMIR-UNDER: llvm.func @bar_(!llvm.ptr) attributes {fir.internal_name = "_QPbar", sym_visibility = "private"}
53+
// LLVMIR-UNDER: llvm.func @bar2_(!llvm.ptr) attributes {fir.internal_name = "_QPbar2", sym_visibility = "private"}
5454

5555
// LLVMIR-NOUNDER: %{{.*}} = llvm.mlir.addressof @a : !llvm.ptr
5656
// LLVMIR-NOUNDER: %{{.*}} = llvm.mlir.addressof @__BLNK__ : !llvm.ptr
@@ -59,8 +59,8 @@ func.func private @_QPbar2(!fir.ref<f32>)
5959

6060
// LLVMIR-NOUNDER: llvm.mlir.global common @a(dense<0> : vector<4xi8>) {{.*}} : !llvm.array<4 x i8>
6161
// LLVMIR-NOUNDER: llvm.mlir.global common @__BLNK__(dense<0> : vector<4xi8>) {{.*}} : !llvm.array<4 x i8>
62-
// LLVMIR-NOUNDER: llvm.func @bar(!llvm.ptr) attributes {sym_visibility = "private"}
63-
// LLVMIR-NOUNDER: llvm.func @bar2(!llvm.ptr) attributes {sym_visibility = "private"}
62+
// LLVMIR-NOUNDER: llvm.func @bar(!llvm.ptr) attributes {fir.internal_name = "_QPbar", sym_visibility = "private"}
63+
// LLVMIR-NOUNDER: llvm.func @bar2(!llvm.ptr) attributes {fir.internal_name = "_QPbar2", sym_visibility = "private"}
6464

6565
// -----
6666

@@ -96,5 +96,5 @@ func.func @_QPwriteindex_omp_outline_0() attributes {omp.outline_parent_name = "
9696
return
9797
}
9898

99-
// CHECK-UNDER: attributes {omp.outline_parent_name = "writeindex_"}
100-
// CHECK-NOUNDER: attributes {omp.outline_parent_name = "writeindex"}
99+
// CHECK-UNDER: attributes {fir.internal_name = "_QPwriteindex_omp_outline_0", omp.outline_parent_name = "writeindex_"}
100+
// CHECK-NOUNDER: attributes {fir.internal_name = "_QPwriteindex_omp_outline_0", omp.outline_parent_name = "writeindex"}

0 commit comments

Comments
 (0)