[llvm] [DTLTO] Serialize of LTO Config (PR #219894)

Konstantin Belochapka via llvm-commits llvm-commits at lists.llvm.org
Mon Sep 7 05:23:51 PDT 2026


https://github.com/kbelochapka updated https://github.com/llvm/llvm-project/pull/219894

>From 0c6bf5d82b512243d2233a4ee1999ebe8df87182 Mon Sep 17 00:00:00 2001
From: Konstantin Belochapka <konstantin.belochapka at sony.com>
Date: Mon, 7 Sep 2026 04:02:33 -0700
Subject: [PATCH] [LTO] Preserve unsigned option values in bitcode

---
 llvm/include/llvm/LTO/Config.def              | 128 +++++++
 .../llvm/Passes/PipelineTuningOptions.def     |  70 ++++
 llvm/include/llvm/Target/TargetOptions.def    | 199 ++++++++++
 llvm/include/llvm/Target/TargetOptions.h      | 305 +++------------
 llvm/lib/LTO/BitcodeMetadataUtils.h           | 247 ++++++++++++
 llvm/lib/LTO/LTOConfigBitcode.cpp             | 355 ++++++++++++++++++
 llvm/lib/LTO/TargetOptionsBitcode.cpp         | 265 +++++++++++++
 llvm/unittests/LTO/LTOConfigBitcodeTest.cpp   | 181 +++++++++
 8 files changed, 1497 insertions(+), 253 deletions(-)
 create mode 100644 llvm/include/llvm/LTO/Config.def
 create mode 100644 llvm/include/llvm/Passes/PipelineTuningOptions.def
 create mode 100644 llvm/include/llvm/Target/TargetOptions.def
 create mode 100644 llvm/lib/LTO/BitcodeMetadataUtils.h
 create mode 100644 llvm/lib/LTO/LTOConfigBitcode.cpp
 create mode 100644 llvm/lib/LTO/TargetOptionsBitcode.cpp
 create mode 100644 llvm/unittests/LTO/LTOConfigBitcodeTest.cpp

diff --git a/llvm/include/llvm/LTO/Config.def b/llvm/include/llvm/LTO/Config.def
new file mode 100644
index 0000000000000..c3bbf6805ed3f
--- /dev/null
+++ b/llvm/include/llvm/LTO/Config.def
@@ -0,0 +1,128 @@
+//===-- Config.def - LTO configuration database ---------------*- C++ -*-===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+//
+// This file defines the fields stored in llvm::lto::Config. The Kind argument
+// identifies how a field is serialized; NONE denotes process-local state that
+// is intentionally omitted from serialized configurations.
+//
+// Users must define LTO_CONFIG_OPTION and LTO_CONFIG_MUTABLE_OPTION before
+// including this file.
+//
+// NOTE: NO INCLUDE GUARD DESIRED!
+//
+//===----------------------------------------------------------------------===//
+
+#if !(defined(LTO_CONFIG_OPTION) && defined(LTO_CONFIG_MUTABLE_OPTION))
+#error "Define the LTO_CONFIG_* macros before including Config.def"
+#endif
+
+LTO_CONFIG_OPTION(std::string, CPU, {}, STRING)
+LTO_CONFIG_OPTION(TargetOptions, Options, {}, TARGET_OPTIONS)
+LTO_CONFIG_OPTION(std::vector<std::string>, MAttrs, {}, STRING_LIST)
+LTO_CONFIG_OPTION(std::vector<std::string>, MllvmArgs, {}, STRING_LIST)
+
+/// Process-local pass plugins, intentionally not serialized.
+LTO_CONFIG_OPTION(std::vector<llvm::PassPlugin *>, LoadedPassPlugins, {}, NONE)
+
+LTO_CONFIG_OPTION(std::vector<std::string>, PassPluginFilenames, {},
+                  STRING_LIST)
+
+/// Process-local callback, intentionally not serialized.
+LTO_CONFIG_OPTION(std::function<void(legacy::PassManager &)>,
+                  PreCodeGenPassesHook, {}, NONE)
+
+LTO_CONFIG_OPTION(std::optional<Reloc::Model>, RelocModel, Reloc::PIC_,
+                  OPTIONAL_RELOC_MODEL)
+LTO_CONFIG_OPTION(std::optional<CodeModel::Model>, CodeModel, std::nullopt,
+                  OPTIONAL_ENUM)
+LTO_CONFIG_OPTION(CodeGenOptLevel, CGOptLevel, CodeGenOptLevel::Default, ENUM)
+LTO_CONFIG_OPTION(CodeGenFileType, CGFileType, CodeGenFileType::ObjectFile,
+                  ENUM)
+LTO_CONFIG_OPTION(unsigned, OptLevel, 2, U32)
+LTO_CONFIG_OPTION(bool, VerifyEach, false, BOOL)
+LTO_CONFIG_OPTION(bool, DisableVerify, false, BOOL)
+
+/// Do not assume that target builtins are present.
+LTO_CONFIG_OPTION(bool, Freestanding, false, BOOL)
+
+/// Disable the optimizer, including importing for ThinLTO.
+LTO_CONFIG_OPTION(bool, CodeGenOnly, false, BOOL)
+
+/// Run context-sensitive PGO IR instrumentation.
+LTO_CONFIG_OPTION(bool, RunCSIRInstr, false, BOOL)
+
+/// Warn about hash mismatches in PGO profile data.
+LTO_CONFIG_OPTION(bool, PGOWarnMismatch, true, BOOL)
+
+LTO_CONFIG_OPTION(bool, HasWholeProgramVisibility, false, BOOL)
+LTO_CONFIG_OPTION(bool, ValidateAllVtablesHaveTypeInfos, false, BOOL)
+LTO_CONFIG_OPTION(bool, AllVtablesHaveTypeInfos, false, BOOL)
+LTO_CONFIG_OPTION(bool, AlwaysEmitRegularLTOObj, false, BOOL)
+LTO_CONFIG_OPTION(bool, KeepSymbolNameCopies, true, BOOL)
+
+/// Distinguish in-process and out-of-process DTLTO cache entries.
+LTO_CONFIG_MUTABLE_OPTION(bool, Dtlto, 0, BOOL)
+
+LTO_CONFIG_OPTION(Config::VisScheme, VisibilityScheme, FromPrevailing, ENUM)
+
+LTO_CONFIG_OPTION(std::string, OptPipeline, {}, STRING)
+LTO_CONFIG_OPTION(std::string, AAPipeline, {}, STRING)
+LTO_CONFIG_OPTION(std::string, OverrideTriple, {}, STRING)
+LTO_CONFIG_OPTION(std::string, DefaultTriple, {}, STRING)
+LTO_CONFIG_OPTION(std::string, CSIRProfile, {}, STRING)
+LTO_CONFIG_OPTION(std::string, SampleProfile, {}, STRING)
+LTO_CONFIG_OPTION(std::string, ProfileRemapping, {}, STRING)
+LTO_CONFIG_OPTION(std::string, DwoDir, {}, STRING)
+LTO_CONFIG_OPTION(std::string, SplitDwarfFile, {}, STRING)
+LTO_CONFIG_OPTION(std::string, SplitDwarfOutput, {}, STRING)
+LTO_CONFIG_OPTION(std::string, RemarksFilename, {}, STRING)
+LTO_CONFIG_OPTION(std::string, RemarksPasses, {}, STRING)
+LTO_CONFIG_OPTION(bool, RemarksWithHotness, false, BOOL)
+
+/// Optional remarks threshold with disabled, manual, and automatic modes.
+LTO_CONFIG_OPTION(std::optional<uint64_t>, RemarksHotnessThreshold, 0,
+                  REMARKS_HOTNESS)
+
+LTO_CONFIG_OPTION(std::string, RemarksFormat, {}, STRING)
+LTO_CONFIG_OPTION(bool, DebugPassManager, false, BOOL)
+LTO_CONFIG_OPTION(std::string, StatsFile, {}, STRING)
+LTO_CONFIG_OPTION(std::vector<std::string>, ThinLTOModulesToCompile, {},
+                  STRING_LIST)
+LTO_CONFIG_OPTION(bool, TimeTraceEnabled, false, BOOL)
+LTO_CONFIG_OPTION(unsigned, TimeTraceGranularity, 500, U32)
+LTO_CONFIG_OPTION(bool, ShouldDiscardValueNames, true, BOOL)
+
+/// Process-local diagnostic callback, intentionally not serialized.
+LTO_CONFIG_OPTION(DiagnosticHandlerFunction, DiagHandler, {}, NONE)
+
+LTO_CONFIG_OPTION(bool, AddFSDiscriminator, false, BOOL)
+
+/// Process-local output stream, intentionally not serialized.
+LTO_CONFIG_OPTION(std::unique_ptr<raw_ostream>, ResolutionFile, {}, NONE)
+
+LTO_CONFIG_OPTION(PipelineTuningOptions, PTO, {}, PIPELINE_TUNING_OPTIONS)
+
+/// Process-local module hooks, intentionally not serialized.
+LTO_CONFIG_OPTION(ModuleHookFn, PreOptModuleHook, {}, NONE)
+LTO_CONFIG_OPTION(ModuleHookFn, PostPromoteModuleHook, {}, NONE)
+LTO_CONFIG_OPTION(ModuleHookFn, PostInternalizeModuleHook, {}, NONE)
+LTO_CONFIG_OPTION(ModuleHookFn, PostImportModuleHook, {}, NONE)
+LTO_CONFIG_OPTION(ModuleHookFn, PostOptModuleHook, {}, NONE)
+LTO_CONFIG_OPTION(ModuleHookFn, PreCodeGenModuleHook, {}, NONE)
+LTO_CONFIG_OPTION(CombinedIndexHookFn, CombinedIndexHook, {}, NONE)
+
+/// Process-local backend callbacks, intentionally not serialized.
+LTO_CONFIG_OPTION(std::function<std::unique_ptr<raw_pwrite_stream>(size_t)>,
+                  GetSummaryIndexOutputStream, {}, NONE)
+LTO_CONFIG_OPTION(std::function<std::vector<std::string> &(size_t)>,
+                  GetImportsListOutputArray, {}, NONE)
+LTO_CONFIG_OPTION(std::function<std::string &(size_t)>, GetCacheKeyOutputString,
+                  {}, NONE)
+
+#undef LTO_CONFIG_OPTION
+#undef LTO_CONFIG_MUTABLE_OPTION
diff --git a/llvm/include/llvm/Passes/PipelineTuningOptions.def b/llvm/include/llvm/Passes/PipelineTuningOptions.def
new file mode 100644
index 0000000000000..1bcbec54f8491
--- /dev/null
+++ b/llvm/include/llvm/Passes/PipelineTuningOptions.def
@@ -0,0 +1,70 @@
+//===-- PipelineTuningOptions.def - Pipeline tuning database ---*- C++ -*-===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+//
+// This file defines the options stored in llvm::PipelineTuningOptions. Users
+// must define the PIPELINE_TUNING_OPTION macro before including this file.
+//
+// NOTE: NO INCLUDE GUARD DESIRED!
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef PIPELINE_TUNING_OPTION
+#error                                                                         \
+    "Define PIPELINE_TUNING_OPTION before including PipelineTuningOptions.def"
+#endif
+
+/// Enable loop interleaving, subject to the optimization level.
+PIPELINE_TUNING_OPTION(bool, LoopInterleaving, true, BOOL)
+
+/// Enable loop vectorization, subject to the optimization level.
+PIPELINE_TUNING_OPTION(bool, LoopVectorization, true, BOOL)
+
+/// Enable SLP vectorization, subject to the optimization level.
+PIPELINE_TUNING_OPTION(bool, SLPVectorization, false, BOOL)
+
+/// Enable loop unrolling.
+PIPELINE_TUNING_OPTION(bool, LoopUnrolling, true, BOOL)
+
+/// Enable loop interchange.
+PIPELINE_TUNING_OPTION(bool, LoopInterchange, EnableLoopInterchange, BOOL)
+
+/// Enable loop fusion.
+PIPELINE_TUNING_OPTION(bool, LoopFusion, false, BOOL)
+
+/// Forget all SCEV loops during loop unrolling.
+PIPELINE_TUNING_OPTION(bool, ForgetAllSCEVInLoopUnroll, ForgetSCEVInLoopUnroll,
+                       BOOL)
+
+/// Cap MemorySSA clobbering-access queries in LICM.
+PIPELINE_TUNING_OPTION(unsigned, LicmMssaOptCap, SetLicmMssaOptCap, U32)
+
+/// Disable LICM scalar promotion when the number of accesses is too large.
+PIPELINE_TUNING_OPTION(unsigned, LicmMssaNoAccForPromotionCap,
+                       SetLicmMssaNoAccForPromotionCap, U32)
+
+/// Enable call-graph profiling.
+PIPELINE_TUNING_OPTION(bool, CallGraphProfile, true, BOOL)
+
+/// Enable the unified LTO pipeline.
+PIPELINE_TUNING_OPTION(bool, UnifiedLTO, false, BOOL)
+
+/// Enable function merging.
+PIPELINE_TUNING_OPTION(bool, MergeFunctions, EnableMergeFunctions, BOOL)
+
+/// Override the default inliner threshold.
+PIPELINE_TUNING_OPTION(int, InlinerThreshold, -1, I32)
+
+/// Eagerly invalidate analyses to potentially reduce peak memory usage.
+PIPELINE_TUNING_OPTION(bool, EagerlyInvalidateAnalyses,
+                       EnableEagerlyInvalidateAnalyses, BOOL)
+
+/// Enable speculative devirtualization.
+PIPELINE_TUNING_OPTION(bool, DevirtualizeSpeculatively,
+                       EnableDevirtualizeSpeculatively, BOOL)
+
+#undef PIPELINE_TUNING_OPTION
diff --git a/llvm/include/llvm/Target/TargetOptions.def b/llvm/include/llvm/Target/TargetOptions.def
new file mode 100644
index 0000000000000..9583a1a780937
--- /dev/null
+++ b/llvm/include/llvm/Target/TargetOptions.def
@@ -0,0 +1,199 @@
+//===-- TargetOptions.def - Target option database ------------*- C++ -*-===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+//
+// This file defines the options stored in llvm::TargetOptions. Users of this
+// file must define TARGET_OPTION before including it.
+//
+// NOTE: NO INCLUDE GUARD DESIRED!
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef TARGET_OPTION
+#error "Define TARGET_OPTION before including TargetOptions.def"
+#endif
+
+/// Enable the extended Altivec ABI on AIX.
+TARGET_OPTION((unsigned), EnableAIXExtendedAltivecABI, 1, false, BOOL)
+
+/// Honor a dynamically changing, sign-dependent floating-point rounding mode.
+TARGET_OPTION((unsigned), HonorSignDependentRoundingFPMathOption, 1, false,
+              BOOL)
+
+/// Do not place zero-initialized data in the BSS section.
+TARGET_OPTION((unsigned), NoZerosInBSS, 1, false, BOOL)
+
+/// Perform guaranteed tail-call optimization for eligible fastcc calls.
+TARGET_OPTION((unsigned), GuaranteedTailCallOpt, 1, false, BOOL)
+
+/// Allow CodeGen to order local stack symbols.
+TARGET_OPTION((unsigned), StackSymbolOrdering, 1, true, BOOL)
+
+/// Enable fast-path instruction selection.
+TARGET_OPTION((unsigned), EnableFastISel, 1, false, BOOL)
+
+/// Enable global instruction selection.
+TARGET_OPTION((unsigned), EnableGlobalISel, 1, false, BOOL)
+
+/// Control abort behavior when global instruction selection fails.
+TARGET_OPTION((GlobalISelAbortMode), GlobalISelAbort, 0,
+              GlobalISelAbortMode::Enable, ENUM)
+
+/// Control when and how the Swift async frame pointer bit is set.
+TARGET_OPTION((SwiftAsyncFramePointerMode), SwiftAsyncFramePointer, 0,
+              SwiftAsyncFramePointerMode::Always, ENUM)
+
+/// Use .init_array instead of .ctors for static constructors.
+TARGET_OPTION((unsigned), UseInitArray, 1, false, BOOL)
+
+/// Emit functions into separate sections.
+TARGET_OPTION((unsigned), FunctionSections, 1, false, BOOL)
+
+/// Emit data into separate sections.
+TARGET_OPTION((unsigned), DataSections, 1, false, BOOL)
+
+/// Do not emit visibility attributes for XCOFF.
+TARGET_OPTION((unsigned), IgnoreXCOFFVisibility, 1, false, BOOL)
+
+/// Emit the XCOFF traceback table.
+TARGET_OPTION((unsigned), XCOFFTracebackTable, 1, true, BOOL)
+
+TARGET_OPTION((unsigned), UniqueSectionNames, 1, true, BOOL)
+
+/// Use unique names for basic block sections.
+TARGET_OPTION((unsigned), UniqueBasicBlockSectionNames, 1, false, BOOL)
+
+/// Emit named sections with the same name into different sections.
+TARGET_OPTION((unsigned), SeparateNamedSections, 1, false, BOOL)
+
+/// Emit a target-specific trap instruction for unreachable IR instructions.
+TARGET_OPTION((unsigned), TrapUnreachable, 1, false, BOOL)
+
+/// Do not emit a trap after noreturn calls even when TrapUnreachable is true.
+TARGET_OPTION((unsigned), NoTrapAfterNoreturn, 1, false, BOOL)
+
+/// Bit size of immediate TLS offsets (0 means use the default).
+TARGET_OPTION((unsigned), TLSSize, 8, 0, U32_BITFIELD)
+
+/// Enable the emulated TLS model.
+TARGET_OPTION((unsigned), EmulatedTLS, 1, false, BOOL)
+
+/// Enable TLS descriptors.
+TARGET_OPTION((unsigned), EnableTLSDESC, 1, false, BOOL)
+
+/// Enable interprocedural register allocation.
+TARGET_OPTION((unsigned), EnableIPRA, 1, false, BOOL)
+
+/// Emit a section containing function stack-size metadata.
+TARGET_OPTION((unsigned), EmitStackSizeSection, 1, false, BOOL)
+
+/// Enable the MachineOutliner pass.
+TARGET_OPTION((unsigned), EnableMachineOutliner, 1, false, BOOL)
+
+/// Enable the MachineFunctionSplitter pass.
+TARGET_OPTION((unsigned), EnableMachineFunctionSplitter, 1, false, BOOL)
+
+/// Enable the StaticDataSplitter pass.
+TARGET_OPTION((unsigned), EnableStaticDataPartitioning, 1, false, BOOL)
+
+/// Set if the target supports default outlining behavior.
+TARGET_OPTION((unsigned), SupportsDefaultOutlining, 1, false, BOOL)
+
+/// Enable the machine verifier at the end of default NPM CodeGen pipelines.
+TARGET_OPTION((unsigned), EnableDefaultMachineVerifier, 1, true, BOOL)
+
+/// Emit an address-significance table.
+TARGET_OPTION((unsigned), EmitAddrsig, 1, false, BOOL)
+
+/// Emit the SHT_LLVM_BB_ADDR_MAP section.
+TARGET_OPTION((unsigned), BBAddrMap, 1, false, BOOL)
+
+/// Select which basic blocks are emitted into separate sections.
+TARGET_OPTION((BasicBlockSection), BBSections, 0, BasicBlockSection::None, ENUM)
+
+/// Sampled basic-block information used to select basic block sections.
+TARGET_OPTION((std::shared_ptr<MemoryBuffer>), BBSectionsFuncListBuf, 0, {},
+              BUFFER)
+
+/// Emit a section containing call-graph metadata.
+TARGET_OPTION((unsigned), EmitCallGraphSection, 1, false, BOOL)
+
+/// Enable call-site information production.
+TARGET_OPTION((unsigned), EmitCallSiteInfo, 1, false, BOOL)
+
+/// Set if the target supports debug entry values by default.
+TARGET_OPTION((unsigned), SupportsDebugEntryValues, 1, false, BOOL)
+
+/// Force production of debug entry values (for testing only).
+TARGET_OPTION((unsigned), EnableDebugEntryValues, 1, false, BOOL)
+
+/// Use experimental value-tracking variable locations.
+TARGET_OPTION((unsigned), ValueTrackingVariableLocations, 1, false, BOOL)
+
+/// Emit the DWARF debug frame section.
+TARGET_OPTION((unsigned), ForceDwarfFrameSection, 1, false, BOOL)
+
+/// Emit the XRay function index section.
+TARGET_OPTION((unsigned), XRayFunctionIndex, 1, true, BOOL)
+
+/// Do not use DWARF extensions in later DWARF versions.
+TARGET_OPTION((unsigned), DebugStrictDwarf, 1, false, BOOL)
+
+/// Emit the hotpatch flag in CodeView debug information.
+TARGET_OPTION((unsigned), Hotpatch, 1, false, BOOL)
+
+/// Enable scalar MASS conversions.
+TARGET_OPTION((unsigned), PPCGenScalarMASSEntries, 1, false, BOOL)
+
+/// Enable JustMyCode instrumentation.
+TARGET_OPTION((unsigned), JMCInstrument, 1, false, BOOL)
+
+/// Enable the CFIFixup pass.
+TARGET_OPTION((unsigned), EnableCFIFixup, 1, false, BOOL)
+
+/// Enable MisExpect diagnostics.
+TARGET_OPTION((unsigned), MisExpect, 1, false, BOOL)
+
+/// Put const objects with relocatable addresses in XCOFF read-only data.
+TARGET_OPTION((unsigned), XCOFFReadOnlyPointers, 1, false, BOOL)
+
+/// Verify narrow-integer call/return argument extensions in target backends.
+TARGET_OPTION((unsigned), VerifyArgABICompliance, 1, true, BOOL)
+
+/// Name of the stack-usage output file.
+TARGET_OPTION((std::string), StackUsageFile, 0, {}, STRING)
+
+/// Override TargetLoweringBase::PrefLoopAlignment when greater than zero.
+TARGET_OPTION((unsigned), LoopAlignment, 0, 0, U32)
+
+/// Control formation of fused floating-point operations.
+TARGET_OPTION((FPOpFusion::FPOpFusionMode), AllowFPOpFusion, 0,
+              FPOpFusion::Standard, ENUM)
+
+/// Threading model assumed for operations such as atomics.
+TARGET_OPTION((ThreadModel::Model), ThreadModel, 0, ThreadModel::POSIX, ENUM)
+
+/// EABI version.
+TARGET_OPTION((EABI), EABIVersion, 0, EABI::Default, ENUM)
+
+/// Debugger for which debug information is tuned.
+TARGET_OPTION((DebuggerKind), DebuggerTuning, 0, DebuggerKind::Default, ENUM)
+
+/// Vector math library to use.
+TARGET_OPTION((VectorLibrary), VecLib, 0, VectorLibrary::NoLibrary, ENUM)
+
+/// Exception-handling model to use.
+TARGET_OPTION((ExceptionHandling), ExceptionModel, 0, ExceptionHandling::None,
+              ENUM)
+
+/// Machine-level options.
+TARGET_OPTION((MCTargetOptions), MCOptions, 0, {}, MC)
+
+/// Final object filename/path to record in CodeView debug information.
+TARGET_OPTION((std::string), ObjectFilenameForDebug, 0, {}, STRING)
+
+#undef TARGET_OPTION
diff --git a/llvm/include/llvm/Target/TargetOptions.h b/llvm/include/llvm/Target/TargetOptions.h
index 71e7b17ba3bd8..5de01237dfa6a 100644
--- a/llvm/include/llvm/Target/TargetOptions.h
+++ b/llvm/include/llvm/Target/TargetOptions.h
@@ -21,6 +21,7 @@
 #include "llvm/Support/Compiler.h"
 
 #include <memory>
+#include <string>
 
 namespace llvm {
 struct fltSemantics;
@@ -118,266 +119,64 @@ enum CodeObjectVersionKind {
 
 class TargetOptions {
 public:
-  TargetOptions()
-      : EnableAIXExtendedAltivecABI(false),
-        HonorSignDependentRoundingFPMathOption(false), NoZerosInBSS(false),
-        GuaranteedTailCallOpt(false), StackSymbolOrdering(true),
-        EnableFastISel(false), EnableGlobalISel(false), UseInitArray(false),
-        FunctionSections(false), DataSections(false),
-        IgnoreXCOFFVisibility(false), XCOFFTracebackTable(true),
-        UniqueSectionNames(true), UniqueBasicBlockSectionNames(false),
-        SeparateNamedSections(false), TrapUnreachable(false),
-        NoTrapAfterNoreturn(false), TLSSize(0), EmulatedTLS(false),
-        EnableTLSDESC(false), EnableIPRA(false), EmitStackSizeSection(false),
-        EnableMachineOutliner(false), EnableMachineFunctionSplitter(false),
-        EnableStaticDataPartitioning(false), SupportsDefaultOutlining(false),
-        EnableDefaultMachineVerifier(true), EmitAddrsig(false),
-        BBAddrMap(false), EmitCallGraphSection(false), EmitCallSiteInfo(false),
-        SupportsDebugEntryValues(false), EnableDebugEntryValues(false),
-        ValueTrackingVariableLocations(false), ForceDwarfFrameSection(false),
-        XRayFunctionIndex(true), DebugStrictDwarf(false), Hotpatch(false),
-        PPCGenScalarMASSEntries(false), JMCInstrument(false),
-        EnableCFIFixup(false), MisExpect(false), XCOFFReadOnlyPointers(false),
-        VerifyArgABICompliance(true) {}
+  TargetOptions() {
+#define TARGET_OPTION_INIT_BOOL(Type, Name, Bits, Default) Name = Default;
+#define TARGET_OPTION_INIT_U32_BITFIELD(Type, Name, Bits, Default)             \
+  Name = Default;
+#define TARGET_OPTION_INIT_PAIR(Type, Name, Bits, Default)
+#define TARGET_OPTION_INIT_ENUM(Type, Name, Bits, Default)
+#define TARGET_OPTION_INIT_STRING(Type, Name, Bits, Default)
+#define TARGET_OPTION_INIT_U32(Type, Name, Bits, Default)
+#define TARGET_OPTION_INIT_BUFFER(Type, Name, Bits, Default)
+#define TARGET_OPTION_INIT_MC(Type, Name, Bits, Default)
+#define TARGET_OPTION(Type, Name, Bits, Default, Kind)                         \
+  TARGET_OPTION_INIT_##Kind(Type, Name, Bits, Default)
+#include "llvm/Target/TargetOptions.def"
+#undef TARGET_OPTION_INIT_BOOL
+#undef TARGET_OPTION_INIT_U32_BITFIELD
+#undef TARGET_OPTION_INIT_PAIR
+#undef TARGET_OPTION_INIT_ENUM
+#undef TARGET_OPTION_INIT_STRING
+#undef TARGET_OPTION_INIT_U32
+#undef TARGET_OPTION_INIT_BUFFER
+#undef TARGET_OPTION_INIT_MC
+  }
 
-  /// EnableAIXExtendedAltivecABI - This flag returns true when -vec-extabi is
-  /// specified. The code generator is then able to use both volatile and
-  /// nonvolitle vector registers. When false, the code generator only uses
-  /// volatile vector registers which is the default setting on AIX.
-  unsigned EnableAIXExtendedAltivecABI : 1;
-
-  /// HonorSignDependentRoundingFPMath - This returns true when the
-  /// -enable-sign-dependent-rounding-fp-math is specified.  If this returns
-  /// false (the default), the code generator is allowed to assume that the
-  /// rounding behavior is the default (round-to-zero for all floating point
-  /// to integer conversions, and round-to-nearest for all other arithmetic
-  /// truncations).  If this is enabled (set to true), the code generator must
-  /// assume that the rounding mode may dynamically change.
-  unsigned HonorSignDependentRoundingFPMathOption : 1;
   LLVM_ABI bool HonorSignDependentRoundingFPMath() const;
 
-  /// NoZerosInBSS - By default some codegens place zero-initialized data to
-  /// .bss section. This flag disables such behaviour (necessary, e.g. for
-  /// crt*.o compiling).
-  unsigned NoZerosInBSS : 1;
-
-  /// GuaranteedTailCallOpt - This flag is enabled when -tailcallopt is
-  /// specified on the commandline. When the flag is on, participating targets
-  /// will perform tail call optimization on all calls which use the fastcc
-  /// calling convention and which satisfy certain target-independent
-  /// criteria (being at the end of a function, having the same return type
-  /// as their parent function, etc.), using an alternate ABI if necessary.
-  unsigned GuaranteedTailCallOpt : 1;
-
-  /// StackSymbolOrdering - When true, this will allow CodeGen to order
-  /// the local stack symbols (for code size, code locality, or any other
-  /// heuristics). When false, the local symbols are left in whatever order
-  /// they were generated. Default is true.
-  unsigned StackSymbolOrdering : 1;
-
-  /// EnableFastISel - This flag enables fast-path instruction selection
-  /// which trades away generated code quality in favor of reducing
-  /// compile time.
-  unsigned EnableFastISel : 1;
-
-  /// EnableGlobalISel - This flag enables global instruction selection.
-  unsigned EnableGlobalISel : 1;
-
-  /// EnableGlobalISelAbort - Control abort behaviour when global instruction
-  /// selection fails to lower/select an instruction.
-  GlobalISelAbortMode GlobalISelAbort = GlobalISelAbortMode::Enable;
-
-  /// Control when and how the Swift async frame pointer bit should
-  /// be set.
-  SwiftAsyncFramePointerMode SwiftAsyncFramePointer =
-      SwiftAsyncFramePointerMode::Always;
-
-  /// UseInitArray - Use .init_array instead of .ctors for static
-  /// constructors.
-  unsigned UseInitArray : 1;
-
-  /// Emit functions into separate sections.
-  unsigned FunctionSections : 1;
-
-  /// Emit data into separate sections.
-  unsigned DataSections : 1;
-
-  /// Do not emit visibility attribute for xcoff.
-  unsigned IgnoreXCOFFVisibility : 1;
-
-  /// Emit XCOFF traceback table.
-  unsigned XCOFFTracebackTable : 1;
-
-  unsigned UniqueSectionNames : 1;
-
-  /// Use unique names for basic block sections.
-  unsigned UniqueBasicBlockSectionNames : 1;
-
-  /// Emit named sections with the same name into different sections.
-  unsigned SeparateNamedSections : 1;
-
-  /// Emit target-specific trap instruction for 'unreachable' IR instructions.
-  unsigned TrapUnreachable : 1;
-
-  /// Do not emit a trap instruction for 'unreachable' IR instructions behind
-  /// noreturn calls, even if TrapUnreachable is true.
-  unsigned NoTrapAfterNoreturn : 1;
-
-  /// Bit size of immediate TLS offsets (0 == use the default).
-  unsigned TLSSize : 8;
-
-  /// EmulatedTLS - This flag enables emulated TLS model, using emutls
-  /// function in the runtime library..
-  unsigned EmulatedTLS : 1;
-
-  /// EnableTLSDESC - This flag enables TLS Descriptors.
-  unsigned EnableTLSDESC : 1;
-
-  /// This flag enables InterProcedural Register Allocation (IPRA).
-  unsigned EnableIPRA : 1;
-
-  /// Emit section containing metadata on function stack sizes.
-  unsigned EmitStackSizeSection : 1;
-
-  /// Enables the MachineOutliner pass.
-  unsigned EnableMachineOutliner : 1;
-
-  /// Enables the MachineFunctionSplitter pass.
-  unsigned EnableMachineFunctionSplitter : 1;
-
-  /// Enables the StaticDataSplitter pass.
-  unsigned EnableStaticDataPartitioning : 1;
-
-  /// Set if the target supports default outlining behaviour.
-  unsigned SupportsDefaultOutlining : 1;
-
-  /// Enable Machine verifier at the end of default codegen pipelines. (Only
-  /// used with NPM)
-  unsigned EnableDefaultMachineVerifier : 1;
-
-  /// Emit address-significance table.
-  unsigned EmitAddrsig : 1;
-
-  // Emit the SHT_LLVM_BB_ADDR_MAP section containing basic block address
-  // which can be used to map virtual addresses to machine basic blocks.
-  unsigned BBAddrMap : 1;
-
-  /// Emit basic blocks into separate sections.
-  BasicBlockSection BBSections = BasicBlockSection::None;
-
-  /// Memory Buffer that contains information on sampled basic blocks and used
-  /// to selectively generate basic block sections.
-  std::shared_ptr<MemoryBuffer> BBSectionsFuncListBuf;
-
-  /// Emit section containing call graph metadata.
-  unsigned EmitCallGraphSection : 1;
-
-  /// The flag enables call site info production. It is used only for debug
-  /// info, and it is restricted only to optimized code. This can be used for
-  /// something else, so that should be controlled in the frontend.
-  unsigned EmitCallSiteInfo : 1;
-  /// Set if the target supports the debug entry values by default.
-  unsigned SupportsDebugEntryValues : 1;
-  /// When set to true, the EnableDebugEntryValues option forces production
-  /// of debug entry values even if the target does not officially support
-  /// it. Useful for testing purposes only. This flag should never be checked
-  /// directly, always use \ref ShouldEmitDebugEntryValues instead.
-  unsigned EnableDebugEntryValues : 1;
   /// NOTE: There are targets that still do not support the debug entry values
   /// production.
   LLVM_ABI bool ShouldEmitDebugEntryValues() const;
 
-  // When set to true, use experimental new debug variable location tracking,
-  // which seeks to follow the values of variables rather than their location,
-  // post isel.
-  unsigned ValueTrackingVariableLocations : 1;
-
-  /// Emit DWARF debug frame section.
-  unsigned ForceDwarfFrameSection : 1;
-
-  /// Emit XRay Function Index section
-  unsigned XRayFunctionIndex : 1;
-
-  /// When set to true, don't use DWARF extensions in later DWARF versions.
-  /// By default, it is set to false.
-  unsigned DebugStrictDwarf : 1;
-
-  /// Emit the hotpatch flag in CodeView debug.
-  unsigned Hotpatch : 1;
-
-  /// Enables scalar MASS conversions
-  unsigned PPCGenScalarMASSEntries : 1;
-
-  /// Enable JustMyCode instrumentation.
-  unsigned JMCInstrument : 1;
-
-  /// Enable the CFIFixup pass.
-  unsigned EnableCFIFixup : 1;
-
-  /// When set to true, enable MisExpect Diagnostics
-  /// By default, it is set to false
-  unsigned MisExpect : 1;
-
-  /// When set to true, const objects with relocatable address values are put
-  /// into the RO data section.
-  unsigned XCOFFReadOnlyPointers : 1;
-
-  /// When set to true, call/return argument extensions of narrow integers
-  /// are verified in the target backend if it cares about them. This is
-  /// not done with internal tools like llc that run many tests that ignore
-  /// (lack) these extensions.
-  unsigned VerifyArgABICompliance : 1;
-
-  /// Name of the stack usage file (i.e., .su file) if user passes
-  /// -fstack-usage. If empty, it can be implied that -fstack-usage is not
-  /// passed on the command line.
-  std::string StackUsageFile;
-
-  /// If greater than 0, override TargetLoweringBase::PrefLoopAlignment.
-  unsigned LoopAlignment = 0;
-
-  /// AllowFPOpFusion - This flag is set by the -fp-contract=xxx option.
-  /// This controls the creation of fused FP ops that store intermediate
-  /// results in higher precision than IEEE allows (E.g. FMAs).
-  ///
-  /// Fast mode - allows formation of fused FP ops whenever they're
-  /// profitable.
-  /// Standard mode - allow fusion only for 'blessed' FP ops. At present the
-  /// only blessed op is the fmuladd intrinsic. In the future more blessed ops
-  /// may be added.
-  /// Strict mode - allow fusion only if/when it can be proven that the excess
-  /// precision won't effect the result.
-  ///
-  /// Note: This option only controls formation of fused ops by the
-  /// optimizers.  Fused operations that are explicitly specified (e.g. FMA
-  /// via the llvm.fma.* intrinsic) will always be honored, regardless of
-  /// the value of this option.
-  FPOpFusion::FPOpFusionMode AllowFPOpFusion = FPOpFusion::Standard;
-
-  /// ThreadModel - This flag specifies the type of threading model to assume
-  /// for things like atomics
-  ThreadModel::Model ThreadModel = ThreadModel::POSIX;
-
-  /// EABIVersion - This flag specifies the EABI version
-  EABI EABIVersion = EABI::Default;
-
-  /// Which debugger to tune for.
-  DebuggerKind DebuggerTuning = DebuggerKind::Default;
-
-  /// Vector math library to use.
-  VectorLibrary VecLib = VectorLibrary::NoLibrary;
-
-public:
-  /// What exception model to use
-  ExceptionHandling ExceptionModel = ExceptionHandling::None;
-
-  /// Machine level options.
-  MCTargetOptions MCOptions;
-
-  /// Stores the filename/path of the final .o/.obj file, to be written in the
-  /// debug information. This is used for emitting the CodeView S_OBJNAME
-  /// record.
-  std::string ObjectFilenameForDebug;
+#define TARGET_OPTION_TYPE(...) __VA_ARGS__
+#define TARGET_OPTION_DECLARE_BOOL(Type, Name, Bits, Default)                  \
+  TARGET_OPTION_TYPE Type Name : Bits;
+#define TARGET_OPTION_DECLARE_U32_BITFIELD(Type, Name, Bits, Default)          \
+  TARGET_OPTION_TYPE Type Name : Bits;
+#define TARGET_OPTION_DECLARE_PAIR(Type, Name, Bits, Default)                  \
+  TARGET_OPTION_TYPE Type Name = Default;
+#define TARGET_OPTION_DECLARE_ENUM(Type, Name, Bits, Default)                  \
+  TARGET_OPTION_TYPE Type Name = Default;
+#define TARGET_OPTION_DECLARE_STRING(Type, Name, Bits, Default)                \
+  TARGET_OPTION_TYPE Type Name = Default;
+#define TARGET_OPTION_DECLARE_U32(Type, Name, Bits, Default)                   \
+  TARGET_OPTION_TYPE Type Name = Default;
+#define TARGET_OPTION_DECLARE_BUFFER(Type, Name, Bits, Default)                \
+  TARGET_OPTION_TYPE Type Name = Default;
+#define TARGET_OPTION_DECLARE_MC(Type, Name, Bits, Default)                    \
+  TARGET_OPTION_TYPE Type Name = Default;
+#define TARGET_OPTION(Type, Name, Bits, Default, Kind)                         \
+  TARGET_OPTION_DECLARE_##Kind(Type, Name, Bits, Default)
+#include "llvm/Target/TargetOptions.def"
+#undef TARGET_OPTION_TYPE
+#undef TARGET_OPTION_DECLARE_BOOL
+#undef TARGET_OPTION_DECLARE_U32_BITFIELD
+#undef TARGET_OPTION_DECLARE_PAIR
+#undef TARGET_OPTION_DECLARE_ENUM
+#undef TARGET_OPTION_DECLARE_STRING
+#undef TARGET_OPTION_DECLARE_U32
+#undef TARGET_OPTION_DECLARE_BUFFER
+#undef TARGET_OPTION_DECLARE_MC
 };
 
 } // namespace llvm
diff --git a/llvm/lib/LTO/BitcodeMetadataUtils.h b/llvm/lib/LTO/BitcodeMetadataUtils.h
new file mode 100644
index 0000000000000..7a32e1a5f3b8b
--- /dev/null
+++ b/llvm/lib/LTO/BitcodeMetadataUtils.h
@@ -0,0 +1,247 @@
+//===- BitcodeMetadataUtils.h - shared LTO metadata helpers -----*- C++ -*-===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+// Internal helpers shared by LTOConfigBitcode.cpp and TargetOptionsBitcode.cpp.
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_LIB_LTO_BITCODEMETADATAUTILS_H
+#define LLVM_LIB_LTO_BITCODEMETADATAUTILS_H
+
+#include "llvm/ADT/FunctionExtras.h"
+#include "llvm/ADT/SmallVector.h"
+#include "llvm/ADT/StringRef.h"
+#include "llvm/ADT/Twine.h"
+#include "llvm/IR/Constants.h"
+#include "llvm/IR/LLVMContext.h"
+#include "llvm/IR/Metadata.h"
+#include "llvm/Support/Error.h"
+
+#include <cstdint>
+#include <string>
+#include <vector>
+
+namespace llvm {
+namespace lto {
+namespace bitcodemeta {
+
+inline Error metadataError(const Twine &Msg) {
+  return make_error<StringError>(Msg.str(), inconvertibleErrorCode());
+}
+
+inline Metadata *getI32Value(LLVMContext &Ctx, int32_t V) {
+  return ConstantAsMetadata::get(
+      ConstantInt::getSigned(Type::getInt32Ty(Ctx), V));
+}
+
+inline Metadata *getU32Value(LLVMContext &Ctx, uint32_t V) {
+  return ConstantAsMetadata::get(ConstantInt::get(Type::getInt32Ty(Ctx), V));
+}
+
+inline Metadata *getI64Value(LLVMContext &Ctx, uint64_t V) {
+  return ConstantAsMetadata::get(ConstantInt::get(Type::getInt64Ty(Ctx), V));
+}
+
+inline Metadata *getStringValue(LLVMContext &Ctx, StringRef S) {
+  return MDString::get(Ctx, S);
+}
+
+class MetadataWriter {
+  SmallVectorImpl<Metadata *> &Out;
+  LLVMContext &Ctx;
+
+public:
+  MetadataWriter(SmallVectorImpl<Metadata *> &Out, LLVMContext &Ctx)
+      : Out(Out), Ctx(Ctx) {}
+
+  LLVMContext &getContext() const { return Ctx; }
+
+  void putEntry(StringRef Key, Metadata *Value) {
+    Metadata *Ops[] = {getStringValue(Ctx, Key), Value};
+    Out.push_back(MDNode::get(Ctx, Ops));
+  }
+
+  void putI32(StringRef Key, int32_t V) { putEntry(Key, getI32Value(Ctx, V)); }
+
+  void putU32(StringRef Key, uint32_t V) { putEntry(Key, getU32Value(Ctx, V)); }
+
+  void putI64(StringRef Key, uint64_t V) { putEntry(Key, getI64Value(Ctx, V)); }
+
+  void putBool(StringRef Key, bool V) { putI32(Key, V ? 1 : 0); }
+
+  void putString(StringRef Key, StringRef V) {
+    if (!V.empty())
+      putEntry(Key, getStringValue(Ctx, V));
+  }
+
+  void putNode(StringRef Key, MDNode *Node) { putEntry(Key, Node); }
+
+  void putStringList(StringRef Key, ArrayRef<std::string> Values) {
+    if (Values.empty())
+      return;
+    SmallVector<Metadata *, 8> Elems;
+    for (const std::string &S : Values)
+      Elems.push_back(getStringValue(Ctx, S));
+    putEntry(Key, MDNode::get(Ctx, Elems));
+  }
+};
+
+inline Expected<int32_t> getI32Field(const MDNode &Entry,
+                                     StringRef EntryKind = "metadata entry") {
+  if (Entry.getNumOperands() != 2)
+    return metadataError(EntryKind + " must have 2 operands");
+  auto *Val = mdconst::dyn_extract<ConstantInt>(Entry.getOperand(1));
+  if (!Val || !Val->getType()->isIntegerTy(32))
+    return metadataError(EntryKind + " value must be i32");
+  return static_cast<int32_t>(Val->getSExtValue());
+}
+
+inline Expected<uint32_t> getU32Field(const MDNode &Entry,
+                                      StringRef EntryKind = "metadata entry") {
+  if (Entry.getNumOperands() != 2)
+    return metadataError(EntryKind + " must have 2 operands");
+  auto *Val = mdconst::dyn_extract<ConstantInt>(Entry.getOperand(1));
+  if (!Val || !Val->getType()->isIntegerTy(32))
+    return metadataError(EntryKind + " value must be i32");
+  return static_cast<uint32_t>(Val->getZExtValue());
+}
+
+inline Expected<int64_t> getI64Field(const MDNode &Entry,
+                                     StringRef EntryKind = "metadata entry") {
+  if (Entry.getNumOperands() != 2)
+    return metadataError(EntryKind + " must have 2 operands");
+  auto *Val = mdconst::dyn_extract<ConstantInt>(Entry.getOperand(1));
+  if (!Val || !Val->getType()->isIntegerTy(64))
+    return metadataError(EntryKind + " value must be i64");
+  return static_cast<int64_t>(Val->getSExtValue());
+}
+
+inline Expected<StringRef>
+getStringField(const MDNode &Entry, StringRef EntryKind = "metadata entry") {
+  if (Entry.getNumOperands() != 2)
+    return metadataError(EntryKind + " must have 2 operands");
+  auto *Val = dyn_cast<MDString>(Entry.getOperand(1));
+  if (!Val)
+    return metadataError(EntryKind + " value must be a string");
+  return Val->getString();
+}
+
+inline Expected<std::vector<std::string>>
+getStringListField(const MDNode &Entry,
+                   StringRef EntryKind = "metadata entry") {
+  if (Entry.getNumOperands() != 2)
+    return metadataError(EntryKind + " must have 2 operands");
+  auto *List = dyn_cast<MDNode>(Entry.getOperand(1));
+  if (!List)
+    return metadataError(EntryKind + " value must be a string list");
+  std::vector<std::string> Out;
+  Out.reserve(List->getNumOperands());
+  for (Metadata *Op : List->operands()) {
+    auto *S = dyn_cast<MDString>(Op);
+    if (!S)
+      return metadataError(EntryKind + " string list element must be a string");
+    Out.push_back(S->getString().str());
+  }
+  return Out;
+}
+
+inline Expected<MDNode *> getNodeField(const MDNode &Entry,
+                                       StringRef EntryKind = "metadata entry") {
+  if (Entry.getNumOperands() != 2)
+    return metadataError(EntryKind + " must have 2 operands");
+  auto *Node = dyn_cast<MDNode>(Entry.getOperand(1));
+  if (!Node)
+    return metadataError(EntryKind + " value must be a metadata node");
+  return Node;
+}
+
+struct EntryApplier {
+  const MDNode &Entry;
+  StringRef EntryKind;
+
+  Error applyI32(function_ref<void(int32_t)> Setter) {
+    auto V = getI32Field(Entry, EntryKind);
+    if (!V)
+      return V.takeError();
+    Setter(*V);
+    return Error::success();
+  }
+
+  Error applyU32(function_ref<void(uint32_t)> Setter) {
+    auto V = getU32Field(Entry, EntryKind);
+    if (!V)
+      return V.takeError();
+    Setter(*V);
+    return Error::success();
+  }
+
+  Error applyI64(function_ref<void(int64_t)> Setter) {
+    auto V = getI64Field(Entry, EntryKind);
+    if (!V)
+      return V.takeError();
+    Setter(*V);
+    return Error::success();
+  }
+
+  Error applyBool(function_ref<void(bool)> Setter) {
+    auto V = getI32Field(Entry, EntryKind);
+    if (!V)
+      return V.takeError();
+    if (*V != 0 && *V != 1)
+      return metadataError(EntryKind + " boolean value must be 0 or 1");
+    Setter(*V != 0);
+    return Error::success();
+  }
+
+  Error applyString(function_ref<void(StringRef)> Setter) {
+    auto V = getStringField(Entry, EntryKind);
+    if (!V)
+      return V.takeError();
+    Setter(*V);
+    return Error::success();
+  }
+
+  Error applyStringList(function_ref<void(std::vector<std::string>)> Setter) {
+    auto V = getStringListField(Entry, EntryKind);
+    if (!V)
+      return V.takeError();
+    Setter(std::move(*V));
+    return Error::success();
+  }
+};
+
+template <typename T, typename ApplyEntryFn>
+Expected<T>
+decodeVersionedMetadata(const MDNode *Root, unsigned ExpectedVersion,
+                        StringRef RootKind, ApplyEntryFn ApplyEntry) {
+  if (!Root || Root->getNumOperands() < 1)
+    return metadataError("malformed " + RootKind + " metadata root");
+
+  auto *VersionVal = mdconst::dyn_extract<ConstantInt>(Root->getOperand(0));
+  if (!VersionVal || !VersionVal->getType()->isIntegerTy(32))
+    return metadataError("malformed " + RootKind + " metadata version");
+  if (VersionVal->getZExtValue() != ExpectedVersion)
+    return metadataError("unsupported " + RootKind + " metadata version");
+
+  T Result{};
+  for (unsigned I = 1; I < Root->getNumOperands(); ++I) {
+    auto *Entry = dyn_cast<MDNode>(Root->getOperand(I));
+    if (!Entry || Entry->getNumOperands() != 2)
+      return metadataError("malformed " + RootKind + " metadata entry");
+    auto *KeyMD = dyn_cast<MDString>(Entry->getOperand(0));
+    if (!KeyMD)
+      return metadataError(RootKind + " key must be a string");
+    if (Error E = ApplyEntry(Result, KeyMD->getString(), *Entry))
+      return std::move(E);
+  }
+  return Result;
+}
+
+} // namespace bitcodemeta
+} // namespace lto
+} // namespace llvm
+
+#endif
diff --git a/llvm/lib/LTO/LTOConfigBitcode.cpp b/llvm/lib/LTO/LTOConfigBitcode.cpp
new file mode 100644
index 0000000000000..836c080ed5df5
--- /dev/null
+++ b/llvm/lib/LTO/LTOConfigBitcode.cpp
@@ -0,0 +1,355 @@
+//===- LTOConfigBitcode.cpp - lto::Config in bitcode ----------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+// Encodes serializable lto::Config fields as module metadata in bitcode.
+//
+// Layout:
+//   !llvm.lto.config = !{ !0 }
+//   !0 = !{ i32 <version>, !1, !2, ... }
+//   !1 = !{ !"<key>", <value> }
+//
+// Value kinds:
+//   - i32 / i64 ConstantInt for scalars
+//   - MDString for strings
+//   - MDNode list of MDStrings for vector<string>
+//   - nested MDNode for TargetOptions (via encodeTargetOptionsAsNode)
+//
+// Omitted fields (process-local / non-data):
+//   LoadedPassPlugins, PreCodeGenPassesHook, DiagHandler, ResolutionFile,
+//   PreOptModuleHook, PostPromoteModuleHook, PostInternalizeModuleHook,
+//   PostImportModuleHook, PostOptModuleHook, PreCodeGenModuleHook,
+//   CombinedIndexHook, GetSummaryIndexOutputStream, GetImportsListOutputArray,
+//   GetCacheKeyOutputString
+//
+//===----------------------------------------------------------------------===//
+
+#include "llvm/LTO/LTOConfigBitcode.h"
+
+#include "BitcodeMetadataUtils.h"
+
+#include "llvm/Bitcode/BitcodeReader.h"
+#include "llvm/Bitcode/BitcodeWriter.h"
+#include "llvm/IR/Constants.h"
+#include "llvm/IR/LLVMContext.h"
+#include "llvm/IR/Metadata.h"
+#include "llvm/LTO/TargetOptionsBitcode.h"
+#include "llvm/Support/Error.h"
+#include "llvm/Support/FileSystem.h"
+#include "llvm/Support/MemoryBuffer.h"
+#include "llvm/Support/raw_ostream.h"
+
+using namespace llvm;
+using namespace llvm::lto;
+using namespace llvm::lto::bitcodemeta;
+
+namespace {
+
+constexpr unsigned kVersion = 1;
+constexpr StringRef kEntryKind = "lto config entry";
+
+Error writeConfigBitcode(raw_ostream &Out, const Config &Config) {
+  LLVMContext Ctx;
+  Module M("llvm.lto.config", Ctx);
+  if (Error Err = encodeLTOConfigToModule(M, Config))
+    return Err;
+  WriteBitcodeToFile(M, Out);
+  return Error::success();
+}
+
+Expected<std::optional<Config>>
+readConfigBitcodeIfPresent(MemoryBufferRef Buffer) {
+  LLVMContext Ctx;
+  Expected<std::unique_ptr<Module>> M = parseBitcodeFile(Buffer, Ctx);
+  if (!M)
+    return M.takeError();
+  if (!hasEncodedLTOConfig(**M))
+    return std::nullopt;
+  return decodeLTOConfigFromModule(**M);
+}
+
+Expected<Config> readConfigBitcode(MemoryBufferRef Buffer) {
+  Expected<std::optional<Config>> Config = readConfigBitcodeIfPresent(Buffer);
+  if (!Config)
+    return Config.takeError();
+  if (!*Config)
+    return metadataError("missing lto config metadata");
+  return std::move(**Config);
+}
+
+void encodePipelineTuningOptions(MetadataWriter &Writer,
+                                 const PipelineTuningOptions &PTO) {
+#define PIPELINE_TUNING_OPTION_BOOL(Name)                                      \
+  Writer.putBool("pto." #Name, PTO.Name);
+#define PIPELINE_TUNING_OPTION_I32(Name) Writer.putI32("pto." #Name, PTO.Name);
+#define PIPELINE_TUNING_OPTION_U32(Name) Writer.putU32("pto." #Name, PTO.Name);
+#define PIPELINE_TUNING_OPTION(Type, Name, Default, Kind)                      \
+  PIPELINE_TUNING_OPTION_##Kind(Name)
+#include "llvm/Passes/PipelineTuningOptions.def"
+#undef PIPELINE_TUNING_OPTION_BOOL
+#undef PIPELINE_TUNING_OPTION_I32
+#undef PIPELINE_TUNING_OPTION_U32
+}
+
+void encodeRemarksHotnessThreshold(MetadataWriter &Writer,
+                                   const std::optional<uint64_t> &Threshold) {
+  int32_t Mode = 0;
+  uint64_t Value = 0;
+  if (!Threshold.has_value()) {
+    Mode = 2; // auto
+  } else if (*Threshold == 0) {
+    Mode = 0; // disabled
+  } else {
+    Mode = 1; // manual
+    Value = *Threshold;
+  }
+  LLVMContext &Ctx = Writer.getContext();
+  Metadata *Ops[] = {getI32Value(Ctx, Mode), getI64Value(Ctx, Value)};
+  Writer.putEntry("RemarksHotnessThreshold", MDNode::get(Ctx, Ops));
+}
+
+Error decodeRemarksHotnessThreshold(std::optional<uint64_t> &Threshold,
+                                    const MDNode &Entry) {
+  auto Node = getNodeField(Entry, kEntryKind);
+  if (!Node)
+    return Node.takeError();
+  if ((*Node)->getNumOperands() != 2)
+    return metadataError("RemarksHotnessThreshold must have mode and value");
+  auto *Mode = mdconst::dyn_extract<ConstantInt>((*Node)->getOperand(0));
+  auto *Value = mdconst::dyn_extract<ConstantInt>((*Node)->getOperand(1));
+  if (!Mode || !Mode->getType()->isIntegerTy(32) || !Value ||
+      !Value->getType()->isIntegerTy(64))
+    return metadataError("malformed RemarksHotnessThreshold metadata");
+  switch (Mode->getZExtValue()) {
+  case 0:
+    Threshold = 0;
+    break;
+  case 1:
+    Threshold = Value->getZExtValue();
+    break;
+  case 2:
+    Threshold = std::nullopt;
+    break;
+  default:
+    return metadataError("invalid RemarksHotnessThreshold mode");
+  }
+  return Error::success();
+}
+
+void encodeConfigFields(MetadataWriter &Writer, const Config &C) {
+#define LTO_CONFIG_ENCODE_STRING(Type, Name) Writer.putString(#Name, C.Name);
+#define LTO_CONFIG_ENCODE_TARGET_OPTIONS(Type, Name)                           \
+  Writer.putNode(#Name, encodeTargetOptionsAsNode(Writer.getContext(), C.Name));
+#define LTO_CONFIG_ENCODE_STRING_LIST(Type, Name)                              \
+  Writer.putStringList(#Name, C.Name);
+#define LTO_CONFIG_ENCODE_NONE(Type, Name)
+#define LTO_CONFIG_ENCODE_OPTIONAL_RELOC_MODEL(Type, Name)                     \
+  Writer.putBool(#Name ".HasValue", C.Name.has_value());                       \
+  if (C.Name)                                                                  \
+    Writer.putI32(#Name, static_cast<int32_t>(*C.Name));
+#define LTO_CONFIG_ENCODE_OPTIONAL_ENUM(Type, Name)                            \
+  if (C.Name)                                                                  \
+    Writer.putI32(#Name, static_cast<int32_t>(*C.Name));
+#define LTO_CONFIG_ENCODE_ENUM(Type, Name)                                     \
+  Writer.putI32(#Name, static_cast<int32_t>(C.Name));
+#define LTO_CONFIG_ENCODE_I32(Type, Name)                                      \
+  Writer.putI32(#Name, static_cast<int32_t>(C.Name));
+#define LTO_CONFIG_ENCODE_U32(Type, Name) Writer.putU32(#Name, C.Name);
+#define LTO_CONFIG_ENCODE_BOOL(Type, Name) Writer.putBool(#Name, C.Name);
+#define LTO_CONFIG_ENCODE_REMARKS_HOTNESS(Type, Name)                          \
+  encodeRemarksHotnessThreshold(Writer, C.Name);
+#define LTO_CONFIG_ENCODE_PIPELINE_TUNING_OPTIONS(Type, Name)                  \
+  encodePipelineTuningOptions(Writer, C.Name);
+#define LTO_CONFIG_OPTION(Type, Name, Default, Kind)                           \
+  LTO_CONFIG_ENCODE_##Kind(Type, Name)
+#define LTO_CONFIG_MUTABLE_OPTION(Type, Name, Default, Kind)                   \
+  LTO_CONFIG_ENCODE_##Kind(Type, Name)
+#include "llvm/LTO/Config.def"
+#undef LTO_CONFIG_ENCODE_STRING
+#undef LTO_CONFIG_ENCODE_TARGET_OPTIONS
+#undef LTO_CONFIG_ENCODE_STRING_LIST
+#undef LTO_CONFIG_ENCODE_NONE
+#undef LTO_CONFIG_ENCODE_OPTIONAL_RELOC_MODEL
+#undef LTO_CONFIG_ENCODE_OPTIONAL_ENUM
+#undef LTO_CONFIG_ENCODE_ENUM
+#undef LTO_CONFIG_ENCODE_I32
+#undef LTO_CONFIG_ENCODE_U32
+#undef LTO_CONFIG_ENCODE_BOOL
+#undef LTO_CONFIG_ENCODE_REMARKS_HOTNESS
+#undef LTO_CONFIG_ENCODE_PIPELINE_TUNING_OPTIONS
+}
+
+Error applyEntry(Config &C, StringRef Key, const MDNode &Entry) {
+  EntryApplier Applier{Entry, kEntryKind};
+
+#define LTO_CONFIG_DECODE_STRING(Type, Name)                                   \
+  if (Key == #Name)                                                            \
+    return Applier.applyString([&](StringRef V) { C.Name = V.str(); });
+#define LTO_CONFIG_DECODE_TARGET_OPTIONS(Type, Name)                           \
+  if (Key == #Name) {                                                          \
+    auto Node = getNodeField(Entry, kEntryKind);                               \
+    if (!Node)                                                                 \
+      return Node.takeError();                                                 \
+    auto Opt = decodeTargetOptionsFromNode(*Node);                             \
+    if (!Opt)                                                                  \
+      return Opt.takeError();                                                  \
+    C.Name = std::move(*Opt);                                                  \
+    return Error::success();                                                   \
+  }
+#define LTO_CONFIG_DECODE_STRING_LIST(Type, Name)                              \
+  if (Key == #Name)                                                            \
+    return Applier.applyStringList(                                            \
+        [&](std::vector<std::string> V) { C.Name = std::move(V); });
+#define LTO_CONFIG_DECODE_NONE(Type, Name)
+#define LTO_CONFIG_DECODE_OPTIONAL_RELOC_MODEL(Type, Name)                     \
+  if (Key == #Name)                                                            \
+    return Applier.applyI32(                                                   \
+        [&](int32_t V) { C.Name = static_cast<Reloc::Model>(V); });            \
+  if (Key == #Name ".HasValue")                                                \
+    return Applier.applyBool([&](bool V) {                                     \
+      if (!V)                                                                  \
+        C.Name = std::nullopt;                                                 \
+    });
+#define LTO_CONFIG_DECODE_OPTIONAL_ENUM(Type, Name)                            \
+  if (Key == #Name)                                                            \
+    return Applier.applyI32([&](int32_t V) {                                   \
+      C.Name = static_cast<typename Type::value_type>(V);                      \
+    });
+#define LTO_CONFIG_DECODE_ENUM(Type, Name)                                     \
+  if (Key == #Name)                                                            \
+    return Applier.applyI32([&](int32_t V) { C.Name = static_cast<Type>(V); });
+#define LTO_CONFIG_DECODE_I32(Type, Name)                                      \
+  if (Key == #Name)                                                            \
+    return Applier.applyI32([&](int32_t V) { C.Name = static_cast<Type>(V); });
+#define LTO_CONFIG_DECODE_U32(Type, Name)                                      \
+  if (Key == #Name)                                                            \
+    return Applier.applyU32([&](uint32_t V) { C.Name = V; });
+#define LTO_CONFIG_DECODE_BOOL(Type, Name)                                     \
+  if (Key == #Name)                                                            \
+    return Applier.applyBool([&](bool V) { C.Name = V; });
+#define LTO_CONFIG_DECODE_REMARKS_HOTNESS(Type, Name)                          \
+  if (Key == #Name)                                                            \
+    return decodeRemarksHotnessThreshold(C.Name, Entry);
+#define LTO_CONFIG_DECODE_PIPELINE_TUNING_OPTIONS(Type, Name)                  \
+  PipelineTuningOptions &PTO = C.Name;
+#define LTO_CONFIG_OPTION(Type, Name, Default, Kind)                           \
+  LTO_CONFIG_DECODE_##Kind(Type, Name)
+#define LTO_CONFIG_MUTABLE_OPTION(Type, Name, Default, Kind)                   \
+  LTO_CONFIG_DECODE_##Kind(Type, Name)
+#include "llvm/LTO/Config.def"
+#undef LTO_CONFIG_DECODE_STRING
+#undef LTO_CONFIG_DECODE_TARGET_OPTIONS
+#undef LTO_CONFIG_DECODE_STRING_LIST
+#undef LTO_CONFIG_DECODE_NONE
+#undef LTO_CONFIG_DECODE_OPTIONAL_RELOC_MODEL
+#undef LTO_CONFIG_DECODE_OPTIONAL_ENUM
+#undef LTO_CONFIG_DECODE_ENUM
+#undef LTO_CONFIG_DECODE_I32
+#undef LTO_CONFIG_DECODE_U32
+#undef LTO_CONFIG_DECODE_BOOL
+#undef LTO_CONFIG_DECODE_REMARKS_HOTNESS
+#undef LTO_CONFIG_DECODE_PIPELINE_TUNING_OPTIONS
+
+#define PIPELINE_TUNING_DECODE_BOOL(Type, Name)                                \
+  if (Key == "pto." #Name)                                                     \
+    return Applier.applyBool([&](bool V) { PTO.Name = V; });
+#define PIPELINE_TUNING_DECODE_I32(Type, Name)                                 \
+  if (Key == "pto." #Name)                                                     \
+    return Applier.applyI32(                                                   \
+        [&](int32_t V) { PTO.Name = static_cast<Type>(V); });
+#define PIPELINE_TUNING_DECODE_U32(Type, Name)                                 \
+  if (Key == "pto." #Name)                                                     \
+    return Applier.applyU32([&](uint32_t V) { PTO.Name = V; });
+#define PIPELINE_TUNING_OPTION(Type, Name, Default, Kind)                      \
+  PIPELINE_TUNING_DECODE_##Kind(Type, Name)
+#include "llvm/Passes/PipelineTuningOptions.def"
+#undef PIPELINE_TUNING_DECODE_BOOL
+#undef PIPELINE_TUNING_DECODE_I32
+#undef PIPELINE_TUNING_DECODE_U32
+
+  return metadataError("unknown lto config key: " + Key);
+}
+
+Expected<Config> decodeConfigFromRoot(const MDNode *Root) {
+  return decodeVersionedMetadata<Config>(
+      Root, kVersion, "lto config",
+      [](Config &C, StringRef Key, const MDNode &Entry) {
+        return applyEntry(C, Key, Entry);
+      });
+}
+
+} // namespace
+
+bool lto::hasEncodedLTOConfig(const Module &M) {
+  return M.getNamedMetadata(LTOConfigMetadataName) != nullptr;
+}
+
+Error lto::encodeLTOConfigToModule(Module &M, const Config &Config) {
+  LLVMContext &Ctx = M.getContext();
+  SmallVector<Metadata *, 64> Entries;
+  Entries.push_back(getI32Value(Ctx, kVersion));
+  MetadataWriter Writer(Entries, Ctx);
+  encodeConfigFields(Writer, Config);
+
+  MDNode *Root = MDNode::get(Ctx, Entries);
+  NamedMDNode *NMD = M.getOrInsertNamedMetadata(LTOConfigMetadataName);
+  NMD->clearOperands();
+  NMD->addOperand(Root);
+  return Error::success();
+}
+
+Expected<Config> lto::decodeLTOConfigFromModule(const Module &M) {
+  NamedMDNode *NMD = M.getNamedMetadata(LTOConfigMetadataName);
+  if (!NMD || NMD->getNumOperands() == 0)
+    return metadataError("missing lto config metadata");
+  return decodeConfigFromRoot(dyn_cast<MDNode>(NMD->getOperand(0)));
+}
+
+Error lto::writeLTOConfigToFile(StringRef Path, const Config &Config) {
+  std::error_code EC;
+  raw_fd_ostream OS(Path, EC, sys::fs::OF_None);
+  if (EC)
+    return createStringError(EC, "cannot open LTO config file '%s'",
+                             Path.str().c_str());
+  if (Error Err = writeConfigBitcode(OS, Config))
+    return Err;
+  OS.close();
+  if (OS.has_error())
+    return createStringError(OS.error(), "cannot write LTO config file '%s'",
+                             Path.str().c_str());
+  return Error::success();
+}
+
+Expected<Config> lto::readLTOConfigFromFile(StringRef Path) {
+  ErrorOr<std::unique_ptr<MemoryBuffer>> Buffer = MemoryBuffer::getFile(Path);
+  if (!Buffer)
+    return createStringError(Buffer.getError(),
+                             "cannot read LTO config file '%s'",
+                             Path.str().c_str());
+
+  return readConfigBitcode((*Buffer)->getMemBufferRef());
+}
+
+Error lto::writeIndexWithLTOConfigToFile(
+    const ModuleSummaryIndex &Index, const Config &Config, raw_ostream &Out,
+    const ModuleToSummariesForIndexTy *ModuleToSummariesForIndex,
+    const GVSummaryPtrSet *DecSummaries) {
+  LLVMContext Ctx;
+  Module MetadataModule("llvm.lto.config", Ctx);
+  if (Error Err = encodeLTOConfigToModule(MetadataModule, Config))
+    return Err;
+  writeIndexToFile(Index, Out, ModuleToSummariesForIndex, DecSummaries,
+                   &MetadataModule);
+  return Error::success();
+}
+
+Expected<Config> lto::readLTOConfigFromSummaryIndex(MemoryBufferRef Buffer) {
+  return readConfigBitcode(Buffer);
+}
+
+Expected<std::optional<Config>>
+lto::readLTOConfigFromSummaryIndexIfPresent(MemoryBufferRef Buffer) {
+  return readConfigBitcodeIfPresent(Buffer);
+}
diff --git a/llvm/lib/LTO/TargetOptionsBitcode.cpp b/llvm/lib/LTO/TargetOptionsBitcode.cpp
new file mode 100644
index 0000000000000..d3eede5bd4065
--- /dev/null
+++ b/llvm/lib/LTO/TargetOptionsBitcode.cpp
@@ -0,0 +1,265 @@
+//===- TargetOptionsBitcode.cpp - TargetOptions in bitcode ---------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+// Encodes llvm::TargetOptions as module metadata that is stored in bitcode.
+//
+// Layout:
+//   !llvm.lto.target_options = !{ !0 }
+//   !0 = !{ i32 <version>, !1, !2, ... }
+//   !1 = !{ !"<key>", <value> }
+//
+// Value kinds:
+//   - i32 ConstantInt for bools, enums, and small integers
+//   - MDString for std::string fields
+//   - nested MDNode for structured fields such as MemoryBuffer
+//
+// Fields that cannot be represented in IR (such as callbacks) are
+// intentionally omitted.
+//
+//===----------------------------------------------------------------------===//
+
+#include "llvm/LTO/TargetOptionsBitcode.h"
+
+#include "BitcodeMetadataUtils.h"
+
+#include "llvm/IR/Constants.h"
+#include "llvm/IR/LLVMContext.h"
+#include "llvm/IR/Metadata.h"
+#include "llvm/Support/Error.h"
+#include "llvm/Support/MemoryBuffer.h"
+
+using namespace llvm;
+using namespace llvm::lto;
+using namespace llvm::lto::bitcodemeta;
+
+namespace {
+
+constexpr unsigned kVersion = 1;
+constexpr StringRef kEntryKind = "target options entry";
+
+void encodeMemoryBuffer(MetadataWriter &Writer, StringRef Key,
+                        const std::shared_ptr<MemoryBuffer> &Buffer) {
+  if (!Buffer)
+    return;
+
+  Metadata *Fields[] = {
+      getStringValue(Writer.getContext(), Buffer->getBufferIdentifier()),
+      getStringValue(Writer.getContext(), Buffer->getBuffer())};
+  Writer.putNode(Key, MDNode::get(Writer.getContext(), Fields));
+}
+
+Error decodeMemoryBuffer(std::shared_ptr<MemoryBuffer> &Buffer,
+                         const MDNode &Entry) {
+  Expected<MDNode *> Fields = getNodeField(Entry, kEntryKind);
+  if (!Fields)
+    return Fields.takeError();
+  if ((*Fields)->getNumOperands() != 2)
+    return metadataError(kEntryKind +
+                         " memory buffer must contain an identifier and data");
+
+  auto *Identifier = dyn_cast<MDString>((*Fields)->getOperand(0));
+  auto *Data = dyn_cast<MDString>((*Fields)->getOperand(1));
+  if (!Identifier || !Data)
+    return metadataError(kEntryKind + " memory buffer fields must be strings");
+
+  Buffer = MemoryBuffer::getMemBufferCopy(Data->getString(),
+                                          Identifier->getString());
+  return Error::success();
+}
+
+void encodeMCTargetOptions(MetadataWriter &Writer, const MCTargetOptions &MC) {
+#define MC_TARGET_OPTION_ENCODE_BITFIELD(Type, Name, Bits, Default)            \
+  Writer.putBool("mc." #Name, MC.Name);
+#define MC_TARGET_OPTION_ENCODE_BOOL(Type, Name, Bits, Default)                \
+  Writer.putBool("mc." #Name, MC.Name);
+#define MC_TARGET_OPTION_ENCODE_ENUM(Type, Name, Bits, Default)                \
+  Writer.putI32("mc." #Name, static_cast<int32_t>(MC.Name));
+#define MC_TARGET_OPTION_ENCODE_OPTIONAL_UINT(Type, Name, Bits, Default)       \
+  if (MC.Name)                                                                 \
+    Writer.putU32("mc." #Name, *MC.Name);
+#define MC_TARGET_OPTION_ENCODE_INT(Type, Name, Bits, Default)                 \
+  Writer.putI32("mc." #Name, MC.Name);
+#define MC_TARGET_OPTION_ENCODE_PAIR(Type, Name, Bits, Default)                \
+  Writer.putI32("mc." #Name "Major", MC.Name.first);                           \
+  Writer.putI32("mc." #Name "Minor", MC.Name.second);
+#define MC_TARGET_OPTION_ENCODE_STRING(Type, Name, Bits, Default)              \
+  Writer.putString("mc." #Name, MC.Name);
+#define MC_TARGET_OPTION_ENCODE_STRING_LIST(Type, Name, Bits, Default)         \
+  Writer.putStringList("mc." #Name, MC.Name);
+#define MC_TARGET_OPTION(Type, Name, Bits, Default, Kind)                      \
+  MC_TARGET_OPTION_ENCODE_##Kind(Type, Name, Bits, Default)
+#include "llvm/MC/MCTargetOptions.def"
+#undef MC_TARGET_OPTION_ENCODE_BITFIELD
+#undef MC_TARGET_OPTION_ENCODE_BOOL
+#undef MC_TARGET_OPTION_ENCODE_ENUM
+#undef MC_TARGET_OPTION_ENCODE_OPTIONAL_UINT
+#undef MC_TARGET_OPTION_ENCODE_INT
+#undef MC_TARGET_OPTION_ENCODE_PAIR
+#undef MC_TARGET_OPTION_ENCODE_STRING
+#undef MC_TARGET_OPTION_ENCODE_STRING_LIST
+}
+
+void encodeTargetOptionsFields(MetadataWriter &Writer,
+                               const TargetOptions &Opt) {
+#define TARGET_OPTION_ENCODE_BOOL(Type, Name, Bits, Default)                   \
+  Writer.putBool(#Name, Opt.Name);
+#define TARGET_OPTION_ENCODE_U32_BITFIELD(Type, Name, Bits, Default)           \
+  Writer.putU32(#Name, Opt.Name);
+#define TARGET_OPTION_ENCODE_U32(Type, Name, Bits, Default)                    \
+  Writer.putU32(#Name, Opt.Name);
+#define TARGET_OPTION_ENCODE_ENUM(Type, Name, Bits, Default)                   \
+  Writer.putI32(#Name, static_cast<int32_t>(Opt.Name));
+#define TARGET_OPTION_ENCODE_STRING(Type, Name, Bits, Default)                 \
+  Writer.putString(#Name, Opt.Name);
+#define TARGET_OPTION_ENCODE_PAIR(Type, Name, Bits, Default)                   \
+  Writer.putI32(#Name "Major", Opt.Name.first);                                \
+  Writer.putI32(#Name "Minor", Opt.Name.second);
+#define TARGET_OPTION_ENCODE_BUFFER(Type, Name, Bits, Default)                 \
+  encodeMemoryBuffer(Writer, #Name, Opt.Name);
+#define TARGET_OPTION_ENCODE_MC(Type, Name, Bits, Default)                     \
+  encodeMCTargetOptions(Writer, Opt.Name);
+#define TARGET_OPTION(Type, Name, Bits, Default, Kind)                         \
+  TARGET_OPTION_ENCODE_##Kind(Type, Name, Bits, Default)
+#include "llvm/Target/TargetOptions.def"
+#undef TARGET_OPTION_ENCODE_BOOL
+#undef TARGET_OPTION_ENCODE_U32_BITFIELD
+#undef TARGET_OPTION_ENCODE_U32
+#undef TARGET_OPTION_ENCODE_ENUM
+#undef TARGET_OPTION_ENCODE_STRING
+#undef TARGET_OPTION_ENCODE_PAIR
+#undef TARGET_OPTION_ENCODE_BUFFER
+#undef TARGET_OPTION_ENCODE_MC
+}
+
+Error applyEntry(TargetOptions &Opt, StringRef Key, const MDNode &Entry) {
+  EntryApplier Applier{Entry, kEntryKind};
+
+#define TARGET_OPTION_TYPE(...) __VA_ARGS__
+#define TARGET_OPTION_DECODE_BOOL(Type, Name, Bits, Default)                   \
+  if (Key == #Name)                                                            \
+    return Applier.applyBool([&](bool V) { Opt.Name = V; });
+#define TARGET_OPTION_DECODE_U32_BITFIELD(Type, Name, Bits, Default)           \
+  if (Key == #Name)                                                            \
+    return Applier.applyU32([&](uint32_t V) { Opt.Name = V; });
+#define TARGET_OPTION_DECODE_U32(Type, Name, Bits, Default)                    \
+  if (Key == #Name)                                                            \
+    return Applier.applyU32([&](uint32_t V) { Opt.Name = V; });
+#define TARGET_OPTION_DECODE_ENUM(Type, Name, Bits, Default)                   \
+  if (Key == #Name)                                                            \
+    return Applier.applyI32([&](int32_t V) {                                   \
+      Opt.Name = static_cast<TARGET_OPTION_TYPE Type>(V);                      \
+    });
+#define TARGET_OPTION_DECODE_STRING(Type, Name, Bits, Default)                 \
+  if (Key == #Name)                                                            \
+    return Applier.applyString([&](StringRef V) { Opt.Name = V.str(); });
+#define TARGET_OPTION_DECODE_PAIR(Type, Name, Bits, Default)                   \
+  if (Key == #Name "Major")                                                    \
+    return Applier.applyI32([&](int32_t V) { Opt.Name.first = V; });           \
+  if (Key == #Name "Minor")                                                    \
+    return Applier.applyI32([&](int32_t V) { Opt.Name.second = V; });
+#define TARGET_OPTION_DECODE_BUFFER(Type, Name, Bits, Default)                 \
+  if (Key == #Name)                                                            \
+    return decodeMemoryBuffer(Opt.Name, Entry);
+#define TARGET_OPTION_DECODE_MC(Type, Name, Bits, Default)                     \
+  MCTargetOptions &MC = Opt.Name;
+#define TARGET_OPTION(Type, Name, Bits, Default, Kind)                         \
+  TARGET_OPTION_DECODE_##Kind(Type, Name, Bits, Default)
+#include "llvm/Target/TargetOptions.def"
+#undef TARGET_OPTION_TYPE
+#undef TARGET_OPTION_DECODE_BOOL
+#undef TARGET_OPTION_DECODE_U32_BITFIELD
+#undef TARGET_OPTION_DECODE_U32
+#undef TARGET_OPTION_DECODE_ENUM
+#undef TARGET_OPTION_DECODE_STRING
+#undef TARGET_OPTION_DECODE_PAIR
+#undef TARGET_OPTION_DECODE_BUFFER
+#undef TARGET_OPTION_DECODE_MC
+
+#define MC_TARGET_OPTION_TYPE(...) __VA_ARGS__
+#define MC_TARGET_OPTION_DECODE_BITFIELD(Type, Name, Bits, Default)            \
+  if (Key == "mc." #Name)                                                      \
+    return Applier.applyBool([&](bool V) { MC.Name = V; });
+#define MC_TARGET_OPTION_DECODE_BOOL(Type, Name, Bits, Default)                \
+  if (Key == "mc." #Name)                                                      \
+    return Applier.applyBool([&](bool V) { MC.Name = V; });
+#define MC_TARGET_OPTION_DECODE_ENUM(Type, Name, Bits, Default)                \
+  if (Key == "mc." #Name)                                                      \
+    return Applier.applyI32([&](int32_t V) {                                   \
+      MC.Name = static_cast<MC_TARGET_OPTION_TYPE Type>(V);                    \
+    });
+#define MC_TARGET_OPTION_DECODE_OPTIONAL_UINT(Type, Name, Bits, Default)       \
+  if (Key == "mc." #Name)                                                      \
+    return Applier.applyU32([&](uint32_t V) { MC.Name = V; });
+#define MC_TARGET_OPTION_DECODE_INT(Type, Name, Bits, Default)                 \
+  if (Key == "mc." #Name)                                                      \
+    return Applier.applyI32([&](int32_t V) { MC.Name = V; });
+#define MC_TARGET_OPTION_DECODE_PAIR(Type, Name, Bits, Default)                \
+  if (Key == "mc." #Name "Major")                                              \
+    return Applier.applyI32([&](int32_t V) { MC.Name.first = V; });            \
+  if (Key == "mc." #Name "Minor")                                              \
+    return Applier.applyI32([&](int32_t V) { MC.Name.second = V; });
+#define MC_TARGET_OPTION_DECODE_STRING(Type, Name, Bits, Default)              \
+  if (Key == "mc." #Name)                                                      \
+    return Applier.applyString([&](StringRef V) { MC.Name = V.str(); });
+#define MC_TARGET_OPTION_DECODE_STRING_LIST(Type, Name, Bits, Default)         \
+  if (Key == "mc." #Name)                                                      \
+    return Applier.applyStringList(                                            \
+        [&](std::vector<std::string> V) { MC.Name = std::move(V); });
+#define MC_TARGET_OPTION(Type, Name, Bits, Default, Kind)                      \
+  MC_TARGET_OPTION_DECODE_##Kind(Type, Name, Bits, Default)
+#include "llvm/MC/MCTargetOptions.def"
+#undef MC_TARGET_OPTION_TYPE
+#undef MC_TARGET_OPTION_DECODE_BITFIELD
+#undef MC_TARGET_OPTION_DECODE_BOOL
+#undef MC_TARGET_OPTION_DECODE_ENUM
+#undef MC_TARGET_OPTION_DECODE_OPTIONAL_UINT
+#undef MC_TARGET_OPTION_DECODE_INT
+#undef MC_TARGET_OPTION_DECODE_PAIR
+#undef MC_TARGET_OPTION_DECODE_STRING
+#undef MC_TARGET_OPTION_DECODE_STRING_LIST
+
+  return metadataError("unknown target options key: " + Key);
+}
+
+} // namespace
+
+bool lto::hasEncodedTargetOptions(const Module &M) {
+  return M.getNamedMetadata(TargetOptionsMetadataName) != nullptr;
+}
+
+Error lto::encodeTargetOptionsToModule(Module &M,
+                                       const TargetOptions &Options) {
+  MDNode *Root = encodeTargetOptionsAsNode(M.getContext(), Options);
+  NamedMDNode *NMD = M.getOrInsertNamedMetadata(TargetOptionsMetadataName);
+  NMD->clearOperands();
+  NMD->addOperand(Root);
+  return Error::success();
+}
+
+MDNode *lto::encodeTargetOptionsAsNode(LLVMContext &Ctx,
+                                       const TargetOptions &Options) {
+  SmallVector<Metadata *, 32> Entries;
+  Entries.push_back(getI32Value(Ctx, kVersion));
+  MetadataWriter Writer(Entries, Ctx);
+  encodeTargetOptionsFields(Writer, Options);
+  return MDNode::get(Ctx, Entries);
+}
+
+Expected<TargetOptions> lto::decodeTargetOptionsFromNode(const MDNode *Root) {
+  return decodeVersionedMetadata<TargetOptions>(
+      Root, kVersion, "target options",
+      [](TargetOptions &Opt, StringRef Key, const MDNode &Entry) {
+        return applyEntry(Opt, Key, Entry);
+      });
+}
+
+Expected<TargetOptions> lto::decodeTargetOptionsFromModule(const Module &M) {
+  NamedMDNode *NMD = M.getNamedMetadata(TargetOptionsMetadataName);
+  if (!NMD || NMD->getNumOperands() == 0)
+    return metadataError("missing target options metadata");
+
+  return decodeTargetOptionsFromNode(dyn_cast<MDNode>(NMD->getOperand(0)));
+}
diff --git a/llvm/unittests/LTO/LTOConfigBitcodeTest.cpp b/llvm/unittests/LTO/LTOConfigBitcodeTest.cpp
new file mode 100644
index 0000000000000..6b6cf4dab36f0
--- /dev/null
+++ b/llvm/unittests/LTO/LTOConfigBitcodeTest.cpp
@@ -0,0 +1,181 @@
+//===- LTOConfigBitcodeTest.cpp - LTO config bitcode tests --------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#include "llvm/LTO/LTOConfigBitcode.h"
+#include "llvm/Bitcode/BitcodeReader.h"
+#include "llvm/Bitcode/BitcodeWriter.h"
+#include "llvm/IR/LLVMContext.h"
+#include "llvm/IR/Module.h"
+#include "llvm/LTO/TargetOptionsBitcode.h"
+#include "llvm/Support/FileSystem.h"
+#include "llvm/Support/FileUtilities.h"
+#include "llvm/Support/MemoryBuffer.h"
+#include "llvm/Support/MemoryBufferRef.h"
+#include "llvm/Support/raw_ostream.h"
+#include "llvm/Testing/Support/Error.h"
+#include "gtest/gtest.h"
+
+#include <limits>
+
+using namespace llvm;
+using namespace llvm::lto;
+
+TEST(TargetOptionsBitcodeTest, RoundTripThroughBitcode) {
+  TargetOptions Input;
+  Input.MCOptions.BinutilsVersion = {2, 41};
+  Input.FunctionSections = true;
+  Input.DataSections = true;
+  Input.GlobalISelAbort = GlobalISelAbortMode::DisableWithDiag;
+  Input.TLSSize = 64;
+  Input.BBSections = BasicBlockSection::List;
+  Input.BBSectionsFuncListBuf = MemoryBuffer::getMemBufferCopy(
+      "v1\nf foo\nc 0 1\n", "basic-block-sections.profile");
+  Input.EnableDefaultMachineVerifier = false;
+  Input.StackUsageFile = "output.su";
+  Input.LoopAlignment = std::numeric_limits<unsigned>::max();
+  Input.ExceptionModel = ExceptionHandling::Wasm;
+  Input.MCOptions.MCRelaxAll = true;
+  Input.MCOptions.RelocSectionSym = RelocSectionSymType::Internal;
+  Input.MCOptions.DwarfVersion = 5;
+  Input.MCOptions.ABIName = "test-abi";
+  Input.MCOptions.OutputAsmVariant = std::numeric_limits<unsigned>::max();
+  Input.MCOptions.IASSearchPaths = {"include/one", "include/two"};
+  Input.MCOptions.InstPrinterOptions = {"no-aliases"};
+  Input.MCOptions.LargeEHEncoding = true;
+
+  LLVMContext WriteCtx;
+  Module M("target-options", WriteCtx);
+  ASSERT_THAT_ERROR(encodeTargetOptionsToModule(M, Input), Succeeded());
+  EXPECT_TRUE(hasEncodedTargetOptions(M));
+
+  SmallString<0> Storage;
+  raw_svector_ostream OS(Storage);
+  WriteBitcodeToFile(M, OS);
+
+  LLVMContext ReadCtx;
+  Expected<std::unique_ptr<Module>> Parsed = parseBitcodeFile(
+      MemoryBufferRef(StringRef(Storage.data(), Storage.size()), "options.bc"),
+      ReadCtx);
+  ASSERT_THAT_EXPECTED(Parsed, Succeeded());
+  Expected<TargetOptions> Output = decodeTargetOptionsFromModule(**Parsed);
+  ASSERT_THAT_EXPECTED(Output, Succeeded());
+
+  EXPECT_EQ(Output->MCOptions.BinutilsVersion, Input.MCOptions.BinutilsVersion);
+  EXPECT_TRUE(Output->FunctionSections);
+  EXPECT_TRUE(Output->DataSections);
+  EXPECT_EQ(Output->GlobalISelAbort, GlobalISelAbortMode::DisableWithDiag);
+  EXPECT_EQ(Output->TLSSize, 64u);
+  EXPECT_EQ(Output->BBSections, BasicBlockSection::List);
+  ASSERT_TRUE(Output->BBSectionsFuncListBuf);
+  EXPECT_EQ(Output->BBSectionsFuncListBuf->getBufferIdentifier(),
+            "basic-block-sections.profile");
+  EXPECT_EQ(Output->BBSectionsFuncListBuf->getBuffer(), "v1\nf foo\nc 0 1\n");
+  EXPECT_FALSE(Output->EnableDefaultMachineVerifier);
+  EXPECT_EQ(Output->StackUsageFile, "output.su");
+  EXPECT_EQ(Output->LoopAlignment, std::numeric_limits<unsigned>::max());
+  EXPECT_EQ(Output->ExceptionModel, ExceptionHandling::Wasm);
+  EXPECT_TRUE(Output->MCOptions.MCRelaxAll);
+  EXPECT_EQ(Output->MCOptions.RelocSectionSym, RelocSectionSymType::Internal);
+  EXPECT_EQ(Output->MCOptions.DwarfVersion, 5);
+  EXPECT_EQ(Output->MCOptions.ABIName, "test-abi");
+  EXPECT_EQ(Output->MCOptions.OutputAsmVariant,
+            std::numeric_limits<unsigned>::max());
+  EXPECT_EQ(Output->MCOptions.IASSearchPaths, Input.MCOptions.IASSearchPaths);
+  EXPECT_EQ(Output->MCOptions.InstPrinterOptions,
+            Input.MCOptions.InstPrinterOptions);
+  EXPECT_TRUE(Output->MCOptions.LargeEHEncoding);
+}
+
+TEST(LTOConfigBitcodeTest, RoundTripThroughFile) {
+  Config Input;
+  Input.CPU = "generic";
+  Input.MAttrs = {"+crc", "+simd"};
+  Input.MllvmArgs = {"-inline-threshold=42"};
+  Input.PassPluginFilenames = {"plugin.so"};
+  Input.RelocModel = std::nullopt;
+  Input.CodeModel = CodeModel::Large;
+  Input.CGOptLevel = CodeGenOptLevel::Aggressive;
+  Input.OptLevel = 3;
+  Input.Dtlto = true;
+  Input.RemarksHotnessThreshold = std::numeric_limits<uint64_t>::max();
+  Input.TimeTraceGranularity = std::numeric_limits<unsigned>::max();
+  Input.ThinLTOModulesToCompile = {"one.bc", "two.bc"};
+  Input.PTO.LoopInterchange = true;
+  Input.PTO.LicmMssaOptCap = std::numeric_limits<unsigned>::max();
+  Input.PTO.InlinerThreshold = 42;
+  Input.Options.FunctionSections = true;
+  Input.Options.MCOptions.IASSearchPaths = {"sdk/include"};
+
+  SmallString<128> Path;
+  ASSERT_FALSE(sys::fs::createTemporaryFile("lto-config", "bc", Path));
+  FileRemover Cleanup(Path);
+
+  ASSERT_THAT_ERROR(writeLTOConfigToFile(Path, Input), Succeeded());
+  Expected<Config> Output = readLTOConfigFromFile(Path);
+  ASSERT_THAT_EXPECTED(Output, Succeeded());
+
+  EXPECT_EQ(Output->CPU, "generic");
+  EXPECT_EQ(Output->MAttrs, Input.MAttrs);
+  EXPECT_EQ(Output->MllvmArgs, Input.MllvmArgs);
+  EXPECT_EQ(Output->PassPluginFilenames, Input.PassPluginFilenames);
+  EXPECT_EQ(Output->RelocModel, std::nullopt);
+  EXPECT_EQ(Output->CodeModel, CodeModel::Large);
+  EXPECT_EQ(Output->CGOptLevel, CodeGenOptLevel::Aggressive);
+  EXPECT_EQ(Output->OptLevel, 3u);
+  EXPECT_TRUE(Output->Dtlto);
+  EXPECT_EQ(Output->RemarksHotnessThreshold,
+            std::numeric_limits<uint64_t>::max());
+  EXPECT_EQ(Output->TimeTraceGranularity, std::numeric_limits<unsigned>::max());
+  EXPECT_EQ(Output->ThinLTOModulesToCompile, Input.ThinLTOModulesToCompile);
+  EXPECT_TRUE(Output->PTO.LoopInterchange);
+  EXPECT_EQ(Output->PTO.LicmMssaOptCap, std::numeric_limits<unsigned>::max());
+  EXPECT_EQ(Output->PTO.InlinerThreshold, 42);
+  EXPECT_TRUE(Output->Options.FunctionSections);
+  EXPECT_EQ(Output->Options.MCOptions.IASSearchPaths,
+            Input.Options.MCOptions.IASSearchPaths);
+}
+
+TEST(LTOConfigBitcodeTest, RoundTripThroughThinLTOSummaryIndex) {
+  ModuleSummaryIndex InputIndex(/*HaveGVs=*/false);
+  Config InputConfig;
+  InputConfig.CPU = "summary-cpu";
+  InputConfig.OptLevel = 3;
+  InputConfig.Options.DataSections = true;
+
+  SmallString<0> Storage;
+  raw_svector_ostream OS(Storage);
+  ASSERT_THAT_ERROR(writeIndexWithLTOConfigToFile(InputIndex, InputConfig, OS),
+                    Succeeded());
+
+  MemoryBufferRef Buffer(StringRef(Storage.data(), Storage.size()),
+                         "summary.thinlto.bc");
+
+  // Existing summary-index readers must accept the embedded metadata.
+  ModuleSummaryIndex OutputIndex(/*HaveGVs=*/false);
+  ASSERT_THAT_ERROR(readModuleSummaryIndex(Buffer, OutputIndex), Succeeded());
+
+  Expected<Config> OutputConfig = readLTOConfigFromSummaryIndex(Buffer);
+  ASSERT_THAT_EXPECTED(OutputConfig, Succeeded());
+  EXPECT_EQ(OutputConfig->CPU, "summary-cpu");
+  EXPECT_EQ(OutputConfig->OptLevel, 3u);
+  EXPECT_TRUE(OutputConfig->Options.DataSections);
+}
+
+TEST(LTOConfigBitcodeTest, ThinLTOSummaryIndexWithoutConfig) {
+  ModuleSummaryIndex Index(/*HaveGVs=*/false);
+  SmallString<0> Storage;
+  raw_svector_ostream OS(Storage);
+  writeIndexToFile(Index, OS);
+
+  MemoryBufferRef Buffer(StringRef(Storage.data(), Storage.size()),
+                         "summary.thinlto.bc");
+  Expected<std::optional<Config>> OutputConfig =
+      readLTOConfigFromSummaryIndexIfPresent(Buffer);
+  ASSERT_THAT_EXPECTED(OutputConfig, Succeeded());
+  EXPECT_FALSE(OutputConfig->has_value());
+}



More information about the llvm-commits mailing list