Skip to content

Worsened debug build codegen in beta #68855

Closed
@rodrimati1992

Description

@rodrimati1992

The assembly generated in debug builds (-C opt-level=0) worsened for this code that uses an associated constant from a trait.This seems to be caused by the fact that the function is generic,even though the associated constant doesn't depend on the generic parameters of the function.

The code:
https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=83dfb31158166b5ed7a92ca5dd7bb610

trait NeedsDrop:Sized{
    const NEEDS:bool=std::mem::needs_drop::<Self>();
}

impl<This> NeedsDrop for This{}

pub fn hello<T>(){
    if <bool>::NEEDS {
        panic!()
    }
}

pub fn hi(){
    hello::<()>();
    hello::<Vec<()>>();
}

The entirety of the generated assembly for stable 1.41.0 in a debug build:

playground::hello:
    retq

playground::hello:
    retq

playground::hi:
    pushq   %rax
    callq   *playground::hello@GOTPCREL(%rip)
    callq   *playground::hello@GOTPCREL(%rip)
    popq    %rax
    retq

The assembly generated for the same functions by rustc 1.42.0-beta.2 (2020-02-04 3d2613e) in a debug build:

playground::hello:
    pushq   %rax
    xorl    %eax, %eax
    testb   $1, %al
    jne .LBB45_2
    jmp .LBB45_1

.LBB45_1:
    popq    %rax
    retq

.LBB45_2:
    leaq    .Lanon.257b63aea292a17f4811b89c35bea688.2(%rip), %rdi
    leaq    .Lanon.257b63aea292a17f4811b89c35bea688.4(%rip), %rdx
    movq    std::panicking::begin_panic@GOTPCREL(%rip), %rax
    movl    $14, %esi
    callq   *%rax
    ud2

playground::hello:
    pushq   %rax
    xorl    %eax, %eax
    testb   $1, %al
    jne .LBB46_2
    jmp .LBB46_1

.LBB46_1:
    popq    %rax
    retq

.LBB46_2:
    leaq    .Lanon.257b63aea292a17f4811b89c35bea688.2(%rip), %rdi
    leaq    .Lanon.257b63aea292a17f4811b89c35bea688.4(%rip), %rdx
    movq    std::panicking::begin_panic@GOTPCREL(%rip), %rax
    movl    $14, %esi
    callq   *%rax
    ud2

playground::hi:
    pushq   %rax
    callq   *playground::hello@GOTPCREL(%rip)
    callq   *playground::hello@GOTPCREL(%rip)
    popq    %rax
    retq

Metadata

Metadata

Assignees

Labels

A-associated-itemsArea: Associated items (types, constants & functions)A-const-evalArea: Constant evaluation, covers all const contexts (static, const fn, ...)I-slowIssue: Problems and improvements with respect to performance of generated code.P-mediumMedium priorityT-compilerRelevant to the compiler team, which will review and decide on the PR/issue.regression-from-stable-to-betaPerformance or correctness regression from stable to beta.

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions