[Fuchsia] Rely on linker switch rather than dead code ref for profile runtime

Follow the model used on Linux, where the clang driver passes the
linker a -u switch to force the profile runtime to be linked in,
rather than having every TU emit a dead function with a reference.

Differential Revision: https://reviews.llvm.org/D79835
This commit is contained in:
Petr Hosek 2020-06-04 15:47:05 -07:00
parent e1ab90001a
commit b16ed493dd
7 changed files with 37 additions and 3 deletions

View File

@ -15,6 +15,7 @@
#include "clang/Driver/Options.h" #include "clang/Driver/Options.h"
#include "clang/Driver/SanitizerArgs.h" #include "clang/Driver/SanitizerArgs.h"
#include "llvm/Option/ArgList.h" #include "llvm/Option/ArgList.h"
#include "llvm/ProfileData/InstrProf.h"
#include "llvm/Support/FileSystem.h" #include "llvm/Support/FileSystem.h"
#include "llvm/Support/Path.h" #include "llvm/Support/Path.h"
#include "llvm/Support/VirtualFileSystem.h" #include "llvm/Support/VirtualFileSystem.h"
@ -365,3 +366,13 @@ SanitizerMask Fuchsia::getDefaultSanitizers() const {
} }
return Res; return Res;
} }
void Fuchsia::addProfileRTLibs(const llvm::opt::ArgList &Args,
llvm::opt::ArgStringList &CmdArgs) const {
// Add linker option -u__llvm_profile_runtime to cause runtime
// initialization module to be linked in.
if (needsProfileRT(Args))
CmdArgs.push_back(Args.MakeArgString(
Twine("-u", llvm::getInstrProfRuntimeHookVarName())));
ToolChain::addProfileRTLibs(Args, CmdArgs);
}

View File

@ -69,6 +69,9 @@ public:
SanitizerMask getSupportedSanitizers() const override; SanitizerMask getSupportedSanitizers() const override;
SanitizerMask getDefaultSanitizers() const override; SanitizerMask getDefaultSanitizers() const override;
void addProfileRTLibs(const llvm::opt::ArgList &Args,
llvm::opt::ArgStringList &CmdArgs) const override;
RuntimeLibType RuntimeLibType
GetRuntimeLibType(const llvm::opt::ArgList &Args) const override; GetRuntimeLibType(const llvm::opt::ArgList &Args) const override;
CXXStdlibType CXXStdlibType

View File

@ -242,3 +242,21 @@
// RUN: -gsplit-dwarf -c %s 2>&1 \ // RUN: -gsplit-dwarf -c %s 2>&1 \
// RUN: | FileCheck %s -check-prefix=CHECK-SPLIT-DWARF // RUN: | FileCheck %s -check-prefix=CHECK-SPLIT-DWARF
// CHECK-SPLIT-DWARF: "-split-dwarf-output" "fuchsia.dwo" // CHECK-SPLIT-DWARF: "-split-dwarf-output" "fuchsia.dwo"
// RUN: %clang %s -### --target=aarch64-fuchsia \
// RUN: -fprofile-instr-generate -fcoverage-mapping \
// RUN: -resource-dir=%S/Inputs/resource_dir_with_per_target_subdir \
// RUN: -fuse-ld=lld 2>&1 \
// RUN: | FileCheck %s -check-prefix=CHECK-PROFRT-AARCH64
// CHECK-PROFRT-AARCH64: "-resource-dir" "[[RESOURCE_DIR:[^"]+]]"
// CHECK-PROFRT-AARCH64: "-u__llvm_profile_runtime"
// CHECK-PROFRT-AARCH64: "[[RESOURCE_DIR]]{{/|\\\\}}lib{{/|\\\\}}aarch64-fuchsia{{/|\\\\}}libclang_rt.profile.a"
// RUN: %clang %s -### --target=x86_64-fuchsia \
// RUN: -fprofile-instr-generate -fcoverage-mapping \
// RUN: -resource-dir=%S/Inputs/resource_dir_with_per_target_subdir \
// RUN: -fuse-ld=lld 2>&1 \
// RUN: | FileCheck %s -check-prefix=CHECK-PROFRT-X86_64
// CHECK-PROFRT-X86_64: "-resource-dir" "[[RESOURCE_DIR:[^"]+]]"
// CHECK-PROFRT-X86_64: "-u__llvm_profile_runtime"
// CHECK-PROFRT-X86_64: "[[RESOURCE_DIR]]{{/|\\\\}}lib{{/|\\\\}}x86_64-fuchsia{{/|\\\\}}libclang_rt.profile.a"

View File

@ -1034,9 +1034,9 @@ void InstrProfiling::emitRegistration() {
} }
bool InstrProfiling::emitRuntimeHook() { bool InstrProfiling::emitRuntimeHook() {
// We expect the linker to be invoked with -u<hook_var> flag for linux, // We expect the linker to be invoked with -u<hook_var> flag for Linux or
// for which case there is no need to emit the user function. // Fuchsia, in which case there is no need to emit the user function.
if (TT.isOSLinux()) if (TT.isOSLinux() || TT.isOSFuchsia())
return false; return false;
// If the module's provided its own runtime, we don't need to do anything. // If the module's provided its own runtime, we don't need to do anything.

View File

@ -2,9 +2,11 @@
; RUN: opt < %s -mtriple=x86_64-apple-macosx10.10.0 -instrprof -S | FileCheck %s --check-prefixes=POSIX,MACHO ; RUN: opt < %s -mtriple=x86_64-apple-macosx10.10.0 -instrprof -S | FileCheck %s --check-prefixes=POSIX,MACHO
; RUN: opt < %s -mtriple=x86_64-unknown-linux -instrprof -S | FileCheck %s --check-prefixes=POSIX,LINUX ; RUN: opt < %s -mtriple=x86_64-unknown-linux -instrprof -S | FileCheck %s --check-prefixes=POSIX,LINUX
; RUN: opt < %s -mtriple=x86_64-unknown-fuchsia -instrprof -S | FileCheck %s --check-prefixes=POSIX,LINUX
; RUN: opt < %s -mtriple=x86_64-pc-win32-coff -instrprof -S | FileCheck %s --check-prefixes=COFF ; RUN: opt < %s -mtriple=x86_64-pc-win32-coff -instrprof -S | FileCheck %s --check-prefixes=COFF
; RUN: opt < %s -mtriple=x86_64-apple-macosx10.10.0 -passes=instrprof -S | FileCheck %s --check-prefixes=POSIX,MACHO ; RUN: opt < %s -mtriple=x86_64-apple-macosx10.10.0 -passes=instrprof -S | FileCheck %s --check-prefixes=POSIX,MACHO
; RUN: opt < %s -mtriple=x86_64-unknown-linux -passes=instrprof -S | FileCheck %s --check-prefixes=POSIX,LINUX ; RUN: opt < %s -mtriple=x86_64-unknown-linux -passes=instrprof -S | FileCheck %s --check-prefixes=POSIX,LINUX
; RUN: opt < %s -mtriple=x86_64-unknown-fuchsia -passes=instrprof -S | FileCheck %s --check-prefixes=POSIX,LINUX
; RUN: opt < %s -mtriple=x86_64-pc-win32-coff -passes=instrprof -S | FileCheck %s --check-prefixes=COFF ; RUN: opt < %s -mtriple=x86_64-pc-win32-coff -passes=instrprof -S | FileCheck %s --check-prefixes=COFF
; MACHO: @__llvm_profile_runtime = external global i32 ; MACHO: @__llvm_profile_runtime = external global i32