Skip to content

Commit 12762b8

Browse files
authored
[Clang][Interp] Diagnose use sizeless vector type as the argument of __builtin_vectorelements (llvm#99794)
This PR implement an opcode to diagnose use sizeless vector type as the argument of `__builtin_vectorelements`. --------- Signed-off-by: yronglin <[email protected]>
1 parent b15aa7f commit 12762b8

File tree

4 files changed

+14
-3
lines changed

4 files changed

+14
-3
lines changed

clang/lib/AST/Interp/Compiler.cpp

Lines changed: 1 addition & 3 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).
17041701
assert(E->getTypeOfArgument()->isSizelessVectorType());
1702+
return this->emitSizelessVectorElementSize(E);
17051703
}
17061704

17071705
if (Kind == UETT_VecStep) {

clang/lib/AST/Interp/Interp.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2736,6 +2736,15 @@ inline bool InvalidDeclRef(InterpState &S, CodePtr OpPC,
27362736
return CheckDeclRef(S, OpPC, DR);
27372737
}
27382738

2739+
inline bool SizelessVectorElementSize(InterpState &S, CodePtr OpPC) {
2740+
if (S.inConstantContext()) {
2741+
const SourceRange &ArgRange = S.Current->getRange(OpPC);
2742+
const Expr *E = S.Current->getExpr(OpPC);
2743+
S.CCEDiag(E, diag::note_constexpr_non_const_vectorelements) << ArgRange;
2744+
}
2745+
return false;
2746+
}
2747+
27392748
inline bool Assume(InterpState &S, CodePtr OpPC) {
27402749
const auto Val = S.Stk.pop<Boolean>();
27412750

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: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
// RUN: %clang_cc1 -triple x86_64 -std=c++20 -fsyntax-only -verify -disable-llvm-passes %s
2+
// RUN: %clang_cc1 -triple x86_64 -std=c++20 -fsyntax-only -verify -disable-llvm-passes -fexperimental-new-constant-interpreter %s
23

34
// REQUIRES: aarch64-registered-target
45
// RUN: %clang_cc1 -triple aarch64 -target-feature +sve -std=c++20 -fsyntax-only -verify -disable-llvm-passes %s
6+
// RUN: %clang_cc1 -triple aarch64 -target-feature +sve -std=c++20 -fsyntax-only -verify -disable-llvm-passes -fexperimental-new-constant-interpreter %s
57

68
template <typename T>
79
using VecT __attribute__((vector_size(16))) = T;

0 commit comments

Comments
 (0)