-
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
Merged
EthanLuisMcDonough
merged 69 commits into
llvm:main
from
EthanLuisMcDonough:gpuprofwrite
Feb 12, 2025
Merged
Changes from 68 commits
Commits
Show all changes
69 commits
Select commit
Hold shift + click to select a range
530eb98
Add profiling functions to libomptarget
EthanLuisMcDonough fb067d4
Fix PGO instrumentation for GPU targets
EthanLuisMcDonough 7a0e0ef
Change global visibility on GPU targets
EthanLuisMcDonough fddc079
Make names global public on GPU
EthanLuisMcDonough e9db03c
Read and print GPU device PGO globals
EthanLuisMcDonough aa83bd2
Merge branch 'main' into gpuprof
EthanLuisMcDonough e468760
Fix rebase bug
EthanLuisMcDonough ec18ce9
Refactor portions to be more idiomatic
EthanLuisMcDonough 0872556
Reformat DeviceRTL prof functions
EthanLuisMcDonough 94f47f3
Merge branch 'main' into gpuprof
EthanLuisMcDonough 62f31d1
Style changes + catch name error
EthanLuisMcDonough 0c4bbeb
Add GPU PGO test
EthanLuisMcDonough c7ae2a7
Fix PGO test formatting
EthanLuisMcDonough 9e66bfb
Merge branch 'main' into gpuprof
EthanLuisMcDonough 8bb2207
Refactor visibility logic
EthanLuisMcDonough 9f13943
Add LLVM instrumentation support
EthanLuisMcDonough b28d4a9
Merge branch 'main' into gpuprof
EthanLuisMcDonough 23d7fe2
Merge branch 'main' into gpuprof
EthanLuisMcDonough 0606f0d
Use explicit addrspace instead of unqual
EthanLuisMcDonough 23f75b2
Merge branch 'main' into gpuprof
EthanLuisMcDonough c1f9be3
Remove redundant namespaces
EthanLuisMcDonough 721dac6
Merge branch 'main' into gpuprof
EthanLuisMcDonough 6a3ae40
Clang format
EthanLuisMcDonough 6866862
Use getAddrSpaceCast
EthanLuisMcDonough 62a5ee1
Revert "Use getAddrSpaceCast"
EthanLuisMcDonough 052394f
Revert "Use getAddrSpaceCast"
EthanLuisMcDonough 612d5a5
Write PGO
EthanLuisMcDonough b8c9163
Fix tests
EthanLuisMcDonough 4be80e5
Merge branch 'main' into gpuprof
EthanLuisMcDonough b2fe222
Merge branch 'main' into gpuprofwrite
EthanLuisMcDonough 7770b37
Fix params
EthanLuisMcDonough f6a1545
Merge branch 'main' into gpuprof
EthanLuisMcDonough 92260d8
Merge branch 'main' into gpuprofwrite
EthanLuisMcDonough 1dbde8e
Merge branch 'main' into gpuprof
EthanLuisMcDonough 1278989
Merge branch 'main' into gpuprofwrite
EthanLuisMcDonough ed2a289
Merge branch 'main' into gpuprof_ptrcastfix
EthanLuisMcDonough aa895a1
Fix elf obj file
EthanLuisMcDonough 2031e49
Add more addrspace casts for GPU targets
EthanLuisMcDonough 5de6082
Merge branch 'gpuprof_ptrcastfix' into gpuprofwrite
EthanLuisMcDonough be6524b
Have test read from profraw instead of dump
EthanLuisMcDonough 2ba27e8
Merge branch 'main' into gpuprof
EthanLuisMcDonough c754f7f
Merge branch 'main' into gpuprofwrite
EthanLuisMcDonough 2b8eb29
Fix PGO test format
EthanLuisMcDonough 67f3009
Refactor profile writer
EthanLuisMcDonough cee07bc
Merge branch 'main' into gpuprofwrite
EthanLuisMcDonough e8ad132
Fix refactor bug
EthanLuisMcDonough 4c9f814
Make requested clang-format change
EthanLuisMcDonough 9cddcf4
Merge branch 'main' into gpuprof
EthanLuisMcDonough 53d6309
Merge branch 'main' into gpuprofwrite
EthanLuisMcDonough 344e357
Tighten PGO test requirements
EthanLuisMcDonough 2f75142
Tighten PGO test requirements
EthanLuisMcDonough 488cb4a
Apply requested formatting changes
EthanLuisMcDonough b90c015
Add memop function shim to DeviceRTL
EthanLuisMcDonough dc90a5c
Merge branch 'gpuprof' into gpuprofwrite
EthanLuisMcDonough c68c6e2
Make requested changes
EthanLuisMcDonough ca52c58
Only dump counters if PGODump flag is set
EthanLuisMcDonough 0da7627
Merge branch 'main' into gpuprofwrite
EthanLuisMcDonough ee4431a
Update requirements
EthanLuisMcDonough efe70ad
Merge branch 'main' into gpuprofwrite
EthanLuisMcDonough fb699b6
Merge changes
EthanLuisMcDonough 10e6c48
Merge branch 'main' into gpuprofwrite
EthanLuisMcDonough 1d0a961
Add llvm-profdata substitution to offload tests
EthanLuisMcDonough 0ac2d5f
Merge branch 'main' into gpuprofwrite
EthanLuisMcDonough c6b34ad
Prepend target prefix to basename
EthanLuisMcDonough 94ed55b
Merge branch 'main' into gpuprofwrite
EthanLuisMcDonough 26f5428
Merge branch 'main' into gpuprofwrite
EthanLuisMcDonough d9e864e
Merge branch 'main' into gpuprofwrite
EthanLuisMcDonough 55d673d
Merge branch 'main' into gpuprofwrite
EthanLuisMcDonough fa7589e
Move version check to front
EthanLuisMcDonough File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -541,6 +541,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; | ||
|
|
@@ -562,15 +573,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; | ||
| } | ||
|
|
||
|
|
@@ -1359,4 +1362,107 @@ 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); | ||
|
|
||
| /* Find file basename and path sizes */ | ||
| int32_t DirEnd = FilenameLength - 1; | ||
| while (DirEnd >= 0 && !IS_DIR_SEPARATOR(Filename[DirEnd])) { | ||
| DirEnd--; | ||
| } | ||
| uint32_t DirSize = DirEnd + 1, BaseSize = FilenameLength - DirSize; | ||
|
|
||
| /* Prepend "TARGET." to current filename */ | ||
| if (DirSize > 0) { | ||
| memcpy(TargetFilename, Filename, DirSize); | ||
| } | ||
| memcpy(TargetFilename + DirSize, Target, TargetLength); | ||
| TargetFilename[TargetLength + DirSize] = '.'; | ||
| memcpy(TargetFilename + DirSize + 1 + TargetLength, Filename + DirSize, | ||
| BaseSize); | ||
| 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 | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.