llvm / llvm-project

The LLVM Project is a collection of modular and reusable compiler and toolchain technologies.
http://llvm.org
Other
28.02k stars 11.57k forks source link

Suboptimal codegen for comparison of trivially equality comparable types #89820

Open AMP999 opened 4 months ago

AMP999 commented 4 months ago

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; }
scottmcm commented 4 months ago

I wonder if this is one the the things that https://summerofcode.withgoogle.com/programs/2024/projects/tqzvhY0X might be able to help with...