[clang] [lld] [llvm] [LLVM][WebAssembly] Implement branch hinting proposal (PR #146230)
Lukas Döllerer via llvm-commits
llvm-commits at lists.llvm.org
Sun Jun 29 15:04:28 PDT 2025
================
@@ -697,6 +738,34 @@ void WebAssemblyAsmPrinter::emitInstruction(const MachineInstr *MI) {
WebAssemblyMCInstLower MCInstLowering(OutContext, *this);
MCInst TmpInst;
MCInstLowering.lower(MI, TmpInst);
+ if (Subtarget->hasBranchHinting() &&
+ MI->getOpcode() == WebAssembly::BR_IF && MFI &&
+ MFI->BranchProbabilities.contains(MI)) {
+ MCSymbol *BrIfSym = OutContext.createTempSymbol();
+ OutStreamer->emitLabel(BrIfSym);
+
+ constexpr uint8_t HintLikely = 0x01;
+ constexpr uint8_t HintUnlikely = 0x00;
+ const BranchProbability &Prob = MFI->BranchProbabilities[MI];
+ uint8_t HintValue;
+ if (Prob > BranchProbability::getRaw(WasmHighBranchProb.getValue()))
+ HintValue = HintLikely;
+ else if (Prob <= BranchProbability::getRaw(WasmLowBranchProb.getValue()))
+ HintValue = HintUnlikely;
+ else
+ goto emit; // Don't emit branch hint between thresholds
+
+ // we know that we only emit branch hints for internal functions,
+ // therefore we can directly cast and don't need getMCSymbolForFunction
+ MCSymbol *FuncSym = cast<MCSymbolWasm>(getSymbol(&MF->getFunction()));
+ uint32_t LocalFuncIdx = MF->getFunctionNumber();
+ if (branchHints.size() <= LocalFuncIdx) {
----------------
Lukasdoe wrote:
Please check whether I understood correctly what you meant here. The resizing is now done once for each function.
https://github.com/llvm/llvm-project/pull/146230
More information about the llvm-commits
mailing list