Skip to content

Conversation

arsenm
Copy link
Contributor

@arsenm arsenm commented Sep 17, 2025

No description provided.

Copy link
Contributor Author

arsenm commented Sep 17, 2025

@arsenm arsenm marked this pull request as ready for review September 17, 2025 00:35
@llvmbot
Copy link
Member

llvmbot commented Sep 17, 2025

@llvm/pr-subscribers-tools-llvm-mca

Author: Matt Arsenault (arsenm)

Changes

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

1 Files Affected:

  • (modified) llvm/tools/llvm-mca/llvm-mca.cpp (+11-18)
diff --git a/llvm/tools/llvm-mca/llvm-mca.cpp b/llvm/tools/llvm-mca/llvm-mca.cpp
index d330b44894e7a..c969340742c39 100644
--- a/llvm/tools/llvm-mca/llvm-mca.cpp
+++ b/llvm/tools/llvm-mca/llvm-mca.cpp
@@ -83,9 +83,9 @@ static cl::opt<std::string>
              cl::cat(ToolOptions));
 
 static cl::opt<std::string>
-    TripleName("mtriple",
-               cl::desc("Target triple. See -version for available targets"),
-               cl::cat(ToolOptions));
+    TripleNameOpt("mtriple",
+                  cl::desc("Target triple. See -version for available targets"),
+                  cl::cat(ToolOptions));
 
 static cl::opt<std::string>
     MCPU("mcpu",
@@ -292,11 +292,7 @@ static cl::opt<bool> DisableInstrumentManager(
 
 namespace {
 
-const Target *getTarget(const char *ProgName) {
-  if (TripleName.empty())
-    TripleName = Triple::normalize(sys::getDefaultTargetTriple());
-  Triple TheTriple(TripleName);
-
+const Target *getTarget(Triple &TheTriple, const char *ProgName) {
   // Get the target specific parser.
   std::string Error;
   const Target *TheTarget =
@@ -306,9 +302,6 @@ const Target *getTarget(const char *ProgName) {
     return nullptr;
   }
 
-  // Update TripleName with the updated triple from the target lookup.
-  TripleName = TheTriple.str();
-
   // Return the found target.
   return TheTarget;
 }
@@ -387,18 +380,18 @@ int main(int argc, char **argv) {
   cl::ParseCommandLineOptions(argc, argv,
                               "llvm machine code performance analyzer.\n");
 
+  Triple TheTriple(TripleNameOpt.empty()
+                       ? Triple::normalize(sys::getDefaultTargetTriple())
+                       : TripleNameOpt);
+
   // Get the target from the triple. If a triple is not specified, then select
   // the default triple for the host. If the triple doesn't correspond to any
   // registered target, then exit with an error message.
   const char *ProgName = argv[0];
-  const Target *TheTarget = getTarget(ProgName);
+  const Target *TheTarget = getTarget(TheTriple, ProgName);
   if (!TheTarget)
     return 1;
 
-  // GetTarget() may replaced TripleName with a default triple.
-  // For safety, reconstruct the Triple object.
-  Triple TheTriple(TripleName);
-
   ErrorOr<std::unique_ptr<MemoryBuffer>> BufferPtr =
       MemoryBuffer::getFileOrSTDIN(InputFilename);
   if (std::error_code EC = BufferPtr.getError()) {
@@ -469,7 +462,7 @@ int main(int argc, char **argv) {
   unsigned IPtempOutputAsmVariant =
       OutputAsmVariant == -1 ? 0 : OutputAsmVariant;
   std::unique_ptr<MCInstPrinter> IPtemp(TheTarget->createMCInstPrinter(
-      Triple(TripleName), IPtempOutputAsmVariant, *MAI, *MCII, *MRI));
+      TheTriple, IPtempOutputAsmVariant, *MAI, *MCII, *MRI));
   if (!IPtemp) {
     WithColor::error()
         << "unable to create instruction printer for target triple '"
@@ -558,7 +551,7 @@ int main(int argc, char **argv) {
   if (OutputAsmVariant >= 0)
     AssemblerDialect = static_cast<unsigned>(OutputAsmVariant);
   std::unique_ptr<MCInstPrinter> IP(TheTarget->createMCInstPrinter(
-      Triple(TripleName), AssemblerDialect, *MAI, *MCII, *MRI));
+      TheTriple, AssemblerDialect, *MAI, *MCII, *MRI));
   if (!IP) {
     WithColor::error()
         << "unable to create instruction printer for target triple '"

Copy link
Member

@mshockwave mshockwave 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 merged commit 6b07b05 into main Sep 17, 2025
13 checks passed
@arsenm arsenm deleted the users/arsenm/llvm-mca/fix-unnecessary-reconstruction-triple branch September 17, 2025 01:29
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.

3 participants