[compiler-rt] [AArch64][compiler-rt] Add a function returning the current vector length (PR #92921)

Kerry McLaughlin via llvm-commits llvm-commits at lists.llvm.org
Tue May 21 07:27:31 PDT 2024


https://github.com/kmclaughlin-arm created https://github.com/llvm/llvm-project/pull/92921

get_runtime_vl emits a cntd instruction if SVE is available at runtime,
otherwise it will return 0.

>From 1038d54920dbfc6dbdad0842074f8dba463a9f35 Mon Sep 17 00:00:00 2001
From: Kerry McLaughlin <kerry.mclaughlin at arm.com>
Date: Tue, 21 May 2024 09:54:52 +0000
Subject: [PATCH] [AArch64][compiler-rt] Add a function returning the current
 vector length

get_runtime_vl emits a cntd instruction if SVE is available at runtime,
otherwise it will return 0.
---
 .../lib/builtins/aarch64/sme-abi-init.c       | 25 +++++++++++++++++++
 1 file changed, 25 insertions(+)

diff --git a/compiler-rt/lib/builtins/aarch64/sme-abi-init.c b/compiler-rt/lib/builtins/aarch64/sme-abi-init.c
index b6ee12170d56d..331ec4b634b5b 100644
--- a/compiler-rt/lib/builtins/aarch64/sme-abi-init.c
+++ b/compiler-rt/lib/builtins/aarch64/sme-abi-init.c
@@ -2,6 +2,8 @@
 // See https://llvm.org/LICENSE.txt for license information.
 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
 
+#include "../cpu_model/aarch64.c"
+
 __attribute__((visibility("hidden"), nocommon))
 _Bool __aarch64_has_sme_and_tpidr2_el0;
 
@@ -50,3 +52,26 @@ __attribute__((constructor(90)))
 static void init_aarch64_has_sme(void) {
   __aarch64_has_sme_and_tpidr2_el0 = has_sme();
 }
+
+#if __GNUC__ >= 9
+#pragma GCC diagnostic ignored "-Wprio-ctor-dtor"
+#endif
+__attribute__((constructor(90)))
+void get_aarch64_cpu_features(void) {
+  if (!__aarch64_cpu_features.features)
+    __init_cpu_features();
+}
+
+__attribute__((target("sve")))
+long emit_cntd(void) {
+  long vl;
+  __asm__ __volatile__("cntd %0" : "=r" (vl));
+  return vl;
+}
+
+long get_runtime_vl(void) {
+  if (__aarch64_cpu_features.features & (1ULL << FEAT_SVE))
+    return emit_cntd();
+  else
+    return 0;
+}



More information about the llvm-commits mailing list