Skip to content

[MC][RISCV][LoongArch] Add AlignFragment size if layout is available and not need insert nops #76552

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 3, 2024

Conversation

MQ-mengqing
Copy link
Contributor

Due to delayed decision for ADD/SUB relocations, RISCV and LoongArch may go slow fragment walk path with available layout. When RISCV (or LoongArch in the future) don't need insert nops, that means relax is disabled. With available layout and not needing insert nops, the size of AlignFragment should be a constant. So we can add it to Displacement for folding A-B.

@llvmbot llvmbot added mc Machine (object) code backend:loongarch labels Dec 29, 2023
@llvmbot
Copy link
Member

llvmbot commented Dec 29, 2023

@llvm/pr-subscribers-backend-loongarch

@llvm/pr-subscribers-mc

Author: Jinyang He (MQ-mengqing)

Changes

Due to delayed decision for ADD/SUB relocations, RISCV and LoongArch may go slow fragment walk path with available layout. When RISCV (or LoongArch in the future) don't need insert nops, that means relax is disabled. With available layout and not needing insert nops, the size of AlignFragment should be a constant. So we can add it to Displacement for folding A-B.


Full diff: https://github.com/llvm/llvm-project/pull/76552.diff

5 Files Affected:

  • (modified) llvm/lib/MC/MCExpr.cpp (+6)
  • (added) llvm/test/MC/LoongArch/Misc/cfi-advance.s (+27)
  • (modified) llvm/test/MC/LoongArch/Relocations/relax-addsub.s (+3-12)
  • (modified) llvm/test/MC/RISCV/cfi-advance.s (+7)
  • (modified) llvm/test/MC/RISCV/fixups-expr.s (+29)
diff --git a/llvm/lib/MC/MCExpr.cpp b/llvm/lib/MC/MCExpr.cpp
index a85182aa06ad52..9dae026535ccfb 100644
--- a/llvm/lib/MC/MCExpr.cpp
+++ b/llvm/lib/MC/MCExpr.cpp
@@ -704,8 +704,14 @@ static void AttemptToFoldSymbolOffsetDifference(
       }
 
       int64_t Num;
+      unsigned Count;
       if (DF) {
         Displacement += DF->getContents().size();
+      } else if (auto *AF = dyn_cast<MCAlignFragment>(FI);
+                 AF && Layout &&
+                 !Asm->getBackend().shouldInsertExtraNopBytesForCodeAlign(
+                     *AF, Count)) {
+        Displacement += Asm->computeFragmentSize(*Layout, *AF);
       } else if (auto *FF = dyn_cast<MCFillFragment>(FI);
                  FF && FF->getNumValues().evaluateAsAbsolute(Num)) {
         Displacement += Num * FF->getValueSize();
diff --git a/llvm/test/MC/LoongArch/Misc/cfi-advance.s b/llvm/test/MC/LoongArch/Misc/cfi-advance.s
new file mode 100644
index 00000000000000..662c43e6bceafd
--- /dev/null
+++ b/llvm/test/MC/LoongArch/Misc/cfi-advance.s
@@ -0,0 +1,27 @@
+# RUN: llvm-mc --filetype=obj --triple=loongarch64 -mattr=-relax %s -o %t.o
+# RUN: llvm-readobj -r %t.o | FileCheck --check-prefix=RELOC %s
+# RUN: llvm-dwarfdump --debug-frame %t.o | FileCheck --check-prefix=DWARFDUMP %s
+
+# RELOC:       Relocations [
+# RELOC-NEXT:    .rela.eh_frame {
+# RELOC-NEXT:       0x1C R_LARCH_32_PCREL .text 0x0
+# RELOC-NEXT:    }
+# RELOC-NEXT:  ]
+# DWARFDUMP:       DW_CFA_advance_loc: 4
+# DWARFDUMP-NEXT:  DW_CFA_def_cfa_offset: +8
+# DWARFDUMP-NEXT:  DW_CFA_advance_loc: 8
+# DWARFDUMP-NEXT:  DW_CFA_def_cfa_offset: +8
+
+        .text
+        .globl test
+        .p2align 2
+        .type   test,@function
+test:
+        .cfi_startproc
+        nop
+        .cfi_def_cfa_offset 8
+        .p2align 3
+        nop
+        .cfi_def_cfa_offset 8
+        nop
+        .cfi_endproc
diff --git a/llvm/test/MC/LoongArch/Relocations/relax-addsub.s b/llvm/test/MC/LoongArch/Relocations/relax-addsub.s
index c4454f5bb98d11..c30a16562b80bc 100644
--- a/llvm/test/MC/LoongArch/Relocations/relax-addsub.s
+++ b/llvm/test/MC/LoongArch/Relocations/relax-addsub.s
@@ -23,14 +23,6 @@
 # RELAX-NEXT:      0x14 R_LARCH_RELAX - 0x0
 # RELAX-NEXT:    }
 # RELAX-NEXT:    Section ({{.*}}) .rela.data {
-# RELAX-NEXT:      0xF R_LARCH_ADD8 .L3 0x0
-# RELAX-NEXT:      0xF R_LARCH_SUB8 .L2 0x0
-# RELAX-NEXT:      0x10 R_LARCH_ADD16 .L3 0x0
-# RELAX-NEXT:      0x10 R_LARCH_SUB16 .L2 0x0
-# RELAX-NEXT:      0x12 R_LARCH_ADD32 .L3 0x0
-# RELAX-NEXT:      0x12 R_LARCH_SUB32 .L2 0x0
-# RELAX-NEXT:      0x16 R_LARCH_ADD64 .L3 0x0
-# RELAX-NEXT:      0x16 R_LARCH_SUB64 .L2 0x0
 # RELAX-NEXT:      0x1E R_LARCH_ADD8 .L4 0x0
 # RELAX-NEXT:      0x1E R_LARCH_SUB8 .L3 0x0
 # RELAX-NEXT:      0x1F R_LARCH_ADD16 .L4 0x0
@@ -43,8 +35,8 @@
 # RELAX-NEXT:  ]
 
 # RELAX:      Hex dump of section '.data':
-# RELAX-NEXT: 0x00000000 04040004 00000004 00000000 00000000
-# RELAX-NEXT: 0x00000010 00000000 00000000 00000000 00000000
+# RELAX-NEXT: 0x00000000 04040004 00000004 00000000 0000000c
+# RELAX-NEXT: 0x00000010 0c000c00 00000c00 00000000 00000000
 # RELAX-NEXT: 0x00000020 00000000 00000000 00000000 00
 
 .text
@@ -64,8 +56,7 @@
 .word  .L2 - .L1
 .dword .L2 - .L1
 ## With relaxation, emit relocs because of the .align making the diff variable.
-## TODO Handle alignment directive. Why they emit relocs now? They returns
-## without folding symbols offset in AttemptToFoldSymbolOffsetDifference().
+## TODO Handle alignment directive.
 .byte  .L3 - .L2
 .short .L3 - .L2
 .word  .L3 - .L2
diff --git a/llvm/test/MC/RISCV/cfi-advance.s b/llvm/test/MC/RISCV/cfi-advance.s
index d9224fd2ae1c9f..c4af390be757da 100644
--- a/llvm/test/MC/RISCV/cfi-advance.s
+++ b/llvm/test/MC/RISCV/cfi-advance.s
@@ -5,12 +5,16 @@
 
 # CHECK:      .rela.eh_frame {
 # CHECK-NEXT:   0x1C R_RISCV_32_PCREL <null> 0x0
+# CHECK-NEXT:   0x35 R_RISCV_SET6 <null> 0x0
+# CHECK-NEXT:   0x35 R_RISCV_SUB6 <null> 0x0
 # CHECK-NEXT: }
 # CHECK-DWARFDUMP: DW_CFA_advance_loc1: 104
 # CHECK-DWARFDUMP-NEXT: DW_CFA_def_cfa_offset: +8
 # CHECK-DWARFDUMP-NEXT: DW_CFA_advance_loc2: 259
 # CHECK-DWARFDUMP-NEXT: DW_CFA_def_cfa_offset: +8
 # CHECK-DWARFDUMP-NEXT: DW_CFA_advance_loc4: 65539
+# CHECK-DWARFDUMP-NEXT: DW_CFA_def_cfa_offset: +8
+# CHECK-DWARFDUMP-NEXT: DW_CFA_advance_loc: 10
 # CHECK-DWARFDUMP-NEXT: DW_CFA_def_cfa_offset: +8
         .text
         .globl  test                            # -- Begin function test
@@ -28,4 +32,7 @@ test:
         .zero 65535, 0x90
         .cfi_def_cfa_offset 8
         nop
+        .p2align 3
+        .cfi_def_cfa_offset 8
+        nop
         .cfi_endproc
diff --git a/llvm/test/MC/RISCV/fixups-expr.s b/llvm/test/MC/RISCV/fixups-expr.s
index 20e5aacac61928..c850c0a6c0116e 100644
--- a/llvm/test/MC/RISCV/fixups-expr.s
+++ b/llvm/test/MC/RISCV/fixups-expr.s
@@ -16,11 +16,15 @@
 
 .globl G1
 .globl G2
+.globl G3
 .L1:
 G1:
   call extern
 .L2:
 G2:
+  .p2align 3
+.L3:
+G3:
 
 .data
 .dword .L2-.L1
@@ -47,3 +51,28 @@ G2:
 # RELAX: 0x1C R_RISCV_SUB8 .L1 0x0
 # RELAX: 0x1D R_RISCV_ADD8 G2 0x0
 # RELAX: 0x1D R_RISCV_SUB8 G1 0x0
+
+.dword .L3-.L2
+.dword G3-G2
+.word .L3-.L2
+.word G3-G2
+.half .L3-.L2
+.half G3-G2
+.byte .L3-.L2
+.byte G3-G2
+# RELAX: 0x1E R_RISCV_ADD64 .L3 0x0
+# RELAX: 0x1E R_RISCV_SUB64 .L2 0x0
+# RELAX: 0x26 R_RISCV_ADD64 G3 0x0
+# RELAX: 0x26 R_RISCV_SUB64 G2 0x0
+# RELAX: 0x2E R_RISCV_ADD32 .L3 0x0
+# RELAX: 0x2E R_RISCV_SUB32 .L2 0x0
+# RELAX: 0x32 R_RISCV_ADD32 G3 0x0
+# RELAX: 0x32 R_RISCV_SUB32 G2 0x0
+# RELAX: 0x36 R_RISCV_ADD16 .L3 0x0
+# RELAX: 0x36 R_RISCV_SUB16 .L2 0x0
+# RELAX: 0x38 R_RISCV_ADD16 G3 0x0
+# RELAX: 0x38 R_RISCV_SUB16 G2 0x0
+# RELAX: 0x3A R_RISCV_ADD8 .L3 0x0
+# RELAX: 0x3A R_RISCV_SUB8 .L2 0x0
+# RELAX: 0x3B R_RISCV_ADD8 G3 0x0
+# RELAX: 0x3B R_RISCV_SUB8 G2 0x0

@MQ-mengqing
Copy link
Contributor Author

Add: @MaskRay @SixWeining

@SixWeining SixWeining requested a review from MaskRay December 29, 2023 07:11
@SixWeining SixWeining self-requested a review December 30, 2023 01:40
.half G3-G2
.byte .L3-.L2
.byte G3-G2
# RELAX: 0x1E R_RISCV_ADD64 .L3 0x0
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add -NEXT whenever applicable

Copy link
Member

@MaskRay MaskRay left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the patch. LGTM once the ## TODO Handle alignment directive. comment is fixed and fixups-expr.s check prefixes switch to -NEXT (I'll change the existing fixups-expr.s check prefixes to use -NEXT)

…and not need insert nops

Due to delayed decision for ADD/SUB relocations, RISCV and LoongArch
may go slow fragment walk path with available layout. When RISCV (or
LoongArch in the future) don't need insert nops, that means relax is
disabled. With available layout and not needing insert nops, the size
of AlignFragment should be a constant. So we can add it to Displacement
for folding A-B.
@SixWeining SixWeining merged commit 0731567 into llvm:main Jan 3, 2024
@nathanchance
Copy link
Member

I bisected a crash that I see in a particular translation unit when building the Linux kernel for RISC-V, arch/riscv/kernel/kexec_relocate.S, which is built with -mno-relax, to this change.

cvise --not-c spits out:

.section ""
  riscv_kexec_relocate:
 la s6, f
.align 2
riscv_kexec_relocate_end:
  .long riscv_kexec_relocate_end - riscv_kexec_relocate
$ clang --target=riscv64-linux-gnu -c -o /dev/null kexec_relocate.s

$ clang --target=riscv64-linux-gnu -c -o /dev/null kexec_relocate.s -mno-relax
PLEASE submit a bug report to https://github.com/llvm/llvm-project/issues/ and include the crash backtrace, preprocessed source, and associated run script.
Stack dump:
0.	Program arguments: clang --target=riscv64-linux-gnu -c -o /dev/null kexec_relocate.s -mno-relax
 #0 0x0000000003629a08 llvm::sys::PrintStackTrace(llvm::raw_ostream&, int) (/home/nathan/tmp/cvise.fJmmocO8l2/install/llvm-bad/bin/clang-18+0x3629a08)
 #1 0x0000000003627914 llvm::sys::RunSignalHandlers() (/home/nathan/tmp/cvise.fJmmocO8l2/install/llvm-bad/bin/clang-18+0x3627914)
 #2 0x00000000035af2d8 CrashRecoverySignalHandler(int) CrashRecoveryContext.cpp:0:0
 #3 0x0000ffffb09b87a0 (linux-vdso.so.1+0x7a0)
 #4 0x00000000028fead8 llvm::RISCVAsmBackend::shouldInsertExtraNopBytesForCodeAlign(llvm::MCAlignFragment const&, unsigned int&) RISCVAsmBackend.cpp:0:0
 #5 0x00000000033c99c8 AttemptToFoldSymbolOffsetDifference(llvm::MCAssembler const*, llvm::MCAsmLayout const*, llvm::DenseMap<llvm::MCSection const*, unsigned long, llvm::DenseMapInfo<llvm::MCSection const*, void>, llvm::detail::DenseMapPair<llvm::MCSection const*, unsigned long>> const*, bool, llvm::MCSymbolRefExpr const*&, llvm::MCSymbolRefExpr const*&, long&) MCExpr.cpp:0:0
 #6 0x00000000033c92b8 EvaluateSymbolicAdd(llvm::MCAssembler const*, llvm::MCAsmLayout const*, llvm::DenseMap<llvm::MCSection const*, unsigned long, llvm::DenseMapInfo<llvm::MCSection const*, void>, llvm::detail::DenseMapPair<llvm::MCSection const*, unsigned long>> const*, bool, llvm::MCValue const&, llvm::MCValue const&, llvm::MCValue&) MCExpr.cpp:0:0
 #7 0x00000000033c8ef8 llvm::MCExpr::evaluateAsRelocatableImpl(llvm::MCValue&, llvm::MCAssembler const*, llvm::MCAsmLayout const*, llvm::MCFixup const*, llvm::DenseMap<llvm::MCSection const*, unsigned long, llvm::DenseMapInfo<llvm::MCSection const*, void>, llvm::detail::DenseMapPair<llvm::MCSection const*, unsigned long>> const*, bool) const (/home/nathan/tmp/cvise.fJmmocO8l2/install/llvm-bad/bin/clang-18+0x33c8ef8)
 #8 0x00000000033a332c llvm::MCAssembler::evaluateFixup(llvm::MCAsmLayout const&, llvm::MCFixup const&, llvm::MCFragment const*, llvm::MCValue&, llvm::MCSubtargetInfo const*, unsigned long&, bool&) const (/home/nathan/tmp/cvise.fJmmocO8l2/install/llvm-bad/bin/clang-18+0x33a332c)
 #9 0x00000000033a5984 llvm::MCAssembler::layout(llvm::MCAsmLayout&) (/home/nathan/tmp/cvise.fJmmocO8l2/install/llvm-bad/bin/clang-18+0x33a5984)
#10 0x00000000033a6240 llvm::MCAssembler::Finish() (/home/nathan/tmp/cvise.fJmmocO8l2/install/llvm-bad/bin/clang-18+0x33a6240)
#11 0x00000000033c4f04 llvm::MCELFStreamer::finishImpl() (/home/nathan/tmp/cvise.fJmmocO8l2/install/llvm-bad/bin/clang-18+0x33c4f04)
#12 0x00000000034231f8 (anonymous namespace)::AsmParser::Run(bool, bool) AsmParser.cpp:0:0
#13 0x0000000002258898 cc1as_main(llvm::ArrayRef<char const*>, char const*, void*) (/home/nathan/tmp/cvise.fJmmocO8l2/install/llvm-bad/bin/clang-18+0x2258898)
#14 0x00000000022521e4 ExecuteCC1Tool(llvm::SmallVectorImpl<char const*>&, llvm::ToolContext const&) driver.cpp:0:0
#15 0x0000000003f1f800 void llvm::function_ref<void ()>::callback_fn<clang::driver::CC1Command::Execute(llvm::ArrayRef<std::optional<llvm::StringRef>>, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>*, bool*) const::$_0>(long) Job.cpp:0:0
#16 0x00000000035af02c llvm::CrashRecoveryContext::RunSafely(llvm::function_ref<void ()>) (/home/nathan/tmp/cvise.fJmmocO8l2/install/llvm-bad/bin/clang-18+0x35af02c)
#17 0x0000000003f1ee34 clang::driver::CC1Command::Execute(llvm::ArrayRef<std::optional<llvm::StringRef>>, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>*, bool*) const (/home/nathan/tmp/cvise.fJmmocO8l2/install/llvm-bad/bin/clang-18+0x3f1ee34)
#18 0x0000000003ee696c clang::driver::Compilation::ExecuteCommand(clang::driver::Command const&, clang::driver::Command const*&, bool) const (/home/nathan/tmp/cvise.fJmmocO8l2/install/llvm-bad/bin/clang-18+0x3ee696c)
#19 0x0000000003ee6bbc clang::driver::Compilation::ExecuteJobs(clang::driver::JobList const&, llvm::SmallVectorImpl<std::pair<int, clang::driver::Command const*>>&, bool) const (/home/nathan/tmp/cvise.fJmmocO8l2/install/llvm-bad/bin/clang-18+0x3ee6bbc)
#20 0x0000000003eff03c clang::driver::Driver::ExecuteCompilation(clang::driver::Compilation&, llvm::SmallVectorImpl<std::pair<int, clang::driver::Command const*>>&) (/home/nathan/tmp/cvise.fJmmocO8l2/install/llvm-bad/bin/clang-18+0x3eff03c)
#21 0x00000000022515f0 clang_main(int, char**, llvm::ToolContext const&) (/home/nathan/tmp/cvise.fJmmocO8l2/install/llvm-bad/bin/clang-18+0x22515f0)
#22 0x000000000225fcc8 main (/home/nathan/tmp/cvise.fJmmocO8l2/install/llvm-bad/bin/clang-18+0x225fcc8)
#23 0x0000ffffb03209dc __libc_start_call_main (/lib64/libc.so.6+0x309dc)
#24 0x0000ffffb0320ab0 __libc_start_main@GLIBC_2.17 (/lib64/libc.so.6+0x30ab0)
#25 0x000000000224eb30 _start (/home/nathan/tmp/cvise.fJmmocO8l2/install/llvm-bad/bin/clang-18+0x224eb30)
clang: error: clang integrated assembler command failed with exit code 139 (use -v to see invocation)
ClangBuiltLinux clang version 18.0.0git (https://github.com/llvm/llvm-project 0731567a31e4ade97c27801045156a88c4589704)
Target: riscv64-unknown-linux-gnu
Thread model: posix
InstalledDir: /home/nathan/tmp/cvise.fJmmocO8l2/install/llvm-bad/bin
clang: note: diagnostic msg: Error generating preprocessed source(s) - no preprocessable inputs.

I've included the full preprocessed file below, in case you want full introspection.

Full arch/riscv/kernel/kexec_relocate.s
.macro nops, num
 .rept \num
 nop
 .endr
.endm

.macro asm_per_cpu dst sym tmp
 ld \tmp, 32(tp)
 slli \tmp, \tmp, 3
 la \dst, __per_cpu_offset
 add \dst, \dst, \tmp
 ld \tmp, 0(\dst)
 la \dst, \sym
 add \dst, \dst, \tmp
.endm

.macro load_per_cpu dst ptr tmp
 asm_per_cpu \dst \ptr \tmp
 ld \dst, 0(\dst)
.endm

.macro load_global_pointer
.endm

 .macro save_from_x6_to_x31
 sd x6, 48(sp)
 sd x7, 56(sp)
 sd x8, 64(sp)
 sd x9, 72(sp)
 sd x10, 80(sp)
 sd x11, 88(sp)
 sd x12, 96(sp)
 sd x13, 104(sp)
 sd x14, 112(sp)
 sd x15, 120(sp)
 sd x16, 128(sp)
 sd x17, 136(sp)
 sd x18, 144(sp)
 sd x19, 152(sp)
 sd x20, 160(sp)
 sd x21, 168(sp)
 sd x22, 176(sp)
 sd x23, 184(sp)
 sd x24, 192(sp)
 sd x25, 200(sp)
 sd x26, 208(sp)
 sd x27, 216(sp)
 sd x28, 224(sp)
 sd x29, 232(sp)
 sd x30, 240(sp)
 sd x31, 248(sp)
 .endm

 .macro restore_from_x6_to_x31
 ld x6, 48(sp)
 ld x7, 56(sp)
 ld x8, 64(sp)
 ld x9, 72(sp)
 ld x10, 80(sp)
 ld x11, 88(sp)
 ld x12, 96(sp)
 ld x13, 104(sp)
 ld x14, 112(sp)
 ld x15, 120(sp)
 ld x16, 128(sp)
 ld x17, 136(sp)
 ld x18, 144(sp)
 ld x19, 152(sp)
 ld x20, 160(sp)
 ld x21, 168(sp)
 ld x22, 176(sp)
 ld x23, 184(sp)
 ld x24, 192(sp)
 ld x25, 200(sp)
 ld x26, 208(sp)
 ld x27, 216(sp)
 ld x28, 224(sp)
 ld x29, 232(sp)
 ld x30, 240(sp)
 ld x31, 248(sp)
 .endm

.section ".rodata"
.globl riscv_kexec_relocate ; .balign 4 ; riscv_kexec_relocate:

 mv s0, a0
 mv s1, a1
 mv s2, a2
 mv s3, a3
 mv s4, a4
 mv s5, zero
 mv s6, zero

 csrw 0x104, zero
 csrw 0x144, zero

 la s6, 1f
 sub s6, s6, s4
 csrw 0x105, s6

 nop

.align 2
1:
 ld t0, 0(s0)
 addi s0, s0, 8

 andi t1, t0, 0x1
 beqz t1, 2f
 andi s5, t0, ~0x1
 j 1b

2:

 andi t1, t0, 0x2
 beqz t1, 2f
 andi s0, t0, ~0x2
 csrw 0x180, zero
 jr s6

2:

 andi t1, t0, 0x4
 beqz t1, 2f
 j 4f

2:

 andi t1, t0, 0x8
 beqz t1, 1b
 andi t0, t0, ~0x8
 li t3, ((1 << (12)) / 8)
3:
 ld t1, (t0)
 sd t1, (s5)
 addi t0, t0, 8
 addi s5, s5, 8
 addi t3, t3, -0x1
 beqz t3, 1b
 j 3b

4:

 mv a0, s3
 mv a1, s2
 mv a2, s1

 mv a3, zero
 mv a4, zero
 mv a5, zero
 mv a6, zero
 mv a7, zero

 mv s0, zero
 mv s1, zero
 mv s2, zero
 mv s3, zero
 mv s4, zero
 mv s5, zero
 mv s6, zero
 mv s7, zero
 mv s8, zero
 mv s9, zero
 mv s10, zero
 mv s11, zero

 mv t0, zero
 mv t1, zero
 mv t2, zero
 mv t3, zero
 mv t4, zero
 mv t5, zero
 mv t6, zero
 csrw 0x141, zero
 csrw 0x142, zero
 csrw 0x140, zero

 fence.i

 jr a2

.type riscv_kexec_relocate STT_NOTYPE ; .set .L__sym_size_riscv_kexec_relocate, .-riscv_kexec_relocate ; .size riscv_kexec_relocate, .L__sym_size_riscv_kexec_relocate
riscv_kexec_relocate_end:

.section ".text"
.globl riscv_kexec_norelocate ; .balign 4 ; riscv_kexec_norelocate:

 mv s0, a1
 mv s1, a2
 mv s2, a3

 csrw 0x104, zero
 csrw 0x144, zero

 mv a0, s2
 mv a1, s1
 mv a2, s0

 mv a3, zero
 mv a4, zero
 mv a5, zero
 mv a6, zero
 mv a7, zero

 mv s0, zero
 mv s1, zero
 mv s2, zero
 mv s3, zero
 mv s4, zero
 mv s5, zero
 mv s6, zero
 mv s7, zero
 mv s8, zero
 mv s9, zero
 mv s10, zero
 mv s11, zero

 mv t0, zero
 mv t1, zero
 mv t2, zero
 mv t3, zero
 mv t4, zero
 mv t5, zero
 mv t6, zero
 csrw 0x141, zero
 csrw 0x142, zero
 csrw 0x140, zero

 csrw 0x105, a2
 csrw 0x180, zero

.type riscv_kexec_norelocate STT_NOTYPE ; .set .L__sym_size_riscv_kexec_norelocate, .-riscv_kexec_norelocate ; .size riscv_kexec_norelocate, .L__sym_size_riscv_kexec_norelocate

.section ".rodata"
.globl riscv_kexec_relocate_size ; ; riscv_kexec_relocate_size: ; .long riscv_kexec_relocate_end - riscv_kexec_relocate ; .type riscv_kexec_relocate_size STT_OBJECT ; .set .L__sym_size_riscv_kexec_relocate_size, .-riscv_kexec_relocate_size ; .size riscv_kexec_relocate_size, .L__sym_size_riscv_kexec_relocate_size
Bisect log
# bad: [597086c60959dd5b3c032552e8b42dd1d053f233] DAG: Implement promotion for strict_fp_round (#74332)
# good: [7e405eb722e40c79b7726201d0f76b5dab34ba0f] [FuncAttrs] Don't infer `noundef` for functions with `sanitize_memory` attribute (#76691)
git bisect start '597086c60959dd5b3c032552e8b42dd1d053f233' '7e405eb722e40c79b7726201d0f76b5dab34ba0f'
# bad: [0faf46befa7c07e58034ef8c6a7cd4bd5715de0a] [coroutines][DPValue] Update DILocation in DPValue for hoisted dbg.declare (#76765)
git bisect bad 0faf46befa7c07e58034ef8c6a7cd4bd5715de0a
# good: [1fa18fee72639944291b1300a6995abde9b9f51a] [ConstraintElim] Add test case for #76713
git bisect good 1fa18fee72639944291b1300a6995abde9b9f51a
# bad: [c054d8f47d2642de722d64811d9908b6514ea012] [AArch64][LLVM]SME2.1 non-widening BFloat instructions now depend on … (#76222)
git bisect bad c054d8f47d2642de722d64811d9908b6514ea012
# bad: [0731567a31e4ade97c27801045156a88c4589704] [MC][RISCV][LoongArch] Add AlignFragment size if layout is available and not need insert nops (#76552)
git bisect bad 0731567a31e4ade97c27801045156a88c4589704
# good: [41a07e668c29e219ed2f26d61da8b6b3295ff967] [mlir][sparse] recognize NVidia 2:4 type for matmul (#76758)
git bisect good 41a07e668c29e219ed2f26d61da8b6b3295ff967
# good: [7122f55c639a00e719b6088249f4fca1810cf04c] [-Wunsafe-buffer-usage] Warning for unsafe invocation of span::data (#75650)
git bisect good 7122f55c639a00e719b6088249f4fca1810cf04c
# good: [d933b88b71b00461815667d7cd0bb2fecd8606db] [mlir][sparse] use a common util function to query the tensor level s… (#76764)
git bisect good d933b88b71b00461815667d7cd0bb2fecd8606db
# good: [7d81e072712f4e6a150561b5538ccebda289aa13] [SimplifyCFG] When only one case value is missing, replace default with that case (#76669)
git bisect good 7d81e072712f4e6a150561b5538ccebda289aa13
# first bad commit: [0731567a31e4ade97c27801045156a88c4589704] [MC][RISCV][LoongArch] Add AlignFragment size if layout is available and not need insert nops (#76552)

@MQ-mengqing
Copy link
Contributor Author

.section ""

I find it is because the AF->STI is uninitialized in this section. (Not call MCAlignFragment::setEmitNops). I think it can fix by the following diff,

diff --git a/llvm/lib/MC/MCExpr.cpp b/llvm/lib/MC/MCExpr.cpp
index 9dae026535cc..80def6dfc24b 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);

MQ-mengqing added a commit to MQ-mengqing/llvm-project that referenced this pull request Jan 7, 2024
…rCodeAlign

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: llvm#76552 (comment)
SixWeining pushed a commit that referenced this pull request Jan 9, 2024
…rCodeAlign (#77236)

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:
#76552 (comment)
justinfargnoli pushed a commit to justinfargnoli/llvm-project that referenced this pull request Jan 28, 2024
…rCodeAlign (llvm#77236)

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:
llvm#76552 (comment)
MaskRay added a commit that referenced this pull request May 29, 2025
Commit bb03cdc caused a Linux kernel
regression ClangBuiltLinux/linux#2091

When a section contains linker-relaxable MCAlignmentFragment but no
linker-relaxable instructions, the RISCVAsmBackend::isPCRelFixupResolved
code path should be taken as well. The #76552 condition in the fragment
walk code will make the fixup unresolvable, leading to a relocation.
svkeerthy pushed a commit that referenced this pull request May 29, 2025
Commit bb03cdc caused a Linux kernel
regression ClangBuiltLinux/linux#2091

When a section contains linker-relaxable MCAlignmentFragment but no
linker-relaxable instructions, the RISCVAsmBackend::isPCRelFixupResolved
code path should be taken as well. The #76552 condition in the fragment
walk code will make the fixup unresolvable, leading to a relocation.
google-yfyang pushed a commit to google-yfyang/llvm-project that referenced this pull request May 29, 2025
Commit bb03cdc caused a Linux kernel
regression ClangBuiltLinux/linux#2091

When a section contains linker-relaxable MCAlignmentFragment but no
linker-relaxable instructions, the RISCVAsmBackend::isPCRelFixupResolved
code path should be taken as well. The llvm#76552 condition in the fragment
walk code will make the fixup unresolvable, leading to a relocation.
sivan-shani pushed a commit to sivan-shani/llvm-project that referenced this pull request Jun 3, 2025
Commit bb03cdc caused a Linux kernel
regression ClangBuiltLinux/linux#2091

When a section contains linker-relaxable MCAlignmentFragment but no
linker-relaxable instructions, the RISCVAsmBackend::isPCRelFixupResolved
code path should be taken as well. The llvm#76552 condition in the fragment
walk code will make the fixup unresolvable, leading to a relocation.
leecheechen pushed a commit to leecheechen/llvm-project that referenced this pull request Jun 9, 2025
… need insert nops (llvm#76552)

Due to delayed decision for ADD/SUB relocations, RISCV and LoongArch may
go slow fragment walk path with available layout. When RISCV (or
LoongArch in the future) don't need insert nops, that means relax is
disabled. With available layout and not needing insert nops, the size of
AlignFragment should be a constant. So we can add it to Displacement for
folding A-B.

(cherry picked from commit 0731567)
Change-Id: I554d6766bd7f688204e956e4a6431574b4c511c9
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
backend:loongarch mc Machine (object) code
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants