Skip to content

Commit 163c920

Browse files
committed
[Clang][Interp] Diagnose use sizeless vector type as the argument of __builtin_vectorelements
Signed-off-by: yronglin <[email protected]>
1 parent b4f3a96 commit 163c920

File tree

4 files changed

+13
-4
lines changed

4 files changed

+13
-4
lines changed

clang/lib/AST/Interp/Compiler.cpp

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1698,10 +1698,8 @@ bool Compiler<Emitter>::VisitUnaryExprOrTypeTraitExpr(
16981698
if (Kind == UETT_VectorElements) {
16991699
if (const auto *VT = E->getTypeOfArgument()->getAs<VectorType>())
17001700
return this->emitConst(VT->getNumElements(), E);
1701-
1702-
// FIXME: Apparently we need to catch the fact that a sizeless vector type
1703-
// has been passed and diagnose that (at run time).
1704-
assert(E->getTypeOfArgument()->isSizelessVectorType());
1701+
if (E->getTypeOfArgument()->isSizelessVectorType())
1702+
return this->emitSizelessVectorElementSize(E);
17051703
}
17061704

17071705
if (Kind == UETT_VecStep) {

clang/lib/AST/Interp/Interp.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
#include "State.h"
3030
#include "clang/AST/ASTContext.h"
3131
#include "clang/AST/Expr.h"
32+
#include "clang/Basic/DiagnosticSema.h"
3233
#include "llvm/ADT/APFloat.h"
3334
#include "llvm/ADT/APSInt.h"
3435
#include <type_traits>
@@ -2735,6 +2736,13 @@ inline bool InvalidDeclRef(InterpState &S, CodePtr OpPC,
27352736
return CheckDeclRef(S, OpPC, DR);
27362737
}
27372738

2739+
inline bool SizelessVectorElementSize(InterpState &S, CodePtr OpPC) {
2740+
const SourceRange &ArgRange = S.Current->getRange(OpPC);
2741+
const Expr *E = S.Current->getExpr(OpPC);
2742+
S.FFDiag(E, diag::note_constexpr_non_const_vectorelements) << ArgRange;
2743+
return false;
2744+
}
2745+
27382746
inline bool Assume(InterpState &S, CodePtr OpPC) {
27392747
const auto Val = S.Stk.pop<Boolean>();
27402748

clang/lib/AST/Interp/Opcodes.td

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -733,6 +733,8 @@ def InvalidDeclRef : Opcode {
733733
let Args = [ArgDeclRef];
734734
}
735735

736+
def SizelessVectorElementSize : Opcode;
737+
736738
def Assume : Opcode;
737739

738740
def ArrayDecay : Opcode;

clang/test/SemaCXX/builtin_vectorelements.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
// REQUIRES: aarch64-registered-target
44
// RUN: %clang_cc1 -triple aarch64 -target-feature +sve -std=c++20 -fsyntax-only -verify -disable-llvm-passes %s
5+
// RUN: %clang_cc1 -triple aarch64 -target-feature +sve -std=c++20 -fsyntax-only -verify -disable-llvm-passes -fexperimental-new-constant-interpreter %s
56

67
template <typename T>
78
using VecT __attribute__((vector_size(16))) = T;

0 commit comments

Comments
 (0)