[clang] [clang][Mips] Set __mips and _MIPS_ISA from the ISA level (PR #244951)
Brad Smith via cfe-commits
cfe-commits at lists.llvm.org
Sat Nov 21 14:15:32 PST 2026
https://github.com/brad0 created https://github.com/llvm/llvm-project/pull/244951
__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. __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.
>From ca74b8a99a4c7b71fce51e9a7c6089fbc1924a00 Mon Sep 17 00:00:00 2001
From: Brad Smith <brad at comstyle.com>
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. __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 <noreply at anthropic.com>
---
clang/lib/Basic/Targets/Mips.cpp | 35 ++++++++++----
clang/lib/Basic/Targets/Mips.h | 1 +
clang/test/Preprocessor/init-mips.c | 74 +++++++++++++++++++++++++++++
3 files changed, 100 insertions(+), 10 deletions(-)
diff --git a/clang/lib/Basic/Targets/Mips.cpp b/clang/lib/Basic/Targets/Mips.cpp
index 76c90815670531..3a9c783dbea5c6 100644
--- a/clang/lib/Basic/Targets/Mips.cpp
+++ b/clang/lib/Basic/Targets/Mips.cpp
@@ -68,6 +68,21 @@ void MipsTargetInfo::fillValidCPUList(
Values.append(std::begin(ValidCPUNames), std::end(ValidCPUNames));
}
+StringRef MipsTargetInfo::getISALevel() const {
+ return llvm::StringSwitch<StringRef>(getCPU())
+ .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",
+ "octeon", "octeon+", "i6400", "i6500"},
+ "64")
+ .Default(ABI == "o32" ? "32" : "64");
+}
+
unsigned MipsTargetInfo::getISARev() const {
return llvm::StringSwitch<unsigned>(getCPU())
.Cases({"mips32", "mips64"}, 1)
@@ -93,20 +108,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.
+ StringRef ISALevel = getISALevel();
+ Builder.defineMacro("__mips", ISALevel);
+ Builder.defineMacro("_MIPS_ISA", "_MIPS_ISA_MIPS" + ISALevel);
+
+ 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..e21d70c0ddc28c 100644
--- a/clang/lib/Basic/Targets/Mips.h
+++ b/clang/lib/Basic/Targets/Mips.h
@@ -176,6 +176,7 @@ class LLVM_LIBRARY_VISIBILITY MipsTargetInfo : public TargetInfo {
return TargetInfo::initFeatureMap(Features, Diags, CPU, FeaturesVec);
}
+ StringRef getISALevel() const;
unsigned getISARev() const;
void getTargetDefines(const LangOptions &Opts,
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 \
More information about the cfe-commits
mailing list