I surprisingly find that FuncSpec pass can't specialize calls with direct constant.
An example is given as the test case.
I checked that the original pass couldn't pass that.
Test Plan: check-llvm
Differential D104365
[FuncSpec] Enabling specializing direct constant ChuanqiXu on Jun 16 2021, 1:49 AM. Authored by
Details I surprisingly find that FuncSpec pass can't specialize calls with direct constant. Test Plan: check-llvm
Diff Detail Event Timeline
Comment Actions Supporting integer constants opens up a whole new set of opportunities for function specialisation. This will have a big impact on compile-times, which is the main concern of this pass.
Comment Actions Yeah, I forgot to measure the compile-time. I would measure it later. But I guess it may be OK since we control the cost to specialize function incrementally (implemented in getSpecializationCost()). So the amount of new specialized functions wouldn't be much larger.
Comment Actions
Why would we not specialise more? getSpecializationCost() just returns the cost for specialising a function. If we start doing this for functions with integer constants, I don't see why we could not start specialising more. But will double check if I missed anything. I still think some fine gain control what to specialise will be convenient (for testing/experimenting). I will add an option for function pointers and globals. Comment Actions Since the cost of specializing a function would be multiplied by a penalty, which is the number of function specialized. So within the process of function specialized, the penalty would be larger and larger. So the number of new specialized functions wouldn't much more. It should be convergent. Comment Actions I tested the compile time overhead on SPEC2017 intrate. The results show that this change wouldn't increase the compile-time with significant change. And the improvement for 505.mcf_r is remained. Comment Actions Let me explain why I am a bit pushing back at this: I would like to see function specialisation enabled by default one day, and therefore I am interested to know how much more difficult that is going to be when we also specialise on integer constants. In previous comments you mentioned:
If we are going to specialise on integers, we are going to specialise more, even though the cost-model indeed takes the number of specialised functions into account. I don't think there's another way of looking at it.
I don't think we can draw this conclusion because we know that for SPEC function pointers are important. To understand what is happening, I would like to know how many functions were specialised on integer constants. Only then we can say something about this. And in order to get these numbers, it is probably convenient:
Regardless whether this triggers or not in SPEC, I also think we need a few more data points to draw any conclusions. Because it is easy and relevant, I would suggest at least a bootstrap build and sqlite. Comment Actions Yeah, that's my wish too.
I understand that it is always better to be careful and require an option to control the behavior. I could add an option and do more stats. But I think that it is a little bit counter-intuitive that function specialization wouldn't specialize integer constants. For a general example: void foo(..., bool cond) { if (cond) { } else { } } From my mind, every one should think cond could be handled by function specialization. My point is that function specialization should support for constant integers. Even we add options, the ability to handle constant integers should be the default behavior too. But even the stats shows that this may incur some CT increasement, I would still think function specialization pass should handle the constant integers. Since it should be. On the other hand, I think the main part we should focus on is the cost model. In other words, even the CT increases due to this patch, we should investigate on the cost model instead of turn this off simply. I hope that we can get a consensus on this. Comment Actions Specialising on constant integers would definitely be good, and is indeed what we want. But it's early days for function specialisation, and there are quite a few limitations, and this is just one of them. I disagree with this:
First of all, because we first need to know what the increase is. Second, when we know the increase, only then we can say if it should or not.
Agreed on this. At the moment, we lack insights and data what this patch is doing. If we are going to build on top function specialisation and add more features, I would like to see the the new features and the cost-model to be developed hand in hand, otherwise it is only going to be harder later. Repeating what I said earlier, I think the order of events should be this: I would like to see more fine-grain stats and options, compile time data for a few different code bases, and then a discussion on the cost-model before we add this. We would like to achieve parity with GCC which has this enabled by default and is the reason I would like to get this enabled by default. My preferred approach would be to try this for constant globals and function pointers, because if we don't succeed in that, we are definitely not going to succeed if we also specialise on constant integers. Thus, I would like to do this step by step, by address some existing TODOs first to get the code in shape for this enabling this by default, then flip a switch to also specialise on constant integers. Thus, if you do this under an option that is off by default first, I have no problems at all with adding thi, while perhaps in the mean time you work on getting numbers and the cost-model. Comment Actions
Agreed. Although it seems like that the process of improving in this way would be much slower than I thought before, it would be much more stable. I could understand it.
Would do. Comment Actions Add an option to control the behavior. Then I did some statistics for SPEC2017int The total number of specialized functions increased about 18% (from 49 to 58) Then I didn't find significant compile-time change with turning option on. I wonder if this means that we can turn this option on by default. Comment Actions This looks good to me. Let's start with having this off by default. Like I mentioned earlier, for this to be enabled too by default we probably need some more numbers than only SPEC, but also e.g. a bootstrap build, llvm test suite, sqlite, etc. But now that we have this option, we can experiment with this, get more numbers, and also address some other TODOs before we try to enable function specialisation by default. Before committing, can you rephrase the subject "Enabling specializing direct constant", make the description a bit more descriptive, and look at the inlined nits.
I am surprised by this big number of specialisations. I remember seeing only 4 specialisations for SPEC2017int.
Comment Actions Sure.
It's surprising. My codebase is trunk with LTO. And I printed log for every function specialized. So I am pretty sure that I got the right number. |
Well, it is not really surprising, it was documented here as a limitation, so you can change that line. I.e., remove 'integer constants', keep the 'integer ranges'.
In your subject and description, you use the term 'direct constant', but better to keep the same terminology, which I think is 'integer constant'.