This is an archive of the discontinued LLVM Phabricator instance.

[FuncSpec] Make sure function is actually the callee before trying to specialize.
AcceptedPublic

Authored by duck-37 on Oct 12 2021, 10:37 AM.

Details

Reviewers
SjoerdMeijer
Summary

This causes issues if we start allowing functions with their addresses taken to be specialized.
I intend to submit a patch that does this soon, so this is a blocker at the moment.

Diff Detail

Event Timeline

duck-37 created this revision.Oct 12 2021, 10:37 AM
duck-37 requested review of this revision.Oct 12 2021, 10:37 AM
Herald added a project: Restricted Project. · View Herald TranscriptOct 12 2021, 10:37 AM
SjoerdMeijer added inline comments.Oct 12 2021, 11:27 AM
llvm/lib/Transforms/IPO/FunctionSpecialization.cpp
665

Hmmm, interesting. Can this lead to a miscompute, or other problems? I think we need to add a test to trigger that (which this patch fixes)?

duck-37 added inline comments.Oct 12 2021, 11:36 AM
llvm/lib/Transforms/IPO/FunctionSpecialization.cpp
665

Yes, this can lead to miscompiles and crashes if not handled properly. The problem is that I *believe* this issue only arises in very specific scenarios and only when we start allowing specialization of functions that can get their address taken. Otherwise this is never triggered on problematic functions.

Since allowing that is outside of the scope of this PR, I thought it would be best to add the test for that in a separate patch I'm planning on submitting later.

SjoerdMeijer added inline comments.Oct 13 2021, 1:05 AM
llvm/lib/Transforms/IPO/FunctionSpecialization.cpp
665

I don't think we can or should submit code that isn't covered by tests, so I think that leaves us 2 options here:

  • if we can't trigger this now, we could turn this into an assert.
  • if we can trigger the problematic behaviour in a later patch, then it should be be part of that (and covered with tests there).

These options are not mutually exclusive, we could add an assert now, change that later?

Thanks for contributing to function specialisation by the way, I have also noticed your other patch that I will look at soon. I am happy to commit things on your behalf like the previous one, but you have a few patches in the pipeline and if you plan to do more llvm work, you could consider requesting an account so that you can commit it yourself. I believe the process to get commit rights isn't that difficult and should be quick. But up to you, like I said, am happy to commit things on your behalf.

duck-37 added inline comments.Oct 13 2021, 4:56 AM
llvm/lib/Transforms/IPO/FunctionSpecialization.cpp
665

SGTM.

Thanks for contributing to function specialisation by the way, I have also noticed your other patch that I will look at soon. I am happy to commit things on your behalf like the previous one, but you have a few patches in the pipeline and if you plan to do more llvm work, you could consider requesting an account so that you can commit it yourself. I believe the process to get commit rights isn't that difficult and should be quick. But up to you, like I said, am happy to commit things on your behalf.

I'll look into it. Thanks!

duck-37 updated this revision to Diff 379348.Oct 13 2021, 5:08 AM

Change from if statement to assert.

duck-37 marked an inline comment as done.Oct 13 2021, 5:08 AM
SjoerdMeijer accepted this revision.Oct 13 2021, 6:22 AM

Thanks, LGTM.
(and let me know if I need to commit this on your behalf, but sounds like you'll be looking into getting an account)

This revision is now accepted and ready to land.Oct 13 2021, 6:22 AM
SjoerdMeijer requested changes to this revision.Oct 13 2021, 6:26 AM

Oh wait, sorry, regressions test fail, they trigger this assert. We should assert that CS.getCalledFunction() == F? Also, in asserts it is custom to add a message/diagnostic, so that will be:

assert(CS.getCalledFunction() == F && "function and callee should be same");

or something along those lines.

This revision now requires changes to proceed.Oct 13 2021, 6:26 AM
duck-37 added a comment.EditedOct 13 2021, 6:29 AM

Oh wait, sorry, regressions test fail, they trigger this assert. We should assert that CS.getCalledFunction() == F? Also, in asserts it is custom to add a message/diagnostic, so that will be:

assert(CS.getCalledFunction() == F && "function and callee should be same");

or something along those lines.

You're absolutely right, I'm running on not that much sleep at the moment, sorry!

duck-37 updated this revision to Diff 379361.Oct 13 2021, 6:31 AM

Fix faulty assertion.

SjoerdMeijer accepted this revision.Oct 13 2021, 7:02 AM

Yep, cheers, LGTM

This revision is now accepted and ready to land.Oct 13 2021, 7:02 AM
duck-37 updated this revision to Diff 379428.Oct 13 2021, 9:02 AM

Make clang-format happy.