Skip to content

[Clang][Interp] Diagnose use sizeless vector type as the argument of __builtin_vectorelements #99794

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 3 commits into from
Jul 22, 2024

Conversation

yronglin
Copy link
Contributor

@yronglin yronglin commented Jul 21, 2024

This PR implement an opcode to diagnose use sizeless vector type as the argument of __builtin_vectorelements.

@yronglin yronglin requested a review from tbaederr as a code owner July 21, 2024 02:57
@llvmbot llvmbot added clang Clang issues not falling into any other category clang:frontend Language frontend issues, e.g. anything involving "Sema" labels Jul 21, 2024
@llvmbot
Copy link
Member

llvmbot commented Jul 21, 2024

@llvm/pr-subscribers-clang

Author: None (yronglin)

Changes

Full diff: https://github.com/llvm/llvm-project/pull/99794.diff

4 Files Affected:

  • (modified) clang/lib/AST/Interp/Compiler.cpp (+2-4)
  • (modified) clang/lib/AST/Interp/Interp.h (+8)
  • (modified) clang/lib/AST/Interp/Opcodes.td (+2)
  • (modified) clang/test/SemaCXX/builtin_vectorelements.cpp (+1)
diff --git a/clang/lib/AST/Interp/Compiler.cpp b/clang/lib/AST/Interp/Compiler.cpp
index ef579bc5d8972..aeefaf89cfc4b 100644
--- a/clang/lib/AST/Interp/Compiler.cpp
+++ b/clang/lib/AST/Interp/Compiler.cpp
@@ -1698,10 +1698,8 @@ bool Compiler<Emitter>::VisitUnaryExprOrTypeTraitExpr(
   if (Kind == UETT_VectorElements) {
     if (const auto *VT = E->getTypeOfArgument()->getAs<VectorType>())
       return this->emitConst(VT->getNumElements(), E);
-
-    // FIXME: Apparently we need to catch the fact that a sizeless vector type
-    // has been passed and diagnose that (at run time).
-    assert(E->getTypeOfArgument()->isSizelessVectorType());
+    if (E->getTypeOfArgument()->isSizelessVectorType())
+      return this->emitSizelessVectorElementSize(E);
   }
 
   if (Kind == UETT_VecStep) {
diff --git a/clang/lib/AST/Interp/Interp.h b/clang/lib/AST/Interp/Interp.h
index f86b787fb034e..be224a7b0dfd0 100644
--- a/clang/lib/AST/Interp/Interp.h
+++ b/clang/lib/AST/Interp/Interp.h
@@ -29,6 +29,7 @@
 #include "State.h"
 #include "clang/AST/ASTContext.h"
 #include "clang/AST/Expr.h"
+#include "clang/Basic/DiagnosticSema.h"
 #include "llvm/ADT/APFloat.h"
 #include "llvm/ADT/APSInt.h"
 #include <type_traits>
@@ -870,6 +871,13 @@ bool Comp(InterpState &S, CodePtr OpPC) {
   return false;
 }
 
+inline bool SizelessVectorElementSize(InterpState &S, CodePtr OpPC) {
+  const SourceRange &ArgRange = S.Current->getRange(OpPC);
+  const Expr *E = S.Current->getExpr(OpPC);
+  S.FFDiag(E, diag::note_constexpr_non_const_vectorelements) << ArgRange;
+  return false;
+}
+
 //===----------------------------------------------------------------------===//
 // EQ, NE, GT, GE, LT, LE
 //===----------------------------------------------------------------------===//
diff --git a/clang/lib/AST/Interp/Opcodes.td b/clang/lib/AST/Interp/Opcodes.td
index 49ebb156ab2fb..9f29fa9272711 100644
--- a/clang/lib/AST/Interp/Opcodes.td
+++ b/clang/lib/AST/Interp/Opcodes.td
@@ -733,6 +733,8 @@ def InvalidDeclRef : Opcode {
   let Args = [ArgDeclRef];
 }
 
+def SizelessVectorElementSize : Opcode;
+
 def Assume : Opcode;
 
 def ArrayDecay : Opcode;
diff --git a/clang/test/SemaCXX/builtin_vectorelements.cpp b/clang/test/SemaCXX/builtin_vectorelements.cpp
index 59ff09ac72e42..197ab2fd9625e 100644
--- a/clang/test/SemaCXX/builtin_vectorelements.cpp
+++ b/clang/test/SemaCXX/builtin_vectorelements.cpp
@@ -2,6 +2,7 @@
 
 // REQUIRES: aarch64-registered-target
 // RUN: %clang_cc1 -triple aarch64 -target-feature +sve -std=c++20 -fsyntax-only -verify -disable-llvm-passes %s
+// RUN: %clang_cc1 -triple aarch64 -target-feature +sve -std=c++20 -fsyntax-only -verify -disable-llvm-passes -fexperimental-new-constant-interpreter %s
 
 template <typename T>
 using VecT __attribute__((vector_size(16))) = T;

@yronglin yronglin force-pushed the builtin_vectorelements branch from 6d9dbc3 to 163c920 Compare July 21, 2024 03:16
Signed-off-by: yronglin <[email protected]>
Signed-off-by: yronglin <[email protected]>
Copy link
Contributor

@tbaederr tbaederr left a comment

Choose a reason for hiding this comment

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

LGTM, thanks!

@yronglin yronglin merged commit 12762b8 into llvm:main Jul 22, 2024
7 checks passed
yuxuanchen1997 pushed a commit that referenced this pull request Jul 25, 2024
…`__builtin_vectorelements` (#99794)

Summary:
This PR implement an opcode to diagnose use sizeless vector type as the
argument of `__builtin_vectorelements`.

---------

Signed-off-by: yronglin <[email protected]>

Test Plan: 

Reviewers: 

Subscribers: 

Tasks: 

Tags: 


Differential Revision: https://phabricator.intern.facebook.com/D60251513
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
clang:frontend Language frontend issues, e.g. anything involving "Sema" clang Clang issues not falling into any other category
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants