This is an archive of the discontinued LLVM Phabricator instance.

[IR] Improve SwitchInst API to support prof branch_weights
AbandonedPublic

Authored by yrouban on Apr 12 2019, 3:32 AM.

Details

Summary

This patch adds prof branch_weight as an optional parameter to add/remove and set successor methods.
This way preserves correctness of passes with respect to SwitchInst instructions' perf branch_weights metadata.

One of important assumption is that in a SwitchInst instruction we usually change its metadata first and then its cases. In add/remove methods the branch weights are not changed if their dimension is not equal to the number of successors because this means that the metadata has been changed by other means.

Diff Detail

Event Timeline

yrouban created this revision.Apr 12 2019, 3:32 AM
Herald added a project: Restricted Project. · View Herald TranscriptApr 12 2019, 3:32 AM
Herald added a subscriber: hiraditya. · View Herald Transcript
yrouban marked an inline comment as done.Apr 12 2019, 4:10 AM
yrouban added inline comments.
llvm/lib/IR/Instructions.cpp
3818

must be uint64_t

reames requested changes to this revision.Apr 12 2019, 11:48 AM
reames added a subscriber: reames.

I'm not entirely sure about this interface. I see there's precedent in code for working with the branch weights when removing cases, so I see where you're coming from, but the rest of our interface (e.g. branches) don't have an analogous API.

I think that if we're going to add an API like this, it needs to be uniform across all the terminator instructios. Having it otherwise is just too confusing.

This revision now requires changes to proceed.Apr 12 2019, 11:48 AM

I think that if we're going to add an API like this, it needs to be uniform across all the terminator instructios. Having it otherwise is just too confusing.

Philip, as I see in the doc https://llvm.org/docs/BranchWeightMetadata.html#supported-instructions there are only 4 instructions allowed to have prof branch_weights: BranchInst, SwitchInst, IndirectBrInst and CallInst. CallInst is not a terminator instruction.
I'm proposing API changes for SwitchInst only. Very similar changes can be done to IndirectBrInst.
BranchInst and CallInst have fixed num of branch weight params so they do not need the changes in add/remove methods.
If you agree, I will prepare similar changes for all 4 instructions or just 2 (SwitchInst and IndirectBrInst).
The logic behind these changes is the following. I'd like to keep the prof branch_weights metadata to be kept correct through all passes that do not care about metadata and I do not want to change those passes. They add successors without specifying branch weights and remove successors disregarding the metadata consistency. On the other hand passes can explicitly specify the branch weights along with changed successors.

yrouban updated this revision to Diff 195311.EditedApr 16 2019, 12:46 AM
yrouban edited the summary of this revision. (Show Details)

rebased to the latest D60554 and formatted

yrouban abandoned this revision.May 20 2019, 8:23 PM

See 62122 and its children.