Skip to content

Commit 1f039de

Browse files
committed
[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.
1 parent e692af8 commit 1f039de

File tree

2 files changed

+15
-0
lines changed

2 files changed

+15
-0
lines changed

clang/lib/Driver/ToolChains/CommonArgs.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1294,6 +1294,16 @@ void tools::addFortranRuntimeLibs(const ToolChain &TC, const ArgList &Args,
12941294
CmdArgs.push_back("-lFortranRuntime");
12951295
CmdArgs.push_back("-lFortranDecimal");
12961296
}
1297+
1298+
// libomp needs libatomic for atomic operations if using libgcc
1299+
if (Args.hasFlag(options::OPT_fopenmp, options::OPT_fopenmp_EQ,
1300+
options::OPT_fno_openmp, false)) {
1301+
Driver::OpenMPRuntimeKind OMPRuntime =
1302+
TC.getDriver().getOpenMPRuntime(Args);
1303+
ToolChain::RuntimeLibType RuntimeLib = TC.GetRuntimeLibType(Args);
1304+
if (OMPRuntime == Driver::OMPRT_OMP && RuntimeLib == ToolChain::RLT_Libgcc)
1305+
CmdArgs.push_back("-latomic");
1306+
}
12971307
}
12981308

12991309
void tools::addFortranRuntimeLibraryPath(const ToolChain &TC,

flang/test/Driver/atomic.f90

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
!RUN: %flang -fopenmp -rtlib=libgcc -### %s 2>&1 | FileCheck --check-prefixes=GCC %s
2+
!RUN: %flang -fopenmp -rtlib=compiler-rt -### %s 2>&1 | FileCheck --check-prefixes=CRT %s
3+
4+
!GCC: -latomic
5+
!CRT-NOT: -latomic

0 commit comments

Comments
 (0)