[flang-commits] [flang] [openmp] [OpenMP] [Flang] Resolved Issue llvm#76121: Implemented Check for Unhandled Arguments in __kmpc_fork_call_if (PR #82221)

chandan singh via flang-commits flang-commits at lists.llvm.org
Thu Apr 11 23:14:14 PDT 2024


https://github.com/chandankds updated https://github.com/llvm/llvm-project/pull/82221

>From 9b1ea42bd38b4ede10460bef569e80f30a73d115 Mon Sep 17 00:00:00 2001
From: chandan singh <chandankds at gmail.com>
Date: Mon, 19 Feb 2024 12:21:37 +0530
Subject: [PATCH 1/5] [OpenMP] [Flang] Fix Issue llvm#76121

Root cause: Segmentation fault is caused by null pointer dereference inside the __kmpc_fork_call_if function at https://github.com/llvm/llvm-project/blob/main/openmp/runtime/src/z_Linux_asm.S#L1186 . __kmpc_fork_call_if is missing case to handle argc=0 .

Fix: Added a check inside the __kmp_invoke_microtask function to handle the case when argc is 0.
---
 openmp/runtime/src/z_Linux_asm.S | 4 ++++
 1 file changed, 4 insertions(+)

diff --git a/openmp/runtime/src/z_Linux_asm.S b/openmp/runtime/src/z_Linux_asm.S
index 14987c298fa5f9..b44d9fb316a05c 100644
--- a/openmp/runtime/src/z_Linux_asm.S
+++ b/openmp/runtime/src/z_Linux_asm.S
@@ -1150,6 +1150,9 @@ KMP_LABEL(kmp_invoke_pass_parms):	// put 1st - 6th parms to pkfn in registers.
 	movq	%rdi, %rbx	// pkfn -> %rbx
 	leaq	__gtid(%rbp), %rdi // &gtid -> %rdi (store 1st parm to pkfn)
 	leaq	__tid(%rbp), %rsi  // &tid -> %rsi (store 2nd parm to pkfn)
+	// Check if argc is 0
+	cmpq $0, %rax
+	je KMP_LABEL(kmp_no_args) // Jump ahead
 
 	movq	%r8, %r11	// p_argv -> %r11
 
@@ -1195,6 +1198,7 @@ KMP_LABEL(kmp_1_exit):
 	cmovnsq	(%r11), %rdx	// p_argv[0] -> %rdx (store 3rd parm to pkfn)
 #endif // KMP_MIC
 
+	KMP_LABEL(kmp_no_args):
 	call	*%rbx		// call (*pkfn)();
 	movq	$1, %rax	// move 1 into return register;
 

>From 4f4fae3f9d4e28602461c0ffe882d1ed84002e39 Mon Sep 17 00:00:00 2001
From: Singh <chasingh at amd.com>
Date: Sun, 3 Mar 2024 08:21:21 +0530
Subject: [PATCH 2/5] Create parallel_do_if_false.f90

---
 flang/test/Runtime/parallel_do_if_false.f90 | 27 +++++++++++++++++++++
 1 file changed, 27 insertions(+)
 create mode 100644 flang/test/Runtime/parallel_do_if_false.f90

diff --git a/flang/test/Runtime/parallel_do_if_false.f90 b/flang/test/Runtime/parallel_do_if_false.f90
new file mode 100644
index 00000000000000..f15a697eb5868d
--- /dev/null
+++ b/flang/test/Runtime/parallel_do_if_false.f90
@@ -0,0 +1,27 @@
+!RUN: %flang %s -o %t -fopenmp  && %t | FileCheck %s
+PROGRAM parallel_do_if
+
+        USE OMP_LIB
+
+        INTEGER :: I
+
+        INTEGER :: N = 0
+
+        INTEGER, PARAMETER :: NI = 1
+
+        !$OMP PARALLEL DO IF(.false.)
+
+        DO I = 1, NI
+
+        IF (omp_in_parallel() .EQV. .false.) THEN               
+                !CHECK: PASS
+                PRINT *, "PASS"
+        ELSE
+                PRINT *, "FAIL"
+
+        END IF
+
+        END DO
+        !$OMP END PARALLEL DO
+
+END PROGRAM

>From 22f085cb718cbede39d1e3e05fafb22ceafb40b9 Mon Sep 17 00:00:00 2001
From: chandan singh <chandankds at gmail.com>
Date: Mon, 11 Mar 2024 11:13:33 +0530
Subject: [PATCH 3/5] Added test for issue llvm#76121

---
 openmp/runtime/test/lit.site.cfg.in                              | 1 +
 .../runtime/test/parallel}/parallel_do_if_false.f90              | 0
 2 files changed, 1 insertion(+)
 rename {flang/test/Runtime => openmp/runtime/test/parallel}/parallel_do_if_false.f90 (100%)

diff --git a/openmp/runtime/test/lit.site.cfg.in b/openmp/runtime/test/lit.site.cfg.in
index d6c259280619be..c6d623ebd072c3 100644
--- a/openmp/runtime/test/lit.site.cfg.in
+++ b/openmp/runtime/test/lit.site.cfg.in
@@ -2,6 +2,7 @@
 
 config.test_c_compiler = "@OPENMP_TEST_C_COMPILER@"
 config.test_cxx_compiler = "@OPENMP_TEST_CXX_COMPILER@"
+config.test_fortran_compiler="@OPENMP_TEST_Fortran_COMPILER@"
 config.test_compiler_features = @OPENMP_TEST_COMPILER_FEATURES@
 config.test_compiler_has_omp_h = @OPENMP_TEST_COMPILER_HAS_OMP_H@
 config.test_filecheck = "@OPENMP_FILECHECK_EXECUTABLE@"
diff --git a/flang/test/Runtime/parallel_do_if_false.f90 b/openmp/runtime/test/parallel/parallel_do_if_false.f90
similarity index 100%
rename from flang/test/Runtime/parallel_do_if_false.f90
rename to openmp/runtime/test/parallel/parallel_do_if_false.f90

>From 2c4b38bb9669a446034eb60b4e5ba2b8565ba674 Mon Sep 17 00:00:00 2001
From: chandan singh <chandankds at gmail.com>
Date: Fri, 12 Apr 2024 11:26:05 +0530
Subject: [PATCH 4/5] Fix Issue llvm#76121

Added testcase
---
 openmp/runtime/test/lit.site.cfg.in           |  1 -
 .../test/misc_bugs/omp__kmpc_fork_call_if.c   | 24 +++++++++++++++++
 .../test/parallel/parallel_do_if_false.f90    | 27 -------------------
 3 files changed, 24 insertions(+), 28 deletions(-)
 create mode 100644 openmp/runtime/test/misc_bugs/omp__kmpc_fork_call_if.c
 delete mode 100644 openmp/runtime/test/parallel/parallel_do_if_false.f90

diff --git a/openmp/runtime/test/lit.site.cfg.in b/openmp/runtime/test/lit.site.cfg.in
index c6d623ebd072c3..d6c259280619be 100644
--- a/openmp/runtime/test/lit.site.cfg.in
+++ b/openmp/runtime/test/lit.site.cfg.in
@@ -2,7 +2,6 @@
 
 config.test_c_compiler = "@OPENMP_TEST_C_COMPILER@"
 config.test_cxx_compiler = "@OPENMP_TEST_CXX_COMPILER@"
-config.test_fortran_compiler="@OPENMP_TEST_Fortran_COMPILER@"
 config.test_compiler_features = @OPENMP_TEST_COMPILER_FEATURES@
 config.test_compiler_has_omp_h = @OPENMP_TEST_COMPILER_HAS_OMP_H@
 config.test_filecheck = "@OPENMP_FILECHECK_EXECUTABLE@"
diff --git a/openmp/runtime/test/misc_bugs/omp__kmpc_fork_call_if.c b/openmp/runtime/test/misc_bugs/omp__kmpc_fork_call_if.c
new file mode 100644
index 00000000000000..de260385670b4b
--- /dev/null
+++ b/openmp/runtime/test/misc_bugs/omp__kmpc_fork_call_if.c
@@ -0,0 +1,24 @@
+// RUN: %libomp-compile  -Wno-implicit-function-declaration && %t | FileCheck %s
+
+#include <stdio.h>
+#include <omp.h>
+
+
+// Microtask function for parallel region
+void microtask(int* global_tid, int* bound_tid) {
+// CHECK: PASS
+    if (omp_in_parallel()) {
+        printf("FAIL\n");
+    } else {
+        printf("PASS\n");
+    }
+}
+
+int main() {
+    // Condition for parallelization (false in this case)
+    int cond = 0;
+
+    // Call __kmpc_fork_call_if
+    __kmpc_fork_call_if(NULL, 0, microtask, cond, NULL);
+    return 0;
+}
\ No newline at end of file
diff --git a/openmp/runtime/test/parallel/parallel_do_if_false.f90 b/openmp/runtime/test/parallel/parallel_do_if_false.f90
deleted file mode 100644
index f15a697eb5868d..00000000000000
--- a/openmp/runtime/test/parallel/parallel_do_if_false.f90
+++ /dev/null
@@ -1,27 +0,0 @@
-!RUN: %flang %s -o %t -fopenmp  && %t | FileCheck %s
-PROGRAM parallel_do_if
-
-        USE OMP_LIB
-
-        INTEGER :: I
-
-        INTEGER :: N = 0
-
-        INTEGER, PARAMETER :: NI = 1
-
-        !$OMP PARALLEL DO IF(.false.)
-
-        DO I = 1, NI
-
-        IF (omp_in_parallel() .EQV. .false.) THEN               
-                !CHECK: PASS
-                PRINT *, "PASS"
-        ELSE
-                PRINT *, "FAIL"
-
-        END IF
-
-        END DO
-        !$OMP END PARALLEL DO
-
-END PROGRAM

>From a67dd1a14e5510b7aba9e75582c271d59065bbf8 Mon Sep 17 00:00:00 2001
From: chandan singh <chandankds at gmail.com>
Date: Fri, 12 Apr 2024 11:43:06 +0530
Subject: [PATCH 5/5] Update omp__kmpc_fork_call_if.c

fixed formatting
---
 .../test/misc_bugs/omp__kmpc_fork_call_if.c   | 26 +++++++++----------
 1 file changed, 12 insertions(+), 14 deletions(-)

diff --git a/openmp/runtime/test/misc_bugs/omp__kmpc_fork_call_if.c b/openmp/runtime/test/misc_bugs/omp__kmpc_fork_call_if.c
index de260385670b4b..c91c9e8b34a508 100644
--- a/openmp/runtime/test/misc_bugs/omp__kmpc_fork_call_if.c
+++ b/openmp/runtime/test/misc_bugs/omp__kmpc_fork_call_if.c
@@ -3,22 +3,20 @@
 #include <stdio.h>
 #include <omp.h>
 
-
 // Microtask function for parallel region
-void microtask(int* global_tid, int* bound_tid) {
-// CHECK: PASS
-    if (omp_in_parallel()) {
-        printf("FAIL\n");
-    } else {
-        printf("PASS\n");
-    }
+void microtask(int *global_tid, int *bound_tid) {
+  // CHECK: PASS
+  if (omp_in_parallel()) {
+    printf("FAIL\n");
+  } else {
+    printf("PASS\n");
+  }
 }
 
 int main() {
-    // Condition for parallelization (false in this case)
-    int cond = 0;
-
-    // Call __kmpc_fork_call_if
-    __kmpc_fork_call_if(NULL, 0, microtask, cond, NULL);
-    return 0;
+  // Condition for parallelization (false in this case)
+  int cond = 0;
+  // Call __kmpc_fork_call_if
+  __kmpc_fork_call_if(NULL, 0, microtask, cond, NULL);
+  return 0;
 }
\ No newline at end of file



More information about the flang-commits mailing list