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

via llvm-commits llvm-commits at lists.llvm.org
Wed Sep 27 04:25:46 PDT 2023


llvmbot wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-backend-risc-v

<details>
<summary>Changes</summary>

$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

Adapted from https://reviews.llvm.org/D137417

Note this does cause GNU binutils to output a warning about an unknown ISA suffixed with ".n". I've submitted a patch to binutils to avoid this, as it is a legal mapping symbol.

---
Full diff: https://github.com/llvm/llvm-project/pull/67541.diff


3 Files Affected:

- (modified) llvm/lib/Target/RISCV/MCTargetDesc/RISCVELFStreamer.cpp (+15-1) 
- (modified) llvm/lib/Target/RISCV/MCTargetDesc/RISCVELFStreamer.h (+4-1) 
- (added) llvm/test/MC/RISCV/mapping-isa.s (+47) 


``````````diff
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
+

``````````

</details>


https://github.com/llvm/llvm-project/pull/67541


More information about the llvm-commits mailing list