Skip to content

Codegen regression in Ipv6Addr [u16; 8] to [u8; 16] conversion #122805

@paolobarbolini

Description

@paolobarbolini
Contributor

Code

I tried this code:

// Taken from `Ipv6Addr::new`

pub fn convert1(value: [u16; 8]) -> [u8; 16] {
    let addr16 = [
        value[0].to_be(),
        value[1].to_be(),
        value[2].to_be(),
        value[3].to_be(),
        value[4].to_be(),
        value[5].to_be(),
        value[6].to_be(),
        value[7].to_be(),
    ];
    unsafe { std::mem::transmute::<_, [u8; 16]>(addr16) }
}

pub fn convert2(value: [u16; 8]) -> [u8; 16] {
    let addr16 = value.map(|val| val.to_be());
    unsafe { std::mem::transmute::<_, [u8; 16]>(addr16) }
}

I expected to see this happen: these two pieces of code produced the same assembly

Instead, this happened: the first version generates worse code https://rust.godbolt.org/z/qrz7Pn1a7

Version it worked on

It most recently worked on: 1.76

Version with regression

rustc --version --verbose:

rustc 1.78.0-beta.1 (efd9d2df1 2024-03-18)
binary: rustc
commit-hash: efd9d2df12b5e17fac0b4d0fb89f612ecd79f259
commit-date: 2024-03-18
host: x86_64-unknown-linux-gnu
release: 1.78.0-beta.1
LLVM version: 18.1.2

Activity

added
I-prioritizeIssue: Indicates that prioritization has been requested for this issue.
needs-triageThis issue may need triage. Remove it if it has been sufficiently triaged.
on Mar 21, 2024
added
A-LLVMArea: Code generation parts specific to LLVM. Both correctness bugs and optimization-related issues.
I-slowIssue: Problems and improvements with respect to performance of generated code.
and removed
needs-triageThis issue may need triage. Remove it if it has been sufficiently triaged.
on Mar 21, 2024
dianqk

dianqk commented on Mar 21, 2024

@dianqk
Member

The main branch of LLVM may have fixed this issue: https://llvm.godbolt.org/z/WzM1dvrTb.

workingjubilee

workingjubilee commented on Mar 21, 2024

@workingjubilee
Member

This seems to become much better if one uses infallible destructuring, like so: https://rust.godbolt.org/z/csraMobE8

Whoops, compared against 1.76, my mistake.

apiraino

apiraino commented on Mar 21, 2024

@apiraino
Contributor

WG-prioritization assigning priority (Zulip discussion).

@rustbot label -I-prioritize +P-medium

added and removed
I-prioritizeIssue: Indicates that prioritization has been requested for this issue.
on Mar 21, 2024
dianqk

dianqk commented on Mar 21, 2024

@dianqk
Member

I have bisected to llvm/llvm-project@1e828f8 (llvm/llvm-project#82279). But I'm not sure if this match the backport rules.

I have submitted a backport request, but it may be rejected.

dianqk

dianqk commented on Mar 24, 2024

@dianqk
Member

@rustbot label llvm-fixed-upstream

added
llvm-fixed-upstreamIssue expected to be fixed by the next major LLVM upgrade, or backported fixes
on Mar 24, 2024
added a commit that references this issue on Apr 7, 2024
76e3145
added a commit that references this issue on Apr 7, 2024
0e3235f
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-LLVMArea: Code generation parts specific to LLVM. Both correctness bugs and optimization-related issues.A-codegenArea: Code generationC-bugCategory: This is a bug.I-slowIssue: Problems and improvements with respect to performance of generated code.P-mediumMedium priorityllvm-fixed-upstreamIssue expected to be fixed by the next major LLVM upgrade, or backported fixesregression-untriagedUntriaged performance or correctness regression.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      Participants

      @apiraino@paolobarbolini@dianqk@saethlin@workingjubilee

      Issue actions

        Codegen regression in `Ipv6Addr` `[u16; 8]` to `[u8; 16]` conversion · Issue #122805 · rust-lang/rust