Skip to content

[flang][OpenMP] Delayed privatization for variables with equivalence association #100531

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Aug 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 14 additions & 3 deletions flang/lib/Lower/OpenMP/DataSharingProcessor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -549,9 +549,20 @@ void DataSharingProcessor::doPrivatize(const semantics::Symbol *sym,
symTable->addSymbol(*sym, localExV);
symTable->pushScope();
cloneSymbol(sym);
firOpBuilder.create<mlir::omp::YieldOp>(
hsb.getAddr().getLoc(),
symTable->shallowLookupSymbol(*sym).getAddr());
mlir::Value cloneAddr = symTable->shallowLookupSymbol(*sym).getAddr();
mlir::Type cloneType = cloneAddr.getType();

// A `convert` op is required for variables that are storage associated
// via `equivalence`. The problem is that these variables are declared as
// `fir.ptr`s while their privatized storage is declared as `fir.ref`,
// therefore we convert to proper symbol type.
mlir::Value yieldedValue =
(symType == cloneType) ? cloneAddr
: firOpBuilder.createConvert(
cloneAddr.getLoc(), symType, cloneAddr);

firOpBuilder.create<mlir::omp::YieldOp>(hsb.getAddr().getLoc(),
yieldedValue);
symTable->popScope();
}

Expand Down
37 changes: 37 additions & 0 deletions flang/test/Lower/OpenMP/DelayedPrivatization/equivalence.f90
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
! Test delayed privatization for variables that are storage associated via `EQUIVALENCE`.

! RUN: %flang_fc1 -emit-hlfir -fopenmp -mmlir --openmp-enable-delayed-privatization \
! RUN: -o - %s 2>&1 | FileCheck %s
! RUN: bbc -emit-hlfir -fopenmp --openmp-enable-delayed-privatization -o - %s 2>&1 \
! RUN: | FileCheck %s

subroutine private_common
real x, y
equivalence (x,y)
!$omp parallel firstprivate(x)
x = 3.14
!$omp end parallel
end subroutine

! CHECK: omp.private {type = firstprivate} @[[X_PRIVATIZER:.*]] : ![[X_TYPE:fir.ptr<f32>]] alloc {
! CHECK: ^bb0(%{{.*}}: ![[X_TYPE]]):
! CHECK: %[[PRIV_ALLOC:.*]] = fir.alloca f32 {bindc_name = "x", {{.*}}}
! CHECK: %[[PRIV_DECL:.*]]:2 = hlfir.declare %[[PRIV_ALLOC]] {{{.*}}} : (![[PRIV_TYPE:fir.ref<f32>]]) -> ({{.*}})
! CHECK: %[[PRIV_CONV:.*]] = fir.convert %[[PRIV_DECL]]#0 : (![[PRIV_TYPE]]) -> ![[X_TYPE]]
! CHECK: omp.yield(%[[PRIV_CONV]] : ![[X_TYPE]])
! CHECK: } copy {
! CHECK: ^bb0(%[[ORIG_PTR:.*]]: ![[X_TYPE]], %[[PRIV_REF:.*]]: ![[X_TYPE]]):
! CHECK: %[[ORIG_VAL:.*]] = fir.load %[[ORIG_PTR]] : !fir.ptr<f32>
! CHECK: hlfir.assign %[[ORIG_VAL]] to %[[PRIV_REF]] temporary_lhs : f32, ![[X_TYPE]]
! CHECK: omp.yield(%[[PRIV_REF]] : ![[X_TYPE]])
! CHECK: }

! CHECK: func.func @_QPprivate_common() {
! CHECK: omp.parallel private(@[[X_PRIVATIZER]] %{{.*}}#0 -> %[[PRIV_ARG:.*]] : ![[X_TYPE]]) {
! CHECK: %[[REG_DECL:.*]]:2 = hlfir.declare %[[PRIV_ARG]] {{{.*}}} : (![[X_TYPE]]) -> ({{.*}})
! CHECK: %[[CST:.*]] = arith.constant {{.*}}
! CHECK: hlfir.assign %[[CST]] to %[[REG_DECL]]#0 : {{.*}}
! CHECK: omp.terminator
! CHECK: }
! CHECK: return
! CHECK: }
36 changes: 36 additions & 0 deletions mlir/test/Target/LLVMIR/openmp-private.mlir
Original file line number Diff line number Diff line change
Expand Up @@ -234,3 +234,39 @@ omp.declare_reduction @reducer.part : !llvm.ptr init {
^bb0(%arg0: !llvm.ptr):
omp.yield
}

// -----

llvm.func @_QPequivalence() {
%0 = llvm.mlir.constant(1 : i64) : i64
%1 = llvm.alloca %0 x !llvm.array<4 x i8> : (i64) -> !llvm.ptr
%2 = llvm.mlir.constant(0 : index) : i64
%3 = llvm.getelementptr %1[0, %2] : (!llvm.ptr, i64) -> !llvm.ptr, !llvm.array<4 x i8>
omp.parallel private(@_QFequivalenceEx_firstprivate_ptr_f32 %3 -> %arg0 : !llvm.ptr) {
%4 = llvm.mlir.constant(3.140000e+00 : f32) : f32
llvm.store %4, %arg0 : f32, !llvm.ptr
omp.terminator
}
llvm.return
}

omp.private {type = firstprivate} @_QFequivalenceEx_firstprivate_ptr_f32 : !llvm.ptr alloc {
^bb0(%arg0: !llvm.ptr):
%0 = llvm.mlir.constant(1 : i64) : i64
%1 = llvm.alloca %0 x f32 {bindc_name = "x", pinned} : (i64) -> !llvm.ptr
omp.yield(%1 : !llvm.ptr)
} copy {
^bb0(%arg0: !llvm.ptr, %arg1: !llvm.ptr):
%0 = llvm.load %arg0 : !llvm.ptr -> f32
llvm.store %0, %arg1 : f32, !llvm.ptr
omp.yield(%arg1 : !llvm.ptr)
}

// CHECK: define internal void @_QPequivalence..omp_par
// CHECK-NOT: define {{.*}} @{{.*}}
// CHECK: %[[PRIV_ALLOC:.*]] = alloca float, i64 1, align 4
// CHECK: %[[HOST_VAL:.*]] = load float, ptr %{{.*}}, align 4
// Test that we initialize the firstprivate variable.
// CHECK: store float %[[HOST_VAL]], ptr %[[PRIV_ALLOC]], align 4
// Test that we inlined the body of the parallel region.
// CHECK: store float 0x{{.*}}, ptr %[[PRIV_ALLOC]], align 4
Loading