Skip to content

Commit 7b13394

Browse files
asbtstellar
authored andcommitted
[RISCV] Allow llvm-objdump to disassemble objects with unrecognised versions of known extensions
This Moves ELFObjectFile to using RISCVISAInfo::parseNormalizedArchString which does not ignore or produce an error for ISA extensions with a version that isn't recognised/supported by LLVM. As current GCC is marking its objects with a higher version of the A, F, and D extensions than LLVM (see [extension versioning discussion](https://discourse.llvm.org/t/rfc-resolving-issues-related-to-extension-versioning-in-risc-v/68472) this massively improves the usability of llvm-objdump with such binaries. This patch is functionally a cherry-pick of af602ed and 91c6174, though I've constructed the backport manually because other changes unrelated to this specific fix make a direct cherry pick difficult.
1 parent 110b4fd commit 7b13394

File tree

4 files changed

+119
-6
lines changed

4 files changed

+119
-6
lines changed

llvm/lib/Object/ELFObjectFile.cpp

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -303,12 +303,7 @@ Expected<SubtargetFeatures> ELFObjectFileBase::getRISCVFeatures() const {
303303
std::optional<StringRef> Attr =
304304
Attributes.getAttributeString(RISCVAttrs::ARCH);
305305
if (Attr) {
306-
// Suppress version checking for experimental extensions to prevent erroring
307-
// when getting any unknown version of experimental extension.
308-
auto ParseResult = RISCVISAInfo::parseArchString(
309-
*Attr, /*EnableExperimentalExtension=*/true,
310-
/*ExperimentalExtensionVersionCheck=*/false,
311-
/*IgnoreUnknown=*/true);
306+
auto ParseResult = RISCVISAInfo::parseNormalizedArchString(*Attr);
312307
if (!ParseResult)
313308
return ParseResult.takeError();
314309
auto &ISAInfo = *ParseResult;

llvm/lib/Support/RISCVISAInfo.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1060,6 +1060,8 @@ std::vector<std::string> RISCVISAInfo::toFeatureVector() const {
10601060
std::string ExtName = Ext.first;
10611061
if (ExtName == "i") // i is not recognized in clang -cc1
10621062
continue;
1063+
if (!isSupportedExtension(ExtName))
1064+
continue;
10631065
std::string Feature = isExperimentalExtension(ExtName)
10641066
? "+experimental-" + ExtName
10651067
: "+" + ExtName;
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
# RUN: rm -rf %t && split-file %s %t && cd %t
2+
3+
# RUN: llvm-mc -filetype=obj -triple=riscv64 -mattr=+m,+f,+d,+v noncanonicalized_arch.s -o noncanonicalized_arch.o
4+
# RUN: not llvm-objdump -d noncanonicalized_arch.o 2>&1 | FileCheck %s -DFILE=noncanonicalized_arch.o --check-prefix=NONCANON
5+
6+
# RUN: llvm-mc -filetype=obj -triple=riscv64 invalid_arch.s -o invalid_arch.o
7+
# RUN: not llvm-objdump -d invalid_arch.o 2>&1 | FileCheck %s -DFILE=invalid_arch.o --check-prefix=INVALID
8+
9+
# RUN: llvm-mc -filetype=obj -triple=riscv32 unknown_i_version.s -o unknown_i_version.o
10+
# RUN: llvm-objdump -d unknown_i_version.o 2>&1 | FileCheck %s --check-prefix=UNKNOWN-I-VERSION
11+
12+
# RUN: llvm-mc -filetype=obj -triple=riscv32 -mattr=+zicbom unknown_ext_version.s -o unknown_ext_version.o
13+
# RUN: llvm-objdump -d unknown_ext_version.o 2>&1 | FileCheck %s --check-prefix=UNKNOWN-EXT-VERSION
14+
15+
# RUN: llvm-mc -filetype=obj -triple=riscv64 unknown_ext_name.s -o unknown_ext_name.o
16+
# RUN: llvm-objdump -d unknown_ext_name.o 2>&1 | FileCheck %s --check-prefix=UNKNOWN-EXT-NAME
17+
18+
#--- noncanonicalized_arch.s
19+
# NONCANON: error: '[[FILE]]': arch string must begin with valid base ISA
20+
# NONCANON-NOT: {{.}}
21+
vsetvli a3, a2, e8, m8, tu, mu
22+
23+
.section .riscv.attributes,"",@0x70000003
24+
.byte 0x41
25+
.long .Lend-.riscv.attributes-1
26+
.asciz "riscv" # vendor
27+
.Lbegin:
28+
.byte 1 # Tag_File
29+
.long .Lend-.Lbegin
30+
.byte 5 # Tag_RISCV_arch
31+
.asciz "rv64gcv"
32+
.Lend:
33+
34+
#--- invalid_arch.s
35+
# INVALID: error: '[[FILE]]': arch string must begin with valid base ISA
36+
# INVALID-NOT: {{.}}
37+
nop
38+
39+
.section .riscv.attributes,"",@0x70000003
40+
.byte 0x41
41+
.long .Lend-.riscv.attributes-1
42+
.asciz "riscv" # vendor
43+
.Lbegin:
44+
.byte 1 # Tag_File
45+
.long .Lend-.Lbegin
46+
.byte 5 # Tag_RISCV_arch
47+
.asciz "nonsense"
48+
.Lend:
49+
50+
#--- unknown_i_version.s
51+
# UNKNOWN-I-VERSION: nop
52+
nop
53+
54+
.section .riscv.attributes,"",@0x70000003
55+
.byte 0x41
56+
.long .Lend-.riscv.attributes-1
57+
.asciz "riscv" # vendor
58+
.Lbegin:
59+
.byte 1 # Tag_File
60+
.long .Lend-.Lbegin
61+
.byte 5 # Tag_RISCV_arch
62+
.asciz "rv32i99p99"
63+
.Lend:
64+
65+
#--- unknown_ext_version.s
66+
# UNKNOWN-EXT-VERSION: cbo.clean (t0)
67+
cbo.clean (t0)
68+
69+
.section .riscv.attributes,"",@0x70000003
70+
.byte 0x41
71+
.long .Lend-.riscv.attributes-1
72+
.asciz "riscv" # vendor
73+
.Lbegin:
74+
.byte 1 # Tag_File
75+
.long .Lend-.Lbegin
76+
.byte 5 # Tag_RISCV_arch
77+
.asciz "rv32i2p0_zicbom0p1"
78+
.Lend:
79+
80+
#--- unknown_ext_name.s
81+
# UNKNOWN-EXT-NAME: nop
82+
nop
83+
84+
.section .riscv.attributes,"",@0x70000003
85+
.byte 0x41
86+
.long .Lend-.riscv.attributes-1
87+
.asciz "riscv" # vendor
88+
.Lbegin:
89+
.byte 1 # Tag_File
90+
.long .Lend-.Lbegin
91+
.byte 5 # Tag_RISCV_arch
92+
.asciz "rv32i2p0_zmadeup1p0_smadeup1p0_xmadeup1p0_sxmadeup1p0"
93+
.Lend:

llvm/unittests/Support/RISCVISAInfoTest.cpp

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
#include "llvm/Testing/Support/Error.h"
1111
#include "gtest/gtest.h"
1212

13+
using ::testing::ElementsAre;
14+
1315
using namespace llvm;
1416

1517
bool operator==(const llvm::RISCVExtensionInfo &A,
@@ -103,3 +105,24 @@ TEST(ParseNormalizedArchString, UpdatesFLenMinVLenMaxELen) {
103105
EXPECT_EQ(Info.getMinVLen(), 64U);
104106
EXPECT_EQ(Info.getMaxELen(), 64U);
105107
}
108+
109+
TEST(ToFeatureVector, IIsDroppedAndExperimentalExtensionsArePrefixed) {
110+
auto MaybeISAInfo1 =
111+
RISCVISAInfo::parseArchString("rv64im_zihintntl", true, false);
112+
ASSERT_THAT_EXPECTED(MaybeISAInfo1, Succeeded());
113+
EXPECT_THAT((*MaybeISAInfo1)->toFeatureVector(),
114+
ElementsAre("+m", "+experimental-zihintntl"));
115+
116+
auto MaybeISAInfo2 = RISCVISAInfo::parseArchString(
117+
"rv32e_zihintntl_xventanacondops", true, false);
118+
ASSERT_THAT_EXPECTED(MaybeISAInfo2, Succeeded());
119+
EXPECT_THAT((*MaybeISAInfo2)->toFeatureVector(),
120+
ElementsAre("+e", "+experimental-zihintntl", "+xventanacondops"));
121+
}
122+
123+
TEST(ToFeatureVector, UnsupportedExtensionsAreDropped) {
124+
auto MaybeISAInfo =
125+
RISCVISAInfo::parseNormalizedArchString("rv64i2p0_m2p0_xmadeup1p0");
126+
ASSERT_THAT_EXPECTED(MaybeISAInfo, Succeeded());
127+
EXPECT_THAT((*MaybeISAInfo)->toFeatureVector(), ElementsAre("+m"));
128+
}

0 commit comments

Comments
 (0)