https://github.com/mjklemm updated https://github.com/llvm/llvm-project/pull/73124
>From 2a2693364cb8e9b657b9ff54aa78df0466b55fe4 Mon Sep 17 00:00:00 2001 From: Michael Klemm <michael.kl...@amd.com> Date: Wed, 22 Nov 2023 14:22:20 +0100 Subject: [PATCH 01/11] Let the linker fail on multiple definitions of main() --- clang/lib/Driver/ToolChains/CommonArgs.cpp | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/clang/lib/Driver/ToolChains/CommonArgs.cpp b/clang/lib/Driver/ToolChains/CommonArgs.cpp index 1f31c6395206ee8..740ae71177b2c3a 100644 --- a/clang/lib/Driver/ToolChains/CommonArgs.cpp +++ b/clang/lib/Driver/ToolChains/CommonArgs.cpp @@ -982,7 +982,20 @@ void tools::addFortranRuntimeLibs(const ToolChain &TC, // These are handled earlier on Windows by telling the frontend driver to add // the correct libraries to link against as dependents in the object file. if (!TC.getTriple().isKnownWindowsMSVCEnvironment()) { + // --whole-archive needs to be part of the link line to make sure + // that the main() function from Fortran_main.a is pulled in by + // the linker. + // + // We are using this --whole-archive/--no-whole-archive bracket w/o + // any further checks, because -Wl,--whole-archive at the flang-new new + // line will not sucessfully complete, unless the user correctly specified + // -Wl,--no-whole-archive (e.g., -Wl,--whole-archive -ldummy + // -Wl,--no-whole-archive). + CmdArgs.push_back("--whole-archive"); CmdArgs.push_back("-lFortran_main"); + CmdArgs.push_back("--no-whole-archive"); + + // Perform regular linkage of the remaining runtime libraries. CmdArgs.push_back("-lFortranRuntime"); CmdArgs.push_back("-lFortranDecimal"); } @@ -993,7 +1006,7 @@ void tools::addFortranRuntimeLibraryPath(const ToolChain &TC, ArgStringList &CmdArgs) { // Default to the <driver-path>/../lib directory. This works fine on the // platforms that we have tested so far. We will probably have to re-fine - // this in the future. In particular, on some platforms, we may need to use + // this in the future. In particular, on some platforms, we may need to useq // lib64 instead of lib. SmallString<256> DefaultLibPath = llvm::sys::path::parent_path(TC.getDriver().Dir); >From 0d652282f4dbed2dde11df53ead3e6c8b6856bed Mon Sep 17 00:00:00 2001 From: Michael Klemm <michael.kl...@amd.com> Date: Wed, 22 Nov 2023 15:18:51 +0100 Subject: [PATCH 02/11] Improve comments and remove accidental typo --- clang/lib/Driver/ToolChains/CommonArgs.cpp | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/clang/lib/Driver/ToolChains/CommonArgs.cpp b/clang/lib/Driver/ToolChains/CommonArgs.cpp index 740ae71177b2c3a..464a87737de062c 100644 --- a/clang/lib/Driver/ToolChains/CommonArgs.cpp +++ b/clang/lib/Driver/ToolChains/CommonArgs.cpp @@ -987,10 +987,10 @@ void tools::addFortranRuntimeLibs(const ToolChain &TC, // the linker. // // We are using this --whole-archive/--no-whole-archive bracket w/o - // any further checks, because -Wl,--whole-archive at the flang-new new - // line will not sucessfully complete, unless the user correctly specified - // -Wl,--no-whole-archive (e.g., -Wl,--whole-archive -ldummy - // -Wl,--no-whole-archive). + // any further checks, because -Wl,--whole-archive at the flang + // driver's link line will not sucessfully complete, unless the user + // correctly specified -Wl,--whole-archive/-Wl,--no-whole-archive + // (e.g., -Wl,--whole-archive -ldummy -Wl,--no-whole-archive). CmdArgs.push_back("--whole-archive"); CmdArgs.push_back("-lFortran_main"); CmdArgs.push_back("--no-whole-archive"); @@ -1006,7 +1006,7 @@ void tools::addFortranRuntimeLibraryPath(const ToolChain &TC, ArgStringList &CmdArgs) { // Default to the <driver-path>/../lib directory. This works fine on the // platforms that we have tested so far. We will probably have to re-fine - // this in the future. In particular, on some platforms, we may need to useq + // this in the future. In particular, on some platforms, we may need to use // lib64 instead of lib. SmallString<256> DefaultLibPath = llvm::sys::path::parent_path(TC.getDriver().Dir); >From 39612e237cb815cf4ea0120027783d35304bcb6b Mon Sep 17 00:00:00 2001 From: Michael Klemm <michael.kl...@amd.com> Date: Wed, 22 Nov 2023 20:26:02 +0100 Subject: [PATCH 03/11] Correct link line test for flang-new (for Linux) --- flang/test/Driver/linker-flags.f90 | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/flang/test/Driver/linker-flags.f90 b/flang/test/Driver/linker-flags.f90 index 85c4d60b3f09862..ea91946316cfaa6 100644 --- a/flang/test/Driver/linker-flags.f90 +++ b/flang/test/Driver/linker-flags.f90 @@ -28,7 +28,7 @@ ! executable and may find the GNU linker from MinGW or Cygwin. ! UNIX-LABEL: "{{.*}}ld{{(\.exe)?}}" ! UNIX-SAME: "[[object_file]]" -! UNIX-SAME: "-lFortran_main" "-lFortranRuntime" "-lFortranDecimal" "-lm" +! UNIX-SAME: "--whole-archive" "-lFortran_main" "--no-whole-archive" "-lFortranRuntime" "-lFortranDecimal" "-lm" ! DARWIN-LABEL: "{{.*}}ld{{(\.exe)?}}" ! DARWIN-SAME: "[[object_file]]" @@ -38,7 +38,7 @@ ! HAIKU-LABEL: "{{.*}}ld{{(\.exe)?}}" ! HAIKU-SAME: "[[object_file]]" -! HAIKU-SAME: "-lFortran_main" "-lFortranRuntime" "-lFortranDecimal" +! HAIKU-SAME: "--whole-archive" "-lFortran_main" "--no-whole-archive" "-lFortranRuntime" "-lFortranDecimal" ! MINGW-LABEL: "{{.*}}ld{{(\.exe)?}}" ! MINGW-SAME: "[[object_file]]" >From f2efed8a45c06937a3f9bd72112c344582fd0b05 Mon Sep 17 00:00:00 2001 From: Michael Klemm <michael.kl...@amd.com> Date: Thu, 23 Nov 2023 14:26:18 +0100 Subject: [PATCH 04/11] Extend flang test system with %clang substitution --- flang/test/lit.cfg.py | 1 + 1 file changed, 1 insertion(+) diff --git a/flang/test/lit.cfg.py b/flang/test/lit.cfg.py index ac504dc5d1fbdfa..faddaee94b6b313 100644 --- a/flang/test/lit.cfg.py +++ b/flang/test/lit.cfg.py @@ -122,6 +122,7 @@ # the build directory holding that tool. tools = [ ToolSubst("%flang", command=FindTool("flang-new"), unresolved="fatal"), + ToolSubst("%clang", command=FindTool("clang"), unresolved="fatal"), ToolSubst( "%flang_fc1", command=FindTool("flang-new"), >From f9a023bc40f1d290ead4793ae56f10aafe4c05ca Mon Sep 17 00:00:00 2001 From: Michael Klemm <michael.kl...@amd.com> Date: Thu, 23 Nov 2023 12:25:13 +0100 Subject: [PATCH 05/11] Add tests for failing linker (albeit not very sophisticated yet) --- flang/test/Driver/main_dupes.f90 | 13 +++++++++++++ flang/test/Driver/main_dupes.f90.c-part | 8 ++++++++ 2 files changed, 21 insertions(+) create mode 100644 flang/test/Driver/main_dupes.f90 create mode 100644 flang/test/Driver/main_dupes.f90.c-part diff --git a/flang/test/Driver/main_dupes.f90 b/flang/test/Driver/main_dupes.f90 new file mode 100644 index 000000000000000..7ed5128ca5dd32f --- /dev/null +++ b/flang/test/Driver/main_dupes.f90 @@ -0,0 +1,13 @@ +! RUN: %clang -x c -o %t.c-part -c %s.c-part +! RUN: %flang -o %t -c %s +! RUN: not %flang -o %t.exe %t %t.c-part 2>&1 + +! TODO: potentially add further checks to ensure that proper +! linker error messages are detected and checked via +! FileCheck. + +program main_dupes + ! Irrelevant what to do in here. + ! Test is supposed to fail at link time. + print '(A)', 'Hello from Fortran' +end program main_dupes diff --git a/flang/test/Driver/main_dupes.f90.c-part b/flang/test/Driver/main_dupes.f90.c-part new file mode 100644 index 000000000000000..baae10f94e26364 --- /dev/null +++ b/flang/test/Driver/main_dupes.f90.c-part @@ -0,0 +1,8 @@ +#include <stdio.h> + +int main(int argc, char * argv[]) { + // Irrelevant what to do in here. + // Test is supposed to fail at link time. + printf("Hello from C [%s]\n", __FUNCTION__); + return 0; +} >From 95556a1e421b5e523ee1f9f2739f5b2cc5cb98c5 Mon Sep 17 00:00:00 2001 From: Michael Klemm <michael.kl...@amd.com> Date: Thu, 23 Nov 2023 18:00:58 +0100 Subject: [PATCH 06/11] Move the C part of the main_dupes test to the Inputs sub-folder --- .../Driver/{main_dupes.f90.c-part => Inputs/main_dupes.c} | 0 flang/test/Driver/main_dupes.f90 | 6 ++++-- 2 files changed, 4 insertions(+), 2 deletions(-) rename flang/test/Driver/{main_dupes.f90.c-part => Inputs/main_dupes.c} (100%) diff --git a/flang/test/Driver/main_dupes.f90.c-part b/flang/test/Driver/Inputs/main_dupes.c similarity index 100% rename from flang/test/Driver/main_dupes.f90.c-part rename to flang/test/Driver/Inputs/main_dupes.c diff --git a/flang/test/Driver/main_dupes.f90 b/flang/test/Driver/main_dupes.f90 index 7ed5128ca5dd32f..017a86b79f0d609 100644 --- a/flang/test/Driver/main_dupes.f90 +++ b/flang/test/Driver/main_dupes.f90 @@ -1,6 +1,8 @@ -! RUN: %clang -x c -o %t.c-part -c %s.c-part +! UNSUPPORTED: system-windows + +! RUN: %clang -o %t.c-object -c %S/Inputs/main_dupes.c ! RUN: %flang -o %t -c %s -! RUN: not %flang -o %t.exe %t %t.c-part 2>&1 +! RUN: not %flang -o %t.exe %t %t.c-object 2>&1 ! TODO: potentially add further checks to ensure that proper ! linker error messages are detected and checked via >From 1d78798a0d363b67c3cb17d0b7dfd21ece640248 Mon Sep 17 00:00:00 2001 From: Michael Klemm <michael.kl...@amd.com> Date: Thu, 23 Nov 2023 18:05:10 +0100 Subject: [PATCH 07/11] Make clang-format happy --- flang/test/Driver/Inputs/main_dupes.c | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/flang/test/Driver/Inputs/main_dupes.c b/flang/test/Driver/Inputs/main_dupes.c index baae10f94e26364..dd318c0816f2af9 100644 --- a/flang/test/Driver/Inputs/main_dupes.c +++ b/flang/test/Driver/Inputs/main_dupes.c @@ -1,8 +1,8 @@ #include <stdio.h> -int main(int argc, char * argv[]) { - // Irrelevant what to do in here. - // Test is supposed to fail at link time. - printf("Hello from C [%s]\n", __FUNCTION__); - return 0; +int main(int argc, char *argv[]) { + // Irrelevant what to do in here. + // Test is supposed to fail at link time. + printf("Hello from C [%s]\n", __FUNCTION__); + return 0; } >From cf3c4d7760e047c00ebe7ee65443c10da6cba813 Mon Sep 17 00:00:00 2001 From: Michael Klemm <michael.kl...@amd.com> Date: Fri, 24 Nov 2023 14:23:28 +0100 Subject: [PATCH 08/11] Revert "Extend flang test system with %clang substitution" This reverts commit 72f56d7f03343b9c183b929a4226de89f92472b4. --- flang/test/lit.cfg.py | 1 - 1 file changed, 1 deletion(-) diff --git a/flang/test/lit.cfg.py b/flang/test/lit.cfg.py index faddaee94b6b313..ac504dc5d1fbdfa 100644 --- a/flang/test/lit.cfg.py +++ b/flang/test/lit.cfg.py @@ -122,7 +122,6 @@ # the build directory holding that tool. tools = [ ToolSubst("%flang", command=FindTool("flang-new"), unresolved="fatal"), - ToolSubst("%clang", command=FindTool("clang"), unresolved="fatal"), ToolSubst( "%flang_fc1", command=FindTool("flang-new"), >From 327566eb92087ee4871ac6902821d469767c1574 Mon Sep 17 00:00:00 2001 From: Michael Klemm <michael.kl...@amd.com> Date: Fri, 24 Nov 2023 14:23:21 +0100 Subject: [PATCH 09/11] Remove dependency to clang by using an IR file --- flang/test/Driver/Inputs/main_dupes.c | 8 -------- flang/test/Driver/Inputs/main_dupes.ll | 7 +++++++ flang/test/Driver/main_dupes.f90 | 2 +- 3 files changed, 8 insertions(+), 9 deletions(-) delete mode 100644 flang/test/Driver/Inputs/main_dupes.c create mode 100644 flang/test/Driver/Inputs/main_dupes.ll diff --git a/flang/test/Driver/Inputs/main_dupes.c b/flang/test/Driver/Inputs/main_dupes.c deleted file mode 100644 index dd318c0816f2af9..000000000000000 --- a/flang/test/Driver/Inputs/main_dupes.c +++ /dev/null @@ -1,8 +0,0 @@ -#include <stdio.h> - -int main(int argc, char *argv[]) { - // Irrelevant what to do in here. - // Test is supposed to fail at link time. - printf("Hello from C [%s]\n", __FUNCTION__); - return 0; -} diff --git a/flang/test/Driver/Inputs/main_dupes.ll b/flang/test/Driver/Inputs/main_dupes.ll new file mode 100644 index 000000000000000..ff2b19dfe9e288b --- /dev/null +++ b/flang/test/Driver/Inputs/main_dupes.ll @@ -0,0 +1,7 @@ +; Create the symbol 'main'; does not have to be the correct +; signature for 'main', we just need the symbol for the linker +; to fail during the test. + +define i32 @main() { + ret i32 0 +} diff --git a/flang/test/Driver/main_dupes.f90 b/flang/test/Driver/main_dupes.f90 index 017a86b79f0d609..ce97e543ed8578b 100644 --- a/flang/test/Driver/main_dupes.f90 +++ b/flang/test/Driver/main_dupes.f90 @@ -1,6 +1,6 @@ ! UNSUPPORTED: system-windows -! RUN: %clang -o %t.c-object -c %S/Inputs/main_dupes.c +! RUN: %flang -x ir -o %t.c-object -c %S/Inputs/main_dupes.ll ! RUN: %flang -o %t -c %s ! RUN: not %flang -o %t.exe %t %t.c-object 2>&1 >From 11ab38d3c7ddd70e961768ad1df299af2cbedb55 Mon Sep 17 00:00:00 2001 From: Michael Klemm <michael.kl...@amd.com> Date: Fri, 24 Nov 2023 18:01:22 +0100 Subject: [PATCH 10/11] Simplify test --- flang/test/Driver/main_dupes.f90 | 1 - 1 file changed, 1 deletion(-) diff --git a/flang/test/Driver/main_dupes.f90 b/flang/test/Driver/main_dupes.f90 index ce97e543ed8578b..5abc56428f0c144 100644 --- a/flang/test/Driver/main_dupes.f90 +++ b/flang/test/Driver/main_dupes.f90 @@ -11,5 +11,4 @@ program main_dupes ! Irrelevant what to do in here. ! Test is supposed to fail at link time. - print '(A)', 'Hello from Fortran' end program main_dupes >From 6805e38375375bc0e6af6c1f345e54596e3608e9 Mon Sep 17 00:00:00 2001 From: Michael Klemm <michael.kl...@amd.com> Date: Mon, 27 Nov 2023 09:23:17 +0100 Subject: [PATCH 11/11] Rename test, according to reviewer comments --- .../test/Driver/Inputs/{main_dupes.ll => no_duplicate_main.ll} | 0 flang/test/Driver/{main_dupes.f90 => no_duplicate_main.f90} | 2 +- 2 files changed, 1 insertion(+), 1 deletion(-) rename flang/test/Driver/Inputs/{main_dupes.ll => no_duplicate_main.ll} (100%) rename flang/test/Driver/{main_dupes.f90 => no_duplicate_main.f90} (84%) diff --git a/flang/test/Driver/Inputs/main_dupes.ll b/flang/test/Driver/Inputs/no_duplicate_main.ll similarity index 100% rename from flang/test/Driver/Inputs/main_dupes.ll rename to flang/test/Driver/Inputs/no_duplicate_main.ll diff --git a/flang/test/Driver/main_dupes.f90 b/flang/test/Driver/no_duplicate_main.f90 similarity index 84% rename from flang/test/Driver/main_dupes.f90 rename to flang/test/Driver/no_duplicate_main.f90 index 5abc56428f0c144..4e33f4f2aeba3f7 100644 --- a/flang/test/Driver/main_dupes.f90 +++ b/flang/test/Driver/no_duplicate_main.f90 @@ -1,6 +1,6 @@ ! UNSUPPORTED: system-windows -! RUN: %flang -x ir -o %t.c-object -c %S/Inputs/main_dupes.ll +! RUN: %flang -x ir -o %t.c-object -c %S/Inputs/no_duplicate_main.ll ! RUN: %flang -o %t -c %s ! RUN: not %flang -o %t.exe %t %t.c-object 2>&1 _______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits