diff --git a/llvm/lib/Target/LoongArch/MCTargetDesc/LoongArchAsmBackend.cpp b/llvm/lib/Target/LoongArch/MCTargetDesc/LoongArchAsmBackend.cpp --- a/llvm/lib/Target/LoongArch/MCTargetDesc/LoongArchAsmBackend.cpp +++ b/llvm/lib/Target/LoongArch/MCTargetDesc/LoongArchAsmBackend.cpp @@ -179,13 +179,14 @@ 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(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 --- 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 +# 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: