diff --git a/flang/lib/Lower/PFTBuilder.cpp b/flang/lib/Lower/PFTBuilder.cpp --- a/flang/lib/Lower/PFTBuilder.cpp +++ b/flang/lib/Lower/PFTBuilder.cpp @@ -1324,6 +1324,13 @@ const Fortran::semantics::Symbol *aggregateSym = nullptr; bool isGlobal = false; const semantics::Symbol &first = *aggregate.front(); + // Skip aggregates related to common blocks as they will be handled by + // instantiateCommon and the aggregate store information will not be used. + // Additionally, the AggregateStoreKeys for common block related aggregate + // stores can collide with non common block ones, potentially resulting in + // incorrect stores being used. + if (lower::definedInCommonBlock(first)) + continue; std::size_t start = first.offset(); std::size_t end = first.offset() + first.size(); const Fortran::semantics::Symbol *namingSym = nullptr; diff --git a/flang/test/Lower/equivalence-2.f90 b/flang/test/Lower/equivalence-2.f90 --- a/flang/test/Lower/equivalence-2.f90 +++ b/flang/test/Lower/equivalence-2.f90 @@ -97,3 +97,34 @@ ! CHECK: %[[xCast:.*]] = fir.convert %[[x]] : (!fir.ptr>) -> !fir.ref> ! CHECK: %[[iCast:.*]] = fir.convert %[[i]] : (!fir.ptr) -> !fir.ref ! CHECK: fir.call @_QPfoo2(%[[xCast]], %[[iCast]]) : (!fir.ref>, !fir.ref) -> () + + +! Check that cases where equivalenced local variables and common blocks will +! share the same offset use the correct stores +! CHECK-LABEL: @_QPeq_and_comm_same_offset() +subroutine eq_and_comm_same_offset + real common_arr1(133),common_arr2(133) + common /my_common_block/ common_arr1,common_arr2 + real arr1(133),arr2(133) + real arr3(133,133),arr4(133,133) + equivalence(arr1,common_arr1),(arr2,common_arr2) + equivalence(arr3,arr4) + + ! CHECK: %[[arr4Store:.*]] = fir.alloca !fir.array<70756xi8> {uniq_name = "_QFeq_and_comm_same_offsetEarr3"} + ! CHECK: %[[mcbAddr:.*]] = fir.address_of(@_QBmy_common_block) : !fir.ref> + ! CHECK: %[[mcbCast:.*]] = fir.convert %[[mcbAddr]] : (!fir.ref>) -> !fir.ref> + ! CHECK: %[[c0:.*]] = arith.constant 0 : index + ! CHECK: %[[mcbCoor:.*]] = fir.coordinate_of %[[mcbCast]], %[[c0]] : (!fir.ref>, index) -> !fir.ref + ! CHECK: %[[mcbCoorCast:.*]] = fir.convert %[[mcbCoor]] : (!fir.ref) -> !fir.ptr> + ! CHECK: %[[c1:.*]] = arith.constant 0 : index + ! CHECK: %[[arr4Addr:.*]] = fir.coordinate_of %[[arr4Store]], %[[c1]] : (!fir.ref>, index) -> !fir.ref + ! CHECK: %[[arr4Cast:.*]] = fir.convert %[[arr4Addr]] : (!fir.ref) -> !fir.ptr> + + arr1(1) = 1 + ! CHECK:%[[mcbFinalAddr:.*]] = fir.coordinate_of %[[mcbCoorCast]], %{{.*}} : (!fir.ptr>, i64) -> !fir.ref + ! CHECK:fir.store %{{.*}} to %[[mcbFinalAddr]] : !fir.ref + + arr4(1,1) = 2 + ! CHECK: %[[arr4FinalAddr:.*]] = fir.coordinate_of %[[arr4Cast]], %{{.*}}, %{{.*}} : (!fir.ptr>, i64, i64) -> !fir.ref + ! CHECK: fir.store %{{.*}} to %[[arr4FinalAddr]] : !fir.ref +end subroutine