Skip to content
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

lib/libm/wf_[lt]gamma: Define _IEEE_LIBM only if not set. #15183

Merged
merged 1 commit into from
Jun 6, 2024

Conversation

agatti
Copy link
Contributor

@agatti agatti commented Jun 1, 2024

fdilibm was originally meant to see _IEEE_LIBM defined from outside the libm code, not it being hardcoded in (see section 4 in https://netlib.org/fdlibm/readme). Picolibc assumes this assumption holds true and attempts to define it itself, conflicting with the existing definition.

Although Picolibc provides its own libm support, on RISC-V the version of Picolibc installed on the CI machine currently crashes when running the math tests. This is a stop-gap solution that would make things work until either a fix for the test crashes is found or a newer version of Picolibc is provided by the CI machine's package repository.

Edit: Picolibc crashes due to its lgammaf implementation requiring TLS support enabled.

Copy link

github-actions bot commented Jun 1, 2024

Code size report:

   bare-arm:    +0 +0.000% 
minimal x86:    +0 +0.000% 
   unix x64:    +0 +0.000% standard
      stm32:    +0 +0.000% PYBV10
     mimxrt:    +0 +0.000% TEENSY40
        rp2:    +0 +0.000% RPI_PICO_W
       samd:    +0 +0.000% ADAFRUIT_ITSYBITSY_M4_EXPRESS

Copy link

codecov bot commented Jun 1, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 98.39%. Comparing base (ace08c3) to head (4978959).

Current head 4978959 differs from pull request most recent head d7aa2fe

Please upload reports for the commit d7aa2fe to get more accurate results.

Additional details and impacted files
@@           Coverage Diff           @@
##           master   #15183   +/-   ##
=======================================
  Coverage   98.39%   98.39%           
=======================================
  Files         161      161           
  Lines       21204    21204           
=======================================
  Hits        20864    20864           
  Misses        340      340           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@dpgeorge
Copy link
Member

dpgeorge commented Jun 3, 2024

Since the code only ever uses #ifdef _IEEE_LIBM, this change is a no-op, so it's fine.

@dpgeorge dpgeorge added the lib label Jun 3, 2024
@dpgeorge
Copy link
Member

dpgeorge commented Jun 3, 2024

Edit: Picolibc crashes due to its lgammaf implementation requiring TLS support enabled.

@agatti Given that knowledge, do you think the change in this PR is still a good way forward, does it fix the crash?

@agatti
Copy link
Contributor Author

agatti commented Jun 3, 2024

@dpgeorge I believe this is still needed. This PR will fix the case when you want to use Picolibc's libc with MicroPython's libm - the TLS issue is for when you want to use both Picolibc's libc and libm, and MicroPython for everything else.

I can add the TLS fix to #12853 if you think it's needed in the context of a RISC-V test environment to also work with an external libm implementation.

@agatti
Copy link
Contributor Author

agatti commented Jun 3, 2024

Better yet, with this PR in, the CI scripts can be updated to include Picolibc compatibility tests for Arm and Lx106 targets.

The CI machine can have picolibc-arm-none-eabi and picolibc-xtensa-lx106-elf packages installed, so the build scripts can be updated to make sure MicroPython also works with Picolibc for Arm and ESP8266 targets.

@projectgus
Copy link
Contributor

The CI machine can have picolibc-arm-none-eabi and picolibc-xtensa-lx106-elf packages installed, so the build scripts can be updated to make sure MicroPython also works with Picolibc for Arm and ESP8266 targets.

Ooh, I think this would be very interesting! I've been side-eyeing the amount of newlib stuff that ends up in the stm32 builds lately, and wondering how much code size we'd save by using picolibc instead...

@projectgus
Copy link
Contributor

projectgus commented Jun 4, 2024

the TLS issue is for when you want to use both Picolibc's libc and libm, and MicroPython for everything else.

For now is it reasonable for MicroPython to not support or test that combo, assuming the riscv picolibc builds work properly with MicroPython libm?

Similar to my previous comment, though, it might be interesting to experiment with picolibc libm at some point, as a comparison for code size, performance, and accuracy. I haven't looked, but I'm guessing it might be more optimised for riscv perhaps?

@projectgus
Copy link
Contributor

projectgus commented Jun 4, 2024

(Off-topic comment about the fdlibm readme.)

Please send comments and bug reports to the electronic mail address
suggested by:
fdlibm-comments AT sun.com

This unexpectedly triggered a bit of nostalgia. 😿

@agatti
Copy link
Contributor Author

agatti commented Jun 4, 2024

the TLS issue is for when you want to use both Picolibc's libc and libm, and MicroPython for everything else.

For now is it reasonable for MicroPython to not support or test that combo, assuming the riscv picolibc builds work properly with MicroPython libm?

That's what I think as well. In fact if you get the latest qemu-riscv version from my work branch (https://github.com/agatti/micropython/tree/qemu-riscv/ - there's a wrong fixup in there but I didn't push a correction to not restart CI for nothing) and apply the libm modifications from this PR it should work.

Similar to my previous comment, though, it might be interesting to experiment with picolibc libm at some point, as a comparison for code size, performance, and accuracy. I haven't looked, but I'm guessing it might be more optimised for riscv perhaps?

As an aside, whilst waiting for something else to compile I did try that with minimal TLS initialisation code to see that for myself. Weirdly, the float/math_domain_special test fails as for Picolibc cosh(-INFINITY) equals -INFINITY instead of INFINITY. Haven't stepped through the binary to see what's going on, but Picolibc's code looks correct (it is the same as recent-ish newlib), so I may have missed something myself.

fdilibm was originally meant to see _IEEE_LIBM defined from outside the
libm code, not it being hardcoded in.  Picolibc assumes this assumption
holds true and attempts to define itself, conflicting with the existing
definition.

Signed-off-by: Alessandro Gatti <a.gatti@frob.it>
@dpgeorge
Copy link
Member

dpgeorge commented Jun 6, 2024

I've been side-eyeing the amount of newlib stuff that ends up in the stm32 builds lately, and wondering how much code size we'd save by using picolibc instead...

That would be interesting to try, using picolibc instead of newlib.

In the meantime, this PR is good to merge.

@dpgeorge dpgeorge merged commit d7aa2fe into micropython:master Jun 6, 2024
58 checks passed
@agatti agatti deleted the picolibc-ieeelibm branch June 6, 2024 05:18
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants