From 1e6f521b25ff60432c4c6ead08e9c6e473977b07 Mon Sep 17 00:00:00 2001 From: Philip Reames Date: Mon, 22 Jul 2024 14:51:55 -0700 Subject: [PATCH 1/4] [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}, From 90292050675dde99ff3072e4729bb3d20cb6a603 Mon Sep 17 00:00:00 2001 From: Philip Reames Date: Mon, 22 Jul 2024 19:32:53 -0700 Subject: [PATCH 2/4] Address review comment --- compiler-rt/lib/builtins/riscv/feature_bits.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/compiler-rt/lib/builtins/riscv/feature_bits.c b/compiler-rt/lib/builtins/riscv/feature_bits.c index da4208396a0f9..759555aaac882 100644 --- a/compiler-rt/lib/builtins/riscv/feature_bits.c +++ b/compiler-rt/lib/builtins/riscv/feature_bits.c @@ -277,7 +277,7 @@ static int FeaturesBitCached = 0; void __init_riscv_feature_bits() CONSTRUCTOR_ATTRIBUTE; -// A constructor function that is sets __riscv_feature_bits, and +// A constructor function that 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 From 4d589a348665e16e12900cb43bc4ff8da1a6c652 Mon Sep 17 00:00:00 2001 From: Philip Reames Date: Mon, 22 Jul 2024 19:37:21 -0700 Subject: [PATCH 3/4] Attempt to fix build --- compiler-rt/lib/builtins/riscv/feature_bits.c | 2 ++ 1 file changed, 2 insertions(+) diff --git a/compiler-rt/lib/builtins/riscv/feature_bits.c b/compiler-rt/lib/builtins/riscv/feature_bits.c index 759555aaac882..946208fbb8bf1 100644 --- a/compiler-rt/lib/builtins/riscv/feature_bits.c +++ b/compiler-rt/lib/builtins/riscv/feature_bits.c @@ -6,6 +6,8 @@ // //===----------------------------------------------------------------------===// +#include "cpu_model.h" + #define RISCV_FEATURE_BITS_LENGTH 1 struct { unsigned length; From ede49c7485fa49ab0b73707ed02b152afc95dfcd Mon Sep 17 00:00:00 2001 From: Philip Reames Date: Tue, 23 Jul 2024 09:16:20 -0700 Subject: [PATCH 4/4] Move feature_bits.c into cpu_model Patch by @BeMG --- compiler-rt/lib/builtins/CMakeLists.txt | 2 +- .../lib/builtins/{riscv/feature_bits.c => cpu_model/riscv.c} | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) rename compiler-rt/lib/builtins/{riscv/feature_bits.c => cpu_model/riscv.c} (99%) diff --git a/compiler-rt/lib/builtins/CMakeLists.txt b/compiler-rt/lib/builtins/CMakeLists.txt index 88a5998fd4610..13adbd6c4d57d 100644 --- a/compiler-rt/lib/builtins/CMakeLists.txt +++ b/compiler-rt/lib/builtins/CMakeLists.txt @@ -739,7 +739,7 @@ endif() set(powerpc64le_SOURCES ${powerpc64_SOURCES}) set(riscv_SOURCES - riscv/feature_bits.c + cpu_model/riscv.c riscv/fp_mode.c riscv/save.S riscv/restore.S diff --git a/compiler-rt/lib/builtins/riscv/feature_bits.c b/compiler-rt/lib/builtins/cpu_model/riscv.c similarity index 99% rename from compiler-rt/lib/builtins/riscv/feature_bits.c rename to compiler-rt/lib/builtins/cpu_model/riscv.c index 946208fbb8bf1..145954e704433 100644 --- a/compiler-rt/lib/builtins/riscv/feature_bits.c +++ b/compiler-rt/lib/builtins/cpu_model/riscv.c @@ -1,4 +1,4 @@ -//=== feature_bits.c - Update RISC-V Feature Bits Structure -*- C -*-=========// +//=== cpu_model/riscv.c - Update RISC-V Feature Bits Structure -*- C -*-======// // // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. // See https://llvm.org/LICENSE.txt for license information.