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

Has Rust recently updated to an LLVM which emits calls to clzsi2? #260

Closed
ketsuban opened this issue Sep 9, 2018 · 5 comments
Closed

Has Rust recently updated to an LLVM which emits calls to clzsi2? #260

ketsuban opened this issue Sep 9, 2018 · 5 comments

Comments

@ketsuban
Copy link
Contributor

ketsuban commented Sep 9, 2018

According to the README, LLVM never emits calls to __clzsi2. However, when compiling my code targeting the Game Boy Advance (thumbv4-none-eabi) with a recent nightly (rustc 1.30.0-nightly (0198a1ea4 2018-09-08)) compiler-builtins is built with calls to __clzsi2 in many functions, leading to undefined references at link time.

Here's my target file on the offchance that's relevant.

{
    "abi-blacklist": [
        "stdcall",
        "fastcall",
        "vectorcall",
        "thiscall",
        "win64",
        "sysv64"
    ],
    "ar": "arm-none-eabi-ar",
    "arch": "arm",
    "atomic-cas": false,
    "data-layout": "e-m:e-p:32:32-i64:64-v128:64:128-a:0:32-n32-S64",
    "emit-debug-gdb-scripts": false,
    "env": "",
    "executables": true,
    "features": "+soft-float,+strict-align",
    "linker": "arm-none-eabi-gcc",
    "linker-flavor": "gcc",
    "llvm-target": "thumbv4-none-eabi",
    "os": "none",
    "panic-strategy": "abort",
    "pre-link-args": {
        "gcc": [
            "--specs=gba.specs"
        ]
    },
    "relocation-model": "static",
    "target-c-int-width": "32",
    "target-endian": "little",
    "target-pointer-width": "32",
    "vendor": ""
}

It presupposes a devkitARM toolchain is available.

@alexcrichton
Copy link
Member

Thanks for the report! It probably is the case yeah that LLVM has changed from when the README was written to now. We can certainly implement this function!

@Lokathor
Copy link
Contributor

Lokathor commented Dec 18, 2018

@alexcrichton the relevant function is fairly simple

EDIT: had the 32-bit only version at first, this works for both 32-bit and 64-bit

#[no_mangle]
pub extern "C" fn __clzsi2(mut x: usize) -> usize {
  let mut y: usize;
  let mut n: usize = if cfg!(target_pointer_width = "32") { 32 } else { 64 };
  #[cfg(target_pointer_width = "64")]
  {
    y = x >> 32;
    if y != 0 {
      n = n - 32;
      x = y;
    }
  }
  y = x >> 16;
  if y != 0 {
    n = n - 16;
    x = y;
  }
  y = x >> 8;
  if y != 0 {
    n = n - 8;
    x = y;
  }
  y = x >> 4;
  if y != 0 {
    n = n - 4;
    x = y;
  }
  y = x >> 2;
  if y != 0 {
    n = n - 2;
    x = y;
  }
  y = x >> 1;
  if y != 0 {
    n - 2
  } else {
    n - x
  }
}

Should I just put in a PR for this or is there some additional process needed for adding new builtins?

@alexcrichton
Copy link
Member

@Lokathor nah that'd be great to add! It can be hooked up to the test suite we already have to verify correctness as well

@Lokathor
Copy link
Contributor

I have another PR waiting, but once it's in I'll get to this one done up.

@Lokathor
Copy link
Contributor

Lokathor commented Jan 7, 2019

#267 is merged

@ketsuban ketsuban closed this as completed Jan 8, 2019
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

No branches or pull requests

3 participants