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

Always have math functions but with weak linking attribute if we can #577

Merged
merged 2 commits into from
Apr 10, 2024

Conversation

Amjad50
Copy link
Contributor

@Amjad50 Amjad50 commented Feb 19, 2024

This is a replacement for rust-lang/libm#290

This fixes crashes during compilations for targets that don't have math symbols by default.

So, we will provide them libm symbols, but mark it as weak (if its supported), so that the linker will choose the system builtin functions, since those are sometimes more optimized.
If the linker couldn't find those, it will go with libm implementation.

@Amjad50
Copy link
Contributor Author

Amjad50 commented Feb 19, 2024

For some reason, windows building fails, and the error is confusing, it can't find some symbols that are unrelated to what have changed.

@Amjad50 Amjad50 mentioned this pull request Feb 23, 2024
5 tasks
@Amjad50
Copy link
Contributor Author

Amjad50 commented Mar 5, 2024

Can you check this @Amanieu? Thanks

@Amanieu
Copy link
Member

Amanieu commented Mar 5, 2024

We're currently waiting for fixes on the rustc side before work on compiler-builtins can continue: rust-lang/rust#121552

@Amjad50
Copy link
Contributor Author

Amjad50 commented Mar 5, 2024

Ah, thanks for the info.

@Amanieu
Copy link
Member

Amanieu commented Mar 28, 2024

CI should be fixed now, can you rebase?

@Amjad50
Copy link
Contributor Author

Amjad50 commented Mar 28, 2024

Thanks, Builds now

src/math.rs Outdated Show resolved Hide resolved
src/math.rs Outdated Show resolved Hide resolved
src/math.rs Outdated Show resolved Hide resolved
src/math.rs Outdated Show resolved Hide resolved
src/math.rs Outdated
all(target_arch = "xtensa", target_os = "none"),
all(target_vendor = "fortanix", target_env = "sgx")
))]
#[cfg(not(target_os = "windows"))]
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
#[cfg(not(target_os = "windows"))]
#[cfg_attr(all(not(windows), not(target_vendor = "apple")), weak)]

Copy link
Contributor Author

Choose a reason for hiding this comment

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

We do have this already inside no_mangle, so probably just remove the cfg completely is a better choice

Copy link
Member

Choose a reason for hiding this comment

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

No, we shouldn't provide the math intrinsics at all on windows/apple targets since weak linkage doesn't work properly. On those target we should always be getting these symbols from libc. The only exception is lgamma_r and lgammaf_r which are missing on MSVC targets.

Copy link
Contributor Author

@Amjad50 Amjad50 Mar 31, 2024

Choose a reason for hiding this comment

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

then you probably mean to use cfg and not cfg_attr? The suggested change is to use cfg_attr(weak) for non windows/apple

This is a replacement for rust-lang/libm#290

This fixes crashes during compilations for targets that don't have math
symbols by default.

So, we will provide them libm symbols, but mark it as `weak` (if its
supported), so that the linker will choose the system builtin functions,
since those are sometimes more optimized.
If the linker couldn't find those, it will go with `libm`
implementation.
@Amanieu Amanieu merged commit db7b5db into rust-lang:master Apr 10, 2024
22 checks passed
taiki-e added a commit to taiki-e/portable-atomic that referenced this pull request Apr 17, 2024
```
error[E0658]: use of unstable library feature 'proc_macro_byte_character'
   --> /home/runner/.cargo/registry/src/index.crates.io-6f17d22bba15001f/proc-macro2-1.0.80/src/wrapper.rs:871:21
    |
871 |                     proc_macro::Literal::byte_character(byte)
    |                     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    |
    = note: see issue #115268 <rust-lang/rust#115268> for more information
    = help: add `#![feature(proc_macro_byte_character)]` to the crate attributes to enable
    = note: this compiler was built on 2024-03-21; consider upgrading it if it is out of date

error[E0658]: use of unstable library feature 'proc_macro_c_str_literals'
   --> /home/runner/.cargo/registry/src/index.crates.io-6f17d22bba15001f/proc-macro2-1.0.80/src/wrapper.rs:898:21
    |
898 |                     proc_macro::Literal::c_string(string)
    |                     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    |
    = note: see issue #119750 <rust-lang/rust#119750> for more information
    = help: add `#![feature(proc_macro_c_str_literals)]` to the crate attributes to enable
    = note: this compiler was built on 2024-03-21; consider upgrading it if it is out of date
```

Also, the latest nightly cannot be used due to rust-lang/compiler-builtins#577.

```
  = note: /usr/lib/gcc/avr/5.4.0/../../../avr/lib/avr6/libm.a(addsf3.o): In function `__addsf3':
          (.text.avr-libc.fplib+0x2): multiple definition of `__addsf3'
          /home/runner/work/portable-atomic/portable-atomic/target/no-std-test/avr-unknown-gnu-atmega2560/debug/deps/libcompiler_builtins-81c93cbf49042e5b.rlib(compiler_builtins-81c93cbf49042e5b.compiler_builtins.3808c58458fddf11-cgu.07.rcgu.o):/home/runner/.cargo/registry/src/index.crates.io-6f17d22bba15001f/compiler_builtins-0.1.109/src/macros.rs:500: first defined here
```
taiki-e added a commit to taiki-e/portable-atomic that referenced this pull request Apr 17, 2024
rust-lang/compiler-builtins#577 caused regression on no-std hexagon:

```
  error: symbol 'fma' is already defined

  error: symbol 'fmax' is already defined
```
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants