Skip to content

Commit 95ee4bd

Browse files
committed
[XRay] Add -fxray-default-options to pass build-time defined XRay options
This is useful in cases where setting the `XRAY_OPTIONS` environment variable might be difficult. Plus, it's a convenient way to populate XRay options when you always want the instrumentation to be enabled. The implementation follows what memory profiler and PGO did for their profile file name and profile version, respectively: insert a weak linkage global variable carrying the option string, which is then intercepted by compiler-rt during xray's option parsing phase.
1 parent def22f4 commit 95ee4bd

File tree

18 files changed

+183
-1
lines changed

18 files changed

+183
-1
lines changed

clang/include/clang/Basic/CodeGenOptions.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -399,6 +399,10 @@ class CodeGenOptions : public CodeGenOptionsBase {
399399
/// Set of XRay instrumentation kinds to emit.
400400
XRayInstrSet XRayInstrumentationBundle;
401401

402+
/// Default XRay options. Will be overrided by the XRAY_OPTIONS
403+
/// environment variable during run-time.
404+
std::string XRayDefaultOptions;
405+
402406
std::vector<std::string> DefaultFunctionAttrs;
403407

404408
/// List of dynamic shared object files to be loaded as pass plugins.

clang/include/clang/Driver/Options.td

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2882,6 +2882,12 @@ defm xray_instrument : BoolFOption<"xray-instrument",
28822882
"Generate XRay instrumentation sleds on function entry and exit">,
28832883
NegFlag<SetFalse>>;
28842884

2885+
def fxray_default_options_EQ :
2886+
Joined<["-"], "fxray-default-options=">,
2887+
Group<f_Group>, Visibility<[ClangOption, CC1Option]>,
2888+
HelpText<"Default XRay options. Can be overwritten by XRAY_OPTIONS environment variable during run-time.">,
2889+
MarshallingInfoString<CodeGenOpts<"XRayDefaultOptions">>;
2890+
28852891
def fxray_instruction_threshold_EQ :
28862892
Joined<["-"], "fxray-instruction-threshold=">,
28872893
Group<f_Group>, Visibility<[ClangOption, CC1Option]>,

clang/lib/CodeGen/BackendUtil.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@
7777
#include "llvm/Transforms/Instrumentation/SanitizerBinaryMetadata.h"
7878
#include "llvm/Transforms/Instrumentation/SanitizerCoverage.h"
7979
#include "llvm/Transforms/Instrumentation/ThreadSanitizer.h"
80+
#include "llvm/Transforms/Instrumentation/XRayPreparation.h"
8081
#include "llvm/Transforms/ObjCARC.h"
8182
#include "llvm/Transforms/Scalar/EarlyCSE.h"
8283
#include "llvm/Transforms/Scalar/GVN.h"
@@ -1062,6 +1063,13 @@ void EmitAssemblyHelper::RunOptimizationPipeline(
10621063
});
10631064
}
10641065

1066+
if (CodeGenOpts.XRayInstrumentFunctions &&
1067+
!CodeGenOpts.XRayDefaultOptions.empty()) {
1068+
PB.registerOptimizerLastEPCallback(
1069+
[](ModulePassManager &MPM, OptimizationLevel Level,
1070+
ThinOrFullLTOPhase) { MPM.addPass(XRayPreparationPass()); });
1071+
}
1072+
10651073
if (CodeGenOpts.FatLTO) {
10661074
MPM.addPass(PB.buildFatLTODefaultPipeline(
10671075
Level, PrepareForThinLTO,

clang/lib/CodeGen/CodeGenModule.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -448,6 +448,14 @@ CodeGenModule::CodeGenModule(ASTContext &C,
448448
if (Context.getTargetInfo().getTriple().getArch() == llvm::Triple::x86)
449449
getModule().addModuleFlag(llvm::Module::Error, "NumRegisterParameters",
450450
CodeGenOpts.NumRegisterParameters);
451+
452+
// Insert XRay default options if it hasn't been done.
453+
if (CodeGenOpts.XRayInstrumentFunctions &&
454+
!CodeGenOpts.XRayDefaultOptions.empty() &&
455+
!getModule().getModuleFlag("xray-default-opts"))
456+
getModule().addModuleFlag(
457+
llvm::Module::Error, "xray-default-opts",
458+
llvm::MDString::get(LLVMContext, CodeGenOpts.XRayDefaultOptions));
451459
}
452460

453461
CodeGenModule::~CodeGenModule() {}

clang/lib/Driver/XRayArgs.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,9 @@ void XRayArgs::addArgs(const ToolChain &TC, const ArgList &Args,
200200
Args.addOptInFlag(CmdArgs, options::OPT_fxray_shared,
201201
options::OPT_fno_xray_shared);
202202

203+
if (const Arg *A = Args.getLastArg(options::OPT_fxray_default_options_EQ))
204+
A->render(Args, CmdArgs);
205+
203206
if (const Arg *A =
204207
Args.getLastArg(options::OPT_fxray_instruction_threshold_EQ)) {
205208
int Value;
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
// RUN: %clang_cc1 -fxray-instrument -fxray-default-options='patch_premain=true,xray_mode=xray-basic' \
2+
// RUN: -std=c11 -triple x86_64-unknown-unknown -emit-llvm -o - %s | FileCheck %s
3+
4+
void justAFunction() {
5+
}
6+
7+
// CHECK: !{{[0-9]+}} = !{i32 1, !"xray-default-opts", !"patch_premain=true,xray_mode=xray-basic"}

compiler-rt/lib/xray/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ set(XRAY_SOURCES
77
xray_flags.cpp
88
xray_interface.cpp
99
xray_log_interface.cpp
10+
xray_options_var.cpp
1011
xray_utils.cpp
1112
)
1213

compiler-rt/lib/xray/xray_flags.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,10 @@ void initializeFlags() XRAY_NEVER_INSTRUMENT {
6767
const char *XRayCompileFlags = useCompilerDefinedFlags();
6868
XRayParser.ParseString(XRayCompileFlags);
6969

70+
// Override from compile-time options.
71+
if (XRAY_OPTIONS_VAR[0] != 0)
72+
XRayParser.ParseString(XRAY_OPTIONS_VAR);
73+
7074
// Override from environment variables.
7175
XRayParser.ParseStringFromEnv("XRAY_OPTIONS");
7276

compiler-rt/lib/xray/xray_flags.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,12 @@
1717
#include "sanitizer_common/sanitizer_flag_parser.h"
1818
#include "sanitizer_common/sanitizer_internal_defs.h"
1919

20+
#define XRAY_OPTIONS_VAR __llvm_xray_options
21+
22+
extern "C" {
23+
extern char XRAY_OPTIONS_VAR[1];
24+
}
25+
2026
namespace __xray {
2127

2228
struct Flags {
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/*===----- xray_options_var.cpp - XRay option variable setup -------------===*\
2+
|*
3+
|* Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
|* See https://llvm.org/LICENSE.txt for license information.
5+
|* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
|*
7+
\*===----------------------------------------------------------------------===*/
8+
9+
#include "xray_flags.h"
10+
11+
// FIXME: Generalize these. See lib/profile/InstrProfilingPort.h and
12+
// include/profile/InstrProfData.inc
13+
#define COMPILER_RT_VISIBILITY __attribute__((visibility("hidden")))
14+
#define COMPILER_RT_WEAK __attribute__((weak))
15+
16+
extern "C" {
17+
/* char __llvm_xray_options[1]
18+
*
19+
* The runtime should only provide its own definition of this symbol when the
20+
* user has not specified one. Set this up by moving the runtime's copy of this
21+
* symbol to an object file within the archive.
22+
*/
23+
COMPILER_RT_WEAK COMPILER_RT_VISIBILITY char XRAY_OPTIONS_VAR[1] = {0};
24+
}

0 commit comments

Comments
 (0)