[flang-commits] [flang] [Flang][OpenMP] Seek over an HLFIR declare to check for threadprivate (PR #65507)
via flang-commits
flang-commits at lists.llvm.org
Wed Sep 6 10:33:15 PDT 2023
https://github.com/kiranchandramohan created https://github.com/llvm/llvm-project/pull/65507:
To check whether we have already generated a threadprivate operation, we have to seek over the HLFIR declare in the HLFIR flow.
>From 9f5627d4f8dadea298941b792bfab4f4da4bcdcb Mon Sep 17 00:00:00 2001
From: Kiran Chandramohan <kiran.chandramohan at arm.com>
Date: Wed, 6 Sep 2023 17:09:44 +0000
Subject: [PATCH] [Flang][OpenMP] Seek over an HLFIR declare to check for
threadprivate
To check whether we have already generated a threadprivate operation,
we have to seek over the HLFIR declare.
---
flang/lib/Lower/OpenMP.cpp | 8 +++-
.../threadprivate-use-association-2-hlfir.f90 | 43 +++++++++++++++++++
2 files changed, 50 insertions(+), 1 deletion(-)
create mode 100644 flang/test/Lower/OpenMP/threadprivate-use-association-2-hlfir.f90
diff --git a/flang/lib/Lower/OpenMP.cpp b/flang/lib/Lower/OpenMP.cpp
index 9cb370e6130dc2e..e56b26a243a4423 100644
--- a/flang/lib/Lower/OpenMP.cpp
+++ b/flang/lib/Lower/OpenMP.cpp
@@ -3666,12 +3666,18 @@ void Fortran::lower::genThreadprivateOp(
currentLocation, symValue.getType(), symValue);
} else {
mlir::Value symValue = converter.getSymbolAddress(sym);
- mlir::Operation *op = symValue.getDefiningOp();
+
// The symbol may be use-associated multiple times, and nothing needs to be
// done after the original symbol is mapped to the threadprivatized value
// for the first time. Use the threadprivatized value directly.
+ mlir::Operation *op;
+ if (auto declOp = symValue.getDefiningOp<hlfir::DeclareOp>())
+ op = declOp.getMemref().getDefiningOp();
+ else
+ op = symValue.getDefiningOp();
if (mlir::isa<mlir::omp::ThreadprivateOp>(op))
return;
+
symThreadprivateValue = firOpBuilder.create<mlir::omp::ThreadprivateOp>(
currentLocation, symValue.getType(), symValue);
}
diff --git a/flang/test/Lower/OpenMP/threadprivate-use-association-2-hlfir.f90 b/flang/test/Lower/OpenMP/threadprivate-use-association-2-hlfir.f90
new file mode 100644
index 000000000000000..722f023fbefc96d
--- /dev/null
+++ b/flang/test/Lower/OpenMP/threadprivate-use-association-2-hlfir.f90
@@ -0,0 +1,43 @@
+! This test checks lowering of OpenMP Threadprivate Directive.
+! Test for threadprivate variable double use in use association.
+
+!RUN: %flang_fc1 -emit-hlfir -flang-experimental-hlfir -fopenmp %s -o - | FileCheck %s
+!RUN: bbc -emit-hlfir -fopenmp %s -o - | FileCheck %s
+
+! CHECK-LABEL: fir.global @_QMmEx : i32
+module m
+ integer :: x
+ !$omp threadprivate(x)
+end
+
+! CHECK-LABEL: func.func @_QMm2Ptest() {
+! CHECK: %[[VAL_0:.*]] = fir.address_of(@_QMmEx) : !fir.ref<i32>
+! CHECK: %[[VAL_1:.*]]:2 = hlfir.declare %[[VAL_0]] {uniq_name = "_QMmEx"} : (!fir.ref<i32>) -> (!fir.ref<i32>, !fir.ref<i32>)
+! CHECK: %[[VAL_2:.*]] = omp.threadprivate %[[VAL_1]]#1 : !fir.ref<i32> -> !fir.ref<i32>
+! CHECK: %[[VAL_3:.*]]:2 = hlfir.declare %[[VAL_2]] {uniq_name = "_QMmEx"} : (!fir.ref<i32>) -> (!fir.ref<i32>, !fir.ref<i32>)
+! CHECK: fir.call @_QPbar(%[[VAL_3]]#1) {{.*}}: (!fir.ref<i32>) -> ()
+! CHECK: return
+! CHECK: }
+
+! CHECK-LABEL: func.func @_QMm2FtestPinternal_test() {
+! CHECK: %[[VAL_0:.*]] = fir.address_of(@_QMmEx) : !fir.ref<i32>
+! CHECK: %[[VAL_1:.*]]:2 = hlfir.declare %[[VAL_0]] {uniq_name = "_QMmEx"} : (!fir.ref<i32>) -> (!fir.ref<i32>, !fir.ref<i32>)
+! CHECK: %[[VAL_2:.*]] = omp.threadprivate %[[VAL_1]]#1 : !fir.ref<i32> -> !fir.ref<i32>
+! CHECK: %[[VAL_3:.*]]:2 = hlfir.declare %[[VAL_2]] {uniq_name = "_QMmEx"} : (!fir.ref<i32>) -> (!fir.ref<i32>, !fir.ref<i32>)
+! CHECK: fir.call @_QPbar(%[[VAL_3]]#1) {{.*}}: (!fir.ref<i32>) -> ()
+! CHECK: return
+! CHECK: }
+
+module m2
+ use m
+ contains
+ subroutine test()
+ use m
+ call bar(x)
+ contains
+ subroutine internal_test()
+ use m
+ call bar(x)
+ end
+ end
+end
More information about the flang-commits
mailing list