Index: compiler-rt/trunk/lib/profile/InstrProfData.inc =================================================================== --- compiler-rt/trunk/lib/profile/InstrProfData.inc +++ compiler-rt/trunk/lib/profile/InstrProfData.inc @@ -605,6 +605,10 @@ #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 Index: compiler-rt/trunk/lib/profile/InstrProfiling.h =================================================================== --- compiler-rt/trunk/lib/profile/InstrProfiling.h +++ compiler-rt/trunk/lib/profile/InstrProfiling.h @@ -112,9 +112,8 @@ * 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 @@ */ 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 @@ */ 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_ */ Index: compiler-rt/trunk/lib/profile/InstrProfiling.c =================================================================== --- compiler-rt/trunk/lib/profile/InstrProfiling.c +++ compiler-rt/trunk/lib/profile/InstrProfiling.c @@ -20,6 +20,8 @@ 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); Index: compiler-rt/trunk/lib/profile/InstrProfilingFile.c =================================================================== --- compiler-rt/trunk/lib/profile/InstrProfilingFile.c +++ compiler-rt/trunk/lib/profile/InstrProfilingFile.c @@ -330,12 +330,6 @@ if (!FilenamePat) FilenamePat = DefaultProfileName; - /* When -fprofile-instr-generate= 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 @@ * 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 @@ 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 Index: compiler-rt/trunk/test/profile/instrprof-override-filename-then-reset-default.c =================================================================== --- compiler-rt/trunk/test/profile/instrprof-override-filename-then-reset-default.c +++ compiler-rt/trunk/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} Index: compiler-rt/trunk/test/profile/instrprof-override-filename-with-env.c =================================================================== --- compiler-rt/trunk/test/profile/instrprof-override-filename-with-env.c +++ compiler-rt/trunk/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} Index: compiler-rt/trunk/test/profile/instrprof-override-filename.c =================================================================== --- compiler-rt/trunk/test/profile/instrprof-override-filename.c +++ compiler-rt/trunk/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}