Skip to content

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

Merged
Amanieu merged 2 commits into
rust-lang:masterfrom
Amjad50:weak_link_libm
Apr 10, 2024
Merged

Always have math functions but with weak linking attribute if we can#577
Amanieu merged 2 commits into
rust-lang:masterfrom
Amjad50:weak_link_libm

Conversation

@Amjad50

@Amjad50 Amjad50 commented Feb 19, 2024

Copy link
Copy Markdown
Contributor

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

Amjad50 commented Feb 19, 2024

Copy link
Copy Markdown
Contributor Author

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

Amjad50 commented Mar 5, 2024

Copy link
Copy Markdown
Contributor Author

Can you check this @Amanieu? Thanks

@Amanieu

Amanieu commented Mar 5, 2024

Copy link
Copy Markdown
Member

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

@Amjad50

Amjad50 commented Mar 5, 2024

Copy link
Copy Markdown
Contributor Author

Ah, thanks for the info.

@Amanieu

Amanieu commented Mar 28, 2024

Copy link
Copy Markdown
Member

CI should be fixed now, can you rebase?

@Amjad50

Amjad50 commented Mar 28, 2024

Copy link
Copy Markdown
Contributor Author

Thanks, Builds now

Comment thread src/math.rs Outdated
Comment thread src/math.rs Outdated
Comment thread src/math.rs Outdated
Comment thread src/math.rs Outdated
@Amjad50 Amjad50 requested a review from Amanieu March 29, 2024 05:47
Comment thread 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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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.

@Amjad50 Amjad50 Mar 31, 2024

Copy link
Copy Markdown
Contributor Author

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

@Amanieu Amanieu enabled auto-merge April 10, 2024 11:40
Amjad50 added 2 commits April 10, 2024 13:40
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
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
```
tgross35 pushed a commit that referenced this pull request Jan 31, 2026
Always have math functions but with `weak` linking attribute if we can
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