Skip to content

Commit a6f7596

Browse files
committed
Add PreservedSymbols from LLVM to LTO.
When building with LTO, builtin functions that are defined but whose calls have not been inserted yet, get internalized. We need to prevent these symbols from being internalized at LTO time. Refer to https://reviews.llvm.org/D49434.
1 parent 6762d64 commit a6f7596

File tree

4 files changed

+105
-3
lines changed

4 files changed

+105
-3
lines changed

compiler/rustc_llvm/llvm-wrapper/PassWrapper.cpp

Lines changed: 103 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1120,6 +1120,102 @@ extern "C" void LLVMRustPrintPasses() {
11201120
PB.printPassNames(outs());
11211121
}
11221122

1123+
// from https://github.com/llvm/llvm-project/blob/7021182d6b43de9488ab70de626192ce70b3a4a6/llvm/lib/Object/IRSymtab.cpp#L48-L57
1124+
static const char *PreservedLibcallSymbols[] = {
1125+
#define HANDLE_LIBCALL(code, name) name,
1126+
#include "llvm/IR/RuntimeLibcalls.def"
1127+
#undef HANDLE_LIBCALL
1128+
// RuntimeLibcalls.def missing symbols.
1129+
"__ctzsi2",
1130+
"__ctzdi2",
1131+
"__ctzti2",
1132+
"__ffssi2",
1133+
"__ffsdi2",
1134+
"__ffsti2",
1135+
"__paritysi2",
1136+
"__paritydi2",
1137+
"__parityti2",
1138+
"__popcountsi2",
1139+
"__popcountdi2",
1140+
"__popcountti2",
1141+
"__bswapsi2",
1142+
"__bswapdi2",
1143+
"__negti2",
1144+
"__udivmoddi4",
1145+
"__udivmodti4",
1146+
"__udivmodsi4",
1147+
"__divmodsi4",
1148+
"__divmoddi4",
1149+
"__divmodti4",
1150+
"__absvsi2",
1151+
"__absvdi2",
1152+
"__absvti2",
1153+
"__negvsi2",
1154+
"__negvdi2",
1155+
"__negvti2",
1156+
"__addvsi3",
1157+
"__addvdi3",
1158+
"__addvti3",
1159+
"__subvsi3",
1160+
"__subvdi3",
1161+
"__subvti3",
1162+
"__mulvsi3",
1163+
"__mulvdi3",
1164+
"__mulvti3",
1165+
"__cmpdi2",
1166+
"__cmpti2",
1167+
"__ucmpdi2",
1168+
"__ucmpti2",
1169+
"__mulsc3",
1170+
"__muldc3",
1171+
"__mulxc3",
1172+
"__multc3",
1173+
"__divsc3",
1174+
"__divdc3",
1175+
"__divxc3",
1176+
"__divtc3",
1177+
"__clear_cache",
1178+
"__enable_execute_stack",
1179+
"__gcc_personality_v0",
1180+
"__eprintf",
1181+
"__emutls_get_address",
1182+
"__trampoline_setup",
1183+
"__addsf3vfp",
1184+
"__adddf3vfp",
1185+
"__divsf3vfp",
1186+
"__divdf3vfp",
1187+
"__eqsf2vfp",
1188+
"__eqdf2vfp",
1189+
"__extendsfdf2vfp",
1190+
"__fixdfsivfp",
1191+
"__fixsfsivfp",
1192+
"__fixunssfsivfp",
1193+
"__fixunsdfsivfp",
1194+
"__floatsidfvfp",
1195+
"__floatsisfvfp",
1196+
"__floatunssidfvfp",
1197+
"__floatunssisfvfp",
1198+
"__gedf2vfp",
1199+
"__gesf2vfp",
1200+
"__gtdf2vfp",
1201+
"__gtsf2vfp",
1202+
"__ledf2vfp",
1203+
"__lesf2vfp",
1204+
"__ltdf2vfp",
1205+
"__ltsf2vfp",
1206+
"__muldf3vfp",
1207+
"__mulsf3vfp",
1208+
"__nedf2vfp",
1209+
"__negdf2vfp",
1210+
"__negsf2vfp",
1211+
"__negsf2vfp",
1212+
"__subdf3vfp",
1213+
"__subsf3vfp",
1214+
"__truncdfsf2vfp",
1215+
"__unorddf2vfp",
1216+
"__unordsf2vfp",
1217+
};
1218+
11231219
extern "C" void LLVMRustRunRestrictionPass(LLVMModuleRef M, char **Symbols,
11241220
size_t Len) {
11251221
auto PreserveFunctions = [=](const GlobalValue &GV) {
@@ -1135,7 +1231,7 @@ extern "C" void LLVMRustRunRestrictionPass(LLVMModuleRef M, char **Symbols,
11351231
return true;
11361232
}
11371233
}
1138-
return false;
1234+
return llvm::is_contained(PreservedLibcallSymbols, GV.getName());
11391235
};
11401236

11411237
internalizeModule(*unwrap(M), PreserveFunctions);
@@ -1293,6 +1389,12 @@ LLVMRustCreateThinLTOData(LLVMRustThinLTOModule *modules,
12931389
auto GUID = GlobalValue::getGUID(preserved_symbols[i]);
12941390
Ret->GUIDPreservedSymbols.insert(GUID);
12951391
}
1392+
for (int i = 0; i < sizeof(PreservedLibcallSymbols) / sizeof(PreservedLibcallSymbols[0]); i++) {
1393+
if (auto *PreservedLibcallSymbol = PreservedLibcallSymbols[i]) {
1394+
auto GUID = GlobalValue::getGUID(PreservedLibcallSymbol);
1395+
Ret->GUIDPreservedSymbols.insert(GUID);
1396+
}
1397+
}
12961398

12971399
// Collect the import/export lists for all modules from the call-graph in the
12981400
// combined index

tests/run-make/wasm-spurious-import/main.rs renamed to tests/run-make/wasm-builtins-import/main.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ fn my_panic(_info: &core::panic::PanicInfo) -> ! {
88

99
#[no_mangle]
1010
pub fn multer(a: i128, b: i128) -> i128 {
11-
// Trigger usage of the __multi3 compiler intrinsic which then leads to an imported
12-
// panic function in case of a bug. We verify that no imports exist in our verifier.
11+
// Trigger usage of the __multi3 compiler intrinsic which then leads to an imported function
12+
// such as panic or __multi3 in case of a bug. We verify that no imports exist in our verifier.
1313
a * b
1414
}

0 commit comments

Comments
 (0)