[clang] [lld] [llvm] [LLVM][WebAssembly] Implement branch hinting proposal (PR #146230)
Heejin Ahn via llvm-commits
llvm-commits at lists.llvm.org
Tue Jul 1 22:53:33 PDT 2025
================
@@ -2608,6 +2612,21 @@ void WebAssemblyCFGStackify::rewriteDepthImmediates(MachineFunction &MF) {
Stack.push_back(std::make_pair(&MBB, &MI));
break;
+ case WebAssembly::BR_IF: {
+ // this is the last place where we can easily calculate the branch
+ // probabilities. we do not emit scf-ifs, therefore, only br_ifs have
+ // to be annotated with branch probabilities.
+ if (MF.getSubtarget<WebAssemblySubtarget>().hasBranchHinting() &&
+ MI.getParent()->hasSuccessorProbabilities()) {
+ const auto Prob = MBPI->getEdgeProbability(
+ MI.getParent(), MI.operands().begin()->getMBB());
+ WebAssemblyFunctionInfo *MFI = MF.getInfo<WebAssemblyFunctionInfo>();
+ assert(!MFI->BranchProbabilities.contains(&MI));
+ MFI->BranchProbabilities[&MI] = Prob;
+ }
----------------
aheejin wrote:
1. How often `br_if`s do usually have branch probability data attached? If every `br_if` has it, it may increase in-memory size of `WebAssemblyMachineFunctionInfo`. That may or may not be negligible; I'm just not sure.
2. If you'd like to do this in CFGStackify, I think it is better to create a separate function for it and call it before `rewriteImmediates`, because this task is technically not rewriting immediates.
3. If you do it here, you won't be able to handle `br_unless`, a codegen-only instrution: https://github.com/llvm/llvm-project/blob/2723a6d9928c7ba5d27125e03dff8eaba8661d7f/llvm/lib/Target/WebAssembly/WebAssemblyInstrControl.td#L20-L22
We convert them to `br_if`s in https://github.com/llvm/llvm-project/blob/main/llvm/lib/Target/WebAssembly/WebAssemblyLowerBrUnless.cpp, which runs after CFGStackify. And saving `br_unless`es to `WebAssemblyMachineFunctionInfo` does not work because they will be removed in replaced by newly created `br_if`s in that pass.
https://github.com/llvm/llvm-project/pull/146230
More information about the llvm-commits
mailing list