Skip to content

Conversation

@DominikAdamski
Copy link
Contributor

Tbaa tags for local pointers can lead to incorrect results of alias analysis for local objects:
#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.

Tbaa tags for local pointers can lead to incorrect results
of alias analysis for local objects:
llvm#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.
@DominikAdamski DominikAdamski requested review from tblah and vzakhari June 9, 2025 12:44
@llvmbot llvmbot added flang Flang issues not falling into any other category flang:fir-hlfir labels Jun 9, 2025
@llvmbot
Copy link
Member

llvmbot commented Jun 9, 2025

@llvm/pr-subscribers-flang-fir-hlfir

Author: Dominik Adamski (DominikAdamski)

Changes

Tbaa tags for local pointers can lead to incorrect results of alias analysis for local objects:
#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.


Full diff: https://github.com/llvm/llvm-project/pull/143376.diff

2 Files Affected:

  • (modified) flang/lib/Optimizer/Transforms/AddAliasTags.cpp (+12)
  • (modified) flang/test/Transforms/tbaa3.fir (+3-1)
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>>>>

@vzakhari
Copy link
Contributor

vzakhari commented Jun 9, 2025

Please see my comment in #141928.

@DominikAdamski
Copy link
Contributor Author

Closed after Flang Technical Call. See #143489 as a follow-up.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

flang:fir-hlfir flang Flang issues not falling into any other category

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants