[llvm] [RISCV][MC] Implement ISA mapping symbols (PR #67541)

Joe Faulls via llvm-commits llvm-commits at lists.llvm.org
Thu Sep 28 09:07:21 PDT 2023


https://github.com/joe-img updated https://github.com/llvm/llvm-project/pull/67541

>From f948bd7a7125314a9bf2138ae827bb2ea5f79cb6 Mon Sep 17 00:00:00 2001
From: "Joseph.Faulls" <Joseph.Faulls at imgtec.com>
Date: Tue, 26 Sep 2023 16:49:00 +0100
Subject: [PATCH 1/2] [RISCV][MC] Implement ISA mapping symbols

$x<ISA> indicates the start of a sequence of instructions with <ISA>
extension [1].

[1]: https://github.com/riscv-non-isa/riscv-elf-psabi-doc/blob/master/riscv-elf.adoc#mapping-symbol
---
 .../RISCV/MCTargetDesc/RISCVELFStreamer.cpp   | 16 ++++++-
 .../RISCV/MCTargetDesc/RISCVELFStreamer.h     |  5 +-
 llvm/test/MC/RISCV/mapping-isa.s              | 47 +++++++++++++++++++
 3 files changed, 66 insertions(+), 2 deletions(-)
 create mode 100644 llvm/test/MC/RISCV/mapping-isa.s

diff --git a/llvm/lib/Target/RISCV/MCTargetDesc/RISCVELFStreamer.cpp b/llvm/lib/Target/RISCV/MCTargetDesc/RISCVELFStreamer.cpp
index 9db5148208b3ec0..38c0c759bc53b80 100644
--- a/llvm/lib/Target/RISCV/MCTargetDesc/RISCVELFStreamer.cpp
+++ b/llvm/lib/Target/RISCV/MCTargetDesc/RISCVELFStreamer.cpp
@@ -59,6 +59,7 @@ void RISCVTargetELFStreamer::emitAttribute(unsigned Attribute, unsigned Value) {
 void RISCVTargetELFStreamer::emitTextAttribute(unsigned Attribute,
                                                StringRef String) {
   getStreamer().setAttributeItem(Attribute, String, /*OverwriteExisting=*/true);
+  getStreamer().changeISAMappingSymbol(Attribute, String);
 }
 
 void RISCVTargetELFStreamer::emitIntTextAttribute(unsigned Attribute,
@@ -140,7 +141,10 @@ void RISCVELFStreamer::emitDataMappingSymbol() {
 void RISCVELFStreamer::emitInstructionsMappingSymbol() {
   if (LastEMS == EMS_Instructions)
     return;
-  emitMappingSymbol("$x");
+  if (LastEMS == EMS_ChangeISA)
+    emitMappingSymbol("$x" + ISAString);
+  else
+    emitMappingSymbol("$x");
   LastEMS = EMS_Instructions;
 }
 
@@ -152,6 +156,16 @@ void RISCVELFStreamer::emitMappingSymbol(StringRef Name) {
   Symbol->setBinding(ELF::STB_LOCAL);
 }
 
+void RISCVELFStreamer::changeISAMappingSymbol(unsigned Attribute,
+                                              StringRef arch) {
+  if (Attribute != RISCVAttrs::ARCH)
+    return;
+  if (arch != ISAString) {
+    LastEMS = EMS_ChangeISA;
+    ISAString = std::string(arch);
+  }
+}
+
 void RISCVELFStreamer::changeSection(MCSection *Section,
                                      const MCExpr *Subsection) {
   // We have to keep track of the mapping symbol state of any sections we
diff --git a/llvm/lib/Target/RISCV/MCTargetDesc/RISCVELFStreamer.h b/llvm/lib/Target/RISCV/MCTargetDesc/RISCVELFStreamer.h
index a6f54bf67b5d2bb..88fb5b235388efa 100644
--- a/llvm/lib/Target/RISCV/MCTargetDesc/RISCVELFStreamer.h
+++ b/llvm/lib/Target/RISCV/MCTargetDesc/RISCVELFStreamer.h
@@ -20,11 +20,13 @@ class RISCVELFStreamer : public MCELFStreamer {
   void emitInstructionsMappingSymbol();
   void emitMappingSymbol(StringRef Name);
 
-  enum ElfMappingSymbol { EMS_None, EMS_Instructions, EMS_Data };
+  enum ElfMappingSymbol { EMS_None, EMS_ChangeISA, EMS_Instructions,
+    EMS_Data };
 
   int64_t MappingSymbolCounter = 0;
   DenseMap<const MCSection *, ElfMappingSymbol> LastMappingSymbols;
   ElfMappingSymbol LastEMS = EMS_None;
+  std::string ISAString;
 
 public:
   RISCVELFStreamer(MCContext &C, std::unique_ptr<MCAsmBackend> MAB,
@@ -37,6 +39,7 @@ class RISCVELFStreamer : public MCELFStreamer {
   void emitBytes(StringRef Data) override;
   void emitFill(const MCExpr &NumBytes, uint64_t FillValue, SMLoc Loc) override;
   void emitValueImpl(const MCExpr *Value, unsigned Size, SMLoc Loc) override;
+  void changeISAMappingSymbol(unsigned Attribute, StringRef arch);
 };
 
 namespace llvm {
diff --git a/llvm/test/MC/RISCV/mapping-isa.s b/llvm/test/MC/RISCV/mapping-isa.s
new file mode 100644
index 000000000000000..737746e889ffb52
--- /dev/null
+++ b/llvm/test/MC/RISCV/mapping-isa.s
@@ -0,0 +1,47 @@
+## Instruction mapping symbols with ISA string
+
+# RUN: llvm-mc -triple=riscv32 -filetype=obj -o %t.o %s
+# RUN: llvm-objdump -t %t.o | FileCheck %s -check-prefix=CHECK-MAPPINGSYMBOLS
+# RUN: llvm-mc -triple=riscv64 -filetype=obj -o %t.o %s
+# RUN: llvm-objdump -t %t.o | FileCheck %s -check-prefix=CHECK-MAPPINGSYMBOLS
+
+.text
+.attribute arch, "rv32i"
+nop
+# CHECK-MAPPINGSYMBOLS: $xrv32i2p1
+
+.attribute arch, "rv32i"
+nop
+nop
+# CHECK-MAPPINGSYMBOLS-NOT: $xrv32i2p1
+## Multiple instructions with same isa string, so no mapping symbol expected.
+
+.word 4
+nop
+# CHECK-MAPPINGSYMBOLS-NOT: $xrv32i2p1
+# CHECK-MAPPINGSYMBOLS: $x
+## Data followed by an instruction should produce an instruction mapping
+## symbol, but the isa string should not be present.
+
+.attribute arch, "rv32i2p1"
+nop
+# CHECK-MAPPINGSYMBOLS-NOT: $xrv32i2p1
+## The arch "rv32i" and "rv32i2p1" has the same isa string, so no mapping
+## symbol expected.
+
+.attribute arch, "rv32e"
+nop
+# CHECK-MAPPINGSYMBOLS: $xrv32e2p0
+
+.attribute arch, "rv64e"
+nop
+# CHECK-MAPPINGSYMBOLS: $xrv64e2p0
+
+.attribute arch, "rv32g"
+nop
+# CHECK-MAPPINGSYMBOLS: $xrv32i2p1_m2p0_a2p1_f2p2_d2p2_zicsr2p0_zifencei2p0
+
+.attribute arch, "rv64g"
+nop
+# CHECK-MAPPINGSYMBOLS: $xrv64i2p1_m2p0_a2p1_f2p2_d2p2_zicsr2p0_zifencei2p0
+

>From 393f5681a42181daccfe6fc812759715bb4ef113 Mon Sep 17 00:00:00 2001
From: "Joseph.Faulls" <Joseph.Faulls at imgtec.com>
Date: Thu, 28 Sep 2023 17:06:02 +0100
Subject: [PATCH 2/2] Fix formatting

---
 llvm/lib/Target/RISCV/MCTargetDesc/RISCVELFStreamer.h | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)

diff --git a/llvm/lib/Target/RISCV/MCTargetDesc/RISCVELFStreamer.h b/llvm/lib/Target/RISCV/MCTargetDesc/RISCVELFStreamer.h
index 88fb5b235388efa..46b56298b7885f8 100644
--- a/llvm/lib/Target/RISCV/MCTargetDesc/RISCVELFStreamer.h
+++ b/llvm/lib/Target/RISCV/MCTargetDesc/RISCVELFStreamer.h
@@ -20,8 +20,7 @@ class RISCVELFStreamer : public MCELFStreamer {
   void emitInstructionsMappingSymbol();
   void emitMappingSymbol(StringRef Name);
 
-  enum ElfMappingSymbol { EMS_None, EMS_ChangeISA, EMS_Instructions,
-    EMS_Data };
+  enum ElfMappingSymbol { EMS_None, EMS_ChangeISA, EMS_Instructions, EMS_Data };
 
   int64_t MappingSymbolCounter = 0;
   DenseMap<const MCSection *, ElfMappingSymbol> LastMappingSymbols;



More information about the llvm-commits mailing list