Skip to content

Commit c74f290

Browse files
committed
Merge remote-tracking branch 'llvm.org/master' into upstream-with-swift
Conflicts: lib/profile/InstrProfilingFile.c Cause: <rdar://problem/24098975> Code coverage info isn't written out when an app is terminated with a unix signal apple-llvm-split-commit: 3d2f66cb42c0a931e952c8e3de3afe12aa66db89 apple-llvm-split-dir: compiler-rt/
2 parents 960ed01 + 408f50d commit c74f290

File tree

8 files changed

+114
-21
lines changed

8 files changed

+114
-21
lines changed

compiler-rt/lib/profile/InstrProfiling.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,14 @@ void __llvm_profile_initialize_file(void);
170170
*/
171171
const char *__llvm_profile_get_path_prefix();
172172

173+
/*!
174+
* \brief Return filename (including path) of the profile data. Note that if the
175+
* user calls __llvm_profile_set_filename later after invoking this interface,
176+
* the actual file name may differ from what is returned here.
177+
* Side-effect: this API call will invoke malloc with dynamic memory allocation.
178+
*/
179+
const char *__llvm_profile_get_filename();
180+
173181
/*! \brief Get the magic token for the file format. */
174182
uint64_t __llvm_profile_get_magic(void);
175183

compiler-rt/lib/profile/InstrProfilingFile.c

Lines changed: 48 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ typedef struct lprofFilename {
7171
* by runtime. */
7272
unsigned OwnsFilenamePat;
7373
const char *ProfilePathPrefix;
74+
const char *Filename;
7475
char PidChars[MAX_PID_SIZE];
7576
char Hostname[COMPILER_RT_MAX_HOSTLEN];
7677
unsigned NumPids;
@@ -88,11 +89,12 @@ typedef struct lprofFilename {
8889
ProfileNameSpecifier PNS;
8990
} lprofFilename;
9091

91-
COMPILER_RT_WEAK lprofFilename lprofCurFilename = {
92-
0, 0, 0, {0}, {0}, 0, 0, 0, {0}, 0, PNS_unknown};
92+
COMPILER_RT_WEAK lprofFilename lprofCurFilename = {0, 0, 0, 0, {0},
93+
{0}, 0, 0, 0, {0}, 0,
94+
PNS_unknown};
9395

9496
static int getCurFilenameLength();
95-
static const char *getCurFilename(char *FilenameBuf);
97+
static const char *getCurFilename(char *FilenameBuf, int ForceUseBuf);
9698
static unsigned doMerging() { return lprofCurFilename.MergePoolSize; }
9799

98100
/* Return 1 if there is an error, otherwise return 0. */
@@ -270,7 +272,7 @@ static void truncateCurrentFile(void) {
270272

271273
Length = getCurFilenameLength();
272274
FilenameBuf = (char *)COMPILER_RT_ALLOCA(Length + 1);
273-
Filename = getCurFilename(FilenameBuf);
275+
Filename = getCurFilename(FilenameBuf, 0);
274276
if (!Filename)
275277
return;
276278

@@ -340,9 +342,12 @@ static int parseFilenamePattern(const char *FilenamePat,
340342
int MergingEnabled = 0;
341343
char SignalNo;
342344

343-
/* Clean up cached prefix. */
345+
/* Clean up cached prefix and filename. */
344346
if (lprofCurFilename.ProfilePathPrefix)
345347
free((void *)lprofCurFilename.ProfilePathPrefix);
348+
if (lprofCurFilename.Filename)
349+
free((void *)lprofCurFilename.Filename);
350+
346351
memset(&lprofCurFilename, 0, sizeof(lprofCurFilename));
347352

348353
if (lprofCurFilename.FilenamePat && lprofCurFilename.OwnsFilenamePat) {
@@ -480,17 +485,25 @@ static int getCurFilenameLength() {
480485
/* Return the pointer to the current profile file name (after substituting
481486
* PIDs and Hostnames in filename pattern. \p FilenameBuf is the buffer
482487
* to store the resulting filename. If no substitution is needed, the
483-
* current filename pattern string is directly returned. */
484-
static const char *getCurFilename(char *FilenameBuf) {
485-
int I, J, PidLength, HostNameLength;
488+
* current filename pattern string is directly returned, unless ForceUseBuf
489+
* is enabled. */
490+
static const char *getCurFilename(char *FilenameBuf, int ForceUseBuf) {
491+
int I, J, PidLength, HostNameLength, FilenamePatLength;
486492
const char *FilenamePat = lprofCurFilename.FilenamePat;
487493

488494
if (!lprofCurFilename.FilenamePat || !lprofCurFilename.FilenamePat[0])
489495
return 0;
490496

491497
if (!(lprofCurFilename.NumPids || lprofCurFilename.NumHosts ||
492-
lprofCurFilename.MergePoolSize || lprofCurFilename.NumExitSignals))
493-
return lprofCurFilename.FilenamePat;
498+
lprofCurFilename.MergePoolSize || lprofCurFilename.NumExitSignals)) {
499+
if (!ForceUseBuf)
500+
return lprofCurFilename.FilenamePat;
501+
502+
FilenamePatLength = strlen(lprofCurFilename.FilenamePat);
503+
memcpy(FilenameBuf, lprofCurFilename.FilenamePat, FilenamePatLength);
504+
FilenameBuf[FilenamePatLength] = '\0';
505+
return FilenameBuf;
506+
}
494507

495508
PidLength = strlen(lprofCurFilename.PidChars);
496509
HostNameLength = strlen(lprofCurFilename.Hostname);
@@ -547,7 +560,7 @@ const char *__llvm_profile_get_path_prefix(void) {
547560

548561
Length = getCurFilenameLength();
549562
FilenameBuf = (char *)COMPILER_RT_ALLOCA(Length + 1);
550-
Filename = getCurFilename(FilenameBuf);
563+
Filename = getCurFilename(FilenameBuf, 0);
551564
if (!Filename)
552565
return "\0";
553566

@@ -567,6 +580,29 @@ const char *__llvm_profile_get_path_prefix(void) {
567580
return Prefix;
568581
}
569582

583+
COMPILER_RT_VISIBILITY
584+
const char *__llvm_profile_get_filename(void) {
585+
int Length;
586+
char *FilenameBuf;
587+
const char *Filename;
588+
589+
if (lprofCurFilename.Filename)
590+
return lprofCurFilename.Filename;
591+
592+
Length = getCurFilenameLength();
593+
FilenameBuf = (char *)malloc(Length + 1);
594+
if (!FilenameBuf) {
595+
PROF_ERR("Failed to %s\n", "allocate memory.");
596+
return "\0";
597+
}
598+
Filename = getCurFilename(FilenameBuf, 1);
599+
if (!Filename)
600+
return "\0";
601+
602+
lprofCurFilename.Filename = FilenameBuf;
603+
return FilenameBuf;
604+
}
605+
570606
/* This method is invoked by the runtime initialization hook
571607
* InstrProfilingRuntime.o if it is linked in. Both user specified
572608
* profile path via -fprofile-instr-generate= and LLVM_PROFILE_FILE
@@ -623,7 +659,7 @@ int __llvm_profile_write_file(void) {
623659

624660
Length = getCurFilenameLength();
625661
FilenameBuf = (char *)COMPILER_RT_ALLOCA(Length + 1);
626-
Filename = getCurFilename(FilenameBuf);
662+
Filename = getCurFilename(FilenameBuf, 0);
627663

628664
/* Check the filename. */
629665
if (!Filename) {

compiler-rt/test/cfi/CMakeLists.txt

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,21 @@
11
set(CFI_TESTSUITES)
22

3-
macro (add_cfi_test_suites lld thinlto)
3+
macro (add_cfi_test_suites lld thinlto newpm)
44
set(suffix)
55
if (${lld})
66
set(suffix ${suffix}-lld)
77
endif()
88
if (${thinlto})
99
set(suffix ${suffix}-thinlto)
1010
endif()
11+
if (${newpm})
12+
set(suffix ${suffix}-newpm)
13+
endif()
1114
set(suffix ${suffix}-${CFI_TEST_TARGET_ARCH})
1215

1316
set(CFI_TEST_USE_LLD ${lld})
1417
set(CFI_TEST_USE_THINLTO ${thinlto})
18+
set(CFI_TEST_USE_NEWPM ${newpm})
1519

1620
set(CFI_LIT_TEST_MODE Standalone)
1721
set(CFI_TEST_CONFIG_SUFFIX -standalone${suffix})
@@ -40,16 +44,18 @@ foreach(arch ${CFI_TEST_ARCH})
4044
get_test_cc_for_arch(${arch} CFI_TEST_TARGET_CC CFI_TEST_TARGET_CFLAGS)
4145
if (APPLE)
4246
# FIXME: enable ThinLTO tests after fixing http://llvm.org/pr32741
43-
add_cfi_test_suites(False False)
47+
add_cfi_test_suites(False False False)
4448
elseif(WIN32)
45-
add_cfi_test_suites(True False)
46-
add_cfi_test_suites(True True)
49+
add_cfi_test_suites(True False False)
50+
add_cfi_test_suites(True True False)
4751
else()
48-
add_cfi_test_suites(False False)
49-
add_cfi_test_suites(False True)
52+
add_cfi_test_suites(False False False)
53+
add_cfi_test_suites(False True False)
54+
add_cfi_test_suites(False False True)
55+
add_cfi_test_suites(False True True)
5056
if (COMPILER_RT_HAS_LLD AND NOT arch STREQUAL "i386")
51-
add_cfi_test_suites(True False)
52-
add_cfi_test_suites(True True)
57+
add_cfi_test_suites(True False False)
58+
add_cfi_test_suites(True True False)
5359
endif()
5460
endif()
5561
endforeach()

compiler-rt/test/cfi/lit.site.cfg.in

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ config.target_cflags = "@CFI_TEST_TARGET_CFLAGS@"
77
config.use_lld = @CFI_TEST_USE_LLD@
88
config.use_lto = True # CFI *requires* LTO.
99
config.use_thinlto = @CFI_TEST_USE_THINLTO@
10+
config.use_newpm = @CFI_TEST_USE_NEWPM@
1011

1112
lit_config.load_config(config, "@COMPILER_RT_BINARY_DIR@/test/lit.common.configured")
1213
lit_config.load_config(config, "@CMAKE_CURRENT_SOURCE_DIR@/lit.cfg")

compiler-rt/test/lit.common.cfg

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -319,6 +319,8 @@ if config.lto_supported:
319319
config.lto_flags += ["-flto=thin"]
320320
else:
321321
config.lto_flags += ["-flto"]
322+
if config.use_newpm:
323+
config.lto_flags += ["-fexperimental-new-pass-manager"]
322324

323325
# Ask llvm-config about assertion mode.
324326
try:

compiler-rt/test/lit.common.configured.in

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ set_default("can_symbolize", @CAN_SYMBOLIZE@)
3434
set_default("use_lld", False)
3535
set_default("use_thinlto", False)
3636
set_default("use_lto", config.use_thinlto)
37+
set_default("use_newpm", False)
3738
set_default("android", @ANDROID_PYBOOL@)
3839
config.available_features.add('target-is-%s' % config.target_arch)
3940

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
// Test __llvm_profile_get_filename.
2+
// RUN: %clang_pgogen -O2 -o %t %s
3+
// RUN: %run %t
4+
5+
#include <stdio.h>
6+
#include <string.h>
7+
8+
const char *__llvm_profile_get_filename();
9+
void __llvm_profile_set_filename(const char *);
10+
11+
int main(int argc, const char *argv[]) {
12+
int i;
13+
const char *filename;
14+
const char *new_filename = "/path/to/test.profraw";
15+
16+
filename = __llvm_profile_get_filename();
17+
if (strncmp(filename, "default_", 8)) {
18+
fprintf(stderr,
19+
"Error: got filename %s, expected it to start with 'default_'\n",
20+
filename);
21+
return 1;
22+
}
23+
if (strcmp(filename + strlen(filename) - strlen(".profraw"), ".profraw")) {
24+
fprintf(stderr,
25+
"Error: got filename %s, expected it to end with '.profraw'\n",
26+
filename);
27+
return 1;
28+
}
29+
30+
__llvm_profile_set_filename(new_filename);
31+
filename = __llvm_profile_get_filename();
32+
if (strcmp(filename, new_filename)) {
33+
fprintf(stderr, "Error: got filename %s, expected '%s'\n", filename,
34+
new_filename);
35+
return 1;
36+
}
37+
38+
return 0;
39+
}

compiler-rt/test/profile/instrprof-path.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
#include <string.h>
1313

1414
const char *__llvm_profile_get_path_prefix();
15-
void __llvm_profile_set_filanem(const char*);
15+
void __llvm_profile_set_filename(const char*);
1616

1717
int main(int argc, const char *argv[]) {
1818
int i;

0 commit comments

Comments
 (0)