diff options
7 files changed, 41 insertions, 45 deletions
diff --git a/compiler-rt/lib/profile/InstrProfData.inc b/compiler-rt/lib/profile/InstrProfData.inc index 4138e18fa22..cc2638cc624 100644 --- a/compiler-rt/lib/profile/InstrProfData.inc +++ b/compiler-rt/lib/profile/InstrProfData.inc @@ -605,6 +605,10 @@ serializeValueProfDataFrom(ValueProfRecordClosure *Closure, #define VARIANT_MASK_IR_PROF (0x1ULL << 56) #define IR_LEVEL_PROF_VERSION_VAR __llvm_profile_raw_version +/* The variable that holds the name of the profile data + * specified via command line. */ +#define INSTR_PROF_PROFILE_NAME_VAR __llvm_profile_filename + /* Runtime section names and name strings. */ #define INSTR_PROF_DATA_SECT_NAME __llvm_prf_data #define INSTR_PROF_NAME_SECT_NAME __llvm_prf_names diff --git a/compiler-rt/lib/profile/InstrProfiling.c b/compiler-rt/lib/profile/InstrProfiling.c index c763a44233a..0995137dec4 100644 --- a/compiler-rt/lib/profile/InstrProfiling.c +++ b/compiler-rt/lib/profile/InstrProfiling.c @@ -20,6 +20,8 @@ COMPILER_RT_VISIBILITY char *(*GetEnvHook)(const char *) = 0; COMPILER_RT_WEAK uint64_t __llvm_profile_raw_version = INSTR_PROF_RAW_VERSION; +COMPILER_RT_WEAK char INSTR_PROF_PROFILE_NAME_VAR[1] = {0}; + COMPILER_RT_VISIBILITY uint64_t __llvm_profile_get_magic(void) { return sizeof(void *) == sizeof(uint64_t) ? (INSTR_PROF_RAW_MAGIC_64) : (INSTR_PROF_RAW_MAGIC_32); diff --git a/compiler-rt/lib/profile/InstrProfiling.h b/compiler-rt/lib/profile/InstrProfiling.h index 69abb2c126e..9727573b43b 100644 --- a/compiler-rt/lib/profile/InstrProfiling.h +++ b/compiler-rt/lib/profile/InstrProfiling.h @@ -112,9 +112,8 @@ void INSTR_PROF_VALUE_PROF_FUNC( * Writes to the file with the last name given to \a * * __llvm_profile_set_filename(), * or if it hasn't been called, the \c LLVM_PROFILE_FILE environment variable, - * or if that's not set, the last name given to - * \a __llvm_profile_override_default_filename(), or if that's not set, - * \c "default.profraw". + * or if that's not set, the last name set to INSTR_PROF_PROFILE_NAME_VAR, + * or if that's not set, \c "default.profraw". */ int __llvm_profile_write_file(void); @@ -129,19 +128,6 @@ int __llvm_profile_write_file(void); */ void __llvm_profile_set_filename(const char *Name); -/*! - * \brief Set the filename for writing instrumentation data, unless the - * \c LLVM_PROFILE_FILE environment variable was set. - * - * Unless overridden, sets the filename to be used for subsequent calls to - * \a __llvm_profile_write_file(). - * - * \c Name is not copied, so it must remain valid. Passing NULL resets the - * filename logic to the default behaviour (unless the \c LLVM_PROFILE_FILE - * was set in which case it has no effect). - */ -void __llvm_profile_override_default_filename(const char *Name); - /*! \brief Register to write instrumentation data to file at exit. */ int __llvm_profile_register_write_file_atexit(void); @@ -191,4 +177,11 @@ COMPILER_RT_VISIBILITY extern int __llvm_profile_runtime; */ extern uint64_t __llvm_profile_raw_version; +/*! + * This variable is a weak symbol defined in InstrProfiling.c. It allows + * compiler instrumentation to provide overriding definition with value + * from compiler command line. This variable has default visibility. + */ +extern char INSTR_PROF_PROFILE_NAME_VAR[1]; /* __llvm_profile_filename. */ + #endif /* PROFILE_INSTRPROFILING_H_ */ diff --git a/compiler-rt/lib/profile/InstrProfilingFile.c b/compiler-rt/lib/profile/InstrProfilingFile.c index 1b49d6f42e3..6b4b9cdf6bd 100644 --- a/compiler-rt/lib/profile/InstrProfilingFile.c +++ b/compiler-rt/lib/profile/InstrProfilingFile.c @@ -330,12 +330,6 @@ static void parseAndSetFilename(const char *FilenamePat, if (!FilenamePat) FilenamePat = DefaultProfileName; - /* When -fprofile-instr-generate=<path> is specified on the - * command line, each module will be instrumented with runtime - * init call to __llvm_profile_init function which calls - * __llvm_profile_override_default_filename. In most of the cases, - * the path will be identical, so bypass the parsing completely. - */ if (OldFilenamePat && !strcmp(OldFilenamePat, FilenamePat)) { lprofCurFilename.PNS = PNS; return; @@ -472,10 +466,24 @@ const char *__llvm_profile_get_path_prefix(void) { * environment variable can override this default value. */ COMPILER_RT_VISIBILITY void __llvm_profile_initialize_file(void) { - const char *FilenamePat; + const char *EnvFilenamePat; + const char *SelectedPat = NULL; + ProfileNameSpecifier PNS = PNS_unknown; + int hasCommandLineOverrider = (INSTR_PROF_PROFILE_NAME_VAR[0] != 0); + + EnvFilenamePat = getFilenamePatFromEnv(); + if (EnvFilenamePat) { + SelectedPat = EnvFilenamePat; + PNS = PNS_environment; + } else if (hasCommandLineOverrider) { + SelectedPat = INSTR_PROF_PROFILE_NAME_VAR; + PNS = PNS_command_line; + } else { + SelectedPat = NULL; + PNS = PNS_default; + } - FilenamePat = getFilenamePatFromEnv(); - parseAndSetFilename(FilenamePat, FilenamePat ? PNS_environment : PNS_default); + parseAndSetFilename(SelectedPat, PNS); } /* This API is directly called by the user application code. It has the @@ -487,17 +495,6 @@ void __llvm_profile_set_filename(const char *FilenamePat) { parseAndSetFilename(FilenamePat, PNS_runtime_api); } -/* - * This API is invoked by the global initializers emitted by Clang/LLVM when - * -fprofile-instr-generate=<..> is specified (vs -fprofile-instr-generate - * without an argument). This option has lower precedence than the - * LLVM_PROFILE_FILE environment variable. - */ -COMPILER_RT_VISIBILITY -void __llvm_profile_override_default_filename(const char *FilenamePat) { - parseAndSetFilename(FilenamePat, PNS_command_line); -} - /* The public API for writing profile data into the file with name * set by previous calls to __llvm_profile_set_filename or * __llvm_profile_override_default_filename or diff --git a/compiler-rt/test/profile/instrprof-override-filename-then-reset-default.c b/compiler-rt/test/profile/instrprof-override-filename-then-reset-default.c index 137a3b2f229..3438227213e 100644 --- a/compiler-rt/test/profile/instrprof-override-filename-then-reset-default.c +++ b/compiler-rt/test/profile/instrprof-override-filename-then-reset-default.c @@ -7,13 +7,13 @@ // RUN: %clang_profuse=%t.d/default.profdata -o - -S -emit-llvm %s | FileCheck %s -void __llvm_profile_override_default_filename(const char *); +void __llvm_profile_set_filename(const char *); int main(int argc, const char *argv[]) { // CHECK: br i1 %{{.*}}, label %{{.*}}, label %{{.*}}, !prof ![[PD1:[0-9]+]] if (argc < 2) return 1; - __llvm_profile_override_default_filename(argv[1]); - __llvm_profile_override_default_filename(0); + __llvm_profile_set_filename(argv[1]); + __llvm_profile_set_filename(0); return 0; } // CHECK: ![[PD1]] = !{!"branch_weights", i32 1, i32 2} diff --git a/compiler-rt/test/profile/instrprof-override-filename-with-env.c b/compiler-rt/test/profile/instrprof-override-filename-with-env.c index cce83891663..3f4e5c8d251 100644 --- a/compiler-rt/test/profile/instrprof-override-filename-with-env.c +++ b/compiler-rt/test/profile/instrprof-override-filename-with-env.c @@ -1,14 +1,14 @@ -// RUN: %clang_profgen -o %t -O3 %s +// RUN: %clang_profgen=%t.bad.profraw -o %t -O3 %s // RUN: env LLVM_PROFILE_FILE=%t.good.profraw %run %t %t.bad.profraw // RUN: llvm-profdata merge -o %t.profdata %t.good.profraw // RUN: %clang_profuse=%t.profdata -o - -S -emit-llvm %s | FileCheck %s -void __llvm_profile_override_default_filename(const char *); +void bar () {} int main(int argc, const char *argv[]) { // CHECK: br i1 %{{.*}}, label %{{.*}}, label %{{.*}}, !prof ![[PD1:[0-9]+]] if (argc < 2) return 1; - __llvm_profile_override_default_filename(argv[1]); + bar(); return 0; } // CHECK: ![[PD1]] = !{!"branch_weights", i32 1, i32 2} diff --git a/compiler-rt/test/profile/instrprof-override-filename.c b/compiler-rt/test/profile/instrprof-override-filename.c index 59dea29e3b8..a67c7076afb 100644 --- a/compiler-rt/test/profile/instrprof-override-filename.c +++ b/compiler-rt/test/profile/instrprof-override-filename.c @@ -1,14 +1,14 @@ -// RUN: %clang_profgen -o %t -O3 %s +// RUN: %clang_profgen=%t.profraw -o %t -O3 %s // RUN: %run %t %t.profraw // RUN: llvm-profdata merge -o %t.profdata %t.profraw // RUN: %clang_profuse=%t.profdata -o - -S -emit-llvm %s | FileCheck %s -void __llvm_profile_override_default_filename(const char *); +void bar() {} int main(int argc, const char *argv[]) { // CHECK: br i1 %{{.*}}, label %{{.*}}, label %{{.*}}, !prof ![[PD1:[0-9]+]] if (argc < 2) return 1; - __llvm_profile_override_default_filename(argv[1]); + bar(); return 0; } // CHECK: ![[PD1]] = !{!"branch_weights", i32 1, i32 2} |

