[clang] [clang][Builtins] Parse clang extended vectors types. (PR #83584)

Francesco Petrogalli via cfe-commits cfe-commits at lists.llvm.org
Fri Mar 1 11:16:18 PST 2024


https://github.com/fpetrogalli updated https://github.com/llvm/llvm-project/pull/83584

>From 9cd3e17192833e2cbafed55b649307ead32dcc3e Mon Sep 17 00:00:00 2001
From: Francesco Petrogalli <francesco.petrogalli at apple.com>
Date: Fri, 1 Mar 2024 16:23:57 +0100
Subject: [PATCH 1/4] [clang][Builtins] Parse clang extended vectors types.

Clang extended vector types are mangled as follows:

   ext_vector_type_<lanes>_<scalar type>

This is used to defetmine the builtins signature for builtins that
use parmeters defined as

    typedef <scalar type> ext_vector_type_<lanes>_<scalar type> __attribute__((ext_vector_type(<lanes>)))

For example:

    typedef double ext_vector_type_4_double __attribute__((ext_vector_type(4)))
---
 .../target-builtins-prototype-parser.td       | 20 +++++++++++++++++++
 clang/utils/TableGen/ClangBuiltinsEmitter.cpp | 10 ++++++++++
 2 files changed, 30 insertions(+)
 create mode 100644 clang/test/TableGen/target-builtins-prototype-parser.td

diff --git a/clang/test/TableGen/target-builtins-prototype-parser.td b/clang/test/TableGen/target-builtins-prototype-parser.td
new file mode 100644
index 00000000000000..681a607da7e115
--- /dev/null
+++ b/clang/test/TableGen/target-builtins-prototype-parser.td
@@ -0,0 +1,20 @@
+// RUN: clang-tblgen -I %p/../../../clang/include/ %s --gen-clang-builtins | FileCheck %s
+// RUN: not clang-tblgen -I %p/../../../clang/include/ %s --gen-clang-builtins -DERROR_EXPECTED_LANES 2>&1 | FileCheck %s --check-prefix=ERROR_EXPECTED_LANES
+
+include "clang/Basic/BuiltinsBase.td"
+
+def : Builtin {
+  let Prototype = "ext_vector_type_8_int(double, ext_vector_type_4_bool)";
+  let Spellings = ["__builtin_test_use_clang_extended_vectors"];
+}
+
+// CHECK: BUILTIN(__builtin_test_use_clang_extended_vectors, "E8idE4b", "")
+
+#ifdef ERROR_EXPECTED_LANES
+def : Builtin {
+// ERROR_EXPECTED_LANES: :[[# @LINE + 1]]:7: error: Expected number of lanes
+  let Prototype = "ext_vector_type__int(double, ext_vector_type_4_bool)";
+  let Spellings = ["__builtin_test_use_clang_extended_vectors"];
+}
+#endif
+
diff --git a/clang/utils/TableGen/ClangBuiltinsEmitter.cpp b/clang/utils/TableGen/ClangBuiltinsEmitter.cpp
index 48f55b8af97e4e..774f703390a05e 100644
--- a/clang/utils/TableGen/ClangBuiltinsEmitter.cpp
+++ b/clang/utils/TableGen/ClangBuiltinsEmitter.cpp
@@ -85,6 +85,16 @@ class PrototypeParser {
       if (Substitution.empty())
         PrintFatalError(Loc, "Not a template");
       ParseType(Substitution);
+    } else if (T.consume_front("ext_vector_type_")) {
+      // Clang extended vector types are mangled as follows:
+      //
+      //    ext_vector_type_<lanes>_<scalar type>
+      unsigned long long Lanes;
+      if (llvm::consumeUnsignedInteger(T, 10, Lanes))
+        PrintFatalError(Loc, "Expected number of lanes ");
+      Type += "E" + std::to_string(Lanes);
+      T.consume_front("_");
+      ParseType(T);
     } else {
       auto ReturnTypeVal = StringSwitch<std::string>(T)
                                .Case("__builtin_va_list_ref", "A")

>From e7ac017c9092a99828401d2209061b02570c7dbb Mon Sep 17 00:00:00 2001
From: Francesco Petrogalli <francesco.petrogalli at apple.com>
Date: Fri, 1 Mar 2024 16:41:04 +0100
Subject: [PATCH 2/4] Move the check just before the prototype. NFC.

---
 clang/test/TableGen/target-builtins-prototype-parser.td | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/clang/test/TableGen/target-builtins-prototype-parser.td b/clang/test/TableGen/target-builtins-prototype-parser.td
index 681a607da7e115..97de30d91a9491 100644
--- a/clang/test/TableGen/target-builtins-prototype-parser.td
+++ b/clang/test/TableGen/target-builtins-prototype-parser.td
@@ -4,11 +4,11 @@
 include "clang/Basic/BuiltinsBase.td"
 
 def : Builtin {
+// CHECK: BUILTIN(__builtin_test_use_clang_extended_vectors, "E8idE4b", "")
   let Prototype = "ext_vector_type_8_int(double, ext_vector_type_4_bool)";
   let Spellings = ["__builtin_test_use_clang_extended_vectors"];
 }
 
-// CHECK: BUILTIN(__builtin_test_use_clang_extended_vectors, "E8idE4b", "")
 
 #ifdef ERROR_EXPECTED_LANES
 def : Builtin {

>From 9b470e12e7578257825b50786234bcec2c36fc50 Mon Sep 17 00:00:00 2001
From: Francesco Petrogalli <francesco.petrogalli at apple.com>
Date: Fri, 1 Mar 2024 16:53:15 +0100
Subject: [PATCH 3/4] Teach how to parse vector of unsigned integral types.

---
 .../test/TableGen/target-builtins-prototype-parser.td  | 10 ++++++++--
 1 file changed, 8 insertions(+), 2 deletions(-)

diff --git a/clang/test/TableGen/target-builtins-prototype-parser.td b/clang/test/TableGen/target-builtins-prototype-parser.td
index 97de30d91a9491..fdb4b1d6e57a77 100644
--- a/clang/test/TableGen/target-builtins-prototype-parser.td
+++ b/clang/test/TableGen/target-builtins-prototype-parser.td
@@ -4,9 +4,15 @@
 include "clang/Basic/BuiltinsBase.td"
 
 def : Builtin {
-// CHECK: BUILTIN(__builtin_test_use_clang_extended_vectors, "E8idE4b", "")
+// CHECK: BUILTIN(__builtin_A, "E8idE4b", "")
   let Prototype = "ext_vector_type_8_int(double, ext_vector_type_4_bool)";
-  let Spellings = ["__builtin_test_use_clang_extended_vectors"];
+  let Spellings = ["__builtin_A"];
+}
+
+def : Builtin {
+// CHECK: BUILTIN(__builtin_B, "E8UiE4s", "")
+  let Prototype = "ext_vector_type_8_unsigned int(ext_vector_type_4_short)";
+  let Spellings = ["__builtin_B"];
 }
 
 

>From ab12de397fa0c028ad6b92751a31ccabfe89dc7d Mon Sep 17 00:00:00 2001
From: Francesco Petrogalli <francesco.petrogalli at apple.com>
Date: Fri, 1 Mar 2024 20:15:28 +0100
Subject: [PATCH 4/4] Address code review. Mangle as _ExtVector<N:type>.

---
 .../TableGen/target-builtins-prototype-parser.td   |  7 +++----
 clang/utils/TableGen/ClangBuiltinsEmitter.cpp      | 14 +++++++++-----
 2 files changed, 12 insertions(+), 9 deletions(-)

diff --git a/clang/test/TableGen/target-builtins-prototype-parser.td b/clang/test/TableGen/target-builtins-prototype-parser.td
index fdb4b1d6e57a77..bbe5673c39a087 100644
--- a/clang/test/TableGen/target-builtins-prototype-parser.td
+++ b/clang/test/TableGen/target-builtins-prototype-parser.td
@@ -5,21 +5,20 @@ include "clang/Basic/BuiltinsBase.td"
 
 def : Builtin {
 // CHECK: BUILTIN(__builtin_A, "E8idE4b", "")
-  let Prototype = "ext_vector_type_8_int(double, ext_vector_type_4_bool)";
+  let Prototype = "_ExtVector<8:int>(double, _ExtVector<4:bool>)";
   let Spellings = ["__builtin_A"];
 }
 
 def : Builtin {
 // CHECK: BUILTIN(__builtin_B, "E8UiE4s", "")
-  let Prototype = "ext_vector_type_8_unsigned int(ext_vector_type_4_short)";
+  let Prototype = "_ExtVector<8:unsigned int>(_ExtVector<4:short>)";
   let Spellings = ["__builtin_B"];
 }
 
-
 #ifdef ERROR_EXPECTED_LANES
 def : Builtin {
 // ERROR_EXPECTED_LANES: :[[# @LINE + 1]]:7: error: Expected number of lanes
-  let Prototype = "ext_vector_type__int(double, ext_vector_type_4_bool)";
+  let Prototype = "_ExtVector<int>(double)";
   let Spellings = ["__builtin_test_use_clang_extended_vectors"];
 }
 #endif
diff --git a/clang/utils/TableGen/ClangBuiltinsEmitter.cpp b/clang/utils/TableGen/ClangBuiltinsEmitter.cpp
index 774f703390a05e..8aa3bc2503cbc8 100644
--- a/clang/utils/TableGen/ClangBuiltinsEmitter.cpp
+++ b/clang/utils/TableGen/ClangBuiltinsEmitter.cpp
@@ -54,6 +54,7 @@ class PrototypeParser {
 
   void ParseType(StringRef T) {
     T = T.trim();
+
     if (T.consume_back("*")) {
       ParseType(T);
       Type += "*";
@@ -85,15 +86,18 @@ class PrototypeParser {
       if (Substitution.empty())
         PrintFatalError(Loc, "Not a template");
       ParseType(Substitution);
-    } else if (T.consume_front("ext_vector_type_")) {
+    } else if (T.consume_front("_ExtVector")) {
       // Clang extended vector types are mangled as follows:
-      //
-      //    ext_vector_type_<lanes>_<scalar type>
+      if (!T.consume_front("<"))
+        PrintFatalError(Loc, "Expected '<' after '_ExtVector'");
       unsigned long long Lanes;
       if (llvm::consumeUnsignedInteger(T, 10, Lanes))
-        PrintFatalError(Loc, "Expected number of lanes ");
+        PrintFatalError(Loc, "Expected number of lanes");
       Type += "E" + std::to_string(Lanes);
-      T.consume_front("_");
+      if (!T.consume_front(":"))
+        PrintFatalError(Loc, "Expected ':' after number of lanes");
+      if(!T.consume_back(">"))
+        PrintFatalError(Loc, "Expected '>' after scalar type");
       ParseType(T);
     } else {
       auto ReturnTypeVal = StringSwitch<std::string>(T)



More information about the cfe-commits mailing list