[PATCH] D132482: RISCV: permit unaligned nop-slide padding emission
Saleem Abdulrasool via Phabricator via llvm-commits
llvm-commits at lists.llvm.org
Tue Aug 23 14:36:28 PDT 2022
compnerd updated this revision to Diff 454965.
compnerd added a comment.
add context, address comments from @luismarques
CHANGES SINCE LAST ACTION
https://reviews.llvm.org/D132482/new/
https://reviews.llvm.org/D132482
Files:
llvm/lib/Target/RISCV/MCTargetDesc/RISCVAsmBackend.cpp
llvm/test/MC/RISCV/align.s
llvm/test/MC/RISCV/nop-slide.s
Index: llvm/test/MC/RISCV/nop-slide.s
===================================================================
--- /dev/null
+++ llvm/test/MC/RISCV/nop-slide.s
@@ -0,0 +1,27 @@
+# RUN: llvm-mc -triple riscv64 -mattr +c,-relax -filetype obj -o - %s | llvm-objdump -d - | FileCheck %s -check-prefix CHECK-RVC-NORELAX
+# RUN: llvm-mc -triple riscv64 -mattr +c,+relax -filetype obj -o - %s | llvm-objdump -d - | FileCheck %s -check-prefix CHECK-RVC-RELAX
+# RUN: llvm-mc -triple riscv64 -mattr -c,-relax -filetype obj -o - %s | llvm-objdump -d - | FileCheck %s
+# RUN: llvm-mc -triple riscv64 -mattr -c,+relax -filetype obj -o - %s | llvm-objdump -d - | FileCheck %s
+
+.balign 4
+.byte 0
+
+.balign 4
+auipc a0, 0
+
+# CHECK-RVC-NORELAX: 0000000000000000 <.text>:
+# CHECK-RVC-NORELAX-NEXT: 0: 00 00 unimp
+# CHECK-RVC-NORELAX-NEXT: 2: 01 00 nop
+# CHECK-RVC-NORELAX-NEXT: 4: 17 05 00 00 auipc a0, 0
+
+# CHECK-RVC-RELAX: 0000000000000000 <.text>:
+# CHECK-RVC-RELAX-NEXT: 0: 01 00 nop
+# CHECK-RVC-RELAX-NEXT: 2: 00 01 addi s0, sp, 128
+# CHECK-RVC-RELAX-NEXT: 4: 00 17 addi s0, sp, 928
+# CHECK-RVC-RELAX-NEXT: 6: 05 00 c.nop 1
+# CHECK-RVC-RELAX-NEXT: 8: 00 <unknown>
+
+# CHECK: 0000000000000000 <.text>:
+# CHECK-NEXT: 0: 00 00 <unknown>
+# CHECK-NEXT: 2: 00 00 <unknown>
+# CHECK-NEXT: 4: 17 05 00 00 auipc a0, 0
Index: llvm/test/MC/RISCV/align.s
===================================================================
--- llvm/test/MC/RISCV/align.s
+++ llvm/test/MC/RISCV/align.s
@@ -41,7 +41,7 @@
# If the +c extension is enabled, the text section will be 2-byte aligned, so
# one c.nop instruction is sufficient.
# C-EXT-RELAX-RELOC-NOT: R_RISCV_ALIGN - 0x2
-# C-EXT-RELAX-INST-NOT: c.nop
+# C-EXT-RELAX-INST: c.nop
bne zero, a0, .LBB0_2
mv a0, zero
.p2align 3
@@ -75,7 +75,7 @@
# NORELAX-INST: addi zero, zero, 0
# C-EXT-RELAX-RELOC: R_RISCV_ALIGN - 0x6
# C-EXT-RELAX-INST: addi zero, zero, 0
-# C-EXT-RELAX-INST: c.nop
+# C-EXT-RELAX-INST-NOT: c.nop
# C-EXT-INST: addi zero, zero, 0
# C-EXT-INST: c.nop
add a0, a0, a1
Index: llvm/lib/Target/RISCV/MCTargetDesc/RISCVAsmBackend.cpp
===================================================================
--- llvm/lib/Target/RISCV/MCTargetDesc/RISCVAsmBackend.cpp
+++ llvm/lib/Target/RISCV/MCTargetDesc/RISCVAsmBackend.cpp
@@ -354,20 +354,28 @@
bool RISCVAsmBackend::writeNopData(raw_ostream &OS, uint64_t Count,
const MCSubtargetInfo *STI) const {
- bool HasStdExtC = STI->getFeatureBits()[RISCV::FeatureStdExtC];
- unsigned MinNopLen = HasStdExtC ? 2 : 4;
+ // We mostly follow binutils' convention here: align to even boundary with a
+ // 0-fill padding. We emit up to 1 2-byte nop, though we use c.nop if RVC is
+ // enabled or 0-fill otherwise. The remainder is now padded with 4-byte nops.
+
+ // Instructions always are at even addresses. We must be in a data area or
+ // be unaligned due to some other reason.
+ if (Count % 2) {
+ OS.write("\0", 1);
+ Count -= 1;
+ }
- if ((Count % MinNopLen) != 0)
- return false;
+ // The canonical nop on RVC is c.nop.
+ if (Count % 4 == 2) {
+ OS.write(STI->getFeatureBits()[RISCV::FeatureStdExtC] ? "\x01\0" : "\0\0",
+ 2);
+ Count -= 2;
+ }
// The canonical nop on RISC-V is addi x0, x0, 0.
for (; Count >= 4; Count -= 4)
OS.write("\x13\0\0\0", 4);
- // The canonical nop on RVC is c.nop.
- if (Count && HasStdExtC)
- OS.write("\x01\0", 2);
-
return true;
}
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D132482.454965.patch
Type: text/x-patch
Size: 3582 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20220823/66447e64/attachment-0001.bin>
More information about the llvm-commits
mailing list