[lld] [lldb] [clang-tools-extra] [libunwind] [flang] [llvm] [clang] [libc] [libcxx] [compiler-rt] [Clang] Add __builtin_vectorelements to get number of elements in vector (PR #69010)

2023-11-20 Thread Lawrence Benson via cfe-commits


@@ -5126,6 +5126,14 @@ void CXXNameMangler::mangleExpression(const Expr *E, 
unsigned Arity,
   Diags.Report(DiagID);
   return;
 }
+case UETT_VectorElements: {

lawben wrote:

@philnik777 I'm adding you here because of your recent commit that adds 
`__datasizeof` (#67805). This is essentially the same code path and you also 
specify the "cannot mangle" for Itanium but not for Microsoft. Do you know if 
this is needed for Microsoft (see comments above)? 

I'm happy to address this for `__builtin_vectorelements`, but I really don't 
know where to start and `__datasizeof` seems to be the same.

https://github.com/llvm/llvm-project/pull/69010
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [Clang] Add __builtin_vectorelements to get number of elements in vector (PR #69010)

2023-10-19 Thread Lawrence Benson via cfe-commits

lawben wrote:

As nobody has come forward in the the last two days, I'm gonna merge this now. 
I'll have a look a mangling this for Microsoft in a follow-up, as this may 
require a few changes. I'm not yet sure what has to be changed for mangling.

https://github.com/llvm/llvm-project/pull/69010
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [Clang] Add __builtin_vectorelements to get number of elements in vector (PR #69010)

2023-10-19 Thread Lawrence Benson via cfe-commits

https://github.com/lawben closed https://github.com/llvm/llvm-project/pull/69010
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[libunwind] [Clang] Add __builtin_vectorelements to get number of elements in vector (PR #69010)

2023-10-19 Thread Lawrence Benson via cfe-commits

https://github.com/lawben closed https://github.com/llvm/llvm-project/pull/69010
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang-tools-extra] [Clang] Add __builtin_vectorelements to get number of elements in vector (PR #69010)

2023-10-19 Thread Lawrence Benson via cfe-commits

https://github.com/lawben closed https://github.com/llvm/llvm-project/pull/69010
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] Fix __builtin_vectorelements tests with REQUIRES (PR #69582)

2023-10-19 Thread Lawrence Benson via cfe-commits

https://github.com/lawben created 
https://github.com/llvm/llvm-project/pull/69582

Small fix for failing tests after merge of #69010. The tests need `REQUIRES` to 
ensure that the correct headers are available. I've also added a generic x86 
build which does not need headers, so there is at least one run per test.

Side note: I'm still quite new to the LLVM test setup. _a)_ Is this the correct 
way to do this and _b)_ canI trigger the full tests before merging to main to 
avoid a second set of failed buildbots?

>From 3718ce234c07f433264798d5459e3f4787235d1e Mon Sep 17 00:00:00 2001
From: Lawrence Benson 
Date: Thu, 19 Oct 2023 11:59:33 +0200
Subject: [PATCH] Fix builtin_vectorelements tests with REQUIRES

---
 clang/test/CodeGen/builtin_vectorelements.c   | 20 ---
 clang/test/SemaCXX/builtin_vectorelements.cpp |  3 +++
 2 files changed, 16 insertions(+), 7 deletions(-)

diff --git a/clang/test/CodeGen/builtin_vectorelements.c 
b/clang/test/CodeGen/builtin_vectorelements.c
index a825ab2b7273d52..06d9ee7e056a83e 100644
--- a/clang/test/CodeGen/builtin_vectorelements.c
+++ b/clang/test/CodeGen/builtin_vectorelements.c
@@ -1,10 +1,17 @@
-// RUN: %clang_cc1 -O1 -triple aarch64 -target-feature +neon %s -emit-llvm 
-disable-llvm-passes -o - | FileCheck --check-prefixes=CHECK,NEON %s
-// RUN: %clang_cc1 -O1 -triple aarch64 -target-feature +sve  %s -emit-llvm 
-disable-llvm-passes -o - | FileCheck --check-prefixes=CHECK,SVE  %s
-// RUN: %clang_cc1 -O1 -triple riscv64 -target-feature +v%s -emit-llvm 
-disable-llvm-passes -o - | FileCheck --check-prefixes=CHECK,RISCV  %s
+// RUN: %clang_cc1 -O1 -triple x86_64%s -emit-llvm 
-disable-llvm-passes -o - | FileCheck --check-prefixes=CHECK   %s
 
-// Note that this does not make sense to check for x86 SIMD types, because
-// __m128i, __m256i, and __m512i do not specify the element type. There are no
-// "logical" number of elements in them.
+// REQUIRES: target=aarch64-{{.*}}
+// RUN: %clang_cc1 -O1 -triple aarch64 -target-feature +neon %s -emit-llvm 
-disable-llvm-passes -o - | FileCheck --check-prefixes=CHECK,NEON  %s
+
+// REQUIRES: target=aarch64-{{.*}}
+// RUN: %clang_cc1 -O1 -triple aarch64 -target-feature +sve  %s -emit-llvm 
-disable-llvm-passes -o - | FileCheck --check-prefixes=CHECK,SVE   %s
+
+// REQUIRES: target=riscv64{{.*}}
+// RUN: %clang_cc1 -O1 -triple riscv64 -target-feature +v%s -emit-llvm 
-disable-llvm-passes -o - | FileCheck --check-prefixes=CHECK,RISCV %s
+
+/// Note that this does not make sense to check for x86 SIMD types, because
+/// __m128i, __m256i, and __m512i do not specify the element type. There are no
+/// "logical" number of elements in them.
 
 typedef int int1 __attribute__((vector_size(4)));
 typedef int int4 __attribute__((vector_size(16)));
@@ -56,7 +63,6 @@ int test_builtin_vectorelements_multiply_constant() {
   return __builtin_vectorelements(int16) * 2;
 }
 
-
 #if defined(__ARM_NEON)
 #include 
 
diff --git a/clang/test/SemaCXX/builtin_vectorelements.cpp 
b/clang/test/SemaCXX/builtin_vectorelements.cpp
index 423051def7f7c29..f40ba2a902cb5fc 100644
--- a/clang/test/SemaCXX/builtin_vectorelements.cpp
+++ b/clang/test/SemaCXX/builtin_vectorelements.cpp
@@ -1,3 +1,6 @@
+// RUN: %clang_cc1 -triple x86_64   -std=c++20 
-fsyntax-only -verify -disable-llvm-passes %s
+
+// REQUIRES: target=aarch64-{{.*}}
 // RUN: %clang_cc1 -triple aarch64 -target-feature +sve -std=c++20 
-fsyntax-only -verify -disable-llvm-passes %s
 
 template 

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [Clang] Add __builtin_vectorelements to get number of elements in vector (PR #69010)

2023-10-19 Thread Lawrence Benson via cfe-commits

lawben wrote:

@tbaederr Jupp, I'm on it. I was not aware that I had to add `REQUIRES` to all 
the tests for it to find the header files. I assumed they are always available. 
See #69582.

https://github.com/llvm/llvm-project/pull/69010
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] Fix __builtin_vectorelements tests with REQUIRES (PR #69582)

2023-10-19 Thread Lawrence Benson via cfe-commits

https://github.com/lawben closed https://github.com/llvm/llvm-project/pull/69582
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] Fix __builtin_vectorelements tests with REQUIRES (PR #69582)

2023-10-19 Thread Lawrence Benson via cfe-commits

lawben wrote:

@nikic Sorry for the mess :/ 

I took the `target=` approach from 
[here](https://llvm.org/docs/TestingGuide.html#constraining-test-execution). 
Following the current buildbot status, this also does not seem to crash. Does 
this mean the rests are still incorrect and are not being executed at all? My 
local setup has all targets enabled, so it's hard for me to reproduce this 
locally. 

If this is still incorrect, I'd kindly ask you to point out the correct way of 
doing this across all tests. I guess it would have `REQUIRES: 
aarch64-registered-target` for AArch64 and `REQUIRES: 
riscv64-registered-target` for RISCV?

https://github.com/llvm/llvm-project/pull/69582
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [Clang] Actually fix tests for __builtin_vectorelements (PR #69589)

2023-10-19 Thread Lawrence Benson via cfe-commits

https://github.com/lawben created 
https://github.com/llvm/llvm-project/pull/69589

In #69582, I accidentally disabled all tests for the changed introduced in 
#69010. This change should use the correct `REQUIRES` syntax to en-/disable 
target-specific tests.

>From 52c2267696651c533d9ffa8f511047960e459155 Mon Sep 17 00:00:00 2001
From: Lawrence Benson 
Date: Thu, 19 Oct 2023 12:45:57 +0200
Subject: [PATCH] Actually fix tests for __builtin_vectorelements

---
 clang/test/CodeGen/builtin_vectorelements.c   | 6 +++---
 clang/test/SemaCXX/builtin_vectorelements.cpp | 2 +-
 2 files changed, 4 insertions(+), 4 deletions(-)

diff --git a/clang/test/CodeGen/builtin_vectorelements.c 
b/clang/test/CodeGen/builtin_vectorelements.c
index 06d9ee7e056a83e..b0ff6f83b1e4adb 100644
--- a/clang/test/CodeGen/builtin_vectorelements.c
+++ b/clang/test/CodeGen/builtin_vectorelements.c
@@ -1,12 +1,12 @@
 // RUN: %clang_cc1 -O1 -triple x86_64%s -emit-llvm 
-disable-llvm-passes -o - | FileCheck --check-prefixes=CHECK   %s
 
-// REQUIRES: target=aarch64-{{.*}}
+// REQUIRES: aarch64-registered-target
 // RUN: %clang_cc1 -O1 -triple aarch64 -target-feature +neon %s -emit-llvm 
-disable-llvm-passes -o - | FileCheck --check-prefixes=CHECK,NEON  %s
 
-// REQUIRES: target=aarch64-{{.*}}
+// REQUIRES: aarch64-registered-target
 // RUN: %clang_cc1 -O1 -triple aarch64 -target-feature +sve  %s -emit-llvm 
-disable-llvm-passes -o - | FileCheck --check-prefixes=CHECK,SVE   %s
 
-// REQUIRES: target=riscv64{{.*}}
+// REQUIRES: riscv-registered-target
 // RUN: %clang_cc1 -O1 -triple riscv64 -target-feature +v%s -emit-llvm 
-disable-llvm-passes -o - | FileCheck --check-prefixes=CHECK,RISCV %s
 
 /// Note that this does not make sense to check for x86 SIMD types, because
diff --git a/clang/test/SemaCXX/builtin_vectorelements.cpp 
b/clang/test/SemaCXX/builtin_vectorelements.cpp
index f40ba2a902cb5fc..59ff09ac72e42d7 100644
--- a/clang/test/SemaCXX/builtin_vectorelements.cpp
+++ b/clang/test/SemaCXX/builtin_vectorelements.cpp
@@ -1,6 +1,6 @@
 // RUN: %clang_cc1 -triple x86_64   -std=c++20 
-fsyntax-only -verify -disable-llvm-passes %s
 
-// REQUIRES: target=aarch64-{{.*}}
+// REQUIRES: aarch64-registered-target
 // RUN: %clang_cc1 -triple aarch64 -target-feature +sve -std=c++20 
-fsyntax-only -verify -disable-llvm-passes %s
 
 template 

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [Clang] Actually fix tests for __builtin_vectorelements (PR #69589)

2023-10-19 Thread Lawrence Benson via cfe-commits

lawben wrote:

@nikic Could you please double-check if the syntax here is correct. After the 
mistake in #69582, I'm a bit nervous about merging a wrong fix again... 😅 

https://github.com/llvm/llvm-project/pull/69589
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [Clang] Actually fix tests for __builtin_vectorelements (PR #69589)

2023-10-19 Thread Lawrence Benson via cfe-commits

https://github.com/lawben closed https://github.com/llvm/llvm-project/pull/69589
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [Clang] Add __builtin_vectorelements to get number of elements in vector (PR #69010)

2023-10-13 Thread Lawrence Benson via cfe-commits

https://github.com/lawben created 
https://github.com/llvm/llvm-project/pull/69010

Adds a new `__builtin_vectorelements()` function which returns the number of 
elements for a given vector either at compile-time for fixed-sized vectors, 
e.g., via `__attribute__((vector_size(N))` or runtime via a call to 
`@llvm.vscale.i32()` for scalable vectors, e.g., SVE or RISCV V.

The new builtin follows a similar path as `sizeof()`, as it essentially does 
the same thing but for the number of elements in vector instead of the number 
of bytes. This allows us to re-use a lot of the existing logic to handle types 
etc. 

A small side addition is `Type::isSizelessVectorType()`, which we need to 
distinguish between sizeless vectors (SVE, RISCV V) and sizeless types (WASM). 

This is the [corresponding 
discussion](https://discourse.llvm.org/t/new-builtin-function-to-get-number-of-lanes-in-simd-vectors/73911).
 

>From df8d0a53a31e1351bb6cd3b340e9012b489e9885 Mon Sep 17 00:00:00 2001
From: Lawrence Benson 
Date: Wed, 11 Oct 2023 17:26:11 +0200
Subject: [PATCH 1/4] Add __builtin_vectorelements to get the number of
 elements in a fixed-sized vector at compile-time or via a @llvm.vscale call
 at runtime.

---
 clang/include/clang/AST/Type.h   |  3 +++
 clang/include/clang/Basic/Builtins.def   |  1 +
 clang/include/clang/Basic/TokenKinds.def |  1 +
 clang/lib/AST/ExprConstant.cpp   |  8 
 clang/lib/AST/ItaniumMangle.cpp  |  8 
 clang/lib/AST/Type.cpp   |  6 +-
 clang/lib/CodeGen/CGExprScalar.cpp   | 12 
 clang/lib/Parse/ParseExpr.cpp|  7 +--
 clang/lib/Sema/SemaChecking.cpp  | 18 ++
 clang/lib/Sema/SemaExpr.cpp  | 14 ++
 10 files changed, 75 insertions(+), 3 deletions(-)

diff --git a/clang/include/clang/AST/Type.h b/clang/include/clang/AST/Type.h
index a78d8f60462b231..f6e425783176ba2 100644
--- a/clang/include/clang/AST/Type.h
+++ b/clang/include/clang/AST/Type.h
@@ -2058,6 +2058,9 @@ class alignas(8) Type : public ExtQualsTypeCommonBase {
   bool isSizelessType() const;
   bool isSizelessBuiltinType() const;
 
+  /// Returns true for all scalable vector types.
+  bool isSizelessVectorType() const;
+
   /// Returns true for SVE scalable vector types.
   bool isSVESizelessBuiltinType() const;
 
diff --git a/clang/include/clang/Basic/Builtins.def 
b/clang/include/clang/Basic/Builtins.def
index 6ea8484606cfd5d..6033e8a955fb8bd 100644
--- a/clang/include/clang/Basic/Builtins.def
+++ b/clang/include/clang/Basic/Builtins.def
@@ -674,6 +674,7 @@ BUILTIN(__builtin_debugtrap, "v", "n")
 BUILTIN(__builtin_unreachable, "v", "nr")
 BUILTIN(__builtin_shufflevector, "v."   , "nct")
 BUILTIN(__builtin_convertvector, "v."   , "nct")
+BUILTIN(__builtin_vectorelements, "v."  , "nct")
 BUILTIN(__builtin_alloca, "v*z"   , "Fn")
 BUILTIN(__builtin_alloca_uninitialized, "v*z", "Fn")
 BUILTIN(__builtin_alloca_with_align, "v*zIz", "Fn")
diff --git a/clang/include/clang/Basic/TokenKinds.def 
b/clang/include/clang/Basic/TokenKinds.def
index 94db56a9fd5d78c..bbae1200d376c0d 100644
--- a/clang/include/clang/Basic/TokenKinds.def
+++ b/clang/include/clang/Basic/TokenKinds.def
@@ -746,6 +746,7 @@ ALIAS("_pascal"  , __pascal   , KEYBORLAND)
 
 // Clang Extensions.
 KEYWORD(__builtin_convertvector  , KEYALL)
+UNARY_EXPR_OR_TYPE_TRAIT(__builtin_vectorelements, VectorElements, KEYALL)
 ALIAS("__char16_t"   , char16_t  , KEYCXX)
 ALIAS("__char32_t"   , char32_t  , KEYCXX)
 KEYWORD(__builtin_bit_cast   , KEYALL)
diff --git a/clang/lib/AST/ExprConstant.cpp b/clang/lib/AST/ExprConstant.cpp
index e5539dedec02a4b..eb36a57e462f3f1 100644
--- a/clang/lib/AST/ExprConstant.cpp
+++ b/clang/lib/AST/ExprConstant.cpp
@@ -13595,6 +13595,14 @@ bool IntExprEvaluator::VisitUnaryExprOrTypeTraitExpr(
 Info.Ctx.getOpenMPDefaultSimdAlign(E->getArgumentType()))
 .getQuantity(),
 E);
+  case UETT_VectorElements: {
+QualType Ty = E->getTypeOfArgument();
+// If the vector has a fixed size, we can determine the number of elements 
at compile time.
+if (Ty->isVectorType())
+  return Success(Ty->castAs()->getNumElements(), E);
+
+return false;
+  }
   }
 
   llvm_unreachable("unknown expr/type trait");
diff --git a/clang/lib/AST/ItaniumMangle.cpp b/clang/lib/AST/ItaniumMangle.cpp
index 23ec35cae4b7b40..171dfe429c12d31 100644
--- a/clang/lib/AST/ItaniumMangle.cpp
+++ b/clang/lib/AST/ItaniumMangle.cpp
@@ -5126,6 +5126,14 @@ void CXXNameMangler::mangleExpression(const Expr *E, 
unsigned Arity,
   Diags.Report(DiagID);
   return;
 }
+case UETT_VectorElements: {
+  DiagnosticsEngine &Diags = Context.getDiags();
+  unsigned DiagID = Diags.getCustomDiagID(
+  DiagnosticsEngine::Error,
+  "cannot yet mangle __builtin_vectorelements expression");
+  Diags.Report(DiagID);
+  return;
+}
 }
 break;
 

[clang] [Clang] Add __builtin_vectorelements to get number of elements in vector (PR #69010)

2023-10-13 Thread Lawrence Benson via cfe-commits

https://github.com/lawben edited https://github.com/llvm/llvm-project/pull/69010
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [Clang] Add __builtin_vectorelements to get number of elements in vector (PR #69010)

2023-10-13 Thread Lawrence Benson via cfe-commits

https://github.com/lawben edited https://github.com/llvm/llvm-project/pull/69010
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [Clang] Add __builtin_vectorelements to get number of elements in vector (PR #69010)

2023-10-13 Thread Lawrence Benson via cfe-commits

lawben wrote:

@erichkeane This is my first PR to the frontend side of LLVM. Please let me 
know if there is something missing for a Clang PR that I should add.

https://github.com/llvm/llvm-project/pull/69010
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [Clang] Add __builtin_vectorelements to get number of elements in vector (PR #69010)

2023-10-13 Thread Lawrence Benson via cfe-commits

https://github.com/lawben updated 
https://github.com/llvm/llvm-project/pull/69010

>From df8d0a53a31e1351bb6cd3b340e9012b489e9885 Mon Sep 17 00:00:00 2001
From: Lawrence Benson 
Date: Wed, 11 Oct 2023 17:26:11 +0200
Subject: [PATCH 1/5] Add __builtin_vectorelements to get the number of
 elements in a fixed-sized vector at compile-time or via a @llvm.vscale call
 at runtime.

---
 clang/include/clang/AST/Type.h   |  3 +++
 clang/include/clang/Basic/Builtins.def   |  1 +
 clang/include/clang/Basic/TokenKinds.def |  1 +
 clang/lib/AST/ExprConstant.cpp   |  8 
 clang/lib/AST/ItaniumMangle.cpp  |  8 
 clang/lib/AST/Type.cpp   |  6 +-
 clang/lib/CodeGen/CGExprScalar.cpp   | 12 
 clang/lib/Parse/ParseExpr.cpp|  7 +--
 clang/lib/Sema/SemaChecking.cpp  | 18 ++
 clang/lib/Sema/SemaExpr.cpp  | 14 ++
 10 files changed, 75 insertions(+), 3 deletions(-)

diff --git a/clang/include/clang/AST/Type.h b/clang/include/clang/AST/Type.h
index a78d8f60462b231..f6e425783176ba2 100644
--- a/clang/include/clang/AST/Type.h
+++ b/clang/include/clang/AST/Type.h
@@ -2058,6 +2058,9 @@ class alignas(8) Type : public ExtQualsTypeCommonBase {
   bool isSizelessType() const;
   bool isSizelessBuiltinType() const;
 
+  /// Returns true for all scalable vector types.
+  bool isSizelessVectorType() const;
+
   /// Returns true for SVE scalable vector types.
   bool isSVESizelessBuiltinType() const;
 
diff --git a/clang/include/clang/Basic/Builtins.def 
b/clang/include/clang/Basic/Builtins.def
index 6ea8484606cfd5d..6033e8a955fb8bd 100644
--- a/clang/include/clang/Basic/Builtins.def
+++ b/clang/include/clang/Basic/Builtins.def
@@ -674,6 +674,7 @@ BUILTIN(__builtin_debugtrap, "v", "n")
 BUILTIN(__builtin_unreachable, "v", "nr")
 BUILTIN(__builtin_shufflevector, "v."   , "nct")
 BUILTIN(__builtin_convertvector, "v."   , "nct")
+BUILTIN(__builtin_vectorelements, "v."  , "nct")
 BUILTIN(__builtin_alloca, "v*z"   , "Fn")
 BUILTIN(__builtin_alloca_uninitialized, "v*z", "Fn")
 BUILTIN(__builtin_alloca_with_align, "v*zIz", "Fn")
diff --git a/clang/include/clang/Basic/TokenKinds.def 
b/clang/include/clang/Basic/TokenKinds.def
index 94db56a9fd5d78c..bbae1200d376c0d 100644
--- a/clang/include/clang/Basic/TokenKinds.def
+++ b/clang/include/clang/Basic/TokenKinds.def
@@ -746,6 +746,7 @@ ALIAS("_pascal"  , __pascal   , KEYBORLAND)
 
 // Clang Extensions.
 KEYWORD(__builtin_convertvector  , KEYALL)
+UNARY_EXPR_OR_TYPE_TRAIT(__builtin_vectorelements, VectorElements, KEYALL)
 ALIAS("__char16_t"   , char16_t  , KEYCXX)
 ALIAS("__char32_t"   , char32_t  , KEYCXX)
 KEYWORD(__builtin_bit_cast   , KEYALL)
diff --git a/clang/lib/AST/ExprConstant.cpp b/clang/lib/AST/ExprConstant.cpp
index e5539dedec02a4b..eb36a57e462f3f1 100644
--- a/clang/lib/AST/ExprConstant.cpp
+++ b/clang/lib/AST/ExprConstant.cpp
@@ -13595,6 +13595,14 @@ bool IntExprEvaluator::VisitUnaryExprOrTypeTraitExpr(
 Info.Ctx.getOpenMPDefaultSimdAlign(E->getArgumentType()))
 .getQuantity(),
 E);
+  case UETT_VectorElements: {
+QualType Ty = E->getTypeOfArgument();
+// If the vector has a fixed size, we can determine the number of elements 
at compile time.
+if (Ty->isVectorType())
+  return Success(Ty->castAs()->getNumElements(), E);
+
+return false;
+  }
   }
 
   llvm_unreachable("unknown expr/type trait");
diff --git a/clang/lib/AST/ItaniumMangle.cpp b/clang/lib/AST/ItaniumMangle.cpp
index 23ec35cae4b7b40..171dfe429c12d31 100644
--- a/clang/lib/AST/ItaniumMangle.cpp
+++ b/clang/lib/AST/ItaniumMangle.cpp
@@ -5126,6 +5126,14 @@ void CXXNameMangler::mangleExpression(const Expr *E, 
unsigned Arity,
   Diags.Report(DiagID);
   return;
 }
+case UETT_VectorElements: {
+  DiagnosticsEngine &Diags = Context.getDiags();
+  unsigned DiagID = Diags.getCustomDiagID(
+  DiagnosticsEngine::Error,
+  "cannot yet mangle __builtin_vectorelements expression");
+  Diags.Report(DiagID);
+  return;
+}
 }
 break;
   }
diff --git a/clang/lib/AST/Type.cpp b/clang/lib/AST/Type.cpp
index 4c433f7fe9daca0..050761784498a9c 100644
--- a/clang/lib/AST/Type.cpp
+++ b/clang/lib/AST/Type.cpp
@@ -2369,7 +2369,7 @@ bool Type::isIncompleteType(NamedDecl **Def) const {
 }
 
 bool Type::isSizelessBuiltinType() const {
-  if (isSVESizelessBuiltinType() || isRVVSizelessBuiltinType())
+  if (isSizelessVectorType())
 return true;
 
   if (const BuiltinType *BT = getAs()) {
@@ -2403,6 +2403,10 @@ bool Type::isWebAssemblyTableType() const {
 
 bool Type::isSizelessType() const { return isSizelessBuiltinType(); }
 
+bool Type::isSizelessVectorType() const {
+  return isSVESizelessBuiltinType() || isRVVSizelessBuiltinType();
+}
+
 bool Type::isSVESizelessBuiltinType() const {
   if (const BuiltinType *BT = getAs()) {
 switch (BT->getKind()) {
di

[libunwind] [Clang] Add __builtin_vectorelements to get number of elements in vector (PR #69010)

2023-10-13 Thread Lawrence Benson via cfe-commits

https://github.com/lawben updated 
https://github.com/llvm/llvm-project/pull/69010

>From df8d0a53a31e1351bb6cd3b340e9012b489e9885 Mon Sep 17 00:00:00 2001
From: Lawrence Benson 
Date: Wed, 11 Oct 2023 17:26:11 +0200
Subject: [PATCH 1/5] Add __builtin_vectorelements to get the number of
 elements in a fixed-sized vector at compile-time or via a @llvm.vscale call
 at runtime.

---
 clang/include/clang/AST/Type.h   |  3 +++
 clang/include/clang/Basic/Builtins.def   |  1 +
 clang/include/clang/Basic/TokenKinds.def |  1 +
 clang/lib/AST/ExprConstant.cpp   |  8 
 clang/lib/AST/ItaniumMangle.cpp  |  8 
 clang/lib/AST/Type.cpp   |  6 +-
 clang/lib/CodeGen/CGExprScalar.cpp   | 12 
 clang/lib/Parse/ParseExpr.cpp|  7 +--
 clang/lib/Sema/SemaChecking.cpp  | 18 ++
 clang/lib/Sema/SemaExpr.cpp  | 14 ++
 10 files changed, 75 insertions(+), 3 deletions(-)

diff --git a/clang/include/clang/AST/Type.h b/clang/include/clang/AST/Type.h
index a78d8f60462b231..f6e425783176ba2 100644
--- a/clang/include/clang/AST/Type.h
+++ b/clang/include/clang/AST/Type.h
@@ -2058,6 +2058,9 @@ class alignas(8) Type : public ExtQualsTypeCommonBase {
   bool isSizelessType() const;
   bool isSizelessBuiltinType() const;
 
+  /// Returns true for all scalable vector types.
+  bool isSizelessVectorType() const;
+
   /// Returns true for SVE scalable vector types.
   bool isSVESizelessBuiltinType() const;
 
diff --git a/clang/include/clang/Basic/Builtins.def 
b/clang/include/clang/Basic/Builtins.def
index 6ea8484606cfd5d..6033e8a955fb8bd 100644
--- a/clang/include/clang/Basic/Builtins.def
+++ b/clang/include/clang/Basic/Builtins.def
@@ -674,6 +674,7 @@ BUILTIN(__builtin_debugtrap, "v", "n")
 BUILTIN(__builtin_unreachable, "v", "nr")
 BUILTIN(__builtin_shufflevector, "v."   , "nct")
 BUILTIN(__builtin_convertvector, "v."   , "nct")
+BUILTIN(__builtin_vectorelements, "v."  , "nct")
 BUILTIN(__builtin_alloca, "v*z"   , "Fn")
 BUILTIN(__builtin_alloca_uninitialized, "v*z", "Fn")
 BUILTIN(__builtin_alloca_with_align, "v*zIz", "Fn")
diff --git a/clang/include/clang/Basic/TokenKinds.def 
b/clang/include/clang/Basic/TokenKinds.def
index 94db56a9fd5d78c..bbae1200d376c0d 100644
--- a/clang/include/clang/Basic/TokenKinds.def
+++ b/clang/include/clang/Basic/TokenKinds.def
@@ -746,6 +746,7 @@ ALIAS("_pascal"  , __pascal   , KEYBORLAND)
 
 // Clang Extensions.
 KEYWORD(__builtin_convertvector  , KEYALL)
+UNARY_EXPR_OR_TYPE_TRAIT(__builtin_vectorelements, VectorElements, KEYALL)
 ALIAS("__char16_t"   , char16_t  , KEYCXX)
 ALIAS("__char32_t"   , char32_t  , KEYCXX)
 KEYWORD(__builtin_bit_cast   , KEYALL)
diff --git a/clang/lib/AST/ExprConstant.cpp b/clang/lib/AST/ExprConstant.cpp
index e5539dedec02a4b..eb36a57e462f3f1 100644
--- a/clang/lib/AST/ExprConstant.cpp
+++ b/clang/lib/AST/ExprConstant.cpp
@@ -13595,6 +13595,14 @@ bool IntExprEvaluator::VisitUnaryExprOrTypeTraitExpr(
 Info.Ctx.getOpenMPDefaultSimdAlign(E->getArgumentType()))
 .getQuantity(),
 E);
+  case UETT_VectorElements: {
+QualType Ty = E->getTypeOfArgument();
+// If the vector has a fixed size, we can determine the number of elements 
at compile time.
+if (Ty->isVectorType())
+  return Success(Ty->castAs()->getNumElements(), E);
+
+return false;
+  }
   }
 
   llvm_unreachable("unknown expr/type trait");
diff --git a/clang/lib/AST/ItaniumMangle.cpp b/clang/lib/AST/ItaniumMangle.cpp
index 23ec35cae4b7b40..171dfe429c12d31 100644
--- a/clang/lib/AST/ItaniumMangle.cpp
+++ b/clang/lib/AST/ItaniumMangle.cpp
@@ -5126,6 +5126,14 @@ void CXXNameMangler::mangleExpression(const Expr *E, 
unsigned Arity,
   Diags.Report(DiagID);
   return;
 }
+case UETT_VectorElements: {
+  DiagnosticsEngine &Diags = Context.getDiags();
+  unsigned DiagID = Diags.getCustomDiagID(
+  DiagnosticsEngine::Error,
+  "cannot yet mangle __builtin_vectorelements expression");
+  Diags.Report(DiagID);
+  return;
+}
 }
 break;
   }
diff --git a/clang/lib/AST/Type.cpp b/clang/lib/AST/Type.cpp
index 4c433f7fe9daca0..050761784498a9c 100644
--- a/clang/lib/AST/Type.cpp
+++ b/clang/lib/AST/Type.cpp
@@ -2369,7 +2369,7 @@ bool Type::isIncompleteType(NamedDecl **Def) const {
 }
 
 bool Type::isSizelessBuiltinType() const {
-  if (isSVESizelessBuiltinType() || isRVVSizelessBuiltinType())
+  if (isSizelessVectorType())
 return true;
 
   if (const BuiltinType *BT = getAs()) {
@@ -2403,6 +2403,10 @@ bool Type::isWebAssemblyTableType() const {
 
 bool Type::isSizelessType() const { return isSizelessBuiltinType(); }
 
+bool Type::isSizelessVectorType() const {
+  return isSVESizelessBuiltinType() || isRVVSizelessBuiltinType();
+}
+
 bool Type::isSVESizelessBuiltinType() const {
   if (const BuiltinType *BT = getAs()) {
 switch (BT->getKind()) {
di

[clang] [Clang] Add __builtin_vectorelements to get number of elements in vector (PR #69010)

2023-10-13 Thread Lawrence Benson via cfe-commits


@@ -5456,9 +5459,8 @@ class DeducedTemplateSpecializationType : public 
DeducedType,
 /// TemplateArguments, followed by a QualType representing the
 /// non-canonical aliased type when the template is a type alias
 /// template.
-class alignas(8) TemplateSpecializationType
-: public Type,
-  public llvm::FoldingSetNode {
+class alignas(8) TemplateSpecializationType : public Type,

lawben wrote:

This was a clang-format error because of a merge conflict. Reverted it.

https://github.com/llvm/llvm-project/pull/69010
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[libunwind] [Clang] Add __builtin_vectorelements to get number of elements in vector (PR #69010)

2023-10-13 Thread Lawrence Benson via cfe-commits


@@ -10136,6 +10136,9 @@ def err_vec_builtin_incompatible_vector : Error<
 def err_vsx_builtin_nonconstant_argument : Error<
   "argument %0 to %1 must be a 2-bit unsigned literal (i.e. 0, 1, 2 or 3)">;
 
+def err_vectorelements_non_vector : Error<
+ "'__builtin_vectorelements' argument must be a vector">;

lawben wrote:

this was inspired by "`first argument to __builtin_convertvector must be a 
vector`". but if you have a suggestion, i'm happy to change this. 

https://github.com/llvm/llvm-project/pull/69010
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [Clang] Add __builtin_vectorelements to get number of elements in vector (PR #69010)

2023-10-16 Thread Lawrence Benson via cfe-commits

https://github.com/lawben updated 
https://github.com/llvm/llvm-project/pull/69010

>From df8d0a53a31e1351bb6cd3b340e9012b489e9885 Mon Sep 17 00:00:00 2001
From: Lawrence Benson 
Date: Wed, 11 Oct 2023 17:26:11 +0200
Subject: [PATCH 1/6] Add __builtin_vectorelements to get the number of
 elements in a fixed-sized vector at compile-time or via a @llvm.vscale call
 at runtime.

---
 clang/include/clang/AST/Type.h   |  3 +++
 clang/include/clang/Basic/Builtins.def   |  1 +
 clang/include/clang/Basic/TokenKinds.def |  1 +
 clang/lib/AST/ExprConstant.cpp   |  8 
 clang/lib/AST/ItaniumMangle.cpp  |  8 
 clang/lib/AST/Type.cpp   |  6 +-
 clang/lib/CodeGen/CGExprScalar.cpp   | 12 
 clang/lib/Parse/ParseExpr.cpp|  7 +--
 clang/lib/Sema/SemaChecking.cpp  | 18 ++
 clang/lib/Sema/SemaExpr.cpp  | 14 ++
 10 files changed, 75 insertions(+), 3 deletions(-)

diff --git a/clang/include/clang/AST/Type.h b/clang/include/clang/AST/Type.h
index a78d8f60462b231..f6e425783176ba2 100644
--- a/clang/include/clang/AST/Type.h
+++ b/clang/include/clang/AST/Type.h
@@ -2058,6 +2058,9 @@ class alignas(8) Type : public ExtQualsTypeCommonBase {
   bool isSizelessType() const;
   bool isSizelessBuiltinType() const;
 
+  /// Returns true for all scalable vector types.
+  bool isSizelessVectorType() const;
+
   /// Returns true for SVE scalable vector types.
   bool isSVESizelessBuiltinType() const;
 
diff --git a/clang/include/clang/Basic/Builtins.def 
b/clang/include/clang/Basic/Builtins.def
index 6ea8484606cfd5d..6033e8a955fb8bd 100644
--- a/clang/include/clang/Basic/Builtins.def
+++ b/clang/include/clang/Basic/Builtins.def
@@ -674,6 +674,7 @@ BUILTIN(__builtin_debugtrap, "v", "n")
 BUILTIN(__builtin_unreachable, "v", "nr")
 BUILTIN(__builtin_shufflevector, "v."   , "nct")
 BUILTIN(__builtin_convertvector, "v."   , "nct")
+BUILTIN(__builtin_vectorelements, "v."  , "nct")
 BUILTIN(__builtin_alloca, "v*z"   , "Fn")
 BUILTIN(__builtin_alloca_uninitialized, "v*z", "Fn")
 BUILTIN(__builtin_alloca_with_align, "v*zIz", "Fn")
diff --git a/clang/include/clang/Basic/TokenKinds.def 
b/clang/include/clang/Basic/TokenKinds.def
index 94db56a9fd5d78c..bbae1200d376c0d 100644
--- a/clang/include/clang/Basic/TokenKinds.def
+++ b/clang/include/clang/Basic/TokenKinds.def
@@ -746,6 +746,7 @@ ALIAS("_pascal"  , __pascal   , KEYBORLAND)
 
 // Clang Extensions.
 KEYWORD(__builtin_convertvector  , KEYALL)
+UNARY_EXPR_OR_TYPE_TRAIT(__builtin_vectorelements, VectorElements, KEYALL)
 ALIAS("__char16_t"   , char16_t  , KEYCXX)
 ALIAS("__char32_t"   , char32_t  , KEYCXX)
 KEYWORD(__builtin_bit_cast   , KEYALL)
diff --git a/clang/lib/AST/ExprConstant.cpp b/clang/lib/AST/ExprConstant.cpp
index e5539dedec02a4b..eb36a57e462f3f1 100644
--- a/clang/lib/AST/ExprConstant.cpp
+++ b/clang/lib/AST/ExprConstant.cpp
@@ -13595,6 +13595,14 @@ bool IntExprEvaluator::VisitUnaryExprOrTypeTraitExpr(
 Info.Ctx.getOpenMPDefaultSimdAlign(E->getArgumentType()))
 .getQuantity(),
 E);
+  case UETT_VectorElements: {
+QualType Ty = E->getTypeOfArgument();
+// If the vector has a fixed size, we can determine the number of elements 
at compile time.
+if (Ty->isVectorType())
+  return Success(Ty->castAs()->getNumElements(), E);
+
+return false;
+  }
   }
 
   llvm_unreachable("unknown expr/type trait");
diff --git a/clang/lib/AST/ItaniumMangle.cpp b/clang/lib/AST/ItaniumMangle.cpp
index 23ec35cae4b7b40..171dfe429c12d31 100644
--- a/clang/lib/AST/ItaniumMangle.cpp
+++ b/clang/lib/AST/ItaniumMangle.cpp
@@ -5126,6 +5126,14 @@ void CXXNameMangler::mangleExpression(const Expr *E, 
unsigned Arity,
   Diags.Report(DiagID);
   return;
 }
+case UETT_VectorElements: {
+  DiagnosticsEngine &Diags = Context.getDiags();
+  unsigned DiagID = Diags.getCustomDiagID(
+  DiagnosticsEngine::Error,
+  "cannot yet mangle __builtin_vectorelements expression");
+  Diags.Report(DiagID);
+  return;
+}
 }
 break;
   }
diff --git a/clang/lib/AST/Type.cpp b/clang/lib/AST/Type.cpp
index 4c433f7fe9daca0..050761784498a9c 100644
--- a/clang/lib/AST/Type.cpp
+++ b/clang/lib/AST/Type.cpp
@@ -2369,7 +2369,7 @@ bool Type::isIncompleteType(NamedDecl **Def) const {
 }
 
 bool Type::isSizelessBuiltinType() const {
-  if (isSVESizelessBuiltinType() || isRVVSizelessBuiltinType())
+  if (isSizelessVectorType())
 return true;
 
   if (const BuiltinType *BT = getAs()) {
@@ -2403,6 +2403,10 @@ bool Type::isWebAssemblyTableType() const {
 
 bool Type::isSizelessType() const { return isSizelessBuiltinType(); }
 
+bool Type::isSizelessVectorType() const {
+  return isSVESizelessBuiltinType() || isRVVSizelessBuiltinType();
+}
+
 bool Type::isSVESizelessBuiltinType() const {
   if (const BuiltinType *BT = getAs()) {
 switch (BT->getKind()) {
di

[libunwind] [Clang] Add __builtin_vectorelements to get number of elements in vector (PR #69010)

2023-10-16 Thread Lawrence Benson via cfe-commits

https://github.com/lawben updated 
https://github.com/llvm/llvm-project/pull/69010

>From df8d0a53a31e1351bb6cd3b340e9012b489e9885 Mon Sep 17 00:00:00 2001
From: Lawrence Benson 
Date: Wed, 11 Oct 2023 17:26:11 +0200
Subject: [PATCH 1/6] Add __builtin_vectorelements to get the number of
 elements in a fixed-sized vector at compile-time or via a @llvm.vscale call
 at runtime.

---
 clang/include/clang/AST/Type.h   |  3 +++
 clang/include/clang/Basic/Builtins.def   |  1 +
 clang/include/clang/Basic/TokenKinds.def |  1 +
 clang/lib/AST/ExprConstant.cpp   |  8 
 clang/lib/AST/ItaniumMangle.cpp  |  8 
 clang/lib/AST/Type.cpp   |  6 +-
 clang/lib/CodeGen/CGExprScalar.cpp   | 12 
 clang/lib/Parse/ParseExpr.cpp|  7 +--
 clang/lib/Sema/SemaChecking.cpp  | 18 ++
 clang/lib/Sema/SemaExpr.cpp  | 14 ++
 10 files changed, 75 insertions(+), 3 deletions(-)

diff --git a/clang/include/clang/AST/Type.h b/clang/include/clang/AST/Type.h
index a78d8f60462b231..f6e425783176ba2 100644
--- a/clang/include/clang/AST/Type.h
+++ b/clang/include/clang/AST/Type.h
@@ -2058,6 +2058,9 @@ class alignas(8) Type : public ExtQualsTypeCommonBase {
   bool isSizelessType() const;
   bool isSizelessBuiltinType() const;
 
+  /// Returns true for all scalable vector types.
+  bool isSizelessVectorType() const;
+
   /// Returns true for SVE scalable vector types.
   bool isSVESizelessBuiltinType() const;
 
diff --git a/clang/include/clang/Basic/Builtins.def 
b/clang/include/clang/Basic/Builtins.def
index 6ea8484606cfd5d..6033e8a955fb8bd 100644
--- a/clang/include/clang/Basic/Builtins.def
+++ b/clang/include/clang/Basic/Builtins.def
@@ -674,6 +674,7 @@ BUILTIN(__builtin_debugtrap, "v", "n")
 BUILTIN(__builtin_unreachable, "v", "nr")
 BUILTIN(__builtin_shufflevector, "v."   , "nct")
 BUILTIN(__builtin_convertvector, "v."   , "nct")
+BUILTIN(__builtin_vectorelements, "v."  , "nct")
 BUILTIN(__builtin_alloca, "v*z"   , "Fn")
 BUILTIN(__builtin_alloca_uninitialized, "v*z", "Fn")
 BUILTIN(__builtin_alloca_with_align, "v*zIz", "Fn")
diff --git a/clang/include/clang/Basic/TokenKinds.def 
b/clang/include/clang/Basic/TokenKinds.def
index 94db56a9fd5d78c..bbae1200d376c0d 100644
--- a/clang/include/clang/Basic/TokenKinds.def
+++ b/clang/include/clang/Basic/TokenKinds.def
@@ -746,6 +746,7 @@ ALIAS("_pascal"  , __pascal   , KEYBORLAND)
 
 // Clang Extensions.
 KEYWORD(__builtin_convertvector  , KEYALL)
+UNARY_EXPR_OR_TYPE_TRAIT(__builtin_vectorelements, VectorElements, KEYALL)
 ALIAS("__char16_t"   , char16_t  , KEYCXX)
 ALIAS("__char32_t"   , char32_t  , KEYCXX)
 KEYWORD(__builtin_bit_cast   , KEYALL)
diff --git a/clang/lib/AST/ExprConstant.cpp b/clang/lib/AST/ExprConstant.cpp
index e5539dedec02a4b..eb36a57e462f3f1 100644
--- a/clang/lib/AST/ExprConstant.cpp
+++ b/clang/lib/AST/ExprConstant.cpp
@@ -13595,6 +13595,14 @@ bool IntExprEvaluator::VisitUnaryExprOrTypeTraitExpr(
 Info.Ctx.getOpenMPDefaultSimdAlign(E->getArgumentType()))
 .getQuantity(),
 E);
+  case UETT_VectorElements: {
+QualType Ty = E->getTypeOfArgument();
+// If the vector has a fixed size, we can determine the number of elements 
at compile time.
+if (Ty->isVectorType())
+  return Success(Ty->castAs()->getNumElements(), E);
+
+return false;
+  }
   }
 
   llvm_unreachable("unknown expr/type trait");
diff --git a/clang/lib/AST/ItaniumMangle.cpp b/clang/lib/AST/ItaniumMangle.cpp
index 23ec35cae4b7b40..171dfe429c12d31 100644
--- a/clang/lib/AST/ItaniumMangle.cpp
+++ b/clang/lib/AST/ItaniumMangle.cpp
@@ -5126,6 +5126,14 @@ void CXXNameMangler::mangleExpression(const Expr *E, 
unsigned Arity,
   Diags.Report(DiagID);
   return;
 }
+case UETT_VectorElements: {
+  DiagnosticsEngine &Diags = Context.getDiags();
+  unsigned DiagID = Diags.getCustomDiagID(
+  DiagnosticsEngine::Error,
+  "cannot yet mangle __builtin_vectorelements expression");
+  Diags.Report(DiagID);
+  return;
+}
 }
 break;
   }
diff --git a/clang/lib/AST/Type.cpp b/clang/lib/AST/Type.cpp
index 4c433f7fe9daca0..050761784498a9c 100644
--- a/clang/lib/AST/Type.cpp
+++ b/clang/lib/AST/Type.cpp
@@ -2369,7 +2369,7 @@ bool Type::isIncompleteType(NamedDecl **Def) const {
 }
 
 bool Type::isSizelessBuiltinType() const {
-  if (isSVESizelessBuiltinType() || isRVVSizelessBuiltinType())
+  if (isSizelessVectorType())
 return true;
 
   if (const BuiltinType *BT = getAs()) {
@@ -2403,6 +2403,10 @@ bool Type::isWebAssemblyTableType() const {
 
 bool Type::isSizelessType() const { return isSizelessBuiltinType(); }
 
+bool Type::isSizelessVectorType() const {
+  return isSVESizelessBuiltinType() || isRVVSizelessBuiltinType();
+}
+
 bool Type::isSVESizelessBuiltinType() const {
   if (const BuiltinType *BT = getAs()) {
 switch (BT->getKind()) {
di

[clang-tools-extra] [Clang] Add __builtin_vectorelements to get number of elements in vector (PR #69010)

2023-10-16 Thread Lawrence Benson via cfe-commits

https://github.com/lawben updated 
https://github.com/llvm/llvm-project/pull/69010

>From df8d0a53a31e1351bb6cd3b340e9012b489e9885 Mon Sep 17 00:00:00 2001
From: Lawrence Benson 
Date: Wed, 11 Oct 2023 17:26:11 +0200
Subject: [PATCH 1/6] Add __builtin_vectorelements to get the number of
 elements in a fixed-sized vector at compile-time or via a @llvm.vscale call
 at runtime.

---
 clang/include/clang/AST/Type.h   |  3 +++
 clang/include/clang/Basic/Builtins.def   |  1 +
 clang/include/clang/Basic/TokenKinds.def |  1 +
 clang/lib/AST/ExprConstant.cpp   |  8 
 clang/lib/AST/ItaniumMangle.cpp  |  8 
 clang/lib/AST/Type.cpp   |  6 +-
 clang/lib/CodeGen/CGExprScalar.cpp   | 12 
 clang/lib/Parse/ParseExpr.cpp|  7 +--
 clang/lib/Sema/SemaChecking.cpp  | 18 ++
 clang/lib/Sema/SemaExpr.cpp  | 14 ++
 10 files changed, 75 insertions(+), 3 deletions(-)

diff --git a/clang/include/clang/AST/Type.h b/clang/include/clang/AST/Type.h
index a78d8f60462b231..f6e425783176ba2 100644
--- a/clang/include/clang/AST/Type.h
+++ b/clang/include/clang/AST/Type.h
@@ -2058,6 +2058,9 @@ class alignas(8) Type : public ExtQualsTypeCommonBase {
   bool isSizelessType() const;
   bool isSizelessBuiltinType() const;
 
+  /// Returns true for all scalable vector types.
+  bool isSizelessVectorType() const;
+
   /// Returns true for SVE scalable vector types.
   bool isSVESizelessBuiltinType() const;
 
diff --git a/clang/include/clang/Basic/Builtins.def 
b/clang/include/clang/Basic/Builtins.def
index 6ea8484606cfd5d..6033e8a955fb8bd 100644
--- a/clang/include/clang/Basic/Builtins.def
+++ b/clang/include/clang/Basic/Builtins.def
@@ -674,6 +674,7 @@ BUILTIN(__builtin_debugtrap, "v", "n")
 BUILTIN(__builtin_unreachable, "v", "nr")
 BUILTIN(__builtin_shufflevector, "v."   , "nct")
 BUILTIN(__builtin_convertvector, "v."   , "nct")
+BUILTIN(__builtin_vectorelements, "v."  , "nct")
 BUILTIN(__builtin_alloca, "v*z"   , "Fn")
 BUILTIN(__builtin_alloca_uninitialized, "v*z", "Fn")
 BUILTIN(__builtin_alloca_with_align, "v*zIz", "Fn")
diff --git a/clang/include/clang/Basic/TokenKinds.def 
b/clang/include/clang/Basic/TokenKinds.def
index 94db56a9fd5d78c..bbae1200d376c0d 100644
--- a/clang/include/clang/Basic/TokenKinds.def
+++ b/clang/include/clang/Basic/TokenKinds.def
@@ -746,6 +746,7 @@ ALIAS("_pascal"  , __pascal   , KEYBORLAND)
 
 // Clang Extensions.
 KEYWORD(__builtin_convertvector  , KEYALL)
+UNARY_EXPR_OR_TYPE_TRAIT(__builtin_vectorelements, VectorElements, KEYALL)
 ALIAS("__char16_t"   , char16_t  , KEYCXX)
 ALIAS("__char32_t"   , char32_t  , KEYCXX)
 KEYWORD(__builtin_bit_cast   , KEYALL)
diff --git a/clang/lib/AST/ExprConstant.cpp b/clang/lib/AST/ExprConstant.cpp
index e5539dedec02a4b..eb36a57e462f3f1 100644
--- a/clang/lib/AST/ExprConstant.cpp
+++ b/clang/lib/AST/ExprConstant.cpp
@@ -13595,6 +13595,14 @@ bool IntExprEvaluator::VisitUnaryExprOrTypeTraitExpr(
 Info.Ctx.getOpenMPDefaultSimdAlign(E->getArgumentType()))
 .getQuantity(),
 E);
+  case UETT_VectorElements: {
+QualType Ty = E->getTypeOfArgument();
+// If the vector has a fixed size, we can determine the number of elements 
at compile time.
+if (Ty->isVectorType())
+  return Success(Ty->castAs()->getNumElements(), E);
+
+return false;
+  }
   }
 
   llvm_unreachable("unknown expr/type trait");
diff --git a/clang/lib/AST/ItaniumMangle.cpp b/clang/lib/AST/ItaniumMangle.cpp
index 23ec35cae4b7b40..171dfe429c12d31 100644
--- a/clang/lib/AST/ItaniumMangle.cpp
+++ b/clang/lib/AST/ItaniumMangle.cpp
@@ -5126,6 +5126,14 @@ void CXXNameMangler::mangleExpression(const Expr *E, 
unsigned Arity,
   Diags.Report(DiagID);
   return;
 }
+case UETT_VectorElements: {
+  DiagnosticsEngine &Diags = Context.getDiags();
+  unsigned DiagID = Diags.getCustomDiagID(
+  DiagnosticsEngine::Error,
+  "cannot yet mangle __builtin_vectorelements expression");
+  Diags.Report(DiagID);
+  return;
+}
 }
 break;
   }
diff --git a/clang/lib/AST/Type.cpp b/clang/lib/AST/Type.cpp
index 4c433f7fe9daca0..050761784498a9c 100644
--- a/clang/lib/AST/Type.cpp
+++ b/clang/lib/AST/Type.cpp
@@ -2369,7 +2369,7 @@ bool Type::isIncompleteType(NamedDecl **Def) const {
 }
 
 bool Type::isSizelessBuiltinType() const {
-  if (isSVESizelessBuiltinType() || isRVVSizelessBuiltinType())
+  if (isSizelessVectorType())
 return true;
 
   if (const BuiltinType *BT = getAs()) {
@@ -2403,6 +2403,10 @@ bool Type::isWebAssemblyTableType() const {
 
 bool Type::isSizelessType() const { return isSizelessBuiltinType(); }
 
+bool Type::isSizelessVectorType() const {
+  return isSVESizelessBuiltinType() || isRVVSizelessBuiltinType();
+}
+
 bool Type::isSVESizelessBuiltinType() const {
   if (const BuiltinType *BT = getAs()) {
 switch (BT->getKind()) {
di

[libunwind] [Clang] Add __builtin_vectorelements to get number of elements in vector (PR #69010)

2023-10-16 Thread Lawrence Benson via cfe-commits


@@ -13595,6 +13595,15 @@ bool IntExprEvaluator::VisitUnaryExprOrTypeTraitExpr(
 Info.Ctx.getOpenMPDefaultSimdAlign(E->getArgumentType()))
 .getQuantity(),
 E);
+  case UETT_VectorElements: {
+QualType Ty = E->getTypeOfArgument();
+// If the vector has a fixed size, we can determine the number of elements
+// at compile time.
+if (Ty->isVectorType())
+  return Success(Ty->castAs()->getNumElements(), E);
+
+return false;

lawben wrote:

maybe my understanding here is wrong, so please correct me in that case. If the 
vector is fixed-sized, we can determine the number of elements at compile time, 
which is why we return the number here in `ExprConstant.cpp`. if it is a 
scalable vector, we `return false`, as we cannot determine a constant value. in 
that case, we move along and hit `CGExprScalar.cpp`, which is where we emit the 
runtime `vscale` code for anything that is not constant. 

So in my understanding, we either emit a constant in `ExprConstant` or the 
runtime logic in `CGExprScalar`. I can add assertions to check that we are 
always in a good case, but i've not come across a fixed-sized vector path that 
does not get caught in `ExprConstant`. if there are alternative code paths, 
then you are correct and I should add checks. but I don't want to duplicate 
logic if this case can never happen anyway.

https://github.com/llvm/llvm-project/pull/69010
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [Clang] Add __builtin_vectorelements to get number of elements in vector (PR #69010)

2023-10-16 Thread Lawrence Benson via cfe-commits


@@ -10136,6 +10136,9 @@ def err_vec_builtin_incompatible_vector : Error<
 def err_vsx_builtin_nonconstant_argument : Error<
   "argument %0 to %1 must be a 2-bit unsigned literal (i.e. 0, 1, 2 or 3)">;
 
+def err_vectorelements_non_vector : Error<
+ "'__builtin_vectorelements' argument must be a vector">;

lawben wrote:

Makes sense. I've combined the two.

https://github.com/llvm/llvm-project/pull/69010
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [Clang] Add __builtin_vectorelements to get number of elements in vector (PR #69010)

2023-10-16 Thread Lawrence Benson via cfe-commits


@@ -3083,6 +3083,19 @@ ScalarExprEmitter::VisitUnaryExprOrTypeTraitExpr(
 E->getTypeOfArgument()->getPointeeType()))
 .getQuantity();
 return llvm::ConstantInt::get(CGF.SizeTy, Alignment);
+  } else if (E->getKind() == UETT_VectorElements) {
+// For scalable vectors, we don't know the size at compile time. We can use
+// @llvm.vscale to calculate it at runtime.
+if (E->getTypeOfArgument()->isSizelessVectorType()) {
+  auto *VecTy = dyn_cast(
+  ConvertType(E->getTypeOfArgument()));
+  uint64_t NumUnscaledElements = VecTy->getMinNumElements();

lawben wrote:

the argument here is the vector type that was passed to 
`__builtin_vectorelements()`. the call to `@llvm.vscale` is independent of the 
type, as it is a system-wide thing, so it does not need the type. 

scalable vectors are represented as ``. so if we get `vscale`, we still need to figure out the `NUM_ELEMENTS` 
part so we can get the actual number of elements on this system by multiplying 
it with. as that depends on the vector type passed to the builtin, as the 
number of elements depends on the element's type. for SVE, LLVM assumes a 
minimum 16-byte vector, so a ` has `4 * vscale` but a 
`` has `8 * vscale`. 

I hope this clarifies the code. if so, I'll probably add a comment to explain.

https://github.com/llvm/llvm-project/pull/69010
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[libunwind] [Clang] Add __builtin_vectorelements to get number of elements in vector (PR #69010)

2023-10-16 Thread Lawrence Benson via cfe-commits


@@ -10136,6 +10136,9 @@ def err_vec_builtin_incompatible_vector : Error<
 def err_vsx_builtin_nonconstant_argument : Error<
   "argument %0 to %1 must be a 2-bit unsigned literal (i.e. 0, 1, 2 or 3)">;
 
+def err_vectorelements_non_vector : Error<
+ "'__builtin_vectorelements' argument must be a vector">;

lawben wrote:

Makes sense. I've combined the two.

https://github.com/llvm/llvm-project/pull/69010
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[libunwind] [Clang] Add __builtin_vectorelements to get number of elements in vector (PR #69010)

2023-10-16 Thread Lawrence Benson via cfe-commits


@@ -3083,6 +3083,19 @@ ScalarExprEmitter::VisitUnaryExprOrTypeTraitExpr(
 E->getTypeOfArgument()->getPointeeType()))
 .getQuantity();
 return llvm::ConstantInt::get(CGF.SizeTy, Alignment);
+  } else if (E->getKind() == UETT_VectorElements) {
+// For scalable vectors, we don't know the size at compile time. We can use
+// @llvm.vscale to calculate it at runtime.
+if (E->getTypeOfArgument()->isSizelessVectorType()) {
+  auto *VecTy = dyn_cast(
+  ConvertType(E->getTypeOfArgument()));
+  uint64_t NumUnscaledElements = VecTy->getMinNumElements();

lawben wrote:

the argument here is the vector type that was passed to 
`__builtin_vectorelements()`. the call to `@llvm.vscale` is independent of the 
type, as it is a system-wide thing, so it does not need the type. 

scalable vectors are represented as ``. so if we get `vscale`, we still need to figure out the `NUM_ELEMENTS` 
part so we can get the actual number of elements on this system by multiplying 
it with. as that depends on the vector type passed to the builtin, as the 
number of elements depends on the element's type. for SVE, LLVM assumes a 
minimum 16-byte vector, so a ` has `4 * vscale` but a 
`` has `8 * vscale`. 

I hope this clarifies the code. if so, I'll probably add a comment to explain.

https://github.com/llvm/llvm-project/pull/69010
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [Clang] Add __builtin_vectorelements to get number of elements in vector (PR #69010)

2023-10-16 Thread Lawrence Benson via cfe-commits


@@ -0,0 +1,121 @@
+// RUN: %clang_cc1 -O1 -triple aarch64 -target-feature +neon %s -emit-llvm -o 
- | FileCheck --check-prefixes=CHECK,NEON %s

lawben wrote:

Done.

https://github.com/llvm/llvm-project/pull/69010
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[libunwind] [Clang] Add __builtin_vectorelements to get number of elements in vector (PR #69010)

2023-10-16 Thread Lawrence Benson via cfe-commits


@@ -0,0 +1,121 @@
+// RUN: %clang_cc1 -O1 -triple aarch64 -target-feature +neon %s -emit-llvm -o 
- | FileCheck --check-prefixes=CHECK,NEON %s

lawben wrote:

Done.

https://github.com/llvm/llvm-project/pull/69010
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang-tools-extra] [Clang] Add __builtin_vectorelements to get number of elements in vector (PR #69010)

2023-10-16 Thread Lawrence Benson via cfe-commits


@@ -10136,6 +10136,9 @@ def err_vec_builtin_incompatible_vector : Error<
 def err_vsx_builtin_nonconstant_argument : Error<
   "argument %0 to %1 must be a 2-bit unsigned literal (i.e. 0, 1, 2 or 3)">;
 
+def err_vectorelements_non_vector : Error<
+ "'__builtin_vectorelements' argument must be a vector">;

lawben wrote:

Makes sense. I've combined the two.

https://github.com/llvm/llvm-project/pull/69010
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang-tools-extra] [Clang] Add __builtin_vectorelements to get number of elements in vector (PR #69010)

2023-10-16 Thread Lawrence Benson via cfe-commits


@@ -0,0 +1,121 @@
+// RUN: %clang_cc1 -O1 -triple aarch64 -target-feature +neon %s -emit-llvm -o 
- | FileCheck --check-prefixes=CHECK,NEON %s

lawben wrote:

Done.

https://github.com/llvm/llvm-project/pull/69010
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[libunwind] [Clang] Add __builtin_vectorelements to get number of elements in vector (PR #69010)

2023-10-16 Thread Lawrence Benson via cfe-commits

https://github.com/lawben updated 
https://github.com/llvm/llvm-project/pull/69010

>From df8d0a53a31e1351bb6cd3b340e9012b489e9885 Mon Sep 17 00:00:00 2001
From: Lawrence Benson 
Date: Wed, 11 Oct 2023 17:26:11 +0200
Subject: [PATCH 1/7] Add __builtin_vectorelements to get the number of
 elements in a fixed-sized vector at compile-time or via a @llvm.vscale call
 at runtime.

---
 clang/include/clang/AST/Type.h   |  3 +++
 clang/include/clang/Basic/Builtins.def   |  1 +
 clang/include/clang/Basic/TokenKinds.def |  1 +
 clang/lib/AST/ExprConstant.cpp   |  8 
 clang/lib/AST/ItaniumMangle.cpp  |  8 
 clang/lib/AST/Type.cpp   |  6 +-
 clang/lib/CodeGen/CGExprScalar.cpp   | 12 
 clang/lib/Parse/ParseExpr.cpp|  7 +--
 clang/lib/Sema/SemaChecking.cpp  | 18 ++
 clang/lib/Sema/SemaExpr.cpp  | 14 ++
 10 files changed, 75 insertions(+), 3 deletions(-)

diff --git a/clang/include/clang/AST/Type.h b/clang/include/clang/AST/Type.h
index a78d8f60462b231..f6e425783176ba2 100644
--- a/clang/include/clang/AST/Type.h
+++ b/clang/include/clang/AST/Type.h
@@ -2058,6 +2058,9 @@ class alignas(8) Type : public ExtQualsTypeCommonBase {
   bool isSizelessType() const;
   bool isSizelessBuiltinType() const;
 
+  /// Returns true for all scalable vector types.
+  bool isSizelessVectorType() const;
+
   /// Returns true for SVE scalable vector types.
   bool isSVESizelessBuiltinType() const;
 
diff --git a/clang/include/clang/Basic/Builtins.def 
b/clang/include/clang/Basic/Builtins.def
index 6ea8484606cfd5d..6033e8a955fb8bd 100644
--- a/clang/include/clang/Basic/Builtins.def
+++ b/clang/include/clang/Basic/Builtins.def
@@ -674,6 +674,7 @@ BUILTIN(__builtin_debugtrap, "v", "n")
 BUILTIN(__builtin_unreachable, "v", "nr")
 BUILTIN(__builtin_shufflevector, "v."   , "nct")
 BUILTIN(__builtin_convertvector, "v."   , "nct")
+BUILTIN(__builtin_vectorelements, "v."  , "nct")
 BUILTIN(__builtin_alloca, "v*z"   , "Fn")
 BUILTIN(__builtin_alloca_uninitialized, "v*z", "Fn")
 BUILTIN(__builtin_alloca_with_align, "v*zIz", "Fn")
diff --git a/clang/include/clang/Basic/TokenKinds.def 
b/clang/include/clang/Basic/TokenKinds.def
index 94db56a9fd5d78c..bbae1200d376c0d 100644
--- a/clang/include/clang/Basic/TokenKinds.def
+++ b/clang/include/clang/Basic/TokenKinds.def
@@ -746,6 +746,7 @@ ALIAS("_pascal"  , __pascal   , KEYBORLAND)
 
 // Clang Extensions.
 KEYWORD(__builtin_convertvector  , KEYALL)
+UNARY_EXPR_OR_TYPE_TRAIT(__builtin_vectorelements, VectorElements, KEYALL)
 ALIAS("__char16_t"   , char16_t  , KEYCXX)
 ALIAS("__char32_t"   , char32_t  , KEYCXX)
 KEYWORD(__builtin_bit_cast   , KEYALL)
diff --git a/clang/lib/AST/ExprConstant.cpp b/clang/lib/AST/ExprConstant.cpp
index e5539dedec02a4b..eb36a57e462f3f1 100644
--- a/clang/lib/AST/ExprConstant.cpp
+++ b/clang/lib/AST/ExprConstant.cpp
@@ -13595,6 +13595,14 @@ bool IntExprEvaluator::VisitUnaryExprOrTypeTraitExpr(
 Info.Ctx.getOpenMPDefaultSimdAlign(E->getArgumentType()))
 .getQuantity(),
 E);
+  case UETT_VectorElements: {
+QualType Ty = E->getTypeOfArgument();
+// If the vector has a fixed size, we can determine the number of elements 
at compile time.
+if (Ty->isVectorType())
+  return Success(Ty->castAs()->getNumElements(), E);
+
+return false;
+  }
   }
 
   llvm_unreachable("unknown expr/type trait");
diff --git a/clang/lib/AST/ItaniumMangle.cpp b/clang/lib/AST/ItaniumMangle.cpp
index 23ec35cae4b7b40..171dfe429c12d31 100644
--- a/clang/lib/AST/ItaniumMangle.cpp
+++ b/clang/lib/AST/ItaniumMangle.cpp
@@ -5126,6 +5126,14 @@ void CXXNameMangler::mangleExpression(const Expr *E, 
unsigned Arity,
   Diags.Report(DiagID);
   return;
 }
+case UETT_VectorElements: {
+  DiagnosticsEngine &Diags = Context.getDiags();
+  unsigned DiagID = Diags.getCustomDiagID(
+  DiagnosticsEngine::Error,
+  "cannot yet mangle __builtin_vectorelements expression");
+  Diags.Report(DiagID);
+  return;
+}
 }
 break;
   }
diff --git a/clang/lib/AST/Type.cpp b/clang/lib/AST/Type.cpp
index 4c433f7fe9daca0..050761784498a9c 100644
--- a/clang/lib/AST/Type.cpp
+++ b/clang/lib/AST/Type.cpp
@@ -2369,7 +2369,7 @@ bool Type::isIncompleteType(NamedDecl **Def) const {
 }
 
 bool Type::isSizelessBuiltinType() const {
-  if (isSVESizelessBuiltinType() || isRVVSizelessBuiltinType())
+  if (isSizelessVectorType())
 return true;
 
   if (const BuiltinType *BT = getAs()) {
@@ -2403,6 +2403,10 @@ bool Type::isWebAssemblyTableType() const {
 
 bool Type::isSizelessType() const { return isSizelessBuiltinType(); }
 
+bool Type::isSizelessVectorType() const {
+  return isSVESizelessBuiltinType() || isRVVSizelessBuiltinType();
+}
+
 bool Type::isSVESizelessBuiltinType() const {
   if (const BuiltinType *BT = getAs()) {
 switch (BT->getKind()) {
di

[clang] [Clang] Add __builtin_vectorelements to get number of elements in vector (PR #69010)

2023-10-16 Thread Lawrence Benson via cfe-commits

https://github.com/lawben updated 
https://github.com/llvm/llvm-project/pull/69010

>From df8d0a53a31e1351bb6cd3b340e9012b489e9885 Mon Sep 17 00:00:00 2001
From: Lawrence Benson 
Date: Wed, 11 Oct 2023 17:26:11 +0200
Subject: [PATCH 1/7] Add __builtin_vectorelements to get the number of
 elements in a fixed-sized vector at compile-time or via a @llvm.vscale call
 at runtime.

---
 clang/include/clang/AST/Type.h   |  3 +++
 clang/include/clang/Basic/Builtins.def   |  1 +
 clang/include/clang/Basic/TokenKinds.def |  1 +
 clang/lib/AST/ExprConstant.cpp   |  8 
 clang/lib/AST/ItaniumMangle.cpp  |  8 
 clang/lib/AST/Type.cpp   |  6 +-
 clang/lib/CodeGen/CGExprScalar.cpp   | 12 
 clang/lib/Parse/ParseExpr.cpp|  7 +--
 clang/lib/Sema/SemaChecking.cpp  | 18 ++
 clang/lib/Sema/SemaExpr.cpp  | 14 ++
 10 files changed, 75 insertions(+), 3 deletions(-)

diff --git a/clang/include/clang/AST/Type.h b/clang/include/clang/AST/Type.h
index a78d8f60462b231..f6e425783176ba2 100644
--- a/clang/include/clang/AST/Type.h
+++ b/clang/include/clang/AST/Type.h
@@ -2058,6 +2058,9 @@ class alignas(8) Type : public ExtQualsTypeCommonBase {
   bool isSizelessType() const;
   bool isSizelessBuiltinType() const;
 
+  /// Returns true for all scalable vector types.
+  bool isSizelessVectorType() const;
+
   /// Returns true for SVE scalable vector types.
   bool isSVESizelessBuiltinType() const;
 
diff --git a/clang/include/clang/Basic/Builtins.def 
b/clang/include/clang/Basic/Builtins.def
index 6ea8484606cfd5d..6033e8a955fb8bd 100644
--- a/clang/include/clang/Basic/Builtins.def
+++ b/clang/include/clang/Basic/Builtins.def
@@ -674,6 +674,7 @@ BUILTIN(__builtin_debugtrap, "v", "n")
 BUILTIN(__builtin_unreachable, "v", "nr")
 BUILTIN(__builtin_shufflevector, "v."   , "nct")
 BUILTIN(__builtin_convertvector, "v."   , "nct")
+BUILTIN(__builtin_vectorelements, "v."  , "nct")
 BUILTIN(__builtin_alloca, "v*z"   , "Fn")
 BUILTIN(__builtin_alloca_uninitialized, "v*z", "Fn")
 BUILTIN(__builtin_alloca_with_align, "v*zIz", "Fn")
diff --git a/clang/include/clang/Basic/TokenKinds.def 
b/clang/include/clang/Basic/TokenKinds.def
index 94db56a9fd5d78c..bbae1200d376c0d 100644
--- a/clang/include/clang/Basic/TokenKinds.def
+++ b/clang/include/clang/Basic/TokenKinds.def
@@ -746,6 +746,7 @@ ALIAS("_pascal"  , __pascal   , KEYBORLAND)
 
 // Clang Extensions.
 KEYWORD(__builtin_convertvector  , KEYALL)
+UNARY_EXPR_OR_TYPE_TRAIT(__builtin_vectorelements, VectorElements, KEYALL)
 ALIAS("__char16_t"   , char16_t  , KEYCXX)
 ALIAS("__char32_t"   , char32_t  , KEYCXX)
 KEYWORD(__builtin_bit_cast   , KEYALL)
diff --git a/clang/lib/AST/ExprConstant.cpp b/clang/lib/AST/ExprConstant.cpp
index e5539dedec02a4b..eb36a57e462f3f1 100644
--- a/clang/lib/AST/ExprConstant.cpp
+++ b/clang/lib/AST/ExprConstant.cpp
@@ -13595,6 +13595,14 @@ bool IntExprEvaluator::VisitUnaryExprOrTypeTraitExpr(
 Info.Ctx.getOpenMPDefaultSimdAlign(E->getArgumentType()))
 .getQuantity(),
 E);
+  case UETT_VectorElements: {
+QualType Ty = E->getTypeOfArgument();
+// If the vector has a fixed size, we can determine the number of elements 
at compile time.
+if (Ty->isVectorType())
+  return Success(Ty->castAs()->getNumElements(), E);
+
+return false;
+  }
   }
 
   llvm_unreachable("unknown expr/type trait");
diff --git a/clang/lib/AST/ItaniumMangle.cpp b/clang/lib/AST/ItaniumMangle.cpp
index 23ec35cae4b7b40..171dfe429c12d31 100644
--- a/clang/lib/AST/ItaniumMangle.cpp
+++ b/clang/lib/AST/ItaniumMangle.cpp
@@ -5126,6 +5126,14 @@ void CXXNameMangler::mangleExpression(const Expr *E, 
unsigned Arity,
   Diags.Report(DiagID);
   return;
 }
+case UETT_VectorElements: {
+  DiagnosticsEngine &Diags = Context.getDiags();
+  unsigned DiagID = Diags.getCustomDiagID(
+  DiagnosticsEngine::Error,
+  "cannot yet mangle __builtin_vectorelements expression");
+  Diags.Report(DiagID);
+  return;
+}
 }
 break;
   }
diff --git a/clang/lib/AST/Type.cpp b/clang/lib/AST/Type.cpp
index 4c433f7fe9daca0..050761784498a9c 100644
--- a/clang/lib/AST/Type.cpp
+++ b/clang/lib/AST/Type.cpp
@@ -2369,7 +2369,7 @@ bool Type::isIncompleteType(NamedDecl **Def) const {
 }
 
 bool Type::isSizelessBuiltinType() const {
-  if (isSVESizelessBuiltinType() || isRVVSizelessBuiltinType())
+  if (isSizelessVectorType())
 return true;
 
   if (const BuiltinType *BT = getAs()) {
@@ -2403,6 +2403,10 @@ bool Type::isWebAssemblyTableType() const {
 
 bool Type::isSizelessType() const { return isSizelessBuiltinType(); }
 
+bool Type::isSizelessVectorType() const {
+  return isSVESizelessBuiltinType() || isRVVSizelessBuiltinType();
+}
+
 bool Type::isSVESizelessBuiltinType() const {
   if (const BuiltinType *BT = getAs()) {
 switch (BT->getKind()) {
di

[libunwind] [Clang] Add __builtin_vectorelements to get number of elements in vector (PR #69010)

2023-10-16 Thread Lawrence Benson via cfe-commits


@@ -3083,6 +3083,19 @@ ScalarExprEmitter::VisitUnaryExprOrTypeTraitExpr(
 E->getTypeOfArgument()->getPointeeType()))
 .getQuantity();
 return llvm::ConstantInt::get(CGF.SizeTy, Alignment);
+  } else if (E->getKind() == UETT_VectorElements) {
+// For scalable vectors, we don't know the size at compile time. We can use
+// @llvm.vscale to calculate it at runtime.
+if (E->getTypeOfArgument()->isSizelessVectorType()) {
+  auto *VecTy = dyn_cast(
+  ConvertType(E->getTypeOfArgument()));
+  uint64_t NumUnscaledElements = VecTy->getMinNumElements();
+
+  llvm::Value *VScale =
+  Builder.CreateVScale(llvm::ConstantInt::get(CGF.SizeTy, 1));
+  return Builder.CreateMul(
+  VScale, llvm::ConstantInt::get(CGF.SizeTy, NumUnscaledElements));

lawben wrote:

Neat, thanks for the pointer. that is a lot nicer. @erichkeane this should fix 
the comments above, as we now handle both types of vectors here and the code is 
more clear.

https://github.com/llvm/llvm-project/pull/69010
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [Clang] Add __builtin_vectorelements to get number of elements in vector (PR #69010)

2023-10-16 Thread Lawrence Benson via cfe-commits


@@ -3083,6 +3083,19 @@ ScalarExprEmitter::VisitUnaryExprOrTypeTraitExpr(
 E->getTypeOfArgument()->getPointeeType()))
 .getQuantity();
 return llvm::ConstantInt::get(CGF.SizeTy, Alignment);
+  } else if (E->getKind() == UETT_VectorElements) {
+// For scalable vectors, we don't know the size at compile time. We can use
+// @llvm.vscale to calculate it at runtime.
+if (E->getTypeOfArgument()->isSizelessVectorType()) {
+  auto *VecTy = dyn_cast(
+  ConvertType(E->getTypeOfArgument()));
+  uint64_t NumUnscaledElements = VecTy->getMinNumElements();
+
+  llvm::Value *VScale =
+  Builder.CreateVScale(llvm::ConstantInt::get(CGF.SizeTy, 1));
+  return Builder.CreateMul(
+  VScale, llvm::ConstantInt::get(CGF.SizeTy, NumUnscaledElements));

lawben wrote:

Neat, thanks for the pointer. that is a lot nicer. @erichkeane this should fix 
the comments above, as we now handle both types of vectors here and the code is 
more clear.

https://github.com/llvm/llvm-project/pull/69010
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[libunwind] [Clang] Add __builtin_vectorelements to get number of elements in vector (PR #69010)

2023-10-16 Thread Lawrence Benson via cfe-commits

lawben wrote:

> Also, needs a release note.

How do I do this? I cannot seem to find documentation on the process...

https://github.com/llvm/llvm-project/pull/69010
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [Clang] Add __builtin_vectorelements to get number of elements in vector (PR #69010)

2023-10-16 Thread Lawrence Benson via cfe-commits

lawben wrote:

> Also, needs a release note.

How do I do this? I cannot seem to find documentation on the process...

https://github.com/llvm/llvm-project/pull/69010
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [Clang] Add __builtin_vectorelements to get number of elements in vector (PR #69010)

2023-10-17 Thread Lawrence Benson via cfe-commits

https://github.com/lawben updated 
https://github.com/llvm/llvm-project/pull/69010

>From df8d0a53a31e1351bb6cd3b340e9012b489e9885 Mon Sep 17 00:00:00 2001
From: Lawrence Benson 
Date: Wed, 11 Oct 2023 17:26:11 +0200
Subject: [PATCH 1/9] Add __builtin_vectorelements to get the number of
 elements in a fixed-sized vector at compile-time or via a @llvm.vscale call
 at runtime.

---
 clang/include/clang/AST/Type.h   |  3 +++
 clang/include/clang/Basic/Builtins.def   |  1 +
 clang/include/clang/Basic/TokenKinds.def |  1 +
 clang/lib/AST/ExprConstant.cpp   |  8 
 clang/lib/AST/ItaniumMangle.cpp  |  8 
 clang/lib/AST/Type.cpp   |  6 +-
 clang/lib/CodeGen/CGExprScalar.cpp   | 12 
 clang/lib/Parse/ParseExpr.cpp|  7 +--
 clang/lib/Sema/SemaChecking.cpp  | 18 ++
 clang/lib/Sema/SemaExpr.cpp  | 14 ++
 10 files changed, 75 insertions(+), 3 deletions(-)

diff --git a/clang/include/clang/AST/Type.h b/clang/include/clang/AST/Type.h
index a78d8f60462b231..f6e425783176ba2 100644
--- a/clang/include/clang/AST/Type.h
+++ b/clang/include/clang/AST/Type.h
@@ -2058,6 +2058,9 @@ class alignas(8) Type : public ExtQualsTypeCommonBase {
   bool isSizelessType() const;
   bool isSizelessBuiltinType() const;
 
+  /// Returns true for all scalable vector types.
+  bool isSizelessVectorType() const;
+
   /// Returns true for SVE scalable vector types.
   bool isSVESizelessBuiltinType() const;
 
diff --git a/clang/include/clang/Basic/Builtins.def 
b/clang/include/clang/Basic/Builtins.def
index 6ea8484606cfd5d..6033e8a955fb8bd 100644
--- a/clang/include/clang/Basic/Builtins.def
+++ b/clang/include/clang/Basic/Builtins.def
@@ -674,6 +674,7 @@ BUILTIN(__builtin_debugtrap, "v", "n")
 BUILTIN(__builtin_unreachable, "v", "nr")
 BUILTIN(__builtin_shufflevector, "v."   , "nct")
 BUILTIN(__builtin_convertvector, "v."   , "nct")
+BUILTIN(__builtin_vectorelements, "v."  , "nct")
 BUILTIN(__builtin_alloca, "v*z"   , "Fn")
 BUILTIN(__builtin_alloca_uninitialized, "v*z", "Fn")
 BUILTIN(__builtin_alloca_with_align, "v*zIz", "Fn")
diff --git a/clang/include/clang/Basic/TokenKinds.def 
b/clang/include/clang/Basic/TokenKinds.def
index 94db56a9fd5d78c..bbae1200d376c0d 100644
--- a/clang/include/clang/Basic/TokenKinds.def
+++ b/clang/include/clang/Basic/TokenKinds.def
@@ -746,6 +746,7 @@ ALIAS("_pascal"  , __pascal   , KEYBORLAND)
 
 // Clang Extensions.
 KEYWORD(__builtin_convertvector  , KEYALL)
+UNARY_EXPR_OR_TYPE_TRAIT(__builtin_vectorelements, VectorElements, KEYALL)
 ALIAS("__char16_t"   , char16_t  , KEYCXX)
 ALIAS("__char32_t"   , char32_t  , KEYCXX)
 KEYWORD(__builtin_bit_cast   , KEYALL)
diff --git a/clang/lib/AST/ExprConstant.cpp b/clang/lib/AST/ExprConstant.cpp
index e5539dedec02a4b..eb36a57e462f3f1 100644
--- a/clang/lib/AST/ExprConstant.cpp
+++ b/clang/lib/AST/ExprConstant.cpp
@@ -13595,6 +13595,14 @@ bool IntExprEvaluator::VisitUnaryExprOrTypeTraitExpr(
 Info.Ctx.getOpenMPDefaultSimdAlign(E->getArgumentType()))
 .getQuantity(),
 E);
+  case UETT_VectorElements: {
+QualType Ty = E->getTypeOfArgument();
+// If the vector has a fixed size, we can determine the number of elements 
at compile time.
+if (Ty->isVectorType())
+  return Success(Ty->castAs()->getNumElements(), E);
+
+return false;
+  }
   }
 
   llvm_unreachable("unknown expr/type trait");
diff --git a/clang/lib/AST/ItaniumMangle.cpp b/clang/lib/AST/ItaniumMangle.cpp
index 23ec35cae4b7b40..171dfe429c12d31 100644
--- a/clang/lib/AST/ItaniumMangle.cpp
+++ b/clang/lib/AST/ItaniumMangle.cpp
@@ -5126,6 +5126,14 @@ void CXXNameMangler::mangleExpression(const Expr *E, 
unsigned Arity,
   Diags.Report(DiagID);
   return;
 }
+case UETT_VectorElements: {
+  DiagnosticsEngine &Diags = Context.getDiags();
+  unsigned DiagID = Diags.getCustomDiagID(
+  DiagnosticsEngine::Error,
+  "cannot yet mangle __builtin_vectorelements expression");
+  Diags.Report(DiagID);
+  return;
+}
 }
 break;
   }
diff --git a/clang/lib/AST/Type.cpp b/clang/lib/AST/Type.cpp
index 4c433f7fe9daca0..050761784498a9c 100644
--- a/clang/lib/AST/Type.cpp
+++ b/clang/lib/AST/Type.cpp
@@ -2369,7 +2369,7 @@ bool Type::isIncompleteType(NamedDecl **Def) const {
 }
 
 bool Type::isSizelessBuiltinType() const {
-  if (isSVESizelessBuiltinType() || isRVVSizelessBuiltinType())
+  if (isSizelessVectorType())
 return true;
 
   if (const BuiltinType *BT = getAs()) {
@@ -2403,6 +2403,10 @@ bool Type::isWebAssemblyTableType() const {
 
 bool Type::isSizelessType() const { return isSizelessBuiltinType(); }
 
+bool Type::isSizelessVectorType() const {
+  return isSVESizelessBuiltinType() || isRVVSizelessBuiltinType();
+}
+
 bool Type::isSVESizelessBuiltinType() const {
   if (const BuiltinType *BT = getAs()) {
 switch (BT->getKind()) {
di

[clang] [Clang] Add __builtin_vectorelements to get number of elements in vector (PR #69010)

2023-10-17 Thread Lawrence Benson via cfe-commits


@@ -0,0 +1,23 @@
+// RUN: %clang_cc1 -triple aarch64 -fsyntax-only -verify %s
+
+void test_builtin_vectorelements() {
+  __builtin_vectorelements(int); // expected-error 
{{'__builtin_vectorelements' argument must be a vector}}
+  __builtin_vectorelements(float); // expected-error 
{{'__builtin_vectorelements' argument must be a vector}}
+  __builtin_vectorelements(long*); // expected-error 
{{'__builtin_vectorelements' argument must be a vector}}
+
+  int a;
+  __builtin_vectorelements(a); // expected-error {{'__builtin_vectorelements' 
argument must be a vector}}
+
+  typedef int veci4 __attribute__((vector_size(16)));
+  (void) __builtin_vectorelements(veci4);
+
+  veci4 vec;
+  (void) __builtin_vectorelements(vec);
+
+  typedef veci4 some_other_vec;
+  (void) __builtin_vectorelements(some_other_vec);
+
+  struct Foo { int a; };
+  __builtin_vectorelements(struct Foo); // expected-error 
{{'__builtin_vectorelements' argument must be a vector}}

lawben wrote:

just FYI: its `CCEDiag`

https://github.com/llvm/llvm-project/pull/69010
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [Clang] Add __builtin_vectorelements to get number of elements in vector (PR #69010)

2023-10-17 Thread Lawrence Benson via cfe-commits

lawben wrote:

@erichkeane I think I've addressed all of your comments so far. Please check if 
there is anything else missing.

https://github.com/llvm/llvm-project/pull/69010
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[libunwind] [Clang] Add __builtin_vectorelements to get number of elements in vector (PR #69010)

2023-10-17 Thread Lawrence Benson via cfe-commits


@@ -5126,6 +5126,14 @@ void CXXNameMangler::mangleExpression(const Expr *E, 
unsigned Arity,
   Diags.Report(DiagID);
   return;
 }
+case UETT_VectorElements: {

lawben wrote:

I don't know. But none of the other `UETT_*` types are covered in 
`MicrosoftMangle.cpp`. I'm not sure whether they are missing or if it not 
necessary to add this.

https://github.com/llvm/llvm-project/pull/69010
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang-tools-extra] [Clang] Add __builtin_vectorelements to get number of elements in vector (PR #69010)

2023-10-17 Thread Lawrence Benson via cfe-commits

https://github.com/lawben updated 
https://github.com/llvm/llvm-project/pull/69010

>From df8d0a53a31e1351bb6cd3b340e9012b489e9885 Mon Sep 17 00:00:00 2001
From: Lawrence Benson 
Date: Wed, 11 Oct 2023 17:26:11 +0200
Subject: [PATCH 01/10] Add __builtin_vectorelements to get the number of
 elements in a fixed-sized vector at compile-time or via a @llvm.vscale call
 at runtime.

---
 clang/include/clang/AST/Type.h   |  3 +++
 clang/include/clang/Basic/Builtins.def   |  1 +
 clang/include/clang/Basic/TokenKinds.def |  1 +
 clang/lib/AST/ExprConstant.cpp   |  8 
 clang/lib/AST/ItaniumMangle.cpp  |  8 
 clang/lib/AST/Type.cpp   |  6 +-
 clang/lib/CodeGen/CGExprScalar.cpp   | 12 
 clang/lib/Parse/ParseExpr.cpp|  7 +--
 clang/lib/Sema/SemaChecking.cpp  | 18 ++
 clang/lib/Sema/SemaExpr.cpp  | 14 ++
 10 files changed, 75 insertions(+), 3 deletions(-)

diff --git a/clang/include/clang/AST/Type.h b/clang/include/clang/AST/Type.h
index a78d8f60462b231..f6e425783176ba2 100644
--- a/clang/include/clang/AST/Type.h
+++ b/clang/include/clang/AST/Type.h
@@ -2058,6 +2058,9 @@ class alignas(8) Type : public ExtQualsTypeCommonBase {
   bool isSizelessType() const;
   bool isSizelessBuiltinType() const;
 
+  /// Returns true for all scalable vector types.
+  bool isSizelessVectorType() const;
+
   /// Returns true for SVE scalable vector types.
   bool isSVESizelessBuiltinType() const;
 
diff --git a/clang/include/clang/Basic/Builtins.def 
b/clang/include/clang/Basic/Builtins.def
index 6ea8484606cfd5d..6033e8a955fb8bd 100644
--- a/clang/include/clang/Basic/Builtins.def
+++ b/clang/include/clang/Basic/Builtins.def
@@ -674,6 +674,7 @@ BUILTIN(__builtin_debugtrap, "v", "n")
 BUILTIN(__builtin_unreachable, "v", "nr")
 BUILTIN(__builtin_shufflevector, "v."   , "nct")
 BUILTIN(__builtin_convertvector, "v."   , "nct")
+BUILTIN(__builtin_vectorelements, "v."  , "nct")
 BUILTIN(__builtin_alloca, "v*z"   , "Fn")
 BUILTIN(__builtin_alloca_uninitialized, "v*z", "Fn")
 BUILTIN(__builtin_alloca_with_align, "v*zIz", "Fn")
diff --git a/clang/include/clang/Basic/TokenKinds.def 
b/clang/include/clang/Basic/TokenKinds.def
index 94db56a9fd5d78c..bbae1200d376c0d 100644
--- a/clang/include/clang/Basic/TokenKinds.def
+++ b/clang/include/clang/Basic/TokenKinds.def
@@ -746,6 +746,7 @@ ALIAS("_pascal"  , __pascal   , KEYBORLAND)
 
 // Clang Extensions.
 KEYWORD(__builtin_convertvector  , KEYALL)
+UNARY_EXPR_OR_TYPE_TRAIT(__builtin_vectorelements, VectorElements, KEYALL)
 ALIAS("__char16_t"   , char16_t  , KEYCXX)
 ALIAS("__char32_t"   , char32_t  , KEYCXX)
 KEYWORD(__builtin_bit_cast   , KEYALL)
diff --git a/clang/lib/AST/ExprConstant.cpp b/clang/lib/AST/ExprConstant.cpp
index e5539dedec02a4b..eb36a57e462f3f1 100644
--- a/clang/lib/AST/ExprConstant.cpp
+++ b/clang/lib/AST/ExprConstant.cpp
@@ -13595,6 +13595,14 @@ bool IntExprEvaluator::VisitUnaryExprOrTypeTraitExpr(
 Info.Ctx.getOpenMPDefaultSimdAlign(E->getArgumentType()))
 .getQuantity(),
 E);
+  case UETT_VectorElements: {
+QualType Ty = E->getTypeOfArgument();
+// If the vector has a fixed size, we can determine the number of elements 
at compile time.
+if (Ty->isVectorType())
+  return Success(Ty->castAs()->getNumElements(), E);
+
+return false;
+  }
   }
 
   llvm_unreachable("unknown expr/type trait");
diff --git a/clang/lib/AST/ItaniumMangle.cpp b/clang/lib/AST/ItaniumMangle.cpp
index 23ec35cae4b7b40..171dfe429c12d31 100644
--- a/clang/lib/AST/ItaniumMangle.cpp
+++ b/clang/lib/AST/ItaniumMangle.cpp
@@ -5126,6 +5126,14 @@ void CXXNameMangler::mangleExpression(const Expr *E, 
unsigned Arity,
   Diags.Report(DiagID);
   return;
 }
+case UETT_VectorElements: {
+  DiagnosticsEngine &Diags = Context.getDiags();
+  unsigned DiagID = Diags.getCustomDiagID(
+  DiagnosticsEngine::Error,
+  "cannot yet mangle __builtin_vectorelements expression");
+  Diags.Report(DiagID);
+  return;
+}
 }
 break;
   }
diff --git a/clang/lib/AST/Type.cpp b/clang/lib/AST/Type.cpp
index 4c433f7fe9daca0..050761784498a9c 100644
--- a/clang/lib/AST/Type.cpp
+++ b/clang/lib/AST/Type.cpp
@@ -2369,7 +2369,7 @@ bool Type::isIncompleteType(NamedDecl **Def) const {
 }
 
 bool Type::isSizelessBuiltinType() const {
-  if (isSVESizelessBuiltinType() || isRVVSizelessBuiltinType())
+  if (isSizelessVectorType())
 return true;
 
   if (const BuiltinType *BT = getAs()) {
@@ -2403,6 +2403,10 @@ bool Type::isWebAssemblyTableType() const {
 
 bool Type::isSizelessType() const { return isSizelessBuiltinType(); }
 
+bool Type::isSizelessVectorType() const {
+  return isSVESizelessBuiltinType() || isRVVSizelessBuiltinType();
+}
+
 bool Type::isSVESizelessBuiltinType() const {
   if (const BuiltinType *BT = getAs()) {
 switch (BT->getKind()) {

[libunwind] [Clang] Add __builtin_vectorelements to get number of elements in vector (PR #69010)

2023-10-17 Thread Lawrence Benson via cfe-commits

https://github.com/lawben updated 
https://github.com/llvm/llvm-project/pull/69010

>From df8d0a53a31e1351bb6cd3b340e9012b489e9885 Mon Sep 17 00:00:00 2001
From: Lawrence Benson 
Date: Wed, 11 Oct 2023 17:26:11 +0200
Subject: [PATCH 01/10] Add __builtin_vectorelements to get the number of
 elements in a fixed-sized vector at compile-time or via a @llvm.vscale call
 at runtime.

---
 clang/include/clang/AST/Type.h   |  3 +++
 clang/include/clang/Basic/Builtins.def   |  1 +
 clang/include/clang/Basic/TokenKinds.def |  1 +
 clang/lib/AST/ExprConstant.cpp   |  8 
 clang/lib/AST/ItaniumMangle.cpp  |  8 
 clang/lib/AST/Type.cpp   |  6 +-
 clang/lib/CodeGen/CGExprScalar.cpp   | 12 
 clang/lib/Parse/ParseExpr.cpp|  7 +--
 clang/lib/Sema/SemaChecking.cpp  | 18 ++
 clang/lib/Sema/SemaExpr.cpp  | 14 ++
 10 files changed, 75 insertions(+), 3 deletions(-)

diff --git a/clang/include/clang/AST/Type.h b/clang/include/clang/AST/Type.h
index a78d8f60462b231..f6e425783176ba2 100644
--- a/clang/include/clang/AST/Type.h
+++ b/clang/include/clang/AST/Type.h
@@ -2058,6 +2058,9 @@ class alignas(8) Type : public ExtQualsTypeCommonBase {
   bool isSizelessType() const;
   bool isSizelessBuiltinType() const;
 
+  /// Returns true for all scalable vector types.
+  bool isSizelessVectorType() const;
+
   /// Returns true for SVE scalable vector types.
   bool isSVESizelessBuiltinType() const;
 
diff --git a/clang/include/clang/Basic/Builtins.def 
b/clang/include/clang/Basic/Builtins.def
index 6ea8484606cfd5d..6033e8a955fb8bd 100644
--- a/clang/include/clang/Basic/Builtins.def
+++ b/clang/include/clang/Basic/Builtins.def
@@ -674,6 +674,7 @@ BUILTIN(__builtin_debugtrap, "v", "n")
 BUILTIN(__builtin_unreachable, "v", "nr")
 BUILTIN(__builtin_shufflevector, "v."   , "nct")
 BUILTIN(__builtin_convertvector, "v."   , "nct")
+BUILTIN(__builtin_vectorelements, "v."  , "nct")
 BUILTIN(__builtin_alloca, "v*z"   , "Fn")
 BUILTIN(__builtin_alloca_uninitialized, "v*z", "Fn")
 BUILTIN(__builtin_alloca_with_align, "v*zIz", "Fn")
diff --git a/clang/include/clang/Basic/TokenKinds.def 
b/clang/include/clang/Basic/TokenKinds.def
index 94db56a9fd5d78c..bbae1200d376c0d 100644
--- a/clang/include/clang/Basic/TokenKinds.def
+++ b/clang/include/clang/Basic/TokenKinds.def
@@ -746,6 +746,7 @@ ALIAS("_pascal"  , __pascal   , KEYBORLAND)
 
 // Clang Extensions.
 KEYWORD(__builtin_convertvector  , KEYALL)
+UNARY_EXPR_OR_TYPE_TRAIT(__builtin_vectorelements, VectorElements, KEYALL)
 ALIAS("__char16_t"   , char16_t  , KEYCXX)
 ALIAS("__char32_t"   , char32_t  , KEYCXX)
 KEYWORD(__builtin_bit_cast   , KEYALL)
diff --git a/clang/lib/AST/ExprConstant.cpp b/clang/lib/AST/ExprConstant.cpp
index e5539dedec02a4b..eb36a57e462f3f1 100644
--- a/clang/lib/AST/ExprConstant.cpp
+++ b/clang/lib/AST/ExprConstant.cpp
@@ -13595,6 +13595,14 @@ bool IntExprEvaluator::VisitUnaryExprOrTypeTraitExpr(
 Info.Ctx.getOpenMPDefaultSimdAlign(E->getArgumentType()))
 .getQuantity(),
 E);
+  case UETT_VectorElements: {
+QualType Ty = E->getTypeOfArgument();
+// If the vector has a fixed size, we can determine the number of elements 
at compile time.
+if (Ty->isVectorType())
+  return Success(Ty->castAs()->getNumElements(), E);
+
+return false;
+  }
   }
 
   llvm_unreachable("unknown expr/type trait");
diff --git a/clang/lib/AST/ItaniumMangle.cpp b/clang/lib/AST/ItaniumMangle.cpp
index 23ec35cae4b7b40..171dfe429c12d31 100644
--- a/clang/lib/AST/ItaniumMangle.cpp
+++ b/clang/lib/AST/ItaniumMangle.cpp
@@ -5126,6 +5126,14 @@ void CXXNameMangler::mangleExpression(const Expr *E, 
unsigned Arity,
   Diags.Report(DiagID);
   return;
 }
+case UETT_VectorElements: {
+  DiagnosticsEngine &Diags = Context.getDiags();
+  unsigned DiagID = Diags.getCustomDiagID(
+  DiagnosticsEngine::Error,
+  "cannot yet mangle __builtin_vectorelements expression");
+  Diags.Report(DiagID);
+  return;
+}
 }
 break;
   }
diff --git a/clang/lib/AST/Type.cpp b/clang/lib/AST/Type.cpp
index 4c433f7fe9daca0..050761784498a9c 100644
--- a/clang/lib/AST/Type.cpp
+++ b/clang/lib/AST/Type.cpp
@@ -2369,7 +2369,7 @@ bool Type::isIncompleteType(NamedDecl **Def) const {
 }
 
 bool Type::isSizelessBuiltinType() const {
-  if (isSVESizelessBuiltinType() || isRVVSizelessBuiltinType())
+  if (isSizelessVectorType())
 return true;
 
   if (const BuiltinType *BT = getAs()) {
@@ -2403,6 +2403,10 @@ bool Type::isWebAssemblyTableType() const {
 
 bool Type::isSizelessType() const { return isSizelessBuiltinType(); }
 
+bool Type::isSizelessVectorType() const {
+  return isSVESizelessBuiltinType() || isRVVSizelessBuiltinType();
+}
+
 bool Type::isSVESizelessBuiltinType() const {
   if (const BuiltinType *BT = getAs()) {
 switch (BT->getKind()) {

[clang] [Clang] Add __builtin_vectorelements to get number of elements in vector (PR #69010)

2023-10-17 Thread Lawrence Benson via cfe-commits

https://github.com/lawben updated 
https://github.com/llvm/llvm-project/pull/69010

>From df8d0a53a31e1351bb6cd3b340e9012b489e9885 Mon Sep 17 00:00:00 2001
From: Lawrence Benson 
Date: Wed, 11 Oct 2023 17:26:11 +0200
Subject: [PATCH 01/10] Add __builtin_vectorelements to get the number of
 elements in a fixed-sized vector at compile-time or via a @llvm.vscale call
 at runtime.

---
 clang/include/clang/AST/Type.h   |  3 +++
 clang/include/clang/Basic/Builtins.def   |  1 +
 clang/include/clang/Basic/TokenKinds.def |  1 +
 clang/lib/AST/ExprConstant.cpp   |  8 
 clang/lib/AST/ItaniumMangle.cpp  |  8 
 clang/lib/AST/Type.cpp   |  6 +-
 clang/lib/CodeGen/CGExprScalar.cpp   | 12 
 clang/lib/Parse/ParseExpr.cpp|  7 +--
 clang/lib/Sema/SemaChecking.cpp  | 18 ++
 clang/lib/Sema/SemaExpr.cpp  | 14 ++
 10 files changed, 75 insertions(+), 3 deletions(-)

diff --git a/clang/include/clang/AST/Type.h b/clang/include/clang/AST/Type.h
index a78d8f60462b231..f6e425783176ba2 100644
--- a/clang/include/clang/AST/Type.h
+++ b/clang/include/clang/AST/Type.h
@@ -2058,6 +2058,9 @@ class alignas(8) Type : public ExtQualsTypeCommonBase {
   bool isSizelessType() const;
   bool isSizelessBuiltinType() const;
 
+  /// Returns true for all scalable vector types.
+  bool isSizelessVectorType() const;
+
   /// Returns true for SVE scalable vector types.
   bool isSVESizelessBuiltinType() const;
 
diff --git a/clang/include/clang/Basic/Builtins.def 
b/clang/include/clang/Basic/Builtins.def
index 6ea8484606cfd5d..6033e8a955fb8bd 100644
--- a/clang/include/clang/Basic/Builtins.def
+++ b/clang/include/clang/Basic/Builtins.def
@@ -674,6 +674,7 @@ BUILTIN(__builtin_debugtrap, "v", "n")
 BUILTIN(__builtin_unreachable, "v", "nr")
 BUILTIN(__builtin_shufflevector, "v."   , "nct")
 BUILTIN(__builtin_convertvector, "v."   , "nct")
+BUILTIN(__builtin_vectorelements, "v."  , "nct")
 BUILTIN(__builtin_alloca, "v*z"   , "Fn")
 BUILTIN(__builtin_alloca_uninitialized, "v*z", "Fn")
 BUILTIN(__builtin_alloca_with_align, "v*zIz", "Fn")
diff --git a/clang/include/clang/Basic/TokenKinds.def 
b/clang/include/clang/Basic/TokenKinds.def
index 94db56a9fd5d78c..bbae1200d376c0d 100644
--- a/clang/include/clang/Basic/TokenKinds.def
+++ b/clang/include/clang/Basic/TokenKinds.def
@@ -746,6 +746,7 @@ ALIAS("_pascal"  , __pascal   , KEYBORLAND)
 
 // Clang Extensions.
 KEYWORD(__builtin_convertvector  , KEYALL)
+UNARY_EXPR_OR_TYPE_TRAIT(__builtin_vectorelements, VectorElements, KEYALL)
 ALIAS("__char16_t"   , char16_t  , KEYCXX)
 ALIAS("__char32_t"   , char32_t  , KEYCXX)
 KEYWORD(__builtin_bit_cast   , KEYALL)
diff --git a/clang/lib/AST/ExprConstant.cpp b/clang/lib/AST/ExprConstant.cpp
index e5539dedec02a4b..eb36a57e462f3f1 100644
--- a/clang/lib/AST/ExprConstant.cpp
+++ b/clang/lib/AST/ExprConstant.cpp
@@ -13595,6 +13595,14 @@ bool IntExprEvaluator::VisitUnaryExprOrTypeTraitExpr(
 Info.Ctx.getOpenMPDefaultSimdAlign(E->getArgumentType()))
 .getQuantity(),
 E);
+  case UETT_VectorElements: {
+QualType Ty = E->getTypeOfArgument();
+// If the vector has a fixed size, we can determine the number of elements 
at compile time.
+if (Ty->isVectorType())
+  return Success(Ty->castAs()->getNumElements(), E);
+
+return false;
+  }
   }
 
   llvm_unreachable("unknown expr/type trait");
diff --git a/clang/lib/AST/ItaniumMangle.cpp b/clang/lib/AST/ItaniumMangle.cpp
index 23ec35cae4b7b40..171dfe429c12d31 100644
--- a/clang/lib/AST/ItaniumMangle.cpp
+++ b/clang/lib/AST/ItaniumMangle.cpp
@@ -5126,6 +5126,14 @@ void CXXNameMangler::mangleExpression(const Expr *E, 
unsigned Arity,
   Diags.Report(DiagID);
   return;
 }
+case UETT_VectorElements: {
+  DiagnosticsEngine &Diags = Context.getDiags();
+  unsigned DiagID = Diags.getCustomDiagID(
+  DiagnosticsEngine::Error,
+  "cannot yet mangle __builtin_vectorelements expression");
+  Diags.Report(DiagID);
+  return;
+}
 }
 break;
   }
diff --git a/clang/lib/AST/Type.cpp b/clang/lib/AST/Type.cpp
index 4c433f7fe9daca0..050761784498a9c 100644
--- a/clang/lib/AST/Type.cpp
+++ b/clang/lib/AST/Type.cpp
@@ -2369,7 +2369,7 @@ bool Type::isIncompleteType(NamedDecl **Def) const {
 }
 
 bool Type::isSizelessBuiltinType() const {
-  if (isSVESizelessBuiltinType() || isRVVSizelessBuiltinType())
+  if (isSizelessVectorType())
 return true;
 
   if (const BuiltinType *BT = getAs()) {
@@ -2403,6 +2403,10 @@ bool Type::isWebAssemblyTableType() const {
 
 bool Type::isSizelessType() const { return isSizelessBuiltinType(); }
 
+bool Type::isSizelessVectorType() const {
+  return isSVESizelessBuiltinType() || isRVVSizelessBuiltinType();
+}
+
 bool Type::isSVESizelessBuiltinType() const {
   if (const BuiltinType *BT = getAs()) {
 switch (BT->getKind()) {

[clang] [Clang] Add `__builtin_experimental_vectorcompress` (PR #102476)

2024-09-11 Thread Lawrence Benson via cfe-commits

lawben wrote:

@philnik777 This PR is still waiting for #104904 and #105515 to be merged, so 
we have support for vaid x86, NEON, and SVE vectors. Once those PRs are in, 
this can probably be merged too.

https://github.com/llvm/llvm-project/pull/102476
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [Clang] Add support for scalable vectors in __builtin_reduce_* functions (PR #87750)

2024-04-05 Thread Lawrence Benson via cfe-commits

https://github.com/lawben created 
https://github.com/llvm/llvm-project/pull/87750

Currently, a lot of `__builtin_reduce_*` function do not support scalable 
vectors, i.e., ARM SVE and RISCV V. This PR adds support for them. The main 
code change is to use a different path to extract the type from the vectors, 
the rest is the same and LLVM supports the reduce functions for `vscale` 
vectors.

This PR adds scalable vector support for:
- `__builtin_reduce_add`
- `__builtin_reduce_mul`
- `__builtin_reduce_xor`
- `__builtin_reduce_or`
- `__builtin_reduce_and`
- `__builtin_reduce_min`
- `__builtin_reduce_max`

Note: For all except `min/max`, the element type must still be an integer 
value. Adding floating point support for `add` and `mul` is still an open TODO.

>From a56d6eacc7053a0dac38c5b7ba21d2e37a790baa Mon Sep 17 00:00:00 2001
From: Lawrence Benson 
Date: Fri, 5 Apr 2024 09:15:30 +0200
Subject: [PATCH] [AARCH64,RISCV] Add support for scalable vectors in
 __builtin_reduce* functions.

---
 clang/include/clang/AST/Type.h   |  4 ++
 clang/lib/AST/Type.cpp   | 12 +
 clang/lib/CodeGen/CGBuiltin.cpp  | 10 +++-
 clang/lib/Sema/SemaChecking.cpp  | 23 +++--
 clang/test/CodeGen/builtins-reduction-math.c | 53 
 5 files changed, 96 insertions(+), 6 deletions(-)

diff --git a/clang/include/clang/AST/Type.h b/clang/include/clang/AST/Type.h
index 99f45d518c7960..a9f888a037109b 100644
--- a/clang/include/clang/AST/Type.h
+++ b/clang/include/clang/AST/Type.h
@@ -2172,6 +2172,10 @@ class alignas(TypeAlignment) Type : public 
ExtQualsTypeCommonBase {
   /// 'riscv_rvv_vector_bits' type attribute as VectorType.
   QualType getRVVEltType(const ASTContext &Ctx) const;
 
+  /// Returns the representative type for the element of a sizeless vector
+  /// builtin type.
+  QualType getSizelessVectorEltType(const ASTContext &Ctx) const;
+
   /// Types are partitioned into 3 broad categories (C99 6.2.5p1):
   /// object types, function types, and incomplete types.
 
diff --git a/clang/lib/AST/Type.cpp b/clang/lib/AST/Type.cpp
index cb22c91a12aa89..dcba47de0cc7ae 100644
--- a/clang/lib/AST/Type.cpp
+++ b/clang/lib/AST/Type.cpp
@@ -2510,6 +2510,18 @@ bool Type::isSveVLSBuiltinType() const {
   return false;
 }
 
+QualType Type::getSizelessVectorEltType(const ASTContext &Ctx) const {
+  assert(isSizelessVectorType() && "Must be sizeless vector type");
+  // Currently supports SVE and RVV
+  if (isSVESizelessBuiltinType())
+return getSveEltType(Ctx);
+
+  if (isRVVSizelessBuiltinType())
+return getRVVEltType(Ctx);
+
+  llvm_unreachable("Unhandled type");
+}
+
 QualType Type::getSveEltType(const ASTContext &Ctx) const {
   assert(isSveVLSBuiltinType() && "unsupported type!");
 
diff --git a/clang/lib/CodeGen/CGBuiltin.cpp b/clang/lib/CodeGen/CGBuiltin.cpp
index 2537e715b63ee4..e76a211242fdd7 100644
--- a/clang/lib/CodeGen/CGBuiltin.cpp
+++ b/clang/lib/CodeGen/CGBuiltin.cpp
@@ -3868,9 +3868,12 @@ RValue CodeGenFunction::EmitBuiltinExpr(const GlobalDecl 
GD, unsigned BuiltinID,
   }
 
   case Builtin::BI__builtin_reduce_max: {
-auto GetIntrinsicID = [](QualType QT) {
+auto GetIntrinsicID = [this](QualType QT) {
   if (auto *VecTy = QT->getAs())
 QT = VecTy->getElementType();
+  else if (QT->isSizelessVectorType())
+QT = QT->getSizelessVectorEltType(CGM.getContext());
+
   if (QT->isSignedIntegerType())
 return llvm::Intrinsic::vector_reduce_smax;
   if (QT->isUnsignedIntegerType())
@@ -3883,9 +3886,12 @@ RValue CodeGenFunction::EmitBuiltinExpr(const GlobalDecl 
GD, unsigned BuiltinID,
   }
 
   case Builtin::BI__builtin_reduce_min: {
-auto GetIntrinsicID = [](QualType QT) {
+auto GetIntrinsicID = [this](QualType QT) {
   if (auto *VecTy = QT->getAs())
 QT = VecTy->getElementType();
+  else if (QT->isSizelessVectorType())
+QT = QT->getSizelessVectorEltType(CGM.getContext());
+
   if (QT->isSignedIntegerType())
 return llvm::Intrinsic::vector_reduce_smin;
   if (QT->isUnsignedIntegerType())
diff --git a/clang/lib/Sema/SemaChecking.cpp b/clang/lib/Sema/SemaChecking.cpp
index 3dcd18b3afc8b4..6d45dd8bb7ed97 100644
--- a/clang/lib/Sema/SemaChecking.cpp
+++ b/clang/lib/Sema/SemaChecking.cpp
@@ -3166,13 +3166,20 @@ Sema::CheckBuiltinFunctionCall(FunctionDecl *FDecl, 
unsigned BuiltinID,
 
 const Expr *Arg = TheCall->getArg(0);
 const auto *TyA = Arg->getType()->getAs();
-if (!TyA) {
+
+QualType ElTy;
+if (TyA)
+  ElTy = TyA->getElementType();
+else if (Arg->getType()->isSizelessVectorType())
+  ElTy = Arg->getType()->getSizelessVectorEltType(Context);
+
+if (ElTy.isNull()) {
   Diag(Arg->getBeginLoc(), diag::err_builtin_invalid_arg_type)
   << 1 << /* vector ty*/ 4 << Arg->getType();
   return ExprError();
 }
 
-TheCall->setType(TyA->getElementType());
+TheCall->setType(ElTy);
 break;
   }
 
@

[clang] [Clang] Add support for scalable vectors in __builtin_reduce_* functions (PR #87750)

2024-04-11 Thread Lawrence Benson via cfe-commits

lawben wrote:

@efriedma-quic While there is no target independent way to create a type, it is 
possible to pass this type around. This is the use case I'm thinking of. So if 
a user has one `#ifdef` block to get the right `using VecT = ...` for the 
platform, they can use that `VecT` all over the place, regardless of the exact 
type. I'll add this to the docs and release note.

https://github.com/llvm/llvm-project/pull/87750
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [Clang] Add support for scalable vectors in __builtin_reduce_* functions (PR #87750)

2024-04-11 Thread Lawrence Benson via cfe-commits

https://github.com/lawben updated 
https://github.com/llvm/llvm-project/pull/87750

>From a56d6eacc7053a0dac38c5b7ba21d2e37a790baa Mon Sep 17 00:00:00 2001
From: Lawrence Benson 
Date: Fri, 5 Apr 2024 09:15:30 +0200
Subject: [PATCH] [AARCH64,RISCV] Add support for scalable vectors in
 __builtin_reduce* functions.

---
 clang/include/clang/AST/Type.h   |  4 ++
 clang/lib/AST/Type.cpp   | 12 +
 clang/lib/CodeGen/CGBuiltin.cpp  | 10 +++-
 clang/lib/Sema/SemaChecking.cpp  | 23 +++--
 clang/test/CodeGen/builtins-reduction-math.c | 53 
 5 files changed, 96 insertions(+), 6 deletions(-)

diff --git a/clang/include/clang/AST/Type.h b/clang/include/clang/AST/Type.h
index 99f45d518c7960..a9f888a037109b 100644
--- a/clang/include/clang/AST/Type.h
+++ b/clang/include/clang/AST/Type.h
@@ -2172,6 +2172,10 @@ class alignas(TypeAlignment) Type : public 
ExtQualsTypeCommonBase {
   /// 'riscv_rvv_vector_bits' type attribute as VectorType.
   QualType getRVVEltType(const ASTContext &Ctx) const;
 
+  /// Returns the representative type for the element of a sizeless vector
+  /// builtin type.
+  QualType getSizelessVectorEltType(const ASTContext &Ctx) const;
+
   /// Types are partitioned into 3 broad categories (C99 6.2.5p1):
   /// object types, function types, and incomplete types.
 
diff --git a/clang/lib/AST/Type.cpp b/clang/lib/AST/Type.cpp
index cb22c91a12aa89..dcba47de0cc7ae 100644
--- a/clang/lib/AST/Type.cpp
+++ b/clang/lib/AST/Type.cpp
@@ -2510,6 +2510,18 @@ bool Type::isSveVLSBuiltinType() const {
   return false;
 }
 
+QualType Type::getSizelessVectorEltType(const ASTContext &Ctx) const {
+  assert(isSizelessVectorType() && "Must be sizeless vector type");
+  // Currently supports SVE and RVV
+  if (isSVESizelessBuiltinType())
+return getSveEltType(Ctx);
+
+  if (isRVVSizelessBuiltinType())
+return getRVVEltType(Ctx);
+
+  llvm_unreachable("Unhandled type");
+}
+
 QualType Type::getSveEltType(const ASTContext &Ctx) const {
   assert(isSveVLSBuiltinType() && "unsupported type!");
 
diff --git a/clang/lib/CodeGen/CGBuiltin.cpp b/clang/lib/CodeGen/CGBuiltin.cpp
index 2537e715b63ee4..e76a211242fdd7 100644
--- a/clang/lib/CodeGen/CGBuiltin.cpp
+++ b/clang/lib/CodeGen/CGBuiltin.cpp
@@ -3868,9 +3868,12 @@ RValue CodeGenFunction::EmitBuiltinExpr(const GlobalDecl 
GD, unsigned BuiltinID,
   }
 
   case Builtin::BI__builtin_reduce_max: {
-auto GetIntrinsicID = [](QualType QT) {
+auto GetIntrinsicID = [this](QualType QT) {
   if (auto *VecTy = QT->getAs())
 QT = VecTy->getElementType();
+  else if (QT->isSizelessVectorType())
+QT = QT->getSizelessVectorEltType(CGM.getContext());
+
   if (QT->isSignedIntegerType())
 return llvm::Intrinsic::vector_reduce_smax;
   if (QT->isUnsignedIntegerType())
@@ -3883,9 +3886,12 @@ RValue CodeGenFunction::EmitBuiltinExpr(const GlobalDecl 
GD, unsigned BuiltinID,
   }
 
   case Builtin::BI__builtin_reduce_min: {
-auto GetIntrinsicID = [](QualType QT) {
+auto GetIntrinsicID = [this](QualType QT) {
   if (auto *VecTy = QT->getAs())
 QT = VecTy->getElementType();
+  else if (QT->isSizelessVectorType())
+QT = QT->getSizelessVectorEltType(CGM.getContext());
+
   if (QT->isSignedIntegerType())
 return llvm::Intrinsic::vector_reduce_smin;
   if (QT->isUnsignedIntegerType())
diff --git a/clang/lib/Sema/SemaChecking.cpp b/clang/lib/Sema/SemaChecking.cpp
index 3dcd18b3afc8b4..6d45dd8bb7ed97 100644
--- a/clang/lib/Sema/SemaChecking.cpp
+++ b/clang/lib/Sema/SemaChecking.cpp
@@ -3166,13 +3166,20 @@ Sema::CheckBuiltinFunctionCall(FunctionDecl *FDecl, 
unsigned BuiltinID,
 
 const Expr *Arg = TheCall->getArg(0);
 const auto *TyA = Arg->getType()->getAs();
-if (!TyA) {
+
+QualType ElTy;
+if (TyA)
+  ElTy = TyA->getElementType();
+else if (Arg->getType()->isSizelessVectorType())
+  ElTy = Arg->getType()->getSizelessVectorEltType(Context);
+
+if (ElTy.isNull()) {
   Diag(Arg->getBeginLoc(), diag::err_builtin_invalid_arg_type)
   << 1 << /* vector ty*/ 4 << Arg->getType();
   return ExprError();
 }
 
-TheCall->setType(TyA->getElementType());
+TheCall->setType(ElTy);
 break;
   }
 
@@ -3188,12 +3195,20 @@ Sema::CheckBuiltinFunctionCall(FunctionDecl *FDecl, 
unsigned BuiltinID,
 
 const Expr *Arg = TheCall->getArg(0);
 const auto *TyA = Arg->getType()->getAs();
-if (!TyA || !TyA->getElementType()->isIntegerType()) {
+
+QualType ElTy;
+if (TyA)
+  ElTy = TyA->getElementType();
+else if (Arg->getType()->isSizelessVectorType())
+  ElTy = Arg->getType()->getSizelessVectorEltType(Context);
+
+if (ElTy.isNull() || !ElTy->isIntegerType()) {
   Diag(Arg->getBeginLoc(), diag::err_builtin_invalid_arg_type)
   << 1  << /* vector of integers */ 6 << Arg->getType();
   return ExprError();
 }
-TheCall->setType(Ty

[clang] [Clang] Add support for scalable vectors in __builtin_reduce_* functions (PR #87750)

2024-04-11 Thread Lawrence Benson via cfe-commits

https://github.com/lawben updated 
https://github.com/llvm/llvm-project/pull/87750

>From a56d6eacc7053a0dac38c5b7ba21d2e37a790baa Mon Sep 17 00:00:00 2001
From: Lawrence Benson 
Date: Fri, 5 Apr 2024 09:15:30 +0200
Subject: [PATCH 1/2] [AARCH64,RISCV] Add support for scalable vectors in
 __builtin_reduce* functions.

---
 clang/include/clang/AST/Type.h   |  4 ++
 clang/lib/AST/Type.cpp   | 12 +
 clang/lib/CodeGen/CGBuiltin.cpp  | 10 +++-
 clang/lib/Sema/SemaChecking.cpp  | 23 +++--
 clang/test/CodeGen/builtins-reduction-math.c | 53 
 5 files changed, 96 insertions(+), 6 deletions(-)

diff --git a/clang/include/clang/AST/Type.h b/clang/include/clang/AST/Type.h
index 99f45d518c7960..a9f888a037109b 100644
--- a/clang/include/clang/AST/Type.h
+++ b/clang/include/clang/AST/Type.h
@@ -2172,6 +2172,10 @@ class alignas(TypeAlignment) Type : public 
ExtQualsTypeCommonBase {
   /// 'riscv_rvv_vector_bits' type attribute as VectorType.
   QualType getRVVEltType(const ASTContext &Ctx) const;
 
+  /// Returns the representative type for the element of a sizeless vector
+  /// builtin type.
+  QualType getSizelessVectorEltType(const ASTContext &Ctx) const;
+
   /// Types are partitioned into 3 broad categories (C99 6.2.5p1):
   /// object types, function types, and incomplete types.
 
diff --git a/clang/lib/AST/Type.cpp b/clang/lib/AST/Type.cpp
index cb22c91a12aa89..dcba47de0cc7ae 100644
--- a/clang/lib/AST/Type.cpp
+++ b/clang/lib/AST/Type.cpp
@@ -2510,6 +2510,18 @@ bool Type::isSveVLSBuiltinType() const {
   return false;
 }
 
+QualType Type::getSizelessVectorEltType(const ASTContext &Ctx) const {
+  assert(isSizelessVectorType() && "Must be sizeless vector type");
+  // Currently supports SVE and RVV
+  if (isSVESizelessBuiltinType())
+return getSveEltType(Ctx);
+
+  if (isRVVSizelessBuiltinType())
+return getRVVEltType(Ctx);
+
+  llvm_unreachable("Unhandled type");
+}
+
 QualType Type::getSveEltType(const ASTContext &Ctx) const {
   assert(isSveVLSBuiltinType() && "unsupported type!");
 
diff --git a/clang/lib/CodeGen/CGBuiltin.cpp b/clang/lib/CodeGen/CGBuiltin.cpp
index 2537e715b63ee4..e76a211242fdd7 100644
--- a/clang/lib/CodeGen/CGBuiltin.cpp
+++ b/clang/lib/CodeGen/CGBuiltin.cpp
@@ -3868,9 +3868,12 @@ RValue CodeGenFunction::EmitBuiltinExpr(const GlobalDecl 
GD, unsigned BuiltinID,
   }
 
   case Builtin::BI__builtin_reduce_max: {
-auto GetIntrinsicID = [](QualType QT) {
+auto GetIntrinsicID = [this](QualType QT) {
   if (auto *VecTy = QT->getAs())
 QT = VecTy->getElementType();
+  else if (QT->isSizelessVectorType())
+QT = QT->getSizelessVectorEltType(CGM.getContext());
+
   if (QT->isSignedIntegerType())
 return llvm::Intrinsic::vector_reduce_smax;
   if (QT->isUnsignedIntegerType())
@@ -3883,9 +3886,12 @@ RValue CodeGenFunction::EmitBuiltinExpr(const GlobalDecl 
GD, unsigned BuiltinID,
   }
 
   case Builtin::BI__builtin_reduce_min: {
-auto GetIntrinsicID = [](QualType QT) {
+auto GetIntrinsicID = [this](QualType QT) {
   if (auto *VecTy = QT->getAs())
 QT = VecTy->getElementType();
+  else if (QT->isSizelessVectorType())
+QT = QT->getSizelessVectorEltType(CGM.getContext());
+
   if (QT->isSignedIntegerType())
 return llvm::Intrinsic::vector_reduce_smin;
   if (QT->isUnsignedIntegerType())
diff --git a/clang/lib/Sema/SemaChecking.cpp b/clang/lib/Sema/SemaChecking.cpp
index 3dcd18b3afc8b4..6d45dd8bb7ed97 100644
--- a/clang/lib/Sema/SemaChecking.cpp
+++ b/clang/lib/Sema/SemaChecking.cpp
@@ -3166,13 +3166,20 @@ Sema::CheckBuiltinFunctionCall(FunctionDecl *FDecl, 
unsigned BuiltinID,
 
 const Expr *Arg = TheCall->getArg(0);
 const auto *TyA = Arg->getType()->getAs();
-if (!TyA) {
+
+QualType ElTy;
+if (TyA)
+  ElTy = TyA->getElementType();
+else if (Arg->getType()->isSizelessVectorType())
+  ElTy = Arg->getType()->getSizelessVectorEltType(Context);
+
+if (ElTy.isNull()) {
   Diag(Arg->getBeginLoc(), diag::err_builtin_invalid_arg_type)
   << 1 << /* vector ty*/ 4 << Arg->getType();
   return ExprError();
 }
 
-TheCall->setType(TyA->getElementType());
+TheCall->setType(ElTy);
 break;
   }
 
@@ -3188,12 +3195,20 @@ Sema::CheckBuiltinFunctionCall(FunctionDecl *FDecl, 
unsigned BuiltinID,
 
 const Expr *Arg = TheCall->getArg(0);
 const auto *TyA = Arg->getType()->getAs();
-if (!TyA || !TyA->getElementType()->isIntegerType()) {
+
+QualType ElTy;
+if (TyA)
+  ElTy = TyA->getElementType();
+else if (Arg->getType()->isSizelessVectorType())
+  ElTy = Arg->getType()->getSizelessVectorEltType(Context);
+
+if (ElTy.isNull() || !ElTy->isIntegerType()) {
   Diag(Arg->getBeginLoc(), diag::err_builtin_invalid_arg_type)
   << 1  << /* vector of integers */ 6 << Arg->getType();
   return ExprError();
 }
-TheCall->setTyp

[clang] [Clang] Add support for scalable vectors in __builtin_reduce_* functions (PR #87750)

2024-04-19 Thread Lawrence Benson via cfe-commits

lawben wrote:

@efriedma-quic @fhahn just a quick ping for this PR. is something still 
missing?  

https://github.com/llvm/llvm-project/pull/87750
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [Clang] Add support for scalable vectors in __builtin_reduce_* functions (PR #87750)

2024-04-29 Thread Lawrence Benson via cfe-commits

https://github.com/lawben updated 
https://github.com/llvm/llvm-project/pull/87750

>From a56d6eacc7053a0dac38c5b7ba21d2e37a790baa Mon Sep 17 00:00:00 2001
From: Lawrence Benson 
Date: Fri, 5 Apr 2024 09:15:30 +0200
Subject: [PATCH 1/2] [AARCH64,RISCV] Add support for scalable vectors in
 __builtin_reduce* functions.

---
 clang/include/clang/AST/Type.h   |  4 ++
 clang/lib/AST/Type.cpp   | 12 +
 clang/lib/CodeGen/CGBuiltin.cpp  | 10 +++-
 clang/lib/Sema/SemaChecking.cpp  | 23 +++--
 clang/test/CodeGen/builtins-reduction-math.c | 53 
 5 files changed, 96 insertions(+), 6 deletions(-)

diff --git a/clang/include/clang/AST/Type.h b/clang/include/clang/AST/Type.h
index 99f45d518c7960..a9f888a037109b 100644
--- a/clang/include/clang/AST/Type.h
+++ b/clang/include/clang/AST/Type.h
@@ -2172,6 +2172,10 @@ class alignas(TypeAlignment) Type : public 
ExtQualsTypeCommonBase {
   /// 'riscv_rvv_vector_bits' type attribute as VectorType.
   QualType getRVVEltType(const ASTContext &Ctx) const;
 
+  /// Returns the representative type for the element of a sizeless vector
+  /// builtin type.
+  QualType getSizelessVectorEltType(const ASTContext &Ctx) const;
+
   /// Types are partitioned into 3 broad categories (C99 6.2.5p1):
   /// object types, function types, and incomplete types.
 
diff --git a/clang/lib/AST/Type.cpp b/clang/lib/AST/Type.cpp
index cb22c91a12aa89..dcba47de0cc7ae 100644
--- a/clang/lib/AST/Type.cpp
+++ b/clang/lib/AST/Type.cpp
@@ -2510,6 +2510,18 @@ bool Type::isSveVLSBuiltinType() const {
   return false;
 }
 
+QualType Type::getSizelessVectorEltType(const ASTContext &Ctx) const {
+  assert(isSizelessVectorType() && "Must be sizeless vector type");
+  // Currently supports SVE and RVV
+  if (isSVESizelessBuiltinType())
+return getSveEltType(Ctx);
+
+  if (isRVVSizelessBuiltinType())
+return getRVVEltType(Ctx);
+
+  llvm_unreachable("Unhandled type");
+}
+
 QualType Type::getSveEltType(const ASTContext &Ctx) const {
   assert(isSveVLSBuiltinType() && "unsupported type!");
 
diff --git a/clang/lib/CodeGen/CGBuiltin.cpp b/clang/lib/CodeGen/CGBuiltin.cpp
index 2537e715b63ee4..e76a211242fdd7 100644
--- a/clang/lib/CodeGen/CGBuiltin.cpp
+++ b/clang/lib/CodeGen/CGBuiltin.cpp
@@ -3868,9 +3868,12 @@ RValue CodeGenFunction::EmitBuiltinExpr(const GlobalDecl 
GD, unsigned BuiltinID,
   }
 
   case Builtin::BI__builtin_reduce_max: {
-auto GetIntrinsicID = [](QualType QT) {
+auto GetIntrinsicID = [this](QualType QT) {
   if (auto *VecTy = QT->getAs())
 QT = VecTy->getElementType();
+  else if (QT->isSizelessVectorType())
+QT = QT->getSizelessVectorEltType(CGM.getContext());
+
   if (QT->isSignedIntegerType())
 return llvm::Intrinsic::vector_reduce_smax;
   if (QT->isUnsignedIntegerType())
@@ -3883,9 +3886,12 @@ RValue CodeGenFunction::EmitBuiltinExpr(const GlobalDecl 
GD, unsigned BuiltinID,
   }
 
   case Builtin::BI__builtin_reduce_min: {
-auto GetIntrinsicID = [](QualType QT) {
+auto GetIntrinsicID = [this](QualType QT) {
   if (auto *VecTy = QT->getAs())
 QT = VecTy->getElementType();
+  else if (QT->isSizelessVectorType())
+QT = QT->getSizelessVectorEltType(CGM.getContext());
+
   if (QT->isSignedIntegerType())
 return llvm::Intrinsic::vector_reduce_smin;
   if (QT->isUnsignedIntegerType())
diff --git a/clang/lib/Sema/SemaChecking.cpp b/clang/lib/Sema/SemaChecking.cpp
index 3dcd18b3afc8b4..6d45dd8bb7ed97 100644
--- a/clang/lib/Sema/SemaChecking.cpp
+++ b/clang/lib/Sema/SemaChecking.cpp
@@ -3166,13 +3166,20 @@ Sema::CheckBuiltinFunctionCall(FunctionDecl *FDecl, 
unsigned BuiltinID,
 
 const Expr *Arg = TheCall->getArg(0);
 const auto *TyA = Arg->getType()->getAs();
-if (!TyA) {
+
+QualType ElTy;
+if (TyA)
+  ElTy = TyA->getElementType();
+else if (Arg->getType()->isSizelessVectorType())
+  ElTy = Arg->getType()->getSizelessVectorEltType(Context);
+
+if (ElTy.isNull()) {
   Diag(Arg->getBeginLoc(), diag::err_builtin_invalid_arg_type)
   << 1 << /* vector ty*/ 4 << Arg->getType();
   return ExprError();
 }
 
-TheCall->setType(TyA->getElementType());
+TheCall->setType(ElTy);
 break;
   }
 
@@ -3188,12 +3195,20 @@ Sema::CheckBuiltinFunctionCall(FunctionDecl *FDecl, 
unsigned BuiltinID,
 
 const Expr *Arg = TheCall->getArg(0);
 const auto *TyA = Arg->getType()->getAs();
-if (!TyA || !TyA->getElementType()->isIntegerType()) {
+
+QualType ElTy;
+if (TyA)
+  ElTy = TyA->getElementType();
+else if (Arg->getType()->isSizelessVectorType())
+  ElTy = Arg->getType()->getSizelessVectorEltType(Context);
+
+if (ElTy.isNull() || !ElTy->isIntegerType()) {
   Diag(Arg->getBeginLoc(), diag::err_builtin_invalid_arg_type)
   << 1  << /* vector of integers */ 6 << Arg->getType();
   return ExprError();
 }
-TheCall->setTyp

[clang] [Clang] Add support for scalable vectors in __builtin_reduce_* functions (PR #87750)

2024-04-29 Thread Lawrence Benson via cfe-commits

https://github.com/lawben closed https://github.com/llvm/llvm-project/pull/87750
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [Clang] Add `__builtin_experimental_vectorcompress` (PR #102476)

2024-08-08 Thread Lawrence Benson via cfe-commits

https://github.com/lawben created 
https://github.com/llvm/llvm-project/pull/102476

This PR exposes the new `@llvm.experimental.vector.compress` intrinsic to 
Clang, so it can be called from C/C++. 

TODO: Add Docs and Release note if people are fine with this addition.

>From 40e7eca608e7f8cfe87fc51022dd878df67e0e12 Mon Sep 17 00:00:00 2001
From: Lawrence Benson 
Date: Thu, 8 Aug 2024 14:46:17 +0200
Subject: [PATCH 1/2] Add __builtin_vectorcompress skeleton

---
 clang/include/clang/Basic/Builtins.td   |  6 ++
 clang/lib/CodeGen/CGBuiltin.cpp | 14 
 clang/lib/Sema/SemaChecking.cpp | 46 
 clang/test/CodeGen/builtin_vectorcompress.c | 81 +
 4 files changed, 147 insertions(+)
 create mode 100644 clang/test/CodeGen/builtin_vectorcompress.c

diff --git a/clang/include/clang/Basic/Builtins.td 
b/clang/include/clang/Basic/Builtins.td
index b025a7681bfac3..666d4b2b65978a 100644
--- a/clang/include/clang/Basic/Builtins.td
+++ b/clang/include/clang/Basic/Builtins.td
@@ -1218,6 +1218,12 @@ def NondetermenisticValue : Builtin {
   let Prototype = "void(...)";
 }
 
+def VectorCompress : Builtin {
+  let Spellings = ["__builtin_experimental_vectorcompress"];
+  let Attributes = [NoThrow, Const, CustomTypeChecking];
+  let Prototype = "void(...)";
+}
+
 def ElementwiseAbs : Builtin {
   let Spellings = ["__builtin_elementwise_abs"];
   let Attributes = [NoThrow, Const, CustomTypeChecking];
diff --git a/clang/lib/CodeGen/CGBuiltin.cpp b/clang/lib/CodeGen/CGBuiltin.cpp
index 51d1162c6e403c..86d47a2c533151 100644
--- a/clang/lib/CodeGen/CGBuiltin.cpp
+++ b/clang/lib/CodeGen/CGBuiltin.cpp
@@ -3773,6 +3773,20 @@ RValue CodeGenFunction::EmitBuiltinExpr(const GlobalDecl 
GD, unsigned BuiltinID,
 return RValue::get(Result);
   }
 
+  case Builtin::BI__builtin_experimental_vectorcompress: {
+QualType VecTy = E->getArg(0)->getType();
+Value *Vec = EmitScalarExpr(E->getArg(0));
+Value *Mask = EmitScalarExpr(E->getArg(1));
+Value *Passthru = E->getNumArgs() == 3 ? EmitScalarExpr(E->getArg(2)) : 
llvm::UndefValue::get(ConvertType(VecTy));
+
+// Cast svbool_t to right number of elements.
+if (VecTy->isSVESizelessBuiltinType())
+  Mask = EmitSVEPredicateCast(Mask, 
cast(Vec->getType()));
+
+Function *F = CGM.getIntrinsic(Intrinsic::experimental_vector_compress, 
Vec->getType());
+return RValue::get(Builder.CreateCall(F, {Vec, Mask, Passthru}));
+  }
+
   case Builtin::BI__builtin_elementwise_abs: {
 Value *Result;
 QualType QT = E->getArg(0)->getType();
diff --git a/clang/lib/Sema/SemaChecking.cpp b/clang/lib/Sema/SemaChecking.cpp
index ee143381cf4f79..68010b11759a2b 100644
--- a/clang/lib/Sema/SemaChecking.cpp
+++ b/clang/lib/Sema/SemaChecking.cpp
@@ -2804,6 +2804,52 @@ Sema::CheckBuiltinFunctionCall(FunctionDecl *FDecl, 
unsigned BuiltinID,
 TheCall->setType(Magnitude.get()->getType());
 break;
   }
+  case Builtin::BI__builtin_experimental_vectorcompress: {
+unsigned NumArgs = TheCall->getNumArgs();
+if (NumArgs < 2 || NumArgs > 3)
+  return ExprError();
+
+Expr *VecArg = TheCall->getArg(0);
+QualType VecTy = VecArg->getType();
+if (!VecTy->isVectorType() && !VecTy->isSizelessVectorType()) {
+  Diag(VecArg->getBeginLoc(), diag::err_builtin_invalid_arg_type)
+  << 1 << /* vector ty*/ 4 << VecTy;
+  return ExprError();
+}
+
+Expr *MaskArg = TheCall->getArg(1);
+QualType MaskTy = MaskArg->getType();
+if (!MaskTy->isVectorType() && !MaskTy->isSizelessVectorType()) {
+  Diag(MaskArg->getBeginLoc(), diag::err_builtin_invalid_arg_type)
+  << 1 << /* vector ty*/ 4 << MaskTy;
+  return ExprError();
+}
+
+if (VecTy->isVectorType() != MaskTy->isVectorType()) {
+  // TODO: diag
+  return ExprError();
+}
+
+if (VecTy->isVectorType() && VecTy->getAs()->getNumElements() 
!= MaskTy->getAs()->getNumElements()) {
+// TODO: diag
+return ExprError();
+}
+
+// TODO: find way to compare MinKnownElements for sizeless vectors.
+// if (VecTy->isSizelessVectorType() && 
VecTy->getAs()->getNumElements() != 
MaskTy->getAs()->getNumElements()) {}
+
+if (NumArgs == 3) {
+  Expr *PassthruArg = TheCall->getArg(2);
+  QualType PassthruTy = PassthruArg->getType();
+  if (PassthruTy != VecTy) {
+// TODO: diag
+return ExprError();
+  }
+}
+TheCall->setType(VecTy);
+
+break;
+  }
   case Builtin::BI__builtin_reduce_max:
   case Builtin::BI__builtin_reduce_min: {
 if (PrepareBuiltinReduceMathOneArgCall(TheCall))
diff --git a/clang/test/CodeGen/builtin_vectorcompress.c 
b/clang/test/CodeGen/builtin_vectorcompress.c
new file mode 100644
index 00..1eebb3461241b5
--- /dev/null
+++ b/clang/test/CodeGen/builtin_vectorcompress.c
@@ -0,0 +1,81 @@
+// NOTE: Assertions have been autogenerated by utils/update_cc_test_checks.py 
UTC_ARGS: --version 5
+// RUN: %clang_cc1 -O1 -t

[clang] [Clang] Add `__builtin_experimental_vectorcompress` (PR #102476)

2024-08-20 Thread Lawrence Benson via cfe-commits

lawben wrote:

Makes sense. I'll try to add the logic for the reamining SVE types in the next 
few days. I've been working on x86 instruction selection for this. I'll wait 
with this PR until both ARM and x86 are supported.

https://github.com/llvm/llvm-project/pull/102476
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits