Skip to content

Conversation

arsenm
Copy link
Contributor

@arsenm arsenm commented Sep 11, 2025

Simplify Triple computing code and avoid reconstruction and
string based logic

Simplify Triple computing code and avoid reconstruction and
string based logic
@arsenm arsenm added the LTO Link time optimization (regular/full LTO or ThinLTO) label Sep 11, 2025 — with Graphite App
Copy link
Contributor Author

arsenm commented Sep 11, 2025

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

@llvmbot
Copy link
Member

llvmbot commented Sep 11, 2025

@llvm/pr-subscribers-lto

Author: Matt Arsenault (arsenm)

Changes

Simplify Triple computing code and avoid reconstruction and
string based logic


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

2 Files Affected:

  • (modified) llvm/include/llvm/LTO/legacy/LTOCodeGenerator.h (-1)
  • (modified) llvm/lib/LTO/LTOCodeGenerator.cpp (+6-10)
diff --git a/llvm/include/llvm/LTO/legacy/LTOCodeGenerator.h b/llvm/include/llvm/LTO/legacy/LTOCodeGenerator.h
index 391589acba40a..806d3c5bdfd77 100644
--- a/llvm/include/llvm/LTO/legacy/LTOCodeGenerator.h
+++ b/llvm/include/llvm/LTO/legacy/LTOCodeGenerator.h
@@ -239,7 +239,6 @@ struct LTOCodeGenerator {
   std::string FeatureStr;
   std::string NativeObjectPath;
   const Target *MArch = nullptr;
-  std::string TripleStr;
   lto_diagnostic_handler_t DiagHandler = nullptr;
   void *DiagContext = nullptr;
   bool ShouldInternalize = EnableLTOInternalization;
diff --git a/llvm/lib/LTO/LTOCodeGenerator.cpp b/llvm/lib/LTO/LTOCodeGenerator.cpp
index 23e22e7ffcd5c..6f13df73e787b 100644
--- a/llvm/lib/LTO/LTOCodeGenerator.cpp
+++ b/llvm/lib/LTO/LTOCodeGenerator.cpp
@@ -375,16 +375,12 @@ bool LTOCodeGenerator::determineTarget() {
   if (TargetMach)
     return true;
 
-  TripleStr = MergedModule->getTargetTriple().str();
-  if (TripleStr.empty()) {
-    TripleStr = sys::getDefaultTargetTriple();
-    MergedModule->setTargetTriple(Triple(TripleStr));
-  }
-  llvm::Triple Triple(TripleStr);
+  if (const Triple &TT = MergedModule->getTargetTriple(); TT.empty())
+    MergedModule->setTargetTriple(Triple(sys::getDefaultTargetTriple()));
 
   // create target machine from info for merged modules
   std::string ErrMsg;
-  MArch = TargetRegistry::lookupTarget(Triple, ErrMsg);
+  MArch = TargetRegistry::lookupTarget(MergedModule->getTargetTriple(), ErrMsg);
   if (!MArch) {
     emitError(ErrMsg);
     return false;
@@ -393,10 +389,10 @@ bool LTOCodeGenerator::determineTarget() {
   // Construct LTOModule, hand over ownership of module and target. Use MAttr as
   // the default set of features.
   SubtargetFeatures Features(join(Config.MAttrs, ""));
-  Features.getDefaultSubtargetFeatures(Triple);
+  Features.getDefaultSubtargetFeatures(MergedModule->getTargetTriple());
   FeatureStr = Features.getString();
   if (Config.CPU.empty())
-    Config.CPU = lto::getThinLTODefaultCPU(Triple);
+    Config.CPU = lto::getThinLTODefaultCPU(MergedModule->getTargetTriple());
 
   // If data-sections is not explicitly set or unset, set data-sections by
   // default to match the behaviour of lld and gold plugin.
@@ -412,7 +408,7 @@ bool LTOCodeGenerator::determineTarget() {
 std::unique_ptr<TargetMachine> LTOCodeGenerator::createTargetMachine() {
   assert(MArch && "MArch is not set!");
   return std::unique_ptr<TargetMachine>(MArch->createTargetMachine(
-      Triple(TripleStr), Config.CPU, FeatureStr, Config.Options,
+      MergedModule->getTargetTriple(), Config.CPU, FeatureStr, Config.Options,
       Config.RelocModel, std::nullopt, Config.CGOptLevel));
 }
 

@arsenm arsenm marked this pull request as ready for review September 11, 2025 04:52
Co-authored-by: Florian Hahn <flo@fhahn.com>
Copy link
Contributor

@fzou1 fzou1 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 arsenm enabled auto-merge (squash) September 11, 2025 08:09
@arsenm arsenm merged commit 3a7da9a into main Sep 11, 2025
9 checks passed
@arsenm arsenm deleted the users/arsenm/lto/simplify-triple-handling branch September 11, 2025 08:20
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
LTO Link time optimization (regular/full LTO or ThinLTO)
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants