26
26
#include " llvm/Transforms/IPO/PassManagerBuilder.h"
27
27
28
28
#if LLVM_VERSION_GE(4, 0)
29
- #include " llvm/Object/ModuleSummaryIndexObjectFile.h"
30
29
#include " llvm/Transforms/IPO/AlwaysInliner.h"
31
30
#include " llvm/Transforms/IPO/FunctionImport.h"
32
31
#include " llvm/Transforms/Utils/FunctionImportUtils.h"
33
32
#include " llvm/LTO/LTO.h"
33
+ #if LLVM_VERSION_LE(4, 0)
34
+ #include " llvm/Object/ModuleSummaryIndexObjectFile.h"
35
+ #endif
34
36
#endif
35
37
36
38
#include " llvm-c/Transforms/PassManagerBuilder.h"
@@ -888,6 +890,33 @@ addPreservedGUID(const ModuleSummaryIndex &Index,
888
890
return ;
889
891
Preserved.insert (GUID);
890
892
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
891
920
auto SummaryList = Index.findGlobalValueSummaryList (GUID);
892
921
if (SummaryList == Index.end ())
893
922
return ;
@@ -919,6 +948,7 @@ addPreservedGUID(const ModuleSummaryIndex &Index,
919
948
addPreservedGUID (Index, Preserved, GUID);
920
949
}
921
950
}
951
+ #endif
922
952
}
923
953
924
954
// The main entry point for creating the global ThinLTO analysis. The structure
@@ -939,6 +969,12 @@ LLVMRustCreateThinLTOData(LLVMRustThinLTOModule *modules,
939
969
940
970
Ret->ModuleMap [module->identifier ] = mem_buffer;
941
971
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
942
978
Expected<std::unique_ptr<object::ModuleSummaryIndexObjectFile>> ObjOrErr =
943
979
object::ModuleSummaryIndexObjectFile::create (mem_buffer);
944
980
if (!ObjOrErr) {
@@ -947,6 +983,7 @@ LLVMRustCreateThinLTOData(LLVMRustThinLTOModule *modules,
947
983
}
948
984
auto Index = (*ObjOrErr)->takeIndex ();
949
985
Ret->Index .mergeFrom (std::move (Index), i);
986
+ #endif
950
987
}
951
988
952
989
// Collect for each module the list of function it defines (GUID -> Summary)
@@ -965,6 +1002,15 @@ LLVMRustCreateThinLTOData(LLVMRustThinLTOModule *modules,
965
1002
// combined index
966
1003
//
967
1004
// 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
968
1014
auto DeadSymbols = computeDeadSymbols (Ret->Index , Ret->GUIDPreservedSymbols );
969
1015
ComputeCrossModuleImport (
970
1016
Ret->Index ,
@@ -973,6 +1019,7 @@ LLVMRustCreateThinLTOData(LLVMRustThinLTOModule *modules,
973
1019
Ret->ExportLists ,
974
1020
&DeadSymbols
975
1021
);
1022
+ #endif
976
1023
977
1024
// Resolve LinkOnce/Weak symbols, this has to be computed early be cause it
978
1025
// impacts the caching.
@@ -981,8 +1028,13 @@ LLVMRustCreateThinLTOData(LLVMRustThinLTOModule *modules,
981
1028
StringMap<std::map<GlobalValue::GUID, GlobalValue::LinkageTypes>> ResolvedODR;
982
1029
DenseMap<GlobalValue::GUID, const GlobalValueSummary *> PrevailingCopy;
983
1030
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
984
1035
if (I.second .size () > 1 )
985
1036
PrevailingCopy[I.first ] = getFirstDefinitionForLinker (I.second );
1037
+ #endif
986
1038
}
987
1039
auto isPrevailing = [&](GlobalValue::GUID GUID, const GlobalValueSummary *S) {
988
1040
const auto &Prevailing = PrevailingCopy.find (GUID);
0 commit comments