[llvm] [MC][RISCV] Check hasEmitNops before call shouldInsertExtraNopBytesForCodeAlign (PR #77236)
Jinyang He via llvm-commits
llvm-commits at lists.llvm.org
Sun Jan 7 19:04:08 PST 2024
https://github.com/MQ-mengqing updated https://github.com/llvm/llvm-project/pull/77236
>From 3ae28f8b40dcd957180985db2dd687d393282af2 Mon Sep 17 00:00:00 2001
From: Jinyang He <hejinyang at loongson.cn>
Date: Sun, 7 Jan 2024 20:31:55 +0800
Subject: [PATCH 1/3] [MC][RISCV] Check hasEmitNops before call
shouldInsertExtraNopBytesForCodeAlign
The shouldInsertExtraNopBytesForCodeAlign() need STI to check whether
relax is enabled or not. It is initialized when call setEmitNops. The
setEmitNops may not be called in a section which has instructions but
is not executable. In this case uninitialized STI will cause problems.
Thus, check hasEmitNops before call it.
Fixes: https://github.com/llvm/llvm-project/pull/76552#issuecomment-1878952480
---
llvm/lib/MC/MCExpr.cpp | 2 +-
llvm/test/MC/RISCV/align-non-executable.s | 29 +++++++++++++++++++++++
2 files changed, 30 insertions(+), 1 deletion(-)
create mode 100644 llvm/test/MC/RISCV/align-non-executable.s
diff --git a/llvm/lib/MC/MCExpr.cpp b/llvm/lib/MC/MCExpr.cpp
index 9dae026535ccfb..80def6dfc24b1a 100644
--- a/llvm/lib/MC/MCExpr.cpp
+++ b/llvm/lib/MC/MCExpr.cpp
@@ -708,7 +708,7 @@ static void AttemptToFoldSymbolOffsetDifference(
if (DF) {
Displacement += DF->getContents().size();
} else if (auto *AF = dyn_cast<MCAlignFragment>(FI);
- AF && Layout &&
+ AF && Layout && AF->hasEmitNops() &&
!Asm->getBackend().shouldInsertExtraNopBytesForCodeAlign(
*AF, Count)) {
Displacement += Asm->computeFragmentSize(*Layout, *AF);
diff --git a/llvm/test/MC/RISCV/align-non-executable.s b/llvm/test/MC/RISCV/align-non-executable.s
new file mode 100644
index 00000000000000..a5bccf360729e1
--- /dev/null
+++ b/llvm/test/MC/RISCV/align-non-executable.s
@@ -0,0 +1,29 @@
+# RUN: llvm-mc --filetype=obj --triple=riscv64 --mattr=+relax %s \
+# RUN: | llvm-readobj -r - | FileCheck --check-prefixes=CHECK,RELAX %s
+# RUN: llvm-mc --filetype=obj --triple=riscv64 --mattr=-relax %s \
+# RUN: | llvm-readobj -r - | FileCheck %s
+
+.section ".dummy", "a"
+.L1:
+ call func
+.p2align 3
+.L2:
+.dword .L2 - .L1
+.word .L2 - .L1
+.half .L2 - .L1
+.byte .L2 - .L1
+
+# CHECK: Relocations [
+# CHECK-NEXT: Section ({{.*}}) .rela.dummy {
+# CHECK-NEXT: 0x0 R_RISCV_CALL_PLT func 0x0
+# RELAX-NEXT: 0x0 R_RISCV_RELAX - 0x0
+# CHECK-NEXT: 0x8 R_RISCV_ADD64 .L2 0x0
+# CHECK-NEXT: 0x8 R_RISCV_SUB64 .L1 0x0
+# CHECK-NEXT: 0x10 R_RISCV_ADD32 .L2 0x0
+# CHECK-NEXT: 0x10 R_RISCV_SUB32 .L1 0x0
+# CHECK-NEXT: 0x14 R_RISCV_ADD16 .L2 0x0
+# CHECK-NEXT: 0x14 R_RISCV_SUB16 .L1 0x0
+# CHECK-NEXT: 0x16 R_RISCV_ADD8 .L2 0x0
+# CHECK-NEXT: 0x16 R_RISCV_SUB8 .L1 0x0
+# CHECK-NEXT: }
+# CHECK-NEXT: ]
>From 02e670d7336e6bc8051f35fc184b053434e7a27a Mon Sep 17 00:00:00 2001
From: Jinyang He <hejinyang at loongson.cn>
Date: Mon, 8 Jan 2024 10:25:52 +0800
Subject: [PATCH 2/3] Address @MaskRay's comments.
---
llvm/test/MC/RISCV/align-non-executable.s | 14 +++++---------
1 file changed, 5 insertions(+), 9 deletions(-)
diff --git a/llvm/test/MC/RISCV/align-non-executable.s b/llvm/test/MC/RISCV/align-non-executable.s
index a5bccf360729e1..9fb91919c5d098 100644
--- a/llvm/test/MC/RISCV/align-non-executable.s
+++ b/llvm/test/MC/RISCV/align-non-executable.s
@@ -1,3 +1,8 @@
+## Check the data diff (separated by aligment directives) directives which in
+## a section which has instructions but is not executable should generate relocs
+## because it can not be calculated out in AttemptToFoldSymbolOffsetDifference.
+## https://github.com/llvm/llvm-project/pull/76552
+
# RUN: llvm-mc --filetype=obj --triple=riscv64 --mattr=+relax %s \
# RUN: | llvm-readobj -r - | FileCheck --check-prefixes=CHECK,RELAX %s
# RUN: llvm-mc --filetype=obj --triple=riscv64 --mattr=-relax %s \
@@ -9,9 +14,6 @@
.p2align 3
.L2:
.dword .L2 - .L1
-.word .L2 - .L1
-.half .L2 - .L1
-.byte .L2 - .L1
# CHECK: Relocations [
# CHECK-NEXT: Section ({{.*}}) .rela.dummy {
@@ -19,11 +21,5 @@
# RELAX-NEXT: 0x0 R_RISCV_RELAX - 0x0
# CHECK-NEXT: 0x8 R_RISCV_ADD64 .L2 0x0
# CHECK-NEXT: 0x8 R_RISCV_SUB64 .L1 0x0
-# CHECK-NEXT: 0x10 R_RISCV_ADD32 .L2 0x0
-# CHECK-NEXT: 0x10 R_RISCV_SUB32 .L1 0x0
-# CHECK-NEXT: 0x14 R_RISCV_ADD16 .L2 0x0
-# CHECK-NEXT: 0x14 R_RISCV_SUB16 .L1 0x0
-# CHECK-NEXT: 0x16 R_RISCV_ADD8 .L2 0x0
-# CHECK-NEXT: 0x16 R_RISCV_SUB8 .L1 0x0
# CHECK-NEXT: }
# CHECK-NEXT: ]
>From 4e0e394896b84044a53164ab6f30c58732b1fb1d Mon Sep 17 00:00:00 2001
From: Jinyang He <hejinyang at loongson.cn>
Date: Mon, 8 Jan 2024 11:02:58 +0800
Subject: [PATCH 3/3] Improve comment
---
llvm/test/MC/RISCV/align-non-executable.s | 6 +++---
1 file changed, 3 insertions(+), 3 deletions(-)
diff --git a/llvm/test/MC/RISCV/align-non-executable.s b/llvm/test/MC/RISCV/align-non-executable.s
index 9fb91919c5d098..95f91d93369f2e 100644
--- a/llvm/test/MC/RISCV/align-non-executable.s
+++ b/llvm/test/MC/RISCV/align-non-executable.s
@@ -1,6 +1,6 @@
-## Check the data diff (separated by aligment directives) directives which in
-## a section which has instructions but is not executable should generate relocs
-## because it can not be calculated out in AttemptToFoldSymbolOffsetDifference.
+## A label difference separated by an alignment directive, when the
+## referenced symbols are in a non-executable section with instructions,
+## should generate ADD/SUB relocations.
## https://github.com/llvm/llvm-project/pull/76552
# RUN: llvm-mc --filetype=obj --triple=riscv64 --mattr=+relax %s \
More information about the llvm-commits
mailing list