[llvm] 1d3baf2 - [LoongArch] Allow writing unaligned nop sequences

via llvm-commits llvm-commits at lists.llvm.org
Thu Dec 8 02:44:45 PST 2022


Author: wanglei
Date: 2022-12-08T18:44:28+08:00
New Revision: 1d3baf21b673db0c99a3560ec0653e63e0e4b01a

URL: https://github.com/llvm/llvm-project/commit/1d3baf21b673db0c99a3560ec0653e63e0e4b01a
DIFF: https://github.com/llvm/llvm-project/commit/1d3baf21b673db0c99a3560ec0653e63e0e4b01a.diff

LOG: [LoongArch] Allow writing unaligned nop sequences

In case of unaligned nop sequences, pad to the nearest 4-byte boundary
with zeros before filling with `nop` instructions. This is consistent
with gas behavior, and is necessary to compile the Linux kernel with
LLVM IAS.

Replace `support::endian::write` with `OS.write` while at it. This is
simpler and correct because we only have little endian.

Reviewed By: SixWeining, xen0n

Differential Revision: https://reviews.llvm.org/D139285

Added: 
    

Modified: 
    llvm/lib/Target/LoongArch/MCTargetDesc/LoongArchAsmBackend.cpp
    llvm/test/MC/LoongArch/Misc/unaligned-nops.s

Removed: 
    


################################################################################
diff  --git a/llvm/lib/Target/LoongArch/MCTargetDesc/LoongArchAsmBackend.cpp b/llvm/lib/Target/LoongArch/MCTargetDesc/LoongArchAsmBackend.cpp
index 6cadadf19a6f..ff0804e2a144 100644
--- a/llvm/lib/Target/LoongArch/MCTargetDesc/LoongArchAsmBackend.cpp
+++ b/llvm/lib/Target/LoongArch/MCTargetDesc/LoongArchAsmBackend.cpp
@@ -179,13 +179,14 @@ bool LoongArchAsmBackend::shouldForceRelocation(const MCAssembler &Asm,
 
 bool LoongArchAsmBackend::writeNopData(raw_ostream &OS, uint64_t Count,
                                        const MCSubtargetInfo *STI) const {
-  // Check for byte count not multiple of instruction word size
-  if (Count % 4 != 0)
-    return false;
+  // We mostly follow binutils' convention here: align to 4-byte boundary with a
+  // 0-fill padding.
+  OS.write_zeros(Count % 4);
 
-  // The nop on LoongArch is andi r0, r0, 0.
+  // The remainder is now padded with 4-byte nops.
+  // nop: andi r0, r0, 0
   for (; Count >= 4; Count -= 4)
-    support::endian::write<uint32_t>(OS, 0x03400000, support::little);
+    OS.write("\0\0\x40\x03", 4);
 
   return true;
 }

diff  --git a/llvm/test/MC/LoongArch/Misc/unaligned-nops.s b/llvm/test/MC/LoongArch/Misc/unaligned-nops.s
index 5952540b46d0..5a515dc9e9ce 100644
--- a/llvm/test/MC/LoongArch/Misc/unaligned-nops.s
+++ b/llvm/test/MC/LoongArch/Misc/unaligned-nops.s
@@ -1,5 +1,8 @@
-# RUN: not --crash llvm-mc --filetype=obj --triple=loongarch64 %s -o %t
+# RUN: llvm-mc --triple=loongarch64 --filetype=obj %s -o %t
+# RUN: llvm-objdump -d %t | FileCheck %s
+
+# CHECK:      01 00 00 00   <unknown>
+# CHECK-NEXT: 00 00 40 03   nop
 .byte 1
-# CHECK: LLVM ERROR: unable to write nop sequence of 3 bytes
-.p2align 2
+.p2align 3
 foo:


        


More information about the llvm-commits mailing list