Skip to content

Commit c70f679

Browse files
committed
Update implementation to match proposal
1 parent dece4a6 commit c70f679

File tree

10 files changed

+136
-51
lines changed

10 files changed

+136
-51
lines changed

llvm/lib/Target/RISCV/AsmParser/RISCVAsmParser.cpp

Lines changed: 23 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -3173,46 +3173,44 @@ bool RISCVAsmParser::parseDirectiveOption() {
31733173
return false;
31743174
}
31753175

3176-
if (Option == "rvc") {
3176+
if (Option == "exact") {
31773177
if (Parser.parseEOL())
31783178
return true;
31793179

3180-
getTargetStreamer().emitDirectiveOptionRVC();
3181-
setFeatureBits(RISCV::FeatureStdExtC, "c");
3182-
clearFeatureBits(RISCV::FeatureDisableAsmCompression,
3183-
"disable-asm-compression");
3180+
getTargetStreamer().emitDirectiveOptionExact();
3181+
setFeatureBits(RISCV::FeatureExactAssembly,
3182+
"exact-asm");
3183+
clearFeatureBits(RISCV::FeatureRelax, "relax");
31843184
return false;
31853185
}
31863186

3187-
if (Option == "norvc") {
3187+
if (Option == "noexact") {
31883188
if (Parser.parseEOL())
31893189
return true;
31903190

3191-
getTargetStreamer().emitDirectiveOptionNoRVC();
3192-
clearFeatureBits(RISCV::FeatureStdExtC, "c");
3193-
clearFeatureBits(RISCV::FeatureStdExtZca, "zca");
3194-
setFeatureBits(RISCV::FeatureDisableAsmCompression,
3195-
"disable-asm-compression");
3191+
getTargetStreamer().emitDirectiveOptionNoExact();
3192+
clearFeatureBits(RISCV::FeatureExactAssembly,
3193+
"exact-asm");
3194+
setFeatureBits(RISCV::FeatureRelax, "relax");
31963195
return false;
31973196
}
31983197

3199-
if (Option == "autocompress") {
3198+
if (Option == "rvc") {
32003199
if (Parser.parseEOL())
32013200
return true;
32023201

3203-
getTargetStreamer().emitDirectiveOptionAutoCompress();
3204-
clearFeatureBits(RISCV::FeatureDisableAsmCompression,
3205-
"disable-asm-compression");
3202+
getTargetStreamer().emitDirectiveOptionRVC();
3203+
setFeatureBits(RISCV::FeatureStdExtC, "c");
32063204
return false;
32073205
}
32083206

3209-
if (Option == "noautocompress") {
3207+
if (Option == "norvc") {
32103208
if (Parser.parseEOL())
32113209
return true;
32123210

3213-
getTargetStreamer().emitDirectiveOptionNoAutoCompress();
3214-
setFeatureBits(RISCV::FeatureDisableAsmCompression,
3215-
"disable-asm-compression");
3211+
getTargetStreamer().emitDirectiveOptionNoRVC();
3212+
clearFeatureBits(RISCV::FeatureStdExtC, "c");
3213+
clearFeatureBits(RISCV::FeatureStdExtZca, "zca");
32163214
return false;
32173215
}
32183216

@@ -3254,8 +3252,8 @@ bool RISCVAsmParser::parseDirectiveOption() {
32543252

32553253
// Unknown option.
32563254
Warning(Parser.getTok().getLoc(), "unknown option, expected 'push', 'pop', "
3257-
"'rvc', 'norvc', 'arch', 'relax' or "
3258-
"'norelax'");
3255+
"'rvc', 'norvc', 'arch', 'relax', 'norelax', "
3256+
"'exact' or 'noexact'");
32593257
Parser.eatToEndOfStatement();
32603258
return false;
32613259
}
@@ -3466,11 +3464,12 @@ bool RISCVAsmParser::parseDirectiveVariantCC() {
34663464
void RISCVAsmParser::emitToStreamer(MCStreamer &S, const MCInst &Inst) {
34673465
MCInst CInst;
34683466
bool Res = false;
3469-
if (!getSTI().hasFeature(RISCV::FeatureDisableAsmCompression))
3470-
Res = RISCVRVC::compress(CInst, Inst, getSTI());
3467+
const MCSubtargetInfo &STI = getSTI();
3468+
if (!STI.hasFeature(RISCV::FeatureExactAssembly))
3469+
Res = RISCVRVC::compress(CInst, Inst, STI);
34713470
if (Res)
34723471
++RISCVNumInstrsCompressed;
3473-
S.emitInstruction((Res ? CInst : Inst), getSTI());
3472+
S.emitInstruction((Res ? CInst : Inst), STI);
34743473
}
34753474

34763475
void RISCVAsmParser::emitLoadImm(MCRegister DestReg, int64_t Value,

llvm/lib/Target/RISCV/MCTargetDesc/RISCVAsmBackend.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -344,6 +344,10 @@ std::pair<bool, bool> RISCVAsmBackend::relaxLEB128(const MCAssembler &Asm,
344344
// Given a compressed control flow instruction this function returns
345345
// the expanded instruction.
346346
unsigned RISCVAsmBackend::getRelaxedOpcode(unsigned Op) const {
347+
// Disable relaxation if FeatureExactAssembly
348+
if (STI.hasFeature(RISCV::FeatureExactAssembly))
349+
return Op;
350+
347351
switch (Op) {
348352
default:
349353
return Op;
@@ -371,6 +375,12 @@ unsigned RISCVAsmBackend::getRelaxedOpcode(unsigned Op) const {
371375

372376
bool RISCVAsmBackend::mayNeedRelaxation(const MCInst &Inst,
373377
const MCSubtargetInfo &STI) const {
378+
// This function has access to two STIs, the member of the AsmBackend, and the
379+
// one passed as an argument. The latter is more specific, so we query it for
380+
// specific features.
381+
if (STI.hasFeature(RISCV::FeatureExactAssembly))
382+
return false;
383+
374384
return getRelaxedOpcode(Inst.getOpcode()) != Inst.getOpcode();
375385
}
376386

llvm/lib/Target/RISCV/MCTargetDesc/RISCVELFStreamer.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,8 @@ void RISCVTargetELFStreamer::emitDirectiveOptionPIC() {}
5353
void RISCVTargetELFStreamer::emitDirectiveOptionNoPIC() {}
5454
void RISCVTargetELFStreamer::emitDirectiveOptionRVC() {}
5555
void RISCVTargetELFStreamer::emitDirectiveOptionNoRVC() {}
56-
void RISCVTargetELFStreamer::emitDirectiveOptionAutoCompress() {}
57-
void RISCVTargetELFStreamer::emitDirectiveOptionNoAutoCompress() {}
56+
void RISCVTargetELFStreamer::emitDirectiveOptionExact() {}
57+
void RISCVTargetELFStreamer::emitDirectiveOptionNoExact() {}
5858
void RISCVTargetELFStreamer::emitDirectiveOptionRelax() {}
5959
void RISCVTargetELFStreamer::emitDirectiveOptionNoRelax() {}
6060

llvm/lib/Target/RISCV/MCTargetDesc/RISCVELFStreamer.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,8 @@ class RISCVTargetELFStreamer : public RISCVTargetStreamer {
6262
void emitDirectiveOptionNoPIC() override;
6363
void emitDirectiveOptionRVC() override;
6464
void emitDirectiveOptionNoRVC() override;
65-
void emitDirectiveOptionAutoCompress() override;
66-
void emitDirectiveOptionNoAutoCompress() override;
65+
void emitDirectiveOptionExact() override;
66+
void emitDirectiveOptionNoExact() override;
6767
void emitDirectiveOptionRelax() override;
6868
void emitDirectiveOptionNoRelax() override;
6969
void emitDirectiveVariantCC(MCSymbol &Symbol) override;

llvm/lib/Target/RISCV/MCTargetDesc/RISCVTargetStreamer.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,8 @@ void RISCVTargetStreamer::emitDirectiveOptionPIC() {}
3939
void RISCVTargetStreamer::emitDirectiveOptionNoPIC() {}
4040
void RISCVTargetStreamer::emitDirectiveOptionRVC() {}
4141
void RISCVTargetStreamer::emitDirectiveOptionNoRVC() {}
42-
void RISCVTargetStreamer::emitDirectiveOptionAutoCompress() {}
43-
void RISCVTargetStreamer::emitDirectiveOptionNoAutoCompress() {}
42+
void RISCVTargetStreamer::emitDirectiveOptionExact() {}
43+
void RISCVTargetStreamer::emitDirectiveOptionNoExact() {}
4444
void RISCVTargetStreamer::emitDirectiveOptionRelax() {}
4545
void RISCVTargetStreamer::emitDirectiveOptionNoRelax() {}
4646
void RISCVTargetStreamer::emitDirectiveOptionArch(
@@ -127,12 +127,12 @@ void RISCVTargetAsmStreamer::emitDirectiveOptionNoRVC() {
127127
OS << "\t.option\tnorvc\n";
128128
}
129129

130-
void RISCVTargetAsmStreamer::emitDirectiveOptionAutoCompress() {
131-
OS << "\t.option\tautocompress\n";
130+
void RISCVTargetAsmStreamer::emitDirectiveOptionExact() {
131+
OS << "\t.option\texact\n";
132132
}
133133

134-
void RISCVTargetAsmStreamer::emitDirectiveOptionNoAutoCompress() {
135-
OS << "\t.option\tnoautocompress\n";
134+
void RISCVTargetAsmStreamer::emitDirectiveOptionNoExact() {
135+
OS << "\t.option\tnoexact\n";
136136
}
137137

138138
void RISCVTargetAsmStreamer::emitDirectiveOptionRelax() {

llvm/lib/Target/RISCV/MCTargetDesc/RISCVTargetStreamer.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,8 @@ class RISCVTargetStreamer : public MCTargetStreamer {
4747
virtual void emitDirectiveOptionNoPIC();
4848
virtual void emitDirectiveOptionRVC();
4949
virtual void emitDirectiveOptionNoRVC();
50-
virtual void emitDirectiveOptionAutoCompress();
51-
virtual void emitDirectiveOptionNoAutoCompress();
50+
virtual void emitDirectiveOptionExact();
51+
virtual void emitDirectiveOptionNoExact();
5252
virtual void emitDirectiveOptionRelax();
5353
virtual void emitDirectiveOptionNoRelax();
5454
virtual void emitDirectiveOptionArch(ArrayRef<RISCVOptionArchArg> Args);
@@ -86,8 +86,8 @@ class RISCVTargetAsmStreamer : public RISCVTargetStreamer {
8686
void emitDirectiveOptionNoPIC() override;
8787
void emitDirectiveOptionRVC() override;
8888
void emitDirectiveOptionNoRVC() override;
89-
void emitDirectiveOptionAutoCompress() override;
90-
void emitDirectiveOptionNoAutoCompress() override;
89+
void emitDirectiveOptionExact() override;
90+
void emitDirectiveOptionNoExact() override;
9191
void emitDirectiveOptionRelax() override;
9292
void emitDirectiveOptionNoRelax() override;
9393
void emitDirectiveOptionArch(ArrayRef<RISCVOptionArchArg> Args) override;

llvm/lib/Target/RISCV/RISCVAsmPrinter.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -255,7 +255,7 @@ bool RISCVAsmPrinter::EmitToStreamer(MCStreamer &S, const MCInst &Inst,
255255
const MCSubtargetInfo &SubtargetInfo) {
256256
MCInst CInst;
257257
bool Res = false;
258-
if (!SubtargetInfo.hasFeature(RISCV::FeatureDisableAsmCompression))
258+
if (!SubtargetInfo.hasFeature(RISCV::FeatureExactAssembly))
259259
Res = RISCVRVC::compress(CInst, Inst, SubtargetInfo);
260260
if (Res)
261261
++RISCVNumInstrsCompressed;

llvm/lib/Target/RISCV/RISCVFeatures.td

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1470,9 +1470,9 @@ def FeatureRelax
14701470
: SubtargetFeature<"relax", "EnableLinkerRelax", "true",
14711471
"Enable Linker relaxation.">;
14721472

1473-
def FeatureDisableAsmCompression
1474-
: SubtargetFeature<"disable-asm-compression", "EnableAsmCompression", "false",
1475-
"Disable Automatic Assembly Compression.">;
1473+
def FeatureExactAssembly
1474+
: SubtargetFeature<"exact-asm", "EnableExactAssembly", "true",
1475+
"Enable Exact Assembly (Disables Compression and Relaxation)">;
14761476

14771477
foreach i = {1-31} in
14781478
def FeatureReserveX#i :

llvm/test/MC/RISCV/option-autocompress.s renamed to llvm/test/MC/RISCV/option-exact-compression.s

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,13 @@
2121
# RUN: | FileCheck -check-prefixes=CHECK-BYTES,CHECK-INST %s
2222

2323

24-
# `.option (no)autocompress` enables and disables instruction compression in the
25-
# assembler, without changing the current architecture.
26-
#
27-
# The default is as if `.option autocompress` has been specified, that is, the
28-
# assembler compresses by default.
24+
## `.option exact` disables a variety of assembler behaviour:
25+
## - automatic compression
26+
## - branch relaxation (of short branches to longer equivalent sequences)
27+
## - linker relaxation (emitting R_RISCV_RELAX)
28+
## `.option noexact` enables these behaviours again. It is also the default.
29+
30+
## This test only checks the automatic compression part of this behaviour.
2931

3032
# CHECK-BYTES: 4108
3133
# CHECK-INST: c.lw a0, 0(a0)
@@ -39,8 +41,8 @@ lw a0, 0(a0)
3941
# CHECK: # encoding: [0x08,0x41]
4042
c.lw a0, 0(a0)
4143

42-
# CHECK: .option noautocompress
43-
.option noautocompress
44+
# CHECK: .option exact
45+
.option exact
4446

4547
# CHECK-BYTES: 00052503
4648
# CHECK-INST: lw a0, 0(a0)
@@ -54,8 +56,8 @@ lw a0, 0(a0)
5456
# CHECK: # encoding: [0x08,0x41]
5557
c.lw a0, 0(a0)
5658

57-
# CHECK: .option autocompress
58-
.option autocompress
59+
# CHECK: .option noexact
60+
.option noexact
5961

6062
# CHECK-BYTES: 4108
6163
# CHECK-INST: c.lw a0, 0(a0)
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
# RUN: llvm-mc -triple riscv32 -show-encoding -mattr=+relax %s \
2+
# RUN: | FileCheck -check-prefixes=CHECK-ASM %s
3+
# RUN: llvm-mc -triple riscv32 -filetype=obj -mattr=+relax %s \
4+
# RUN: | llvm-objdump --triple=riscv32 --no-show-raw-insn -dr - \
5+
# RUN: | FileCheck -check-prefixes=CHECK-OBJDUMP %s
6+
7+
# RUN: llvm-mc -triple riscv64 -show-encoding -mattr=+relax %s \
8+
# RUN: | FileCheck -check-prefixes=CHECK-ASM %s
9+
# RUN: llvm-mc -triple riscv64 -filetype=obj -mattr=+relax %s \
10+
# RUN: | llvm-objdump --triple=riscv64 --no-show-raw-insn -dr - \
11+
# RUN: | FileCheck -check-prefixes=CHECK-OBJDUMP %s
12+
13+
## `.option exact` disables a variety of assembler behaviour:
14+
## - automatic compression
15+
## - branch relaxation (of short branches to longer equivalent sequences)
16+
## - linker relaxation (emitting R_RISCV_RELAX)
17+
## `.option noexact` enables these behaviours again. It is also the default.
18+
19+
## This test only checks the branch and linker relaxation part of this behaviour.
20+
21+
22+
23+
# CHECK-ASM: call undefined
24+
# CHECK-ASM-NEXT: fixup A - offset: 0, value: undefined, kind: fixup_riscv_call_plt
25+
# CHECK-ASM-NEXT: fixup B - offset: 0, value: 0, kind: fixup_riscv_relax
26+
# CHECK-OBJDUMP: auipc ra, 0x0
27+
# CHECK-OBJDUMP-NEXT: R_RISCV_CALL_PLT undefined
28+
# CHECK-OBJDUMP-NEXT: R_RISCV_RELAX *ABS*
29+
# CHECK-OBJDUMP-NEXT: jalr ra
30+
call undefined@plt
31+
32+
# CHECK-ASM: beq a0, a1, undefined
33+
# CHECK-ASM-NEXT: fixup A - offset: 0, value: undefined, kind: fixup_riscv_branch
34+
# CHECK-OBJDUMP: bne a0, a1, 0x10
35+
# CHECK-OBJDUMP-NEXT: j 0xc
36+
# CHECK-OBJDUMP-NEXT: R_RISCV_JAL undefined
37+
beq a0, a1, undefined
38+
39+
# CHECK-ASM: .option exact
40+
.option exact
41+
42+
# CHECK-ASM: call undefined
43+
# CHECK-ASM-NEXT: fixup A - offset: 0, value: undefined, kind: fixup_riscv_call_plt
44+
# CHECK-ASM-NOT: fixup_riscv_relax
45+
# CHECK-OBJDUMP: auipc ra, 0x0
46+
# CHECK-OBJDUMP-NEXT: R_RISCV_CALL_PLT undefined
47+
# CHECK-OBJDUMP-NOT: R_RISCV_RELAX
48+
# CHECK-OBJDUMP-NEXT: jalr ra
49+
call undefined@plt
50+
51+
# CHECK-ASM: beq a0, a1, undefined
52+
# CHECK-ASM-NEXT: fixup A - offset: 0, value: undefined, kind: fixup_riscv_branch
53+
# CHECK-OBJDUMP: beq a0, a1, 0x18
54+
# CHECK-OBJDUMP-NEXT: R_RISCV_BRANCH undefined
55+
beq a0, a1, undefined
56+
57+
# CHECK-ASM: .option noexact
58+
.option noexact
59+
60+
# CHECK-ASM: call undefined
61+
# CHECK-ASM-NEXT: fixup A - offset: 0, value: undefined, kind: fixup_riscv_call_plt
62+
# CHECK-ASM-NEXT: fixup B - offset: 0, value: 0, kind: fixup_riscv_relax
63+
# CHECK-OBJDUMP: auipc ra, 0x0
64+
# CHECK-OBJDUMP-NEXT: R_RISCV_CALL_PLT undefined
65+
# CHECK-OBJDUMP-NEXT: R_RISCV_RELAX *ABS*
66+
# CHECK-OBJDUMP-NEXT: jalr ra
67+
call undefined@plt
68+
69+
# CHECK-ASM: beq a0, a1, undefined
70+
# CHECK-ASM-NEXT: fixup A - offset: 0, value: undefined, kind: fixup_riscv_branch
71+
# CHECK-OBJDUMP: bne a0, a1, 0x2c
72+
# CHECK-OBJDUMP-NEXT: j 0x28
73+
# CHECK-OBJDUMP-NEXT: R_RISCV_JAL undefined
74+
beq a0, a1, undefined

0 commit comments

Comments
 (0)