[clang] [flang] [flang] Link to libatomic with openmp and rtlib=libgcc (PR #112202)
David Truby via cfe-commits
cfe-commits at lists.llvm.org
Tue Oct 15 06:19:33 PDT 2024
https://github.com/DavidTruby updated https://github.com/llvm/llvm-project/pull/112202
>From 1f039de4e91c052033bed2ecab1ac0fe101ffeea Mon Sep 17 00:00:00 2001
From: David Truby <david.truby at arm.com>
Date: Mon, 14 Oct 2024 14:39:44 +0100
Subject: [PATCH 1/2] [flang] Link to libatomic with openmp and rtlib=libgcc
Currently when using OpenMP atomics we depend on some symbols from
libatomic. These symbols are provided in a separate library for the
libgcc runtime, so we should link to that when rtlib=libgcc.
For the compiler-rt case, the presence and location of the symbols is
dependent on how compiler-rt itself was built so we cannot make that
decision for the user. As such no extra flags are added in that case.
---
clang/lib/Driver/ToolChains/CommonArgs.cpp | 10 ++++++++++
flang/test/Driver/atomic.f90 | 5 +++++
2 files changed, 15 insertions(+)
create mode 100644 flang/test/Driver/atomic.f90
diff --git a/clang/lib/Driver/ToolChains/CommonArgs.cpp b/clang/lib/Driver/ToolChains/CommonArgs.cpp
index 0c6a585c3acffd..7f9fc3559fcbd8 100644
--- a/clang/lib/Driver/ToolChains/CommonArgs.cpp
+++ b/clang/lib/Driver/ToolChains/CommonArgs.cpp
@@ -1294,6 +1294,16 @@ void tools::addFortranRuntimeLibs(const ToolChain &TC, const ArgList &Args,
CmdArgs.push_back("-lFortranRuntime");
CmdArgs.push_back("-lFortranDecimal");
}
+
+ // libomp needs libatomic for atomic operations if using libgcc
+ if (Args.hasFlag(options::OPT_fopenmp, options::OPT_fopenmp_EQ,
+ options::OPT_fno_openmp, false)) {
+ Driver::OpenMPRuntimeKind OMPRuntime =
+ TC.getDriver().getOpenMPRuntime(Args);
+ ToolChain::RuntimeLibType RuntimeLib = TC.GetRuntimeLibType(Args);
+ if (OMPRuntime == Driver::OMPRT_OMP && RuntimeLib == ToolChain::RLT_Libgcc)
+ CmdArgs.push_back("-latomic");
+ }
}
void tools::addFortranRuntimeLibraryPath(const ToolChain &TC,
diff --git a/flang/test/Driver/atomic.f90 b/flang/test/Driver/atomic.f90
new file mode 100644
index 00000000000000..4d3ba89be27580
--- /dev/null
+++ b/flang/test/Driver/atomic.f90
@@ -0,0 +1,5 @@
+!RUN: %flang -fopenmp -rtlib=libgcc -### %s 2>&1 | FileCheck --check-prefixes=GCC %s
+!RUN: %flang -fopenmp -rtlib=compiler-rt -### %s 2>&1 | FileCheck --check-prefixes=CRT %s
+
+!GCC: -latomic
+!CRT-NOT: -latomic
>From c4cb26690ec10175f0db32869a8beb5bb47f1edb Mon Sep 17 00:00:00 2001
From: David Truby <david.truby at arm.com>
Date: Tue, 15 Oct 2024 14:18:51 +0100
Subject: [PATCH 2/2] Specify target for tests
---
flang/test/Driver/atomic.f90 | 4 ++--
1 file changed, 2 insertions(+), 2 deletions(-)
diff --git a/flang/test/Driver/atomic.f90 b/flang/test/Driver/atomic.f90
index 4d3ba89be27580..dcae65ac807a12 100644
--- a/flang/test/Driver/atomic.f90
+++ b/flang/test/Driver/atomic.f90
@@ -1,5 +1,5 @@
-!RUN: %flang -fopenmp -rtlib=libgcc -### %s 2>&1 | FileCheck --check-prefixes=GCC %s
-!RUN: %flang -fopenmp -rtlib=compiler-rt -### %s 2>&1 | FileCheck --check-prefixes=CRT %s
+!RUN: %flang --target=aarch64-unknown-linux-gnu -fopenmp -rtlib=libgcc -### %s 2>&1 | FileCheck --check-prefixes=GCC %s
+!RUN: %flang --target=aarch64-unknown-linux-gnu -fopenmp -rtlib=compiler-rt -### %s 2>&1 | FileCheck --check-prefixes=CRT %s
!GCC: -latomic
!CRT-NOT: -latomic
More information about the cfe-commits
mailing list