Skip to content

Missed optimization comparing TypeId arrays #84253

Description

@AngelicosPhosphoros

Edit: Look nikic example below.

I tried this code:

use std::any::TypeId;

trait CheckEqTuple{
    type OutSliceType: AsRef<[TypeId]> + AsMut<[TypeId]>;
    fn get_unsorted_ids()->Self::OutSliceType;
    fn get_quick_sorted_ids()->Self::OutSliceType{
        let mut ret = Self::get_unsorted_ids();
        ret.as_mut().sort_unstable();
        ret
    }
    fn get_slow_sorted_ids()->Self::OutSliceType{
        let mut ret = Self::get_unsorted_ids();
        ret.as_mut().sort();
        ret
    }
}

impl<A: 'static, B: 'static> CheckEqTuple for (A, B){
    type OutSliceType = [TypeId; 2];
    fn get_unsorted_ids()->Self::OutSliceType{
        [TypeId::of::<A>(), TypeId::of::<B>()]
    }
}

fn cmp_unord<A: CheckEqTuple, B: CheckEqTuple>()->bool{
    A::get_unsorted_ids().as_ref()
     == B::get_unsorted_ids().as_ref()
}

fn cmp_ord_q<A: CheckEqTuple, B: CheckEqTuple>()->bool{
    A::get_quick_sorted_ids().as_ref() 
        == B::get_quick_sorted_ids().as_ref()
}

fn cmp_ord_s<A: CheckEqTuple, B: CheckEqTuple>()->bool{
    A::get_slow_sorted_ids().as_ref()
     == B::get_slow_sorted_ids().as_ref()
}

pub fn cmp_same()->bool{
    cmp_unord::<(i32, bool), (i32, bool)>()
}

pub fn cmp_diff_order()->bool{
    cmp_unord::<(bool, i32), (i32, bool)>()
}

pub fn cmp_ord()->bool{
    cmp_ord_q::<(bool, i32), (i32, bool)>()
}

pub fn cmp_ord2()->bool{
    cmp_ord_s::<(bool, i32), (i32, bool)>()
}

I expected to see this happen:

This code should compile to return true or return false.

example::cmp_same:
        mov     al, 1
        ret

example::cmp_diff_order:
        xor     eax, eax
        ret

example::cmp_ord:
        mov     al, 1
        ret

Instead, this happened:

asm contains useless operations

example::cmp_same:
        sub     rsp, 32
        movabs  rax, -8661621401413125213
        mov     qword ptr [rsp + 8], rax
        mov     al, 1
        add     rsp, 32
        ret

example::cmp_diff_order:
        sub     rsp, 32
        movabs  rax, -5015437470765251660
        mov     qword ptr [rsp + 8], rax
        xor     eax, eax
        add     rsp, 32
        ret

example::cmp_ord:
        sub     rsp, 32
        movabs  rax, -5015437470765251660
        mov     qword ptr [rsp + 8], rax
        mov     al, 1
        add     rsp, 32
        ret

Meta

rustc --version --verbose:

rustc 1.51.0 (2fd73fabe 2021-03-23)

Proposed fix

If I add additional SROA flag, useless code is eliminated. I think, we need to add another SROA to pipeline somewhere before last instcombine and dead code elimination.

Here godbolt link.

Activity

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

Metadata

Metadata

Assignees

Labels

A-LLVMArea: Code generation parts specific to LLVM. Both correctness bugs and optimization-related issues.C-bugCategory: This is a bug.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