[PATCH] D125905: [RISCV] Fix state persistence bugs (PR55548)

Kito Cheng via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Wed May 18 09:52:40 PDT 2022


kito-cheng created this revision.
Herald added subscribers: sunshaoce, VincentWu, luke957, vkmr, frasercrmck, evandro, luismarques, apazos, sameer.abuasal, s.egerton, Jim, benna, psnobl, jocewei, PkmX, the_o, brucehoult, MartinMosbeck, rogfer01, edward-jones, zzheng, jrtc27, shiva0217, niosHD, sabuasal, simoncook, johnrusso, rbar, asb, hiraditya, kristof.beyls, arichardson.
Herald added a project: All.
kito-cheng requested review of this revision.
Herald added subscribers: llvm-commits, pcwang-thead, eopXD, MaskRay.
Herald added a project: LLVM.

We didn't implement RISCVELFStreamer::reset and cause some very strange
section output for attribute section...just reference D15950 <https://reviews.llvm.org/D15950> to see how
ARM implement that.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D125905

Files:
  llvm/lib/Target/RISCV/MCTargetDesc/RISCVELFStreamer.cpp
  llvm/lib/Target/RISCV/MCTargetDesc/RISCVELFStreamer.h
  llvm/lib/Target/RISCV/MCTargetDesc/RISCVTargetStreamer.cpp
  llvm/lib/Target/RISCV/MCTargetDesc/RISCVTargetStreamer.h
  llvm/test/MC/RISCV/twice.ll


Index: llvm/test/MC/RISCV/twice.ll
===================================================================
--- /dev/null
+++ llvm/test/MC/RISCV/twice.ll
@@ -0,0 +1,21 @@
+; Check for state persistence bugs in the RISC-V MC backend
+; This should neither fail (in the comparison that the second object
+; is bit-identical to the first) nor crash. Either failure would most
+; likely indicate some state that is not properly reset in the
+; appropriate ::reset method.
+; RUN: llc -compile-twice -filetype=obj %s -o - \
+; RUN:     | llvm-objdump --section-headers - \
+; RUN:     | FileCheck %s
+
+
+; CHECK: Sections:
+; CHECK-NEXT: Idx Name              Size     VMA              Type
+; CHECK-NEXT:  0
+; CHECK-NEXT:  1 .strtab
+; CHECK-NEXT:  2 .text
+; CHECK-NEXT:  3 .note.GNU-stack
+; CHECK-NEXT:  4 .riscv.attributes
+; CHECK-NEXT:  5 .symtab
+
+target datalayout = "e-m:e-p:64:64-i64:64-i128:128-n64-S128"
+target triple = "riscv64-unknown-linux-gnu"
Index: llvm/lib/Target/RISCV/MCTargetDesc/RISCVTargetStreamer.h
===================================================================
--- llvm/lib/Target/RISCV/MCTargetDesc/RISCVTargetStreamer.h
+++ llvm/lib/Target/RISCV/MCTargetDesc/RISCVTargetStreamer.h
@@ -23,6 +23,7 @@
 public:
   RISCVTargetStreamer(MCStreamer &S);
   void finish() override;
+  virtual void reset();
 
   virtual void emitDirectiveOptionPush();
   virtual void emitDirectiveOptionPop();
Index: llvm/lib/Target/RISCV/MCTargetDesc/RISCVTargetStreamer.cpp
===================================================================
--- llvm/lib/Target/RISCV/MCTargetDesc/RISCVTargetStreamer.cpp
+++ llvm/lib/Target/RISCV/MCTargetDesc/RISCVTargetStreamer.cpp
@@ -22,6 +22,7 @@
 RISCVTargetStreamer::RISCVTargetStreamer(MCStreamer &S) : MCTargetStreamer(S) {}
 
 void RISCVTargetStreamer::finish() { finishAttributeSection(); }
+void RISCVTargetStreamer::reset() {}
 
 void RISCVTargetStreamer::emitDirectiveOptionPush() {}
 void RISCVTargetStreamer::emitDirectiveOptionPop() {}
Index: llvm/lib/Target/RISCV/MCTargetDesc/RISCVELFStreamer.h
===================================================================
--- llvm/lib/Target/RISCV/MCTargetDesc/RISCVELFStreamer.h
+++ llvm/lib/Target/RISCV/MCTargetDesc/RISCVELFStreamer.h
@@ -92,6 +92,8 @@
   void finishAttributeSection() override;
   size_t calculateContentSize() const;
 
+  void reset() override;
+
 public:
   MCELFStreamer &getStreamer();
   RISCVTargetELFStreamer(MCStreamer &S, const MCSubtargetInfo &STI);
Index: llvm/lib/Target/RISCV/MCTargetDesc/RISCVELFStreamer.cpp
===================================================================
--- llvm/lib/Target/RISCV/MCTargetDesc/RISCVELFStreamer.cpp
+++ llvm/lib/Target/RISCV/MCTargetDesc/RISCVELFStreamer.cpp
@@ -180,6 +180,11 @@
   MCA.setELFHeaderEFlags(EFlags);
 }
 
+void RISCVTargetELFStreamer::reset() {
+  AttributeSection = nullptr;
+  Contents.clear();
+}
+
 namespace {
 class RISCVELFStreamer : public MCELFStreamer {
   static std::pair<unsigned, unsigned> getRelocPairForSize(unsigned Size) {
@@ -226,6 +231,14 @@
                             : !B.getName().empty());
   }
 
+  // Reset any state
+  void reset() override {
+    MCTargetStreamer &TS = *getTargetStreamer();
+    RISCVTargetStreamer &RTS = static_cast<RISCVTargetStreamer &>(TS);
+    RTS.reset();
+    MCELFStreamer::reset();
+  }
+
 public:
   RISCVELFStreamer(MCContext &C, std::unique_ptr<MCAsmBackend> MAB,
                    std::unique_ptr<MCObjectWriter> MOW,


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D125905.430424.patch
Type: text/x-patch
Size: 3471 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20220518/46e100d1/attachment.bin>


More information about the llvm-commits mailing list