Skip to content

Commit 73ac953

Browse files
BeMgpreames
andauthored
[RISCV][compiler-rt] Small fixes for __riscv_feature_bits (#100158)
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: Copy from #99958 --------- Co-authored-by: Philip Reames <[email protected]>
1 parent ef8de68 commit 73ac953

File tree

2 files changed

+15
-5
lines changed

2 files changed

+15
-5
lines changed

compiler-rt/lib/builtins/CMakeLists.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -739,7 +739,7 @@ endif()
739739
set(powerpc64le_SOURCES ${powerpc64_SOURCES})
740740

741741
set(riscv_SOURCES
742-
riscv/feature_bits.c
742+
cpu_model/riscv.c
743743
riscv/fp_mode.c
744744
riscv/save.S
745745
riscv/restore.S

compiler-rt/lib/builtins/riscv/feature_bits.c renamed to compiler-rt/lib/builtins/cpu_model/riscv.c

+14-4
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
1-
//=== feature_bits.c - Update RISC-V Feature Bits Structure -*- C -*-=========//
1+
//=== cpu_model/riscv.c - Update RISC-V Feature Bits Structure -*- C -*-======//
22
//
33
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
44
// See https://llvm.org/LICENSE.txt for license information.
55
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
66
//
77
//===----------------------------------------------------------------------===//
88

9+
#include "cpu_model.h"
10+
911
#define RISCV_FEATURE_BITS_LENGTH 1
1012
struct {
1113
unsigned length;
@@ -204,12 +206,10 @@ static void initRISCVFeature(struct riscv_hwprobe Hwprobes[]) {
204206
// This unsets all extension bitmask bits.
205207

206208
// Init vendor extension
207-
__riscv_vendor_feature_bits.length = 0;
208209
__riscv_vendor_feature_bits.vendorID = Hwprobes[2].value;
209210

210211
// Init standard extension
211212
// TODO: Maybe Extension implied generate from tablegen?
212-
__riscv_feature_bits.length = RISCV_FEATURE_BITS_LENGTH;
213213

214214
unsigned long long features[RISCV_FEATURE_BITS_LENGTH];
215215
int i;
@@ -277,11 +277,21 @@ static void initRISCVFeature(struct riscv_hwprobe Hwprobes[]) {
277277

278278
static int FeaturesBitCached = 0;
279279

280-
void __init_riscv_feature_bits() {
280+
void __init_riscv_feature_bits() CONSTRUCTOR_ATTRIBUTE;
281+
282+
// A constructor function that sets __riscv_feature_bits, and
283+
// __riscv_vendor_feature_bits to the right values. This needs to run
284+
// only once. This constructor is given the highest priority and it should
285+
// run before constructors without the priority set. However, it still runs
286+
// after ifunc initializers and needs to be called explicitly there.
287+
void CONSTRUCTOR_ATTRIBUTE __init_riscv_feature_bits() {
281288

282289
if (FeaturesBitCached)
283290
return;
284291

292+
__riscv_feature_bits.length = RISCV_FEATURE_BITS_LENGTH;
293+
__riscv_vendor_feature_bits.length = RISCV_VENDOR_FEATURE_BITS_LENGTH;
294+
285295
#if defined(__linux__)
286296
struct riscv_hwprobe Hwprobes[] = {
287297
{RISCV_HWPROBE_KEY_BASE_BEHAVIOR, 0},

0 commit comments

Comments
 (0)