Skip to content

bytes!(...).as_ptr() generates bad code on 32-bit architectures #14762

@taralx

Description

@taralx

On ARM, bytes!(...).as_ptr() generates a cast to i64*, load, truncate to i32. Something is not right here. :)

Code:

#![crate_type="rlib"]
#![no_std]

extern crate core;

use core::slice::ImmutableVector;

extern "C" {
    fn puts(p: *i8) -> i32;
}

unsafe fn dostuff(p: *u8) {
    puts(p as *i8);
}

pub fn main() {
    unsafe { dostuff(bytes!("TEXT!", 0).as_ptr()); }
}

Output on x86:

; ModuleID = 't.rs'
target datalayout = "e-i64:64-f80:128-n8:16:32:64-S128"
target triple = "x86_64-unknown-linux-gnu"

@const = private constant [6 x i8] c"TEXT!\00"

; Function Attrs: nounwind
declare i32 @puts(i8* nocapture readonly) unnamed_addr #0

; Function Attrs: nounwind uwtable
define void @_ZN4main20hafd3b7e891e0f6adCaa4v0.0E() unnamed_addr #1 {
entry-block:
  %0 = tail call i32 @puts(i8* getelementptr inbounds ([6 x i8]* @const, i64 0, i64 0)) #2
  ret void
}

attributes #0 = { nounwind "split-stack" }
attributes #1 = { nounwind uwtable "split-stack" }
attributes #2 = { nounwind }

Output on arm:

; ModuleID = 't.rs'
target datalayout = "e-p:32:32-i64:64-v128:64:128-n32"
target triple = "arm-unknown-linux-gnu"

@const = private constant [6 x i8] c"TEXT!\00"
@_ZN4main5BYTES20h9dc6d6c5b6e2b9ccOaa4v0.0E = internal unnamed_addr constant { i8*, i32 } { i8* getelementptr inbounds ([6 x i8]* @const, i32 0, i32 0), i32 6 }

; Function Attrs: nounwind
declare i32 @puts(i8* nocapture readonly) unnamed_addr #0

; Function Attrs: nounwind uwtable
define void @_ZN4main20hafd3b7e891e0f6adCaa4v0.0E() unnamed_addr #1 {
entry-block:
  %0 = load i64* bitcast ({ i8*, i32 }* @_ZN4main5BYTES20h9dc6d6c5b6e2b9ccOaa4v0.0E to i64*), align 8
  %.sroa.0.0.extract.trunc.i = trunc i64 %0 to i32
  %1 = inttoptr i32 %.sroa.0.0.extract.trunc.i to i8*
  %2 = tail call i32 @puts(i8* %1) #2
  ret void
}

attributes #0 = { nounwind "split-stack" }
attributes #1 = { nounwind uwtable "split-stack" }
attributes #2 = { nounwind }

Compiled with: rust/bin/rustc --emit=ir --target=$CPU-unknown-linux-gnu -L . -O t.rs

Metadata

Metadata

Assignees

No one assigned

    Labels

    E-needs-testCall for participation: An issue has been fixed and does not reproduce, but no test has been added.I-slowIssue: Problems and improvements with respect to performance of generated code.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions