From 3f186c9cdb2f14a11e97ba211cb2cc673a688c1e Mon Sep 17 00:00:00 2001 From: Manish Kausik H Date: Mon, 13 Oct 2025 09:04:44 +0530 Subject: [PATCH] Remove prof branch weights if switch has 0-1 cases or no branch-weights set in SwitchInstProfUpdateWrapper If the switch statement does not have any cases, or has only one case, or if no weights were set through the ProfUpdate wrapper, such switch statements should not have prof branch weights metadata. And this is what we did prior to commit 240b73e. --- llvm/include/llvm/IR/Instructions.h | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/llvm/include/llvm/IR/Instructions.h b/llvm/include/llvm/IR/Instructions.h index 27930bbc651bd..08f89ea00375c 100644 --- a/llvm/include/llvm/IR/Instructions.h +++ b/llvm/include/llvm/IR/Instructions.h @@ -3548,8 +3548,12 @@ class SwitchInstProfUpdateWrapper { SwitchInstProfUpdateWrapper(SwitchInst &SI) : SI(SI) { init(); } ~SwitchInstProfUpdateWrapper() { - if (Changed && Weights.has_value() && Weights->size() >= 2) - setBranchWeights(SI, Weights.value(), /*IsExpected=*/false); + if (Changed) { + if (Weights.has_value() && Weights->size() >= 2) + setBranchWeights(SI, Weights.value(), /*IsExpected=*/false); + else + SI.setMetadata(LLVMContext::MD_prof, nullptr); + } } /// Delegate the call to the underlying SwitchInst::removeCase() and remove