Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions llvm/test/tools/opt/invalid-target.ll
Original file line number Diff line number Diff line change
Expand Up @@ -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:
15 changes: 8 additions & 7 deletions llvm/tools/opt/optdriver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Copy link
Member

@arichardson arichardson Nov 26, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we also pass the value of the --target-abi= command line option here? Since depending on the ABI, there could be a different data layouts.
For testing this we could instantiate opt with e.g. MIPS n32 ABI or one of the ARM ABI variants that affect the data layout string.

I know this is a functional change rather than NFC, so also fine to defer until later.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's also apparently a target-abi module flag, and this doesn't seem like it's all handled correctly

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah it seems to be mostly ignored which causes a bunch of issues with LTO (especially on RISC-V) such as #50591 / #69780. Probably easiest to ignore that attribute for now and only look at command line flags.

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)
Expand Down
Loading