[clang] [clang][Mips] Set _MIPS_SPFPSET to 16 before MIPS32 with 32-bit FPRs (PR #246579)

Brad Smith via cfe-commits cfe-commits at lists.llvm.org
Fri Nov 27 00:36:55 PST 2026


https://github.com/brad0 created https://github.com/llvm/llvm-project/pull/246579

Before MIPS32, single-precision arithmetic can't use the odd FPRs when
the FPRs are 32 bits wide, and the backend doesn't use them there.
Clang still defined _MIPS_SPFPSET as 32 for MIPS I, and for MIPS II to
MIPS IV with -mfp32. Define it as 16 there, as GCC does. A single-float
FPU such as the R5900 has 32 single-precision FPRs, so single-float is
not changed.

>From c3490401a166c27948493565793a986a3f69a822 Mon Sep 17 00:00:00 2001
From: Brad Smith <brad at comstyle.com>
Date: Tue, 24 Nov 2026 14:13:03 -0500
Subject: [PATCH] [clang][Mips] Set _MIPS_SPFPSET to 16 before MIPS32 with
 32-bit FPRs

Before MIPS32, single-precision arithmetic can't use the odd FPRs when
the FPRs are 32 bits wide, and the backend doesn't use them there.
Clang still defined _MIPS_SPFPSET as 32 for MIPS I, and for MIPS II to
MIPS IV with -mfp32. Define it as 16 there, as GCC does. A single-float
FPU such as the R5900 has 32 single-precision FPRs, so single-float is
not changed.

Co-Authored-By: Claude Opus 5.5 <noreply at anthropic.com>
---
 clang/lib/Basic/Targets/Mips.h      | 10 ++++++++++
 clang/test/Preprocessor/init-mips.c | 31 +++++++++++++++++++++++++++++
 2 files changed, 41 insertions(+)

diff --git a/clang/lib/Basic/Targets/Mips.h b/clang/lib/Basic/Targets/Mips.h
index 16ad603dbbb985..bdd1438645f684 100644
--- a/clang/lib/Basic/Targets/Mips.h
+++ b/clang/lib/Basic/Targets/Mips.h
@@ -316,6 +316,7 @@ class LLVM_LIBRARY_VISIBILITY MipsTargetInfo : public TargetInfo {
     bool OddSpregGiven = false;
     bool StrictAlign = false;
     bool FpGiven = false;
+    bool IsR5900 = false;
 
     for (const auto &Feature : Features) {
       if (Feature == "+single-float")
@@ -361,6 +362,8 @@ class LLVM_LIBRARY_VISIBILITY MipsTargetInfo : public TargetInfo {
         IsNoABICalls = true;
       else if (Feature == "+use-indirect-jump-hazard")
         UseIndirectJumpHazard = true;
+      else if (Feature == "+r5900")
+        IsR5900 = true;
       else if (Feature == "+nooddspreg") {
         NoOddSpreg = true;
         OddSpregGiven = false;
@@ -381,6 +384,13 @@ class LLVM_LIBRARY_VISIBILITY MipsTargetInfo : public TargetInfo {
       Features.push_back("+fp64");
     }
 
+    // Before MIPS32, single-precision arithmetic can't use the odd FPRs when
+    // the FPRs are 32 bits wide, so the backend doesn't use them. A
+    // single-float FPU has 32 single-precision FPRs. The R5900 is single-float,
+    // but that is implied by the CPU and not passed as a feature here.
+    if (ISALevel < 32 && FPMode != FP64 && !IsSingleFloat && !IsR5900)
+      NoOddSpreg = true;
+
     resetDataLayout();
 
     return true;
diff --git a/clang/test/Preprocessor/init-mips.c b/clang/test/Preprocessor/init-mips.c
index 78391a73d40c0f..9eaa62bfdc888f 100644
--- a/clang/test/Preprocessor/init-mips.c
+++ b/clang/test/Preprocessor/init-mips.c
@@ -1935,6 +1935,37 @@
 // MIPS-XXR6:#define __mips_fpr 64
 // MIPS-XXR6:#define __mips_nan2008 1
 
+// Before MIPS32, the odd FPRs can't be used for single-precision arithmetic
+// when the FPRs are 32 bits wide.
+//
+// RUN: %clang_cc1 -target-cpu mips1 \
+// RUN:   -E -dM -triple=mips-none-none < /dev/null \
+// RUN:   | FileCheck -match-full-lines -check-prefix SPFPSET-16 %s
+// RUN: %clang_cc1 -target-cpu mips1 -target-feature -nooddspreg \
+// RUN:   -E -dM -triple=mips-none-none < /dev/null \
+// RUN:   | FileCheck -match-full-lines -check-prefix SPFPSET-16 %s
+// RUN: %clang_cc1 -target-cpu mips1 -target-feature +soft-float \
+// RUN:   -E -dM -triple=mips-none-none < /dev/null \
+// RUN:   | FileCheck -match-full-lines -check-prefix SPFPSET-16 %s
+// RUN: %clang_cc1 -target-cpu mips2 -target-feature -fp64 \
+// RUN:   -E -dM -triple=mips-none-none < /dev/null \
+// RUN:   | FileCheck -match-full-lines -check-prefix SPFPSET-16 %s
+// RUN: %clang_cc1 -target-cpu mips4 -target-feature -fp64 \
+// RUN:   -E -dM -triple=mips-none-none < /dev/null \
+// RUN:   | FileCheck -match-full-lines -check-prefix SPFPSET-16 %s
+// SPFPSET-16:#define _MIPS_SPFPSET 16
+//
+// RUN: %clang_cc1 -target-cpu mips3 \
+// RUN:   -E -dM -triple=mips64-none-none < /dev/null \
+// RUN:   | FileCheck -match-full-lines -check-prefix SPFPSET-32 %s
+// RUN: %clang_cc1 -target-cpu mips1 -target-feature +single-float \
+// RUN:   -E -dM -triple=mips-none-none < /dev/null \
+// RUN:   | FileCheck -match-full-lines -check-prefix SPFPSET-32 %s
+// RUN: %clang_cc1 -target-cpu r5900 -target-feature -fp64 \
+// RUN:   -E -dM -triple=mipsel-none-none < /dev/null \
+// RUN:   | FileCheck -match-full-lines -check-prefix SPFPSET-32 %s
+// SPFPSET-32:#define _MIPS_SPFPSET 32
+
 // RUN: %clang_cc1 -target-cpu mips32 \
 // RUN:   -E -dM -triple=mips-unknown-netbsd -mrelocation-model pic < /dev/null \
 // RUN:   | FileCheck -match-full-lines -check-prefix MIPS-ABICALLS-NETBSD %s



More information about the cfe-commits mailing list