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