Skip to content

Commit 86bc486

Browse files
authored
[BOLT][RISCV] Use target features from object file (#69836)
We used to hard-code target features for RISC-V. However, most features (with the exception of relax) are stored in the object file. This patch extracts those features to ensure BOLT's output doesn't use any features not present in the input file.
1 parent 3eed23d commit 86bc486

File tree

3 files changed

+22
-16
lines changed

3 files changed

+22
-16
lines changed

bolt/lib/Core/BinaryContext.cpp

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ Expected<std::unique_ptr<BinaryContext>>
118118
BinaryContext::createBinaryContext(const ObjectFile *File, bool IsPIC,
119119
std::unique_ptr<DWARFContext> DwCtx) {
120120
StringRef ArchName = "";
121-
StringRef FeaturesStr = "";
121+
std::string FeaturesStr = "";
122122
switch (File->getArch()) {
123123
case llvm::Triple::x86_64:
124124
ArchName = "x86-64";
@@ -128,11 +128,20 @@ BinaryContext::createBinaryContext(const ObjectFile *File, bool IsPIC,
128128
ArchName = "aarch64";
129129
FeaturesStr = "+all";
130130
break;
131-
case llvm::Triple::riscv64:
131+
case llvm::Triple::riscv64: {
132132
ArchName = "riscv64";
133-
// RV64GC
134-
FeaturesStr = "+m,+a,+f,+d,+zicsr,+zifencei,+c,+relax";
133+
Expected<SubtargetFeatures> Features = File->getFeatures();
134+
135+
if (auto E = Features.takeError())
136+
return E;
137+
138+
// We rely on relaxation for some transformations (e.g., promoting all calls
139+
// to PseudoCALL and then making JITLink relax them). Since the relax
140+
// feature is not stored in the object file, we manually enable it.
141+
Features->AddFeature("relax");
142+
FeaturesStr = Features->getString();
135143
break;
144+
}
136145
default:
137146
return createStringError(std::errc::not_supported,
138147
"BOLT-ERROR: Unrecognized machine in ELF file");

bolt/test/RISCV/call-annotations.s

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
/// Test that annotations are properly carried over to fixed calls.
22
/// Note that --enable-bat is used to force offsets to be kept.
33

4-
// RUN: llvm-mc -triple riscv64 -filetype obj -o %t.o %s
4+
// RUN: llvm-mc -triple riscv64 -mattr=+c -filetype obj -o %t.o %s
55
// RUN: ld.lld --emit-relocs -o %t %t.o
66
// RUN: llvm-bolt --enable-bat --print-cfg --print-fix-riscv-calls \
77
// RUN: -o /dev/null %t | FileCheck %s
88

99
.text
10+
.option norvc
1011
.global f
1112
.p2align 1
1213
f:

bolt/test/RISCV/internal-func-reloc.s

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,14 @@
22
/// get transformed by BOLT. The tests rely on the "remove-nops" optimization:
33
/// if nops got removed from the function, it got transformed by BOLT.
44

5-
// RUN: %clang %cflags -o %t %s
5+
// RUN: llvm-mc -triple riscv64 -filetype=obj -o %t.o %s
6+
// RUN: ld.lld --emit-relocs -o %t %t.o
67
// RUN: llvm-bolt -o %t.bolt %t
78
// RUN: llvm-objdump -d %t.bolt | FileCheck %s
89

910
.text
10-
11-
/// These options are only used to make the assembler output easier to predict
12-
.option norelax
13-
.option norvc
14-
1511
.globl _start
16-
.p2align 1
12+
.p2align 2
1713
// CHECK: <_start>:
1814
// CHECK-NEXT: j 0x{{.*}} <_start>
1915
_start:
@@ -23,10 +19,10 @@ _start:
2319
.size _start, .-_start
2420

2521
.globl f
26-
.p2align 1
22+
.p2align 2
2723
// CHECK: <f>:
28-
// CHECK-NEXT: auipc a0, 0
29-
// CHECK-NEXT: addi a0, a0, 64
24+
// CHECK-NEXT: auipc a0, [[#]]
25+
// CHECK-NEXT: addi a0, a0, [[#]]
3026
f:
3127
nop
3228
1:
@@ -37,7 +33,7 @@ f:
3733
.size f, .-f
3834

3935
.globl g
40-
.p2align 1
36+
.p2align 2
4137
g:
4238
ret
4339
.size g, .-g

0 commit comments

Comments
 (0)