PGO: switch to using compiler-rt profile library. (#2527)

Also makes sure that the PGO runtime is available when doing the PGO tests.
This commit is contained in:
Johan Engelen 2018-01-30 20:36:58 +01:00 committed by Martin Kinkelin
parent 6dc59c78e0
commit 80fc2a2f08
18 changed files with 71 additions and 12 deletions

View file

@ -765,12 +765,14 @@ if (LDC_INSTALL_LLVM_RUNTIME_LIBS)
if(APPLE) if(APPLE)
copy_compilerrt_lib("darwin/libclang_rt.asan_osx_dynamic.dylib" "libldc_rt.asan_osx_dynamic.dylib" TRUE) copy_compilerrt_lib("darwin/libclang_rt.asan_osx_dynamic.dylib" "libldc_rt.asan_osx_dynamic.dylib" TRUE)
copy_compilerrt_lib("darwin/libclang_rt.profile_osx.a" "libldc_rt.profile_osx.a" FALSE)
if(NOT (LDC_LLVM_VER LESS 600)) if(NOT (LDC_LLVM_VER LESS 600))
copy_compilerrt_lib("darwin/libclang_rt.fuzzer_osx.a" "libldc_rt.fuzzer_osx.a" FALSE) copy_compilerrt_lib("darwin/libclang_rt.fuzzer_osx.a" "libldc_rt.fuzzer_osx.a" FALSE)
copy_compilerrt_lib("darwin/libclang_rt.xray_osx.a" "libldc_rt.xray_osx.a" FALSE) copy_compilerrt_lib("darwin/libclang_rt.xray_osx.a" "libldc_rt.xray_osx.a" FALSE)
endif() endif()
elseif(UNIX) elseif(UNIX)
copy_compilerrt_lib("linux/libclang_rt.asan-x86_64.a" "libldc_rt.asan-x86_64.a" FALSE) copy_compilerrt_lib("linux/libclang_rt.asan-x86_64.a" "libldc_rt.asan-x86_64.a" FALSE)
copy_compilerrt_lib("linux/libclang_rt.profile-x86_64.a" "libldc_rt.profile-x86_64.a" FALSE)
if(NOT (LDC_LLVM_VER LESS 500)) if(NOT (LDC_LLVM_VER LESS 500))
copy_compilerrt_lib("linux/libclang_rt.xray-x86_64.a" "libldc_rt.xray-x86_64.a" FALSE) copy_compilerrt_lib("linux/libclang_rt.xray-x86_64.a" "libldc_rt.xray-x86_64.a" FALSE)
endif() endif()

View file

@ -53,6 +53,7 @@ private:
virtual void addASanLinkFlags(const llvm::Triple &triple); virtual void addASanLinkFlags(const llvm::Triple &triple);
virtual void addFuzzLinkFlags(const llvm::Triple &triple); virtual void addFuzzLinkFlags(const llvm::Triple &triple);
virtual void addCppStdlibLinkFlags(const llvm::Triple &triple); virtual void addCppStdlibLinkFlags(const llvm::Triple &triple);
virtual void addProfileRuntimeLinkFlags(const llvm::Triple &triple);
virtual void addXRayLinkFlags(const llvm::Triple &triple); virtual void addXRayLinkFlags(const llvm::Triple &triple);
virtual void addLinker(); virtual void addLinker();
@ -368,6 +369,33 @@ void ArgsBuilder::addCppStdlibLinkFlags(const llvm::Triple &triple) {
} }
} }
// Adds all required link flags for PGO.
void ArgsBuilder::addProfileRuntimeLinkFlags(const llvm::Triple &triple) {
std::string searchPaths[] = {
getFullCompilerRTLibPath(triple, "libldc_rt.profile"),
getFullCompilerRTLibPath(triple, "libclang_rt.profile"),
getFullClangCompilerRTLibPath(triple, "libclang_rt.profile"),
};
#if LDC_LLVM_VER >= 308
if (global.params.targetTriple->isOSLinux()) {
// For Linux, explicitly define __llvm_profile_runtime as undefined
// symbol, so that the initialization part of profile-rt is linked in.
addLdFlag("-u", llvm::getInstrProfRuntimeHookVarName());
}
#endif
for (const auto &filepath : searchPaths) {
IF_LOG Logger::println("Searching profile runtime: %s", filepath.c_str());
if (llvm::sys::fs::exists(filepath)) {
IF_LOG Logger::println("Found, linking with %s", filepath.c_str());
args.push_back(filepath);
return;
}
}
}
void ArgsBuilder::addSanitizers(const llvm::Triple &triple) { void ArgsBuilder::addSanitizers(const llvm::Triple &triple) {
if (opts::isSanitizerEnabled(opts::AddressSanitizer)) { if (opts::isSanitizerEnabled(opts::AddressSanitizer)) {
addASanLinkFlags(triple); addASanLinkFlags(triple);
@ -401,14 +429,7 @@ void ArgsBuilder::build(llvm::StringRef outputPath,
// Link with profile-rt library when generating an instrumented binary. // Link with profile-rt library when generating an instrumented binary.
if (opts::isInstrumentingForPGO()) { if (opts::isInstrumentingForPGO()) {
#if LDC_LLVM_VER >= 308 addProfileRuntimeLinkFlags(*global.params.targetTriple);
if (global.params.targetTriple->isOSLinux()) {
// For Linux, explicitly define __llvm_profile_runtime as undefined
// symbol, so that the initialization part of profile-rt is linked in.
addLdFlag("-u", llvm::getInstrProfRuntimeHookVarName());
}
#endif
args.push_back("-lldc-profile-rt");
} }
if (opts::enableDynamicCompile) { if (opts::enableDynamicCompile) {

View file

@ -629,9 +629,10 @@ endmacro()
macro(build_all_runtime_variants d_flags c_flags ld_flags path_suffix outlist_targets) macro(build_all_runtime_variants d_flags c_flags ld_flags path_suffix outlist_targets)
build_runtime_variants("${d_flags}" "${c_flags}" "${ld_flags}" "${path_suffix}" ${outlist_targets}) build_runtime_variants("${d_flags}" "${c_flags}" "${ld_flags}" "${path_suffix}" ${outlist_targets})
# static profile-rt # static profile-rt (currently only needed on Windows)
build_profile_runtime("${c_flags}" "${ld_flags}" "" "${path_suffix}" ${outlist_targets}) if("${TARGET_SYSTEM}" MATCHES "Windows")
get_target_suffix("" "${path_suffix}" target_suffix) build_profile_runtime("${c_flags}" "${ld_flags}" "" "${path_suffix}" ${outlist_targets})
endif()
endmacro() endmacro()
@ -666,7 +667,7 @@ if(MULTILIB AND "${TARGET_SYSTEM}" MATCHES "APPLE")
# KLUDGE: Cannot use `$<TARGET_LINKER_FILE:target>` in custom command. # KLUDGE: Cannot use `$<TARGET_LINKER_FILE:target>` in custom command.
# Set up the list of generated libs (without 'lib' prefix) to be merged manually. # Set up the list of generated libs (without 'lib' prefix) to be merged manually.
set(libs_to_install ldc-profile-rt.a) set(libs_to_install)
if(NOT ${BUILD_SHARED_LIBS} STREQUAL "ON") if(NOT ${BUILD_SHARED_LIBS} STREQUAL "ON")
list(APPEND libs_to_install druntime-ldc.a druntime-ldc-debug.a list(APPEND libs_to_install druntime-ldc.a druntime-ldc-debug.a
phobos2-ldc.a phobos2-ldc-debug.a phobos2-ldc.a phobos2-ldc-debug.a

View file

@ -1,5 +1,7 @@
// Test instrumentation of all 'simple' AST statement types. // Test instrumentation of all 'simple' AST statement types.
// REQUIRES: PGO_RT
// Disable autogenerated boundschecking to declutter the generated code. // Disable autogenerated boundschecking to declutter the generated code.
// See boundscheck.d for boundschecking instrumenation tests. // See boundscheck.d for boundschecking instrumenation tests.

View file

@ -1,5 +1,7 @@
// Test instrumentation of switch with non-constant case expression. // Test instrumentation of switch with non-constant case expression.
// REQUIRES: PGO_RT
// RUN: %ldc -c -output-ll -fprofile-instr-generate -of=%t.ll %s && FileCheck %s --check-prefix=PROFGEN < %t.ll // RUN: %ldc -c -output-ll -fprofile-instr-generate -of=%t.ll %s && FileCheck %s --check-prefix=PROFGEN < %t.ll
// RUN: %ldc -fprofile-instr-generate=%t.profraw -run %s \ // RUN: %ldc -fprofile-instr-generate=%t.profraw -run %s \

View file

@ -1,5 +1,7 @@
// Test calculation of execution counts with loops with break-to-label and continue-to-label. // Test calculation of execution counts with loops with break-to-label and continue-to-label.
// REQUIRES: PGO_RT
// RUN: %ldc -c -output-ll -fprofile-instr-generate -of=%t.ll %s && FileCheck %s --check-prefix=PROFGEN < %t.ll // RUN: %ldc -c -output-ll -fprofile-instr-generate -of=%t.ll %s && FileCheck %s --check-prefix=PROFGEN < %t.ll
// RUN: %ldc -fprofile-instr-generate=%t.profraw -run %s \ // RUN: %ldc -fprofile-instr-generate=%t.profraw -run %s \

View file

@ -1,5 +1,7 @@
// Test instrumentation of statement types involving exceptions. // Test instrumentation of statement types involving exceptions.
// REQUIRES: PGO_RT
// XFAIL: Windows // XFAIL: Windows
// RUN: %ldc -c -output-ll -fprofile-instr-generate -of=%t.ll %s && FileCheck %s --check-prefix=PROFGEN < %t.ll // RUN: %ldc -c -output-ll -fprofile-instr-generate -of=%t.ll %s && FileCheck %s --check-prefix=PROFGEN < %t.ll

View file

@ -3,6 +3,8 @@
// The tests should not test function name mangling, therefore the functionname // The tests should not test function name mangling, therefore the functionname
// matching strings contain regexp wildcards. // matching strings contain regexp wildcards.
// REQUIRES: PGO_RT
// RUN: %ldc -c -output-ll -fprofile-instr-generate -of=%t.ll %s && FileCheck %s --check-prefix=PROFGEN < %t.ll // RUN: %ldc -c -output-ll -fprofile-instr-generate -of=%t.ll %s && FileCheck %s --check-prefix=PROFGEN < %t.ll
// RUN: %ldc -fprofile-instr-generate=%t.profraw -run %s \ // RUN: %ldc -fprofile-instr-generate=%t.profraw -run %s \

View file

@ -2,6 +2,8 @@
// applied when code has changed. // applied when code has changed.
// The code changes are simulated by version(.) blocks. // The code changes are simulated by version(.) blocks.
// REQUIRES: PGO_RT
// RUN: %ldc -d-version=ProfData -fprofile-instr-generate=%t.profraw -run %s \ // RUN: %ldc -d-version=ProfData -fprofile-instr-generate=%t.profraw -run %s \
// RUN: && %profdata merge %t.profraw -o %t.profdata \ // RUN: && %profdata merge %t.profraw -o %t.profdata \
// RUN: && %ldc -d-version=ProfData -c -output-ll -of=%t2.ll -fprofile-instr-use=%t.profdata %s \ // RUN: && %ldc -d-version=ProfData -c -output-ll -of=%t2.ll -fprofile-instr-use=%t.profdata %s \

View file

@ -1,5 +1,7 @@
// Test instrumentation of indirect calls // Test instrumentation of indirect calls
// REQUIRES: PGO_RT
// REQUIRES: atleast_llvm309 // REQUIRES: atleast_llvm309
// RUN: %ldc -c -output-ll -fprofile-instr-generate -of=%t.ll %s && FileCheck %s --check-prefix=PROFGEN < %t.ll // RUN: %ldc -c -output-ll -fprofile-instr-generate -of=%t.ll %s && FileCheck %s --check-prefix=PROFGEN < %t.ll

View file

@ -1,5 +1,7 @@
// Test instrumentation of indirect calls // Test instrumentation of indirect calls
// REQUIRES: PGO_RT
// REQUIRES: atleast_llvm309 // REQUIRES: atleast_llvm309
// There is an LLVM bug, this test currently errors during LLVM codegen for Windows. // There is an LLVM bug, this test currently errors during LLVM codegen for Windows.

10
tests/PGO/lit.local.cfg Normal file
View file

@ -0,0 +1,10 @@
import os
import platform
import re
# Add "PGO_RT" feature if the runtime library is available
for file in os.listdir(config.ldc2_lib_dir):
m = re.match('.*profile.*', file)
if m is not None:
config.available_features.add('PGO_RT')
continue

View file

@ -1,5 +1,6 @@
// Test that maximum function counts are set correctly (LLVM >= 3.8) // Test that maximum function counts are set correctly (LLVM >= 3.8)
// REQUIRES: PGO_RT
// REQUIRES: atleast_llvm308 // REQUIRES: atleast_llvm308
// For LLVM > 3.8, a summary is emitted, see summary.d // For LLVM > 3.8, a summary is emitted, see summary.d

View file

@ -1,5 +1,7 @@
// Tests runtime profile-rt access. // Tests runtime profile-rt access.
// REQUIRES: PGO_RT
// RUN: %ldc -fprofile-instr-generate=%t.profraw -run %s // RUN: %ldc -fprofile-instr-generate=%t.profraw -run %s
import ldc.profile; import ldc.profile;

View file

@ -1,5 +1,7 @@
// Tests (the availability of) the runtime lib function to reset all profile counters. // Tests (the availability of) the runtime lib function to reset all profile counters.
// REQUIRES: PGO_RT
// RUN: %ldc -fprofile-instr-generate=%t.profraw -run %s \ // RUN: %ldc -fprofile-instr-generate=%t.profraw -run %s \
// RUN: && %profdata merge %t.profraw -o %t.profdata \ // RUN: && %profdata merge %t.profraw -o %t.profdata \
// RUN: && %ldc -c -output-ll -of=%t2.ll -fprofile-instr-use=%t.profdata %s \ // RUN: && %ldc -c -output-ll -of=%t2.ll -fprofile-instr-use=%t.profdata %s \

View file

@ -1,5 +1,7 @@
// Tests simulaneous compilation into singleobj // Tests simulaneous compilation into singleobj
// REQUIRES: PGO_RT
// RUN: %ldc -fprofile-instr-generate=%t.profraw %S/inputs/singleobj_input.d -c -of=%t%obj \ // RUN: %ldc -fprofile-instr-generate=%t.profraw %S/inputs/singleobj_input.d -c -of=%t%obj \
// RUN: && %ldc -fprofile-instr-generate=%t.profraw %t%obj -run %s \ // RUN: && %ldc -fprofile-instr-generate=%t.profraw %t%obj -run %s \
// RUN: && %profdata merge %t.profraw -o %t.profdata \ // RUN: && %profdata merge %t.profraw -o %t.profdata \

View file

@ -1,5 +1,6 @@
// Test that maximum function counts are set correctly (LLVM >= 3.8) // Test that maximum function counts are set correctly (LLVM >= 3.8)
// REQUIRES: atleast_llvm309 // REQUIRES: atleast_llvm309
// REQUIRES: PGO_RT
// RUN: %ldc -fprofile-instr-generate=%t.profraw -run %s \ // RUN: %ldc -fprofile-instr-generate=%t.profraw -run %s \
// RUN: && %profdata merge %t.profraw -o %t.profdata \ // RUN: && %profdata merge %t.profraw -o %t.profdata \

View file

@ -3,6 +3,7 @@
// REQUIRES: atleast_llvm400 // REQUIRES: atleast_llvm400
// REQUIRES: LTO // REQUIRES: LTO
// REQUIRES: PGO_RT
// RUN: %ldc -fprofile-instr-generate=%t.profraw -run %s \ // RUN: %ldc -fprofile-instr-generate=%t.profraw -run %s \
// RUN: && %profdata merge %t.profraw -o %t.profdata \ // RUN: && %profdata merge %t.profraw -o %t.profdata \