[llvm] opt: Stop creating TargetMachine to infer the datalayout (PR #169585)

Matt Arsenault via llvm-commits llvm-commits at lists.llvm.org
Tue Nov 25 16:46:40 PST 2025


https://github.com/arsenm created https://github.com/llvm/llvm-project/pull/169585

The Triple directly has the datalayout string in it, so just
use that.

The logical flow here is kind of a mess. We were constructing
a temporary target machine in the asm parser to infer the datalayout,
throwing it away, and then creating another target machine for the
actual compilation. The flow of the Triple construction is still
convoluted, but we can at least drop the TargetMachine.

>From 4264dc6b1406866eae8476cdfb4efd7abbba764b Mon Sep 17 00:00:00 2001
From: Matt Arsenault <Matthew.Arsenault at amd.com>
Date: Tue, 25 Nov 2025 19:43:03 -0500
Subject: [PATCH] opt: Stop creating TargetMachine to infer the datalayout

The Triple directly has the datalayout string in it, so just
use that.

The logical flow here is kind of a mess. We were constructing
a temporary target machine in the asm parser to infer the datalayout,
throwing it away, and then creating another target machine for the
actual compilation. The flow of the Triple construction is still
convoluted, but we can at least drop the TargetMachine.
---
 llvm/test/tools/opt/invalid-target.ll |  4 ++--
 llvm/tools/opt/optdriver.cpp          | 14 +++++++-------
 2 files changed, 9 insertions(+), 9 deletions(-)

diff --git a/llvm/test/tools/opt/invalid-target.ll b/llvm/test/tools/opt/invalid-target.ll
index e55a8a44126ac..724aa6ee07b77 100644
--- a/llvm/test/tools/opt/invalid-target.ll
+++ b/llvm/test/tools/opt/invalid-target.ll
@@ -7,10 +7,10 @@
 
 ;; Using "unknown" as the architecture is explicitly allowed (but warns)
 ; RUN: opt -mtriple=unknown -S -passes=no-op-module -o /dev/null < %s 2>&1 | FileCheck %s --check-prefix=UNKNOWN
-; UNKNOWN: warning: failed to infer data layout: unable to get target for 'unknown', see --version and --triple.
+; UNKNOWN: warning: failed to infer data layout{{$}}
 
 ;; However, any other invalid target triple should cause the tool to fail:
 ; RUN: not opt -mtriple=invalid -S -passes=no-op-module -o /dev/null < %s 2>&1 | FileCheck %s --check-prefix=INVALID
-; INVALID: warning: failed to infer data layout: unable to get target for 'invalid', see --version and --triple.
+; INVALID: warning: failed to infer data layout{{$}}
 ; INVALID-NEXT: unrecognized architecture 'invalid' provided.
 ; INVALID-EMPTY:
diff --git a/llvm/tools/opt/optdriver.cpp b/llvm/tools/opt/optdriver.cpp
index d24c8abef31d0..b7ecdec8991a7 100644
--- a/llvm/tools/opt/optdriver.cpp
+++ b/llvm/tools/opt/optdriver.cpp
@@ -538,15 +538,15 @@ optMain(int argc, char **argv,
     // the IR, we should default to an empty (default) DataLayout.
     if (TripleStr.empty())
       return std::nullopt;
-    // Otherwise we infer the DataLayout from the target machine.
-    Expected<std::unique_ptr<TargetMachine>> ExpectedTM =
-        codegen::createTargetMachineForTriple(TripleStr, GetCodeGenOptLevel());
-    if (!ExpectedTM) {
-      errs() << argv[0] << ": warning: failed to infer data layout: "
-             << toString(ExpectedTM.takeError()) << "\n";
+
+    Triple TT(TripleStr);
+
+    std::string Str = TT.computeDataLayout();
+    if (Str.empty()) {
+      errs() << argv[0] << ": warning: failed to infer data layout\n";
       return std::nullopt;
     }
-    return (*ExpectedTM)->createDataLayout().getStringRepresentation();
+    return Str;
   };
   std::unique_ptr<Module> M;
   if (NoUpgradeDebugInfo)



More information about the llvm-commits mailing list