[flang] [clang] [flang][driver] Add -fno-fortran-main (link time) option to remove Fortran_main from link line (PR #74139)
https://github.com/mjklemm edited https://github.com/llvm/llvm-project/pull/74139 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[flang] [clang] [flang][driver] Add -fno-fortran-main (link time) option to remove Fortran_main from link line (PR #74139)
@@ -6345,6 +6345,10 @@ def J : JoinedOrSeparate<["-"], "J">, Group, Alias; +def no_fortran_main : Flag<["-"], "fno-fortran-main">, + Visibility<[FlangOption]>, Group, + HelpText<"Don't link in Fortran main">; mjklemm wrote: Done, with a slightly different wording https://github.com/llvm/llvm-project/pull/74139 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[flang] [clang] [flang][driver] Add -fno-fortran-main (link time) option to remove Fortran_main from link line (PR #74139)
https://github.com/mjklemm updated https://github.com/llvm/llvm-project/pull/74139 >From e0784bd3a6103fe6852ecc67fb01a4a8da1cf59a Mon Sep 17 00:00:00 2001 From: Michael Klemm Date: Fri, 1 Dec 2023 21:41:44 +0100 Subject: [PATCH 1/9] Add -fno-fortran-main driver option --- clang/include/clang/Driver/Options.td | 4 1 file changed, 4 insertions(+) diff --git a/clang/include/clang/Driver/Options.td b/clang/include/clang/Driver/Options.td index b959fd20fe413..057f1f4b90955 100644 --- a/clang/include/clang/Driver/Options.td +++ b/clang/include/clang/Driver/Options.td @@ -6354,6 +6354,10 @@ def J : JoinedOrSeparate<["-"], "J">, Group, Alias; +def no_fortran_main : Flag<["-"], "fno-fortran-main">, + Visibility<[FlangOption]>, Group, + HelpText<"Don't link in Fortran main">; + //===--===// // FC1 Options //===--===// >From 5c2f898be0d16fdecd44fbcf94cb0513e9a75bb2 Mon Sep 17 00:00:00 2001 From: Michael Klemm Date: Fri, 1 Dec 2023 21:42:09 +0100 Subject: [PATCH 2/9] Skip linking Fortran_main.a if -fno-fortran-main is present --- clang/lib/Driver/ToolChains/CommonArgs.cpp | 46 -- 1 file changed, 25 insertions(+), 21 deletions(-) diff --git a/clang/lib/Driver/ToolChains/CommonArgs.cpp b/clang/lib/Driver/ToolChains/CommonArgs.cpp index 51b336216c565..82ed3e53d9960 100644 --- a/clang/lib/Driver/ToolChains/CommonArgs.cpp +++ b/clang/lib/Driver/ToolChains/CommonArgs.cpp @@ -1116,33 +1116,37 @@ bool tools::addOpenMPRuntime(ArgStringList &CmdArgs, const ToolChain &TC, return true; } +// TODO: add -fno-fortran-main option and check it in this function. void tools::addFortranRuntimeLibs(const ToolChain &TC, const ArgList &Args, llvm::opt::ArgStringList &CmdArgs) { // 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()) { -// The --whole-archive option 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. Determine if --whole-archive is active when -// flang will try to link Fortran_main.a. If it is, don't add the -// --whole-archive flag to the link line. If it's not, add a proper -// --whole-archive/--no-whole-archive bracket to the link line. -bool WholeArchiveActive = false; -for (auto *Arg : Args.filtered(options::OPT_Wl_COMMA)) - if (Arg) -for (StringRef ArgValue : Arg->getValues()) { - if (ArgValue == "--whole-archive") -WholeArchiveActive = true; - if (ArgValue == "--no-whole-archive") -WholeArchiveActive = false; -} - -if (!WholeArchiveActive) - CmdArgs.push_back("--whole-archive"); -CmdArgs.push_back("-lFortran_main"); -if (!WholeArchiveActive) - CmdArgs.push_back("--no-whole-archive"); +// if -fno-fortran-main has been passed, skip linking Fortran_main.a +bool DontLinkFortranMain = Args.getLastArg(options::OPT_no_fortran_main) != nullptr; +if (!DontLinkFortranMain) { + // The --whole-archive option 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. Determine if --whole-archive is active when + // flang will try to link Fortran_main.a. If it is, don't add the + // --whole-archive flag to the link line. If it's not, add a proper + // --whole-archive/--no-whole-archive bracket to the link line. + bool WholeArchiveActive = false; + for (auto *Arg : Args.filtered(options::OPT_Wl_COMMA)) +if (Arg) + for (StringRef ArgValue : Arg->getValues()) { +if (ArgValue == "--whole-archive") + WholeArchiveActive = true; +if (ArgValue == "--no-whole-archive") + WholeArchiveActive = false; + } + if (!WholeArchiveActive) +CmdArgs.push_back("--whole-archive"); + CmdArgs.push_back("-lFortran_main"); + if (!WholeArchiveActive) +CmdArgs.push_back("--no-whole-archive"); +} // Perform regular linkage of the remaining runtime libraries. CmdArgs.push_back("-lFortranRuntime"); CmdArgs.push_back("-lFortranDecimal"); >From a87bdd40e2f4e707a47a185b49a93ae84c809bda Mon Sep 17 00:00:00 2001 From: Michael Klemm Date: Fri, 1 Dec 2023 15:10:29 -0600 Subject: [PATCH 3/9] Cleanup and simplify the code a bit --- clang/lib/Driver/ToolChains/CommonArgs.cpp | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/clang/lib/Driver/ToolChains/CommonArgs.cpp b/clang/lib/Driver/ToolChains/CommonArgs.cpp index 82ed3e53d9960..93877b75beaf7 100644 --- a/clang/lib/Driver/ToolChains/CommonArgs.cpp +++ b/clang/lib/Dr
[flang] [clang] [flang][driver] Add -fno-fortran-main (link time) option to remove Fortran_main from link line (PR #74139)
https://github.com/mjklemm closed https://github.com/llvm/llvm-project/pull/74139 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[flang] [clang] [flang][driver] Add -fno-fortran-main (link time) option to remove Fortran_main from link line (PR #74139)
https://github.com/mjklemm edited https://github.com/llvm/llvm-project/pull/74139 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[flang] [clang] [flang][driver] Add -fno-fortran-main (link time) option to remove Fortran_main from link line (PR #74139)
https://github.com/mjklemm updated https://github.com/llvm/llvm-project/pull/74139 >From 2e41335a7de3d2efa88eacee659172a3b9525e45 Mon Sep 17 00:00:00 2001 From: Michael Klemm Date: Fri, 1 Dec 2023 21:41:44 +0100 Subject: [PATCH 1/5] Add -fno-fortran-main driver option --- clang/include/clang/Driver/Options.td | 4 1 file changed, 4 insertions(+) diff --git a/clang/include/clang/Driver/Options.td b/clang/include/clang/Driver/Options.td index 19d04e82aed4d..aa26344f67b31 100644 --- a/clang/include/clang/Driver/Options.td +++ b/clang/include/clang/Driver/Options.td @@ -6345,6 +6345,10 @@ def J : JoinedOrSeparate<["-"], "J">, Group, Alias; +def no_fortran_main : Flag<["-"], "fno-fortran-main">, + Visibility<[FlangOption]>, Group, + HelpText<"Don't link in Fortran main">; + //===--===// // FC1 Options //===--===// >From 44b684ae43a6da37bb56c5b699628c6807257ad9 Mon Sep 17 00:00:00 2001 From: Michael Klemm Date: Fri, 1 Dec 2023 21:42:09 +0100 Subject: [PATCH 2/5] Skip linking Fortran_main.a if -fno-fortran-main is present --- clang/lib/Driver/ToolChains/CommonArgs.cpp | 46 -- 1 file changed, 25 insertions(+), 21 deletions(-) diff --git a/clang/lib/Driver/ToolChains/CommonArgs.cpp b/clang/lib/Driver/ToolChains/CommonArgs.cpp index 0ae8e2dce32e9..2899f07cb7490 100644 --- a/clang/lib/Driver/ToolChains/CommonArgs.cpp +++ b/clang/lib/Driver/ToolChains/CommonArgs.cpp @@ -1116,33 +1116,37 @@ bool tools::addOpenMPRuntime(ArgStringList &CmdArgs, const ToolChain &TC, return true; } +// TODO: add -fno-fortran-main option and check it in this function. void tools::addFortranRuntimeLibs(const ToolChain &TC, const ArgList &Args, llvm::opt::ArgStringList &CmdArgs) { // 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()) { -// The --whole-archive option 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. Determine if --whole-archive is active when -// flang will try to link Fortran_main.a. If it is, don't add the -// --whole-archive flag to the link line. If it's not, add a proper -// --whole-archive/--no-whole-archive bracket to the link line. -bool WholeArchiveActive = false; -for (auto *Arg : Args.filtered(options::OPT_Wl_COMMA)) - if (Arg) -for (StringRef ArgValue : Arg->getValues()) { - if (ArgValue == "--whole-archive") -WholeArchiveActive = true; - if (ArgValue == "--no-whole-archive") -WholeArchiveActive = false; -} - -if (!WholeArchiveActive) - CmdArgs.push_back("--whole-archive"); -CmdArgs.push_back("-lFortran_main"); -if (!WholeArchiveActive) - CmdArgs.push_back("--no-whole-archive"); +// if -fno-fortran-main has been passed, skip linking Fortran_main.a +bool DontLinkFortranMain = Args.getLastArg(options::OPT_no_fortran_main) != nullptr; +if (!DontLinkFortranMain) { + // The --whole-archive option 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. Determine if --whole-archive is active when + // flang will try to link Fortran_main.a. If it is, don't add the + // --whole-archive flag to the link line. If it's not, add a proper + // --whole-archive/--no-whole-archive bracket to the link line. + bool WholeArchiveActive = false; + for (auto *Arg : Args.filtered(options::OPT_Wl_COMMA)) +if (Arg) + for (StringRef ArgValue : Arg->getValues()) { +if (ArgValue == "--whole-archive") + WholeArchiveActive = true; +if (ArgValue == "--no-whole-archive") + WholeArchiveActive = false; + } + if (!WholeArchiveActive) +CmdArgs.push_back("--whole-archive"); + CmdArgs.push_back("-lFortran_main"); + if (!WholeArchiveActive) +CmdArgs.push_back("--no-whole-archive"); +} // Perform regular linkage of the remaining runtime libraries. CmdArgs.push_back("-lFortranRuntime"); CmdArgs.push_back("-lFortranDecimal"); >From e1a472fa914ea9405be6589e89fbe8201448600f Mon Sep 17 00:00:00 2001 From: Michael Klemm Date: Fri, 1 Dec 2023 15:10:29 -0600 Subject: [PATCH 3/5] Cleanup and simplify the code a bit --- clang/lib/Driver/ToolChains/CommonArgs.cpp | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/clang/lib/Driver/ToolChains/CommonArgs.cpp b/clang/lib/Driver/ToolChains/CommonArgs.cpp index 2899f07cb7490..f0e3df2a63ae9 100644 --- a/clang/lib/Driver/ToolChains/CommonArgs.cpp +++ b/clang/lib/Dr
[flang] [clang] [flang][driver] Add -fno-fortran-main (link time) option to remove Fortran_main from link line (PR #74139)
mjklemm wrote: > I think this solution is fine, at least in the short term. > > I had a think after reviewing the initial patch and looking at the failure > that @tblah showed in #73124; my thoughts are that the “correct” way of doing > this would be instead of linking Fortran_main all the time, we could insert a > linker directive in the object file containing the program statement. That > way we would only link Fortran_main when there is actually a program > statement in the Fortran. However that’s more work and would need a bit more > thought anyway so we at least need something like this or the revert in the > interim. That sounds good! For now, the code should effectively avoid linking Fortran_main on Linux and Windows if -fno-fortran-main is present. https://github.com/llvm/llvm-project/pull/74139 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[flang] [clang] [flang][driver] Add -fno-fortran-main (link time) option to remove Fortran_main from link line (PR #74139)
https://github.com/mjklemm updated https://github.com/llvm/llvm-project/pull/74139 >From 2e41335a7de3d2efa88eacee659172a3b9525e45 Mon Sep 17 00:00:00 2001 From: Michael Klemm Date: Fri, 1 Dec 2023 21:41:44 +0100 Subject: [PATCH 1/5] Add -fno-fortran-main driver option --- clang/include/clang/Driver/Options.td | 4 1 file changed, 4 insertions(+) diff --git a/clang/include/clang/Driver/Options.td b/clang/include/clang/Driver/Options.td index 19d04e82aed4d..aa26344f67b31 100644 --- a/clang/include/clang/Driver/Options.td +++ b/clang/include/clang/Driver/Options.td @@ -6345,6 +6345,10 @@ def J : JoinedOrSeparate<["-"], "J">, Group, Alias; +def no_fortran_main : Flag<["-"], "fno-fortran-main">, + Visibility<[FlangOption]>, Group, + HelpText<"Don't link in Fortran main">; + //===--===// // FC1 Options //===--===// >From 44b684ae43a6da37bb56c5b699628c6807257ad9 Mon Sep 17 00:00:00 2001 From: Michael Klemm Date: Fri, 1 Dec 2023 21:42:09 +0100 Subject: [PATCH 2/5] Skip linking Fortran_main.a if -fno-fortran-main is present --- clang/lib/Driver/ToolChains/CommonArgs.cpp | 46 -- 1 file changed, 25 insertions(+), 21 deletions(-) diff --git a/clang/lib/Driver/ToolChains/CommonArgs.cpp b/clang/lib/Driver/ToolChains/CommonArgs.cpp index 0ae8e2dce32e9..2899f07cb7490 100644 --- a/clang/lib/Driver/ToolChains/CommonArgs.cpp +++ b/clang/lib/Driver/ToolChains/CommonArgs.cpp @@ -1116,33 +1116,37 @@ bool tools::addOpenMPRuntime(ArgStringList &CmdArgs, const ToolChain &TC, return true; } +// TODO: add -fno-fortran-main option and check it in this function. void tools::addFortranRuntimeLibs(const ToolChain &TC, const ArgList &Args, llvm::opt::ArgStringList &CmdArgs) { // 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()) { -// The --whole-archive option 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. Determine if --whole-archive is active when -// flang will try to link Fortran_main.a. If it is, don't add the -// --whole-archive flag to the link line. If it's not, add a proper -// --whole-archive/--no-whole-archive bracket to the link line. -bool WholeArchiveActive = false; -for (auto *Arg : Args.filtered(options::OPT_Wl_COMMA)) - if (Arg) -for (StringRef ArgValue : Arg->getValues()) { - if (ArgValue == "--whole-archive") -WholeArchiveActive = true; - if (ArgValue == "--no-whole-archive") -WholeArchiveActive = false; -} - -if (!WholeArchiveActive) - CmdArgs.push_back("--whole-archive"); -CmdArgs.push_back("-lFortran_main"); -if (!WholeArchiveActive) - CmdArgs.push_back("--no-whole-archive"); +// if -fno-fortran-main has been passed, skip linking Fortran_main.a +bool DontLinkFortranMain = Args.getLastArg(options::OPT_no_fortran_main) != nullptr; +if (!DontLinkFortranMain) { + // The --whole-archive option 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. Determine if --whole-archive is active when + // flang will try to link Fortran_main.a. If it is, don't add the + // --whole-archive flag to the link line. If it's not, add a proper + // --whole-archive/--no-whole-archive bracket to the link line. + bool WholeArchiveActive = false; + for (auto *Arg : Args.filtered(options::OPT_Wl_COMMA)) +if (Arg) + for (StringRef ArgValue : Arg->getValues()) { +if (ArgValue == "--whole-archive") + WholeArchiveActive = true; +if (ArgValue == "--no-whole-archive") + WholeArchiveActive = false; + } + if (!WholeArchiveActive) +CmdArgs.push_back("--whole-archive"); + CmdArgs.push_back("-lFortran_main"); + if (!WholeArchiveActive) +CmdArgs.push_back("--no-whole-archive"); +} // Perform regular linkage of the remaining runtime libraries. CmdArgs.push_back("-lFortranRuntime"); CmdArgs.push_back("-lFortranDecimal"); >From e1a472fa914ea9405be6589e89fbe8201448600f Mon Sep 17 00:00:00 2001 From: Michael Klemm Date: Fri, 1 Dec 2023 15:10:29 -0600 Subject: [PATCH 3/5] Cleanup and simplify the code a bit --- clang/lib/Driver/ToolChains/CommonArgs.cpp | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/clang/lib/Driver/ToolChains/CommonArgs.cpp b/clang/lib/Driver/ToolChains/CommonArgs.cpp index 2899f07cb7490..f0e3df2a63ae9 100644 --- a/clang/lib/Driver/ToolChains/CommonArgs.cpp +++ b/clang/lib/Dr
[flang] [clang] [flang][driver] Add -fno-fortran-main (link time) option to remove Fortran_main from link line (PR #74139)
https://github.com/tblah approved this pull request. Thanks for the fix. LGTM https://github.com/llvm/llvm-project/pull/74139 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[flang] [clang] [flang][driver] Add -fno-fortran-main (link time) option to remove Fortran_main from link line (PR #74139)
mjklemm wrote: @DavidTruby If you are OK with the way I handled MSVC, please approve and I will merge the PR (or change it if you want some changes to be made). https://github.com/llvm/llvm-project/pull/74139 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[flang] [clang] [flang][driver] Add -fno-fortran-main (link time) option to remove Fortran_main from link line (PR #74139)
https://github.com/banach-space edited https://github.com/llvm/llvm-project/pull/74139 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[flang] [clang] [flang][driver] Add -fno-fortran-main (link time) option to remove Fortran_main from link line (PR #74139)
@@ -6345,6 +6345,10 @@ def J : JoinedOrSeparate<["-"], "J">, Group, Alias; +def no_fortran_main : Flag<["-"], "fno-fortran-main">, + Visibility<[FlangOption]>, Group, + HelpText<"Don't link in Fortran main">; + banach-space wrote: ```suggestion def J : JoinedOrSeparate<["-"], "J">, Flags<[RenderJoined]>, Visibility<[FlangOption, FC1Option]>, Group, Alias; } // let Visibility = [FC1Option, FlangOption] //===--===// // FLang Options //===--===// let Visibility = [FlangOption] in { def no_fortran_main : Flag<["-"], "fno-fortran-main">, Visibility<[FlangOption]>, Group, HelpText<"Don't link in Fortran main">; } } // let Visibility = [ FlangOption] ``` I hope this suggestion is clear - sadly GitHub is not allowing me to make a proper suggestion for the code above of what you added. https://github.com/llvm/llvm-project/pull/74139 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[flang] [clang] [flang][driver] Add -fno-fortran-main (link time) option to remove Fortran_main from link line (PR #74139)
https://github.com/mjklemm edited https://github.com/llvm/llvm-project/pull/74139 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[flang] [clang] [flang][driver] Add -fno-fortran-main (link time) option to remove Fortran_main from link line (PR #74139)
https://github.com/DavidTruby approved this pull request. LGTM sorry sometimes reviews get lost in the noise on GitHub... https://github.com/llvm/llvm-project/pull/74139 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[flang] [clang] [flang][driver] Add -fno-fortran-main (link time) option to remove Fortran_main from link line (PR #74139)
https://github.com/banach-space edited https://github.com/llvm/llvm-project/pull/74139 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[flang] [clang] [flang][driver] Add -fno-fortran-main (link time) option to remove Fortran_main from link line (PR #74139)
https://github.com/banach-space approved this pull request. I'm making two more small suggestions, but these are nits. Fell free to ignore. LGTM, thank you! [nit] "legacy flang" --> "Classic Flang" (in summary) https://github.com/llvm/llvm-project/pull/74139 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits