From 9a39c2ff75d0df9dbdf613e5e2ab638176555451 Mon Sep 17 00:00:00 2001 From: jeanPerier Date: Tue, 27 Jan 2026 15:25:21 +0100 Subject: [PATCH] Revert "[flang] Use outermost fir.dummy_scope for TBAA of local allocations. (#146006) (#177617) This reverts commit 90da61634a4accc9869b4e1cb1ac3736158c33e6. See https://github.com/llvm/llvm-project/pull/177615 for more context about why this patch is and can now be reverted. --- .../lib/Optimizer/Transforms/AddAliasTags.cpp | 28 +----- flang/test/Transforms/tbaa-for-local-vars.fir | 97 ------------------- .../tbaa-target-inlined-results.fir | 5 +- .../Transforms/tbaa-with-dummy-scope2.fir | 54 ++++++----- 4 files changed, 33 insertions(+), 151 deletions(-) delete mode 100644 flang/test/Transforms/tbaa-for-local-vars.fir diff --git a/flang/lib/Optimizer/Transforms/AddAliasTags.cpp b/flang/lib/Optimizer/Transforms/AddAliasTags.cpp index 3718848c0577..142e4c8d45c9 100644 --- a/flang/lib/Optimizer/Transforms/AddAliasTags.cpp +++ b/flang/lib/Optimizer/Transforms/AddAliasTags.cpp @@ -213,10 +213,7 @@ public: void processFunctionScopes(mlir::func::FuncOp func); // For the given fir.declare returns the dominating fir.dummy_scope // operation. - fir::DummyScopeOp getDeclarationScope(fir::DeclareOp declareOp) const; - // For the given fir.declare returns the outermost fir.dummy_scope - // in the current function. - fir::DummyScopeOp getOutermostScope(fir::DeclareOp declareOp) const; + fir::DummyScopeOp getDeclarationScope(fir::DeclareOp declareOp); // Returns true, if the given type of a memref of a FirAliasTagOpInterface // operation is a descriptor or contains a descriptor // (e.g. !fir.ref>}>>). @@ -356,8 +353,9 @@ void PassState::processFunctionScopes(mlir::func::FuncOp func) { } } -fir::DummyScopeOp -PassState::getDeclarationScope(fir::DeclareOp declareOp) const { +// For the given fir.declare returns the dominating fir.dummy_scope +// operation. +fir::DummyScopeOp PassState::getDeclarationScope(fir::DeclareOp declareOp) { auto func = declareOp->getParentOfType(); assert(func && "fir.declare does not have parent func.func"); auto &scopeOps = sortedScopeOperations.at(func); @@ -368,15 +366,6 @@ PassState::getDeclarationScope(fir::DeclareOp declareOp) const { return nullptr; } -fir::DummyScopeOp PassState::getOutermostScope(fir::DeclareOp declareOp) const { - auto func = declareOp->getParentOfType(); - assert(func && "fir.declare does not have parent func.func"); - auto &scopeOps = sortedScopeOperations.at(func); - if (!scopeOps.empty()) - return scopeOps[0]; - return nullptr; -} - bool PassState::typeReferencesDescriptor(mlir::Type type) { type = fir::unwrapAllRefAndSeqType(type); if (mlir::isa(type)) @@ -830,15 +819,6 @@ void AddAliasTagsPass::runOnAliasInterface(fir::FirAliasTagOpInterface op, else unknownAllocOp = true; - if (auto declOp = source.origin.instantiationPoint) { - // Use the outermost scope for local allocations, - // because using the innermost scope may result - // in incorrect TBAA, when calls are inlined in MLIR. - auto declareOp = mlir::dyn_cast(declOp); - assert(declareOp && "Instantiation point must be fir.declare"); - scopeOp = state.getOutermostScope(declareOp); - } - if (unknownAllocOp) { LLVM_DEBUG(llvm::dbgs().indent(2) << "WARN: unknown defining op for SourceKind::Allocate " << *op diff --git a/flang/test/Transforms/tbaa-for-local-vars.fir b/flang/test/Transforms/tbaa-for-local-vars.fir deleted file mode 100644 index fde5c400c75e..000000000000 --- a/flang/test/Transforms/tbaa-for-local-vars.fir +++ /dev/null @@ -1,97 +0,0 @@ -// Test TBAA tags assignment for access off the fir.declare -// placed in the middle of the routine (e.g. a temporary). -// Original example: -// module m -// type t -// real :: x -// end type t -// contains -// subroutine bar(this) -// class(t), intent(out) :: this -// this%x = 1.0 -// end subroutine bar -// function foo() result(res) -// type(t) :: res -// call bar(res) -// end function foo -// subroutine test(arg) -// type(t) :: var -// var = foo() -// arg = var%x -// end subroutine test -// end module m -// -// The calls were manually inlined in FIR with fir.save_result's -// destination operand being used instead of the temporary -// alloca of the result inside foo. Runtime calls were removed -// to reduce the test. -// The temporary function result fir.declare is then dominated -// by fir.dummy_scope of the foo function. If we use this scope -// to assign the TBAA local-alloc tag to the accesses of -// the temporary, then it won't conflict with the TBAA dummy tag -// assigned to the accesses of this argument of bar. -// That would be incorrect, because they access the same memory. -// Check that the local-alloc tag is placed into the outermost -// scope's TBAA tree. -// RUN: fir-opt --fir-add-alias-tags %s | FileCheck %s - -// CHECK: #[[$SCOPE_2:.+]] = #llvm.tbaa_root -// CHECK: #[[$SCOPE_1:.+]] = #llvm.tbaa_root -// CHECK: #[[$ANY_ACCESS2:.+]] = #llvm.tbaa_type_desc}> -// CHECK: #[[$ANY_ACCESS1:.+]] = #llvm.tbaa_type_desc}> -// CHECK: #[[$ANY_DATA2:.+]] = #llvm.tbaa_type_desc}> -// CHECK: #[[$ANY_DATA1:.+]] = #llvm.tbaa_type_desc}> -// CHECK: #[[$DUMMY_ARG2:.+]] = #llvm.tbaa_type_desc}> -// CHECK: #[[$ALLOCATED_DATA1:.+]] = #llvm.tbaa_type_desc}> -// CHECK: #[[$DUMMY_ARG1:.+]] = #llvm.tbaa_type_desc}> -// CHECK: #[[$ALLOCATED_DATA1_TAG:.+]] = #llvm.tbaa_tag -// CHECK: #[[$BAR_THIS2:.+]] = #llvm.tbaa_type_desc}> -// CHECK: #[[$TEST_VAR1:.+]] = #llvm.tbaa_type_desc}> -// CHECK: #[[$TEST_ARG1:.+]] = #llvm.tbaa_type_desc}> -// CHECK: #[[$BAR_THIS2_TAG:.+]] = #llvm.tbaa_tag -// CHECK: #[[$TEST_VAR1_TAG:.+]] = #llvm.tbaa_tag -// CHECK: #[[$TEST_ARG2_TAG:.+]] = #llvm.tbaa_tag - -// CHECK-LABEL: func.func @_QMmPtest( -// CHECK-SAME: %[[ARG0:.*]]: !fir.ref {fir.bindc_name = "arg"}) { -// CHECK: %[[VAL_0:.*]] = arith.constant 1.000000e+00 : f32 -// CHECK: %[[VAL_1:.*]] = fir.alloca !fir.type<_QMmTt{x:f32}> {bindc_name = ".result"} -// CHECK: %[[VAL_2:.*]] = fir.dummy_scope : !fir.dscope -// CHECK: %[[VAL_3:.*]] = fir.declare %[[ARG0]] dummy_scope %[[VAL_2]] {uniq_name = "_QMmFtestEarg"} : (!fir.ref, !fir.dscope) -> !fir.ref -// CHECK: %[[VAL_6:.*]] = fir.dummy_scope : !fir.dscope -// CHECK: %[[VAL_7:.*]] = fir.declare %[[VAL_1]] {uniq_name = "_QMmFfooEres"} : (!fir.ref>) -> !fir.ref> -// CHECK: %[[VAL_8:.*]] = fir.embox %[[VAL_7]] : (!fir.ref>) -> !fir.box> -// CHECK: %[[VAL_9:.*]] = fir.convert %[[VAL_8]] : (!fir.box>) -> !fir.class> -// CHECK: %[[VAL_10:.*]] = fir.dummy_scope : !fir.dscope -// CHECK: %[[VAL_11:.*]] = fir.declare %[[VAL_9]] dummy_scope %[[VAL_10]] {fortran_attrs = #fir.var_attrs, uniq_name = "_QMmFbarEthis"} : (!fir.class>, !fir.dscope) -> !fir.class> -// CHECK: %[[VAL_12:.*]] = fir.coordinate_of %[[VAL_11]], x : (!fir.class>) -> !fir.ref -// CHECK: fir.store %[[VAL_0]] to %[[VAL_12]] {tbaa = [#[[$BAR_THIS2_TAG]]]} : !fir.ref -// CHECK: %[[VAL_13:.*]] = fir.declare %[[VAL_1]] {uniq_name = ".tmp.func_result"} : (!fir.ref>) -> !fir.ref> -// CHECK: %[[VAL_14:.*]] = fir.coordinate_of %[[VAL_13]], x : (!fir.ref>) -> !fir.ref -// CHECK: %[[VAL_16:.*]] = fir.load %[[VAL_14]] {tbaa = [#[[$ALLOCATED_DATA1_TAG]]]} : !fir.ref -module attributes {dlti.dl_spec = #dlti.dl_spec : vector<4xi64>, i1 = dense<8> : vector<2xi64>, i8 = dense<8> : vector<2xi64>, i16 = dense<16> : vector<2xi64>, i32 = dense<32> : vector<2xi64>, i64 = dense<[32, 64]> : vector<2xi64>, f16 = dense<16> : vector<2xi64>, f64 = dense<64> : vector<2xi64>, f128 = dense<128> : vector<2xi64>, "dlti.endianness" = "little">, llvm.data_layout = ""} { -func.func @_QMmPtest(%arg0: !fir.ref {fir.bindc_name = "arg"}) { - %cst = arith.constant 1.000000e+00 : f32 - %0 = fir.alloca !fir.type<_QMmTt{x:f32}> {bindc_name = ".result"} - %1 = fir.dummy_scope : !fir.dscope - %2 = fir.declare %arg0 dummy_scope %1 {uniq_name = "_QMmFtestEarg"} : (!fir.ref, !fir.dscope) -> !fir.ref - %3 = fir.alloca !fir.type<_QMmTt{x:f32}> {bindc_name = "var", uniq_name = "_QMmFtestEvar"} - %4 = fir.declare %3 {uniq_name = "_QMmFtestEvar"} : (!fir.ref>) -> !fir.ref> - %5 = fir.dummy_scope : !fir.dscope - %6 = fir.declare %0 {uniq_name = "_QMmFfooEres"} : (!fir.ref>) -> !fir.ref> - %7 = fir.embox %6 : (!fir.ref>) -> !fir.box> - %8 = fir.convert %7 : (!fir.box>) -> !fir.class> - %9 = fir.dummy_scope : !fir.dscope - %10 = fir.declare %8 dummy_scope %9 {fortran_attrs = #fir.var_attrs, uniq_name = "_QMmFbarEthis"} : (!fir.class>, !fir.dscope) -> !fir.class> - %14 = fir.coordinate_of %10, x : (!fir.class>) -> !fir.ref - fir.store %cst to %14 : !fir.ref - %15 = fir.declare %0 {uniq_name = ".tmp.func_result"} : (!fir.ref>) -> !fir.ref> - %16 = fir.coordinate_of %15, x : (!fir.ref>) -> !fir.ref - %17 = fir.coordinate_of %4, x : (!fir.ref>) -> !fir.ref - %18 = fir.load %16 : !fir.ref - fir.store %18 to %17 : !fir.ref - %19 = fir.load %17 : !fir.ref - fir.store %19 to %2 : !fir.ref - return -} -} diff --git a/flang/test/Transforms/tbaa-target-inlined-results.fir b/flang/test/Transforms/tbaa-target-inlined-results.fir index b2919804abdd..8fc354f0c2c0 100644 --- a/flang/test/Transforms/tbaa-target-inlined-results.fir +++ b/flang/test/Transforms/tbaa-target-inlined-results.fir @@ -27,13 +27,10 @@ module attributes {dlti.dl_spec = #dlti.dl_spec = dense<32> : vec // CHECK: #[[TBAA_ROOT:.*]] = #llvm.tbaa_root -// CHECK: #[[TBAA_ROOT1:.*]] = #llvm.tbaa_root // CHECK: #[[TBAA_TYPE_DESC:.*]] = #llvm.tbaa_type_desc}> -// CHECK: #[[TBAA_TYPE_DESC1:.*]] = #llvm.tbaa_type_desc}> // CHECK: #[[TBAA_TYPE_DESC2:.*]] = #llvm.tbaa_type_desc}> -// CHECK: #[[TBAA_TYPE_DESC3:.*]] = #llvm.tbaa_type_desc}> // CHECK: #[[TBAA_TYPE_DESC4:.*]] = #llvm.tbaa_type_desc}> -// CHECK: #[[TBAA_TYPE_DESC5:.*]] = #llvm.tbaa_type_desc}> +// CHECK: #[[TBAA_TYPE_DESC5:.*]] = #llvm.tbaa_type_desc}> // CHECK: #[[TBAA_TAG:.*]] = #llvm.tbaa_tag // CHECK-LABEL: func.func @_QMmPfoo diff --git a/flang/test/Transforms/tbaa-with-dummy-scope2.fir b/flang/test/Transforms/tbaa-with-dummy-scope2.fir index a0718142a793..d5989c2cc4fd 100644 --- a/flang/test/Transforms/tbaa-with-dummy-scope2.fir +++ b/flang/test/Transforms/tbaa-with-dummy-scope2.fir @@ -89,30 +89,32 @@ func.func @_QPtest2() attributes {noinline} { } fir.global @_QMmEglob : i32 } -// CHECK: #[[$ATTR_0:.+]] = #llvm.tbaa_root -// CHECK: #[[$ATTR_1:.+]] = #llvm.tbaa_root -// CHECK: #[[$ANY_ACCESS_0:.+]] = #llvm.tbaa_type_desc}> -// CHECK: #[[$ANY_ACCESS_1:.+]] = #llvm.tbaa_type_desc}> -// CHECK: #[[$ANY_DATA_0:.+]] = #llvm.tbaa_type_desc}> -// CHECK: #[[$ANY_DATA_1:.+]] = #llvm.tbaa_type_desc}> -// CHECK: #[[$LOCAL_ATTR_0:.+]] = #llvm.tbaa_type_desc}> -// CHECK: #[[$ATTR_6:.+]] = #llvm.tbaa_type_desc}> -// CHECK: #[[$ATTR_7:.+]] = #llvm.tbaa_type_desc}> -// CHECK: #[[$LOCAL_ATTR_1:.+]] = #llvm.tbaa_type_desc}> -// CHECK: #[[$ATTR_8:.+]] = #llvm.tbaa_type_desc}> -// CHECK: #[[$ATTR_9:.+]] = #llvm.tbaa_type_desc}> -// CHECK: #[[$LOCAL_ATTR_2:.+]] = #llvm.tbaa_tag -// CHECK: #[[$ATTR_10:.+]] = #llvm.tbaa_tag -// CHECK: #[[$ATTR_11:.+]] = #llvm.tbaa_tag +// CHECK: #[[$ATTR_0:.+]] = #llvm.tbaa_root +// CHECK: #[[$ATTR_1:.+]] = #llvm.tbaa_root +// CHECK: #[[$ATTR_2:.+]] = #llvm.tbaa_type_desc}> +// CHECK: #[[$ATTR_3:.+]] = #llvm.tbaa_type_desc}> +// CHECK: #[[$ATTR_4:.+]] = #llvm.tbaa_type_desc}> +// CHECK: #[[$ATTR_5:.+]] = #llvm.tbaa_type_desc}> +// CHECK: #[[$ATTR_6:.+]] = #llvm.tbaa_type_desc}> +// CHECK: #[[$ATTR_7:.+]] = #llvm.tbaa_type_desc}> +// CHECK: #[[$ATTR_8:.+]] = #llvm.tbaa_type_desc}> +// CHECK: #[[$ATTR_9:.+]] = #llvm.tbaa_type_desc}> +// CHECK: #[[$ATTR_10:.+]] = #llvm.tbaa_type_desc}> +// CHECK: #[[$ATTR_11:.+]] = #llvm.tbaa_type_desc}> +// CHECK: #[[$ATTR_12:.+]] = #llvm.tbaa_tag +// CHECK: #[[$ATTR_13:.+]] = #llvm.tbaa_tag +// CHECK: #[[$ATTR_14:.+]] = #llvm.tbaa_tag // CHECK-LABEL: func.func @_QPtest2() attributes {noinline} { -// CHECK: %[[VAL_2:.*]] = fir.alloca i32 {bindc_name = "y", uniq_name = "_QFtest2FinnerEy"} -// CHECK: %[[VAL_3:.*]] = fir.dummy_scope : !fir.dscope -// CHECK: %[[VAL_4:.*]] = fir.address_of(@_QMmEglob) : !fir.ref -// CHECK: %[[VAL_5:.*]] = fir.declare %[[VAL_4]] {uniq_name = "_QMmEglob"} : (!fir.ref) -> !fir.ref -// CHECK: %[[VAL_6:.*]] = fir.dummy_scope : !fir.dscope -// CHECK: %[[VAL_7:.*]] = fir.declare %[[VAL_5]] dummy_scope %[[VAL_6]] {uniq_name = "_QFtest2FinnerEx"} : (!fir.ref, !fir.dscope) -> !fir.ref -// CHECK: %[[VAL_8:.*]] = fir.declare %[[VAL_2]] {uniq_name = "_QFtest2FinnerEy"} : (!fir.ref) -> !fir.ref -// CHECK: fir.store %{{.*}} to %[[VAL_8]] {tbaa = [#[[$LOCAL_ATTR_2]]]} : !fir.ref -// CHECK: %[[VAL_9:.*]] = fir.load %[[VAL_8]] {tbaa = [#[[$LOCAL_ATTR_2]]]} : !fir.ref -// CHECK: fir.store %[[VAL_9]] to %[[VAL_7]] {tbaa = [#[[$ATTR_10]]]} : !fir.ref -// CHECK: fir.store %{{.*}} to %[[VAL_5]] {tbaa = [#[[$ATTR_11]]]} : !fir.ref +// CHECK: %[[CONSTANT_0:.*]] = arith.constant 1 : i32 +// CHECK: %[[CONSTANT_1:.*]] = arith.constant 2 : i32 +// CHECK: %[[ALLOCA_0:.*]] = fir.alloca i32 {bindc_name = "y", uniq_name = "_QFtest2FinnerEy"} +// CHECK: %[[DUMMY_SCOPE_0:.*]] = fir.dummy_scope : !fir.dscope +// CHECK: %[[ADDRESS_OF_0:.*]] = fir.address_of(@_QMmEglob) : !fir.ref +// CHECK: %[[DECLARE_0:.*]] = fir.declare %[[ADDRESS_OF_0]] {uniq_name = "_QMmEglob"} : (!fir.ref) -> !fir.ref +// CHECK: %[[DUMMY_SCOPE_1:.*]] = fir.dummy_scope : !fir.dscope +// CHECK: %[[DECLARE_1:.*]] = fir.declare %[[DECLARE_0]] dummy_scope %[[DUMMY_SCOPE_1]] {uniq_name = "_QFtest2FinnerEx"} : (!fir.ref, !fir.dscope) -> !fir.ref +// CHECK: %[[DECLARE_2:.*]] = fir.declare %[[ALLOCA_0]] {uniq_name = "_QFtest2FinnerEy"} : (!fir.ref) -> !fir.ref +// CHECK: fir.store %[[CONSTANT_0]] to %[[DECLARE_2]] {tbaa = [#[[$ATTR_12]]]} : !fir.ref +// CHECK: %[[LOAD_0:.*]] = fir.load %[[DECLARE_2]] {tbaa = [#[[$ATTR_12]]]} : !fir.ref +// CHECK: fir.store %[[LOAD_0]] to %[[DECLARE_1]] {tbaa = [#[[$ATTR_13]]]} : !fir.ref +// CHECK: fir.store %[[CONSTANT_1]] to %[[DECLARE_0]] {tbaa = [#[[$ATTR_14]]]} : !fir.ref