Index: lib/profile/InstrProfilingFile.c =================================================================== --- lib/profile/InstrProfilingFile.c +++ lib/profile/InstrProfilingFile.c @@ -14,6 +14,9 @@ #include #include #include +#ifndef _MSC_VER +#include +#endif #define UNCONST(ptr) ((void *)(uintptr_t)(ptr)) @@ -118,9 +121,12 @@ static int setFilenamePossiblyWithPid(const char *Filename) { #define MAX_PID_SIZE 16 char PidChars[MAX_PID_SIZE] = {0}; - int NumPids = 0, PidLength = 0; + int NumPids = 0, PidLength = 0, NumNodes = 0, NodeLength = 0; char *Allocated; int I, J; +#ifndef _MSC_VER + struct utsname Name; +#endif /* Reset filename on NULL, except with env var which is checked by caller. */ if (!Filename) { @@ -130,19 +136,34 @@ /* Check the filename for "%p", which indicates a pid-substitution. */ for (I = 0; Filename[I]; ++I) - if (Filename[I] == '%' && Filename[++I] == 'p') - if (!NumPids++) { - PidLength = snprintf(PidChars, MAX_PID_SIZE, "%d", getpid()); - if (PidLength <= 0) - return -1; + if (Filename[I] == '%') { + if (Filename[++I] == 'p') { + if (!NumPids++) { + PidLength = snprintf(PidChars, MAX_PID_SIZE, "%d", getpid()); + if (PidLength <= 0) + return -1; + } + } else if (Filename[I] == 'h') { + if (!NumNodes++) +#ifndef _MSC_VER + if (uname(&Name) == -1) + return -1; + + NodeLength = strlen(Name.nodename); +#endif + if (NodeLength <= 0) + return -1; } - if (!NumPids) { + } + + if (!(NumPids || NumNodes)) { setFilename(Filename, 0); return 0; } /* Allocate enough space for the substituted filename. */ - Allocated = malloc(I + NumPids*(PidLength - 2) + 1); + Allocated = malloc(I + NumPids*(PidLength - 2) + + NumNodes*(NodeLength - 2) + 1); if (!Allocated) return -1; @@ -153,6 +174,12 @@ memcpy(Allocated + J, PidChars, PidLength); J += PidLength; } +#ifndef _MSC_VER + else if (Filename[I] == 'h') { + memcpy(Allocated + J, Name.nodename, NodeLength); + J += NodeLength; + } +#endif /* Drop any unknown substitutions. */ } else Allocated[J++] = Filename[I]; Index: test/profile/instrprof-hostname.c =================================================================== --- /dev/null +++ test/profile/instrprof-hostname.c @@ -0,0 +1,13 @@ +// RUN: %clang_profgen -o %t -O3 %s +// RUN: env LLVM_PROFILE_FILE=%h.%t-%h.profraw_%h %run %t +// RUN: llvm-profdata merge -o %t.profdata `uname -n`.%t-`uname -n`.profraw_`uname -n` +// RUN: %clang_profuse=%t.profdata -o - -S -emit-llvm %s | FileCheck %s +// REQUIRES: shell + +int main(int argc, const char *argv[]) { + // CHECK: br i1 %{{.*}}, label %{{.*}}, label %{{.*}}, !prof ![[PD1:[0-9]+]] + if (argc > 2) + return 1; + return 0; +} +// CHECK: ![[PD1]] = !{!"branch_weights", i32 1, i32 2}