huojianchaoyy opened a new pull request, #22599:
URL: https://github.com/apache/nuttx/pull/22599

   *Note: Please adhere to [Contributing 
Guidelines](https://github.com/apache/nuttx/blob/master/CONTRIBUTING.md).*
   
   ## Summary
   
   Profile-guided optimization (PGO) for GCC, from Xiaomi Vela. Third of four 
coverage PRs; it contains the commits of #22598, and only the last six commits 
are new here.
   
   depends-on: apache/nuttx/pull/22598
   depends-on: apache/nuttx-apps/pull/4213
   
   - **compiler-rt: set the profile update method to atomic**: builds with 
`-fprofile-update=atomic` so that the counters stay accurate when several 
threads or CPUs run the same code.
   - **libgcc: add PGO instrumentation support**: the GCC PGO runtime 
(`libgcc/profile_value.c`, `profile_write.c`), the `-fprofile-generate` / 
`-fprofile-use` options for GCC in the arch toolchain files and 
`CONFIG_GCOV_COUNTER_POOL_SIZE`. On qemu-armv8a CoreMark ran 4-5% faster with 
the profile. `profile_write.c` now includes `<assert.h>` for `assert()`, which 
it got through other headers in Vela.
   - **pgo: skip instrumentation for mcount**: keeps the profiler out of 
`mcount` to avoid recursion.
   
   Follow-up fixes:
   
   - **libs/libbuiltin/libgcc: fix the nxstyle errors and license headers** of 
the new files. No functional change.
   - **libs/libbuiltin/libgcc: build the PGO runtime with Make**: the Make 
build did not link with `CONFIG_COVERAGE_PGO`, because VPATH picked 
compiler-rt's `profile_value.c` and `profile_write.c` instead of libgcc's. Name 
the libgcc directory with a `vpath` rule, add these sources only with 
`CONFIG_COVERAGE_PGO` and build the whole runtime without the profiling 
options, as the CMake build does.
   - **libs/libbuiltin/libgcc: define __gcov_indirect_call as TLS on sim**: the 
host GCC is configured with `--enable-tls` and accesses `__gcov_indirect_call` 
as a thread-local variable, so the simulator did not link (`TLS reference ... 
mismatches non-TLS definition`). The bare-metal toolchains do not, and keep the 
plain definition.
   
   #4213 in nuttx-apps fixes a `-Werror=maybe-uninitialized` in 
`nshlib/nsh_dbgcmds.c` that GCC reports with the PGO options on 
`qemu-armv8a:nsh_smp`.
   
   ## Impact
   
   - `CONFIG_COVERAGE_PGO` now also works with GCC, with Make and CMake. Builds 
without it are unchanged.
   - New Kconfig `CONFIG_GCOV_COUNTER_POOL_SIZE` (default 8192), only with GCC 
and `CONFIG_COVERAGE_PGO`.
   - `-fprofile-update=atomic` is added to the PGO generate builds.
   
   ## Testing
   
   Host: Ubuntu 22.04 (Linux 6.8, x86_64), host GCC 13.4, aarch64-none-elf-gcc 
13.4, arm-none-eabi-gcc 13.2.1, CMake 3.23 with Ninja. Base: master bec2b2a11e8.
   
   `./tools/checkpatch.sh -c -u -m -g HEAD~15..HEAD` passes.
   
   Each commit builds in these configurations (`CONFIG_COVERAGE_MINI=y`, 
`CONFIG_COVERAGE_ALL=y` unless noted):
   
   - `sim:nsh` (Make and CMake)
   - `qemu-armv8a:nsh` (CMake), `qemu-armv8a:nsh_smp` (Make), 
`qemu-armv8a:knsh` (Make)
   - `fvp-armv8r:pnsh` (Make)
   - `qemu-armv7r:pnsh` (Make, `CONFIG_COVERAGE_MINI` only, because the 
instrumented image does not fit in kflash)
   - `qemu-armv8a:nsh` without coverage (CMake)
   
   PGO builds with `CONFIG_COVERAGE_MINI=y`, `CONFIG_COVERAGE_ALL=y`, 
`CONFIG_COVERAGE_PGO=y` and GCC:
   
   | board:config | build system | result |
   |---|---|---|
   | sim:nsh | Make | links (before the Make fix: undefined 
`__gcov_indirect_call`, `__gcov_time_profiler_counter`) |
   | sim:nsh | CMake | links (before the TLS fix: TLS mismatch on 
`__gcov_indirect_call`), boots and runs `hello` |
   | qemu-armv8a:nsh | Make | links (before the Make fix: undefined references 
as above) |
   | qemu-armv8a:nsh | CMake | links |
   | qemu-armv8a:nsh_smp | CMake | links with apache/nuttx-apps#4213 |
   
   Not tested: collecting a profile and rebuilding with `-fprofile-use` on 
hardware, xtensa, RISC-V.
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to