This is an archive of the discontinued LLVM Phabricator instance.

[FuncSpec] Improve specializing direct constant
Changes PlannedPublic

Authored by duan.db on Dec 22 2021, 12:10 AM.

Details

Summary

I find that FuncSpec pass uses the option function-specialization-for-literal-constant to control whether specialize calls with direct constant.
But the control of this option may not be comprehensive enough.
An example is given as the test case.

Diff Detail

Event Timeline

duan.db created this revision.Dec 22 2021, 12:10 AM
duan.db requested review of this revision.Dec 22 2021, 12:10 AM
Herald added a project: Restricted Project. · View Herald TranscriptDec 22 2021, 12:10 AM

The case should be intended to be forwarded to IPSCCP pass to handle by design. Although I am odd for the design too, I thought Function Specialization should be able to cover IPSCCP once it is mature enough. @SjoerdMeijer may I ask what's the reason and plan for this restriction?

llvm/lib/Transforms/IPO/FunctionSpecialization.cpp
621–622

This might not be accurate. This doesn't just enable literal constant.

Sorry, my description may not be clear.
The original case is :

int f(){...}
int main() {
for(int i=0; i<100;i++)
  {
      f(0,i);
      f(i,13);
  }
}

ipsccp cannot optimize case like this. @ChuanqiXu

Could we observe any performance/code size/compile time change on SPEC after this patch?

SjoerdMeijer added a comment.EditedDec 22 2021, 1:44 AM

Just a general comment.
I am not surprised this doesn't work. The main goal of the work thus far has been to support a very specific use-case: promote indirect calls to direct calls, i.e. specialise on arguments that are function pointers. The main benefit we get out of this is inlining later on. The cost-model is tuned to this: it calculates the inlining cost/benefits.

Inlining "direct constants" is a very different use case from that. Nothing wrong with that, but again, it's just different. :-)
The main concern with this different use-case is:

  • the cost model is tuned for inlining,
  • and as as result, allowing specialisation for other cases is going to lead to more specialisations, resulting in increase compile-times which will be the main obstacle to get this pass enabled by default.

That's the reason we have this:

// TODO: This needs checking to see the impact on compile-times, which is why
// this is off by default for now.
static cl::opt<bool> EnableSpecializationForLiteralConstant(

Adding support for literal constants is probably the easy part. I.e., somewhere some logic needs a bit of changing, but the real question is what the cost-model is going to be? So, if we want to support literal constants, I would rather start some work on thinking what this cost-model could be. On the other hand, I also didn't want to stop any work or experimentation in this direction, so that's why I pushed for this option and it to be off by default.

duan.db planned changes to this revision.Dec 23 2021, 6:12 PM
ormris removed a subscriber: ormris.Jan 18 2022, 4:25 PM