[flang-commits] [flang] [flang][OpenMP] Port OpenMP FIR tests for atomic update/capture to HLFIR (PR #70627)

via flang-commits flang-commits at lists.llvm.org
Sun Oct 29 23:25:46 PDT 2023


llvmbot wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-flang-fir-hlfir

Author: None (NimishMishra)

<details>
<summary>Changes</summary>

Port atomic update and capture tests to HLFIR.

---
Full diff: https://github.com/llvm/llvm-project/pull/70627.diff


3 Files Affected:

- (added) flang/test/Lower/OpenMP/atomic-capture.f90 (+98) 
- (removed) flang/test/Lower/OpenMP/atomic-update-hlfir.f90 (-23) 
- (added) flang/test/Lower/OpenMP/atomic-update.f90 (+149) 


``````````diff
diff --git a/flang/test/Lower/OpenMP/atomic-capture.f90 b/flang/test/Lower/OpenMP/atomic-capture.f90
new file mode 100644
index 000000000000000..cd4a93fd9c10aa3
--- /dev/null
+++ b/flang/test/Lower/OpenMP/atomic-capture.f90
@@ -0,0 +1,98 @@
+! This test checks the lowering of atomic capture
+
+! RUN: bbc --use-desc-for-alloc=false -fopenmp -emit-hlfir %s -o - | FileCheck %s
+! RUN: %flang_fc1 -mllvm --use-desc-for-alloc=false -emit-hlfir -fopenmp %s -o - | FileCheck %s 
+
+
+
+program OmpAtomicCapture
+    use omp_lib                                                                                                       
+
+!CHECK: %[[val_X_alloca:.*]] = fir.alloca i32 {bindc_name = "x", uniq_name = "_QFEx"}
+!CHECK: %[[val_X_declare:.*]]:2 = hlfir.declare %[[val_X_alloca]] {{.*}}
+!CHECK: %[[val_Y_alloca:.*]] = fir.alloca i32 {bindc_name = "y", uniq_name = "_QFEy"}
+!CHECK: %[[val_Y_declare:.*]]:2 = hlfir.declare %[[val_Y_alloca]] {{.*}}
+    integer :: x, y
+
+!CHECK: %[[val_Y_loaded:.*]] = fir.load %[[val_X_declare]]#0 : !fir.ref<i32>
+!CHECK: omp.atomic.capture hint(uncontended) {
+!CHECK: omp.atomic.update %[[val_Y_declare]]#1 : !fir.ref<i32> {
+!CHECK: ^bb0(%[[ARG:.*]]: i32):
+!CHECK: %[[temp:.*]] = arith.muli %[[val_Y_loaded]], %[[ARG]] : i32
+!CHECK: omp.yield(%[[temp]] : i32)
+!CHECK: }
+!CHECK: omp.atomic.read %[[val_X_declare]]#1 = %[[val_Y_declare]]#1 : !fir.ref<i32>, i32
+!CHECK: }
+    !$omp atomic hint(omp_sync_hint_uncontended) capture
+        y = x * y 
+        x = y
+    !$omp end atomic
+
+!CHECK: %[[val_20:.*]] = arith.constant 20 : i32
+!CHECK: %[[val_8:.*]] = arith.constant 8 : i32
+!CHECK: %[[val_X_loaded:.*]] = fir.load %[[val_X_declare]]#0 : !fir.ref<i32>
+!CHECK: %[[sub:.*]] = arith.subi %[[val_8]], %[[val_X_loaded]] : i32
+!CHECK: %[[no_reassoc:.*]] = hlfir.no_reassoc %[[sub]] : i32
+!CHECK: %[[add:.*]] = arith.addi  %[[val_20]], %[[no_reassoc]] : i32
+!CHECK: omp.atomic.capture memory_order(acquire) hint(nonspeculative) {
+!CHECK:   omp.atomic.read %[[val_X_declare]]#1 = %[[val_Y_declare]]#1 : !fir.ref<i32>, i32
+!CHECK:   omp.atomic.write %[[val_Y_declare]]#1 = %[[add]] : !fir.ref<i32>, i32
+!CHECK: }
+!CHECK: return
+!CHECK: }
+    !$omp atomic hint(omp_lock_hint_nonspeculative) capture acquire
+        x = y
+        y = 2 * 10 + (8 - x) 
+    !$omp end atomic
+end program
+
+
+!CHECK: func.func @_QPpointers_in_atomic_capture() {
+subroutine pointers_in_atomic_capture()
+
+!CHECK: %[[val_A_alloca:.*]] = fir.alloca !fir.box<!fir.ptr<i32>> {bindc_name = "a", uniq_name = "_QFpointers_in_atomic_captureEa"}
+!CHECK: %[[zero:.*]] = fir.zero_bits !fir.ptr<i32>
+!CHECK: %[[embox:.*]] = fir.embox %[[zero]] : (!fir.ptr<i32>) -> !fir.box<!fir.ptr<i32>>
+!CHECK: fir.store %[[embox]] to %[[val_A_alloca]] : !fir.ref<!fir.box<!fir.ptr<i32>>>
+!CHECK: %[[val_A_declare:.*]]:2 = hlfir.declare %[[val_A_alloca]] {{.*}}
+!CHECK: %[[val_B_alloca:.*]] = fir.alloca !fir.box<!fir.ptr<i32>> {bindc_name = "b", uniq_name = "_QFpointers_in_atomic_captureEb"}
+!CHECK: %[[zero:.*]] = fir.zero_bits !fir.ptr<i32>
+!CHECK: %[[embox:.*]] = fir.embox %[[zero]] : (!fir.ptr<i32>) -> !fir.box<!fir.ptr<i32>>
+!CHECK: fir.store %[[embox]] to %[[val_B_alloca]] : !fir.ref<!fir.box<!fir.ptr<i32>>>
+!CHECK: %[[val_B_declare:.*]]:2 = hlfir.declare %[[val_B_alloca]] {{.*}}
+!CHECK: %[[val_C_alloca:.*]] = fir.alloca i32 {bindc_name = "c", fir.target, uniq_name = "_QFpointers_in_atomic_captureEc"}
+!CHECK: %[[val_C_declare:.*]]:2 = hlfir.declare %[[val_C_alloca]] {{.*}}
+!CHECK: %[[val_D_alloca:.*]] = fir.alloca i32 {bindc_name = "d", fir.target, uniq_name = "_QFpointers_in_atomic_captureEd"}
+!CHECK: %[[val_D_declare:.*]]:2 = hlfir.declare %[[val_D_alloca]] {{.*}}
+    integer, pointer :: a, b
+    integer, target :: c, d
+
+!CHECK: %[[embox:.*]] = fir.embox %[[val_C_declare]]#1 : (!fir.ref<i32>) -> !fir.box<!fir.ptr<i32>>
+!CHECK: fir.store %[[embox]] to %[[val_A_declare]]#1 : !fir.ref<!fir.box<!fir.ptr<i32>>>
+!CHECK: %[[embox:.*]] = fir.embox %[[val_D_declare]]#1 : (!fir.ref<i32>) -> !fir.box<!fir.ptr<i32>>
+!CHECK: fir.store %[[embox]] to %[[val_B_declare]]#1 : !fir.ref<!fir.box<!fir.ptr<i32>>>
+    a=>c
+    b=>d
+
+!CHECK: %[[val_A_loaded:.*]] = fir.load %[[val_A_declare]]#0 : !fir.ref<!fir.box<!fir.ptr<i32>>>
+!CHECK: %[[val_A_box_addr:.*]] = fir.box_addr %[[val_A_loaded]] : (!fir.box<!fir.ptr<i32>>) -> !fir.ptr<i32>
+!CHECK: %[[val_B_loaded:.*]] = fir.load %[[val_B_declare]]#0 : !fir.ref<!fir.box<!fir.ptr<i32>>>
+!CHECK: %[[val_B_box_addr:.*]] = fir.box_addr %[[val_B_loaded]] : (!fir.box<!fir.ptr<i32>>) -> !fir.ptr<i32>
+!CHECK: %[[val_B_loaded_2:.*]] = fir.load %[[val_B_declare]]#0 : !fir.ref<!fir.box<!fir.ptr<i32>>>
+!CHECK: %[[val_B_box_addr_2:.*]] = fir.box_addr %[[val_B_loaded_2]] : (!fir.box<!fir.ptr<i32>>) -> !fir.ptr<i32>
+!CHECK: %[[val_B:.*]] = fir.load %[[val_B_box_addr_2]] : !fir.ptr<i32>
+!CHECK: omp.atomic.capture {
+!CHECK: omp.atomic.update %[[val_A_box_addr]] : !fir.ptr<i32> {
+!CHECK: ^bb0(%[[ARG:.*]]: i32):
+!CHECK: %[[temp:.*]] = arith.addi %[[ARG]], %[[val_B]] : i32
+!CHECK: omp.yield(%[[temp]] : i32)
+!CHECK: }
+!CHECK: omp.atomic.read %[[val_B_box_addr]] = %[[val_A_box_addr]] : !fir.ptr<i32>, i32
+!CHECK: }
+!CHECK: return
+!CHECK: }
+    !$omp atomic capture
+        a = a + b
+        b = a
+    !$omp end atomic
+end subroutine
diff --git a/flang/test/Lower/OpenMP/atomic-update-hlfir.f90 b/flang/test/Lower/OpenMP/atomic-update-hlfir.f90
deleted file mode 100644
index 329009ab8ef8e9b..000000000000000
--- a/flang/test/Lower/OpenMP/atomic-update-hlfir.f90
+++ /dev/null
@@ -1,23 +0,0 @@
-! This test checks lowering of atomic and atomic update constructs with HLFIR
-! RUN: bbc -hlfir -fopenmp -emit-hlfir %s -o - | FileCheck %s
-! RUN: %flang_fc1 -flang-experimental-hlfir -emit-hlfir -fopenmp %s -o - | FileCheck %s
-
-subroutine sb
-  integer :: x, y
-
-  !$omp atomic update
-    x = x + y
-end subroutine
-
-!CHECK-LABEL: @_QPsb
-!CHECK:   %[[X_REF:.*]] = fir.alloca i32 {bindc_name = "x", uniq_name = "_QFsbEx"}
-!CHECK:   %[[X_DECL:.*]]:2 = hlfir.declare %[[X_REF]] {uniq_name = "_QFsbEx"} : (!fir.ref<i32>) -> (!fir.ref<i32>, !fir.ref<i32>)
-!CHECK:   %[[Y_REF:.*]] = fir.alloca i32 {bindc_name = "y", uniq_name = "_QFsbEy"}
-!CHECK:   %[[Y_DECL:.*]]:2 = hlfir.declare %[[Y_REF]] {uniq_name = "_QFsbEy"} : (!fir.ref<i32>) -> (!fir.ref<i32>, !fir.ref<i32>)
-!CHECK:   %[[Y_VAL:.*]] = fir.load %[[Y_DECL]]#0 : !fir.ref<i32>
-!CHECK:   omp.atomic.update   %[[X_DECL]]#1 : !fir.ref<i32> {
-!CHECK:   ^bb0(%[[ARG_X:.*]]: i32):
-!CHECK:     %[[X_UPDATE_VAL:.*]] = arith.addi %[[ARG_X]], %[[Y_VAL]] : i32
-!CHECK:     omp.yield(%[[X_UPDATE_VAL]] : i32)
-!CHECK:   }
-!CHECK:   return
diff --git a/flang/test/Lower/OpenMP/atomic-update.f90 b/flang/test/Lower/OpenMP/atomic-update.f90
new file mode 100644
index 000000000000000..8934695056ffa7d
--- /dev/null
+++ b/flang/test/Lower/OpenMP/atomic-update.f90
@@ -0,0 +1,149 @@
+! This test checks lowering of atomic and atomic update constructs
+! RUN: bbc --use-desc-for-alloc=false -fopenmp -emit-hlfir %s -o - | FileCheck %s
+! RUN: %flang_fc1 -mllvm --use-desc-for-alloc=false -emit-hlfir -fopenmp %s -o - | FileCheck %s
+
+program OmpAtomicUpdate
+    use omp_lib
+!CHECK: %[[val_A:.*]] = fir.alloca !fir.box<!fir.ptr<i32>> {bindc_name = "a", uniq_name = "_QFEa"}
+!CHECK: %[[zero:.*]] = fir.zero_bits !fir.ptr<i32>
+!CHECK: %[[embox:.*]] = fir.embox %[[zero]] : (!fir.ptr<i32>) -> !fir.box<!fir.ptr<i32>>
+!CHECK: fir.store %[[embox]] to %[[val_A]] : !fir.ref<!fir.box<!fir.ptr<i32>>>
+!CHECK: %[[val_A_declare:.*]]:2 = hlfir.declare %[[val_A]] {{.*}}
+!CHECK: %[[val_B:.*]] = fir.alloca !fir.box<!fir.ptr<i32>> {bindc_name = "b", uniq_name = "_QFEb"}
+!CHECK: %[[zero:.*]] = fir.zero_bits !fir.ptr<i32>
+!CHECK: %[[embox:.*]] = fir.embox %[[zero]] : (!fir.ptr<i32>) -> !fir.box<!fir.ptr<i32>>
+!CHECK: fir.store %[[embox]] to %[[val_B]] : !fir.ref<!fir.box<!fir.ptr<i32>>>
+!CHECK: %[[val_B_declare:.*]]:2 = hlfir.declare %[[val_B]] {{.*}}
+!CHECK: %[[val_C_address:.*]] = fir.address_of(@_QFEc) : !fir.ref<i32>
+!CHECK: %[[val_C_declare:.*]]:2 = hlfir.declare %[[val_C_address]] {{.*}}
+!CHECK: %[[val_D_address:.*]] = fir.address_of(@_QFEd) : !fir.ref<i32>
+!CHECK: %[[val_D_declare:.*]]:2 = hlfir.declare %[[val_D_address]] {{.}}
+!CHECK: %[[val_i1_alloca:.*]] = fir.alloca i8 {bindc_name = "i1", uniq_name = "_QFEi1"}
+!CHECK: %[[val_i1_declare:.*]]:2 = hlfir.declare %[[val_i1_alloca]] {{.*}}
+!CHECK: %[[val_c5:.*]] = arith.constant 5 : index
+!CHECK: %[[val_k_alloca:.*]] = fir.alloca !fir.array<5xi32> {bindc_name = "k", uniq_name = "_QFEk"}
+!CHECK: %[[val_k_shaped:.*]] = fir.shape %[[val_c5]] : (index) -> !fir.shape<1>
+!CHECK: %[[val_K_declare:.*]]:2 = hlfir.declare %[[val_k_alloca]](%[[val_k_shaped]]) {{.*}}
+
+!CHECK: %[[val_X_alloca:.*]] = fir.alloca i32 {bindc_name = "x", uniq_name = "_QFEx"}
+!CHECK: %[[val_X_declare:.*]]:2 = hlfir.declare %[[val_X_alloca]] {uniq_name = "_QFEx"} : (!fir.ref<i32>) -> (!fir.ref<i32>, !fir.ref<i32>)
+!CHECK: %[[val_Y_alloca:.*]] = fir.alloca i32 {bindc_name = "y", uniq_name = "_QFEy"}
+!CHECK: %[[val_Y_declare:.*]]:2 = hlfir.declare %[[val_Y_alloca]] {uniq_name = "_QFEy"} : (!fir.ref<i32>) -> (!fir.ref<i32>, !fir.ref<i32>)
+!CHECK: %[[val_Z_alloca:.*]] = fir.alloca i32 {bindc_name = "z", uniq_name = "_QFEz"}
+!CHECK: %[[val_Z_declare:.*]]:2 = hlfir.declare %[[val_Z_alloca]] {uniq_name = "_QFEz"} : (!fir.ref<i32>) -> (!fir.ref<i32>, !fir.ref<i32>)
+    integer :: x, y, z
+    integer, pointer :: a, b
+    integer, target :: c, d
+    integer(1) :: i1
+    integer, dimension(5) :: k
+
+!CHECK: %[[embox:.*]] = fir.embox %[[val_C_declare]]#1 : (!fir.ref<i32>) -> !fir.box<!fir.ptr<i32>>
+!CHECK: fir.store %[[embox]] to %[[val_A_declare]]#1 : !fir.ref<!fir.box<!fir.ptr<i32>>>
+!CHECK: %[[embox:.*]] = fir.embox %[[val_D_declare]]#1 : (!fir.ref<i32>) -> !fir.box<!fir.ptr<i32>>
+!CHECK: fir.store %[[embox]] to %[[val_B_declare]]#1 : !fir.ref<!fir.box<!fir.ptr<i32>>>
+    a=>c
+    b=>d
+
+!CHECK: %[[val_c3:.*]] = arith.constant 3 : index
+!CHECK: %[[val_K_designate:.*]] = hlfir.designate %[[val_K_declare]]#0 (%[[val_c3]]) : (!fir.ref<!fir.array<5xi32>>, index) -> !fir.ref<i32>
+!CHECK: %[[loaded_Z:.*]] = fir.load %[[val_Z_declare]]#0 : !fir.ref<i32>
+!CHECK: omp.atomic.update %[[val_K_designate]] : !fir.ref<i32> {
+!CHECK:  ^bb0(%[[ARG:.*]]: i32):
+!CHECK:     %[[temp:.*]] = arith.muli %[[loaded_Z]], %[[ARG]] : i32
+!CHECK:     omp.yield(%[[temp]] : i32)
+!CHECK: }
+   !$omp atomic update
+        k(3) = z * k(3)
+
+!CHECK: %[[val_A_loaded:.*]] = fir.load %[[val_A_declare]]#0 : !fir.ref<!fir.box<!fir.ptr<i32>>>
+!CHECK: %[[val_A_box_addr:.*]] = fir.box_addr %[[val_A_loaded]] : (!fir.box<!fir.ptr<i32>>) -> !fir.ptr<i32>
+!CHECK: %[[val_B_loaded:.*]] = fir.load %[[val_B_declare]]#0 : !fir.ref<!fir.box<!fir.ptr<i32>>>
+!CHECK: %[[val_B_box_addr:.*]] = fir.box_addr %[[val_B_loaded]] : (!fir.box<!fir.ptr<i32>>) -> !fir.ptr<i32>
+!CHECK: %[[val_B:.*]] = fir.load %[[val_B_box_addr]] : !fir.ptr<i32>
+!CHECK: omp.atomic.update %[[val_A_box_addr]] : !fir.ptr<i32> {
+!CHECK: ^bb0(%[[ARG:.*]]: i32):
+!CHECK: %[[temp:.*]] = arith.addi %[[ARG]], %[[val_B]] : i32
+!CHECK: omp.yield(%[[temp]] : i32)
+!CHECK: }
+   !$omp atomic update
+        a = a + b 
+
+!CHECK: %[[val_c1:.*]] = arith.constant 1 : i32
+!CHECK: omp.atomic.update %[[val_Y_declare]]#1 : !fir.ref<i32> {
+!CHECK: ^bb0(%[[ARG:.*]]: i32):
+!CHECK: %[[temp:.*]] = arith.addi %[[ARG]], %[[val_c1]] : i32
+!CHECK: omp.yield(%[[temp]] : i32)
+!CHECK: }
+   !$omp atomic 
+        y = y + 1
+
+!CHECK: %[[val_X_loaded:.*]] = fir.load %[[val_X_declare]]#0 : !fir.ref<i32>
+!CHECK: omp.atomic.update %[[val_Z_declare]]#1 : !fir.ref<i32> {
+!CHECK: ^bb0(%[[ARG:.*]]: i32):
+!CHECK: %[[temp:.*]] = arith.muli %[[val_X_loaded]], %[[ARG]] : i32
+!CHECK: omp.yield(%[[temp]] : i32)
+!CHECK: }
+    !$omp atomic update
+        z = x * z 
+
+!CHECK: %[[val_c1:.*]] = arith.constant 1 : i32
+!CHECK: omp.atomic.update memory_order(relaxed) hint(uncontended) %[[val_X_declare]]#1 : !fir.ref<i32> {
+!CHECK: ^bb0(%[[ARG:.*]]: i32):
+!CHECK: %[[temp:.*]] = arith.subi %[[ARG]], %[[val_c1]] : i32
+!CHECK: omp.yield(%[[temp]] : i32)
+!CHECK: }
+   !$omp atomic relaxed update hint(omp_sync_hint_uncontended)
+        x = x - 1
+
+!CHECK: omp.atomic.update memory_order(relaxed) %[[val_Y_declare]]#1 : !fir.ref<i32> {
+!CHECK: ^bb0(%[[ARG:.*]]: i32):
+!CHECK:  %[[val_C_loaded:.*]] = fir.load %[[val_C_declare]]#0 : !fir.ref<i32>
+!CHECK:  %[[val_D_loaded:.*]] = fir.load %[[val_D_declare]]#0 : !fir.ref<i32>
+!CHECK:  {{.*}} = arith.cmpi sgt, %[[ARG]], {{.*}} : i32
+!CHECK:  {{.*}} = arith.select {{.*}}, %[[ARG]], {{.*}} : i32
+!CHECK:  {{.*}} = arith.cmpi sgt, {{.*}}
+!CHECK:  %[[temp:.*]] = arith.select {{.*}} : i32
+!CHECK:  omp.yield(%[[temp]] : i32)
+!CHECK: }
+    !$omp atomic update relaxed 
+        y = max(y, c, d)
+
+!CHECK: %[[val_X_loaded:.*]] = fir.load %[[val_X_declare]]#0 : !fir.ref<i32>
+!CHECK: omp.atomic.update memory_order(relaxed) hint(contended) %[[val_Z_declare]]#1 : !fir.ref<i32> {
+!CHECK: ^bb0(%[[ARG:.*]]: i32):
+!CHECK: %[[temp:.*]] = arith.addi %[[ARG]], %[[val_X_loaded]] : i32
+!CHECK: omp.yield(%[[temp]] : i32)
+!CHECK: }
+    !$omp atomic relaxed hint(omp_sync_hint_contended)
+        z = z + x
+
+!CHECK: %[[val_c10:.*]] = arith.constant 10 : i32
+!CHECK: omp.atomic.update memory_order(release) hint(contended) %[[val_Z_declare]]#1 : !fir.ref<i32> {
+!CHECK: ^bb0(%[[ARG:.*]]: i32):
+!CHECK: %[[temp:.*]] = arith.muli %[[val_c10]], %[[ARG]] : i32
+!CHECK: omp.yield(%[[temp]] : i32)
+!CHECK: }
+    !$omp atomic release update hint(omp_lock_hint_contended)
+        z = z * 10
+
+!CHECK: %[[val_Z_loaded:.*]] = fir.load %[[val_Z_declare]]#0 : !fir.ref<i32>
+!CHECK: omp.atomic.update memory_order(release) hint(speculative) %[[val_X_declare]]#1 : !fir.ref<i32> {
+!CHECK: ^bb0(%[[ARG:.*]]: i32):
+!CHECK: %[[temp:.*]] = arith.divsi %[[ARG]], %[[val_Z_loaded]] : i32
+!CHECK: omp.yield(%[[temp]] : i32)
+!CHECK: }
+    !$omp atomic hint(omp_lock_hint_speculative) update release
+        x = x / z
+
+!CHECK: %[[val_c1:.*]] = arith.constant 1 : i32
+!CHECK: omp.atomic.update %[[val_i1_declare]]#1 : !fir.ref<i8> {
+!CHECK: ^bb0(%[[ARG:.*]]: i8):
+!CHECK: %[[convert:.*]] = fir.convert %[[ARG]] : (i8) -> i32
+!CHECK: %[[add:.*]] = arith.addi %[[convert]], %[[val_c1]] : i32
+!CHECK: %[[temp:.*]] = fir.convert %[[add]] : (i32) -> i8
+!CHECK: omp.yield(%[[temp]] : i8)
+!CHECK: }
+   !$omp atomic
+      i1 = i1 + 1
+    !$omp end atomic
+end program OmpAtomicUpdate

``````````

</details>


https://github.com/llvm/llvm-project/pull/70627


More information about the flang-commits mailing list