[clang] b1a55d0 - Fix a crash on targets where __bf16 isn't supported

Aaron Ballman via cfe-commits cfe-commits at lists.llvm.org
Wed May 4 13:46:12 PDT 2022


Author: Aaron Ballman
Date: 2022-05-04T16:45:59-04:00
New Revision: b1a55d0895249a493da5a442e44ee0a846410e88

URL: https://github.com/llvm/llvm-project/commit/b1a55d0895249a493da5a442e44ee0a846410e88
DIFF: https://github.com/llvm/llvm-project/commit/b1a55d0895249a493da5a442e44ee0a846410e88.diff

LOG: Fix a crash on targets where __bf16 isn't supported

We'd nondeterministically assert (and later crash) when calculating the size or
alignment of a __bf16 type when the type isn't supported on a target because of
reading uninitialized values. Now we check whether the type is supported first.

Fixes #50171

Added: 
    clang/test/Sema/vector-decl-crash.c

Modified: 
    clang/docs/ReleaseNotes.rst
    clang/lib/AST/ASTContext.cpp
    clang/lib/Sema/SemaType.cpp

Removed: 
    


################################################################################
diff  --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index 3187dad2b729e..04418633ec248 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -136,6 +136,9 @@ Bug Fixes
   floating-point vector type when the width of the initialization is exactly
   the same as the elements of the vector being initialized.
   Fixes `Issue 50216 <https://github.com/llvm/llvm-project/issues/50216>`_.
+- Fixed a crash when the ``__bf16`` type is used such that its size or
+  alignment is calculated on a target which does not support that type. This
+  fixes `Issue 50171 <https://github.com/llvm/llvm-project/issues/50171>`_.
 
 Improvements to Clang's diagnostics
 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

diff  --git a/clang/lib/AST/ASTContext.cpp b/clang/lib/AST/ASTContext.cpp
index 838669107b04c..1ee18f913b914 100644
--- a/clang/lib/AST/ASTContext.cpp
+++ b/clang/lib/AST/ASTContext.cpp
@@ -2118,8 +2118,10 @@ TypeInfo ASTContext::getTypeInfoImpl(const Type *T) const {
       Align = Target->getLongFractAlign();
       break;
     case BuiltinType::BFloat16:
-      Width = Target->getBFloat16Width();
-      Align = Target->getBFloat16Align();
+      if (Target->hasBFloat16Type()) {
+        Width = Target->getBFloat16Width();
+        Align = Target->getBFloat16Align();
+      }
       break;
     case BuiltinType::Float16:
     case BuiltinType::Half:

diff  --git a/clang/lib/Sema/SemaType.cpp b/clang/lib/Sema/SemaType.cpp
index 631a53263431c..15f7d36840bad 100644
--- a/clang/lib/Sema/SemaType.cpp
+++ b/clang/lib/Sema/SemaType.cpp
@@ -2630,7 +2630,7 @@ QualType Sema::BuildVectorType(QualType CurType, Expr *SizeExpr,
     return QualType();
   }
 
-  if (VectorSizeBits % TypeSize) {
+  if (!TypeSize || VectorSizeBits % TypeSize) {
     Diag(AttrLoc, diag::err_attribute_invalid_size)
         << SizeExpr->getSourceRange();
     return QualType();

diff  --git a/clang/test/Sema/vector-decl-crash.c b/clang/test/Sema/vector-decl-crash.c
new file mode 100644
index 0000000000000..5e4b098fee2d3
--- /dev/null
+++ b/clang/test/Sema/vector-decl-crash.c
@@ -0,0 +1,7 @@
+// RUN: %clang_cc1 %s -fsyntax-only -verify -triple x86_64-unknown-unknown
+
+// GH50171
+// This would previously crash when __bf16 was not a supported type.
+__bf16 v64bf __attribute__((vector_size(128))); // expected-error {{__bf16 is not supported on this target}} \
+                                                   expected-error {{vector size not an integral multiple of component size}}
+


        


More information about the cfe-commits mailing list