[flang-commits] [flang] [Flang] Do not attach tbaa tags for local pointers (PR #143376)

Dominik Adamski via flang-commits flang-commits at lists.llvm.org
Mon Jun 9 05:44:30 PDT 2025


https://github.com/DominikAdamski created https://github.com/llvm/llvm-project/pull/143376

Tbaa tags for local pointers can lead to incorrect results of alias analysis for local objects:
https://github.com/llvm/llvm-project/issues/141928 The new flag has been added for future testing
to determine if the more advanced Fortran alias analysis can safely identify whether two given items are aliased.

>From 6382b652294b2219e88a630b60fcf4ce404d57b3 Mon Sep 17 00:00:00 2001
From: Dominik Adamski <dominik.adamski at amd.com>
Date: Fri, 6 Jun 2025 07:30:04 -0500
Subject: [PATCH] [Flang] Do not attach tbaa tags for local pointers

Tbaa tags for local pointers can lead to incorrect results
of alias analysis for local objects:
https://github.com/llvm/llvm-project/issues/141928
The new flag has been added for future testing
to determine if the more advanced Fortran alias
analysis can safely identify whether two given
items are aliased.
---
 flang/lib/Optimizer/Transforms/AddAliasTags.cpp | 12 ++++++++++++
 flang/test/Transforms/tbaa3.fir                 |  4 +++-
 2 files changed, 15 insertions(+), 1 deletion(-)

diff --git a/flang/lib/Optimizer/Transforms/AddAliasTags.cpp b/flang/lib/Optimizer/Transforms/AddAliasTags.cpp
index 66b4b84998801..26bab9e2974cb 100644
--- a/flang/lib/Optimizer/Transforms/AddAliasTags.cpp
+++ b/flang/lib/Optimizer/Transforms/AddAliasTags.cpp
@@ -50,6 +50,15 @@ static llvm::cl::opt<bool>
 static llvm::cl::opt<bool> enableLocalAllocs(
     "local-alloc-tbaa", llvm::cl::init(false), llvm::cl::Hidden,
     llvm::cl::desc("Add TBAA tags to local allocations. UNSAFE."));
+// This is **known unsafe** (miscompare in Fujitsu: Fortran/0614/0614_0005.f
+// for ARM). Detailed analysis of the root cause:
+// https://github.com/llvm/llvm-project/issues/141928
+// The code is kept so that these may be tried with new benchmarks to see if
+// this is worth fixing in the future. This flag has no effect unless
+// enableLocalAllocs is set
+static llvm::cl::opt<bool> enablePtrLocalAllocs(
+    "ptr-local-alloc-tbaa", llvm::cl::init(false), llvm::cl::Hidden,
+    llvm::cl::desc("Add TBAA tags to local pointer allocations. UNSAFE."));
 
 namespace {
 
@@ -287,6 +296,9 @@ void AddAliasTagsPass::runOnAliasInterface(fir::FirAliasTagOpInterface op,
                  << "WARN: unknown defining op for SourceKind::Allocate " << *op
                  << "\n");
     } else if (source.isPointer()) {
+      // Do not add tbaa tags for local pointers unless flag is set
+      if (!enablePtrLocalAllocs)
+        return;
       LLVM_DEBUG(llvm::dbgs().indent(2)
                  << "Found reference to allocation at " << *op << "\n");
       tag = state.getFuncTreeWithScope(func, scopeOp).targetDataTree.getTag();
diff --git a/flang/test/Transforms/tbaa3.fir b/flang/test/Transforms/tbaa3.fir
index 28ff8f7c5fa83..5f446cd3fa20e 100644
--- a/flang/test/Transforms/tbaa3.fir
+++ b/flang/test/Transforms/tbaa3.fir
@@ -1,5 +1,6 @@
 // RUN: fir-opt --fir-add-alias-tags %s | FileCheck --check-prefixes=ALL,DEFAULT %s
 // RUN: fir-opt --fir-add-alias-tags --local-alloc-tbaa %s | FileCheck --check-prefixes=ALL,LOCAL %s
+// RUN: fir-opt --fir-add-alias-tags --local-alloc-tbaa  --ptr-local-alloc-tbaa %s | FileCheck --check-prefixes=ALL,PTR-LOCAL %s
 
 // Test AddAliasTagsPass creating sub-tree for TARGET/POINTER variables.
 
@@ -298,7 +299,8 @@ module {
     %83 = fir.array_coor %80(%82) %c1 : (!fir.box<!fir.ptr<!fir.array<?xf32>>>, !fir.shift<1>, index) -> !fir.ref<f32>
 // real, pointer :: localp(:)
 // DEFAULT-NOT: fir.store{{.*}}tbaa
-// LOCAL: fir.store{{.*}}{tbaa = [#[[TARGETTAG]]]} : !fir.ref<f32>
+// LOCAL-NOT: fir.store{{.*}}{tbaa = [#[[TARGETTAG]]]} : !fir.ref<f32>
+// PTR-LOCAL: fir.store{{.*}}{tbaa = [#[[TARGETTAG]]]} : !fir.ref<f32>
     fir.store %cst to %83 : !fir.ref<f32>
 // ALL-NOT: fir.load{{.*}}tbaa
     %84 = fir.load %27 : !fir.ref<!fir.box<!fir.heap<!fir.array<?xf32>>>>



More information about the flang-commits mailing list