2626#include " llvm/Transforms/IPO/PassManagerBuilder.h"
2727
2828#if LLVM_VERSION_GE(4, 0)
29- #include " llvm/Object/ModuleSummaryIndexObjectFile.h"
3029#include " llvm/Transforms/IPO/AlwaysInliner.h"
3130#include " llvm/Transforms/IPO/FunctionImport.h"
3231#include " llvm/Transforms/Utils/FunctionImportUtils.h"
3332#include " llvm/LTO/LTO.h"
33+ #if LLVM_VERSION_LE(4, 0)
34+ #include " llvm/Object/ModuleSummaryIndexObjectFile.h"
35+ #endif
3436#endif
3537
3638#include " llvm-c/Transforms/PassManagerBuilder.h"
@@ -888,6 +890,28 @@ addPreservedGUID(const ModuleSummaryIndex &Index,
888890 return ;
889891 Preserved.insert (GUID);
890892
893+ #if LLVM_VERSION_GE(5, 0)
894+ auto Info = Index.getValueInfo (GUID);
895+ if (!Info) {
896+ return ;
897+ }
898+ for (auto &Summary : Info.getSummaryList ()) {
899+ for (auto &Ref : Summary->refs ()) {
900+ addPreservedGUID (Index, Preserved, Ref.getGUID ());
901+ }
902+
903+ GlobalValueSummary *GVSummary = Summary.get ();
904+ if (isa<FunctionSummary>(GVSummary)) {
905+ FunctionSummary *FS = cast<FunctionSummary>(GVSummary);
906+ for (auto &Call: FS->calls ()) {
907+ addPreservedGUID (Index, Preserved, Call.first .getGUID ());
908+ }
909+ for (auto &GUID: FS->type_tests ()) {
910+ addPreservedGUID (Index, Preserved, GUID);
911+ }
912+ }
913+ }
914+ #else
891915 auto SummaryList = Index.findGlobalValueSummaryList (GUID);
892916 if (SummaryList == Index.end ())
893917 return ;
@@ -919,6 +943,7 @@ addPreservedGUID(const ModuleSummaryIndex &Index,
919943 addPreservedGUID (Index, Preserved, GUID);
920944 }
921945 }
946+ #endif
922947}
923948
924949// The main entry point for creating the global ThinLTO analysis. The structure
@@ -939,6 +964,12 @@ LLVMRustCreateThinLTOData(LLVMRustThinLTOModule *modules,
939964
940965 Ret->ModuleMap [module ->identifier ] = mem_buffer;
941966
967+ #if LLVM_VERSION_GE(5, 0)
968+ if (Error Err = readModuleSummaryIndex (mem_buffer, Ret->Index , i)) {
969+ LLVMRustSetLastError (toString (std::move (Err)).c_str ());
970+ return nullptr ;
971+ }
972+ #else
942973 Expected<std::unique_ptr<object::ModuleSummaryIndexObjectFile>> ObjOrErr =
943974 object::ModuleSummaryIndexObjectFile::create (mem_buffer);
944975 if (!ObjOrErr) {
@@ -947,6 +978,7 @@ LLVMRustCreateThinLTOData(LLVMRustThinLTOModule *modules,
947978 }
948979 auto Index = (*ObjOrErr)->takeIndex ();
949980 Ret->Index .mergeFrom (std::move (Index), i);
981+ #endif
950982 }
951983
952984 // Collect for each module the list of function it defines (GUID -> Summary)
@@ -981,8 +1013,13 @@ LLVMRustCreateThinLTOData(LLVMRustThinLTOModule *modules,
9811013 StringMap<std::map<GlobalValue::GUID, GlobalValue::LinkageTypes>> ResolvedODR;
9821014 DenseMap<GlobalValue::GUID, const GlobalValueSummary *> PrevailingCopy;
9831015 for (auto &I : Ret->Index ) {
1016+ #if LLVM_VERSION_GE(5, 0)
1017+ if (I.second .SummaryList .size () > 1 )
1018+ PrevailingCopy[I.first ] = getFirstDefinitionForLinker (I.second .SummaryList );
1019+ #else
9841020 if (I.second .size () > 1 )
9851021 PrevailingCopy[I.first ] = getFirstDefinitionForLinker (I.second );
1022+ #endif
9861023 }
9871024 auto isPrevailing = [&](GlobalValue::GUID GUID, const GlobalValueSummary *S) {
9881025 const auto &Prevailing = PrevailingCopy.find (GUID);
0 commit comments