Skip to content

Commit e81a564

Browse files
authored
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.
1 parent bfc732e commit e81a564

File tree

2 files changed

+10
-9
lines changed

2 files changed

+10
-9
lines changed

llvm/test/tools/opt/invalid-target.ll

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,10 @@
77

88
;; Using "unknown" as the architecture is explicitly allowed (but warns)
99
; RUN: opt -mtriple=unknown -S -passes=no-op-module -o /dev/null < %s 2>&1 | FileCheck %s --check-prefix=UNKNOWN
10-
; UNKNOWN: warning: failed to infer data layout: unable to get target for 'unknown', see --version and --triple.
10+
; UNKNOWN: warning: failed to infer data layout from target triple{{$}}
1111

1212
;; However, any other invalid target triple should cause the tool to fail:
1313
; RUN: not opt -mtriple=invalid -S -passes=no-op-module -o /dev/null < %s 2>&1 | FileCheck %s --check-prefix=INVALID
14-
; INVALID: warning: failed to infer data layout: unable to get target for 'invalid', see --version and --triple.
14+
; INVALID: warning: failed to infer data layout from target triple{{$}}
1515
; INVALID-NEXT: unrecognized architecture 'invalid' provided.
1616
; INVALID-EMPTY:

llvm/tools/opt/optdriver.cpp

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -538,15 +538,16 @@ optMain(int argc, char **argv,
538538
// the IR, we should default to an empty (default) DataLayout.
539539
if (TripleStr.empty())
540540
return std::nullopt;
541-
// Otherwise we infer the DataLayout from the target machine.
542-
Expected<std::unique_ptr<TargetMachine>> ExpectedTM =
543-
codegen::createTargetMachineForTriple(TripleStr, GetCodeGenOptLevel());
544-
if (!ExpectedTM) {
545-
errs() << argv[0] << ": warning: failed to infer data layout: "
546-
<< toString(ExpectedTM.takeError()) << "\n";
541+
542+
Triple TT(TripleStr);
543+
544+
std::string Str = TT.computeDataLayout();
545+
if (Str.empty()) {
546+
errs() << argv[0]
547+
<< ": warning: failed to infer data layout from target triple\n";
547548
return std::nullopt;
548549
}
549-
return (*ExpectedTM)->createDataLayout().getStringRepresentation();
550+
return Str;
550551
};
551552
std::unique_ptr<Module> M;
552553
if (NoUpgradeDebugInfo)

0 commit comments

Comments
 (0)