Skip to content

Suboptimal codegen for comparison of trivially equality comparable types #89820

@AMP999

Description

@AMP999

Right now eqcompare_T has good codegen but eqcompare_U and eqcompare_O have worse codegen. Ideally, they'd all have the same codegen, since the comparison semantics for each of them are identical.

https://godbolt.org/z/GYdGc6xqa

#include <compare>

struct T {
    int i;
    int j;
    friend auto operator<=>(const T&, const T&) = default;
};
static_assert(__is_trivially_equality_comparable(T));

struct U {
    int i;
    short a, b;
    friend auto operator<=>(const U&, const U&) = default;
};
static_assert(__is_trivially_equality_comparable(U));

struct ObjRep {
    alignas(T) char data[sizeof(T)];
    friend auto operator<=>(const ObjRep&, const ObjRep&) = default;
};
static_assert(__is_trivially_equality_comparable(ObjRep));

// Since all three are trivially equality-comparable, these should have the same codegen.
// Clang fails to optimize.
bool eqcompare_T(T *a, T *b) { return *a == *b; }
bool eqcompare_U(U *a, U *b) { return *a == *b; }
bool eqcompare_O(ObjRep *a, ObjRep *b) { return *a == *b; }

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions