Skip to content

[Mips] Add MipsPat (MipsGPRel tglobaladdr:$in) to select MipsISD::GPRel TargetGlobalAddress#165531

Merged
arsenm merged 1 commit into
llvm:mainfrom
yingopq:Fix_bug_issue_142060
Jan 8, 2026
Merged

[Mips] Add MipsPat (MipsGPRel tglobaladdr:$in) to select MipsISD::GPRel TargetGlobalAddress#165531
arsenm merged 1 commit into
llvm:mainfrom
yingopq:Fix_bug_issue_142060

Conversation

@yingopq

@yingopq yingopq commented Oct 29, 2025

Copy link
Copy Markdown
Contributor

The original logic:

SelectionDAG has 17 nodes:
  t0: ch,glue = EntryToken
          t2: i64,ch = CopyFromReg t0, Register:i64 %0
        t11: i32 = truncate t2
      t14: i32 = and t11, Constant:i32<1>
        t20: i64 = MipsISD::GPRel TargetGlobalAddress:i64<ptr @.str.1> 0 [TF=3]
      t21: i64 = add Register:i64 $gp_64, t20
        t16: i64 = MipsISD::GPRel TargetGlobalAddress:i64<ptr @.str> 0 [TF=3]
      t18: i64 = add Register:i64 $gp_64, t16
    t7: i64 = select t14, t21, t18

When SelectionDAG process visitSELECT, would fold select(cond,
binop(x, y), binop(x, z)) to binop(x, select(cond, y, z)).
As follows:

SelectionDAG has 16 nodes:
  t0: ch,glue = EntryToken
            t2: i64,ch = CopyFromReg t0, Register:i64 %0
          t11: i32 = truncate t2
        t14: i32 = and t11, Constant:i32<1>
        t20: i64 = MipsISD::GPRel TargetGlobalAddress:i64<ptr @.str.1> 0 [TF=3]
        t16: i64 = MipsISD::GPRel TargetGlobalAddress:i64<ptr @.str> 0 [TF=3]
      t22: i64 = select t14, t20, t16
    t23: i64 = add Register:i64 $gp_64, t22

Therefore, the original MipsPat add GPR64:$gp, (MipsGPRel tglobaladdr:$in) is no longer available. And there would be an assert:

ISEL: Starting selection on root node: t20: i64 = MipsISD::GPRel TargetGlobalAddress:i64<ptr @.str.1> 0 [TF=3]
ISEL: Starting pattern match
  Initial Opcode index to 0
  Match failed at index 0
LLVM ERROR: Cannot select: t20: i64 = MipsISD::GPRel TargetGlobalAddress:i64<ptr @.str.1> 0 [TF=3]

So we add a new MipsPat def : MipsPat<(MipsGPRel tglobaladdr:$in), (DADDiu ZERO_64, tglobaladdr:$in)>, ISA_MIPS3, ABI_N64;
to parse MipsISD::GPRel TargetGlobalAddress.

Fix #142060.

@llvmbot

llvmbot commented Oct 29, 2025

Copy link
Copy Markdown
Member

@llvm/pr-subscribers-backend-mips

Author: None (yingopq)

Changes

When SelectionDAG process visitSELECT, would fold select(cond, binop(x, y), binop(x, z)) to binop(x, select(cond, y, z)).
Therefore, the original MipsPat add GPR64:$gp, (MipsGPRel tglobaladdr:$in) is no longer available.


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

2 Files Affected:

  • (modified) llvm/lib/Target/Mips/MipsCondMov.td (+3)
  • (added) llvm/test/CodeGen/Mips/llvm-ir/select-globaladdr.ll (+22)
diff --git a/llvm/lib/Target/Mips/MipsCondMov.td b/llvm/lib/Target/Mips/MipsCondMov.td
index e9e09a188bf5b..947a52bddebb4 100644
--- a/llvm/lib/Target/Mips/MipsCondMov.td
+++ b/llvm/lib/Target/Mips/MipsCondMov.td
@@ -196,6 +196,9 @@ let AdditionalPredicates = [NotInMicroMips] in {
   }
 
   // Instantiation of conditional move patterns.
+  def : MipsPat<(add GPR64:$gp, (select GPR32:$cond, (MipsGPRel tglobaladdr:$T), (MipsGPRel tglobaladdr:$F))),
+              (MOVN_I_I64 (i64 (DADDiu GPR64:$gp, tglobaladdr:$T)), GPR32:$cond, (i64 (DADDiu GPR64:$gp, tglobaladdr:$F)))>, ISA_MIPS3, ABI_N64;
+
   defm : MovzPats0<GPR32, GPR32, MOVZ_I_I, SLT, SLTu, SLTi, SLTiu>,
          INSN_MIPS4_32_NOT_32R6_64R6;
   defm : MovzPats1<GPR32, GPR32, MOVZ_I_I, XOR>, INSN_MIPS4_32_NOT_32R6_64R6;
diff --git a/llvm/test/CodeGen/Mips/llvm-ir/select-globaladdr.ll b/llvm/test/CodeGen/Mips/llvm-ir/select-globaladdr.ll
new file mode 100644
index 0000000000000..54c272a832721
--- /dev/null
+++ b/llvm/test/CodeGen/Mips/llvm-ir/select-globaladdr.ll
@@ -0,0 +1,22 @@
+; RUN: llc < %s -mattr +noabicalls -mgpopt | FileCheck %s -check-prefixes=MIPS64
+
+target datalayout = "E-m:e-i8:8:32-i16:16:32-i64:64-i128:128-n32:64-S128"
+target triple = "mips64-unknown-linux-muslabi64"
+
+@.str = external constant [6 x i8]
+@.str.1 = external constant [6 x i8]
+
+define ptr @tst_select_ptr_ptr(i1 %tobool.not) {
+entry:
+; MIPS64-LABEL: tst_select_ptr_ptr:
+; MIPS64:       # %bb.0: # %entry
+; MIPS64:         sll $1, $4, 0
+; MIPS64:         andi $1, $1, 1
+; MIPS64:         daddiu $2, $gp, %gp_rel(.str)
+; MIPS64:         daddiu $3, $gp, %gp_rel(.str.1)
+; MIPS64:         jr $ra
+; MIPS64:         movn $2, $3, $1
+
+  %cond = select i1 %tobool.not, ptr @.str.1, ptr @.str
+  ret ptr %cond
+}

@brad0

brad0 commented Nov 5, 2025

Copy link
Copy Markdown
Contributor

@alexrp

@alexrp alexrp mentioned this pull request Nov 5, 2025
17 tasks
@brad0

brad0 commented Nov 8, 2025

Copy link
Copy Markdown
Contributor

cc @arsenm

@arsenm arsenm left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

It's not obvious to me what the problem being solved here is from the description. It sounds like an optimization, but when I try the test it's a selection failure. This isn't really a solution for that, you need to MipsISD::GPRel as a leaf

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Suggested change

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Should remove redundant datalayout string

@brad0

brad0 commented Nov 8, 2025

Copy link
Copy Markdown
Contributor

It's not obvious to me what the problem being solved here is from the description.

Look at the bug report mentioned.

@arsenm

arsenm commented Nov 8, 2025

Copy link
Copy Markdown
Contributor

It's not obvious to me what the problem being solved here is from the description.

Look at the bug report mentioned.

The commit message should directly contain all of this information

@yingopq

yingopq commented Nov 12, 2025

Copy link
Copy Markdown
Contributor Author

When SelectionDAG process visitSELECT, would fold select(cond, binop(x, y), binop(x, z)) to binop(x, select(cond, y, z)).
Therefore, the original MipsPat add GPR64:$gp, (MipsGPRel tglobaladdr:$in) is no longer available.

Sorry, I would update the commit message.

@arsenm arsenm left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

The bigger issue is there isn't a freestanding pattern for the global address

@yingopq yingopq force-pushed the Fix_bug_issue_142060 branch from 5a5f0d8 to c12da8d Compare November 13, 2025 03:32
@yingopq

yingopq commented Nov 13, 2025

Copy link
Copy Markdown
Contributor Author

The bigger issue is there isn't a freestanding pattern for the global address

Please help see the new commit message. And I did not get the meanings you said.

@arsenm

arsenm commented Nov 13, 2025

Copy link
Copy Markdown
Contributor

The bigger issue is there isn't a freestanding pattern for the global address

Please help see the new commit message. And I did not get the meanings you said.

The failure is on not handling a freestanding MipsGPRel tglobaladdr:$T. This patch is not a suitable fix for that problem. This patch can only be an optimization, which is hiding a missed pattern on the raw address materialize. If you remove the select from this, I'd expect it could still fail the same way

@yingopq

yingopq commented Nov 13, 2025

Copy link
Copy Markdown
Contributor Author

The bigger issue is there isn't a freestanding pattern for the global address

Please help see the new commit message. And I did not get the meanings you said.

The failure is on not handling a freestanding MipsGPRel tglobaladdr:$T. This patch is not a suitable fix for that problem. This patch can only be an optimization, which is hiding a missed pattern on the raw address materialize. If you remove the select from this, I'd expect it could still fail the same way

I got it, thanks.
Because I did not have another solution about suppooting a freestanding pattern for the global address before, so I choose this optimization. Did you have ?
And I test the issue with this patch, it worked OK.

@yingopq yingopq force-pushed the Fix_bug_issue_142060 branch 2 times, most recently from 5cd8861 to 804f872 Compare December 1, 2025 09:12
@yingopq

yingopq commented Dec 2, 2025

Copy link
Copy Markdown
Contributor Author

@arsenm Can you review again, thanks!

@brad0 brad0 requested a review from arsenm December 4, 2025 00:03
@yingopq

yingopq commented Dec 26, 2025

Copy link
Copy Markdown
Contributor Author

Ping.

@yingopq

yingopq commented Jan 5, 2026

Copy link
Copy Markdown
Contributor Author

@arsenm Can you review again, thanks!

@arsenm

arsenm commented Jan 5, 2026

Copy link
Copy Markdown
Contributor

Can you update the patch title and description

@yingopq

yingopq commented Jan 6, 2026

Copy link
Copy Markdown
Contributor Author

Can you update the patch title and description

OK.

…Rel TargetGlobalAddress

The original logic:
```
SelectionDAG has 17 nodes:
  t0: ch,glue = EntryToken
          t2: i64,ch = CopyFromReg t0, Register:i64 %0
        t11: i32 = truncate t2
      t14: i32 = and t11, Constant:i32<1>
        t20: i64 = MipsISD::GPRel TargetGlobalAddress:i64<ptr @.str.1> 0 [TF=3]
      t21: i64 = add Register:i64 $gp_64, t20
        t16: i64 = MipsISD::GPRel TargetGlobalAddress:i64<ptr @.str> 0 [TF=3]
      t18: i64 = add Register:i64 $gp_64, t16
    t7: i64 = select t14, t21, t18
```

When SelectionDAG process visitSELECT, would fold select(cond,
binop(x, y), binop(x, z)) to binop(x, select(cond, y, z)).
As follows:
```
SelectionDAG has 16 nodes:
  t0: ch,glue = EntryToken
            t2: i64,ch = CopyFromReg t0, Register:i64 %0
          t11: i32 = truncate t2
        t14: i32 = and t11, Constant:i32<1>
        t20: i64 = MipsISD::GPRel TargetGlobalAddress:i64<ptr @.str.1> 0 [TF=3]
        t16: i64 = MipsISD::GPRel TargetGlobalAddress:i64<ptr @.str> 0 [TF=3]
      t22: i64 = select t14, t20, t16
    t23: i64 = add Register:i64 $gp_64, t22
```

Therefore, the original MipsPat `add GPR64:$gp, (MipsGPRel
tglobaladdr:$in)` is no longer available. And there would be an assert:
```
ISEL: Starting selection on root node: t20: i64 = MipsISD::GPRel TargetGlobalAddress:i64<ptr @.str.1> 0 [TF=3]
ISEL: Starting pattern match
  Initial Opcode index to 0
  Match failed at index 0
LLVM ERROR: Cannot select: t20: i64 = MipsISD::GPRel TargetGlobalAddress:i64<ptr @.str.1> 0 [TF=3]
```

So we add a new MipsPat:
  `def : MipsPat<(MipsGPRel tglobaladdr:$in),
              (DADDiu ZERO_64, tglobaladdr:$in)>, ISA_MIPS3, ABI_N64;`
to parse MipsISD::GPRel TargetGlobalAddress.

Fix llvm#142060.
@yingopq yingopq force-pushed the Fix_bug_issue_142060 branch from 804f872 to 631fc2e Compare January 6, 2026 02:56
@yingopq yingopq changed the title [Mips] Add MipsPat add $gp, (select $cond, tglobaladdr, tglobaladdr) to parse MipsISD::GPRel TargetGlobalAddress [Mips] Add MipsPat (MipsGPRel tglobaladdr:$in) to parse MipsISD::GPRel TargetGlobalAddress Jan 6, 2026
@github-actions

github-actions Bot commented Jan 6, 2026

Copy link
Copy Markdown

🪟 Windows x64 Test Results

  • 129121 tests passed
  • 2842 tests skipped
  • 1 test failed

Failed Tests

(click on a test name to see its output)

LLVM

LLVM.CodeGen/RISCV/riscv-macho.ll
Exit Code: 3221225477

Command Output (stdout):
--
# RUN: at line 1
c:\_work\llvm-project\llvm-project\build\bin\llc.exe -mtriple=riscv32-apple-macho C:\_work\llvm-project\llvm-project\llvm\test\CodeGen\RISCV\riscv-macho.ll -o - | c:\_work\llvm-project\llvm-project\build\bin\filecheck.exe C:\_work\llvm-project\llvm-project\llvm\test\CodeGen\RISCV\riscv-macho.ll
# executed command: 'c:\_work\llvm-project\llvm-project\build\bin\llc.exe' -mtriple=riscv32-apple-macho 'C:\_work\llvm-project\llvm-project\llvm\test\CodeGen\RISCV\riscv-macho.ll' -o -
# note: command had no output on stdout or stderr
# executed command: 'c:\_work\llvm-project\llvm-project\build\bin\filecheck.exe' 'C:\_work\llvm-project\llvm-project\llvm\test\CodeGen\RISCV\riscv-macho.ll'
# note: command had no output on stdout or stderr
# RUN: at line 2
c:\_work\llvm-project\llvm-project\build\bin\llc.exe -mtriple=riscv32-apple-macho -filetype=obj C:\_work\llvm-project\llvm-project\llvm\test\CodeGen\RISCV\riscv-macho.ll -o C:\_work\llvm-project\llvm-project\build\test\CodeGen\RISCV\Output\riscv-macho.ll.tmp.o
# executed command: 'c:\_work\llvm-project\llvm-project\build\bin\llc.exe' -mtriple=riscv32-apple-macho -filetype=obj 'C:\_work\llvm-project\llvm-project\llvm\test\CodeGen\RISCV\riscv-macho.ll' -o 'C:\_work\llvm-project\llvm-project\build\test\CodeGen\RISCV\Output\riscv-macho.ll.tmp.o'
# .---command stderr------------
# | PLEASE submit a bug report to https://github.com/llvm/llvm-project/issues/ and include the crash backtrace and instructions to reproduce the bug.
# | Stack dump:
# | 0.	Program arguments: c:\\_work\\llvm-project\\llvm-project\\build\\bin\\llc.exe -mtriple=riscv32-apple-macho -filetype=obj C:\\_work\\llvm-project\\llvm-project\\llvm\\test\\CodeGen\\RISCV\\riscv-macho.ll -o C:\\_work\\llvm-project\\llvm-project\\build\\test\\CodeGen\\RISCV\\Output\\riscv-macho.ll.tmp.o
# | Exception Code: 0xC0000005
# | #0 0x0000000000000000
# | #1 0x00007ff671121901 (c:\_work\llvm-project\llvm-project\build\bin\llc.exe+0x4c1901)
# | #2 0x00007ff671119f11 (c:\_work\llvm-project\llvm-project\build\bin\llc.exe+0x4b9f11)
# | #3 0x00007ff670c67c0c (c:\_work\llvm-project\llvm-project\build\bin\llc.exe+0x7c0c)
# | #4 0x00007ff670c64e86 (c:\_work\llvm-project\llvm-project\build\bin\llc.exe+0x4e86)
# | #5 0x00007ff675278414 (c:\_work\llvm-project\llvm-project\build\bin\llc.exe+0x4618414)
# | #6 0x00007ffdd1864cb0 (C:\Windows\System32\KERNEL32.DLL+0x14cb0)
# | #7 0x00007ffddfffedcb (C:\Windows\SYSTEM32\ntdll.dll+0x7edcb)
# `-----------------------------
# error: command failed with exit status: 0xc0000005

--

If these failures are unrelated to your changes (for example tests are broken or flaky at HEAD), please open an issue at https://github.com/llvm/llvm-project/issues and add the infrastructure label.

@yingopq

yingopq commented Jan 8, 2026

Copy link
Copy Markdown
Contributor Author

Can you update the patch title and description

Can you help review again?

@arsenm arsenm changed the title [Mips] Add MipsPat (MipsGPRel tglobaladdr:$in) to parse MipsISD::GPRel TargetGlobalAddress [Mips] Add MipsPat (MipsGPRel tglobaladdr:$in) to select MipsISD::GPRel TargetGlobalAddress Jan 8, 2026
@arsenm arsenm merged commit 15564a8 into llvm:main Jan 8, 2026
13 of 14 checks passed
Priyanshu3820 pushed a commit to Priyanshu3820/llvm-project that referenced this pull request Jan 18, 2026
…PRel TargetGlobalAddress (llvm#165531)

The original logic:
```
SelectionDAG has 17 nodes:
  t0: ch,glue = EntryToken
          t2: i64,ch = CopyFromReg t0, Register:i64 %0
        t11: i32 = truncate t2
      t14: i32 = and t11, Constant:i32<1>
        t20: i64 = MipsISD::GPRel TargetGlobalAddress:i64<ptr @.str.1> 0 [TF=3]
      t21: i64 = add Register:i64 $gp_64, t20
        t16: i64 = MipsISD::GPRel TargetGlobalAddress:i64<ptr @.str> 0 [TF=3]
      t18: i64 = add Register:i64 $gp_64, t16
    t7: i64 = select t14, t21, t18
```

When SelectionDAG process visitSELECT, would fold select(cond,
binop(x, y), binop(x, z)) to binop(x, select(cond, y, z)).
As follows:
```
SelectionDAG has 16 nodes:
  t0: ch,glue = EntryToken
            t2: i64,ch = CopyFromReg t0, Register:i64 %0
          t11: i32 = truncate t2
        t14: i32 = and t11, Constant:i32<1>
        t20: i64 = MipsISD::GPRel TargetGlobalAddress:i64<ptr @.str.1> 0 [TF=3]
        t16: i64 = MipsISD::GPRel TargetGlobalAddress:i64<ptr @.str> 0 [TF=3]
      t22: i64 = select t14, t20, t16
    t23: i64 = add Register:i64 $gp_64, t22
```

Therefore, the original MipsPat `add GPR64:$gp, (MipsGPRel
tglobaladdr:$in)` is no longer available. And there would be an assert:
```
ISEL: Starting selection on root node: t20: i64 = MipsISD::GPRel TargetGlobalAddress:i64<ptr @.str.1> 0 [TF=3]
ISEL: Starting pattern match
  Initial Opcode index to 0
  Match failed at index 0
LLVM ERROR: Cannot select: t20: i64 = MipsISD::GPRel TargetGlobalAddress:i64<ptr @.str.1> 0 [TF=3]
```

So we add a new MipsPat `def : MipsPat<(MipsGPRel tglobaladdr:$in),
              (DADDiu ZERO_64, tglobaladdr:$in)>, ISA_MIPS3, ABI_N64;`
to parse MipsISD::GPRel TargetGlobalAddress.


Fix llvm#142060.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Mips] Cannot select: t28: i64 = MipsISD::GPRel TargetGlobalAddress:i64<ptr @.str.5> 0 [TF=3] when building musl libc.a with -mno-abicalls

4 participants