[compiler-rt] [RISCV][compiler-rt] Small fixes for __riscv_feature_bits (PR #99958)

Philip Reames via llvm-commits llvm-commits at lists.llvm.org
Mon Jul 22 14:58:36 PDT 2024


https://github.com/preames created https://github.com/llvm/llvm-project/pull/99958

Changes included:
* Adding CONSTRUCTOR_ATTRIBUTE so that the static data is setup early on in process lifetime.  This is required by gcc docs for __builtin_cpu_supports which we hope to implement in terms of this.
* Move the length initialization outside of the #if defined(__linux__) block so that the length field always reflects the size of the structures even if non of the feature bits are non-zero.
* Change the __riscv_vendor_feature_bits.length field to match the length of the actual structure.

Note that this change has not been built or tested.  I could not figure out how to get a working cross build for compiler-rt setup.  @BeMg, if you could confirm this builds and passes tests in your environment, I'd appreciate it.  

>From 1e6f521b25ff60432c4c6ead08e9c6e473977b07 Mon Sep 17 00:00:00 2001
From: Philip Reames <preames at rivosinc.com>
Date: Mon, 22 Jul 2024 14:51:55 -0700
Subject: [PATCH] [RISCV][compiler-rt] Small fixes for __riscv_feature_bits

Changes included:
* Adding CONSTRUCTOR_ATTRIBUTE so that the static data is setup early on
  in process lifetime.  This is required by gcc docs for
  __builtin_cpu_supports which we hope to implement in terms of this.
* Move the length initialization outside of the #if defined(__linux__)
  block so that the length field always reflects the size of the structures
  even if non of the feature bits are non-zero.
* Change the __riscv_vendor_feature_bits.length field to match the length
  of the actual structure.

Note that this change has not been built or tested.  I could not figure
out how to get a working cross build for compiler-rt setup.
---
 compiler-rt/lib/builtins/riscv/feature_bits.c | 14 +++++++++++---
 1 file changed, 11 insertions(+), 3 deletions(-)

diff --git a/compiler-rt/lib/builtins/riscv/feature_bits.c b/compiler-rt/lib/builtins/riscv/feature_bits.c
index 77422935bd2d3..da4208396a0f9 100644
--- a/compiler-rt/lib/builtins/riscv/feature_bits.c
+++ b/compiler-rt/lib/builtins/riscv/feature_bits.c
@@ -204,12 +204,10 @@ static void initRISCVFeature(struct riscv_hwprobe Hwprobes[]) {
   // This unsets all extension bitmask bits.
 
   // Init vendor extension
-  __riscv_vendor_feature_bits.length = 0;
   __riscv_vendor_feature_bits.vendorID = Hwprobes[2].value;
 
   // Init standard extension
   // TODO: Maybe Extension implied generate from tablegen?
-  __riscv_feature_bits.length = RISCV_FEATURE_BITS_LENGTH;
 
   unsigned long long features[RISCV_FEATURE_BITS_LENGTH];
   int i;
@@ -277,11 +275,21 @@ static void initRISCVFeature(struct riscv_hwprobe Hwprobes[]) {
 
 static int FeaturesBitCached = 0;
 
-void __init_riscv_feature_bits() {
+void __init_riscv_feature_bits() CONSTRUCTOR_ATTRIBUTE;
+
+// A constructor function that is sets __riscv_feature_bits, and
+// __riscv_vendor_feature_bits to the right values.  This needs to run
+// only once.  This constructor is given the highest priority and it should
+// run before constructors without the priority set.  However, it still runs
+// after ifunc initializers and needs to be called explicitly there.
+void CONSTRUCTOR_ATTRIBUTE __init_riscv_feature_bits() {
 
   if (FeaturesBitCached)
     return;
 
+  __riscv_feature_bits.length = RISCV_FEATURE_BITS_LENGTH;
+  __riscv_vendor_feature_bits.length = RISCV_VENDOR_FEATURE_BITS_LENGTH;
+
 #if defined(__linux__)
   struct riscv_hwprobe Hwprobes[] = {
       {RISCV_HWPROBE_KEY_BASE_BEHAVIOR, 0},



More information about the llvm-commits mailing list