This is an archive of the discontinued LLVM Phabricator instance.

[2/3] TLS loads optimization (hoist)
AbandonedPublic

Authored by xiangzhangllvm on Mar 28 2022, 12:34 AM.

Details

Summary

We supported TLS loads optimization (LLVM part) at D120000

Now this patch mainly add Clang options (-mtls-load-hoist -mno-tls-load-hoist) for it.

Also we update the old llvm option to a bool option in this patch.

Diff Detail

Event Timeline

xiangzhangllvm requested review of this revision.Mar 28 2022, 12:34 AM
Herald added a project: Restricted Project. · View Herald TranscriptMar 28 2022, 12:34 AM
efriedma added inline comments.Mar 28 2022, 3:52 PM
clang/include/clang/Driver/Options.td
3627

clang driver options should generally have positive and negative versions (i.e. -mtls-load-hoist/-mno-tls-load-hoist).

I'm not sure I understand why we need to expose this as a clang option, though. In the initial patch, you mentioned you wanted to land it off-by-default initially... but I'm not sure why we can't just flip the internal switch to turn it on by default, and call it done.

xiangzhangllvm added inline comments.Mar 28 2022, 5:40 PM
clang/include/clang/Driver/Options.td
3627

First thanks for review!
Let me add "-mno-tls-load-hoist" , can I ask a question ? (sorry for I am not much familiar with the clang driver) Why clang driver should have both positive and negative options ? (if it default to negative, doesn't only add positive is enough?)

Yes, I land it off-by-default at [1/3] patch. Then I mentioned that:

"This is a general optimization which will affect all targets test, I'd like to enable it by another independent patch [3/3] with mainly modify tests."

Another purpose I want to just enable it in an independent patch is that I am not 100% sure it will has gain for all the targets. So I'd like to remain a easy way to revert the "enable patch" if necessary for some targets.

efriedma added inline comments.Mar 28 2022, 6:06 PM
clang/include/clang/Driver/Options.td
3627

In general, providing flags both ways has two benefits:

  1. It lets users request a specific behavior even if the default changes in the future.
  2. Sometimes, in complex build system, editing flags is tricky, so it's helpful to be able to override a flag by just sticking the inverse flag at the end of the command-line.

I think you're being too conservative in terms of the expected impact of this; it's hard for me to imagine a situation where hoisting a call to __tls_get_addr actually causes harm.

clang/include/clang/Driver/Options.td
3627

Thanks very much!

Caution is the parent of safety : )

Add -mno-tls-load-hoist option.

xiangzhangllvm edited the summary of this revision. (Show Details)Mar 28 2022, 10:16 PM
MaskRay added a comment.EditedMar 29 2022, 7:36 PM

If the option is going to be on and never turned off, we don't need a clang driver option. Clang driver option has a commitment to relative stability.
For experimental things you can use cl::opt. cl::opt can be relatively freely removed.

If the option is going to be on and never turned off, we don't need a clang driver option. Clang driver option has a commitment to relative stability.
For experimental things you can use cl::opt. cl::opt can be relatively freely removed.

Make sense, I prefer to keep the clang driver option for customers
(in fact, this job is required from our customers and they point out they want an option to control it)
Yes, I added cl::opt for it, PLS refer TLSVariableHoist.cpp: line 43.
Thanks a lot for your reviewing!

This patch is quite clear. Anyone can help accept it?

If the option is going to be on and never turned off, we don't need a clang driver option. Clang driver option has a commitment to relative stability.
For experimental things you can use cl::opt. cl::opt can be relatively freely removed.

Make sense, I prefer to keep the clang driver option for customers
(in fact, this job is required from our customers and they point out they want an option to control it)
Yes, I added cl::opt for it, PLS refer TLSVariableHoist.cpp: line 43.
Thanks a lot for your reviewing!

This doesn't look like sufficient justification adding a new driver option for a feature which should just be enabled at all time.
Seems like the option is to work around potential issues just in case?
You can do that with cl::opt. In case anything bad happens, you can either revert the patch or tell the customer to add -mllvm -xxx.
When the feature seems robust enough, remove the cl::opt option.
Anyway, I just think we should not commit to have such a driver option.

If the option is going to be on and never turned off

It is not supposed to be "never turned off".
Generally speaking, hoisting the common TLS address call operation will big probably have performance gains.
But this will also lengthen register interval, possible cause more spills.
especially in case

                   BB1
                 /      \
               BB2    BB3 (with 1 tls call or more tls calls but BB is cold)
(with 1 tls call)

And this patch only add about 15 lines to support the driver options. (Others are refining the llvm option and test)

The problem isn't really the amount of code needed to support the option... it's that a formally documented option needs to have clear semantics, and we need to support it forever. We really don't like options that say "turn off optimization X that has no user-visible semantics". I mean, even if you pass -mno-tls-load-hoist, we might end up hoisting anyway, depending on what you're doing with the address...

The problem isn't really the amount of code needed to support the option... it's that a formally documented option needs to have clear semantics, and we need to support it forever. We really don't like options that say "turn off optimization X that has no user-visible semantics". I mean, even if you pass -mno-tls-load-hoist, we might end up hoisting anyway, depending on what you're doing with the address...

Thank you! I begin more clear about the principle of Clang Driver option.
(It should prefer user-visible, functional, performance with visible control)
Let me abandon this patch and enable the optimization in default in PIC mode.

xiangzhangllvm abandoned this revision.Mar 31 2022, 6:17 PM