[llvm] 281df9e - RISCVAsmBackend::relaxDwarflineAddr: Try special opcode
Fangrui Song via llvm-commits
llvm-commits at lists.llvm.org
Sat Aug 2 12:55:33 PDT 2025
Author: Fangrui Song
Date: 2025-08-02T12:55:29-07:00
New Revision: 281df9e64720c3ab1c5e03d05087d3fab752d459
URL: https://github.com/llvm/llvm-project/commit/281df9e64720c3ab1c5e03d05087d3fab752d459
DIFF: https://github.com/llvm/llvm-project/commit/281df9e64720c3ab1c5e03d05087d3fab752d459.diff
LOG: RISCVAsmBackend::relaxDwarflineAddr: Try special opcode
LoongArch #77728 did better than RISCV.
If the label difference can be resolved (there is no linker-relaxable
fragment in between), we should use the default handling, which utilizes
a shorter special opcode.
Added:
Modified:
llvm/lib/Target/RISCV/MCTargetDesc/RISCVAsmBackend.cpp
llvm/test/DebugInfo/RISCV/dwarf-riscv-relocs.ll
Removed:
################################################################################
diff --git a/llvm/lib/Target/RISCV/MCTargetDesc/RISCVAsmBackend.cpp b/llvm/lib/Target/RISCV/MCTargetDesc/RISCVAsmBackend.cpp
index 3a2d14843c40b..eb7460ede7d8a 100644
--- a/llvm/lib/Target/RISCV/MCTargetDesc/RISCVAsmBackend.cpp
+++ b/llvm/lib/Target/RISCV/MCTargetDesc/RISCVAsmBackend.cpp
@@ -327,13 +327,15 @@ bool RISCVAsmBackend::relaxAlign(MCFragment &F, unsigned &Size) {
bool RISCVAsmBackend::relaxDwarfLineAddr(MCFragment &F,
bool &WasRelaxed) const {
- MCContext &C = getContext();
-
int64_t LineDelta = F.getDwarfLineDelta();
const MCExpr &AddrDelta = F.getDwarfAddrDelta();
size_t OldSize = F.getVarSize();
int64_t Value;
+ // If the label
diff erence can be resolved, use the default handling, which
+ // utilizes a shorter special opcode.
+ if (AddrDelta.evaluateAsAbsolute(Value, *Asm))
+ return false;
[[maybe_unused]] bool IsAbsolute =
AddrDelta.evaluateKnownAbsolute(Value, *Asm);
assert(IsAbsolute && "CFA with invalid expression");
@@ -352,12 +354,10 @@ bool RISCVAsmBackend::relaxDwarfLineAddr(MCFragment &F,
// value is therefore 65535. Set a conservative upper bound for relaxation.
unsigned PCBytes;
if (Value > 60000) {
- unsigned PtrSize = C.getAsmInfo()->getCodePointerSize();
- assert((PtrSize == 4 || PtrSize == 8) && "Unexpected pointer size");
- PCBytes = PtrSize;
- OS << uint8_t(dwarf::DW_LNS_extended_op) << uint8_t(PtrSize + 1)
+ PCBytes = getContext().getAsmInfo()->getCodePointerSize();
+ OS << uint8_t(dwarf::DW_LNS_extended_op) << uint8_t(PCBytes + 1)
<< uint8_t(dwarf::DW_LNE_set_address);
- OS.write_zeros(PtrSize);
+ OS.write_zeros(PCBytes);
} else {
PCBytes = 2;
OS << uint8_t(dwarf::DW_LNS_fixed_advance_pc);
diff --git a/llvm/test/DebugInfo/RISCV/dwarf-riscv-relocs.ll b/llvm/test/DebugInfo/RISCV/dwarf-riscv-relocs.ll
index d609a3fa889c0..65542e890632e 100644
--- a/llvm/test/DebugInfo/RISCV/dwarf-riscv-relocs.ll
+++ b/llvm/test/DebugInfo/RISCV/dwarf-riscv-relocs.ll
@@ -1,21 +1,45 @@
; RUN: llc -filetype=obj -mtriple=riscv32 -mattr=+relax %s -o %t.o
-; RUN: llvm-readobj -r %t.o | FileCheck -check-prefix=READOBJ-RELOCS %s
+; RUN: llvm-readobj -r %t.o | FileCheck -check-prefix=RELOC %s
; RUN: llvm-objdump --source %t.o | FileCheck --check-prefix=OBJDUMP-SOURCE %s
-; RUN: llvm-dwarfdump --debug-info %t.o | \
-; RUN: FileCheck -check-prefix=DWARF-DUMP %s
-; RUN: llvm-dwarfdump --debug-line -v %t.o | \
-; RUN: FileCheck -check-prefix=LINE-DUMP %s
+; RUN: llvm-dwarfdump --debug-info -debug-line -v %t.o | \
+; RUN: FileCheck -check-prefix=DWARF %s
-; Check that we actually have relocations, otherwise this is kind of pointless.
-; READOBJ-RELOCS: Section ({{.*}}) .rela.debug_info {
-; READOBJ-RELOCS: 0x1B R_RISCV_ADD32 .L0 0x0
-; READOBJ-RELOCS-NEXT: 0x1B R_RISCV_SUB32 .L0 0x0
-; READOBJ-RELOCS: Section ({{.*}}) .rela.debug_frame {
-; READOBJ-RELOCS: 0x20 R_RISCV_ADD32 .L0 0x0
-; READOBJ-RELOCS-NEXT: 0x20 R_RISCV_SUB32 .L0 0x0
-; READOBJ-RELOCS: Section ({{.*}}) .rela.debug_line {
-; READOBJ-RELOCS: 0x5A R_RISCV_ADD16 .L0 0x0
-; READOBJ-RELOCS-NEXT: 0x5A R_RISCV_SUB16 .L0 0x0
+; RELOC: .rela.debug_info {
+; RELOC-NEXT: 0x8 R_RISCV_32 .debug_abbrev 0x0
+; RELOC-NEXT: 0x11 R_RISCV_32 .L0 0x0
+; RELOC-NEXT: 0x15 R_RISCV_32 .Lline_table_start0 0x0
+; RELOC-NEXT: 0x1B R_RISCV_ADD32 .L0 0x0
+; RELOC-NEXT: 0x1B R_RISCV_SUB32 .L0 0x0
+; RELOC-NEXT: 0x1F R_RISCV_32 .L0 0x0
+; RELOC-NEXT: 0x25 R_RISCV_ADD32 .L0 0x0
+; RELOC-NEXT: 0x25 R_RISCV_SUB32 .L0 0x0
+; RELOC-NEXT: }
+; RELOC-NEXT: .rela.debug_str_offsets {
+; RELOC-NEXT: 0x8 R_RISCV_32 .L0 0x0
+; RELOC-NEXT: 0xC R_RISCV_32 .L0 0x0
+; RELOC-NEXT: 0x10 R_RISCV_32 .L0 0x0
+; RELOC-NEXT: 0x14 R_RISCV_32 .L0 0x0
+; RELOC-NEXT: 0x18 R_RISCV_32 .L0 0x0
+; RELOC-NEXT: }
+; RELOC-NEXT: .rela.debug_addr {
+; RELOC-NEXT: 0x8 R_RISCV_32 .L0 0x0
+; RELOC-NEXT: }
+; RELOC-NEXT: .rela.debug_frame {
+; RELOC-NEXT: 0x18 R_RISCV_32 .L0 0x0
+; RELOC-NEXT: 0x1C R_RISCV_32 .L0 0x0
+; RELOC-NEXT: 0x20 R_RISCV_ADD32 .L0 0x0
+; RELOC-NEXT: 0x20 R_RISCV_SUB32 .L0 0x0
+; RELOC-NEXT: 0x33 R_RISCV_SET6 .L0 0x0
+; RELOC-NEXT: 0x33 R_RISCV_SUB6 .L0 0x0
+; RELOC-NEXT: }
+; RELOC-NEXT: .rela.debug_line {
+; RELOC-NEXT: 0x22 R_RISCV_32 .debug_line_str 0x0
+; RELOC-NEXT: 0x31 R_RISCV_32 .debug_line_str 0x2
+; RELOC-NEXT: 0x46 R_RISCV_32 .debug_line_str 0x17
+; RELOC-NEXT: 0x4F R_RISCV_32 .L0 0x0
+; RELOC-NEXT: 0x5B R_RISCV_ADD16 .L0 0x0
+; RELOC-NEXT: 0x5B R_RISCV_SUB16 .L0 0x0
+; RELOC-NEXT: }
; Check that we can print the source, even with relocations.
; OBJDUMP-SOURCE: Disassembly of section .text:
@@ -24,70 +48,61 @@
; OBJDUMP-SOURCE: ; {
; OBJDUMP-SOURCE: ; return 0;
-; Check that we correctly dump the DWARF info, even with relocations.
-; DWARF-DUMP: DW_AT_name ("dwarf-riscv-relocs.c")
-; DWARF-DUMP: DW_AT_comp_dir (".")
-; DWARF-DUMP: DW_AT_name ("main")
-; DWARF-DUMP: DW_AT_decl_file ("{{.*}}dwarf-riscv-relocs.c")
-; DWARF-DUMP: DW_AT_decl_line (1)
-; DWARF-DUMP: DW_AT_type (0x00000032 "int")
-; DWARF-DUMP: DW_AT_name ("int")
-; DWARF-DUMP: DW_AT_encoding (DW_ATE_signed)
-; DWARF-DUMP: DW_AT_byte_size (0x04)
-
-; LINE-DUMP: .debug_line contents:
-; LINE-DUMP-NEXT: debug_line[0x00000000]
-; LINE-DUMP-NEXT: Line table prologue:
-; LINE-DUMP-NEXT: total_length: 0x00000061
-; LINE-DUMP-NEXT: format: DWARF32
-; LINE-DUMP-NEXT: version: 5
-; LINE-DUMP-NEXT: address_size: 4
-; LINE-DUMP-NEXT: seg_select_size: 0
-; LINE-DUMP-NEXT: prologue_length: 0x0000003e
-; LINE-DUMP-NEXT: min_inst_length: 1
-; LINE-DUMP-NEXT: max_ops_per_inst: 1
-; LINE-DUMP-NEXT: default_is_stmt: 1
-; LINE-DUMP-NEXT: line_base: -5
-; LINE-DUMP-NEXT: line_range: 14
-; LINE-DUMP-NEXT: opcode_base: 13
-; LINE-DUMP-NEXT: standard_opcode_lengths[DW_LNS_copy] = 0
-; LINE-DUMP-NEXT: standard_opcode_lengths[DW_LNS_advance_pc] = 1
-; LINE-DUMP-NEXT: standard_opcode_lengths[DW_LNS_advance_line] = 1
-; LINE-DUMP-NEXT: standard_opcode_lengths[DW_LNS_set_file] = 1
-; LINE-DUMP-NEXT: standard_opcode_lengths[DW_LNS_set_column] = 1
-; LINE-DUMP-NEXT: standard_opcode_lengths[DW_LNS_negate_stmt] = 0
-; LINE-DUMP-NEXT: standard_opcode_lengths[DW_LNS_set_basic_block] = 0
-; LINE-DUMP-NEXT: standard_opcode_lengths[DW_LNS_const_add_pc] = 0
-; LINE-DUMP-NEXT: standard_opcode_lengths[DW_LNS_fixed_advance_pc] = 1
-; LINE-DUMP-NEXT: standard_opcode_lengths[DW_LNS_set_prologue_end] = 0
-; LINE-DUMP-NEXT: standard_opcode_lengths[DW_LNS_set_epilogue_begin] = 0
-; LINE-DUMP-NEXT: standard_opcode_lengths[DW_LNS_set_isa] = 1
-; LINE-DUMP-NEXT: include_directories[ 0] = .debug_line_str[0x00000000] = "."
-; LINE-DUMP-NEXT: file_names[ 0]:
-; LINE-DUMP-NEXT: name: .debug_line_str[0x00000002] = "dwarf-riscv-relocs.c"
-; LINE-DUMP-NEXT: dir_index: 0
-; LINE-DUMP-NEXT: md5_checksum: 05ab89f5481bc9f2d037e7886641e919
-; LINE-DUMP-NEXT: source: .debug_line_str[0x00000017] = "int main()\n{\n return 0;\n}\n"
-; LINE-DUMP-EMPTY:
-; LINE-DUMP-NEXT: Address Line Column File ISA Discriminator OpIndex Flags
-; LINE-DUMP-NEXT: ------------------ ------ ------ ------ --- ------------- ------- -------------
-; LINE-DUMP-NEXT:0x0000004a: 04 DW_LNS_set_file (0)
-; LINE-DUMP-NEXT:0x0000004c: 00 DW_LNE_set_address (0x00000000)
-; LINE-DUMP-NEXT:0x00000053: 13 address += 0, line += 1, op-index += 0
-; LINE-DUMP-NEXT: 0x0000000000000000 2 0 0 0 0 0 is_stmt
-; LINE-DUMP-NEXT:0x00000054: 05 DW_LNS_set_column (5)
-; LINE-DUMP-NEXT:0x00000056: 0a DW_LNS_set_prologue_end
-; LINE-DUMP-NEXT:0x00000057: 03 DW_LNS_advance_line (3)
-; LINE-DUMP-NEXT:0x00000059: 09 DW_LNS_fixed_advance_pc (addr += 0x001c, op-index = 0)
-; LINE-DUMP-NEXT:0x0000005c: 01 DW_LNS_copy
-; LINE-DUMP-NEXT: 0x000000000000001c 3 5 0 0 0 0 is_stmt prologue_end
-; LINE-DUMP-NEXT:0x0000005d: 06 DW_LNS_negate_stmt
-; LINE-DUMP-NEXT:0x0000005e: 0b DW_LNS_set_epilogue_begin
-; LINE-DUMP-NEXT:0x0000005f: 4a address += 4, line += 0, op-index += 0
-; LINE-DUMP-NEXT: 0x0000000000000020 3 5 0 0 0 0 epilogue_begin
-; LINE-DUMP-NEXT:0x00000060: 02 DW_LNS_advance_pc (addr += 16, op-index += 0)
-; LINE-DUMP-NEXT:0x00000062: 00 DW_LNE_end_sequence
-; LINE-DUMP-NEXT: 0x0000000000000030 3 5 0 0 0 0 end_sequence
+; DWARF: .debug_line contents:
+; DWARF-NEXT: debug_line[0x00000000]
+; DWARF-NEXT: Line table prologue:
+; DWARF-NEXT: total_length: 0x00000062
+; DWARF-NEXT: format: DWARF32
+; DWARF-NEXT: version: 5
+; DWARF-NEXT: address_size: 4
+; DWARF-NEXT: seg_select_size: 0
+; DWARF-NEXT: prologue_length: 0x0000003e
+; DWARF-NEXT: min_inst_length: 1
+; DWARF-NEXT: max_ops_per_inst: 1
+; DWARF-NEXT: default_is_stmt: 1
+; DWARF-NEXT: line_base: -5
+; DWARF-NEXT: line_range: 14
+; DWARF-NEXT: opcode_base: 13
+; DWARF-NEXT: standard_opcode_lengths[DW_LNS_copy] = 0
+; DWARF-NEXT: standard_opcode_lengths[DW_LNS_advance_pc] = 1
+; DWARF-NEXT: standard_opcode_lengths[DW_LNS_advance_line] = 1
+; DWARF-NEXT: standard_opcode_lengths[DW_LNS_set_file] = 1
+; DWARF-NEXT: standard_opcode_lengths[DW_LNS_set_column] = 1
+; DWARF-NEXT: standard_opcode_lengths[DW_LNS_negate_stmt] = 0
+; DWARF-NEXT: standard_opcode_lengths[DW_LNS_set_basic_block] = 0
+; DWARF-NEXT: standard_opcode_lengths[DW_LNS_const_add_pc] = 0
+; DWARF-NEXT: standard_opcode_lengths[DW_LNS_fixed_advance_pc] = 1
+; DWARF-NEXT: standard_opcode_lengths[DW_LNS_set_prologue_end] = 0
+; DWARF-NEXT: standard_opcode_lengths[DW_LNS_set_epilogue_begin] = 0
+; DWARF-NEXT: standard_opcode_lengths[DW_LNS_set_isa] = 1
+; DWARF-NEXT: include_directories[ 0] = .debug_line_str[0x00000000] = "."
+; DWARF-NEXT: file_names[ 0]:
+; DWARF-NEXT: name: .debug_line_str[0x00000002] = "dwarf-riscv-relocs.c"
+; DWARF-NEXT: dir_index: 0
+; DWARF-NEXT: md5_checksum: 05ab89f5481bc9f2d037e7886641e919
+; DWARF-NEXT: source: .debug_line_str[0x00000017] = "int main()\n{\n return 0;\n}\n"
+; DWARF-EMPTY:
+; DWARF-NEXT: Address Line Column File ISA Discriminator OpIndex Flags
+; DWARF-NEXT: ------------------ ------ ------ ------ --- ------------- ------- -------------
+; DWARF-NEXT:0x0000004a: 04 DW_LNS_set_file (0)
+; DWARF-NEXT:0x0000004c: 00 DW_LNE_set_address (0x00000000)
+; DWARF-NEXT:0x00000053: 13 address += 0, line += 1, op-index += 0
+; DWARF-NEXT: 0x0000000000000000 2 0 0 0 0 0 is_stmt
+; DWARF-NEXT:0x00000054: 05 DW_LNS_set_column (5)
+; DWARF-NEXT:0x00000056: 0a DW_LNS_set_prologue_end
+; DWARF-NEXT:0x00000057: f3 address += 16, line += 1, op-index += 0
+; DWARF-NEXT: 0x0000000000000010 3 5 0 0 0 0 is_stmt prologue_end
+; DWARF-NEXT:0x00000058: 03 DW_LNS_advance_line (4)
+; DWARF-NEXT:0x0000005a: 09 DW_LNS_fixed_advance_pc (addr += 0x0010, op-index = 0)
+; DWARF-NEXT:0x0000005d: 01 DW_LNS_copy
+; DWARF-NEXT: 0x0000000000000020 4 5 0 0 0 0 is_stmt
+; DWARF-NEXT:0x0000005e: 06 DW_LNS_negate_stmt
+; DWARF-NEXT:0x0000005f: 0b DW_LNS_set_epilogue_begin
+; DWARF-NEXT:0x00000060: 4a address += 4, line += 0, op-index += 0
+; DWARF-NEXT: 0x0000000000000024 4 5 0 0 0 0 epilogue_begin
+; DWARF-NEXT:0x00000061: 02 DW_LNS_advance_pc (addr += 16, op-index += 0)
+; DWARF-NEXT:0x00000063: 00 DW_LNE_end_sequence
+; DWARF-NEXT: 0x0000000000000034 4 5 0 0 0 0 end_sequence
; ModuleID = 'dwarf-riscv-relocs.c'
source_filename = "dwarf-riscv-relocs.c"
@@ -97,10 +112,8 @@ target triple = "riscv32"
; Function Attrs: noinline nounwind optnone
define dso_local i32 @main() #0 !dbg !7 {
entry:
- call void @ext()
- %retval = alloca i32, align 4
- store i32 0, ptr %retval, align 4
- ret i32 0, !dbg !11
+ call void asm sideeffect ".cfi_remember_state\0A\09.cfi_adjust_cfa_offset 16\0A\09nop\0A\09call ext\0A\09nop\0A\09.cfi_restore_state\0A\09", ""() #1, !dbg !11
+ ret i32 0, !dbg !12
}
declare void @ext()
@@ -123,3 +136,4 @@ attributes #0 = { noinline nounwind optnone "correctly-rounded-divide-sqrt-fp-ma
!9 = !{!10}
!10 = !DIBasicType(name: "int", size: 32, encoding: DW_ATE_signed)
!11 = !DILocation(line: 3, column: 5, scope: !7)
+!12 = !DILocation(line: 4, column: 5, scope: !7)
More information about the llvm-commits
mailing list