[llvm] [profcheck][coro] Adding Branch weights PGO in Coroutines Passes (PR #184466)
Chuanqi Xu via llvm-commits
llvm-commits at lists.llvm.org
Sun Mar 8 19:13:43 PDT 2026
================
@@ -1594,6 +1619,30 @@ struct SwitchCoroutineSplitter {
++SuspendIndex;
}
+ // Add the branch weights to the switch instruction.
+ if (!ProfcheckDisableMetadataFixes) {
+ if (Switch->getNumCases() > 0) {
+ SmallVector<uint32_t> Weights;
+ // Add the Unlikely weight for the default case.
+ Weights.push_back(llvm::MDBuilder::kUnlikelyBranchWeight);
+ size_t NumCases = Switch->getNumCases();
+ if (NumCases == 1) {
+ Weights.push_back(llvm::MDBuilder::kUnlikelyBranchWeight);
+ } else {
+ // First case is unlikely.
+ Weights.push_back(llvm::MDBuilder::kUnlikelyBranchWeight);
+ // Cases between first and last are likely.
+ for (size_t i = 1; i < NumCases - 1; ++i)
+ Weights.push_back(llvm::MDBuilder::kLikelyBranchWeight);
+ // The last case is unlikely.
+ Weights.push_back(llvm::MDBuilder::kUnlikelyBranchWeight);
+ }
+ MDBuilder MDB(C);
+ Switch->setMetadata(LLVMContext::MD_prof,
+ MDB.createBranchWeights(Weights));
+ }
+ }
----------------
ChuanqiXu9 wrote:
I am still not sure if this is correct.
e.g, we can have a pattern that always destroy the coroutine after initialization. in this case, the destroyer has higher occurance than the other resuming point.
And also, I am wondering if, techniquely, can we have the first suspend point in a loop. For example, we can have an initial_suspend which is not always_suspend, then the first co_await appears in a loop, then the first resuming point may have higher probability.
So I think, we can leave a comment here and not landing this. And let's work it in later patches.
https://github.com/llvm/llvm-project/pull/184466
More information about the llvm-commits
mailing list