Skip to content

[Feature Request][clang] Declaring C/C++ intrinsic arguments as compile-time immediates #153164

@RKSimon

Description

@RKSimon

As detailed here: https://discourse.llvm.org/t/feature-request-declaring-c-c-intrinsic-arguments-as-compile-time-immediates/82971

X86 has a lot of SSE/AVX intrinsics where some of the arguments are compile-time immediate values (e.g. shuffle masks, rounding modes etc.) - other targets have similar requirements.

Currently we have to expose these as defines to the underlying __builtin intrinsic directly, which have a signature tagging the argument as immediate, and then handle the expansion to IR inside CGBuiltin etc:

#define _mm_shuffle_ps(a, b, mask) \
  ((__m128)__builtin_ia32_shufps((__v4sf)(__m128)(a), (__v4sf)(__m128)(b), (int)(mask)))
TARGET_BUILTIN(__builtin_ia32_shufps, "V4fV4fV4fIi", "ncV:128:", "sse")

But ideally we’d do something like:

inline __m128 _mm_shuffle_ps(__m128 a, __m128 b, CONSTEXPR_ARGUMENT int mask) {
  return (__m128)__builtin_ia32_shufps((__v4sf)a, (__v4sf)b, mask);
}

or in this case we could even evaluate the mask inside the inline directly:

inline __m128 _mm_shuffle_ps(__m128 a, __m128 b, CONSTEXPR_ARGUMENT int mask) {
  return (__m128)__builtin_shufflvector((__v4sf)a, (__v4sf)b, (mask & 3), (mask >> 2) & 3, ((mask >> 4) & 3) + 4, ((mask >> 6) & 3) + 4);
}

We’d want this to work for all C/C++ cases, not just where constexpr is legal.

CONSTEXPR_ARGUMENT would have to be something suitably clang internal specific

This is kind of like a template expansion, but ideally we'd avoid having to handle name mangling so maybe do some form of force inline that prevents users from using the function pointer?

I'm not sure what the first steps for this should be - either aim directly for implementing https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2019/p1045r1.html or create something more modest and more easily alterable later on.

Metadata

Metadata

Assignees

No one assigned

    Labels

    clang:frontendLanguage frontend issues, e.g. anything involving "Sema"constexprAnything related to constant evaluationenhancementImproving things as opposed to bug fixing, e.g. new or missing feature

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions