diff --git a/llvm/include/llvm/AsmParser/LLParser.h b/llvm/include/llvm/AsmParser/LLParser.h --- a/llvm/include/llvm/AsmParser/LLParser.h +++ b/llvm/include/llvm/AsmParser/LLParser.h @@ -180,7 +180,7 @@ BlockAddressPFS(nullptr) {} bool Run( bool UpgradeDebugInfo, DataLayoutCallbackTy DataLayoutCallback = - [](StringRef) { return std::nullopt; }); + [](const Module &) { return std::nullopt; }); bool parseStandaloneConstantValue(Constant *&C, const SlotMapping *Slots); diff --git a/llvm/include/llvm/AsmParser/Parser.h b/llvm/include/llvm/AsmParser/Parser.h --- a/llvm/include/llvm/AsmParser/Parser.h +++ b/llvm/include/llvm/AsmParser/Parser.h @@ -29,7 +29,7 @@ class SMDiagnostic; class Type; -typedef llvm::function_ref(StringRef)> +typedef llvm::function_ref(const Module &)> DataLayoutCallbackTy; /// This function is a main interface to the LLVM Assembly Parser. It parses @@ -86,7 +86,7 @@ ParsedModuleAndIndex parseAssemblyFileWithIndex( StringRef Filename, SMDiagnostic &Err, LLVMContext &Context, SlotMapping *Slots = nullptr, - DataLayoutCallbackTy DataLayoutCallback = [](StringRef) { + DataLayoutCallbackTy DataLayoutCallback = [](const Module &) { return std::nullopt; }); @@ -127,7 +127,7 @@ std::unique_ptr parseAssembly( MemoryBufferRef F, SMDiagnostic &Err, LLVMContext &Context, SlotMapping *Slots = nullptr, - DataLayoutCallbackTy DataLayoutCallback = [](StringRef) { + DataLayoutCallbackTy DataLayoutCallback = [](const Module &) { return std::nullopt; }); @@ -169,7 +169,7 @@ bool parseAssemblyInto( MemoryBufferRef F, Module *M, ModuleSummaryIndex *Index, SMDiagnostic &Err, SlotMapping *Slots = nullptr, - DataLayoutCallbackTy DataLayoutCallback = [](StringRef) { + DataLayoutCallbackTy DataLayoutCallback = [](const Module &) { return std::nullopt; }); diff --git a/llvm/include/llvm/Bitcode/BitcodeReader.h b/llvm/include/llvm/Bitcode/BitcodeReader.h --- a/llvm/include/llvm/Bitcode/BitcodeReader.h +++ b/llvm/include/llvm/Bitcode/BitcodeReader.h @@ -34,20 +34,20 @@ class MemoryBuffer; class ModuleSummaryIndex; -typedef llvm::function_ref(StringRef)> +typedef llvm::function_ref(const Module &)> DataLayoutCallbackTy; - // These functions are for converting Expected/Error values to - // ErrorOr/std::error_code for compatibility with legacy clients. FIXME: - // Remove these functions once no longer needed by the C and libLTO APIs. +// These functions are for converting Expected/Error values to +// ErrorOr/std::error_code for compatibility with legacy clients. FIXME: +// Remove these functions once no longer needed by the C and libLTO APIs. - std::error_code errorToErrorCodeAndEmitErrors(LLVMContext &Ctx, Error Err); +std::error_code errorToErrorCodeAndEmitErrors(LLVMContext &Ctx, Error Err); - template - ErrorOr expectedToErrorOrAndEmitErrors(LLVMContext &Ctx, Expected Val) { - if (!Val) - return errorToErrorCodeAndEmitErrors(Ctx, Val.takeError()); - return std::move(*Val); +template +ErrorOr expectedToErrorOrAndEmitErrors(LLVMContext &Ctx, Expected Val) { + if (!Val) + return errorToErrorCodeAndEmitErrors(Ctx, Val.takeError()); + return std::move(*Val); } struct BitcodeFileContents; @@ -108,7 +108,7 @@ /// Read the entire bitcode module and return it. Expected> parseModule( LLVMContext &Context, DataLayoutCallbackTy DataLayoutCallback = - [](StringRef) { return std::nullopt; }); + [](const Module &) { return std::nullopt; }); /// Returns information about the module to be used for LTO: whether to /// compile with ThinLTO, and whether it has a summary. @@ -175,7 +175,7 @@ /// Read the specified bitcode file, returning the module. Expected> parseBitcodeFile( MemoryBufferRef Buffer, LLVMContext &Context, - DataLayoutCallbackTy DataLayoutCallback = [](StringRef) { + DataLayoutCallbackTy DataLayoutCallback = [](const Module &) { return std::nullopt; }); diff --git a/llvm/include/llvm/CodeGen/MIRParser/MIRParser.h b/llvm/include/llvm/CodeGen/MIRParser/MIRParser.h --- a/llvm/include/llvm/CodeGen/MIRParser/MIRParser.h +++ b/llvm/include/llvm/CodeGen/MIRParser/MIRParser.h @@ -34,7 +34,7 @@ class SMDiagnostic; class StringRef; -typedef llvm::function_ref(StringRef)> +typedef llvm::function_ref(const Module &)> DataLayoutCallbackTy; /// This class initializes machine functions by applying the state loaded from @@ -52,7 +52,7 @@ /// A new, empty module is created if the LLVM IR isn't present. /// \returns nullptr if a parsing error occurred. std::unique_ptr - parseIRModule(DataLayoutCallbackTy DataLayoutCallback = [](StringRef) { + parseIRModule(DataLayoutCallbackTy DataLayoutCallback = [](const Module &) { return std::nullopt; }); diff --git a/llvm/include/llvm/IRReader/IRReader.h b/llvm/include/llvm/IRReader/IRReader.h --- a/llvm/include/llvm/IRReader/IRReader.h +++ b/llvm/include/llvm/IRReader/IRReader.h @@ -27,7 +27,7 @@ class SMDiagnostic; class LLVMContext; -typedef llvm::function_ref(StringRef)> +typedef llvm::function_ref(const Module &)> DataLayoutCallbackTy; /// If the given MemoryBuffer holds a bitcode image, return a Module @@ -55,7 +55,7 @@ /// \param DataLayoutCallback Override datalayout in the llvm assembly. std::unique_ptr parseIR( MemoryBufferRef Buffer, SMDiagnostic &Err, LLVMContext &Context, - DataLayoutCallbackTy DataLayoutCallback = [](StringRef) { + DataLayoutCallbackTy DataLayoutCallback = [](const Module &) { return std::nullopt; }); @@ -65,7 +65,7 @@ /// \param DataLayoutCallback Override datalayout in the llvm assembly. std::unique_ptr parseIRFile( StringRef Filename, SMDiagnostic &Err, LLVMContext &Context, - DataLayoutCallbackTy DataLayoutCallback = [](StringRef) { + DataLayoutCallbackTy DataLayoutCallback = [](const Module &) { return std::nullopt; }); } diff --git a/llvm/lib/AsmParser/LLParser.cpp b/llvm/lib/AsmParser/LLParser.cpp --- a/llvm/lib/AsmParser/LLParser.cpp +++ b/llvm/lib/AsmParser/LLParser.cpp @@ -98,7 +98,7 @@ if (parseTargetDefinitions()) return true; - if (auto LayoutOverride = DataLayoutCallback(M->getTargetTriple())) + if (auto LayoutOverride = DataLayoutCallback(*M)) M->setDataLayout(*LayoutOverride); } diff --git a/llvm/lib/AsmParser/Parser.cpp b/llvm/lib/AsmParser/Parser.cpp --- a/llvm/lib/AsmParser/Parser.cpp +++ b/llvm/lib/AsmParser/Parser.cpp @@ -93,7 +93,7 @@ SlotMapping *Slots) { return ::parseAssemblyWithIndex(F, Err, Context, Slots, /*UpgradeDebugInfo*/ true, - [](StringRef) { return std::nullopt; }); + [](const Module &) { return std::nullopt; }); } static ParsedModuleAndIndex @@ -150,7 +150,7 @@ // index, but we need to initialize it. LLVMContext unusedContext; return LLParser(F.getBuffer(), SM, Err, nullptr, &Index, unusedContext) - .Run(true, [](StringRef) { return std::nullopt; }); + .Run(true, [](const Module &) { return std::nullopt; }); } std::unique_ptr diff --git a/llvm/lib/Bitcode/Reader/BitcodeReader.cpp b/llvm/lib/Bitcode/Reader/BitcodeReader.cpp --- a/llvm/lib/Bitcode/Reader/BitcodeReader.cpp +++ b/llvm/lib/Bitcode/Reader/BitcodeReader.cpp @@ -821,7 +821,7 @@ Error parseAttrKind(uint64_t Code, Attribute::AttrKind *Kind); Error parseModule( uint64_t ResumeBit, bool ShouldLazyLoadMetadata = false, - DataLayoutCallbackTy DataLayoutCallback = [](StringRef) { + DataLayoutCallbackTy DataLayoutCallback = [](const Module &) { return std::nullopt; }); @@ -4204,8 +4204,7 @@ TheModule->getDataLayoutStr(), TheModule->getTargetTriple()); TheModule->setDataLayout(DL); - if (auto LayoutOverride = - DataLayoutCallback(TheModule->getTargetTriple())) + if (auto LayoutOverride = DataLayoutCallback(*TheModule)) TheModule->setDataLayout(*LayoutOverride); }; @@ -7944,7 +7943,7 @@ BitcodeModule::getLazyModule(LLVMContext &Context, bool ShouldLazyLoadMetadata, bool IsImporting) { return getModuleImpl(Context, false, ShouldLazyLoadMetadata, IsImporting, - [](StringRef) { return std::nullopt; }); + [](const Module &) { return std::nullopt; }); } // Parse the specified bitcode buffer and merge the index into CombinedIndex. diff --git a/llvm/lib/CodeGen/MIRParser/MIRParser.cpp b/llvm/lib/CodeGen/MIRParser/MIRParser.cpp --- a/llvm/lib/CodeGen/MIRParser/MIRParser.cpp +++ b/llvm/lib/CodeGen/MIRParser/MIRParser.cpp @@ -234,7 +234,7 @@ // Create an empty module when the MIR file is empty. NoMIRDocuments = true; auto M = std::make_unique(Filename, Context); - if (auto LayoutOverride = DataLayoutCallback(M->getTargetTriple())) + if (auto LayoutOverride = DataLayoutCallback(*M)) M->setDataLayout(*LayoutOverride); return M; } @@ -257,7 +257,7 @@ } else { // Create an new, empty module. M = std::make_unique(Filename, Context); - if (auto LayoutOverride = DataLayoutCallback(M->getTargetTriple())) + if (auto LayoutOverride = DataLayoutCallback(*M)) M->setDataLayout(*LayoutOverride); NoLLVMIR = true; } diff --git a/llvm/tools/llc/llc.cpp b/llvm/tools/llc/llc.cpp --- a/llvm/tools/llc/llc.cpp +++ b/llvm/tools/llc/llc.cpp @@ -530,10 +530,12 @@ // If user just wants to list available options, skip module loading if (!SkipModule) { - auto SetDataLayout = - [&](StringRef DataLayoutTargetTriple) -> std::optional { + auto SetDataLayout = [&](const Module &M) -> std::optional { // If we are supposed to override the target triple, do so now. - std::string IRTargetTriple = DataLayoutTargetTriple.str(); + // TODO: it would be nice to use codegen::inferDataLayoutOverride() to + // avoid code duplication, but this lambda has additional externally + // visible side effects such as setting up the target and TargetOptions. + std::string IRTargetTriple = M.getTargetTriple(); if (!TargetTriple.empty()) IRTargetTriple = Triple::normalize(TargetTriple); TheTriple = Triple(IRTargetTriple); diff --git a/llvm/tools/llvm-as/llvm-as.cpp b/llvm/tools/llvm-as/llvm-as.cpp --- a/llvm/tools/llvm-as/llvm-as.cpp +++ b/llvm/tools/llvm-as/llvm-as.cpp @@ -121,7 +121,7 @@ // Parse the file now... SMDiagnostic Err; - auto SetDataLayout = [](StringRef) -> std::optional { + auto SetDataLayout = [](const Module &) -> std::optional { if (ClDataLayout.empty()) return std::nullopt; return ClDataLayout; diff --git a/llvm/tools/llvm-reduce/ReducerWorkItem.cpp b/llvm/tools/llvm-reduce/ReducerWorkItem.cpp --- a/llvm/tools/llvm-reduce/ReducerWorkItem.cpp +++ b/llvm/tools/llvm-reduce/ReducerWorkItem.cpp @@ -407,12 +407,11 @@ std::unique_ptr MParser = createMIRParser(std::move(FileOrErr.get()), Ctxt); - auto SetDataLayout = - [&](StringRef DataLayoutTargetTriple) -> Optional { + auto SetDataLayout = [&](const Module &M) -> Optional { // NB: We always call createTargetMachineForTriple() even if an explicit // DataLayout is already set in the module since we want to use this // callback to setup the TargetMachine rather than doing it later. - std::string IRTargetTriple = DataLayoutTargetTriple.str(); + std::string IRTargetTriple = M.getTargetTriple(); if (!TargetTriple.empty()) IRTargetTriple = Triple::normalize(TargetTriple); TheTriple = Triple(IRTargetTriple); @@ -420,7 +419,10 @@ TheTriple.setTriple(sys::getDefaultTargetTriple()); ExitOnError ExitOnErr(std::string(ToolName) + ": error: "); TM = ExitOnErr(codegen::createTargetMachineForTriple(TheTriple.str())); - + // If an explicit data layout already defined, we do not use the one from + // the TargetMachine. + if (!M.getDataLayout().isDefault()) + return None; return TM->createDataLayout().getStringRepresentation(); }; diff --git a/llvm/tools/opt/opt.cpp b/llvm/tools/opt/opt.cpp --- a/llvm/tools/opt/opt.cpp +++ b/llvm/tools/opt/opt.cpp @@ -509,7 +509,7 @@ std::unique_ptr RemarksFile = std::move(*RemarksFileOrErr); // Load the input module... - auto SetDataLayout = [](StringRef) -> std::optional { + auto SetDataLayout = [](const Module &) -> std::optional { if (ClDataLayout.empty()) return std::nullopt; return ClDataLayout;