-
Notifications
You must be signed in to change notification settings - Fork 15.6k
[PGO][Offload] Profile profraw generation for GPU instrumentation #76587 #93365
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 61 commits
530eb98
fb067d4
7a0e0ef
fddc079
e9db03c
aa83bd2
e468760
ec18ce9
0872556
94f47f3
62f31d1
0c4bbeb
c7ae2a7
9e66bfb
8bb2207
9f13943
b28d4a9
23d7fe2
0606f0d
23f75b2
c1f9be3
721dac6
6a3ae40
6866862
62a5ee1
052394f
612d5a5
b8c9163
4be80e5
b2fe222
7770b37
f6a1545
92260d8
1dbde8e
1278989
ed2a289
aa895a1
2031e49
5de6082
be6524b
2ba27e8
c754f7f
2b8eb29
67f3009
cee07bc
e8ad132
4c9f814
9cddcf4
53d6309
344e357
2f75142
488cb4a
b90c015
dc90a5c
c68c6e2
ca52c58
0da7627
ee4431a
efe70ad
fb699b6
10e6c48
1d0a961
0ac2d5f
c6b34ad
94ed55b
26f5428
d9e864e
55d673d
fa7589e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -540,6 +540,17 @@ static FILE *getFileObject(const char *OutputName) { | |
| return fopen(OutputName, "ab"); | ||
| } | ||
|
|
||
| static void closeFileObject(FILE *OutputFile) { | ||
| if (OutputFile == getProfileFile()) { | ||
| fflush(OutputFile); | ||
| if (doMerging() && !__llvm_profile_is_continuous_mode_enabled()) { | ||
| lprofUnlockFileHandle(OutputFile); | ||
| } | ||
| } else { | ||
| fclose(OutputFile); | ||
| } | ||
| } | ||
|
|
||
| /* Write profile data to file \c OutputName. */ | ||
| static int writeFile(const char *OutputName) { | ||
| int RetVal; | ||
|
|
@@ -561,15 +572,7 @@ static int writeFile(const char *OutputName) { | |
| initFileWriter(&fileWriter, OutputFile); | ||
| RetVal = lprofWriteData(&fileWriter, lprofGetVPDataReader(), MergeDone); | ||
|
|
||
| if (OutputFile == getProfileFile()) { | ||
| fflush(OutputFile); | ||
| if (doMerging() && !__llvm_profile_is_continuous_mode_enabled()) { | ||
| lprofUnlockFileHandle(OutputFile); | ||
| } | ||
| } else { | ||
| fclose(OutputFile); | ||
| } | ||
|
|
||
| closeFileObject(OutputFile); | ||
| return RetVal; | ||
| } | ||
|
|
||
|
|
@@ -1311,4 +1314,96 @@ COMPILER_RT_VISIBILITY int __llvm_profile_set_file_object(FILE *File, | |
| return 0; | ||
| } | ||
|
|
||
| int __llvm_write_custom_profile(const char *Target, | ||
| const __llvm_profile_data *DataBegin, | ||
| const __llvm_profile_data *DataEnd, | ||
| const char *CountersBegin, | ||
| const char *CountersEnd, const char *NamesBegin, | ||
| const char *NamesEnd) { | ||
| int ReturnValue = 0, FilenameLength, TargetLength; | ||
| char *FilenameBuf, *TargetFilename; | ||
| const char *Filename; | ||
|
|
||
| /* Save old profile data */ | ||
| FILE *oldFile = getProfileFile(); | ||
|
|
||
| // Temporarily suspend getting SIGKILL when the parent exits. | ||
| int PDeathSig = lprofSuspendSigKill(); | ||
|
|
||
| if (lprofProfileDumped() || __llvm_profile_is_continuous_mode_enabled()) { | ||
| PROF_NOTE("Profile data not written to file: %s.\n", "already written"); | ||
| if (PDeathSig == 1) | ||
| lprofRestoreSigKill(); | ||
| return 0; | ||
| } | ||
|
|
||
| /* Get current filename */ | ||
| FilenameLength = getCurFilenameLength(); | ||
| FilenameBuf = (char *)COMPILER_RT_ALLOCA(FilenameLength + 1); | ||
| Filename = getCurFilename(FilenameBuf, 0); | ||
|
|
||
| /* Check the filename. */ | ||
| if (!Filename) { | ||
| PROF_ERR("Failed to write file : %s\n", "Filename not set"); | ||
| if (PDeathSig == 1) | ||
| lprofRestoreSigKill(); | ||
| return -1; | ||
| } | ||
|
|
||
| /* Allocate new space for our target-specific PGO filename */ | ||
| TargetLength = strlen(Target); | ||
| TargetFilename = | ||
| (char *)COMPILER_RT_ALLOCA(FilenameLength + TargetLength + 2); | ||
|
|
||
| /* Prepend "TARGET." to current filename */ | ||
| memcpy(TargetFilename, Target, TargetLength); | ||
| TargetFilename[TargetLength] = '.'; | ||
| memcpy(TargetFilename + 1 + TargetLength, Filename, FilenameLength); | ||
| TargetFilename[FilenameLength + 1 + TargetLength] = 0; | ||
|
|
||
| /* Check if there is llvm/runtime version mismatch. */ | ||
| if (GET_VERSION(__llvm_profile_get_version()) != INSTR_PROF_RAW_VERSION) { | ||
| PROF_ERR("Runtime and instrumentation version mismatch : " | ||
| "expected %d, but get %d\n", | ||
| INSTR_PROF_RAW_VERSION, | ||
| (int)GET_VERSION(__llvm_profile_get_version())); | ||
| if (PDeathSig == 1) | ||
| lprofRestoreSigKill(); | ||
| return -1; | ||
| } | ||
|
||
|
|
||
| /* Open and truncate target-specific PGO file */ | ||
| FILE *OutputFile = fopen(TargetFilename, "w"); | ||
| setProfileFile(OutputFile); | ||
|
|
||
| if (!OutputFile) { | ||
| PROF_ERR("Failed to open file : %s\n", TargetFilename); | ||
| if (PDeathSig == 1) | ||
| lprofRestoreSigKill(); | ||
| return -1; | ||
| } | ||
|
|
||
| FreeHook = &free; | ||
| setupIOBuffer(); | ||
|
|
||
| /* Write custom data */ | ||
| ProfDataWriter fileWriter; | ||
| initFileWriter(&fileWriter, OutputFile); | ||
|
|
||
| /* Write custom data to the file */ | ||
| ReturnValue = lprofWriteDataImpl( | ||
| &fileWriter, DataBegin, DataEnd, CountersBegin, CountersEnd, NULL, NULL, | ||
| lprofGetVPDataReader(), NULL, NULL, NULL, NULL, NamesBegin, NamesEnd, 0); | ||
| closeFileObject(OutputFile); | ||
|
|
||
| // Restore SIGKILL. | ||
| if (PDeathSig == 1) | ||
| lprofRestoreSigKill(); | ||
|
|
||
| /* Restore old profiling file */ | ||
| setProfileFile(oldFile); | ||
|
|
||
| return ReturnValue; | ||
| } | ||
|
|
||
| #endif | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,12 +1,17 @@ | ||
| // RUN: %libomptarget-compile-generic -fprofile-instr-generate \ | ||
| // RUN: -Xclang "-fprofile-instrument=clang" | ||
| // RUN: %libomptarget-run-generic 2>&1 | %fcheck-generic \ | ||
| // RUN: --check-prefix="CLANG-PGO" | ||
| // RUN: %libomptarget-compile-generic -fprofile-generate \ | ||
| // RUN: -Xclang "-fprofile-instrument=llvm" | ||
| // RUN: %libomptarget-run-generic 2>&1 | %fcheck-generic \ | ||
| // RUN: env LLVM_PROFILE_FILE=llvm.profraw %libomptarget-run-generic 2>&1 | ||
| // RUN: llvm-profdata show --all-functions --counts \ | ||
|
||
| // RUN: %target_triple.llvm.profraw | %fcheck-generic \ | ||
| // RUN: --check-prefix="LLVM-PGO" | ||
|
|
||
| // RUN: %libomptarget-compile-generic -fprofile-instr-generate \ | ||
| // RUN: -Xclang "-fprofile-instrument=clang" | ||
| // RUN: env LLVM_PROFILE_FILE=clang.profraw %libomptarget-run-generic 2>&1 | ||
| // RUN: llvm-profdata show --all-functions --counts \ | ||
| // RUN: %target_triple.clang.profraw | %fcheck-generic \ | ||
| // RUN: --check-prefix="CLANG-PGO" | ||
|
|
||
| // REQUIRES: gpu | ||
| // REQUIRES: pgo | ||
|
|
||
|
|
@@ -28,47 +33,35 @@ int main() { | |
| } | ||
| } | ||
|
|
||
| // CLANG-PGO: ======== Counters ========= | ||
| // CLANG-PGO-NEXT: [ 0 11 20 ] | ||
| // CLANG-PGO-NEXT: [ 10 ] | ||
| // CLANG-PGO-NEXT: [ 20 ] | ||
| // CLANG-PGO-NEXT: ========== Data =========== | ||
| // CLANG-PGO-NEXT: { {{[0-9]*}} {{[0-9]*}} | ||
| // CLANG-PGO-SAME: {{0x[0-9a-fA-F]*}} {{0x[0-9a-fA-F]*}} | ||
| // CLANG-PGO-SAME: {{0x[0-9a-fA-F]*}} {{0x[0-9a-fA-F]*}} | ||
| // CLANG-PGO-SAME: {{[0-9]*}} {{[0-9]*}} {{[0-9]*}} } | ||
| // CLANG-PGO-NEXT: { {{[0-9]*}} {{[0-9]*}} | ||
| // CLANG-PGO-SAME: {{0x[0-9a-fA-F]*}} {{0x[0-9a-fA-F]*}} | ||
| // CLANG-PGO-SAME: {{0x[0-9a-fA-F]*}} {{0x[0-9a-fA-F]*}} | ||
| // CLANG-PGO-SAME: {{[0-9]*}} {{[0-9]*}} {{[0-9]*}} } | ||
| // CLANG-PGO-NEXT: { {{[0-9]*}} {{[0-9]*}} | ||
| // CLANG-PGO-SAME: {{0x[0-9a-fA-F]*}} {{0x[0-9a-fA-F]*}} | ||
| // CLANG-PGO-SAME: {{0x[0-9a-fA-F]*}} {{0x[0-9a-fA-F]*}} | ||
| // CLANG-PGO-SAME: {{[0-9]*}} {{[0-9]*}} {{[0-9]*}} } | ||
| // CLANG-PGO-NEXT: ======== Functions ======== | ||
| // CLANG-PGO-NEXT: pgo1.c: | ||
| // CLANG-PGO-SAME: __omp_offloading_{{[_0-9a-zA-Z]*}}_main_{{[_0-9a-zA-Z]*}} | ||
| // CLANG-PGO-NEXT: test1 | ||
| // CLANG-PGO-NEXT: test2 | ||
| // LLVM-PGO-LABEL: __omp_offloading_{{[_0-9a-zA-Z]*}}_main_{{[_0-9a-zA-Z]*}}: | ||
| // LLVM-PGO: Hash: {{0[xX][0-9a-fA-F]+}} | ||
| // LLVM-PGO: Counters: 4 | ||
| // LLVM-PGO: Block counts: [20, 10, 2, 1] | ||
|
|
||
| // LLVM-PGO-LABEL: test1: | ||
| // LLVM-PGO: Hash: {{0[xX][0-9a-fA-F]+}} | ||
| // LLVM-PGO: Counters: 1 | ||
| // LLVM-PGO: Block counts: [10] | ||
|
|
||
| // LLVM-PGO-LABEL: test2: | ||
| // LLVM-PGO: Hash: {{0[xX][0-9a-fA-F]+}} | ||
| // LLVM-PGO: Counters: 1 | ||
| // LLVM-PGO: Block counts: [20] | ||
|
|
||
| // CLANG-PGO-LABEL: __omp_offloading_{{[_0-9a-zA-Z]*}}_main_{{[_0-9a-zA-Z]*}}: | ||
| // CLANG-PGO: Hash: {{0[xX][0-9a-fA-F]+}} | ||
| // CLANG-PGO: Counters: 3 | ||
| // CLANG-PGO: Function count: 0 | ||
| // CLANG-PGO: Block counts: [11, 20] | ||
|
|
||
| // CLANG-PGO-LABEL: test1: | ||
| // CLANG-PGO: Hash: {{0[xX][0-9a-fA-F]+}} | ||
| // CLANG-PGO: Counters: 1 | ||
| // CLANG-PGO: Function count: 10 | ||
| // CLANG-PGO: Block counts: [] | ||
|
|
||
| // LLVM-PGO: ======== Counters ========= | ||
| // LLVM-PGO-NEXT: [ 20 10 2 1 ] | ||
| // LLVM-PGO-NEXT: [ 10 ] | ||
| // LLVM-PGO-NEXT: [ 20 ] | ||
| // LLVM-PGO-NEXT: ========== Data =========== | ||
| // LLVM-PGO-NEXT: { {{[0-9]*}} {{[0-9]*}} | ||
| // LLVM-PGO-SAME: {{0x[0-9a-fA-F]*}} {{0x[0-9a-fA-F]*}} | ||
| // LLVM-PGO-SAME: {{0x[0-9a-fA-F]*}} {{0x[0-9a-fA-F]*}} | ||
| // LLVM-PGO-SAME: {{[0-9]*}} {{[0-9]*}} {{[0-9]*}} } | ||
| // LLVM-PGO-NEXT: { {{[0-9]*}} {{[0-9]*}} | ||
| // LLVM-PGO-SAME: {{0x[0-9a-fA-F]*}} {{0x[0-9a-fA-F]*}} | ||
| // LLVM-PGO-SAME: {{0x[0-9a-fA-F]*}} {{0x[0-9a-fA-F]*}} | ||
| // LLVM-PGO-SAME: {{[0-9]*}} {{[0-9]*}} {{[0-9]*}} } | ||
| // LLVM-PGO-NEXT: { {{[0-9]*}} {{[0-9]*}} | ||
| // LLVM-PGO-SAME: {{0x[0-9a-fA-F]*}} {{0x[0-9a-fA-F]*}} | ||
| // LLVM-PGO-SAME: {{0x[0-9a-fA-F]*}} {{0x[0-9a-fA-F]*}} | ||
| // LLVM-PGO-SAME: {{[0-9]*}} {{[0-9]*}} {{[0-9]*}} } | ||
| // LLVM-PGO-NEXT: ======== Functions ======== | ||
| // LLVM-PGO-NEXT: __omp_offloading_{{[_0-9a-zA-Z]*}}_main_{{[_0-9a-zA-Z]*}} | ||
| // LLVM-PGO-NEXT: test1 | ||
| // LLVM-PGO-NEXT: test2 | ||
| // CLANG-PGO-LABEL: test2: | ||
| // CLANG-PGO: Hash: {{0[xX][0-9a-fA-F]+}} | ||
| // CLANG-PGO: Counters: 1 | ||
| // CLANG-PGO: Function count: 20 | ||
| // CLANG-PGO: Block counts: [] | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we make this handle file names with directory components? Otherwise, I end up with errors like:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Seems to be fixed now. Thanks.