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,33 @@ 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+ auto *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+ if (isa<AliasSummary>(GVSummary)) {
914+ auto *AS = cast<AliasSummary>(GVSummary);
915+ auto GUID = AS->getAliasee ().getOriginalName ();
916+ addPreservedGUID (Index, Preserved, GUID);
917+ }
918+ }
919+ #else
891920 auto SummaryList = Index.findGlobalValueSummaryList (GUID);
892921 if (SummaryList == Index.end ())
893922 return ;
@@ -919,6 +948,7 @@ addPreservedGUID(const ModuleSummaryIndex &Index,
919948 addPreservedGUID (Index, Preserved, GUID);
920949 }
921950 }
951+ #endif
922952}
923953
924954// The main entry point for creating the global ThinLTO analysis. The structure
@@ -939,6 +969,12 @@ LLVMRustCreateThinLTOData(LLVMRustThinLTOModule *modules,
939969
940970 Ret->ModuleMap [module ->identifier ] = mem_buffer;
941971
972+ #if LLVM_VERSION_GE(5, 0)
973+ if (Error Err = readModuleSummaryIndex (mem_buffer, Ret->Index , i)) {
974+ LLVMRustSetLastError (toString (std::move (Err)).c_str ());
975+ return nullptr ;
976+ }
977+ #else
942978 Expected<std::unique_ptr<object::ModuleSummaryIndexObjectFile>> ObjOrErr =
943979 object::ModuleSummaryIndexObjectFile::create (mem_buffer);
944980 if (!ObjOrErr) {
@@ -947,6 +983,7 @@ LLVMRustCreateThinLTOData(LLVMRustThinLTOModule *modules,
947983 }
948984 auto Index = (*ObjOrErr)->takeIndex ();
949985 Ret->Index .mergeFrom (std::move (Index), i);
986+ #endif
950987 }
951988
952989 // Collect for each module the list of function it defines (GUID -> Summary)
@@ -965,6 +1002,15 @@ LLVMRustCreateThinLTOData(LLVMRustThinLTOModule *modules,
9651002 // combined index
9661003 //
9671004 // This is copied from `lib/LTO/ThinLTOCodeGenerator.cpp`
1005+ #if LLVM_VERSION_GE(5, 0)
1006+ computeDeadSymbols (Ret->Index , Ret->GUIDPreservedSymbols );
1007+ ComputeCrossModuleImport (
1008+ Ret->Index ,
1009+ Ret->ModuleToDefinedGVSummaries ,
1010+ Ret->ImportLists ,
1011+ Ret->ExportLists
1012+ );
1013+ #else
9681014 auto DeadSymbols = computeDeadSymbols (Ret->Index , Ret->GUIDPreservedSymbols );
9691015 ComputeCrossModuleImport (
9701016 Ret->Index ,
@@ -973,6 +1019,7 @@ LLVMRustCreateThinLTOData(LLVMRustThinLTOModule *modules,
9731019 Ret->ExportLists ,
9741020 &DeadSymbols
9751021 );
1022+ #endif
9761023
9771024 // Resolve LinkOnce/Weak symbols, this has to be computed early be cause it
9781025 // impacts the caching.
@@ -981,8 +1028,13 @@ LLVMRustCreateThinLTOData(LLVMRustThinLTOModule *modules,
9811028 StringMap<std::map<GlobalValue::GUID, GlobalValue::LinkageTypes>> ResolvedODR;
9821029 DenseMap<GlobalValue::GUID, const GlobalValueSummary *> PrevailingCopy;
9831030 for (auto &I : Ret->Index ) {
1031+ #if LLVM_VERSION_GE(5, 0)
1032+ if (I.second .SummaryList .size () > 1 )
1033+ PrevailingCopy[I.first ] = getFirstDefinitionForLinker (I.second .SummaryList );
1034+ #else
9841035 if (I.second .size () > 1 )
9851036 PrevailingCopy[I.first ] = getFirstDefinitionForLinker (I.second );
1037+ #endif
9861038 }
9871039 auto isPrevailing = [&](GlobalValue::GUID GUID, const GlobalValueSummary *S) {
9881040 const auto &Prevailing = PrevailingCopy.find (GUID);
0 commit comments