https://github.com/brad0 updated 
https://github.com/llvm/llvm-project/pull/244951

>From f08ae6d8c54f4e50a81e7cb1cf1a6fb2f22063b5 Mon Sep 17 00:00:00 2001
From: Brad Smith <[email protected]>
Date: Sat, 21 Nov 2026 04:52:26 -0500
Subject: [PATCH] [clang][Mips] Set __mips and _MIPS_ISA from the ISA level

__mips and _MIPS_ISA were set from the ABI only, so -march=mips1 to
-march=mips5 got __mips 32 or 64 and _MIPS_ISA_MIPS32 or
_MIPS_ISA_MIPS64. Code that checks for MIPS I, such as for the load
delay slot or the missing ll/sc, could not tell.

Do what GCC does: __mips is 1 to 5 for MIPS I to MIPS V and 32 or 64
for MIPS32 and MIPS64, and _MIPS_ISA follows it. The level comes from
the ISA target features. __mips64 stays based on the ABI.
__mips_isa_rev is now only defined for MIPS32 and MIPS64, as GCC does,
instead of being defined as 0 for the older ISAs.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
---
 clang/lib/Basic/Targets/Mips.cpp    | 38 +++++++++++----
 clang/lib/Basic/Targets/Mips.h      |  6 +++
 clang/test/Preprocessor/init-mips.c | 74 +++++++++++++++++++++++++++++
 3 files changed, 108 insertions(+), 10 deletions(-)

diff --git a/clang/lib/Basic/Targets/Mips.cpp b/clang/lib/Basic/Targets/Mips.cpp
index 76c90815670531..c643aa1ad5ee0a 100644
--- a/clang/lib/Basic/Targets/Mips.cpp
+++ b/clang/lib/Basic/Targets/Mips.cpp
@@ -68,6 +68,24 @@ void MipsTargetInfo::fillValidCPUList(
   Values.append(std::begin(ValidCPUNames), std::end(ValidCPUNames));
 }
 
+// Clang does not expand the features implied by a CPU feature, so p5600,
+// i6400, i6500 and r5900 are listed with the ISA they imply.
+unsigned MipsTargetInfo::getISALevelFromFeature(StringRef Feature) {
+  return llvm::StringSwitch<unsigned>(Feature)
+      .Case("+mips1", 1)
+      .Case("+mips2", 2)
+      .Cases({"+mips3", "+r5900"}, 3)
+      .Case("+mips4", 4)
+      .Case("+mips5", 5)
+      .Cases({"+mips32", "+mips32r2", "+mips32r3", "+mips32r5", "+mips32r6",
+              "+p5600"},
+             32)
+      .Cases({"+mips64", "+mips64r2", "+mips64r3", "+mips64r5", "+mips64r6",
+              "+i6400", "+i6500"},
+             64)
+      .Default(0);
+}
+
 unsigned MipsTargetInfo::getISARev() const {
   return llvm::StringSwitch<unsigned>(getCPU())
       .Cases({"mips32", "mips64"}, 1)
@@ -93,20 +111,20 @@ void MipsTargetInfo::getTargetDefines(const LangOptions 
&Opts,
   if (Opts.GNUMode)
     Builder.defineMacro("mips");
 
-  if (ABI == "o32") {
-    Builder.defineMacro("__mips", "32");
-    Builder.defineMacro("_MIPS_ISA", "_MIPS_ISA_MIPS32");
-  } else {
-    Builder.defineMacro("__mips", "64");
+  // Like GCC, __mips and _MIPS_ISA give the ISA level: 1 to 5 for MIPS I to
+  // MIPS V, and 32 or 64 for any revision of MIPS32 or MIPS64.
+  unsigned Level = ISALevel ? ISALevel : (ABI == "o32" ? 32 : 64);
+  Builder.defineMacro("__mips", Twine(Level));
+  Builder.defineMacro("_MIPS_ISA", "_MIPS_ISA_MIPS" + Twine(Level));
+
+  if (ABI != "o32") {
     Builder.defineMacro("__mips64");
     Builder.defineMacro("__mips64__");
-    Builder.defineMacro("_MIPS_ISA", "_MIPS_ISA_MIPS64");
   }
 
-  const std::string ISARev = std::to_string(getISARev());
-
-  if (!ISARev.empty())
-    Builder.defineMacro("__mips_isa_rev", ISARev);
+  // __mips_isa_rev is only defined for MIPS32 and MIPS64.
+  if (unsigned ISARev = getISARev())
+    Builder.defineMacro("__mips_isa_rev", Twine(ISARev));
 
   if (ABI == "o32") {
     Builder.defineMacro("__mips_o32");
diff --git a/clang/lib/Basic/Targets/Mips.h b/clang/lib/Basic/Targets/Mips.h
index 2f251c7eb86907..e94a2012ca4e35 100644
--- a/clang/lib/Basic/Targets/Mips.h
+++ b/clang/lib/Basic/Targets/Mips.h
@@ -37,6 +37,9 @@ class LLVM_LIBRARY_VISIBILITY MipsTargetInfo : public 
TargetInfo {
   bool DisableMadd4;
   bool UseIndirectJumpHazard;
   bool NoOddSpreg;
+  // ISA level from the target features: 1 to 5 for MIPS I to MIPS V, 32 or
+  // 64 for MIPS32 and MIPS64, 0 if no ISA feature was given.
+  unsigned ISALevel = 0;
 
 protected:
   enum FPModeEnum { FPXX, FP32, FP64 } FPMode;
@@ -176,6 +179,7 @@ class LLVM_LIBRARY_VISIBILITY MipsTargetInfo : public 
TargetInfo {
     return TargetInfo::initFeatureMap(Features, Diags, CPU, FeaturesVec);
   }
 
+  static unsigned getISALevelFromFeature(StringRef Feature);
   unsigned getISARev() const;
 
   void getTargetDefines(const LangOptions &Opts,
@@ -306,11 +310,13 @@ class LLVM_LIBRARY_VISIBILITY MipsTargetInfo : public 
TargetInfo {
     DspRev = NoDSP;
     NoOddSpreg = false;
     FPMode = getDefaultFPMode();
+    ISALevel = 0;
     bool OddSpregGiven = false;
     bool StrictAlign = false;
     bool FpGiven = false;
 
     for (const auto &Feature : Features) {
+      ISALevel = std::max(ISALevel, getISALevelFromFeature(Feature));
       if (Feature == "+single-float")
         IsSingleFloat = true;
       else if (Feature == "+soft-float")
diff --git a/clang/test/Preprocessor/init-mips.c 
b/clang/test/Preprocessor/init-mips.c
index c829eebdc9ec98..78391a73d40c0f 100644
--- a/clang/test/Preprocessor/init-mips.c
+++ b/clang/test/Preprocessor/init-mips.c
@@ -1671,6 +1671,80 @@
 // MIPS-ARCH-I6500:#define __mips_isa_rev 6
 // MIPS-ARCH-I6500:#define __mips_nan2008 1
 
+// RUN: %clang_cc1 -E -dM -ffreestanding -triple=mips-none-none \
+// RUN:            -target-cpu mips1 < /dev/null \
+// RUN:   | FileCheck -match-full-lines -check-prefix MIPS-ARCH-1 %s
+//
+// MIPS-ARCH-1:#define _MIPS_ARCH "mips1"
+// MIPS-ARCH-1:#define _MIPS_ARCH_MIPS1 1
+// MIPS-ARCH-1:#define _MIPS_ISA _MIPS_ISA_MIPS1
+// MIPS-ARCH-1:#define __mips 1
+// MIPS-ARCH-1-NOT:#define __mips_isa_rev {{.*}}
+
+// RUN: %clang_cc1 -E -dM -ffreestanding -triple=mips-none-none \
+// RUN:            -target-cpu mips2 < /dev/null \
+// RUN:   | FileCheck -match-full-lines -check-prefix MIPS-ARCH-2 %s
+//
+// MIPS-ARCH-2:#define _MIPS_ARCH "mips2"
+// MIPS-ARCH-2:#define _MIPS_ARCH_MIPS2 1
+// MIPS-ARCH-2:#define _MIPS_ISA _MIPS_ISA_MIPS2
+// MIPS-ARCH-2:#define __mips 2
+// MIPS-ARCH-2-NOT:#define __mips_isa_rev {{.*}}
+
+// RUN: %clang_cc1 -E -dM -ffreestanding -triple=mips64-none-none \
+// RUN:            -target-cpu mips3 < /dev/null \
+// RUN:   | FileCheck -match-full-lines -check-prefix MIPS-ARCH-3 %s
+//
+// MIPS-ARCH-3:#define _MIPS_ARCH "mips3"
+// MIPS-ARCH-3:#define _MIPS_ARCH_MIPS3 1
+// MIPS-ARCH-3:#define _MIPS_ISA _MIPS_ISA_MIPS3
+// MIPS-ARCH-3:#define __mips 3
+// MIPS-ARCH-3:#define __mips64 1
+// MIPS-ARCH-3-NOT:#define __mips_isa_rev {{.*}}
+
+// RUN: %clang_cc1 -E -dM -ffreestanding -triple=mips64-none-none \
+// RUN:            -target-cpu mips4 < /dev/null \
+// RUN:   | FileCheck -match-full-lines -check-prefix MIPS-ARCH-4 %s
+//
+// MIPS-ARCH-4:#define _MIPS_ARCH "mips4"
+// MIPS-ARCH-4:#define _MIPS_ARCH_MIPS4 1
+// MIPS-ARCH-4:#define _MIPS_ISA _MIPS_ISA_MIPS4
+// MIPS-ARCH-4:#define __mips 4
+// MIPS-ARCH-4:#define __mips64 1
+// MIPS-ARCH-4-NOT:#define __mips_isa_rev {{.*}}
+
+// RUN: %clang_cc1 -E -dM -ffreestanding -triple=mips64-none-none \
+// RUN:            -target-cpu mips5 < /dev/null \
+// RUN:   | FileCheck -match-full-lines -check-prefix MIPS-ARCH-5 %s
+//
+// MIPS-ARCH-5:#define _MIPS_ARCH "mips5"
+// MIPS-ARCH-5:#define _MIPS_ARCH_MIPS5 1
+// MIPS-ARCH-5:#define _MIPS_ISA _MIPS_ISA_MIPS5
+// MIPS-ARCH-5:#define __mips 5
+// MIPS-ARCH-5:#define __mips64 1
+// MIPS-ARCH-5-NOT:#define __mips_isa_rev {{.*}}
+
+// RUN: %clang_cc1 -E -dM -ffreestanding -triple=mipsel-none-none \
+// RUN:            -target-cpu r5900 < /dev/null \
+// RUN:   | FileCheck -match-full-lines -check-prefix MIPS-ARCH-R5900 %s
+//
+// MIPS-ARCH-R5900:#define _MIPS_ARCH "r5900"
+// MIPS-ARCH-R5900:#define _MIPS_ARCH_R5900 1
+// MIPS-ARCH-R5900:#define _MIPS_ISA _MIPS_ISA_MIPS3
+// MIPS-ARCH-R5900:#define __mips 3
+// MIPS-ARCH-R5900-NOT:#define __mips_isa_rev {{.*}}
+
+// A MIPS64 CPU with the O32 ABI is still MIPS64.
+//
+// RUN: %clang_cc1 -E -dM -ffreestanding -triple=mips64-none-none \
+// RUN:            -target-abi o32 -target-cpu mips64r2 < /dev/null \
+// RUN:   | FileCheck -match-full-lines -check-prefix MIPS-ARCH-64R2-O32 %s
+//
+// MIPS-ARCH-64R2-O32:#define _MIPS_ISA _MIPS_ISA_MIPS64
+// MIPS-ARCH-64R2-O32:#define __mips 64
+// MIPS-ARCH-64R2-O32-NOT:#define __mips64 {{.*}}
+// MIPS-ARCH-64R2-O32:#define __mips_isa_rev 2
+
 // Check MIPS float ABI macros
 //
 // RUN: %clang_cc1 -E -dM -ffreestanding \

_______________________________________________
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to