This is an archive of the discontinued LLVM Phabricator instance.

[nfc] Refactor SlotIndex::getInstrDistance to better reflect actual functionality
ClosedPublic

Authored by aidengrossman on Sep 6 2022, 5:03 PM.

Details

Summary

This patch refactors SlotIndex::getInstrDistance to
SlotIndex::getApproxInstrDistance to better describe the actual
functionality of this function. This patch also adds in some additional
comments better documenting the assumptions that this function makes to
increase clarity.

Based on discussion on the LLVM Discourse:
https://discourse.llvm.org/t/odd-behavior-in-slotindex-getinstrdistance/64934/5

Diff Detail

Event Timeline

aidengrossman created this revision.Sep 6 2022, 5:03 PM
Herald added a project: Restricted Project. · View Herald TranscriptSep 6 2022, 5:03 PM
aidengrossman requested review of this revision.Sep 6 2022, 5:03 PM
Herald added a project: Restricted Project. · View Herald TranscriptSep 6 2022, 5:03 PM

Minor NFC change to make some of the intricacies of SlotIndex::getInstrDistance more clear so that it's easier for the next person trying to understand it. Based on discussion here.

mtrofin accepted this revision.Sep 6 2022, 5:21 PM
This revision is now accepted and ready to land.Sep 6 2022, 5:21 PM
MatzeB added inline comments.Sep 6 2022, 5:42 PM
llvm/include/llvm/CodeGen/SlotIndexes.h
223

how about getIndexDiff or getIndexDelta?

MatzeB added inline comments.Sep 6 2022, 5:43 PM
llvm/include/llvm/CodeGen/SlotIndexes.h
223

oh sorry, ignore me. This seems to be scaled by Slot_Count... guess "Approximate" is fine then.

MatzeB added inline comments.Sep 6 2022, 5:46 PM
llvm/include/llvm/CodeGen/SlotIndexes.h
219–221

This assumption is also not true after inserting one instruction. You have to insert InstrDist / Slot_Count instructions I believe until you reach the limit (currently 4). And also the 5th insertion would trigger a renumbering making this approximate again...

Updated comment above getApproxInstrDistance to better encapsulate some nuance
in the spacing between SlotIndices.

aidengrossman marked 2 inline comments as done.Sep 6 2022, 7:50 PM
aidengrossman added inline comments.
llvm/include/llvm/CodeGen/SlotIndexes.h
219–221

That's a good point. Thank you.

If I'm understanding everything correctly in insertMachineInstrInMaps, it looks like it will only insert up to (InstrDist / Slot_Count) - 1 instructions before the distance, with the next inserted instruction having a calculated distance of zero and a renumbering being requested. It also looks like the assumption can hold if you insert two instructions in between two normally spaced instructions, and you'll get the correct numbering within those three instructions. I've reworded the statement to talk specifically about the example of calculating the distance between two "normally" (no insertions in between, no renumbering) spaced instructions. Let me know if you think a different statement would better encapsulate some of the nuances and also if my indexing is incorrect.

foad added a comment.Sep 7 2022, 1:55 AM

I would prefer to make the comment a bit vaguer, rather than trying to explain exactly what conditions are required to get dense numbering. How about just adding something like: "Note that if there are gaps in the numbering (which there usually are) then this function will return an over-estimate of the true instruction distance."

Make documentation message for getApproxInstrDistance more vague rather
than trying to list out exact conditions.

aidengrossman marked an inline comment as done.Sep 7 2022, 2:24 AM

Thank you very much for the feedback. I've reworded the message to something akin to your example, talking about relevant conditions rather than all the specific examples that could ever possibly exist.

foad accepted this revision.Sep 7 2022, 5:53 AM

Seems fine to me, thanks.

This revision was landed with ongoing or failed builds.Sep 12 2022, 4:33 PM
This revision was automatically updated to reflect the committed changes.