[llvm-branch-commits] [lld] [RISCV][lld] Support merging RISC-V Atomics ABI attributes (PR #97347)

Paul Kirth via llvm-branch-commits llvm-branch-commits at lists.llvm.org
Tue Jul 2 09:39:58 PDT 2024


https://github.com/ilovepi updated https://github.com/llvm/llvm-project/pull/97347

>From 5c57d12517ad58310511eb27bf7cc4ec660d4fa7 Mon Sep 17 00:00:00 2001
From: Paul Kirth <paulkirth at google.com>
Date: Tue, 2 Jul 2024 09:39:46 -0700
Subject: [PATCH] Add handling for invalid tags

Created using spr 1.3.4
---
 lld/ELF/Arch/RISCV.cpp          | 22 ++++++++++++++++++++--
 lld/test/ELF/riscv-attributes.s |  9 +++++++++
 2 files changed, 29 insertions(+), 2 deletions(-)

diff --git a/lld/ELF/Arch/RISCV.cpp b/lld/ELF/Arch/RISCV.cpp
index c48b9f7c11047..07a1b63be8051 100644
--- a/lld/ELF/Arch/RISCV.cpp
+++ b/lld/ELF/Arch/RISCV.cpp
@@ -1102,7 +1102,20 @@ static void mergeAtomic(DenseMap<unsigned, unsigned>::iterator it,
                 ": atomic_abi=" + Twine(static_cast<unsigned>(newTag)));
   };
 
-  switch (static_cast<RISCVAtomicAbiTag>(oldTag)) {
+  auto reportUnknownAbiError = [](const InputSectionBase *section,
+                                  RISCVAtomicAbiTag tag) {
+    switch (tag) {
+    case RISCVAtomicAbiTag::UNKNOWN:
+    case RISCVAtomicAbiTag::A6C:
+    case RISCVAtomicAbiTag::A6S:
+    case RISCVAtomicAbiTag::A7:
+      return;
+    };
+    errorOrWarn("unknown atomic abi for " + section->name + "\n>>> " +
+                toString(section) +
+                ": atomic_abi=" + Twine(static_cast<unsigned>(tag)));
+  };
+  switch (oldTag) {
   case RISCVAtomicAbiTag::UNKNOWN:
     it->getSecond() = static_cast<unsigned>(newTag);
     return;
@@ -1145,7 +1158,12 @@ static void mergeAtomic(DenseMap<unsigned, unsigned>::iterator it,
       return;
     };
   };
-  llvm_unreachable("unknown AtomicABI");
+
+  // If we get here, then we have an invalid tag, so report it.
+  // Putting these checks at the end allows us to only do these checks when we
+  // need to, since this is expected to be a rare occurrence.
+  reportUnknownAbiError(oldSection, oldTag);
+  reportUnknownAbiError(newSection, newTag);
 }
 
 static RISCVAttributesSection *
diff --git a/lld/test/ELF/riscv-attributes.s b/lld/test/ELF/riscv-attributes.s
index 38b0fe8e7797e..057223c18418e 100644
--- a/lld/test/ELF/riscv-attributes.s
+++ b/lld/test/ELF/riscv-attributes.s
@@ -52,6 +52,12 @@
 # ATOMIC_ABI_ERROR-NEXT: >>> atomic_abi_A6C.o:(.riscv.attributes): atomic_abi=1
 # ATOMIC_ABI_ERROR-NEXT: >>> atomic_abi_A7.o:(.riscv.attributes): atomic_abi=3
 
+## RISC-V tag merging for atomic_abi values A6C and invalid lead to an error.
+# RUN: llvm-mc -filetype=obj -triple=riscv64  atomic_abi_invalid.s -o atomic_abi_invalid.o
+# RUN: not ld.lld atomic_abi_A6C.o atomic_abi_invalid.o -o /dev/null 2>&1 | FileCheck %s --check-prefix=ATOMIC_ABI_INVALID --implicit-check-not=error:
+# ATOMIC_ABI_INVALID: error: unknown atomic abi for .riscv.attributes
+# ATOMIC_ABI_INVALID-NEXT: >>> atomic_abi_invalid.o:(.riscv.attributes): atomic_abi=42
+
 # RUN: llvm-mc -filetype=obj -triple=riscv64  atomic_abi_A6S.s -o atomic_abi_A6S.o
 # RUN: ld.lld atomic_abi_A6S.o atomic_abi_A6C.o -o atomic_abi_A6C_A6S
 # RUN: llvm-readobj -A atomic_abi_A6C_A6S | FileCheck %s --check-prefix=A6C_A6S
@@ -332,6 +338,9 @@
 #--- atomic_abi_A7.s
 .attribute atomic_abi, 3
 
+#--- atomic_abi_invalid.s
+.attribute atomic_abi, 42
+
 #      UNKNOWN_NONE: BuildAttributes {
 # UNKNOWN_NONE-NEXT:   FormatVersion: 0x41
 # UNKNOWN_NONE-NEXT:   Section 1 {



More information about the llvm-branch-commits mailing list