[clang] [Clang][AArch64] Add fix vector types to header into SVE (PR #73258)

Sander de Smalen via cfe-commits cfe-commits at lists.llvm.org
Mon Nov 27 03:44:46 PST 2023


================
@@ -2546,6 +2548,44 @@ void NeonEmitter::runFP16(raw_ostream &OS) {
   OS << "#endif /* __ARM_FP16_H */\n";
 }
 
+void NeonEmitter::runVectorType(raw_ostream &OS) {
+  OS << "/*===---- arm_vector_type - ARM vector type "
+        "------===\n"
+        " *\n"
+        " *\n"
+        " * Part of the LLVM Project, under the Apache License v2.0 with LLVM "
+        "Exceptions.\n"
+        " * See https://llvm.org/LICENSE.txt for license information.\n"
+        " * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception\n"
+        " *\n"
+        " *===-----------------------------------------------------------------"
+        "------===\n"
+        " */\n\n";
+  OS << "#ifndef __ARM_NEON_TYPES_H\n";
+  OS << "#define __ARM_NEON_TYPES_H\n";
+  OS << "#ifdef __cplusplus\n";
+  OS << "extern \"C\" {\n";
+  OS << "#endif\n";
+  OS << "#ifndef __ARM_NEON_H\n";
+
+  std::string TypedefTypes("QcQsQiQlQUcQUsQUiQUlQhQfQdQb");
+  std::vector<TypeSpec> TDTypeVec = TypeSpec::fromTypeSpecs(TypedefTypes);
+  for (auto &TS : TDTypeVec) {
+    Type T(TS, ".");
+    OS << "typedef __attribute__((vector_size(16))) ";
----------------
sdesmalen-arm wrote:

This is using `vector_type`, as opposed to `neon_vector_type` which is used in arm_neon.h. This means that when you would do:
```
#include <arm_neon.h>
#include <arm_vector_type.h>
```
you would get an error that you've redeclared some of these types. See: https://godbolt.org/z/ddbGrsYKK

>From what I can tell, the attributes are almost the same with the exception that Clang ensures that:
* NEON/MVE is enabled in order for these attributes to be valid
* the number of elements / element-type is a valid combination for NEON.

My suggestion would be to use `vector_type` in the separate header like you're doing here since we don't really need the additional checks, because;
* In arm_neon.h it requires NEON to be enabled to even get to the typedef
* We know that the combination of number of elements / element type is valid.

You just need to make sure to remove the conflicting typedefs from arm_neon.h.

It may also be worth adding something like this to this header file:
```
#if !defined(__ARM_NEON) && !defined(__ARM_SVE)
#error "This header should only be included from arm_neon.h or arm_sve.h"
#endif
```
to avoid this header being used outside of the suggested use.

https://github.com/llvm/llvm-project/pull/73258


More information about the cfe-commits mailing list