Skip to content

Commit a24dfda

Browse files
committed
Remove RISCVAsmBackend::ForceRelocs
RISCVAsmBackend::ForceRelocs is a workaround shouldForceRelocation Follow-up to llvm#140494
1 parent 95202ab commit a24dfda

File tree

9 files changed

+31
-35
lines changed

9 files changed

+31
-35
lines changed

llvm/include/llvm/MC/MCAssembler.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ class MCAssembler {
6464
std::unique_ptr<MCObjectWriter> Writer;
6565

6666
bool HasLayout = false;
67+
bool HasRelaxed = false;
6768
bool RelaxAll = false;
6869

6970
SectionListType Sections;
@@ -197,6 +198,7 @@ class MCAssembler {
197198
void layout();
198199

199200
bool hasLayout() const { return HasLayout; }
201+
bool hasRelaxed() const { return HasRelaxed; }
200202
bool getRelaxAll() const { return RelaxAll; }
201203
void setRelaxAll(bool Value) { RelaxAll = Value; }
202204

llvm/lib/MC/MCAssembler.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -897,6 +897,8 @@ void MCAssembler::layout() {
897897
// example, to set the index fields in the symbol data).
898898
getWriter().executePostLayoutBinding(*this);
899899

900+
this->HasRelaxed = true;
901+
900902
// Evaluate and apply the fixups, generating relocation entries as necessary.
901903
for (MCSection &Sec : *this) {
902904
for (MCFragment &Frag : Sec) {

llvm/lib/MC/MCExpr.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -390,6 +390,9 @@ static void attemptToFoldSymbolOffsetDifference(const MCAssembler *Asm,
390390
unsigned Count;
391391
if (DF) {
392392
Displacement += DF->getContents().size();
393+
} else if (auto *RF = dyn_cast<MCRelaxableFragment>(FI);
394+
RF && Asm->hasRelaxed()) {
395+
Displacement += RF->getContents().size();
393396
} else if (auto *AF = dyn_cast<MCAlignFragment>(FI);
394397
AF && Layout && AF->hasEmitNops() &&
395398
!Asm->getBackend().shouldInsertExtraNopBytesForCodeAlign(

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

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2820,21 +2820,6 @@ bool RISCVAsmParser::parseOperand(OperandVector &Operands, StringRef Mnemonic) {
28202820
bool RISCVAsmParser::parseInstruction(ParseInstructionInfo &Info,
28212821
StringRef Name, SMLoc NameLoc,
28222822
OperandVector &Operands) {
2823-
// Ensure that if the instruction occurs when relaxation is enabled,
2824-
// relocations are forced for the file. Ideally this would be done when there
2825-
// is enough information to reliably determine if the instruction itself may
2826-
// cause relaxations. Unfortunately instruction processing stage occurs in the
2827-
// same pass as relocation emission, so it's too late to set a 'sticky bit'
2828-
// for the entire file.
2829-
if (getSTI().hasFeature(RISCV::FeatureRelax)) {
2830-
auto *Assembler = getTargetStreamer().getStreamer().getAssemblerPtr();
2831-
if (Assembler != nullptr) {
2832-
RISCVAsmBackend &MAB =
2833-
static_cast<RISCVAsmBackend &>(Assembler->getBackend());
2834-
MAB.setForceRelocs();
2835-
}
2836-
}
2837-
28382823
// Apply mnemonic aliases because the destination mnemonic may have require
28392824
// custom operand parsing. The generic tblgen'erated code does this later, at
28402825
// the start of MatchInstructionImpl(), but that's too late for custom

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

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ bool RISCVAsmBackend::shouldForceRelocation(const MCAssembler &Asm,
124124
break;
125125
}
126126

127-
return STI->hasFeature(RISCV::FeatureRelax) || ForceRelocs;
127+
return STI->hasFeature(RISCV::FeatureRelax);
128128
}
129129

130130
bool RISCVAsmBackend::fixupNeedsRelaxationAdvanced(const MCAssembler &,
@@ -659,11 +659,23 @@ bool RISCVAsmBackend::addReloc(MCAssembler &Asm, const MCFragment &F,
659659
return false;
660660
}
661661

662+
if (IsResolved && (getFixupKindInfo(Fixup.getKind()).Flags &
663+
MCFixupKindInfo::FKF_IsPCRel)) {
664+
if (!PCRelTemp)
665+
PCRelTemp = Asm.getContext().createTempSymbol();
666+
PCRelTemp->setFragment(const_cast<MCFragment *>(&F));
667+
PCRelTemp->setOffset(Fixup.getOffset());
668+
MCValue Res;
669+
MCExpr::evaluateSymbolicAdd(&Asm, false, MCValue::get(Target.getAddSym()),
670+
MCValue::get(nullptr, PCRelTemp), Res);
671+
if (Res.getSubSym())
672+
IsResolved = false;
673+
}
662674
IsResolved = MCAsmBackend::addReloc(Asm, F, Fixup, Target, FixedValue,
663675
IsResolved, STI);
664676
// If linker relaxation is enabled and supported by the current relocation,
665677
// append a RELAX relocation.
666-
if (Fixup.needsRelax()) {
678+
if (!IsResolved && Fixup.needsRelax()) {
667679
auto FA = MCFixup::create(Fixup.getOffset(), nullptr, ELF::R_RISCV_RELAX);
668680
Asm.getWriter().recordRelocation(Asm, &F, FA, MCValue::get(nullptr),
669681
FixedValueA);

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

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,16 +25,15 @@ class RISCVAsmBackend : public MCAsmBackend {
2525
const MCSubtargetInfo &STI;
2626
uint8_t OSABI;
2727
bool Is64Bit;
28-
bool ForceRelocs = false;
2928
const MCTargetOptions &TargetOptions;
29+
// Temporary symbol used to check whether a PC-relative fixup is resolved.
30+
MCSymbol *PCRelTemp = nullptr;
3031

3132
public:
3233
RISCVAsmBackend(const MCSubtargetInfo &STI, uint8_t OSABI, bool Is64Bit,
3334
const MCTargetOptions &Options);
3435
~RISCVAsmBackend() override = default;
3536

36-
void setForceRelocs() { ForceRelocs = true; }
37-
3837
// Return Size with extra Nop Bytes for alignment directive in code section.
3938
bool shouldInsertExtraNopBytesForCodeAlign(const MCAlignFragment &AF,
4039
unsigned &Size) override;

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

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -34,13 +34,6 @@ RISCVTargetELFStreamer::RISCVTargetELFStreamer(MCStreamer &S,
3434
setTargetABI(RISCVABI::computeTargetABI(STI.getTargetTriple(), Features,
3535
MAB.getTargetOptions().getABIName()));
3636
setFlagsFromFeatures(STI);
37-
// `j label` in `.option norelax; j label; .option relax; ...; label:` needs a
38-
// relocation to ensure the jump target is correct after linking. This is due
39-
// to a limitation that shouldForceRelocation has to make the decision upfront
40-
// without knowing a possibly future .option relax. When RISCVAsmParser is used,
41-
// its ParseInstruction may call setForceRelocs as well.
42-
if (STI.hasFeature(RISCV::FeatureRelax))
43-
static_cast<RISCVAsmBackend &>(MAB).setForceRelocs();
4437
}
4538

4639
RISCVELFStreamer &RISCVTargetELFStreamer::getStreamer() {

llvm/test/CodeGen/RISCV/option-relax-relocation.ll

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
;; after linker relaxation. See https://github.com/ClangBuiltLinux/linux/issues/1965
33

44
; RUN: llc -mtriple=riscv64 -mattr=-relax -filetype=obj < %s \
5-
; RUN: | llvm-objdump -d -r - | FileCheck %s
5+
; RUN: | llvm-objdump -d -r - | FileCheck %s --check-prefixes=CHECK,NORELAX
66
; RUN: llc -mtriple=riscv64 -mattr=+relax -filetype=obj < %s \
77
; RUN: | llvm-objdump -d -r - | FileCheck %s --check-prefixes=CHECK,RELAX
88

@@ -12,14 +12,20 @@
1212
; CHECK-NEXT: R_RISCV_CALL_PLT f
1313
; RELAX-NEXT: R_RISCV_RELAX *ABS*
1414
; CHECK-NEXT: jalr ra
15+
; CHECK-NEXT: j {{.*}}
16+
; CHECK-NEXT: j {{.*}}
17+
; NORELAX-NEXT: li a0, 0x0
18+
; RELAX-NEXT: R_RISCV_JAL .L0
1519

1620
define dso_local noundef signext i32 @main() local_unnamed_addr #0 {
1721
entry:
18-
callbr void asm sideeffect ".option push\0A.option norvc\0A.option norelax\0Aj $0\0A.option pop\0A", "!i"() #2
22+
callbr void asm sideeffect ".option push\0A.option norvc\0A.option norelax\0Aj $0\0A.option pop\0A", "!i"()
1923
to label %asm.fallthrough [label %label]
2024

2125
asm.fallthrough: ; preds = %entry
2226
tail call void @f()
27+
callbr void asm sideeffect ".option push\0A.option norvc\0A.option norelax\0Aj $0\0A.option pop\0A", "!i"()
28+
to label %asm.fallthrough [label %label]
2329
br label %label
2430

2531
label: ; preds = %asm.fallthrough, %entry

llvm/test/MC/RISCV/option-relax.s

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,6 @@
2424
# CHECK-RELOC-NOT: R_RISCV_RELAX - 0x0
2525
call foo
2626

27-
# CHECK-RELOC-NEXT: R_RISCV_ADD64
28-
# CHECK-RELOC-NEXT: R_RISCV_SUB64
2927
.dword .L2-.L1
3028
# CHECK-RELOC-NEXT: R_RISCV_JAL
3129
jal zero, .L1
@@ -41,8 +39,6 @@ beq s1, s1, .L1
4139
# CHECK-RELOC-NEXT: R_RISCV_RELAX - 0x0
4240
call bar
4341

44-
# CHECK-RELOC-NEXT: R_RISCV_ADD64
45-
# CHECK-RELOC-NEXT: R_RISCV_SUB64
4642
.dword .L2-.L1
4743
# CHECK-RELOC-NEXT: R_RISCV_JAL
4844
jal zero, .L1
@@ -57,8 +53,6 @@ beq s1, s1, .L1
5753
# CHECK-RELOC-NOT: R_RISCV_RELAX - 0x0
5854
call baz
5955

60-
# CHECK-RELOC-NEXT: R_RISCV_ADD64
61-
# CHECK-RELOC-NEXT: R_RISCV_SUB64
6256
.dword .L2-.L1
6357
# CHECK-RELOC-NEXT: R_RISCV_JAL
6458
jal zero, .L1

0 commit comments

Comments
 (0)