Skip to content

riscv codegen oddity: emits (hopefully no-op) signext for a u32 param. #98151

Description

@pnkfelix

I tried this code:

// compile-flags: --target riscv64gc-unknown-none-elf -C no-prepopulate-passes --emit=llvm-ir

#![feature(no_core, lang_items)]
#![crate_type = "lib"]
#![no_std]
#![no_core]

#[lang="sized"] trait Sized { }
#[lang="freeze"] trait Freeze { }
#[lang="copy"] trait Copy { }

#[no_mangle] pub extern "C" fn c_arg_u8(_a: u8) { }
#[no_mangle] pub extern "C" fn c_arg_u16(_a: u16) { }
#[no_mangle] pub extern "C" fn c_arg_u32(_a: u32) { }
#[no_mangle] pub extern "C" fn c_arg_u64(_a: u64) { }

#[no_mangle] pub extern "C" fn c_ret_u8() -> u8 { 0 }
#[no_mangle] pub extern "C" fn c_ret_u16() -> u16 { 0 }
#[no_mangle] pub extern "C" fn c_ret_u32() -> u32 { 0 }
#[no_mangle] pub extern "C" fn c_ret_u64() -> u64 { 0 }

#[no_mangle] pub extern "C" fn c_ret_i8() -> i8 { 0 }
#[no_mangle] pub extern "C" fn c_ret_i16() -> i16 { 0 }
#[no_mangle] pub extern "C" fn c_ret_i32() -> i32 { 0 }
#[no_mangle] pub extern "C" fn c_ret_i64() -> i64 { 0 }

Note the compile flags; thus I invoked the compiler like so:

% rustc +nightly --target riscv64gc-unknown-none-elf -C no-prepopulate-passes demo.rs --emit=llvm-ir

I expected to see this happen:

Semi-uniform handling of unsigned and signed extension of the arguments/return values. Namely, I would expect uN types to be (at most) zero-extended, and iN types to be (at most) signed-extended.

Instead, this happened: The u32 handling emits a signext attribute, like so:


; Function Attrs: nounwind
define dso_local void @c_arg_u32(i32 signext %_a) unnamed_addr #0 {
start:
  ret void
}

...

; Function Attrs: nounwind
define dso_local signext i32 @c_ret_u32() unnamed_addr #0 {
start:
  ret i32 0
}

I think this ends up acting as having no effect, but its weird nonetheless to have it there.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-FFIArea: Foreign function interface (FFI)A-LLVMArea: Code generation parts specific to LLVM. Both correctness bugs and optimization-related issues.A-codegenArea: Code generationO-riscvTarget: RISC-V architectureT-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions