[llvm] [CodeGen] Don't codegen the weak function when there is a defined non-weak symbol (PR #76040)

via llvm-commits llvm-commits at lists.llvm.org
Wed Dec 20 17:40:56 PST 2023


DianQK wrote:

> Can you describe your case using concrete commands? We already have `.lto_discard` to handle non-prevailing symbols.

The concrete usage case is to override the default builtin functions in rust. Currently rust adds builtin functions to LTO.

An example might be as follows:
```rust
#![no_std]
#![no_main]

use core::panic::PanicInfo;

#[panic_handler]
fn panic(_info: &PanicInfo) -> ! {
    loop {}
}

core::arch::global_asm!(
    ".global __aeabi_uidivmod",
    ".type __aeabi_uidivmod, %function",
    "__aeabi_uidivmod:",
    "str    r0, [r2, #0x060]",
    "str    r1, [r2, #0x064]",
);
```

Then run `rustc -Clto --target=thumbv6m-none-eabi`. This requires many other settings, such as setting the `weak-intrinsics`. (I should be able to make an example for x86_64 if needed.)

This will probably get `user.ll` and `builtin.ll`.

```llvm
target datalayout = "e-m:e-p:32:32-Fi8-i64:64-v128:64:128-a:0:32-n32-S64"
target triple = "thumbv6m-none-unknown-eabi"

module asm ".global __aeabi_uidivmod"
module asm ".type __aeabi_uidivmod, %function"
module asm "__aeabi_uidivmod:"
module asm "str    r0, [r2, #0x060]"
module asm "str    r1, [r2, #0x064]"
```

```llvm
target datalayout = "e-m:e-p:32:32-Fi8-i64:64-v128:64:128-a:0:32-n32-S64"
target triple = "thumbv6m-none-unknown-eabi"

; Function Attrs: naked nocf_check noinline nounwind
define weak hidden void @__aeabi_uidivmod() unnamed_addr {
  tail call void asm sideeffect alignstack "push {lr}\0Asub sp, sp, #4\0Amov r2, sp\0Abl __udivmodsi4\0Aldr r1, [sp]\0Aadd sp, sp, #4\0Apop {pc}", "~{cc},~{memory}"()
  unreachable
}
```

Then run with `llvm-link user.ll builtin.ll -S -o out.ll` and `llc out.ll`.

Looking at `.lto_discard` related code, this could be useful or something similar. I will look into it. Thanks.

https://github.com/llvm/llvm-project/pull/76040


More information about the llvm-commits mailing list