Index: lib/Transforms/IPO/FunctionImport.cpp =================================================================== --- lib/Transforms/IPO/FunctionImport.cpp +++ lib/Transforms/IPO/FunctionImport.cpp @@ -129,6 +129,21 @@ CalleeSummaryList, [&](const std::unique_ptr &SummaryPtr) { auto *GVSummary = SummaryPtr.get(); + // For SamplePGO, in computeImportForFunction the OriginalId + // may have been used to locate the callee summary list (See + // comment there). + // The mapping from OriginalId to GUID may return a GUID + // that corresponds to a static variable. Filter it out here. + // This can happen when + // 1) There is a call to a library function which is not defined + // in the index. + // 2) There is a static variable with the OriginalGUID identical + // to the GUID of the library function in 1); + // When this happens, the logic for SamplePGO kicks in and + // the static variable in 2) will be found, which needs to be + // filtered out. + if (GVSummary->getSummaryKind() == GlobalValueSummary::GlobalVarKind) + return false; if (GlobalValue::isInterposableLinkage(GVSummary->linkage())) // There is no point in importing these, we can't inline them return false; Index: test/Transforms/FunctionImport/Inputs/funcimport_var2.ll =================================================================== --- test/Transforms/FunctionImport/Inputs/funcimport_var2.ll +++ test/Transforms/FunctionImport/Inputs/funcimport_var2.ll @@ -1,3 +1,6 @@ +target datalayout = "e-m:e-i64:64-f80:128-n8:16:32:64-S128" +target triple = "x86_64-unknown-linux-gnu" + @link = internal global i32 0, align 4 ; Function Attrs: norecurse nounwind readnone uwtable Index: test/Transforms/FunctionImport/funcimport_var.ll =================================================================== --- test/Transforms/FunctionImport/funcimport_var.ll +++ test/Transforms/FunctionImport/funcimport_var.ll @@ -4,6 +4,14 @@ ; RUN: opt -module-summary %p/Inputs/funcimport_var2.ll -o %t2.bc ; RUN: llvm-lto -thinlto -thinlto-action=thinlink -o %t3 %t.bc %t2.bc ; RUN: llvm-lto -thinlto -thinlto-action=import -thinlto-index=%t3 %t.bc %t2.bc +; RUN: llvm-lto2 run %t.bc %t2.bc -o %t.out \ +; RUN: -r %t.bc,_Z4LinkPKcS0_,plx \ +; RUN: -r %t.bc,link,l \ +; RUN: -r %t2.bc,get_link,plx + +target datalayout = "e-m:e-i64:64-f80:128-n8:16:32:64-S128" +target triple = "x86_64-unknown-linux-gnu" + define i32 @_Z4LinkPKcS0_(i8*, i8*) local_unnamed_addr { %3 = tail call i32 @link(i8* %0, i8* %1) #2 ret i32 %3