[llvm] e81a564 - opt: Stop creating TargetMachine to infer the datalayout (#169585)
via llvm-commits
llvm-commits at lists.llvm.org
Tue Nov 25 20:40:46 PST 2025
Author: Matt Arsenault
Date: 2025-11-26T04:40:42Z
New Revision: e81a564cb0031e93d34a941224b14ec73c69bf65
URL: https://github.com/llvm/llvm-project/commit/e81a564cb0031e93d34a941224b14ec73c69bf65
DIFF: https://github.com/llvm/llvm-project/commit/e81a564cb0031e93d34a941224b14ec73c69bf65.diff
LOG: opt: Stop creating TargetMachine to infer the datalayout (#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.
Added:
Modified:
llvm/test/tools/opt/invalid-target.ll
llvm/tools/opt/optdriver.cpp
Removed:
################################################################################
diff --git a/llvm/test/tools/opt/invalid-target.ll b/llvm/test/tools/opt/invalid-target.ll
index e55a8a44126ac..e128dff5cea16 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 from target triple{{$}}
;; 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 from target triple{{$}}
; INVALID-NEXT: unrecognized architecture 'invalid' provided.
; INVALID-EMPTY:
diff --git a/llvm/tools/opt/optdriver.cpp b/llvm/tools/opt/optdriver.cpp
index d24c8abef31d0..63c47151389b5 100644
--- a/llvm/tools/opt/optdriver.cpp
+++ b/llvm/tools/opt/optdriver.cpp
@@ -538,15 +538,16 @@ 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 from target triple\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