From 4264dc6b1406866eae8476cdfb4efd7abbba764b Mon Sep 17 00:00:00 2001 From: Matt Arsenault Date: Tue, 25 Nov 2025 19:43:03 -0500 Subject: [PATCH 1/2] 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> 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 M; if (NoUpgradeDebugInfo) From 70af3bf15582c7ea0cce7741ba1c001ed6fd5e7c Mon Sep 17 00:00:00 2001 From: Matt Arsenault Date: Tue, 25 Nov 2025 22:19:39 -0500 Subject: [PATCH 2/2] Reword error message --- llvm/test/tools/opt/invalid-target.ll | 4 ++-- llvm/tools/opt/optdriver.cpp | 3 ++- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/llvm/test/tools/opt/invalid-target.ll b/llvm/test/tools/opt/invalid-target.ll index 724aa6ee07b77..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{{$}} +; 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{{$}} +; 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 b7ecdec8991a7..63c47151389b5 100644 --- a/llvm/tools/opt/optdriver.cpp +++ b/llvm/tools/opt/optdriver.cpp @@ -543,7 +543,8 @@ optMain(int argc, char **argv, std::string Str = TT.computeDataLayout(); if (Str.empty()) { - errs() << argv[0] << ": warning: failed to infer data layout\n"; + errs() << argv[0] + << ": warning: failed to infer data layout from target triple\n"; return std::nullopt; } return Str;