This is an archive of the discontinued LLVM Phabricator instance.

[FuncSpec] Fix specialisation based on literals
ClosedPublic

Authored by chill on Oct 13 2022, 9:56 AM.

Details

Summary

The FunctionSpecialization pass has support for specialising
functions, which are called with literal arguments. This functionality
is disabled by default and is enabled with the option
-function-specialization-for-literal-constant . There are a few
issues with the implementation, though:

  • even with the default, the pass will still specialise based on floating-point literals
  • even when it's enabled, the pass will specialise only for the i1 type (or i2 if all of the possible 4 values occur, or i3 if all of the possible 8 values occur, etc)

The reason for this is incorrect check of the lattice value of the
function formal parameter. The lattice value is overdefined when the
constant range of the possible arguments is the full set, and this is
the reason for the specialisation to trigger. However, if the set of
the possible arguments is not the full set, that must not prevent the
specialisation.

This patch changes the pass to NOT consider a formal parameter when
specialising a function if the lattice value for that parameter is:

  • unknown or undef
  • a constant
  • a constant range with a single element

on the basis that specialisation is pointless for those cases.

Is also changes the criteria for picking up an actual argument to
specialise if the argument is:

  • a LLVM IR constant
  • has constant lattice value has constantrange lattice value with a single element.

Diff Detail

Event Timeline

chill created this revision.Oct 13 2022, 9:56 AM
Herald added a project: Restricted Project. · View Herald TranscriptOct 13 2022, 9:56 AM
chill requested review of this revision.Oct 13 2022, 9:56 AM
Herald added a project: Restricted Project. · View Herald TranscriptOct 13 2022, 9:56 AM
ChuanqiXu accepted this revision.Oct 13 2022, 7:37 PM

Neat! I really enjoy reading your work. Thanks!

llvm/lib/Transforms/IPO/FunctionSpecialization.cpp
712–715

This change looks not related to this diff? Although it is bad that the tests still pass after deleting this. But I prefer to remain this if this is not intended.

762–775

nit:

This revision is now accepted and ready to land.Oct 13 2022, 7:37 PM
chill added inline comments.Oct 14 2022, 1:24 AM
llvm/lib/Transforms/IPO/FunctionSpecialization.cpp
712–715

Indeed, this change is just a refactoring, I moved it towards the beginning of isArgumentInteresting since it's a more logical place for this test to be.

I don't mind not applying this change here in this patch, I have another one coming that's just refactoring/optimisaiton(?) and I can include it there.

762–775

Ack.

ChuanqiXu added inline comments.Oct 14 2022, 1:28 AM
llvm/lib/Transforms/IPO/FunctionSpecialization.cpp
712–715

Oh, my oversight. Then it should be fine.

Another nit. Can we omit the "func-spec-" prefix from the test names (as well as in D135862 and D135867)? They are already under the relevant directory which makes it obvious to infer what the test is about.

chill added a comment.Oct 14 2022, 2:29 AM

Another nit. Can we omit the "func-spec-" prefix from the test names (as well as in D135862 and D135867)? They are already under the relevant directory which makes it obvious to infer what the test is about.

Agree!

chill updated this revision to Diff 467736.Oct 14 2022, 4:08 AM
chill marked 3 inline comments as done.
This revision was landed with ongoing or failed builds.Oct 26 2022, 2:05 AM
This revision was automatically updated to reflect the committed changes.
chill added a comment.Oct 26 2022, 3:24 AM

There is a build bot failure with the test literal-const.ll. AFAICT, the issue is with the test itself - it's not general enough (or, unlikely, there's a non-determinism in the FunctionSpecialization pass);

chill added a comment.Oct 26 2022, 4:02 AM

For now, temporarily disable the test: https://reviews.llvm.org/D136755

chill added a comment.Oct 26 2022, 4:21 AM

-DLLVM_REVERSE_ITERATION:BOOL=ON

Our cloned function names depend in the order we try to specialise the functions, which is the order as obtained by for (Function &F : M) { ...
and if this order is reversed tests that mention cloned function names break.

chill reopened this revision.Oct 26 2022, 5:56 AM
This revision is now accepted and ready to land.Oct 26 2022, 5:56 AM
chill planned changes to this revision.Oct 26 2022, 5:56 AM
chill updated this revision to Diff 470832.Oct 26 2022, 9:19 AM

Updated the test like in D136755

This revision is now accepted and ready to land.Oct 26 2022, 9:19 AM
chill requested review of this revision.Oct 26 2022, 9:21 AM
labrinea added inline comments.Oct 26 2022, 3:20 PM
llvm/test/Transforms/FunctionSpecialization/literal-const.ll
87–93

Maybe add "define" before the specialized function names to make sure we match the definitions?

ChuanqiXu accepted this revision.Oct 26 2022, 7:14 PM

LGTM if @labrinea's comment get addressed.

llvm/test/Transforms/FunctionSpecialization/literal-const.ll
87–93

Maybe add "define" before the specialized function names to make sure we match the definitions?

+1

This revision is now accepted and ready to land.Oct 26 2022, 7:14 PM
chill updated this revision to Diff 471100.Oct 27 2022, 3:06 AM
chill marked 2 inline comments as done.
This revision was landed with ongoing or failed builds.Oct 27 2022, 4:50 AM
This revision was automatically updated to reflect the committed changes.