[flang-commits] [flang] [clang] [flang][Driver] Support -pthread in the frontend (PR #77360)

Tarun Prabhu via flang-commits flang-commits at lists.llvm.org
Thu Jan 11 11:40:42 PST 2024


https://github.com/tarunprabhu updated https://github.com/llvm/llvm-project/pull/77360

>From 187f91dcd9e602c944b089adfb243127c31de2ca Mon Sep 17 00:00:00 2001
From: Tarun Prabhu <tarun at lanl.gov>
Date: Mon, 8 Jan 2024 10:56:09 -0700
Subject: [PATCH 1/2] [flang][Driver] Support -pthread in the frontend

The -pthread option is supported by both clang and gfortran.
---
 clang/include/clang/Driver/Options.td    |  3 ++-
 flang/test/Driver/driver-help-hidden.f90 |  1 +
 flang/test/Driver/driver-help.f90        |  2 ++
 flang/test/Driver/pthread.f90            | 10 ++++++++++
 4 files changed, 15 insertions(+), 1 deletion(-)
 create mode 100644 flang/test/Driver/pthread.f90

diff --git a/clang/include/clang/Driver/Options.td b/clang/include/clang/Driver/Options.td
index 6aff37f1336871..3aaf3b58f78f4a 100644
--- a/clang/include/clang/Driver/Options.td
+++ b/clang/include/clang/Driver/Options.td
@@ -5303,7 +5303,8 @@ def pthreads : Flag<["-"], "pthreads">;
 defm pthread : BoolOption<"", "pthread",
   LangOpts<"POSIXThreads">, DefaultFalse,
   PosFlag<SetTrue, [], [ClangOption], "Support POSIX threads in generated code">,
-  NegFlag<SetFalse>, BothFlags<[], [ClangOption, CC1Option]>>;
+  NegFlag<SetFalse>,
+  BothFlags<[], [ClangOption, CC1Option, FlangOption, FC1Option]>>;
 def pie : Flag<["-"], "pie">, Group<Link_Group>;
 def static_pie : Flag<["-"], "static-pie">, Group<Link_Group>;
 def read__only__relocs : Separate<["-"], "read_only_relocs">;
diff --git a/flang/test/Driver/driver-help-hidden.f90 b/flang/test/Driver/driver-help-hidden.f90
index 9a11a7a571ffcc..39c607b80ddb98 100644
--- a/flang/test/Driver/driver-help-hidden.f90
+++ b/flang/test/Driver/driver-help-hidden.f90
@@ -134,6 +134,7 @@
 ! CHECK-NEXT: -pedantic               Warn on language extensions
 ! CHECK-NEXT: -print-effective-triple Print the effective target triple
 ! CHECK-NEXT: -print-target-triple    Print the normalized target triple
+! CHECK-NEXT: -pthread                Support POSIX threads in generated code
 ! CHECK-NEXT: -P                      Disable linemarker output in -E mode
 ! CHECK-NEXT: -Rpass-analysis=<value> Report transformation analysis from optimization passes whose name matches the given POSIX regular expression
 ! CHECK-NEXT: -Rpass-missed=<value>   Report missed transformations by optimization passes whose name matches the given POSIX regular expression
diff --git a/flang/test/Driver/driver-help.f90 b/flang/test/Driver/driver-help.f90
index e0e74dc56f331e..f00b3ba47e269a 100644
--- a/flang/test/Driver/driver-help.f90
+++ b/flang/test/Driver/driver-help.f90
@@ -120,6 +120,7 @@
 ! HELP-NEXT: -pedantic               Warn on language extensions
 ! HELP-NEXT: -print-effective-triple Print the effective target triple
 ! HELP-NEXT: -print-target-triple    Print the normalized target triple
+! HELP-NEXT: -pthread                Support POSIX threads in generated code
 ! HELP-NEXT: -P                      Disable linemarker output in -E mode
 ! HELP-NEXT: -Rpass-analysis=<value> Report transformation analysis from optimization passes whose name matches the given POSIX regular expression
 ! HELP-NEXT: -Rpass-missed=<value>   Report missed transformations by optimization passes whose name matches the given POSIX regular expression
@@ -258,6 +259,7 @@
 ! HELP-FC1-NEXT: -pic-is-pie             File is for a position independent executable
 ! HELP-FC1-NEXT: -pic-level <value>      Value for __PIC__
 ! HELP-FC1-NEXT: -plugin <name>          Use the named plugin action instead of the default action (use "help" to list available options)
+! HELP-FC1-NEXT: -pthread                Support POSIX threads in generated code
 ! HELP-FC1-NEXT: -P                      Disable linemarker output in -E mode
 ! HELP-FC1-NEXT: -Rpass-analysis=<value> Report transformation analysis from optimization passes whose name matches the given POSIX regular expression
 ! HELP-FC1-NEXT: -Rpass-missed=<value>   Report missed transformations by optimization passes whose name matches the given POSIX regular expression
diff --git a/flang/test/Driver/pthread.f90 b/flang/test/Driver/pthread.f90
new file mode 100644
index 00000000000000..e3fd392ed91aff
--- /dev/null
+++ b/flang/test/Driver/pthread.f90
@@ -0,0 +1,10 @@
+! RUN: %flang -### -pthread /dev/null -o /dev/null 2>&1 | FileCheck %s
+! RUN: %flang -### -Xflang -pthread /dev/null -o /dev/null 2>&1 | FileCheck %s
+
+! How the -pthread flag is handled is very platform-specific. A lot of that
+! functionality is tested by clang, and the flag itself is handled by clang's
+! driver that flang also uses. Instead of duplicating all that testing here,
+! just check that the presence of the flag does not raise an error. If we need
+! more customized handling of -pthread, the tests for that can be added here.
+!
+! CHECK-NOT: error:

>From ae2e23569616f4cb13b4eaec902803650f0fc1b4 Mon Sep 17 00:00:00 2001
From: Tarun Prabhu <tarun.prabhu at gmail.com>
Date: Thu, 11 Jan 2024 12:40:30 -0700
Subject: [PATCH 2/2] Add comments to the test explaining why it is difficult
 to write a failing test for the -pthread option.

---
 flang/test/Driver/pthread.f90 | 12 ++++++++++++
 1 file changed, 12 insertions(+)

diff --git a/flang/test/Driver/pthread.f90 b/flang/test/Driver/pthread.f90
index e3fd392ed91aff..6201f4d9f9c217 100644
--- a/flang/test/Driver/pthread.f90
+++ b/flang/test/Driver/pthread.f90
@@ -1,3 +1,15 @@
+! In release 2.34, glibc removed libpthread as a separate library. All the
+! pthread_* functions were subsumed into libc, so linking that is sufficient.
+! However, when linking against older glibc builds, the explicit link of
+! -pthread will be required. More details are here:
+!
+! https://developers.redhat.com/articles/2021/12/17/why-glibc-234-removed-libpthread#the_developer_view
+!
+! This makes it difficult to write a test that requires the -pthread flag in
+! order to pass. Checking for the presence of -lpthread in the linker flags is
+! not reliable since the linker could just skip the flag altogether if it is
+! linking against a new libc implementation.
+
 ! RUN: %flang -### -pthread /dev/null -o /dev/null 2>&1 | FileCheck %s
 ! RUN: %flang -### -Xflang -pthread /dev/null -o /dev/null 2>&1 | FileCheck %s
 



More information about the flang-commits mailing list