This is an archive of the discontinued LLVM Phabricator instance.

[BOLT] Fix instrumenting conditional tail calls
ClosedPublic

Authored by Amir on Jul 26 2023, 8:25 PM.

Details

Reviewers
rafauler
maksfb
Group Reviewers
Restricted Project
Commits
rG70e76e0982a9: [BOLT] Fix instrumenting conditional tail calls
Summary

We identify instructions to be instrumented based on Offset annotation.

BOLT "expands" conditional tail calls into a conditional jump to a basic block
with unconditional tail call. Move Offset annotation from former CTC to the tail
call.

For expanded CTC we keep Offset attached to the original instruction which is
converted into a regular conditional jump, while leaving the newly created tail
call without an Offset annotation. This leads to attempting the instrumentation
of the conditional jump which points to the basic block with an inherited input
offset thus creating an invalid edge description. At the same time, the newly
created tail call is skipped entirely which means we're not creating a call
description for it.

If we instead reassign Offset annotation from the conditional jump to the tail
call we fix both issues. The conditional jump will be skipped not creating an
invalid edge description, while tail call will be handled properly (unformly
with regular calls).

Diff Detail

Event Timeline

Amir created this revision.Jul 26 2023, 8:25 PM
Herald added a reviewer: maksfb. · View Herald Transcript
Herald added a project: Restricted Project. · View Herald Transcript
Amir requested review of this revision.Jul 26 2023, 8:25 PM
Herald added a project: Restricted Project. · View Herald TranscriptJul 26 2023, 8:25 PM

Thanks for the quick fix. Can we use instruction annotations instead of IsCondTailCall basic block property for the conditional tail call identification?

Amir added a comment.Jul 27 2023, 3:13 PM
This comment was removed by Amir.
Amir updated this revision to Diff 544953.Jul 27 2023, 3:25 PM

Replace BB->isCondTailCall property with BB->getNumPseudos() == 1 and
BB->getLastNonPseudo is a tail call.

Amir updated this revision to Diff 545015.Jul 27 2023, 9:51 PM

Identify cond tail call blocks by their offset (being equal to parent blocks' input offset)

maksfb added inline comments.Jul 27 2023, 11:36 PM
bolt/lib/Passes/Instrumentation.cpp
103–104 ↗(On Diff #545015)

Can we merge IsInvoke and IsCondTailCall into a single argument, e.g. NeedsInstrumentation?

231–232 ↗(On Diff #545015)

Same args can me merged here too.

396–397 ↗(On Diff #545015)

While this approach works, a more explicit way to identify CTCs will be to mark TC instructions created from CTCs with a proper annotation and check for this annotation during instrumentation.

Amir updated this revision to Diff 545242.Jul 28 2023, 12:13 PM

Move Offset annotation from former CTC to the tail call.

We identify instructions to be instrumented based on Offset annotation.

For expanded CTC we keep Offset attached to the original instruction which is
converted into a regular conditional jump, while leaving the newly created tail
call without an Offset annotation. This leads to attempting the instrumentation
of the conditional jump which points to the basic block with an inherited input
offset thus creating an invalid edge description. At the same time, the newly
created tail call is skipped entirely which means we're not creating a call
description for it.

If we instead reassign Offset annotation from the conditional jump to the tail
call we fix both issues. The conditional jump will be skipped not creating an
invalid edge description, while tail call will be handled properly (unformly
with regular calls).

Amir retitled this revision from [BOLT] Add support for instrumenting conditional tail calls to [BOLT] Fix instrumenting conditional tail calls.Jul 28 2023, 12:16 PM
Amir edited the summary of this revision. (Show Details)
maksfb accepted this revision.Jul 28 2023, 10:58 PM

Nice find!

This revision is now accepted and ready to land.Jul 28 2023, 10:58 PM
This revision was automatically updated to reflect the committed changes.