This is an archive of the discontinued LLVM Phabricator instance.

[clang][driver] Honor the last -flto(=.*)? argument
ClosedPublic

Authored by mnadeem on Aug 28 2021, 3:31 PM.

Details

Summary
  1. After D102479 code in Clang.cpp only propagates -flto instead of -flto=thin if we pass -flto -flto=thin to clang, ignoring the last -flto=.* argument.
  1. I also noticed that parseLTOMode returns LTOK_Thin when we pass -flto=thin -flto. Using the -flto= argument even though a later -flto is present.

Diff Detail

Event Timeline

mnadeem created this revision.Aug 28 2021, 3:31 PM
mnadeem requested review of this revision.Aug 28 2021, 3:31 PM
mnadeem added inline comments.Aug 28 2021, 3:38 PM
clang/lib/Driver/ToolChains/Clang.cpp
4476–4477

Another option would be to do the following, but i am not sure if there is any code that explicitly checks for/needs "=full":

if (D.getLTOMode() == LTOK_Thin)
    CmdArgs.push_back("-flto=thin");
else
    CmdArgs.push_back("-flto");
mnadeem edited the summary of this revision. (Show Details)Aug 28 2021, 4:10 PM

That code has changed quite a bit since I've worked on it.

The only problem I could see is that passing -flto=thin -flto and choosing thin LTO kinda makes sense if you interpret -flto as just "use LTO". The -flto=thin is more specific, so it could make sense to pick that. But that's just something I wonder while looking at it. Not sure if we process other options like this by just choosing the last one.

That code has changed quite a bit since I've worked on it.

The only problem I could see is that passing -flto=thin -flto and choosing thin LTO kinda makes sense if you interpret -flto as just "use LTO". The -flto=thin is more specific, so it could make sense to pick that. But that's just something I wonder while looking at it. Not sure if we process other options like this by just choosing the last one.

The default of -flto is documented as -flto=full, so I think the change here makes sense. In general the last option wins.

clang/lib/Driver/ToolChains/Clang.cpp
4476–4477

Looks like the result of getLTOMode() is already saved in the LTOMode local variable.

4476–4477

Yes, that should be fine since the default is full. But better yet, make the else pass the explicit -flto=full.

4477

Can we consolidate the option handling into parseLTOMode, which does the rest of the -flto option handling and sets the LTOMode returned by the below call to getLTOMode()? The default of -flto is "full" so it can be treated there as such. Actually, looks like it should already be doing this?

clang/test/Driver/lto.c
97

Probably want to check that we don't have additional -flto options being passed too, here and on the below lines.

mnadeem updated this revision to Diff 369498.Aug 30 2021, 11:25 AM
mnadeem marked 3 inline comments as done.
mnadeem edited the summary of this revision. (Show Details)
mnadeem added a subscriber: apazos.

Address comments.

Combined with the code in else if (Triple.isAMDGPU()) since the code is identical.
Removed the code to add -flto in favour of -flto=full since both are equivalent: https://clang.llvm.org/docs/CommandGuide/clang.html#cmdoption-flto

steven_wu added a comment.EditedAug 30 2021, 1:52 PM

I agree that it might be good to change to the behavior that last arg wins but it might be simpler to set -flto to be an alias of -flto=full:

--- a/clang/include/clang/Driver/Options.td
+++ b/clang/include/clang/Driver/Options.td
@@ -2086,7 +2086,7 @@ def flto_EQ : Joined<["-"], "flto=">, Flags<[CoreOption, CC1Option]>, Group<f_Gr
 def flto_EQ_jobserver : Flag<["-"], "flto=jobserver">, Group<f_Group>;
 def flto_EQ_auto : Flag<["-"], "flto=auto">, Group<f_Group>;
 def flto : Flag<["-"], "flto">, Flags<[CoreOption, CC1Option]>, Group<f_Group>,
-  HelpText<"Enable LTO in 'full' mode">;
+  Alias<flto_EQ>, AliasArgs<["full"]>, HelpText<"Enable LTO in 'full' mode">;
 def fno_lto : Flag<["-"], "fno-lto">, Flags<[CoreOption, CC1Option]>, Group<f_Group>,
   HelpText<"Disable LTO mode (default)">;
 def foffload_lto_EQ : Joined<["-"], "foffload-lto=">, Flags<[CoreOption]>, Group<f_Group>,

Of course, you want to clean up the option processing of flto.

mnadeem updated this revision to Diff 369558.Aug 30 2021, 2:51 PM
mnadeem retitled this revision from [clang][driver] Honor the last -flto= flag even if an earlier -flto is present to [clang][driver] Honor the last -flto(=.*)? argument.
mnadeem edited the summary of this revision. (Show Details)

Update Options.td as suggested by steven_wu

I will do a cleanup of parseLTOMode function since we don't need a OptPos parameter anymore. There are few minor places references OPT_flto or OPT_foffload_lto can be cleaned up too.

I will do a cleanup of parseLTOMode function since we don't need a OptPos parameter anymore. There are few minor places references OPT_flto or OPT_foffload_lto can be cleaned up too.

Good idea on simplifying via an alias, agree that this should all just be cleaned up now. Thanks

I will do a cleanup of parseLTOMode function since we don't need a OptPos parameter anymore. There are few minor places references OPT_flto or OPT_foffload_lto can be cleaned up too.

Will you incorporate the functional changes in this patch? Or is there still a need for this change?

I will do a cleanup of parseLTOMode function since we don't need a OptPos parameter anymore. There are few minor places references OPT_flto or OPT_foffload_lto can be cleaned up too.

Will you incorporate the functional changes in this patch? Or is there still a need for this change?

The current change set in this review is functional change while the cleanup I want is not functional after the rewrite the old option as Alias. Once flto is the alias, there is no need to handle that in the driver and those might actually become source of bug in the future.

I think it would be good to do the cleanup in the same commit unless you have compelling reason not to.

I will do a cleanup of parseLTOMode function since we don't need a OptPos parameter anymore. There are few minor places references OPT_flto or OPT_foffload_lto can be cleaned up too.

Will you incorporate the functional changes in this patch? Or is there still a need for this change?

The current change set in this review is functional change while the cleanup I want is not functional after the rewrite the old option as Alias. Once flto is the alias, there is no need to handle that in the driver and those might actually become source of bug in the future.

I think it would be good to do the cleanup in the same commit unless you have compelling reason not to.

Hi @steven_wu any idea about the timeline?

This issue is blocking some internal work, and assuming that it will take longer to get a full fix, I would prefer it if this change could go in on its own.
Otherwise I am good with doing everything in the same commit.

I will do a cleanup of parseLTOMode function since we don't need a OptPos parameter anymore. There are few minor places references OPT_flto or OPT_foffload_lto can be cleaned up too.

Will you incorporate the functional changes in this patch? Or is there still a need for this change?

The current change set in this review is functional change while the cleanup I want is not functional after the rewrite the old option as Alias. Once flto is the alias, there is no need to handle that in the driver and those might actually become source of bug in the future.

I think it would be good to do the cleanup in the same commit unless you have compelling reason not to.

Hi @steven_wu any idea about the timeline?

This issue is blocking some internal work, and assuming that it will take longer to get a full fix, I would prefer it if this change could go in on its own.
Otherwise I am good with doing everything in the same commit.

I expect the cleanup is very very simple and that is why I am suggested to do in the same commit. I am ok with a followup fix as well.

If you didn't quite get what I mean, I am suggesting to cleanup all the reference to the old flag. Since -flto is rewrite by the driver to -flto=full, clang should not query OPT_flto anymore. All references to the old flag needs to be audited and removed while preserving the correct behavior.

I will do a cleanup of parseLTOMode function since we don't need a OptPos parameter anymore. There are few minor places references OPT_flto or OPT_foffload_lto can be cleaned up too.

Will you incorporate the functional changes in this patch? Or is there still a need for this change?

The current change set in this review is functional change while the cleanup I want is not functional after the rewrite the old option as Alias. Once flto is the alias, there is no need to handle that in the driver and those might actually become source of bug in the future.

I think it would be good to do the cleanup in the same commit unless you have compelling reason not to.

Hi @steven_wu any idea about the timeline?

This issue is blocking some internal work, and assuming that it will take longer to get a full fix, I would prefer it if this change could go in on its own.
Otherwise I am good with doing everything in the same commit.

I expect the cleanup is very very simple and that is why I am suggested to do in the same commit. I am ok with a followup fix as well.

I think there might be some high level confusion. @steven_wu earlier in the thread you said "I will do a cleanup", but I think you were asking @mnadeem to do the cleanup here in this patch.

steven_wu added a comment.EditedSep 7 2021, 2:56 PM

I will do a cleanup of parseLTOMode function since we don't need a OptPos parameter anymore. There are few minor places references OPT_flto or OPT_foffload_lto can be cleaned up too.

Will you incorporate the functional changes in this patch? Or is there still a need for this change?

The current change set in this review is functional change while the cleanup I want is not functional after the rewrite the old option as Alias. Once flto is the alias, there is no need to handle that in the driver and those might actually become source of bug in the future.

I think it would be good to do the cleanup in the same commit unless you have compelling reason not to.

Hi @steven_wu any idea about the timeline?

This issue is blocking some internal work, and assuming that it will take longer to get a full fix, I would prefer it if this change could go in on its own.
Otherwise I am good with doing everything in the same commit.

I expect the cleanup is very very simple and that is why I am suggested to do in the same commit. I am ok with a followup fix as well.

I think there might be some high level confusion. @steven_wu earlier in the thread you said "I will do a cleanup", but I think you were asking @mnadeem to do the cleanup here in this patch.

Ah, I see. I probably type the reply from my phone so I didn't double check. I really mean "I would suggest @mnadeem to do a cleanup in the same commit". Sorry about the confusion.

mnadeem updated this revision to Diff 371241.Sep 7 2021, 8:08 PM
  • - Make flto an alias of flto=full.
  • - Make foffload-lto an alias of foffload-lto=full.
  • - Make flto_EQ_jobserver, flto_EQ_auto aliases of flto=full, since they are being treated as full lto right now.
  • - Clean up the code for parseLTOMode and setLTOMode.
  • - Replace uses of OPT_flto with OPT_flto_EQ.

I'm not very familiar with driver code so let me know if I made any mistake or left anything out.

mnadeem added inline comments.Sep 7 2021, 8:10 PM
clang/lib/Driver/Driver.cpp
596–598

These were removed because the options now alias with flto=full.

mnadeem updated this revision to Diff 371245.Sep 7 2021, 8:15 PM
steven_wu accepted this revision.Sep 8 2021, 9:32 AM

I am not familiar with offloading side of LTO driver but the change looks functionally the same to me. Thanks for doing this! Remember to update the commit message with the correct info.

This revision is now accepted and ready to land.Sep 8 2021, 9:32 AM
This revision was landed with ongoing or failed builds.Sep 8 2021, 3:54 PM
This revision was automatically updated to reflect the committed changes.