Skip to content

Commit

Permalink
[LoongArch] Fix GOT usage for non-dso_local function calls in large…
Browse files Browse the repository at this point in the history
… code model

This commit fixes an issue in the large code model where non-dso_local
function calls did not use the GOT as expected in PIC mode. Instead,
direct PC-relative access was incorrectly applied, leading to linker
errors when building shared libraries.

For `ExternalSymbol`, it is not possible to determine whether it is
dso_local during pseudo-instruction expansion. We use target flags to
differentiate whether GOT should be used.

Reviewed By: heiher, SixWeining

Pull Request: #117099
  • Loading branch information
wangleiat authored Nov 21, 2024
1 parent bc7f24c commit 6377ae4
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 50 deletions.
2 changes: 1 addition & 1 deletion llvm/lib/Target/LoongArch/LoongArchExpandPseudoInsts.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -564,7 +564,7 @@ bool LoongArchPreRAExpandPseudo::expandFunctionCALL(
? MF->getRegInfo().createVirtualRegister(&LoongArch::GPRRegClass)
: LoongArch::R1;

bool UseGOT = Func.isGlobal() && !Func.getGlobal()->isDSOLocal();
bool UseGOT = Func.getTargetFlags() == LoongArchII::MO_CALL_PLT;
unsigned MO = UseGOT ? LoongArchII::MO_GOT_PC_HI : LoongArchII::MO_PCREL_LO;
unsigned LAOpcode = UseGOT ? LoongArch::LDX_D : LoongArch::ADD_D;
expandLargeAddressLoad(MBB, MBBI, NextMBBI, LAOpcode, MO, Func, AddrReg,
Expand Down
10 changes: 5 additions & 5 deletions llvm/test/CodeGen/LoongArch/code-models.ll
Original file line number Diff line number Diff line change
Expand Up @@ -82,11 +82,11 @@ define void @call_external_sym(ptr %dst) {
; LARGE-NEXT: .cfi_offset 1, -8
; LARGE-NEXT: ori $a2, $zero, 1000
; LARGE-NEXT: move $a1, $zero
; LARGE-NEXT: pcalau12i $a3, %pc_hi20(memset)
; LARGE-NEXT: addi.d $ra, $zero, %pc_lo12(memset)
; LARGE-NEXT: lu32i.d $ra, %pc64_lo20(memset)
; LARGE-NEXT: lu52i.d $ra, $ra, %pc64_hi12(memset)
; LARGE-NEXT: add.d $ra, $ra, $a3
; LARGE-NEXT: pcalau12i $a3, %got_pc_hi20(memset)
; LARGE-NEXT: addi.d $ra, $zero, %got_pc_lo12(memset)
; LARGE-NEXT: lu32i.d $ra, %got64_pc_lo20(memset)
; LARGE-NEXT: lu52i.d $ra, $ra, %got64_pc_hi12(memset)
; LARGE-NEXT: ldx.d $ra, $ra, $a3
; LARGE-NEXT: jirl $ra, $ra, 0
; LARGE-NEXT: ld.d $ra, $sp, 8 # 8-byte Folded Reload
; LARGE-NEXT: addi.d $sp, $sp, 16
Expand Down
8 changes: 4 additions & 4 deletions llvm/test/CodeGen/LoongArch/expand-adjacency.ll
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@ declare void @llvm.memset.p0.i64(ptr, i8, i64, i1)

define void @call_external_sym(ptr %dst) {
; LARGE-LABEL: call_external_sym:
; LARGE: pcalau12i [[REG1:\$[a-z0-9]+]], %pc_hi20(memset)
; LARGE-NEXT: addi.d [[REG2:\$[a-z0-9]+]], $zero, %pc_lo12(memset)
; LARGE-NEXT: lu32i.d [[REG2]], %pc64_lo20(memset)
; LARGE-NEXT: lu52i.d [[REG2]], [[REG2]], %pc64_hi12(memset)
; LARGE: pcalau12i [[REG1:\$[a-z0-9]+]], %got_pc_hi20(memset)
; LARGE-NEXT: addi.d [[REG2:\$[a-z0-9]+]], $zero, %got_pc_lo12(memset)
; LARGE-NEXT: lu32i.d [[REG2]], %got64_pc_lo20(memset)
; LARGE-NEXT: lu52i.d [[REG2]], [[REG2]], %got64_pc_hi12(memset)
entry:
call void @llvm.memset.p0.i64(ptr %dst, i8 0, i64 1000, i1 false)
ret void
Expand Down
20 changes: 10 additions & 10 deletions llvm/test/CodeGen/LoongArch/machinelicm-address-pseudos.ll
Original file line number Diff line number Diff line change
Expand Up @@ -279,11 +279,11 @@ define void @test_la_tls_ld(i32 signext %n) {
; LA64LARGE-NEXT: .LBB3_1: # %loop
; LA64LARGE-NEXT: # =>This Inner Loop Header: Depth=1
; LA64LARGE-NEXT: move $a0, $s0
; LA64LARGE-NEXT: pcalau12i $a1, %pc_hi20(__tls_get_addr)
; LA64LARGE-NEXT: addi.d $ra, $zero, %pc_lo12(__tls_get_addr)
; LA64LARGE-NEXT: lu32i.d $ra, %pc64_lo20(__tls_get_addr)
; LA64LARGE-NEXT: lu52i.d $ra, $ra, %pc64_hi12(__tls_get_addr)
; LA64LARGE-NEXT: add.d $ra, $ra, $a1
; LA64LARGE-NEXT: pcalau12i $a1, %got_pc_hi20(__tls_get_addr)
; LA64LARGE-NEXT: addi.d $ra, $zero, %got_pc_lo12(__tls_get_addr)
; LA64LARGE-NEXT: lu32i.d $ra, %got64_pc_lo20(__tls_get_addr)
; LA64LARGE-NEXT: lu52i.d $ra, $ra, %got64_pc_hi12(__tls_get_addr)
; LA64LARGE-NEXT: ldx.d $ra, $ra, $a1
; LA64LARGE-NEXT: jirl $ra, $ra, 0
; LA64LARGE-NEXT: ld.w $zero, $a0, 0
; LA64LARGE-NEXT: addi.w $s1, $s1, 1
Expand Down Expand Up @@ -445,11 +445,11 @@ define void @test_la_tls_gd(i32 signext %n) nounwind {
; LA64LARGE-NEXT: .LBB5_1: # %loop
; LA64LARGE-NEXT: # =>This Inner Loop Header: Depth=1
; LA64LARGE-NEXT: move $a0, $s0
; LA64LARGE-NEXT: pcalau12i $a1, %pc_hi20(__tls_get_addr)
; LA64LARGE-NEXT: addi.d $ra, $zero, %pc_lo12(__tls_get_addr)
; LA64LARGE-NEXT: lu32i.d $ra, %pc64_lo20(__tls_get_addr)
; LA64LARGE-NEXT: lu52i.d $ra, $ra, %pc64_hi12(__tls_get_addr)
; LA64LARGE-NEXT: add.d $ra, $ra, $a1
; LA64LARGE-NEXT: pcalau12i $a1, %got_pc_hi20(__tls_get_addr)
; LA64LARGE-NEXT: addi.d $ra, $zero, %got_pc_lo12(__tls_get_addr)
; LA64LARGE-NEXT: lu32i.d $ra, %got64_pc_lo20(__tls_get_addr)
; LA64LARGE-NEXT: lu52i.d $ra, $ra, %got64_pc_hi12(__tls_get_addr)
; LA64LARGE-NEXT: ldx.d $ra, $ra, $a1
; LA64LARGE-NEXT: jirl $ra, $ra, 0
; LA64LARGE-NEXT: ld.w $zero, $a0, 0
; LA64LARGE-NEXT: addi.w $s1, $s1, 1
Expand Down
40 changes: 20 additions & 20 deletions llvm/test/CodeGen/LoongArch/psabi-restricted-scheduling.ll
Original file line number Diff line number Diff line change
Expand Up @@ -102,23 +102,23 @@ define void @foo() nounwind {
; LARGE_NO_SCH-NEXT: lu32i.d $a1, %got64_pc_lo20(gd)
; LARGE_NO_SCH-NEXT: lu52i.d $a1, $a1, %got64_pc_hi12(gd)
; LARGE_NO_SCH-NEXT: add.d $a0, $a1, $a0
; LARGE_NO_SCH-NEXT: pcalau12i $a1, %pc_hi20(__tls_get_addr)
; LARGE_NO_SCH-NEXT: addi.d $ra, $zero, %pc_lo12(__tls_get_addr)
; LARGE_NO_SCH-NEXT: lu32i.d $ra, %pc64_lo20(__tls_get_addr)
; LARGE_NO_SCH-NEXT: lu52i.d $ra, $ra, %pc64_hi12(__tls_get_addr)
; LARGE_NO_SCH-NEXT: add.d $ra, $ra, $a1
; LARGE_NO_SCH-NEXT: pcalau12i $a1, %got_pc_hi20(__tls_get_addr)
; LARGE_NO_SCH-NEXT: addi.d $ra, $zero, %got_pc_lo12(__tls_get_addr)
; LARGE_NO_SCH-NEXT: lu32i.d $ra, %got64_pc_lo20(__tls_get_addr)
; LARGE_NO_SCH-NEXT: lu52i.d $ra, $ra, %got64_pc_hi12(__tls_get_addr)
; LARGE_NO_SCH-NEXT: ldx.d $ra, $ra, $a1
; LARGE_NO_SCH-NEXT: jirl $ra, $ra, 0
; LARGE_NO_SCH-NEXT: ld.d $zero, $a0, 0
; LARGE_NO_SCH-NEXT: pcalau12i $a0, %ld_pc_hi20(ld)
; LARGE_NO_SCH-NEXT: addi.d $a1, $zero, %got_pc_lo12(ld)
; LARGE_NO_SCH-NEXT: lu32i.d $a1, %got64_pc_lo20(ld)
; LARGE_NO_SCH-NEXT: lu52i.d $a1, $a1, %got64_pc_hi12(ld)
; LARGE_NO_SCH-NEXT: add.d $a0, $a1, $a0
; LARGE_NO_SCH-NEXT: pcalau12i $a1, %pc_hi20(__tls_get_addr)
; LARGE_NO_SCH-NEXT: addi.d $ra, $zero, %pc_lo12(__tls_get_addr)
; LARGE_NO_SCH-NEXT: lu32i.d $ra, %pc64_lo20(__tls_get_addr)
; LARGE_NO_SCH-NEXT: lu52i.d $ra, $ra, %pc64_hi12(__tls_get_addr)
; LARGE_NO_SCH-NEXT: add.d $ra, $ra, $a1
; LARGE_NO_SCH-NEXT: pcalau12i $a1, %got_pc_hi20(__tls_get_addr)
; LARGE_NO_SCH-NEXT: addi.d $ra, $zero, %got_pc_lo12(__tls_get_addr)
; LARGE_NO_SCH-NEXT: lu32i.d $ra, %got64_pc_lo20(__tls_get_addr)
; LARGE_NO_SCH-NEXT: lu52i.d $ra, $ra, %got64_pc_hi12(__tls_get_addr)
; LARGE_NO_SCH-NEXT: ldx.d $ra, $ra, $a1
; LARGE_NO_SCH-NEXT: jirl $ra, $ra, 0
; LARGE_NO_SCH-NEXT: ld.d $zero, $a0, 0
; LARGE_NO_SCH-NEXT: pcalau12i $a0, %ie_pc_hi20(ie)
Expand Down Expand Up @@ -158,23 +158,23 @@ define void @foo() nounwind {
; LARGE_SCH-NEXT: lu32i.d $a1, %got64_pc_lo20(gd)
; LARGE_SCH-NEXT: lu52i.d $a1, $a1, %got64_pc_hi12(gd)
; LARGE_SCH-NEXT: add.d $a0, $a1, $a0
; LARGE_SCH-NEXT: pcalau12i $a1, %pc_hi20(__tls_get_addr)
; LARGE_SCH-NEXT: addi.d $ra, $zero, %pc_lo12(__tls_get_addr)
; LARGE_SCH-NEXT: lu32i.d $ra, %pc64_lo20(__tls_get_addr)
; LARGE_SCH-NEXT: lu52i.d $ra, $ra, %pc64_hi12(__tls_get_addr)
; LARGE_SCH-NEXT: add.d $ra, $ra, $a1
; LARGE_SCH-NEXT: pcalau12i $a1, %got_pc_hi20(__tls_get_addr)
; LARGE_SCH-NEXT: addi.d $ra, $zero, %got_pc_lo12(__tls_get_addr)
; LARGE_SCH-NEXT: lu32i.d $ra, %got64_pc_lo20(__tls_get_addr)
; LARGE_SCH-NEXT: lu52i.d $ra, $ra, %got64_pc_hi12(__tls_get_addr)
; LARGE_SCH-NEXT: ldx.d $ra, $ra, $a1
; LARGE_SCH-NEXT: jirl $ra, $ra, 0
; LARGE_SCH-NEXT: ld.d $zero, $a0, 0
; LARGE_SCH-NEXT: pcalau12i $a0, %ld_pc_hi20(ld)
; LARGE_SCH-NEXT: addi.d $a1, $zero, %got_pc_lo12(ld)
; LARGE_SCH-NEXT: lu32i.d $a1, %got64_pc_lo20(ld)
; LARGE_SCH-NEXT: lu52i.d $a1, $a1, %got64_pc_hi12(ld)
; LARGE_SCH-NEXT: add.d $a0, $a1, $a0
; LARGE_SCH-NEXT: pcalau12i $a1, %pc_hi20(__tls_get_addr)
; LARGE_SCH-NEXT: addi.d $ra, $zero, %pc_lo12(__tls_get_addr)
; LARGE_SCH-NEXT: lu32i.d $ra, %pc64_lo20(__tls_get_addr)
; LARGE_SCH-NEXT: lu52i.d $ra, $ra, %pc64_hi12(__tls_get_addr)
; LARGE_SCH-NEXT: add.d $ra, $ra, $a1
; LARGE_SCH-NEXT: pcalau12i $a1, %got_pc_hi20(__tls_get_addr)
; LARGE_SCH-NEXT: addi.d $ra, $zero, %got_pc_lo12(__tls_get_addr)
; LARGE_SCH-NEXT: lu32i.d $ra, %got64_pc_lo20(__tls_get_addr)
; LARGE_SCH-NEXT: lu52i.d $ra, $ra, %got64_pc_hi12(__tls_get_addr)
; LARGE_SCH-NEXT: ldx.d $ra, $ra, $a1
; LARGE_SCH-NEXT: jirl $ra, $ra, 0
; LARGE_SCH-NEXT: ld.d $zero, $a0, 0
; LARGE_SCH-NEXT: pcalau12i $a0, %ie_pc_hi20(ie)
Expand Down
20 changes: 10 additions & 10 deletions llvm/test/CodeGen/LoongArch/tls-models.ll
Original file line number Diff line number Diff line change
Expand Up @@ -55,11 +55,11 @@ define ptr @f1() nounwind {
; LA64LARGEPIC-NEXT: lu32i.d $a1, %got64_pc_lo20(unspecified)
; LA64LARGEPIC-NEXT: lu52i.d $a1, $a1, %got64_pc_hi12(unspecified)
; LA64LARGEPIC-NEXT: add.d $a0, $a1, $a0
; LA64LARGEPIC-NEXT: pcalau12i $a1, %pc_hi20(__tls_get_addr)
; LA64LARGEPIC-NEXT: addi.d $ra, $zero, %pc_lo12(__tls_get_addr)
; LA64LARGEPIC-NEXT: lu32i.d $ra, %pc64_lo20(__tls_get_addr)
; LA64LARGEPIC-NEXT: lu52i.d $ra, $ra, %pc64_hi12(__tls_get_addr)
; LA64LARGEPIC-NEXT: add.d $ra, $ra, $a1
; LA64LARGEPIC-NEXT: pcalau12i $a1, %got_pc_hi20(__tls_get_addr)
; LA64LARGEPIC-NEXT: addi.d $ra, $zero, %got_pc_lo12(__tls_get_addr)
; LA64LARGEPIC-NEXT: lu32i.d $ra, %got64_pc_lo20(__tls_get_addr)
; LA64LARGEPIC-NEXT: lu52i.d $ra, $ra, %got64_pc_hi12(__tls_get_addr)
; LA64LARGEPIC-NEXT: ldx.d $ra, $ra, $a1
; LA64LARGEPIC-NEXT: jirl $ra, $ra, 0
; LA64LARGEPIC-NEXT: ld.d $ra, $sp, 8 # 8-byte Folded Reload
; LA64LARGEPIC-NEXT: addi.d $sp, $sp, 16
Expand Down Expand Up @@ -168,11 +168,11 @@ define ptr @f2() nounwind {
; LA64LARGEPIC-NEXT: lu32i.d $a1, %got64_pc_lo20(ld)
; LA64LARGEPIC-NEXT: lu52i.d $a1, $a1, %got64_pc_hi12(ld)
; LA64LARGEPIC-NEXT: add.d $a0, $a1, $a0
; LA64LARGEPIC-NEXT: pcalau12i $a1, %pc_hi20(__tls_get_addr)
; LA64LARGEPIC-NEXT: addi.d $ra, $zero, %pc_lo12(__tls_get_addr)
; LA64LARGEPIC-NEXT: lu32i.d $ra, %pc64_lo20(__tls_get_addr)
; LA64LARGEPIC-NEXT: lu52i.d $ra, $ra, %pc64_hi12(__tls_get_addr)
; LA64LARGEPIC-NEXT: add.d $ra, $ra, $a1
; LA64LARGEPIC-NEXT: pcalau12i $a1, %got_pc_hi20(__tls_get_addr)
; LA64LARGEPIC-NEXT: addi.d $ra, $zero, %got_pc_lo12(__tls_get_addr)
; LA64LARGEPIC-NEXT: lu32i.d $ra, %got64_pc_lo20(__tls_get_addr)
; LA64LARGEPIC-NEXT: lu52i.d $ra, $ra, %got64_pc_hi12(__tls_get_addr)
; LA64LARGEPIC-NEXT: ldx.d $ra, $ra, $a1
; LA64LARGEPIC-NEXT: jirl $ra, $ra, 0
; LA64LARGEPIC-NEXT: ld.d $ra, $sp, 8 # 8-byte Folded Reload
; LA64LARGEPIC-NEXT: addi.d $sp, $sp, 16
Expand Down

0 comments on commit 6377ae4

Please sign in to comment.