Skip to content

Conversation

arsenm
Copy link
Contributor

@arsenm arsenm commented Sep 8, 2025

Prepare to use this with HwMode. This is mostly code copied from x86.

Mips has an exceptionally broken system where the target-abi option
can be used to change the pointer size. i.e., you can mix and match
32-bit base triples with an explicit request to use 32-bit or 64-bit
pointers such that you cannot rely on the triple reported pointer size.
This hack manages to only work for codegen. The MC subtarget constructors
do not have access to the target-abi name so those will continue to not
have the appropriate feature set.

Copy link
Contributor Author

arsenm commented Sep 8, 2025

This stack of pull requests is managed by Graphite. Learn more about stacking.

@llvmbot
Copy link
Member

llvmbot commented Sep 8, 2025

@llvm/pr-subscribers-backend-mips

Author: Matt Arsenault (arsenm)

Changes

Prepare to use this with HwMode. This is mostly code copied from x86.

Mips has an exceptionally broken system where the target-abi option
can be used to change the pointer size. i.e., you can mix and match
32-bit base triples with an explicit request to use 32-bit or 64-bit
pointers such that you cannot rely on the triple reported pointer size.
This hack manages to only work for codegen. The MC subtarget constructors
do not have access to the target-abi name so those will continue to not
have the appropriate feature set.


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

3 Files Affected:

  • (modified) llvm/lib/Target/Mips/MCTargetDesc/MipsMCTargetDesc.cpp (+10-1)
  • (modified) llvm/lib/Target/Mips/MCTargetDesc/MipsMCTargetDesc.h (+1)
  • (modified) llvm/lib/Target/Mips/MipsSubtarget.cpp (+11-2)
diff --git a/llvm/lib/Target/Mips/MCTargetDesc/MipsMCTargetDesc.cpp b/llvm/lib/Target/Mips/MCTargetDesc/MipsMCTargetDesc.cpp
index 2cc634154bffd..c236c19cdb664 100644
--- a/llvm/lib/Target/Mips/MCTargetDesc/MipsMCTargetDesc.cpp
+++ b/llvm/lib/Target/Mips/MCTargetDesc/MipsMCTargetDesc.cpp
@@ -45,6 +45,10 @@ using namespace llvm;
 #define GET_REGINFO_MC_DESC
 #include "MipsGenRegisterInfo.inc"
 
+std::string MIPS_MC::ParseMIPSTriple(const Triple &TT) {
+  return TT.isMIPS64() ? "+ptr64" : "";
+}
+
 void MIPS_MC::initLLVMToCVRegMapping(MCRegisterInfo *MRI) {
   // Mapping from CodeView to MC register id.
   static const struct {
@@ -165,7 +169,12 @@ static MCRegisterInfo *createMipsMCRegisterInfo(const Triple &TT) {
 static MCSubtargetInfo *createMipsMCSubtargetInfo(const Triple &TT,
                                                   StringRef CPU, StringRef FS) {
   CPU = MIPS_MC::selectMipsCPU(TT, CPU);
-  return createMipsMCSubtargetInfoImpl(TT, CPU, /*TuneCPU*/ CPU, FS);
+
+  std::string ArchFS = MIPS_MC::ParseMIPSTriple(TT);
+  if (!FS.empty())
+    ArchFS = (Twine(ArchFS) + "," + FS).str();
+
+  return createMipsMCSubtargetInfoImpl(TT, CPU, /*TuneCPU=*/CPU, ArchFS);
 }
 
 static MCAsmInfo *createMipsMCAsmInfo(const MCRegisterInfo &MRI,
diff --git a/llvm/lib/Target/Mips/MCTargetDesc/MipsMCTargetDesc.h b/llvm/lib/Target/Mips/MCTargetDesc/MipsMCTargetDesc.h
index f3e3e6e8d1073..eb81d24385bdd 100644
--- a/llvm/lib/Target/Mips/MCTargetDesc/MipsMCTargetDesc.h
+++ b/llvm/lib/Target/Mips/MCTargetDesc/MipsMCTargetDesc.h
@@ -57,6 +57,7 @@ createMipsELFObjectWriter(const Triple &TT, bool IsN32);
 std::unique_ptr<MCObjectTargetWriter> createMipsWinCOFFObjectWriter();
 
 namespace MIPS_MC {
+std::string ParseMIPSTriple(const Triple &TT);
 void initLLVMToCVRegMapping(MCRegisterInfo *MRI);
 
 StringRef selectMipsCPU(const Triple &TT, StringRef CPU);
diff --git a/llvm/lib/Target/Mips/MipsSubtarget.cpp b/llvm/lib/Target/Mips/MipsSubtarget.cpp
index 8c4bb15a7e617..ce1b3056c178d 100644
--- a/llvm/lib/Target/Mips/MipsSubtarget.cpp
+++ b/llvm/lib/Target/Mips/MipsSubtarget.cpp
@@ -245,10 +245,19 @@ CodeGenOptLevel MipsSubtarget::getOptLevelToEnablePostRAScheduler() const {
 MipsSubtarget &
 MipsSubtarget::initializeSubtargetDependencies(StringRef CPU, StringRef FS,
                                                const TargetMachine &TM) {
-  StringRef CPUName = MIPS_MC::selectMipsCPU(TM.getTargetTriple(), CPU);
+  const Triple &TT = TM.getTargetTriple();
+  StringRef CPUName = MIPS_MC::selectMipsCPU(TT, CPU);
+
+  std::string FullFS;
+  if (getABI().ArePtrs64bit()) {
+    FullFS = "+ptr64";
+    if (!FS.empty())
+      FullFS = (Twine(FullFS) + "," + FS).str();
+  } else
+    FullFS = FS.str();
 
   // Parse features string.
-  ParseSubtargetFeatures(CPUName, /*TuneCPU*/ CPUName, FS);
+  ParseSubtargetFeatures(CPUName, /*TuneCPU=*/CPUName, FullFS);
   // Initialize scheduling itinerary for the specified CPU.
   InstrItins = getInstrItineraryForCPU(CPUName);
 

@arsenm arsenm marked this pull request as ready for review September 8, 2025 13:21
Copy link
Contributor

@s-barannikov s-barannikov left a comment

Choose a reason for hiding this comment

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

Perhaps those should not be features after all.

@arsenm
Copy link
Contributor Author

arsenm commented Sep 8, 2025

Perhaps those should not be features after all.

They really shouldn't be, but the target features system is a mess and it helps to have all the targets do the same thing

@arsenm arsenm enabled auto-merge (squash) September 8, 2025 23:43
Copy link
Collaborator

@topperc topperc left a comment

Choose a reason for hiding this comment

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

LGTM

arsenm and others added 4 commits September 9, 2025 10:43
Prepare to use this with HwMode. This is mostly code copied from x86.

Mips has an exceptionally broken system where the target-abi option
can be used to change the pointer size. i.e., you can mix and match
32-bit base triples with an explicit request to use 32-bit or 64-bit
pointers such that you cannot rely on the triple reported pointer size.
This hack manages to only work for codegen. The MC subtarget constructors
do not have access to the target-abi name so those will continue to not
have the appropriate feature set.
Co-authored-by: Sergei Barannikov <barannikov88@gmail.com>
@arsenm arsenm force-pushed the users/arsenm/mips/force-64bit-subtarget-feature branch from ed1e6c7 to f74e60e Compare September 9, 2025 01:44
@arsenm arsenm merged commit 7768cca into main Sep 9, 2025
9 checks passed
@arsenm arsenm deleted the users/arsenm/mips/force-64bit-subtarget-feature branch September 9, 2025 02:15
arsenm added a commit that referenced this pull request Sep 10, 2025
arsenm added a commit that referenced this pull request Sep 10, 2025
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.

4 participants