From 0cfea5b73cadfcf408f3549ff209fba4f56f9138 Mon Sep 17 00:00:00 2001 From: Nikita Popov Date: Wed, 23 Jul 2025 09:52:05 +0200 Subject: [BitcodeReader] Avoid quadratic complexity in intrinsic upgrade (#150032) When materializing a function, we'd upgrade all calls to all upgraded intrinsics. However, this would operate on all calls to the intrinsic (including previously materialized ones), which leads to quadratic complexity. Instead, only upgrade the calls that are in the materialized function. This fixes a compile-time regression introduced by #149310. --- llvm/lib/Bitcode/Reader/BitcodeReader.cpp | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) (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 f763683..290d873 100644 --- a/llvm/lib/Bitcode/Reader/BitcodeReader.cpp +++ b/llvm/lib/Bitcode/Reader/BitcodeReader.cpp @@ -7015,13 +7015,6 @@ Error BitcodeReader::materialize(GlobalValue *GV) { if (StripDebugInfo) stripDebugInfo(*F); - // Upgrade any old intrinsic calls in the function. - for (auto &I : UpgradedIntrinsics) { - for (User *U : llvm::make_early_inc_range(I.first->materialized_users())) - if (CallInst *CI = dyn_cast(U)) - UpgradeIntrinsicCall(CI, I.second); - } - // Finish fn->subprogram upgrade for materialized functions. if (DISubprogram *SP = MDLoader->lookupSubprogramForFunction(F)) F->setSubprogram(SP); @@ -7037,7 +7030,7 @@ Error BitcodeReader::materialize(GlobalValue *GV) { } } - for (auto &I : instructions(F)) { + for (auto &I : make_early_inc_range(instructions(F))) { // "Upgrade" older incorrect branch weights by dropping them. if (auto *MD = I.getMetadata(LLVMContext::MD_prof)) { if (MD->getOperand(0) != nullptr && isa(MD->getOperand(0))) { @@ -7068,8 +7061,8 @@ Error BitcodeReader::materialize(GlobalValue *GV) { } } - // Remove incompatible attributes on function calls. if (auto *CI = dyn_cast(&I)) { + // Remove incompatible attributes on function calls. CI->removeRetAttrs(AttributeFuncs::typeIncompatible( CI->getFunctionType()->getReturnType(), CI->getRetAttributes())); @@ -7077,6 +7070,13 @@ Error BitcodeReader::materialize(GlobalValue *GV) { CI->removeParamAttrs(ArgNo, AttributeFuncs::typeIncompatible( CI->getArgOperand(ArgNo)->getType(), CI->getParamAttributes(ArgNo))); + + // Upgrade intrinsics. + if (Function *OldFn = CI->getCalledFunction()) { + auto It = UpgradedIntrinsics.find(OldFn); + if (It != UpgradedIntrinsics.end()) + UpgradeIntrinsicCall(CI, It->second); + } } } -- cgit v1.1