From 78709345fb343123020a8615325f3943bd1cb420 Mon Sep 17 00:00:00 2001 From: Steven Wu Date: Thu, 23 Jul 2020 09:06:47 -0700 Subject: [Bitcode] Drop invalid branch_weight in BitcodeReader Summary: If bitcode reader gets an invalid branch weight, drop that from the inputs. This allows us to read the broken modules we generated before the verifier was able to catch this. rdar://64870641 Reviewers: yrouban, t.p.northover, dexonsmith, arphaman, aprantl Reviewed By: aprantl Subscribers: aprantl, hiraditya, jkorous, ributzka, llvm-commits Tags: #llvm Differential Revision: https://reviews.llvm.org/D83699 --- llvm/lib/Bitcode/Reader/BitcodeReader.cpp | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) (limited to 'llvm/lib/Bitcode/Reader/BitcodeReader.cpp') diff --git a/llvm/lib/Bitcode/Reader/BitcodeReader.cpp b/llvm/lib/Bitcode/Reader/BitcodeReader.cpp index 86b8f5c..2c7882b 100644 --- a/llvm/lib/Bitcode/Reader/BitcodeReader.cpp +++ b/llvm/lib/Bitcode/Reader/BitcodeReader.cpp @@ -5394,6 +5394,36 @@ Error BitcodeReader::materialize(GlobalValue *GV) { } } + // "Upgrade" older incorrect branch weights by dropping them. + for (auto &I : instructions(F)) { + if (auto *MD = I.getMetadata(LLVMContext::MD_prof)) { + if (MD->getOperand(0) != nullptr && isa(MD->getOperand(0))) { + MDString *MDS = cast(MD->getOperand(0)); + StringRef ProfName = MDS->getString(); + // Check consistency of !prof branch_weights metadata. + if (!ProfName.equals("branch_weights")) + continue; + unsigned ExpectedNumOperands = 0; + if (BranchInst *BI = dyn_cast(&I)) + ExpectedNumOperands = BI->getNumSuccessors(); + else if (SwitchInst *SI = dyn_cast(&I)) + ExpectedNumOperands = SI->getNumSuccessors(); + else if (isa(&I)) + ExpectedNumOperands = 1; + else if (IndirectBrInst *IBI = dyn_cast(&I)) + ExpectedNumOperands = IBI->getNumDestinations(); + else if (isa(&I)) + ExpectedNumOperands = 2; + else + continue; // ignore and continue. + + // If branch weight doesn't match, just strip branch weight. + if (MD->getNumOperands() != 1 + ExpectedNumOperands) + I.setMetadata(LLVMContext::MD_prof, nullptr); + } + } + } + // Look for functions that rely on old function attribute behavior. UpgradeFunctionAttributes(*F); -- cgit v1.1