Skip to content

[flang] Add -rtlib flag#99058

Merged
DavidTruby merged 3 commits into
llvm:mainfrom
DavidTruby:flang-rtlib
Jul 17, 2024
Merged

[flang] Add -rtlib flag#99058
DavidTruby merged 3 commits into
llvm:mainfrom
DavidTruby:flang-rtlib

Conversation

@DavidTruby

Copy link
Copy Markdown
Member

This patch allows the -rtlib flag with flang-new to select between the
libgcc_s and compiler-rt runtimes. The behaviour is identical to the
same flag with clang.

This patch allows the -rtlib flag with flang-new to select between the
libgcc_s and compiler-rt runtimes. The behaviour is identical to the
same flag with clang.
@llvmbot llvmbot added clang Clang issues not falling into any other category flang:driver flang Flang issues not falling into any other category labels Jul 16, 2024
@llvmbot

llvmbot commented Jul 16, 2024

Copy link
Copy Markdown
Member

@llvm/pr-subscribers-clang

@llvm/pr-subscribers-flang-driver

Author: David Truby (DavidTruby)

Changes

This patch allows the -rtlib flag with flang-new to select between the
libgcc_s and compiler-rt runtimes. The behaviour is identical to the
same flag with clang.


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

2 Files Affected:

  • (modified) clang/include/clang/Driver/Options.td (+1-1)
  • (modified) flang/test/Driver/linker-flags.f90 (+6)
diff --git a/clang/include/clang/Driver/Options.td b/clang/include/clang/Driver/Options.td
index 234958f4eb382..c434fe64b3f53 100644
--- a/clang/include/clang/Driver/Options.td
+++ b/clang/include/clang/Driver/Options.td
@@ -5621,7 +5621,7 @@ def resource_dir_EQ : Joined<["-"], "resource-dir=">, Flags<[NoXarchOption]>,
   Alias<resource_dir>;
 def rpath : Separate<["-"], "rpath">, Flags<[LinkerInput]>, Group<Link_Group>,
   Visibility<[ClangOption, CLOption, DXCOption, FlangOption]>;
-def rtlib_EQ : Joined<["-", "--"], "rtlib=">, Visibility<[ClangOption, CLOption]>,
+def rtlib_EQ : Joined<["-", "--"], "rtlib=">, Visibility<[ClangOption, CLOption, FlangOption]>,
   HelpText<"Compiler runtime library to use">;
 def frtlib_add_rpath: Flag<["-"], "frtlib-add-rpath">, Flags<[NoArgumentUnused]>,
   Visibility<[ClangOption, FlangOption]>,
diff --git a/flang/test/Driver/linker-flags.f90 b/flang/test/Driver/linker-flags.f90
index 02e217494f818..25f1d6dcd8786 100644
--- a/flang/test/Driver/linker-flags.f90
+++ b/flang/test/Driver/linker-flags.f90
@@ -11,6 +11,7 @@
 ! RUN: %flang -### --target=x86_64-unknown-dragonfly %S/Inputs/hello.f90 2>&1 | FileCheck %s --check-prefixes=CHECK,UNIX,UNIX-F128%f128-lib
 ! RUN: %flang -### --target=x86_64-unknown-haiku %S/Inputs/hello.f90 2>&1 | FileCheck %s --check-prefixes=CHECK,HAIKU,HAIKU-F128%f128-lib
 ! RUN: %flang -### --target=x86_64-windows-gnu %S/Inputs/hello.f90 2>&1 | FileCheck %s --check-prefixes=CHECK,MINGW,MINGW-F128%f128-lib
+! RUN: %flang -### -rtlib=compiler-rt --target=aarch64-linux-gnu %S/Inputs/hello.f90 2>&1 | FileCheck %s --check-prefixes=CHECK,UNIX,COMPILER-RT
 
 ! NOTE: Clang's driver library, clangDriver, usually adds 'oldnames' on Windows,
 !       but it is not needed when compiling Fortran code and they might bring in
@@ -27,12 +28,14 @@
 !       suffix. Clang's driver will try to resolve the path to the ld
 !       executable and may find the GNU linker from MinGW or Cygwin.
 ! UNIX-LABEL:  "{{.*}}ld{{(\.exe)?}}"
+! COMPILER-RT: "{{.*}}/clang_rt.crtbegin.o" 
 ! UNIX-SAME: "[[object_file]]"
 ! UNIX-F128NONE-NOT: FortranFloat128Math
 ! SOLARIS-F128NONE-NOT: FortranFloat128Math
 ! UNIX-F128LIBQUADMATH-SAME: "-lFortranFloat128Math" "--as-needed" "-lquadmath" "--no-as-needed"
 ! SOLARIS-F128LIBQUADMATH-SAME: "-lFortranFloat128Math" "-z" "ignore" "-lquadmath" "-z" "record"
 ! UNIX-SAME: "-lFortranRuntime" "-lFortranDecimal" "-lm"
+! COMPILER-RT: "{{.*}}/libclang_rt.builtins.a" "{{.*}}/clang_rt.crtend.o"
 
 ! DARWIN-LABEL:  "{{.*}}ld{{(\.exe)?}}"
 ! DARWIN-SAME: "[[object_file]]"
@@ -61,3 +64,6 @@
 ! MSVC-LABEL: link
 ! MSVC-SAME: /subsystem:console
 ! MSVC-SAME: "[[object_file]]"
+
+! COMPILER-RT-NOT: "-lgcc"
+! COMPILER-RT-NOT: "-lgcc_s"

@tblah tblah left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, thanks!

@vzakhari vzakhari left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

@DavidTruby

Copy link
Copy Markdown
Member Author

I don't know why the pre-commit build failed on Windows. The build itself looks fine but seems to be complaining about slow tests?

@ceseo

ceseo commented Jul 16, 2024

Copy link
Copy Markdown
Contributor

I don't know why the pre-commit build failed on Windows. The build itself looks fine but seems to be complaining about slow tests?

Actually, the error is here:

FAIL: Flang :: Driver/linker-flags.f90 (218 of 2807)
******************** TEST 'Flang :: Driver/linker-flags.f90' FAILED ********************
Exit Code: 1
Command Output (stdout):
--
# RUN: at line 5
c:\ws\src\build\bin\flang-new.exe -### --target=ppc64le-linux-gnu C:\ws\src\flang\test\Driver/Inputs/hello.f90 2>&1 | c:\ws\src\build\bin\filecheck.exe C:\ws\src\flang\test\Driver\linker-flags.f90 --check-prefixes=CHECK,UNIX,UNIX-F128NONE
# executed command: 'c:\ws\src\build\bin\flang-new.exe' '-###' --target=ppc64le-linux-gnu 'C:\ws\src\flang\test\Driver/Inputs/hello.f90'
# note: command had no output on stdout or stderr
# executed command: 'c:\ws\src\build\bin\filecheck.exe' 'C:\ws\src\flang\test\Driver\linker-flags.f90' --check-prefixes=CHECK,UNIX,UNIX-F128NONE
# note: command had no output on stdout or stderr
# RUN: at line 6
c:\ws\src\build\bin\flang-new.exe -### --target=aarch64-apple-darwin C:\ws\src\flang\test\Driver/Inputs/hello.f90 2>&1 | c:\ws\src\build\bin\filecheck.exe C:\ws\src\flang\test\Driver\linker-flags.f90 --check-prefixes=CHECK,DARWIN,DARWIN-F128NONE
# executed command: 'c:\ws\src\build\bin\flang-new.exe' '-###' --target=aarch64-apple-darwin 'C:\ws\src\flang\test\Driver/Inputs/hello.f90'
# note: command had no output on stdout or stderr
# executed command: 'c:\ws\src\build\bin\filecheck.exe' 'C:\ws\src\flang\test\Driver\linker-flags.f90' --check-prefixes=CHECK,DARWIN,DARWIN-F128NONE
# note: command had no output on stdout or stderr
# RUN: at line 7
c:\ws\src\build\bin\flang-new.exe -### --target=sparc-sun-solaris2.11 C:\ws\src\flang\test\Driver/Inputs/hello.f90 2>&1 | c:\ws\src\build\bin\filecheck.exe C:\ws\src\flang\test\Driver\linker-flags.f90 --check-prefixes=CHECK,UNIX,SOLARIS-F128NONE
# executed command: 'c:\ws\src\build\bin\flang-new.exe' '-###' --target=sparc-sun-solaris2.11 'C:\ws\src\flang\test\Driver/Inputs/hello.f90'
# note: command had no output on stdout or stderr
# executed command: 'c:\ws\src\build\bin\filecheck.exe' 'C:\ws\src\flang\test\Driver\linker-flags.f90' --check-prefixes=CHECK,UNIX,SOLARIS-F128NONE
# note: command had no output on stdout or stderr
# RUN: at line 8
c:\ws\src\build\bin\flang-new.exe -### --target=x86_64-unknown-freebsd C:\ws\src\flang\test\Driver/Inputs/hello.f90 2>&1 | c:\ws\src\build\bin\filecheck.exe C:\ws\src\flang\test\Driver\linker-flags.f90 --check-prefixes=CHECK,UNIX,UNIX-F128NONE
# executed command: 'c:\ws\src\build\bin\flang-new.exe' '-###' --target=x86_64-unknown-freebsd 'C:\ws\src\flang\test\Driver/Inputs/hello.f90'
# note: command had no output on stdout or stderr
# executed command: 'c:\ws\src\build\bin\filecheck.exe' 'C:\ws\src\flang\test\Driver\linker-flags.f90' --check-prefixes=CHECK,UNIX,UNIX-F128NONE
# note: command had no output on stdout or stderr
# RUN: at line 9
c:\ws\src\build\bin\flang-new.exe -### --target=x86_64-unknown-netbsd C:\ws\src\flang\test\Driver/Inputs/hello.f90 2>&1 | c:\ws\src\build\bin\filecheck.exe C:\ws\src\flang\test\Driver\linker-flags.f90 --check-prefixes=CHECK,UNIX,UNIX-F128NONE
# executed command: 'c:\ws\src\build\bin\flang-new.exe' '-###' --target=x86_64-unknown-netbsd 'C:\ws\src\flang\test\Driver/Inputs/hello.f90'
# note: command had no output on stdout or stderr
# executed command: 'c:\ws\src\build\bin\filecheck.exe' 'C:\ws\src\flang\test\Driver\linker-flags.f90' --check-prefixes=CHECK,UNIX,UNIX-F128NONE
# note: command had no output on stdout or stderr
# RUN: at line 10
c:\ws\src\build\bin\flang-new.exe -### --target=x86_64-unknown-openbsd C:\ws\src\flang\test\Driver/Inputs/hello.f90 2>&1 | c:\ws\src\build\bin\filecheck.exe C:\ws\src\flang\test\Driver\linker-flags.f90 --check-prefixes=CHECK,UNIX,UNIX-F128NONE
# executed command: 'c:\ws\src\build\bin\flang-new.exe' '-###' --target=x86_64-unknown-openbsd 'C:\ws\src\flang\test\Driver/Inputs/hello.f90'
# note: command had no output on stdout or stderr
# executed command: 'c:\ws\src\build\bin\filecheck.exe' 'C:\ws\src\flang\test\Driver\linker-flags.f90' --check-prefixes=CHECK,UNIX,UNIX-F128NONE
# note: command had no output on stdout or stderr
# RUN: at line 11
c:\ws\src\build\bin\flang-new.exe -### --target=x86_64-unknown-dragonfly C:\ws\src\flang\test\Driver/Inputs/hello.f90 2>&1 | c:\ws\src\build\bin\filecheck.exe C:\ws\src\flang\test\Driver\linker-flags.f90 --check-prefixes=CHECK,UNIX,UNIX-F128NONE
# executed command: 'c:\ws\src\build\bin\flang-new.exe' '-###' --target=x86_64-unknown-dragonfly 'C:\ws\src\flang\test\Driver/Inputs/hello.f90'
# note: command had no output on stdout or stderr
# executed command: 'c:\ws\src\build\bin\filecheck.exe' 'C:\ws\src\flang\test\Driver\linker-flags.f90' --check-prefixes=CHECK,UNIX,UNIX-F128NONE
# note: command had no output on stdout or stderr
# RUN: at line 12
c:\ws\src\build\bin\flang-new.exe -### --target=x86_64-unknown-haiku C:\ws\src\flang\test\Driver/Inputs/hello.f90 2>&1 | c:\ws\src\build\bin\filecheck.exe C:\ws\src\flang\test\Driver\linker-flags.f90 --check-prefixes=CHECK,HAIKU,HAIKU-F128NONE
# executed command: 'c:\ws\src\build\bin\flang-new.exe' '-###' --target=x86_64-unknown-haiku 'C:\ws\src\flang\test\Driver/Inputs/hello.f90'
# note: command had no output on stdout or stderr
# executed command: 'c:\ws\src\build\bin\filecheck.exe' 'C:\ws\src\flang\test\Driver\linker-flags.f90' --check-prefixes=CHECK,HAIKU,HAIKU-F128NONE
# note: command had no output on stdout or stderr
# RUN: at line 13
c:\ws\src\build\bin\flang-new.exe -### --target=x86_64-windows-gnu C:\ws\src\flang\test\Driver/Inputs/hello.f90 2>&1 | c:\ws\src\build\bin\filecheck.exe C:\ws\src\flang\test\Driver\linker-flags.f90 --check-prefixes=CHECK,MINGW,MINGW-F128NONE
# executed command: 'c:\ws\src\build\bin\flang-new.exe' '-###' --target=x86_64-windows-gnu 'C:\ws\src\flang\test\Driver/Inputs/hello.f90'
# note: command had no output on stdout or stderr
# executed command: 'c:\ws\src\build\bin\filecheck.exe' 'C:\ws\src\flang\test\Driver\linker-flags.f90' --check-prefixes=CHECK,MINGW,MINGW-F128NONE
# note: command had no output on stdout or stderr
# RUN: at line 14
c:\ws\src\build\bin\flang-new.exe -### -rtlib=compiler-rt --target=aarch64-linux-gnu C:\ws\src\flang\test\Driver/Inputs/hello.f90 2>&1 | c:\ws\src\build\bin\filecheck.exe C:\ws\src\flang\test\Driver\linker-flags.f90 --check-prefixes=CHECK,UNIX,COMPILER-RT
# executed command: 'c:\ws\src\build\bin\flang-new.exe' '-###' -rtlib=compiler-rt --target=aarch64-linux-gnu 'C:\ws\src\flang\test\Driver/Inputs/hello.f90'
# note: command had no output on stdout or stderr
# executed command: 'c:\ws\src\build\bin\filecheck.exe' 'C:\ws\src\flang\test\Driver\linker-flags.f90' --check-prefixes=CHECK,UNIX,COMPILER-RT
# .---command stderr------------
# | C:\ws\src\flang\test\Driver\linker-flags.f90:31:16: error: COMPILER-RT: expected string not found in input
# | ! COMPILER-RT: "{{.*}}/clang_rt.crtbegin.o"
# |                ^
# | <stdin>:7:27: note: scanning from here
# |  "c:\\llvm-mingw\\bin\\ld" "-EL" "--hash-style=gnu" "--eh-frame-hdr" "-m" "aarch64linux" "-pie" "-dynamic-linker" "/lib/ld-linux-aarch64.so.1" "-o" "a.out" "Scrt1.o" "crti.o" "crtbeginS.o" "C:\\Users\\ContainerAdministrator\\AppData\\Local\\Temp\\lit-tmp-9wgczl7q\\hello-1c57c7.o" "-LC:\\ws\\src\\build\\lib" "-lFortranRuntime" "-lFortranDecimal" "-lm" "C:\\ws\\src\\build\\lib\\clang\\19\\lib\\aarch64-unknown-linux-gnu\\libclang_rt.builtins.a" "-lc" "C:\\ws\\src\\build\\lib\\clang\\19\\lib\\aarch64-unknown-linux-gnu\\libclang_rt.builtins.a" "crtendS.o" "crtn.o"
# |                           ^
# | <stdin>:7:163: note: possible intended match here
# |  "c:\\llvm-mingw\\bin\\ld" "-EL" "--hash-style=gnu" "--eh-frame-hdr" "-m" "aarch64linux" "-pie" "-dynamic-linker" "/lib/ld-linux-aarch64.so.1" "-o" "a.out" "Scrt1.o" "crti.o" "crtbeginS.o" "C:\\Users\\ContainerAdministrator\\AppData\\Local\\Temp\\lit-tmp-9wgczl7q\\hello-1c57c7.o" "-LC:\\ws\\src\\build\\lib" "-lFortranRuntime" "-lFortranDecimal" "-lm" "C:\\ws\\src\\build\\lib\\clang\\19\\lib\\aarch64-unknown-linux-gnu\\libclang_rt.builtins.a" "-lc" "C:\\ws\\src\\build\\lib\\clang\\19\\lib\\aarch64-unknown-linux-gnu\\libclang_rt.builtins.a" "crtendS.o" "crtn.o"
# |                                                                                                                                                                   ^
# |
# | Input file: <stdin>
# | Check file: C:\ws\src\flang\test\Driver\linker-flags.f90
# |
# | -dump-input=help explains the following input dump.
# |
# | Input was:
# | <<<<<<
# |             1: flang-new version 19.0.0git (https://github.com/llvm/llvm-project.git 66e13f92a0680742b552fabde25df7752f8510c9)
# |             2: Target: aarch64-unknown-linux-gnu
# |             3: Thread model: posix
# |             4: InstalledDir: C:\ws\src\build\bin
# |             5: Build config: +assertions
# |             6:  "C:\\ws\\src\\build\\bin\\flang-new" "-fc1" "-triple" "aarch64-unknown-linux-gnu" "-emit-obj" "-mrelocation-model" "pic" "-pic-level" "2" "-pic-is-pie" "-target-cpu" "generic" "-target-feature" "+outline-atomics" "-target-feature" "+v8a" "-target-feature" "+fp-armv8" "-target-feature" "+neon" "-resource-dir" "C:\\ws\\src\\build\\lib\\clang\\19" "-mframe-pointer=non-leaf" "-o" "C:\\Users\\ContainerAdministrator\\AppData\\Local\\Temp\\lit-tmp-9wgczl7q\\hello-1c57c7.o" "-x" "f95-cpp-input" "C:\\ws\\src\\flang\\test\\Driver/Inputs/hello.f90"
# |             7:  "c:\\llvm-mingw\\bin\\ld" "-EL" "--hash-style=gnu" "--eh-frame-hdr" "-m" "aarch64linux" "-pie" "-dynamic-linker" "/lib/ld-linux-aarch64.so.1" "-o" "a.out" "Scrt1.o" "crti.o" "crtbeginS.o" "C:\\Users\\ContainerAdministrator\\AppData\\Local\\Temp\\lit-tmp-9wgczl7q\\hello-1c57c7.o" "-LC:\\ws\\src\\build\\lib" "-lFortranRuntime" "-lFortranDecimal" "-lm" "C:\\ws\\src\\build\\lib\\clang\\19\\lib\\aarch64-unknown-linux-gnu\\libclang_rt.builtins.a" "-lc" "C:\\ws\\src\\build\\lib\\clang\\19\\lib\\aarch64-unknown-linux-gnu\\libclang_rt.builtins.a" "crtendS.o" "crtn.o"
# | check:31'0                               X~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ error: no match found
# | check:31'1                                                                                                                                                                       ?                                                                                                                                                                                                                                                                                                                                                                                                                    possible intended match
# | >>>>>>
# `-----------------------------
# error: command failed with exit status: 1
--

@ceseo ceseo left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

linker-flags test seems to be failing on Windows.

@kkwli

kkwli commented Jul 16, 2024

Copy link
Copy Markdown
Contributor

The linker-flags test fails on AIX too.

@DavidTruby

Copy link
Copy Markdown
Member Author

It looks like clang_rt.crtbegin and clang_rt.crtend aren't always used on every platform so I've just removed the check for those. The check for libclang_rt.builtins and NOT libgcc and libgcc_s should be enough to check we are linking the right thing.

@ceseo ceseo left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, this should be enough.

@DavidTruby

Copy link
Copy Markdown
Member Author

Linux builds appear to have stopped, but all the previous linux builds worked so I think this is fine to merge. I will revert if it breaks post-commit.

@DavidTruby DavidTruby merged commit 7647174 into llvm:main Jul 17, 2024
yuxuanchen1997 pushed a commit that referenced this pull request Jul 25, 2024
Summary:
This patch allows the -rtlib flag with flang-new to select between the
libgcc_s and compiler-rt runtimes. The behaviour is identical to the
same flag with clang.

Test Plan: 

Reviewers: 

Subscribers: 

Tasks: 

Tags: 


Differential Revision: https://phabricator.intern.facebook.com/D60251676
@DavidTruby DavidTruby deleted the flang-rtlib branch October 1, 2024 14:08
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

clang Clang issues not falling into any other category flang:driver flang Flang issues not falling into any other category

Projects

None yet

Development

Successfully merging this pull request may close these issues.

7 participants