[clang] [Clang] [Sema] Reject non-power-of-2 `_BitInt` matrix element types (PR #117487)
via cfe-commits
cfe-commits at lists.llvm.org
Mon Nov 25 03:32:35 PST 2024
https://github.com/Sirraide updated https://github.com/llvm/llvm-project/pull/117487
>From a612c8f0a78dd1f29a4885f57efbdd4a9cca374e Mon Sep 17 00:00:00 2001
From: Sirraide <aeternalmail at gmail.com>
Date: Sun, 24 Nov 2024 14:36:41 +0100
Subject: [PATCH 1/3] [Clang] [Sema] Reject non-power-of-2 `_BitInt` matrix
element types
---
clang/docs/ReleaseNotes.rst | 3 ++
.../clang/Basic/DiagnosticSemaKinds.td | 4 +-
clang/lib/Sema/SemaType.cpp | 41 +++++++++++--------
clang/test/SemaCXX/matrix-type.cpp | 10 +++++
4 files changed, 38 insertions(+), 20 deletions(-)
diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index 8bd06fadfdc984..1c0e4043bbe276 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -371,6 +371,9 @@ Non-comprehensive list of changes in this release
- ``__builtin_reduce_and`` function can now be used in constant expressions.
- ``__builtin_reduce_or`` and ``__builtin_reduce_xor`` functions can now be used in constant expressions.
+- Clang now rejects ``_BitInt`` matrix element types if the bit width is less than ``CHAR_WIDTH`` or
+ not a power of two, matching preexisting behaviour for vector types.
+
New Compiler Flags
------------------
diff --git a/clang/include/clang/Basic/DiagnosticSemaKinds.td b/clang/include/clang/Basic/DiagnosticSemaKinds.td
index eb05a6a77978af..f049e72a6b8694 100644
--- a/clang/include/clang/Basic/DiagnosticSemaKinds.td
+++ b/clang/include/clang/Basic/DiagnosticSemaKinds.td
@@ -3233,8 +3233,8 @@ def err_attribute_too_few_arguments : Error<
"%0 attribute takes at least %1 argument%s1">;
def err_attribute_invalid_vector_type : Error<"invalid vector element type %0">;
def err_attribute_invalid_bitint_vector_type : Error<
- "'_BitInt' vector element width must be %select{a power of 2|"
- "at least as wide as 'CHAR_BIT'}0">;
+ "'_BitInt' %select{vector|matrix}0 element width must be %select{a power of 2|"
+ "at least as wide as 'CHAR_BIT'}1">;
def err_attribute_invalid_matrix_type : Error<"invalid matrix element type %0">;
def err_attribute_bad_neon_vector_size : Error<
"Neon vector size must be 64 or 128 bits">;
diff --git a/clang/lib/Sema/SemaType.cpp b/clang/lib/Sema/SemaType.cpp
index f32edc5ac06440..06b779f5ef3aa2 100644
--- a/clang/lib/Sema/SemaType.cpp
+++ b/clang/lib/Sema/SemaType.cpp
@@ -2312,6 +2312,18 @@ QualType Sema::BuildArrayType(QualType T, ArraySizeModifier ASM,
return T;
}
+bool CheckBitIntElementType(Sema &S, SourceLocation AttrLoc,
+ const BitIntType *BIT, bool ForMatrixType = false) {
+ // Only support _BitInt elements with byte-sized power of 2 NumBits.
+ unsigned NumBits = BIT->getNumBits();
+ if (!llvm::isPowerOf2_32(NumBits) || NumBits < 8) {
+ S.Diag(AttrLoc, diag::err_attribute_invalid_bitint_vector_type)
+ << ForMatrixType << (NumBits < 8);
+ return true;
+ }
+ return false;
+}
+
QualType Sema::BuildVectorType(QualType CurType, Expr *SizeExpr,
SourceLocation AttrLoc) {
// The base type must be integer (not Boolean or enumeration) or float, and
@@ -2324,15 +2336,10 @@ QualType Sema::BuildVectorType(QualType CurType, Expr *SizeExpr,
Diag(AttrLoc, diag::err_attribute_invalid_vector_type) << CurType;
return QualType();
}
- // Only support _BitInt elements with byte-sized power of 2 NumBits.
- if (const auto *BIT = CurType->getAs<BitIntType>()) {
- unsigned NumBits = BIT->getNumBits();
- if (!llvm::isPowerOf2_32(NumBits) || NumBits < 8) {
- Diag(AttrLoc, diag::err_attribute_invalid_bitint_vector_type)
- << (NumBits < 8);
- return QualType();
- }
- }
+
+ if (const auto *BIT = CurType->getAs<BitIntType>();
+ BIT && CheckBitIntElementType(*this, AttrLoc, BIT))
+ return QualType();
if (SizeExpr->isTypeDependent() || SizeExpr->isValueDependent())
return Context.getDependentVectorType(CurType, SizeExpr, AttrLoc,
@@ -2402,15 +2409,9 @@ QualType Sema::BuildExtVectorType(QualType T, Expr *ArraySize,
return QualType();
}
- // Only support _BitInt elements with byte-sized power of 2 NumBits.
- if (T->isBitIntType()) {
- unsigned NumBits = T->castAs<BitIntType>()->getNumBits();
- if (!llvm::isPowerOf2_32(NumBits) || NumBits < 8) {
- Diag(AttrLoc, diag::err_attribute_invalid_bitint_vector_type)
- << (NumBits < 8);
- return QualType();
- }
- }
+ if (const auto *BIT = T->getAs<BitIntType>();
+ BIT && CheckBitIntElementType(*this, AttrLoc, BIT))
+ return QualType();
if (!ArraySize->isTypeDependent() && !ArraySize->isValueDependent()) {
std::optional<llvm::APSInt> vecSize =
@@ -2455,6 +2456,10 @@ QualType Sema::BuildMatrixType(QualType ElementTy, Expr *NumRows, Expr *NumCols,
return QualType();
}
+ if (const auto *BIT = ElementTy->getAs<BitIntType>();
+ BIT && CheckBitIntElementType(*this, AttrLoc, BIT, true))
+ return QualType();
+
if (NumRows->isTypeDependent() || NumCols->isTypeDependent() ||
NumRows->isValueDependent() || NumCols->isValueDependent())
return Context.getDependentSizedMatrixType(ElementTy, NumRows, NumCols,
diff --git a/clang/test/SemaCXX/matrix-type.cpp b/clang/test/SemaCXX/matrix-type.cpp
index af31e267fdcae8..d347332ee2d60f 100644
--- a/clang/test/SemaCXX/matrix-type.cpp
+++ b/clang/test/SemaCXX/matrix-type.cpp
@@ -29,3 +29,13 @@ void matrix_unsupported_element_type() {
using matrix3_t = bool __attribute__((matrix_type(1, 1))); // expected-error{{invalid matrix element type 'bool'}}
using matrix4_t = TestEnum __attribute__((matrix_type(1, 1))); // expected-error{{invalid matrix element type 'TestEnum'}}
}
+
+void matrix_unsupported_bit_int() {
+ using m1 = _BitInt(2) __attribute__((matrix_type(4, 4))); // expected-error{{'_BitInt' matrix element width must be at least as wide as 'CHAR_BIT'}}
+ using m2 = _BitInt(7) __attribute__((matrix_type(4, 4))); // expected-error{{'_BitInt' matrix element width must be at least as wide as 'CHAR_BIT'}}
+ using m3 = _BitInt(9) __attribute__((matrix_type(4, 4))); // expected-error{{'_BitInt' matrix element width must be a power of 2}}
+ using m4 = _BitInt(12) __attribute__((matrix_type(4, 4))); // expected-error{{'_BitInt' matrix element width must be a power of 2}}
+ using m5 = _BitInt(8) __attribute__((matrix_type(4, 4)));
+ using m6 = _BitInt(64) __attribute__((matrix_type(4, 4)));
+ using m7 = _BitInt(256) __attribute__((matrix_type(4, 4)));
+}
>From 00fc390b339863f6e6655ccbdde99fda4932051f Mon Sep 17 00:00:00 2001
From: Sirraide <aeternalmail at gmail.com>
Date: Sun, 24 Nov 2024 15:04:14 +0100
Subject: [PATCH 2/3] Remove -pedantic
---
clang/test/SemaCXX/matrix-type.cpp | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/clang/test/SemaCXX/matrix-type.cpp b/clang/test/SemaCXX/matrix-type.cpp
index d347332ee2d60f..bb7a8421ca9e37 100644
--- a/clang/test/SemaCXX/matrix-type.cpp
+++ b/clang/test/SemaCXX/matrix-type.cpp
@@ -1,4 +1,4 @@
-// RUN: %clang_cc1 -fsyntax-only -pedantic -fenable-matrix -std=c++11 -verify -triple x86_64-apple-darwin %s
+// RUN: %clang_cc1 -fsyntax-only -fenable-matrix -std=c++11 -verify -triple x86_64-apple-darwin %s
using matrix_double_t = double __attribute__((matrix_type(6, 6)));
using matrix_float_t = float __attribute__((matrix_type(6, 6)));
>From 4e4fdb98a7e1f0ac4f857838620b5af29e5b6195 Mon Sep 17 00:00:00 2001
From: Sirraide <aeternalmail at gmail.com>
Date: Mon, 25 Nov 2024 12:32:24 +0100
Subject: [PATCH 3/3] Apply feedback from code review
---
clang/lib/Sema/SemaType.cpp | 16 ++++++++--------
1 file changed, 8 insertions(+), 8 deletions(-)
diff --git a/clang/lib/Sema/SemaType.cpp b/clang/lib/Sema/SemaType.cpp
index 06b779f5ef3aa2..03308c067a9c8f 100644
--- a/clang/lib/Sema/SemaType.cpp
+++ b/clang/lib/Sema/SemaType.cpp
@@ -2312,15 +2312,14 @@ QualType Sema::BuildArrayType(QualType T, ArraySizeModifier ASM,
return T;
}
-bool CheckBitIntElementType(Sema &S, SourceLocation AttrLoc,
- const BitIntType *BIT, bool ForMatrixType = false) {
+static bool CheckBitIntElementType(Sema &S, SourceLocation AttrLoc,
+ const BitIntType *BIT,
+ bool ForMatrixType = false) {
// Only support _BitInt elements with byte-sized power of 2 NumBits.
unsigned NumBits = BIT->getNumBits();
- if (!llvm::isPowerOf2_32(NumBits) || NumBits < 8) {
- S.Diag(AttrLoc, diag::err_attribute_invalid_bitint_vector_type)
- << ForMatrixType << (NumBits < 8);
- return true;
- }
+ if (!llvm::isPowerOf2_32(NumBits) || NumBits < 8)
+ return S.Diag(AttrLoc, diag::err_attribute_invalid_bitint_vector_type)
+ << ForMatrixType << (NumBits < 8);
return false;
}
@@ -2457,7 +2456,8 @@ QualType Sema::BuildMatrixType(QualType ElementTy, Expr *NumRows, Expr *NumCols,
}
if (const auto *BIT = ElementTy->getAs<BitIntType>();
- BIT && CheckBitIntElementType(*this, AttrLoc, BIT, true))
+ BIT &&
+ CheckBitIntElementType(*this, AttrLoc, BIT, /*ForMatrixType=*/true))
return QualType();
if (NumRows->isTypeDependent() || NumCols->isTypeDependent() ||
More information about the cfe-commits
mailing list