Skip to content

[clang] Add bitint classification for __builtin_classify_type #72036

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions clang/docs/ReleaseNotes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,8 @@ Non-comprehensive list of changes in this release
determined at runtime.
* The ``__datasizeof`` keyword has been added. It is similar to ``sizeof``
except that it returns the size of a type ignoring tail padding.
* ``__builtin_classify_type()`` now classifies ``_BitInt`` values as the return value ``18``,
to match GCC 14's behavior.
Comment on lines +222 to +223
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should mention that this fixes #71911

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I didn't add that because the issue also mentions some extensions to __builtin_classify_type. We should probably split that out into a separate issue where we also document the exact extensions, maybe with a link to the gcc commit and test cases.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay, that's reasonable.


New Compiler Flags
------------------
Expand Down
7 changes: 6 additions & 1 deletion clang/lib/AST/ExprConstant.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11520,6 +11520,9 @@ enum class GCCTypeClass {
// decay to pointer. (Prior to version 6 it was only used in C++ mode).
// GCC reserves 15 for strings, but actually uses 5 (pointer) for string
// literals.
// Lang = 16,
// OpaqueType = 17,
BitInt = 18
};

/// EvaluateBuiltinClassifyType - Evaluate __builtin_classify_type the same way
Expand Down Expand Up @@ -11652,11 +11655,13 @@ EvaluateBuiltinClassifyType(QualType T, const LangOptions &LangOpts) {
case Type::ObjCInterface:
case Type::ObjCObjectPointer:
case Type::Pipe:
case Type::BitInt:
// GCC classifies vectors as None. We follow its lead and classify all
// other types that don't fit into the regular classification the same way.
return GCCTypeClass::None;

case Type::BitInt:
return GCCTypeClass::BitInt;

case Type::LValueReference:
case Type::RValueReference:
llvm_unreachable("invalid type for expression");
Expand Down
5 changes: 4 additions & 1 deletion clang/test/Sema/builtin-classify-type.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ enum gcc_type_class {
function_type_class, method_type_class,
record_type_class, union_type_class,
array_type_class, string_type_class,
lang_type_class
lang_type_class, opaque_type_class,
bitint_type_class
};

void foo(void) {
Expand Down Expand Up @@ -45,6 +46,7 @@ void foo(void) {
vint32_t3 vt5;
typedef _BitInt(64) vint64_t3 __attribute__((vector_size(16)));
vint64_t3 vt6;
_BitInt(16) bitint;

_Atomic int atomic_i;
_Atomic double atomic_d;
Expand All @@ -70,6 +72,7 @@ void foo(void) {
int a17[__builtin_classify_type(atomic_d) == real_type_class ? 1 : -1];
int a18[__builtin_classify_type(complex_i) == complex_type_class ? 1 : -1];
int a19[__builtin_classify_type(complex_d) == complex_type_class ? 1 : -1];
int a20[__builtin_classify_type(bitint) == bitint_type_class ? 1 : -1];
}

extern int (^p)(void);
Expand Down
5 changes: 4 additions & 1 deletion clang/test/SemaCXX/builtin-classify-type.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ enum gcc_type_class {
function_type_class, method_type_class,
record_type_class, union_type_class,
array_type_class, string_type_class,
lang_type_class
lang_type_class, opaque_type_class,
bitint_type_class
};

class cl {
Expand Down Expand Up @@ -42,6 +43,7 @@ void foo() {
_Atomic double atomic_d;
_Complex int complex_i;
_Complex double complex_d;
_BitInt(32) bitint;

int a1[__builtin_classify_type(f()) == void_type_class ? 1 : -1];
int a2[__builtin_classify_type(i) == integer_type_class ? 1 : -1];
Expand All @@ -65,5 +67,6 @@ void foo() {
int a20[__builtin_classify_type(atomic_d) == real_type_class ? 1 : -1];
int a21[__builtin_classify_type(complex_i) == complex_type_class ? 1 : -1];
int a22[__builtin_classify_type(complex_d) == complex_type_class ? 1 : -1];
int a23[__builtin_classify_type(bitint) == bitint_type_class ? 1 : -1];
}