Skip to content

mmapForContinuousMode: Align Linux's impl to __APPLE__'s more. NFC. #95702

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
merged 1 commit into from
Jun 20, 2024

Conversation

chapuni
Copy link
Contributor

@chapuni chapuni commented Jun 16, 2024

No description provided.

@chapuni chapuni requested review from ellishg and ZequanWu June 16, 2024 13:06
@llvmbot llvmbot added compiler-rt PGO Profile Guided Optimizations labels Jun 16, 2024
@llvmbot
Copy link
Member

llvmbot commented Jun 16, 2024

@llvm/pr-subscribers-pgo

Author: NAKAMURA Takumi (chapuni)

Changes

Full diff: https://github.com/llvm/llvm-project/pull/95702.diff

1 Files Affected:

  • (modified) compiler-rt/lib/profile/InstrProfilingFile.c (+26-4)
diff --git a/compiler-rt/lib/profile/InstrProfilingFile.c b/compiler-rt/lib/profile/InstrProfilingFile.c
index b88e0b4b0b2ab..9faee36e5b815 100644
--- a/compiler-rt/lib/profile/InstrProfilingFile.c
+++ b/compiler-rt/lib/profile/InstrProfilingFile.c
@@ -237,24 +237,46 @@ static int mmapForContinuousMode(uint64_t CurrentFileOffset, FILE *File) {
   const char *CountersEnd = __llvm_profile_end_counters();
   const char *BitmapBegin = __llvm_profile_begin_bitmap();
   const char *BitmapEnd = __llvm_profile_end_bitmap();
+  const char *NamesBegin = __llvm_profile_begin_names();
+  const char *NamesEnd = __llvm_profile_end_names();
+  const uint64_t NamesSize = (NamesEnd - NamesBegin) * sizeof(char);
   uint64_t DataSize = __llvm_profile_get_data_size(DataBegin, DataEnd);
+  uint64_t CountersSize =
+      __llvm_profile_get_counters_size(CountersBegin, CountersEnd);
+  uint64_t NumBitmapBytes =
+      __llvm_profile_get_num_bitmap_bytes(BitmapBegin, BitmapEnd);
   /* Get the file size. */
   uint64_t FileSize = 0;
   if (getProfileFileSizeForMerging(File, &FileSize))
     return 1;
 
+  int Fileno = fileno(File);
+  /* Determine how much padding is needed before/after the counters and
+   * after the names. */
+  uint64_t PaddingBytesBeforeCounters, PaddingBytesAfterCounters,
+      PaddingBytesAfterNames, PaddingBytesAfterBitmapBytes,
+      PaddingBytesAfterVTable, PaddingBytesAfterVNames;
+  __llvm_profile_get_padding_sizes_for_counters(
+      DataSize, CountersSize, NumBitmapBytes, NamesSize, /*VTableSize=*/0,
+      /*VNameSize=*/0, &PaddingBytesBeforeCounters, &PaddingBytesAfterCounters,
+      &PaddingBytesAfterBitmapBytes, &PaddingBytesAfterNames,
+      &PaddingBytesAfterVTable, &PaddingBytesAfterVNames);
+
+  CurrentFileOffset = 0;
+  uint64_t FileOffsetToCounters = CurrentFileOffset +
+                                  sizeof(__llvm_profile_header) + DataSize +
+                                  PaddingBytesBeforeCounters;
+
   /* Map the profile. */
   char *Profile = (char *)mmap(NULL, FileSize, PROT_READ | PROT_WRITE,
-                               MAP_SHARED, fileno(File), 0);
+                               MAP_SHARED, Fileno, 0);
   if (Profile == MAP_FAILED) {
     PROF_ERR("Unable to mmap profile: %s\n", strerror(errno));
     return 1;
   }
-  const uint64_t CountersOffsetInBiasMode =
-      sizeof(__llvm_profile_header) + __llvm_write_binary_ids(NULL) + DataSize;
   /* Update the profile fields based on the current mapping. */
   INSTR_PROF_PROFILE_COUNTER_BIAS_VAR =
-      (intptr_t)Profile - (uintptr_t)CountersBegin + CountersOffsetInBiasMode;
+      (intptr_t)Profile - (uintptr_t)CountersBegin + FileOffsetToCounters;
 
   /* Return the memory allocated for counters to OS. */
   lprofReleaseMemoryPagesToOS((uintptr_t)CountersBegin, (uintptr_t)CountersEnd);

@chapuni chapuni requested a review from ornata June 16, 2024 14:46
@ornata
Copy link

ornata commented Jun 18, 2024

Why?

@chapuni
Copy link
Contributor Author

chapuni commented Jun 18, 2024

Why?

"I want to introduce bitmap bias w/o posting RFC."

Rather than appending similar changes (but with different variables) into linux impl, I think it'd be better to align blocks with same variable for possible commonization in the future. It can also make simpler to explain the difference of bitmap between APPLE and bias mode.

To introduce bitmap in biased continuous mode, the bias for counters cannot be reused since the layout of __llvm_prf_bits and __llvm_prf_cnts is different from profraw.

I thought actual commonization would be too early since it would be intrusive.

Copy link

@ornata ornata left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@chapuni chapuni merged commit 7cf84d3 into llvm:main Jun 20, 2024
9 checks passed
@chapuni chapuni deleted the mcdc/aarefactor branch June 20, 2024 07:52
@ilovepi
Copy link
Contributor

ilovepi commented Jun 20, 2024

We're seeing test failures after this patch. Can you please take a look?

Bot: https://ci.chromium.org/ui/p/fuchsia/builders/toolchain.ci/clang-linux-arm64/b8744624520905017585/overview

Error:

FAIL: Profile-aarch64 :: ContinuousSyncMode/runtime-counter-relocation.c (2289 of 15086)
******************** TEST 'Profile-aarch64 :: ContinuousSyncMode/runtime-counter-relocation.c' FAILED ********************
Exit Code: 1

Command Output (stderr):
--
RUN: at line 3: /b/s/w/ir/x/w/llvm_build/./bin/clang    -Wthread-safety -Wthread-safety-reference -Wthread-safety-beta   -ldl -fprofile-instr-generate -fcoverage-mapping -mllvm -runtime-counter-relocation=true -o /b/s/w/ir/x/w/llvm_build/runtimes/runtimes-aarch64-unknown-linux-gnu-bins/compiler-rt/test/profile/Profile-aarch64/ContinuousSyncMode/Output/runtime-counter-relocation.c.tmp.exe /b/s/w/ir/x/w/llvm-llvm-project/compiler-rt/test/profile/ContinuousSyncMode/runtime-counter-relocation.c
+ /b/s/w/ir/x/w/llvm_build/./bin/clang -Wthread-safety -Wthread-safety-reference -Wthread-safety-beta -ldl -fprofile-instr-generate -fcoverage-mapping -mllvm -runtime-counter-relocation=true -o /b/s/w/ir/x/w/llvm_build/runtimes/runtimes-aarch64-unknown-linux-gnu-bins/compiler-rt/test/profile/Profile-aarch64/ContinuousSyncMode/Output/runtime-counter-relocation.c.tmp.exe /b/s/w/ir/x/w/llvm-llvm-project/compiler-rt/test/profile/ContinuousSyncMode/runtime-counter-relocation.c
RUN: at line 4: echo "garbage" > /b/s/w/ir/x/w/llvm_build/runtimes/runtimes-aarch64-unknown-linux-gnu-bins/compiler-rt/test/profile/Profile-aarch64/ContinuousSyncMode/Output/runtime-counter-relocation.c.tmp.profraw
+ echo garbage
RUN: at line 5: env LLVM_PROFILE_FILE="%c/b/s/w/ir/x/w/llvm_build/runtimes/runtimes-aarch64-unknown-linux-gnu-bins/compiler-rt/test/profile/Profile-aarch64/ContinuousSyncMode/Output/runtime-counter-relocation.c.tmp.profraw"  /b/s/w/ir/x/w/llvm_build/runtimes/runtimes-aarch64-unknown-linux-gnu-bins/compiler-rt/test/profile/Profile-aarch64/ContinuousSyncMode/Output/runtime-counter-relocation.c.tmp.exe
+ env LLVM_PROFILE_FILE=%c/b/s/w/ir/x/w/llvm_build/runtimes/runtimes-aarch64-unknown-linux-gnu-bins/compiler-rt/test/profile/Profile-aarch64/ContinuousSyncMode/Output/runtime-counter-relocation.c.tmp.profraw /b/s/w/ir/x/w/llvm_build/runtimes/runtimes-aarch64-unknown-linux-gnu-bins/compiler-rt/test/profile/Profile-aarch64/ContinuousSyncMode/Output/runtime-counter-relocation.c.tmp.exe
RUN: at line 6: llvm-profdata show --counts --all-functions /b/s/w/ir/x/w/llvm_build/runtimes/runtimes-aarch64-unknown-linux-gnu-bins/compiler-rt/test/profile/Profile-aarch64/ContinuousSyncMode/Output/runtime-counter-relocation.c.tmp.profraw | FileCheck /b/s/w/ir/x/w/llvm-llvm-project/compiler-rt/test/profile/ContinuousSyncMode/runtime-counter-relocation.c -check-prefix=CHECK-COUNTS
+ llvm-profdata show --counts --all-functions /b/s/w/ir/x/w/llvm_build/runtimes/runtimes-aarch64-unknown-linux-gnu-bins/compiler-rt/test/profile/Profile-aarch64/ContinuousSyncMode/Output/runtime-counter-relocation.c.tmp.profraw
+ FileCheck /b/s/w/ir/x/w/llvm-llvm-project/compiler-rt/test/profile/ContinuousSyncMode/runtime-counter-relocation.c -check-prefix=CHECK-COUNTS
/b/s/w/ir/x/w/llvm-llvm-project/compiler-rt/test/profile/ContinuousSyncMode/runtime-counter-relocation.c:14:23: error: CHECK-COUNTS-NEXT: expected string not found in input
// CHECK-COUNTS-NEXT: Function count: 1
                      ^
<stdin>:4:13: note: scanning from here
 Counters: 2
            ^
<stdin>:5:2: note: possible intended match here
 Function count: 0
 ^

Input file: <stdin>
Check file: /b/s/w/ir/x/w/llvm-llvm-project/compiler-rt/test/profile/ContinuousSyncMode/runtime-counter-relocation.c

-dump-input=help explains the following input dump.

Input was:
<<<<<<
           1: Counters: 
           2:  main: 
           3:  Hash: 0x000000000a498458 
           4:  Counters: 2 
next:14'0                 X error: no match found
           5:  Function count: 0 
next:14'0     ~~~~~~~~~~~~~~~~~~~
next:14'1      ?                  possible intended match
           6:  Block counts: [0] 
next:14'0     ~~~~~~~~~~~~~~~~~~~
           7: Instrumentation level: Front-end 
next:14'0     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
           8: Functions shown: 1 
next:14'0     ~~~~~~~~~~~~~~~~~~~
           9: Total functions: 1 
next:14'0     ~~~~~~~~~~~~~~~~~~~
          10: Maximum function count: 0 
next:14'0     ~~~~~~~~~~~~~~~~~~~~~~~~~~
          11: Maximum internal block count: 0 
next:14'0     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
>>>>>>

--

********************
PASS: Profile-aarch64 :: Linux/coverage_ctors.cpp (2290 of 15086)

chapuni added a commit that referenced this pull request Jun 20, 2024
…. NFC. (#95702)"

This reverts commit 7cf84d3.
(llvmorg-19-init-14939-g7cf84d3b0bc5)

This has my wrong assumptions possibly. This was failing fuchsia.
@chapuni
Copy link
Contributor Author

chapuni commented Jun 20, 2024

@ilovepi Thanks for reporting. I've reverted this and I'll rework with less-intrusive way.

@ilovepi
Copy link
Contributor

ilovepi commented Jun 20, 2024

Thanks.

chapuni added a commit that referenced this pull request Jun 21, 2024
… a little.

I've made this small cosmetic changes,
`s/CountersOffsetInBiasMode/FileOffsetToCounters/`
AlexisPerry pushed a commit to llvm-project-tlp/llvm-project that referenced this pull request Jul 9, 2024
AlexisPerry pushed a commit to llvm-project-tlp/llvm-project that referenced this pull request Jul 9, 2024
…. NFC. (llvm#95702)"

This reverts commit 7cf84d3.
(llvmorg-19-init-14939-g7cf84d3b0bc5)

This has my wrong assumptions possibly. This was failing fuchsia.
AlexisPerry pushed a commit to llvm-project-tlp/llvm-project that referenced this pull request Jul 9, 2024
…__'s a little.

I've made this small cosmetic changes,
`s/CountersOffsetInBiasMode/FileOffsetToCounters/`
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
compiler-rt PGO Profile Guided Optimizations
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants