From c94eaa5a8f7f08bc03f3618bd45e88c8d7a33ff8 Mon Sep 17 00:00:00 2001 From: Harish Chambeti Date: Sun, 10 Dec 2023 10:18:47 +0530 Subject: [PATCH] [Flan][OpenMP] Fix to variables not inheriting data sharing attributes --- flang/lib/Semantics/resolve-directives.cpp | 3 +- .../OpenMP/threadprivate-default-clause.f90 | 60 +++++++++++++++++++ 2 files changed, 62 insertions(+), 1 deletion(-) create mode 100644 flang/test/Lower/OpenMP/threadprivate-default-clause.f90 diff --git a/flang/lib/Semantics/resolve-directives.cpp b/flang/lib/Semantics/resolve-directives.cpp index da6c865ad56a3b..d3f8f1d3ad7c91 100644 --- a/flang/lib/Semantics/resolve-directives.cpp +++ b/flang/lib/Semantics/resolve-directives.cpp @@ -1910,7 +1910,8 @@ void OmpAttributeVisitor::Post(const parser::Name &name) { if (Symbol * found{currScope().FindSymbol(name.source)}) { if (symbol != found) { name.symbol = found; // adjust the symbol within region - } else if (GetContext().defaultDSA == Symbol::Flag::OmpNone) { + } else if (GetContext().defaultDSA == Symbol::Flag::OmpNone && + !symbol->test(Symbol::Flag::OmpThreadprivate)) { context_.Say(name.source, "The DEFAULT(NONE) clause requires that '%s' must be listed in " "a data-sharing attribute clause"_err_en_US, diff --git a/flang/test/Lower/OpenMP/threadprivate-default-clause.f90 b/flang/test/Lower/OpenMP/threadprivate-default-clause.f90 new file mode 100644 index 00000000000000..be07aeca2d5c32 --- /dev/null +++ b/flang/test/Lower/OpenMP/threadprivate-default-clause.f90 @@ -0,0 +1,60 @@ +! Simple test for lowering of OpenMP Threadprivate Directive with HLFIR. + +!RUN: %flang_fc1 -emit-hlfir -fopenmp %s -o - | FileCheck %s + +!CHECK-LABEL: func.func @_QPsub1() { +!CHECK: %[[A:.*]] = fir.address_of(@_QFsub1Ea) : !fir.ref +!CHECK: %[[A_DECL:.*]]:2 = hlfir.declare %[[A]] {uniq_name = "_QFsub1Ea"} : (!fir.ref) -> (!fir.ref, !fir.ref) +!CHECK: %[[A_TP0:.*]] = omp.threadprivate %[[A_DECL]]#1 : !fir.ref -> !fir.ref +!CHECK: %[[A_CVT:.*]]:2 = hlfir.declare %[[A_TP0]] {uniq_name = "_QFsub1Ea"} : (!fir.ref) -> (!fir.ref, !fir.ref) +!CHECK: omp.parallel { +!CHECK: %[[A_TP:.*]] = omp.threadprivate %[[A_DECL]]#1 : !fir.ref -> !fir.ref +!CHECK: %[[A_TP_DECL:.*]]:2 = hlfir.declare %[[A_TP]] {uniq_name = "_QFsub1Ea"} : (!fir.ref) -> (!fir.ref, !fir.ref) +!CHECK: %[[TID:.*]] = fir.call @omp_get_thread_num() fastmath : () -> i32 +!CHECK: hlfir.assign %[[TID]] to %[[A_TP_DECL]]#0 : i32, !fir.ref +!CHECK: omp.terminator +!CHECK: } + +subroutine sub1() + use omp_lib + integer, save :: a + !$omp threadprivate(a) + !$omp parallel default(none) + a = omp_get_thread_num() + !$omp end parallel +end subroutine + +!CHECK-LABEL: func.func @_QPsub2() { +!CHECK: %[[BLK:.*]] = fir.address_of(@blk_) : !fir.ref> +!CHECK: %[[BLK_CVT:.*]] = fir.convert %[[BLK]] : (!fir.ref>) -> !fir.ref> +!CHECK: %c0 = arith.constant 0 : index +!CHECK: %[[A_ADDR:.*]] = fir.coordinate_of %[[BLK_CVT]], %c0 : (!fir.ref>, index) -> !fir.ref +!CHECK: %[[A_CVT:.*]] = fir.convert %[[A_ADDR]] : (!fir.ref) -> !fir.ref +!CHECK: %[[A_DECL:.*]]:2 = hlfir.declare %[[A_CVT]] {uniq_name = "_QFsub2Ea"} : (!fir.ref) -> (!fir.ref, !fir.ref) +!CHECK: %[[A_TP0:.*]] = omp.threadprivate %[[BLK]] : !fir.ref> -> !fir.ref> +!CHECK: %[[A_TP0_CVT:.*]] = fir.convert %[[A_TP0]] : (!fir.ref>) -> !fir.ref> +!CHECK: %c0_0 = arith.constant 0 : index +!CHECK: %[[A_TP0_ADDR:.*]] = fir.coordinate_of %[[A_TP0_CVT]], %c0_0 : (!fir.ref>, index) -> !fir.ref +!CHECK: %[[A_TP0_ADDR_CVT:.*]] = fir.convert %[[A_TP0_ADDR]] : (!fir.ref) -> !fir.ref +!CHECK: %[[A_TP0_DECL:.*]]:2 = hlfir.declare %[[A_TP0_ADDR_CVT]] {uniq_name = "_QFsub2Ea"} : (!fir.ref) -> (!fir.ref, !fir.ref) +!CHECK: omp.parallel { +!CHECK: %[[BLK_TP:.*]] = omp.threadprivate %[[BLK]] : !fir.ref> -> !fir.ref> +!CHECK: %[[BLK_TP_CVT:.*]] = fir.convert %[[BLK_TP]] : (!fir.ref>) -> !fir.ref> +!CHECK: %c0_1 = arith.constant 0 : index +!CHECK: %[[A_TP_ADDR:.*]] = fir.coordinate_of %[[BLK_TP_CVT]], %c0_1 : (!fir.ref>, index) -> !fir.ref +!CHECK: %[[A_TP_ADDR_CVT:.*]] = fir.convert %[[A_TP_ADDR]] : (!fir.ref) -> !fir.ref +!CHECK: %[[A_TP_DECL:.*]]:2 = hlfir.declare %[[A_TP_ADDR_CVT]] {uniq_name = "_QFsub2Ea"} : (!fir.ref) -> (!fir.ref, !fir.ref) +!CHECK: %[[TID:.*]] = fir.call @omp_get_thread_num() fastmath : () -> i32 +!CHECK: hlfir.assign %[[TID]] to %[[A_TP_DECL]]#0 : i32, !fir.ref +!CHECK: omp.terminator +!CHECK: } + +subroutine sub2() + use omp_lib + integer :: a + common/blk/a + !$omp threadprivate(/blk/) + !$omp parallel default(none) + a = omp_get_thread_num() + !$omp end parallel +end subroutine