Index: llvm/trunk/lib/Transforms/IPO/FunctionImport.cpp =================================================================== --- llvm/trunk/lib/Transforms/IPO/FunctionImport.cpp +++ llvm/trunk/lib/Transforms/IPO/FunctionImport.cpp @@ -125,6 +125,7 @@ selectCallee(const ModuleSummaryIndex &Index, ArrayRef> CalleeSummaryList, unsigned Threshold, StringRef CallerModulePath) { + // Find the first eligible callee (e.g. legality checks). auto It = llvm::find_if( CalleeSummaryList, [&](const std::unique_ptr &SummaryPtr) { @@ -160,9 +161,6 @@ Summary->modulePath() != CallerModulePath) return false; - if (Summary->instCount() > Threshold) - return false; - if (Summary->notEligibleToImport()) return false; @@ -171,7 +169,19 @@ if (It == CalleeSummaryList.end()) return nullptr; - return cast(It->get()); + // Now check if the first eligible callee is under the instruction + // threshold. Checking this on the first eligible callee ensures that + // we don't end up selecting different callees to import when we invoke + // this routine with different thresholds (when there are multiple copies, + // i.e. with weak or linkonce linkage). + auto *Summary = dyn_cast(It->get()); + if (auto *AS = dyn_cast(It->get())) + Summary = cast(&AS->getAliasee()); + assert(Summary && "Expected FunctionSummary, or alias to one"); + if (Summary->instCount() > Threshold) + return nullptr; + + return Summary; } using EdgeInfo = std::tuple&1 | FileCheck %s --check-prefix=INSTLIMDEFAULT +; INSTLIMDEFAULT-DAG: Is importing function {{.*}} foo from {{.*}}funcimport_resolved1.ll +; INSTLIMDEFAULT-DAG: Is importing function {{.*}} linkonceodrfunc from {{.*}}funcimport_resolved1.ll +; INSTLIMDEFAULT-DAG: Is importing function {{.*}} linkonceodrfunc2 from {{.*}}funcimport_resolved1.ll +; INSTLIMDEFAULT-DAG: Is importing function {{.*}} f from {{.*}}funcimport_resolved1.ll + +; Now run with the lower threshold that will only allow linkonceodrfunc to be +; imported from funcimport_resolved1.ll the second time it is encountered. +; RUN: llvm-lto2 run %t.bc %t2.bc %t3.bc -o %t4 -r=%t.bc,_main,pl -r=%t.bc,_linkonceodrfunc,l -r=%t.bc,_foo,l -r=%t2.bc,_foo,pl -r=%t2.bc,_linkonceodrfunc,pl -r=%t2.bc,_linkonceodrfunc2,pl -r=%t3.bc,_linkonceodrfunc,l -debug-only=function-import -import-instr-limit=8 2>&1 | FileCheck %s --check-prefix=INSTLIM8 +; INSTLIM8-DAG: Is importing function {{.*}} foo from {{.*}}funcimport_resolved1.ll +; INSTLIM8-DAG: Is importing function {{.*}} linkonceodrfunc from {{.*}}funcimport_resolved1.ll +; INSTLIM8-DAG: Not importing function {{.*}} linkonceodrfunc2 from {{.*}}funcimport_resolved1.ll +; INSTLIM8-DAG: Is importing function {{.*}} f from {{.*}}funcimport_resolved1.ll + +target datalayout = "e-m:o-i64:64-f80:128-n8:16:32:64-S128" +target triple = "x86_64-apple-macosx10.11.0" + +define i32 @main() #0 { +entry: + call void (...) @foo() + call void (...) @linkonceodrfunc() + ret i32 0 +} + +declare void @foo(...) #1 +declare void @linkonceodrfunc(...) #1