-
-
Notifications
You must be signed in to change notification settings - Fork 266
/
Copy pathcl_options_instrumentation.cpp
151 lines (130 loc) · 5.77 KB
/
cl_options_instrumentation.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
//===-- driver/cl_options_instrumentation.cpp ------------------*- C++ -*-===//
//
// LDC – the LLVM D compiler
//
// This file is distributed under the BSD-style LDC license. See the LICENSE
// file for details.
//
//===----------------------------------------------------------------------===//
//
// Defines the LDC command line options related to instrumentation, such as PGO
// -finstrument-functions, etc.
// Options for the instrumentation for the sanitizers is not part of this.
//
//===----------------------------------------------------------------------===//
#include "driver/cl_options_instrumentation.h"
#include "dmd/errors.h"
#include "dmd/globals.h"
#include "gen/to_string.h"
#if LDC_LLVM_VER < 1700
#include "llvm/ADT/Triple.h"
#else
#include "llvm/TargetParser/Triple.h"
#endif
namespace {
namespace cl = llvm::cl;
/// Option for generating IR-based PGO instrumentation (LLVM pass)
cl::opt<std::string> IRPGOInstrGenFile(
"fprofile-generate", cl::value_desc("filename"),
cl::desc("Generate instrumented code to collect a runtime "
"profile into default.profraw (overriden by "
"'=<filename>' or LLVM_PROFILE_FILE env var)"),
cl::ZeroOrMore, cl::ValueOptional);
/// Option for using IR-based PGO instrumentation (LLVM pass)
cl::opt<std::string> IRPGOInstrUseFile(
"fprofile-use", cl::ZeroOrMore, cl::value_desc("filename"),
cl::desc("Use instrumentation data for profile-guided optimization"),
cl::ValueRequired);
/// Option for generating frontend-based PGO instrumentation
cl::opt<std::string> ASTPGOInstrGenFile(
"fprofile-instr-generate", cl::value_desc("filename"),
cl::desc("Generate instrumented code to collect a runtime "
"profile into default.profraw (overriden by "
"'=<filename>' or LLVM_PROFILE_FILE env var)"),
cl::ZeroOrMore, cl::ValueOptional);
/// Option for using frontend-based PGO instrumentation
cl::opt<std::string> ASTPGOInstrUseFile(
"fprofile-instr-use", cl::ZeroOrMore, cl::value_desc("filename"),
cl::desc("Use instrumentation data for profile-guided optimization"),
cl::ValueRequired);
/// Option for using sample-based PGO instrumentation
cl::opt<std::string>
SamplePGOInstrUseFile("fprofile-sample-use", cl::Optional,
cl::value_desc("filename"),
cl::desc("Use instrumentation data for sample-based "
"profile guided optimizations"),
cl::ValueRequired);
cl::opt<int> fXRayInstructionThreshold(
"fxray-instruction-threshold", cl::value_desc("value"),
cl::desc("Sets the minimum function size to instrument with XRay"),
cl::init(200), cl::ZeroOrMore, cl::ValueRequired);
} // anonymous namespace
namespace opts {
PGOKind pgoMode = PGO_None;
cl::opt<bool>
instrumentFunctions("finstrument-functions", cl::ZeroOrMore,
cl::desc("Instrument function entry and exit with "
"GCC-compatible profiling calls"));
// DMD-style profiling (`dmd -profile`)
static cl::opt<bool> dmdFunctionTrace(
"fdmd-trace-functions", cl::ZeroOrMore,
cl::desc("DMD-style runtime performance profiling of generated code"));
cl::opt<bool> fXRayInstrument(
"fxray-instrument", cl::ZeroOrMore,
cl::desc("Generate XRay instrumentation sleds on function entry and exit"));
llvm::StringRef getXRayInstructionThresholdString() {
// The instruction threshold is constant during one compiler invoke, so we
// can cache the int->string conversion result.
static std::string thresholdString =
ldc::to_string(fXRayInstructionThreshold);
return thresholdString;
}
cl::opt<CFProtectionType> fCFProtection(
"fcf-protection",
cl::desc("Instrument control-flow architecture protection"), cl::ZeroOrMore,
cl::ValueOptional,
cl::values(clEnumValN(CFProtectionType::None, "none", ""),
clEnumValN(CFProtectionType::Branch, "branch", ""),
clEnumValN(CFProtectionType::Return, "return", ""),
clEnumValN(CFProtectionType::Full, "full", ""),
clEnumValN(CFProtectionType::Full, "",
"") // default to "full" if no argument specified
),
cl::init(CFProtectionType::None));
void initializeInstrumentationOptionsFromCmdline(const llvm::Triple &triple) {
if (ASTPGOInstrGenFile.getNumOccurrences() > 0) {
pgoMode = PGO_ASTBasedInstr;
if (ASTPGOInstrGenFile.empty()) {
// profile-rt provides a default filename by itself
global.params.datafileInstrProf = nullptr;
} else {
global.params.datafileInstrProf = fromPathString(ASTPGOInstrGenFile).ptr;
}
} else if (!ASTPGOInstrUseFile.empty()) {
pgoMode = PGO_ASTBasedUse;
global.params.datafileInstrProf = fromPathString(ASTPGOInstrUseFile).ptr;
} else if (IRPGOInstrGenFile.getNumOccurrences() > 0) {
pgoMode = PGO_IRBasedInstr;
if (IRPGOInstrGenFile.empty()) {
global.params.datafileInstrProf = "default_%m.profraw";
} else {
global.params.datafileInstrProf = fromPathString(IRPGOInstrGenFile).ptr;
}
} else if (!IRPGOInstrUseFile.empty()) {
pgoMode = PGO_IRBasedUse;
global.params.datafileInstrProf = fromPathString(IRPGOInstrUseFile).ptr;
} else if (!SamplePGOInstrUseFile.empty()) {
pgoMode = PGO_SampleBasedUse;
global.params.datafileInstrProf = fromPathString(SamplePGOInstrUseFile).ptr;
}
if (dmdFunctionTrace)
global.params.trace = true;
// fcf-protection is only valid for X86
if (fCFProtection != CFProtectionType::None &&
!(triple.getArch() == llvm::Triple::x86 ||
triple.getArch() == llvm::Triple::x86_64)) {
error(Loc(), "option '--fcf-protection' cannot be specified on this target "
"architecture");
}
}
} // namespace opts