aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAnuj Mittal <anuj.mittal@intel.com>2021-10-21 23:40:23 +0800
committerAnuj Mittal <anuj.mittal@intel.com>2021-10-21 23:42:39 +0800
commit3b386a9dc80eb61055b74d93f2da582ce2c652c7 (patch)
tree1a522e8924fec699408907dd0a96c72ac50d41b4
parent272e0889026e6479fd366d4447e7345e4dd52d11 (diff)
downloadmeta-intel-master.tar.gz
meta-intel-master.tar.bz2
meta-intel-master.zip
llvm-project-source/10.0.1: backport fixes from LLVM 11HEADmaster
Include a fix and two supporting patches from LLVM 11 to fix problems with double registration while both clang-cpp and llvm lib are linked (like in case of opencl-clang). | CommandLine Error: Option 'mc-relax-all' registered more than once! | LLVM ERROR: inconsistency in registered CommandLine options Signed-off-by: Anuj Mittal <anuj.mittal@intel.com>
-rw-r--r--dynamic-layers/clang-layer/recipes-devtools/clang/files/llvm10-Enable-the-call-site-info-only-for-g-optimizations.patch550
-rw-r--r--dynamic-layers/clang-layer/recipes-devtools/clang/files/llvm10-Replace-MCTargetOptionsCommandFlags.inc-and-CommandF.patch2243
-rw-r--r--dynamic-layers/clang-layer/recipes-devtools/clang/files/llvm10-basic-block-sections-support.patch237
-rw-r--r--dynamic-layers/clang-layer/recipes-devtools/clang/llvm-project-source.bbappend3
4 files changed, 3033 insertions, 0 deletions
diff --git a/dynamic-layers/clang-layer/recipes-devtools/clang/files/llvm10-Enable-the-call-site-info-only-for-g-optimizations.patch b/dynamic-layers/clang-layer/recipes-devtools/clang/files/llvm10-Enable-the-call-site-info-only-for-g-optimizations.patch
new file mode 100644
index 00000000..2e3872dc
--- /dev/null
+++ b/dynamic-layers/clang-layer/recipes-devtools/clang/files/llvm10-Enable-the-call-site-info-only-for-g-optimizations.patch
@@ -0,0 +1,550 @@
+From 447cb2e1b2f0d8bdcfd8a0b39f47d28de50b5d82 Mon Sep 17 00:00:00 2001
+From: Djordje Todorovic <djordje.todorovic@syrmia.com>
+Date: Mon, 9 Mar 2020 11:02:35 +0100
+Subject: [PATCH] Enable the call site info only for -g + optimizations
+
+Emit call site info only in the case of '-g' + 'O>0' level.
+
+Differential Revision: https://reviews.llvm.org/D75175
+
+Upstream-Status: Backport [https://github.com/llvm/llvm-project/commit/c15c68abdc6f1afece637bdedba808676191a8e6]
+Signed-off-by: Anuj Mittal <anuj.mittal@intel.com>
+---
+ clang/include/clang/Basic/CodeGenOptions.def | 2 ++
+ clang/lib/CodeGen/BackendUtil.cpp | 1 +
+ clang/lib/Frontend/CompilerInvocation.cpp | 4 +++-
+ llvm/include/llvm/CodeGen/CommandFlags.inc | 7 +++++++
+ llvm/include/llvm/Target/TargetOptions.h | 7 ++++++-
+ llvm/lib/CodeGen/MIRParser/MIRParser.cpp | 4 ++--
+ llvm/lib/CodeGen/MachineFunction.cpp | 2 +-
+ llvm/lib/CodeGen/SelectionDAG/ScheduleDAGSDNodes.cpp | 2 +-
+ llvm/lib/Target/AArch64/AArch64ISelLowering.cpp | 2 +-
+ llvm/lib/Target/ARM/ARMISelLowering.cpp | 2 +-
+ llvm/lib/Target/X86/X86ISelLowering.cpp | 2 +-
+ llvm/test/CodeGen/MIR/Hexagon/bundled-call-site-info.mir | 2 +-
+ llvm/test/CodeGen/X86/call-site-info-output.ll | 4 ++--
+ llvm/test/DebugInfo/AArch64/call-site-info-output.ll | 2 +-
+ llvm/test/DebugInfo/ARM/call-site-info-output.ll | 2 +-
+ .../MIR/AArch64/dbgcall-site-interpret-movzxi.mir | 2 +-
+ .../DebugInfo/MIR/AArch64/dbgcall-site-interpretation.mir | 2 +-
+ llvm/test/DebugInfo/MIR/AArch64/dbgcall-site-orr-moves.mir | 2 +-
+ .../test/DebugInfo/MIR/AArch64/implicit-def-dead-scope.mir | 2 +-
+ .../test/DebugInfo/MIR/ARM/dbgcall-site-interpretation.mir | 2 +-
+ .../DebugInfo/MIR/ARM/dbgcall-site-propagated-value.mir | 2 +-
+ llvm/test/DebugInfo/MIR/ARM/if-coverter-call-site-info.mir | 2 +-
+ .../MIR/Hexagon/dbgcall-site-instr-before-bundled-call.mir | 2 +-
+ .../MIR/Hexagon/live-debug-values-bundled-entry-values.mir | 2 +-
+ llvm/test/DebugInfo/MIR/SystemZ/call-site-lzer.mir | 2 +-
+ llvm/test/DebugInfo/MIR/X86/DW_OP_entry_value.mir | 2 +-
+ llvm/test/DebugInfo/MIR/X86/dbg-call-site-spilled-arg.mir | 2 +-
+ .../test/DebugInfo/MIR/X86/dbgcall-site-copy-super-sub.mir | 2 +-
+ .../test/DebugInfo/MIR/X86/dbgcall-site-interpretation.mir | 2 +-
+ .../DebugInfo/MIR/X86/dbgcall-site-lea-interpretation.mir | 2 +-
+ llvm/test/DebugInfo/MIR/X86/dbgcall-site-reference.mir | 2 +-
+ .../DebugInfo/MIR/X86/dbgcall-site-two-fwd-reg-defs.mir | 2 +-
+ llvm/test/DebugInfo/MIR/X86/dbginfo-entryvals.mir | 2 +-
+ llvm/test/DebugInfo/MIR/X86/debug-call-site-param.mir | 4 ++--
+ .../DebugInfo/MIR/X86/entry-value-of-modified-param.mir | 2 +-
+ llvm/test/DebugInfo/MIR/X86/entry-values-diamond-bbs.mir | 2 +-
+ .../DebugInfo/MIR/X86/propagate-entry-value-cross-bbs.mir | 2 +-
+ .../test/DebugInfo/MIR/X86/unreachable-block-call-site.mir | 2 +-
+ llvm/test/DebugInfo/X86/dbgcall-site-64-bit-imms.ll | 2 +-
+ llvm/test/DebugInfo/X86/dbgcall-site-zero-valued-imms.ll | 2 +-
+ .../tools/llvm-dwarfdump/X86/stats-dbg-callsite-info.ll | 2 +-
+ 41 files changed, 58 insertions(+), 41 deletions(-)
+
+diff --git a/clang/include/clang/Basic/CodeGenOptions.def b/clang/include/clang/Basic/CodeGenOptions.def
+index 1ecae98b13b1..6a6a9465273f 100644
+--- a/clang/include/clang/Basic/CodeGenOptions.def
++++ b/clang/include/clang/Basic/CodeGenOptions.def
+@@ -64,6 +64,8 @@ CODEGENOPT(DebugPassManager, 1, 0) ///< Prints debug information for the new
+ ///< pass manager.
+ CODEGENOPT(DisableRedZone , 1, 0) ///< Set when -mno-red-zone is enabled.
+ CODEGENOPT(EnableDebugEntryValues, 1, 0) ///< Emit call site parameter dbg info
++CODEGENOPT(EmitCallSiteInfo, 1, 0) ///< Emit call site info only in the case of
++ ///< '-g' + 'O>0' level.
+ CODEGENOPT(IndirectTlsSegRefs, 1, 0) ///< Set when -mno-tls-direct-seg-refs
+ ///< is specified.
+ CODEGENOPT(DisableTailCalls , 1, 0) ///< Do not emit tail calls.
+diff --git a/clang/lib/CodeGen/BackendUtil.cpp b/clang/lib/CodeGen/BackendUtil.cpp
+index db8fd4166d7a..db09f9b641fe 100644
+--- a/clang/lib/CodeGen/BackendUtil.cpp
++++ b/clang/lib/CodeGen/BackendUtil.cpp
+@@ -482,6 +482,7 @@ static void initTargetOptions(llvm::TargetOptions &Options,
+ Options.EmitAddrsig = CodeGenOpts.Addrsig;
+ Options.EnableDebugEntryValues = CodeGenOpts.EnableDebugEntryValues;
+ Options.ForceDwarfFrameSection = CodeGenOpts.ForceDwarfFrameSection;
++ Options.EmitCallSiteInfo = CodeGenOpts.EmitCallSiteInfo;
+
+ Options.MCOptions.SplitDwarfFile = CodeGenOpts.SplitDwarfFile;
+ Options.MCOptions.MCRelaxAll = CodeGenOpts.RelaxAll;
+diff --git a/clang/lib/Frontend/CompilerInvocation.cpp b/clang/lib/Frontend/CompilerInvocation.cpp
+index 18fa06bf3c6d..2e73dcbdebe4 100644
+--- a/clang/lib/Frontend/CompilerInvocation.cpp
++++ b/clang/lib/Frontend/CompilerInvocation.cpp
+@@ -789,8 +789,10 @@ static bool ParseCodeGenArgs(CodeGenOptions &Opts, ArgList &Args, InputKind IK,
+
+ llvm::Triple T(TargetOpts.Triple);
+ if (Opts.OptimizationLevel > 0 && Opts.hasReducedDebugInfo() &&
+- llvm::is_contained(DebugEntryValueArchs, T.getArch()))
++ llvm::is_contained(DebugEntryValueArchs, T.getArch())) {
+ Opts.EnableDebugEntryValues = Args.hasArg(OPT_femit_debug_entry_values);
++ Opts.EmitCallSiteInfo = true;
++ }
+
+ Opts.DisableO0ImplyOptNone = Args.hasArg(OPT_disable_O0_optnone);
+ Opts.DisableRedZone = Args.hasArg(OPT_disable_red_zone);
+diff --git a/llvm/include/llvm/CodeGen/CommandFlags.inc b/llvm/include/llvm/CodeGen/CommandFlags.inc
+index 6475a5b19edb..36073fe9cc98 100644
+--- a/llvm/include/llvm/CodeGen/CommandFlags.inc
++++ b/llvm/include/llvm/CodeGen/CommandFlags.inc
+@@ -286,6 +286,12 @@ static cl::opt<bool>
+ EnableAddrsig("addrsig", cl::desc("Emit an address-significance table"),
+ cl::init(false));
+
++static cl::opt<bool> EmitCallSiteInfo(
++ "emit-call-site-info",
++ cl::desc(
++ "Emit call site debug information, if debug information is enabled."),
++ cl::init(false));
++
+ static cl::opt<bool>
+ EnableDebugEntryValues("debug-entry-values",
+ cl::desc("Emit debug info about parameter's entry values"),
+@@ -349,6 +355,7 @@ static TargetOptions InitTargetOptionsFromCodeGenFlags() {
+ Options.ExceptionModel = ExceptionModel;
+ Options.EmitStackSizeSection = EnableStackSizeSection;
+ Options.EmitAddrsig = EnableAddrsig;
++ Options.EmitCallSiteInfo = EmitCallSiteInfo;
+ Options.EnableDebugEntryValues = EnableDebugEntryValues;
+ Options.ForceDwarfFrameSection = ForceDwarfFrameSection;
+
+diff --git a/llvm/include/llvm/Target/TargetOptions.h b/llvm/include/llvm/Target/TargetOptions.h
+index d27c7b0178f0..9378e290bed1 100644
+--- a/llvm/include/llvm/Target/TargetOptions.h
++++ b/llvm/include/llvm/Target/TargetOptions.h
+@@ -134,7 +134,8 @@ namespace llvm {
+ EmulatedTLS(false), ExplicitEmulatedTLS(false), EnableIPRA(false),
+ EmitStackSizeSection(false), EnableMachineOutliner(false),
+ SupportsDefaultOutlining(false), EmitAddrsig(false),
+- EnableDebugEntryValues(false), ForceDwarfFrameSection(false) {}
++ EmitCallSiteInfo(false), EnableDebugEntryValues(false),
++ ForceDwarfFrameSection(false) {}
+
+ /// PrintMachineCode - This flag is enabled when the -print-machineinstrs
+ /// option is specified on the command line, and should enable debugging
+@@ -281,6 +282,10 @@ namespace llvm {
+ /// to selectively generate basic block sections.
+ std::shared_ptr<MemoryBuffer> BBSectionsFuncListBuf;
+
++ /// 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;
+ /// Emit debug info about parameter's entry values.
+ unsigned EnableDebugEntryValues : 1;
+
+diff --git a/llvm/lib/CodeGen/MIRParser/MIRParser.cpp b/llvm/lib/CodeGen/MIRParser/MIRParser.cpp
+index 10157c746b46..f955bdc6186a 100644
+--- a/llvm/lib/CodeGen/MIRParser/MIRParser.cpp
++++ b/llvm/lib/CodeGen/MIRParser/MIRParser.cpp
+@@ -381,11 +381,11 @@ bool MIRParserImpl::initializeCallSiteInfo(
+ CSInfo.emplace_back(Reg, ArgRegPair.ArgNo);
+ }
+
+- if (TM.Options.EnableDebugEntryValues)
++ if (TM.Options.EmitCallSiteInfo)
+ MF.addCallArgsForwardingRegs(&*CallI, std::move(CSInfo));
+ }
+
+- if (YamlMF.CallSitesInfo.size() && !TM.Options.EnableDebugEntryValues)
++ if (YamlMF.CallSitesInfo.size() && !TM.Options.EmitCallSiteInfo)
+ return error(Twine("Call site info provided but not used"));
+ return false;
+ }
+diff --git a/llvm/lib/CodeGen/MachineFunction.cpp b/llvm/lib/CodeGen/MachineFunction.cpp
+index 4612690644fe..c3795b7ed314 100644
+--- a/llvm/lib/CodeGen/MachineFunction.cpp
++++ b/llvm/lib/CodeGen/MachineFunction.cpp
+@@ -855,7 +855,7 @@ MachineFunction::CallSiteInfoMap::iterator
+ MachineFunction::getCallSiteInfo(const MachineInstr *MI) {
+ assert(MI->isCall() && "Call site info refers only to call instructions!");
+
+- if (!Target.Options.EnableDebugEntryValues)
++ if (!Target.Options.EmitCallSiteInfo)
+ return CallSitesInfo.end();
+ return CallSitesInfo.find(MI);
+ }
+diff --git a/llvm/lib/CodeGen/SelectionDAG/ScheduleDAGSDNodes.cpp b/llvm/lib/CodeGen/SelectionDAG/ScheduleDAGSDNodes.cpp
+index 0e4d783e3505..52099f24aca5 100644
+--- a/llvm/lib/CodeGen/SelectionDAG/ScheduleDAGSDNodes.cpp
++++ b/llvm/lib/CodeGen/SelectionDAG/ScheduleDAGSDNodes.cpp
+@@ -863,7 +863,7 @@ EmitSchedule(MachineBasicBlock::iterator &InsertPos) {
+ MI = &*std::next(Before);
+ }
+
+- if (MI->isCall() && DAG->getTarget().Options.EnableDebugEntryValues)
++ if (MI->isCall() && DAG->getTarget().Options.EmitCallSiteInfo)
+ MF.addCallArgsForwardingRegs(MI, DAG->getSDCallSiteInfo(Node));
+
+ return MI;
+diff --git a/llvm/lib/Target/AArch64/AArch64ISelLowering.cpp b/llvm/lib/Target/AArch64/AArch64ISelLowering.cpp
+index 23f05eaad944..63ff3031a5e8 100644
+--- a/llvm/lib/Target/AArch64/AArch64ISelLowering.cpp
++++ b/llvm/lib/Target/AArch64/AArch64ISelLowering.cpp
+@@ -4132,7 +4132,7 @@ AArch64TargetLowering::LowerCall(CallLoweringInfo &CLI,
+ RegsToPass.emplace_back(VA.getLocReg(), Arg);
+ RegsUsed.insert(VA.getLocReg());
+ const TargetOptions &Options = DAG.getTarget().Options;
+- if (Options.EnableDebugEntryValues)
++ if (Options.EmitCallSiteInfo)
+ CSInfo.emplace_back(VA.getLocReg(), i);
+ }
+ } else {
+diff --git a/llvm/lib/Target/ARM/ARMISelLowering.cpp b/llvm/lib/Target/ARM/ARMISelLowering.cpp
+index 9f504b1eaa42..5589ba34a2ac 100644
+--- a/llvm/lib/Target/ARM/ARMISelLowering.cpp
++++ b/llvm/lib/Target/ARM/ARMISelLowering.cpp
+@@ -2222,7 +2222,7 @@ ARMTargetLowering::LowerCall(TargetLowering::CallLoweringInfo &CLI,
+ isThisReturn = true;
+ }
+ const TargetOptions &Options = DAG.getTarget().Options;
+- if (Options.EnableDebugEntryValues)
++ if (Options.EmitCallSiteInfo)
+ CSInfo.emplace_back(VA.getLocReg(), i);
+ RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
+ } else if (isByVal) {
+diff --git a/llvm/lib/Target/X86/X86ISelLowering.cpp b/llvm/lib/Target/X86/X86ISelLowering.cpp
+index d5de94aeb8a2..4808bdf6ddc2 100644
+--- a/llvm/lib/Target/X86/X86ISelLowering.cpp
++++ b/llvm/lib/Target/X86/X86ISelLowering.cpp
+@@ -4030,7 +4030,7 @@ X86TargetLowering::LowerCall(TargetLowering::CallLoweringInfo &CLI,
+ } else if (VA.isRegLoc()) {
+ RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
+ const TargetOptions &Options = DAG.getTarget().Options;
+- if (Options.EnableDebugEntryValues)
++ if (Options.EmitCallSiteInfo)
+ CSInfo.emplace_back(VA.getLocReg(), I);
+ if (isVarArg && IsWin64) {
+ // Win64 ABI requires argument XMM reg to be copied to the corresponding
+diff --git a/llvm/test/CodeGen/MIR/Hexagon/bundled-call-site-info.mir b/llvm/test/CodeGen/MIR/Hexagon/bundled-call-site-info.mir
+index 5ffa0293a2e1..fec542223fc9 100644
+--- a/llvm/test/CodeGen/MIR/Hexagon/bundled-call-site-info.mir
++++ b/llvm/test/CodeGen/MIR/Hexagon/bundled-call-site-info.mir
+@@ -1,4 +1,4 @@
+-# RUN: llc -debug-entry-values -run-pass=none -verify-machineinstrs -o - %s | FileCheck %s
++# RUN: llc -emit-call-site-info -debug-entry-values -run-pass=none -verify-machineinstrs -o - %s | FileCheck %s
+
+ # Verify that it is possible to read and write MIR where a callSites entry
+ # points to a call residing in a bundle. The offset should point to the call
+diff --git a/llvm/test/CodeGen/X86/call-site-info-output.ll b/llvm/test/CodeGen/X86/call-site-info-output.ll
+index 4b1e236aadfe..a0438f0c2b98 100644
+--- a/llvm/test/CodeGen/X86/call-site-info-output.ll
++++ b/llvm/test/CodeGen/X86/call-site-info-output.ll
+@@ -1,6 +1,6 @@
+ ; Test call site info MIR printer and parser.Parser assertions and machine
+ ; verifier will check the rest;
+-; RUN: llc -debug-entry-values %s -stop-before=finalize-isel -o %t.mir
++; RUN: llc -emit-call-site-info -debug-entry-values %s -stop-before=finalize-isel -o %t.mir
+ ; RUN: cat %t.mir | FileCheck %s
+ ; CHECK: name: fn2
+ ; CHECK: callSites:
+@@ -10,7 +10,7 @@
+ ; CHECK-NEXT: arg: 0, reg: '$edi'
+ ; CHECK-NEXT: arg: 1, reg: '$esi'
+ ; CHECK-NEXT: arg: 2, reg: '$edx'
+-; RUN: llc -debug-entry-values %t.mir -run-pass=finalize-isel -o -| FileCheck %s --check-prefix=PARSER
++; RUN: llc -emit-call-site-info -debug-entry-values %t.mir -run-pass=finalize-isel -o -| FileCheck %s --check-prefix=PARSER
+ ; Verify that we are able to parse output mir and that we are getting the same result.
+ ; PARSER: name: fn2
+ ; PARSER: callSites:
+diff --git a/llvm/test/DebugInfo/AArch64/call-site-info-output.ll b/llvm/test/DebugInfo/AArch64/call-site-info-output.ll
+index d52d6962f3c4..17d9f7f18762 100644
+--- a/llvm/test/DebugInfo/AArch64/call-site-info-output.ll
++++ b/llvm/test/DebugInfo/AArch64/call-site-info-output.ll
+@@ -1,4 +1,4 @@
+-; RUN: llc -mtriple aarch64-linux-gnu -debug-entry-values %s -o - -stop-before=finalize-isel | FileCheck %s
++; RUN: llc -emit-call-site-info -mtriple aarch64-linux-gnu -debug-entry-values %s -o - -stop-before=finalize-isel | FileCheck %s
+ ; Verify that Selection DAG knows how to recognize simple function parameter forwarding registers.
+ ; Produced from:
+ ; extern int fn1(int,int,int);
+diff --git a/llvm/test/DebugInfo/ARM/call-site-info-output.ll b/llvm/test/DebugInfo/ARM/call-site-info-output.ll
+index 9255a7d57dde..ed726dfe753f 100644
+--- a/llvm/test/DebugInfo/ARM/call-site-info-output.ll
++++ b/llvm/test/DebugInfo/ARM/call-site-info-output.ll
+@@ -1,4 +1,4 @@
+-; RUN: llc -mtriple arm-linux-gnu -debug-entry-values %s -o - -stop-before=finalize-isel | FileCheck %s
++; RUN: llc -emit-call-site-info -mtriple arm-linux-gnu -debug-entry-values %s -o - -stop-before=finalize-isel | FileCheck %s
+ ; Verify that Selection DAG knows how to recognize simple function parameter forwarding registers.
+ ; Produced from:
+ ; extern int fn1(int,int,int);
+diff --git a/llvm/test/DebugInfo/MIR/AArch64/dbgcall-site-interpret-movzxi.mir b/llvm/test/DebugInfo/MIR/AArch64/dbgcall-site-interpret-movzxi.mir
+index dc7561ca6400..057779a90721 100644
+--- a/llvm/test/DebugInfo/MIR/AArch64/dbgcall-site-interpret-movzxi.mir
++++ b/llvm/test/DebugInfo/MIR/AArch64/dbgcall-site-interpret-movzxi.mir
+@@ -1,4 +1,4 @@
+-# RUN: llc -mtriple aarch64-linux-gnu -debug-entry-values -start-after=machineverifier -filetype=obj %s -o -| llvm-dwarfdump -| FileCheck %s
++# RUN: llc -emit-call-site-info -mtriple aarch64-linux-gnu -debug-entry-values -start-after=machineverifier -filetype=obj %s -o -| llvm-dwarfdump -| FileCheck %s
+ #
+ # Based on the following C reproducer:
+ #
+diff --git a/llvm/test/DebugInfo/MIR/AArch64/dbgcall-site-interpretation.mir b/llvm/test/DebugInfo/MIR/AArch64/dbgcall-site-interpretation.mir
+index 0371ccef603e..d925bc395878 100644
+--- a/llvm/test/DebugInfo/MIR/AArch64/dbgcall-site-interpretation.mir
++++ b/llvm/test/DebugInfo/MIR/AArch64/dbgcall-site-interpretation.mir
+@@ -1,4 +1,4 @@
+-# RUN: llc -mtriple aarch64-linux-gnu -debug-entry-values -start-after=machineverifier -filetype=obj %s -o -| llvm-dwarfdump -| FileCheck %s
++# RUN: llc -emit-call-site-info -mtriple aarch64-linux-gnu -debug-entry-values -start-after=machineverifier -filetype=obj %s -o -| llvm-dwarfdump -| FileCheck %s
+ # Following code is used for producing this test case. Note that
+ # some of argument loading instruction are modified in order to
+ # cover certain cases.
+diff --git a/llvm/test/DebugInfo/MIR/AArch64/dbgcall-site-orr-moves.mir b/llvm/test/DebugInfo/MIR/AArch64/dbgcall-site-orr-moves.mir
+index 916a14022ba5..4a87dad3b9b5 100644
+--- a/llvm/test/DebugInfo/MIR/AArch64/dbgcall-site-orr-moves.mir
++++ b/llvm/test/DebugInfo/MIR/AArch64/dbgcall-site-orr-moves.mir
+@@ -1,4 +1,4 @@
+-# RUN: llc -debug-entry-values -start-after=livedebugvalues -filetype=obj -o - %s | llvm-dwarfdump - | FileCheck %s
++# RUN: llc -emit-call-site-info -debug-entry-values -start-after=livedebugvalues -filetype=obj -o - %s | llvm-dwarfdump - | FileCheck %s
+
+ # Based on the following C reproducer:
+ #
+diff --git a/llvm/test/DebugInfo/MIR/AArch64/implicit-def-dead-scope.mir b/llvm/test/DebugInfo/MIR/AArch64/implicit-def-dead-scope.mir
+index fbf9b3454689..d85f2d25391d 100644
+--- a/llvm/test/DebugInfo/MIR/AArch64/implicit-def-dead-scope.mir
++++ b/llvm/test/DebugInfo/MIR/AArch64/implicit-def-dead-scope.mir
+@@ -1,4 +1,4 @@
+-# RUN: llc -start-after=livedebugvalues -filetype=obj -o - %s \
++# RUN: llc -emit-call-site-info -start-after=livedebugvalues -filetype=obj -o - %s \
+ # RUN: | llvm-dwarfdump -v - | FileCheck %s
+
+ # This tests for a crash in DwarfDebug's singular DBG_VALUE range promotion when
+diff --git a/llvm/test/DebugInfo/MIR/ARM/dbgcall-site-interpretation.mir b/llvm/test/DebugInfo/MIR/ARM/dbgcall-site-interpretation.mir
+index ce8dc97f0e72..0ae4e6ec485c 100644
+--- a/llvm/test/DebugInfo/MIR/ARM/dbgcall-site-interpretation.mir
++++ b/llvm/test/DebugInfo/MIR/ARM/dbgcall-site-interpretation.mir
+@@ -1,4 +1,4 @@
+-# RUN: llc -mtriple=arm-linux-gnueabi -debug-entry-values -filetype=obj -start-after=machineverifier %s -o -| llvm-dwarfdump -| FileCheck %s
++# RUN: llc -emit-call-site-info -mtriple=arm-linux-gnueabi -debug-entry-values -filetype=obj -start-after=machineverifier %s -o -| llvm-dwarfdump -| FileCheck %s
+ # Following code is used for producing this test case. Note that
+ # some of argument loading instruction are modified in order to
+ # cover certain cases.
+diff --git a/llvm/test/DebugInfo/MIR/ARM/dbgcall-site-propagated-value.mir b/llvm/test/DebugInfo/MIR/ARM/dbgcall-site-propagated-value.mir
+index 9001c8ba8eea..5b84d9e9627f 100644
+--- a/llvm/test/DebugInfo/MIR/ARM/dbgcall-site-propagated-value.mir
++++ b/llvm/test/DebugInfo/MIR/ARM/dbgcall-site-propagated-value.mir
+@@ -1,4 +1,4 @@
+-# RUN: llc -debug-entry-values -run-pass=livedebugvalues -o - %s | FileCheck %s
++# RUN: llc -emit-call-site-info -debug-entry-values -run-pass=livedebugvalues -o - %s | FileCheck %s
+
+ # Based on the following C reproducer:
+ #
+diff --git a/llvm/test/DebugInfo/MIR/ARM/if-coverter-call-site-info.mir b/llvm/test/DebugInfo/MIR/ARM/if-coverter-call-site-info.mir
+index aa7b54c1e5bb..11e9c4c90836 100644
+--- a/llvm/test/DebugInfo/MIR/ARM/if-coverter-call-site-info.mir
++++ b/llvm/test/DebugInfo/MIR/ARM/if-coverter-call-site-info.mir
+@@ -1,4 +1,4 @@
+-# RUN: llc -mtriple=arm-linux-gnu -debug-entry-values -run-pass if-converter %s -o -| FileCheck %s
++# RUN: llc -emit-call-site-info -mtriple=arm-linux-gnu -debug-entry-values -run-pass if-converter %s -o -| FileCheck %s
+
+ # Vefify that the call site info will be updated after the optimization.
+ # This test case would previously trigger an assertion when
+diff --git a/llvm/test/DebugInfo/MIR/Hexagon/dbgcall-site-instr-before-bundled-call.mir b/llvm/test/DebugInfo/MIR/Hexagon/dbgcall-site-instr-before-bundled-call.mir
+index 8ae628af2c09..3ae23d4189bf 100644
+--- a/llvm/test/DebugInfo/MIR/Hexagon/dbgcall-site-instr-before-bundled-call.mir
++++ b/llvm/test/DebugInfo/MIR/Hexagon/dbgcall-site-instr-before-bundled-call.mir
+@@ -1,4 +1,4 @@
+-# RUN: llc -mtriple hexagon -debug-entry-values -start-after=machineverifier -filetype=obj %s -o - | llvm-dwarfdump - | FileCheck %s
++# RUN: llc -mtriple hexagon -emit-call-site-info -debug-entry-values -start-after=machineverifier -filetype=obj %s -o - | llvm-dwarfdump - | FileCheck %s
+
+ # Based on the following C reproducer:
+ #
+diff --git a/llvm/test/DebugInfo/MIR/Hexagon/live-debug-values-bundled-entry-values.mir b/llvm/test/DebugInfo/MIR/Hexagon/live-debug-values-bundled-entry-values.mir
+index ff0a539dd15d..8bb0b3202acd 100644
+--- a/llvm/test/DebugInfo/MIR/Hexagon/live-debug-values-bundled-entry-values.mir
++++ b/llvm/test/DebugInfo/MIR/Hexagon/live-debug-values-bundled-entry-values.mir
+@@ -1,4 +1,4 @@
+-# RUN: llc -debug-entry-values -run-pass=livedebugvalues -o - %s | FileCheck %s
++# RUN: llc -emit-call-site-info -debug-entry-values -run-pass=livedebugvalues -o - %s | FileCheck %s
+
+ # Verify that the entry values for the input parameters are inserted after the
+ # bundles which contains the registers' clobbering instructions (the calls to
+diff --git a/llvm/test/DebugInfo/MIR/SystemZ/call-site-lzer.mir b/llvm/test/DebugInfo/MIR/SystemZ/call-site-lzer.mir
+index 8a4e8b5632c2..3cf41467f7f9 100644
+--- a/llvm/test/DebugInfo/MIR/SystemZ/call-site-lzer.mir
++++ b/llvm/test/DebugInfo/MIR/SystemZ/call-site-lzer.mir
+@@ -1,4 +1,4 @@
+-# RUN: llc -debug-entry-values -start-after=livedebugvalues -o - %s | FileCheck %s
++# RUN: llc -emit-call-site-info -debug-entry-values -start-after=livedebugvalues -o - %s | FileCheck %s
+
+ # This test would previously trigger an assertion when trying to describe the
+ # call site value for callee()'s float parameter.
+diff --git a/llvm/test/DebugInfo/MIR/X86/DW_OP_entry_value.mir b/llvm/test/DebugInfo/MIR/X86/DW_OP_entry_value.mir
+index e6fe5d2de878..4e5a07321d42 100644
+--- a/llvm/test/DebugInfo/MIR/X86/DW_OP_entry_value.mir
++++ b/llvm/test/DebugInfo/MIR/X86/DW_OP_entry_value.mir
+@@ -1,4 +1,4 @@
+-# RUN: llc -debug-entry-values -start-before=livedebugvalues -mtriple=x86_64-apple-darwin -o %t %s -filetype=obj
++# RUN: llc -emit-call-site-info -debug-entry-values -start-before=livedebugvalues -mtriple=x86_64-apple-darwin -o %t %s -filetype=obj
+ # RUN: llvm-dwarfdump %t | FileCheck %s
+ #
+ # int global;
+diff --git a/llvm/test/DebugInfo/MIR/X86/dbg-call-site-spilled-arg.mir b/llvm/test/DebugInfo/MIR/X86/dbg-call-site-spilled-arg.mir
+index c32a1155d038..edeef2c7aed4 100644
+--- a/llvm/test/DebugInfo/MIR/X86/dbg-call-site-spilled-arg.mir
++++ b/llvm/test/DebugInfo/MIR/X86/dbg-call-site-spilled-arg.mir
+@@ -1,6 +1,6 @@
+ # Check that llvm can describe a call site parameter which resides in a spill slot.
+ #
+-# RUN: llc -debug-entry-values -start-after=machineverifier -filetype=obj %s -o - | llvm-dwarfdump - | FileCheck %s
++# RUN: llc -emit-call-site-info -debug-entry-values -start-after=machineverifier -filetype=obj %s -o - | llvm-dwarfdump - | FileCheck %s
+ #
+ # Command:
+ # $ ~/src/builds/llvm-project-master-RA/bin/clang -g -Xclang -femit-debug-entry-values -O2 -c -o spill.o spill.cc -mllvm -stop-before=machineverifier -o spill.mir
+diff --git a/llvm/test/DebugInfo/MIR/X86/dbgcall-site-copy-super-sub.mir b/llvm/test/DebugInfo/MIR/X86/dbgcall-site-copy-super-sub.mir
+index a2d51a203512..01a2b887a60b 100644
+--- a/llvm/test/DebugInfo/MIR/X86/dbgcall-site-copy-super-sub.mir
++++ b/llvm/test/DebugInfo/MIR/X86/dbgcall-site-copy-super-sub.mir
+@@ -1,4 +1,4 @@
+-# RUN: llc -debug-entry-values -start-after=livedebugvalues -filetype=obj %s -o -| llvm-dwarfdump -| FileCheck %s
++# RUN: llc -emit-call-site-info -debug-entry-values -start-after=livedebugvalues -filetype=obj %s -o -| llvm-dwarfdump -| FileCheck %s
+
+ # Based on the following reproducer:
+ #
+diff --git a/llvm/test/DebugInfo/MIR/X86/dbgcall-site-interpretation.mir b/llvm/test/DebugInfo/MIR/X86/dbgcall-site-interpretation.mir
+index f9e9459f1abd..104bc0146798 100644
+--- a/llvm/test/DebugInfo/MIR/X86/dbgcall-site-interpretation.mir
++++ b/llvm/test/DebugInfo/MIR/X86/dbgcall-site-interpretation.mir
+@@ -1,4 +1,4 @@
+-# RUN: llc -debug-entry-values -start-after=machineverifier -filetype=obj %s -o -| llvm-dwarfdump -| FileCheck %s
++# RUN: llc -emit-call-site-info -debug-entry-values -start-after=machineverifier -filetype=obj %s -o -| llvm-dwarfdump -| FileCheck %s
+ #
+ # CHECK: DW_TAG_GNU_call_site
+ # CHECK-NEXT: DW_AT_abstract_origin {{.*}} "foo"
+diff --git a/llvm/test/DebugInfo/MIR/X86/dbgcall-site-lea-interpretation.mir b/llvm/test/DebugInfo/MIR/X86/dbgcall-site-lea-interpretation.mir
+index 1bb70f6d4530..4d88fa9aab74 100644
+--- a/llvm/test/DebugInfo/MIR/X86/dbgcall-site-lea-interpretation.mir
++++ b/llvm/test/DebugInfo/MIR/X86/dbgcall-site-lea-interpretation.mir
+@@ -1,4 +1,4 @@
+-# RUN: llc -debug-entry-values -start-after=machineverifier -filetype=obj %s -o -| llvm-dwarfdump -| FileCheck %s
++# RUN: llc -emit-call-site-info -debug-entry-values -start-after=machineverifier -filetype=obj %s -o -| llvm-dwarfdump -| FileCheck %s
+ # CHECK: DW_TAG_GNU_call_site
+ # CHECK-NEXT: DW_AT_abstract_origin {{.*}} "foo")
+ # CHECK-NEXT: DW_AT_low_pc {{.*}}
+diff --git a/llvm/test/DebugInfo/MIR/X86/dbgcall-site-reference.mir b/llvm/test/DebugInfo/MIR/X86/dbgcall-site-reference.mir
+index 235787573f51..81af598ba194 100644
+--- a/llvm/test/DebugInfo/MIR/X86/dbgcall-site-reference.mir
++++ b/llvm/test/DebugInfo/MIR/X86/dbgcall-site-reference.mir
+@@ -1,4 +1,4 @@
+-# RUN: llc -debug-entry-values -start-before=livedebugvalues -filetype=obj -o - %s | llvm-dwarfdump - | FileCheck %s
++# RUN: llc -emit-call-site-info -debug-entry-values -start-before=livedebugvalues -filetype=obj -o - %s | llvm-dwarfdump - | FileCheck %s
+
+ # Based on the following C++ code:
+ # struct A { A(A &) {} };
+diff --git a/llvm/test/DebugInfo/MIR/X86/dbgcall-site-two-fwd-reg-defs.mir b/llvm/test/DebugInfo/MIR/X86/dbgcall-site-two-fwd-reg-defs.mir
+index db0934c595c3..46adedd1be44 100644
+--- a/llvm/test/DebugInfo/MIR/X86/dbgcall-site-two-fwd-reg-defs.mir
++++ b/llvm/test/DebugInfo/MIR/X86/dbgcall-site-two-fwd-reg-defs.mir
+@@ -1,4 +1,4 @@
+-# RUN: llc -O1 -debug-entry-values -start-after=livedebugvalues -filetype=obj %s -o - | llvm-dwarfdump - | FileCheck %s
++# RUN: llc -O1 -emit-call-site-info -debug-entry-values -start-after=livedebugvalues -filetype=obj %s -o - | llvm-dwarfdump - | FileCheck %s
+
+ # Based on the following C reproducer:
+ #
+diff --git a/llvm/test/DebugInfo/MIR/X86/dbginfo-entryvals.mir b/llvm/test/DebugInfo/MIR/X86/dbginfo-entryvals.mir
+index 5d203029936e..1d7b64f169d1 100644
+--- a/llvm/test/DebugInfo/MIR/X86/dbginfo-entryvals.mir
++++ b/llvm/test/DebugInfo/MIR/X86/dbginfo-entryvals.mir
+@@ -1,4 +1,4 @@
+-# RUN: llc -debug-entry-values -run-pass=livedebugvalues -march=x86-64 -o - %s | FileCheck %s
++# RUN: llc -emit-call-site-info -debug-entry-values -run-pass=livedebugvalues -march=x86-64 -o - %s | FileCheck %s
+ #
+ #extern void fn2(int);
+ #
+diff --git a/llvm/test/DebugInfo/MIR/X86/debug-call-site-param.mir b/llvm/test/DebugInfo/MIR/X86/debug-call-site-param.mir
+index e79be66cd4e3..c39bc4db50be 100644
+--- a/llvm/test/DebugInfo/MIR/X86/debug-call-site-param.mir
++++ b/llvm/test/DebugInfo/MIR/X86/debug-call-site-param.mir
+@@ -2,8 +2,8 @@
+ # When the debugger tuning is set to gdb, use GNU opcodes.
+ # For lldb, use the standard DWARF5 opcodes.
+
+-# RUN: llc -debug-entry-values -debugger-tune=gdb -filetype=obj -mtriple=x86_64-unknown-unknown -start-after=machineverifier -o - %s | llvm-dwarfdump - | FileCheck %s -check-prefixes=CHECK-GNU
+-# RUN: llc -debug-entry-values -debugger-tune=lldb -filetype=obj -mtriple=x86_64-unknown-unknown -start-after=machineverifier -o - %s | llvm-dwarfdump - | FileCheck %s -check-prefixes=CHECK-DWARF5
++# RUN: llc -emit-call-site-info -debug-entry-values -debugger-tune=gdb -filetype=obj -mtriple=x86_64-unknown-unknown -start-after=machineverifier -o - %s | llvm-dwarfdump - | FileCheck %s -check-prefixes=CHECK-GNU
++# RUN: llc -emit-call-site-info -debug-entry-values -debugger-tune=lldb -filetype=obj -mtriple=x86_64-unknown-unknown -start-after=machineverifier -o - %s | llvm-dwarfdump - | FileCheck %s -check-prefixes=CHECK-DWARF5
+ #
+ # extern void foo(int *a, int b, int c, int d, int e, int f);
+ # extern int getVal();
+diff --git a/llvm/test/DebugInfo/MIR/X86/entry-value-of-modified-param.mir b/llvm/test/DebugInfo/MIR/X86/entry-value-of-modified-param.mir
+index 8d121c3a30b9..c7f15aaaa562 100644
+--- a/llvm/test/DebugInfo/MIR/X86/entry-value-of-modified-param.mir
++++ b/llvm/test/DebugInfo/MIR/X86/entry-value-of-modified-param.mir
+@@ -1,4 +1,4 @@
+-# RUN: llc -debug-entry-values -run-pass=livedebugvalues -march=x86-64 -o - %s | FileCheck %s
++# RUN: llc -emit-call-site-info -debug-entry-values -run-pass=livedebugvalues -march=x86-64 -o - %s | FileCheck %s
+ #
+ #extern void fn1 (int, int, int);
+ #
+diff --git a/llvm/test/DebugInfo/MIR/X86/entry-values-diamond-bbs.mir b/llvm/test/DebugInfo/MIR/X86/entry-values-diamond-bbs.mir
+index 2396daada876..aa8fdd7afd47 100644
+--- a/llvm/test/DebugInfo/MIR/X86/entry-values-diamond-bbs.mir
++++ b/llvm/test/DebugInfo/MIR/X86/entry-values-diamond-bbs.mir
+@@ -1,4 +1,4 @@
+-# RUN: llc -debug-entry-values -run-pass=livedebugvalues -march=x86-64 -o - %s | FileCheck %s
++# RUN: llc -emit-call-site-info -debug-entry-values -run-pass=livedebugvalues -march=x86-64 -o - %s | FileCheck %s
+ #
+ # The test case was artificially adjusted, in order to make proper diamond basic
+ # block structure relevant to the debug entry values propagation.
+diff --git a/llvm/test/DebugInfo/MIR/X86/propagate-entry-value-cross-bbs.mir b/llvm/test/DebugInfo/MIR/X86/propagate-entry-value-cross-bbs.mir
+index 86b1cddaa462..c5af863954bf 100644
+--- a/llvm/test/DebugInfo/MIR/X86/propagate-entry-value-cross-bbs.mir
++++ b/llvm/test/DebugInfo/MIR/X86/propagate-entry-value-cross-bbs.mir
+@@ -1,4 +1,4 @@
+-# RUN: llc -debug-entry-values -run-pass=livedebugvalues -march=x86-64 -o - %s | FileCheck %s
++# RUN: llc -emit-call-site-info -debug-entry-values -run-pass=livedebugvalues -march=x86-64 -o - %s | FileCheck %s
+ #
+ #extern void fn1 (int, int, int);
+ #__attribute__((noinline))
+diff --git a/llvm/test/DebugInfo/MIR/X86/unreachable-block-call-site.mir b/llvm/test/DebugInfo/MIR/X86/unreachable-block-call-site.mir
+index d282d796f6d7..ea9c12b5a192 100644
+--- a/llvm/test/DebugInfo/MIR/X86/unreachable-block-call-site.mir
++++ b/llvm/test/DebugInfo/MIR/X86/unreachable-block-call-site.mir
+@@ -1,4 +1,4 @@
+-# RUN: llc -mtriple=x86_64-pc-linux -debug-entry-values -run-pass=unreachable-mbb-elimination -o - %s | FileCheck %s
++# RUN: llc -mtriple=x86_64-pc-linux -emit-call-site-info -debug-entry-values -run-pass=unreachable-mbb-elimination -o - %s | FileCheck %s
+
+ # Verify that the call site information for the call residing in the eliminated
+ # block is removed. This test case would previously trigger an assertion when
+diff --git a/llvm/test/DebugInfo/X86/dbgcall-site-64-bit-imms.ll b/llvm/test/DebugInfo/X86/dbgcall-site-64-bit-imms.ll
+index b698f1cdbfe8..b8cd9574cc63 100644
+--- a/llvm/test/DebugInfo/X86/dbgcall-site-64-bit-imms.ll
++++ b/llvm/test/DebugInfo/X86/dbgcall-site-64-bit-imms.ll
+@@ -1,4 +1,4 @@
+-; RUN: llc -O1 -debug-entry-values -filetype=obj -o - %s | llvm-dwarfdump - | FileCheck %s
++; RUN: llc -O1 -emit-call-site-info -debug-entry-values -filetype=obj -o - %s | llvm-dwarfdump - | FileCheck %s
+
+ ; Verify that the 64-bit call site immediates are not truncated.
+ ;
+diff --git a/llvm/test/DebugInfo/X86/dbgcall-site-zero-valued-imms.ll b/llvm/test/DebugInfo/X86/dbgcall-site-zero-valued-imms.ll
+index 9fe67f82a2b4..5d37774f55d6 100644
+--- a/llvm/test/DebugInfo/X86/dbgcall-site-zero-valued-imms.ll
++++ b/llvm/test/DebugInfo/X86/dbgcall-site-zero-valued-imms.ll
+@@ -1,4 +1,4 @@
+-; RUN: llc -O3 -debug-entry-values -filetype=obj -o - %s | llvm-dwarfdump - | FileCheck %s
++; RUN: llc -O3 -emit-call-site-info -debug-entry-values -filetype=obj -o - %s | llvm-dwarfdump - | FileCheck %s
+
+ target datalayout = "e-m:e-i64:64-f80:128-n8:16:32:64-S128"
+ target triple = "x86_64-unknown-linux-gnu"
+diff --git a/llvm/test/tools/llvm-dwarfdump/X86/stats-dbg-callsite-info.ll b/llvm/test/tools/llvm-dwarfdump/X86/stats-dbg-callsite-info.ll
+index c304e9d768a5..d126757398ff 100644
+--- a/llvm/test/tools/llvm-dwarfdump/X86/stats-dbg-callsite-info.ll
++++ b/llvm/test/tools/llvm-dwarfdump/X86/stats-dbg-callsite-info.ll
+@@ -1,4 +1,4 @@
+-; RUN: llc -debug-entry-values %s -o - -filetype=obj \
++; RUN: llc -emit-call-site-info -debug-entry-values %s -o - -filetype=obj \
+ ; RUN: | llvm-dwarfdump -statistics - | FileCheck %s
+ ;
+ ; The LLVM IR file was generated on this source code by using
+--
+2.33.1
+
diff --git a/dynamic-layers/clang-layer/recipes-devtools/clang/files/llvm10-Replace-MCTargetOptionsCommandFlags.inc-and-CommandF.patch b/dynamic-layers/clang-layer/recipes-devtools/clang/files/llvm10-Replace-MCTargetOptionsCommandFlags.inc-and-CommandF.patch
new file mode 100644
index 00000000..6b2db931
--- /dev/null
+++ b/dynamic-layers/clang-layer/recipes-devtools/clang/files/llvm10-Replace-MCTargetOptionsCommandFlags.inc-and-CommandF.patch
@@ -0,0 +1,2243 @@
+From fd7146658e3b4ce045dfb332c2edf216f76c1e1f Mon Sep 17 00:00:00 2001
+From: serge-sans-paille <sguelton@redhat.com>
+Date: Wed, 4 Mar 2020 00:47:43 +0100
+Subject: [PATCH] Replace MCTargetOptionsCommandFlags.inc and CommandFlags.inc
+ by runtime registration
+
+MCTargetOptionsCommandFlags.inc and CommandFlags.inc are headers which contain
+cl::opt with static storage.
+These headers are meant to be incuded by tools to make it easier to parametrize
+codegen/mc.
+
+However, these headers are also included in at least two libraries: lldCommon
+and handle-llvm. As a result, when creating DYLIB, clang-cpp holds a reference
+to the options, and lldCommon holds another reference. Linking the two in a
+single executable, as zig does[0], results in a double registration.
+
+This patch explores an other approach: the .inc files are moved to regular
+files, and the registration happens on-demand through static declaration of
+options in the constructor of a static object.
+
+[0] https://bugzilla.redhat.com/show_bug.cgi?id=1756977#c5
+
+Differential Revision: https://reviews.llvm.org/D75579
+
+Upstream-Status: Backport
+Signed-off-by: Anuj Mittal <anuj.mittal@intel.com>
+---
+ .../clang-fuzzer/handle-llvm/handle_llvm.cpp | 32 +-
+ lld/Common/TargetOptionsCommandFlags.cpp | 25 +-
+ llvm/include/llvm/CodeGen/CommandFlags.h | 149 +++++
+ .../llvm/MC/MCTargetOptionsCommandFlags.h | 54 ++
+ .../llvm/MC/MCTargetOptionsCommandFlags.inc | 65 --
+ llvm/include/llvm/module.modulemap | 3 -
+ llvm/lib/CodeGen/CMakeLists.txt | 1 +
+ llvm/lib/CodeGen/CommandFlags.cpp | 588 ++++++++++++++++++
+ llvm/lib/MC/CMakeLists.txt | 1 +
+ llvm/lib/MC/MCTargetOptionsCommandFlags.cpp | 105 ++++
+ llvm/tools/dsymutil/DwarfStreamer.cpp | 7 +-
+ llvm/tools/gold/gold-plugin.cpp | 16 +-
+ llvm/tools/llc/CMakeLists.txt | 1 +
+ llvm/tools/llc/llc.cpp | 43 +-
+ llvm/tools/lli/CMakeLists.txt | 1 +
+ llvm/tools/lli/lli.cpp | 41 +-
+ llvm/tools/llvm-dwp/llvm-dwp.cpp | 6 +-
+ .../llvm-isel-fuzzer/llvm-isel-fuzzer.cpp | 17 +-
+ llvm/tools/llvm-lto/CMakeLists.txt | 5 +-
+ llvm/tools/llvm-lto/llvm-lto.cpp | 28 +-
+ llvm/tools/llvm-lto2/CMakeLists.txt | 1 +
+ llvm/tools/llvm-lto2/llvm-lto2.cpp | 20 +-
+ .../llvm-mc-assemble-fuzzer/CMakeLists.txt | 1 +
+ .../llvm-mc-assemble-fuzzer.cpp | 12 +-
+ llvm/tools/llvm-mc/CMakeLists.txt | 1 +
+ llvm/tools/llvm-mc/llvm-mc.cpp | 6 +-
+ llvm/tools/llvm-mca/llvm-mca.cpp | 8 +-
+ .../tools/llvm-opt-fuzzer/llvm-opt-fuzzer.cpp | 16 +-
+ llvm/tools/lto/CMakeLists.txt | 4 +-
+ llvm/tools/lto/lto.cpp | 35 +-
+ llvm/tools/opt/opt.cpp | 23 +-
+ .../DebugInfo/DWARF/DwarfGenerator.cpp | 6 +-
+ 32 files changed, 1088 insertions(+), 233 deletions(-)
+ create mode 100644 llvm/include/llvm/CodeGen/CommandFlags.h
+ create mode 100644 llvm/include/llvm/MC/MCTargetOptionsCommandFlags.h
+ delete mode 100644 llvm/include/llvm/MC/MCTargetOptionsCommandFlags.inc
+ create mode 100644 llvm/lib/CodeGen/CommandFlags.cpp
+ create mode 100644 llvm/lib/MC/MCTargetOptionsCommandFlags.cpp
+
+diff --git a/clang/tools/clang-fuzzer/handle-llvm/handle_llvm.cpp b/clang/tools/clang-fuzzer/handle-llvm/handle_llvm.cpp
+index d8ab14594185..aefb761cd70c 100644
+--- a/clang/tools/clang-fuzzer/handle-llvm/handle_llvm.cpp
++++ b/clang/tools/clang-fuzzer/handle-llvm/handle_llvm.cpp
+@@ -19,7 +19,7 @@
+ #include "llvm/ADT/Triple.h"
+ #include "llvm/Analysis/TargetLibraryInfo.h"
+ #include "llvm/Analysis/TargetTransformInfo.h"
+-#include "llvm/CodeGen/CommandFlags.inc"
++#include "llvm/CodeGen/CommandFlags.h"
+ #include "llvm/CodeGen/MachineModuleInfo.h"
+ #include "llvm/CodeGen/TargetPassConfig.h"
+ #include "llvm/ExecutionEngine/JITEventListener.h"
+@@ -29,9 +29,9 @@
+ #include "llvm/ExecutionEngine/RTDyldMemoryManager.h"
+ #include "llvm/ExecutionEngine/SectionMemoryManager.h"
+ #include "llvm/IR/IRPrintingPasses.h"
++#include "llvm/IR/LLVMContext.h"
+ #include "llvm/IR/LegacyPassManager.h"
+ #include "llvm/IR/LegacyPassNameParser.h"
+-#include "llvm/IR/LLVMContext.h"
+ #include "llvm/IR/Module.h"
+ #include "llvm/IR/Verifier.h"
+ #include "llvm/IRReader/IRReader.h"
+@@ -42,12 +42,14 @@
+ #include "llvm/Support/TargetRegistry.h"
+ #include "llvm/Support/TargetSelect.h"
+ #include "llvm/Target/TargetMachine.h"
+-#include "llvm/Transforms/IPO/PassManagerBuilder.h"
+ #include "llvm/Transforms/IPO.h"
++#include "llvm/Transforms/IPO/PassManagerBuilder.h"
+ #include "llvm/Transforms/Vectorize.h"
+
+ using namespace llvm;
+
++static codegen::RegisterCodeGenFlags CGF;
++
+ // Define a type for the functions that are compiled and executed
+ typedef void (*LLVMFunc)(int*, int*, int*, int);
+
+@@ -100,15 +102,17 @@ static std::string OptLLVM(const std::string &IR, CodeGenOpt::Level OLvl) {
+ ErrorAndExit("Could not parse IR");
+
+ Triple ModuleTriple(M->getTargetTriple());
+- const TargetOptions Options = InitTargetOptionsFromCodeGenFlags();
++ const TargetOptions Options = codegen::InitTargetOptionsFromCodeGenFlags();
+ std::string E;
+- const Target *TheTarget = TargetRegistry::lookupTarget(MArch, ModuleTriple, E);
+- TargetMachine *Machine =
+- TheTarget->createTargetMachine(M->getTargetTriple(), getCPUStr(),
+- getFeaturesStr(), Options, getRelocModel(),
+- getCodeModel(), OLvl);
++ const Target *TheTarget =
++ TargetRegistry::lookupTarget(codegen::getMArch(), ModuleTriple, E);
++ TargetMachine *Machine = TheTarget->createTargetMachine(
++ M->getTargetTriple(), codegen::getCPUStr(), codegen::getFeaturesStr(),
++ Options, codegen::getExplicitRelocModel(),
++ codegen::getExplicitCodeModel(), OLvl);
+ std::unique_ptr<TargetMachine> TM(Machine);
+- setFunctionAttributes(getCPUStr(), getFeaturesStr(), *M);
++ codegen::setFunctionAttributes(codegen::getCPUStr(),
++ codegen::getFeaturesStr(), *M);
+
+ legacy::PassManager Passes;
+
+@@ -154,14 +158,14 @@ static void CreateAndRunJITFunc(const std::string &IR, CodeGenOpt::Level OLvl) {
+
+ std::string ErrorMsg;
+ EngineBuilder builder(std::move(M));
+- builder.setMArch(MArch);
+- builder.setMCPU(getCPUStr());
+- builder.setMAttrs(getFeatureList());
++ builder.setMArch(codegen::getMArch());
++ builder.setMCPU(codegen::getCPUStr());
++ builder.setMAttrs(codegen::getFeatureList());
+ builder.setErrorStr(&ErrorMsg);
+ builder.setEngineKind(EngineKind::JIT);
+ builder.setMCJITMemoryManager(std::make_unique<SectionMemoryManager>());
+ builder.setOptLevel(OLvl);
+- builder.setTargetOptions(InitTargetOptionsFromCodeGenFlags());
++ builder.setTargetOptions(codegen::InitTargetOptionsFromCodeGenFlags());
+
+ std::unique_ptr<ExecutionEngine> EE(builder.create());
+ if (!EE)
+diff --git a/lld/Common/TargetOptionsCommandFlags.cpp b/lld/Common/TargetOptionsCommandFlags.cpp
+index 0137feb63f37..9b166a3e130a 100644
+--- a/lld/Common/TargetOptionsCommandFlags.cpp
++++ b/lld/Common/TargetOptionsCommandFlags.cpp
+@@ -5,35 +5,26 @@
+ // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+ //
+ //===----------------------------------------------------------------------===//
+-//
+-// This file exists as a place for global variables defined in LLVM's
+-// CodeGen/CommandFlags.inc. By putting the resulting object file in
+-// an archive and linking with it, the definitions will automatically be
+-// included when needed and skipped when already present.
+-//
+-//===----------------------------------------------------------------------===//
+
+ #include "lld/Common/TargetOptionsCommandFlags.h"
+
+-#include "llvm/CodeGen/CommandFlags.inc"
++#include "llvm/CodeGen/CommandFlags.h"
+ #include "llvm/Target/TargetOptions.h"
+
+-// Define an externally visible version of
+-// initTargetOptionsFromCodeGenFlags, so that its functionality can be
+-// used without having to include llvm/CodeGen/CommandFlags.inc, which
+-// would lead to multiple definitions of the command line flags.
++static llvm::codegen::RegisterCodeGenFlags CGF;
++
+ llvm::TargetOptions lld::initTargetOptionsFromCodeGenFlags() {
+- return ::InitTargetOptionsFromCodeGenFlags();
++ return llvm::codegen::InitTargetOptionsFromCodeGenFlags();
+ }
+
+ llvm::Optional<llvm::Reloc::Model> lld::getRelocModelFromCMModel() {
+- return getRelocModel();
++ return llvm::codegen::getExplicitRelocModel();
+ }
+
+ llvm::Optional<llvm::CodeModel::Model> lld::getCodeModelFromCMModel() {
+- return getCodeModel();
++ return llvm::codegen::getExplicitCodeModel();
+ }
+
+-std::string lld::getCPUStr() { return ::getCPUStr(); }
++std::string lld::getCPUStr() { return llvm::codegen::getCPUStr(); }
+
+-std::vector<std::string> lld::getMAttrs() { return ::MAttrs; }
++std::vector<std::string> lld::getMAttrs() { return llvm::codegen::getMAttrs(); }
+diff --git a/llvm/include/llvm/CodeGen/CommandFlags.h b/llvm/include/llvm/CodeGen/CommandFlags.h
+new file mode 100644
+index 000000000000..cdec26879f73
+--- /dev/null
++++ b/llvm/include/llvm/CodeGen/CommandFlags.h
+@@ -0,0 +1,149 @@
++//===-- CommandFlags.h - Command Line Flags Interface -----------*- 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 contains codegen-specific flags that are shared between different
++// command line tools. The tools "llc" and "opt" both use this file to prevent
++// flag duplication.
++//
++//===----------------------------------------------------------------------===//
++
++#include "llvm/ADT/StringExtras.h"
++#include "llvm/IR/Instructions.h"
++#include "llvm/IR/Intrinsics.h"
++#include "llvm/IR/Module.h"
++#include "llvm/MC/MCTargetOptionsCommandFlags.h"
++#include "llvm/MC/SubtargetFeature.h"
++#include "llvm/Support/CodeGen.h"
++#include "llvm/Support/CommandLine.h"
++#include "llvm/Support/Host.h"
++#include "llvm/Target/TargetMachine.h"
++#include "llvm/Target/TargetOptions.h"
++#include <string>
++
++namespace llvm {
++
++namespace codegen {
++
++std::string getMArch();
++
++std::string getMCPU();
++
++std::vector<std::string> getMAttrs();
++
++Reloc::Model getRelocModel();
++Optional<Reloc::Model> getExplicitRelocModel();
++
++ThreadModel::Model getThreadModel();
++
++CodeModel::Model getCodeModel();
++Optional<CodeModel::Model> getExplicitCodeModel();
++
++llvm::ExceptionHandling getExceptionModel();
++
++CodeGenFileType getFileType();
++Optional<CodeGenFileType> getExplicitFileType();
++
++CodeGenFileType getFileType();
++
++llvm::FramePointer::FP getFramePointerUsage();
++
++bool getEnableUnsafeFPMath();
++
++bool getEnableNoInfsFPMath();
++
++bool getEnableNoNaNsFPMath();
++
++bool getEnableNoSignedZerosFPMath();
++
++bool getEnableNoTrappingFPMath();
++
++llvm::FPDenormal::DenormalMode getDenormalFPMath();
++
++bool getEnableHonorSignDependentRoundingFPMath();
++
++llvm::FloatABI::ABIType getFloatABIForCalls();
++
++llvm::FPOpFusion::FPOpFusionMode getFuseFPOps();
++
++bool getDontPlaceZerosInBSS();
++
++bool getEnableGuaranteedTailCallOpt();
++
++bool getDisableTailCalls();
++
++bool getStackSymbolOrdering();
++
++unsigned getOverrideStackAlignment();
++
++bool getStackRealign();
++
++std::string getTrapFuncName();
++
++bool getUseCtors();
++
++bool getRelaxELFRelocations();
++
++bool getDataSections();
++Optional<bool> getExplicitDataSections();
++
++bool getFunctionSections();
++Optional<bool> getExplicitFunctionSections();
++
++std::string getBBSections();
++
++unsigned getTLSSize();
++
++bool getEmulatedTLS();
++
++bool getUniqueSectionNames();
++
++bool getUniqueBBSectionNames();
++
++llvm::EABI getEABIVersion();
++
++llvm::DebuggerKind getDebuggerTuningOpt();
++
++bool getEnableStackSizeSection();
++
++bool getEnableAddrsig();
++
++bool getEmitCallSiteInfo();
++
++bool getEnableDebugEntryValues();
++
++bool getForceDwarfFrameSection();
++
++/// Create this object with static storage to register codegen-related command
++/// line options.
++struct RegisterCodeGenFlags {
++ RegisterCodeGenFlags();
++};
++
++llvm::BasicBlockSection getBBSectionsMode(llvm::TargetOptions &Options);
++
++// Common utility function tightly tied to the options listed here. Initializes
++// a TargetOptions object with CodeGen flags and returns it.
++TargetOptions InitTargetOptionsFromCodeGenFlags();
++
++std::string getCPUStr();
++
++std::string getFeaturesStr();
++
++std::vector<std::string> getFeatureList();
++
++void renderBoolStringAttr(AttrBuilder &B, StringRef Name, bool Val);
++
++/// Set function attributes of function \p F based on CPU, Features, and command
++/// line flags.
++void setFunctionAttributes(StringRef CPU, StringRef Features, Function &F);
++
++/// Set function attributes of functions in Module M based on CPU,
++/// Features, and command line flags.
++void setFunctionAttributes(StringRef CPU, StringRef Features, Module &M);
++} // namespace codegen
++} // namespace llvm
+diff --git a/llvm/include/llvm/MC/MCTargetOptionsCommandFlags.h b/llvm/include/llvm/MC/MCTargetOptionsCommandFlags.h
+new file mode 100644
+index 000000000000..7a5edf78fdcc
+--- /dev/null
++++ b/llvm/include/llvm/MC/MCTargetOptionsCommandFlags.h
+@@ -0,0 +1,54 @@
++//===-- MCTargetOptionsCommandFlags.h --------------------------*- 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 contains machine code-specific flags that are shared between
++// different command line tools.
++//
++//===----------------------------------------------------------------------===//
++
++#ifndef LLVM_MC_MCTARGETOPTIONSCOMMANDFLAGS_H
++#define LLVM_MC_MCTARGETOPTIONSCOMMANDFLAGS_H
++
++#include "llvm/ADT/Optional.h"
++#include "llvm/MC/MCTargetOptions.h"
++#include "llvm/Support/CommandLine.h"
++
++namespace llvm {
++
++namespace mc {
++
++bool getRelaxAll();
++Optional<bool> getExplicitRelaxAll();
++
++bool getIncrementalLinkerCompatible();
++
++int getDwarfVersion();
++
++bool getShowMCInst();
++
++bool getFatalWarnings();
++
++bool getNoWarn();
++
++bool getNoDeprecatedWarn();
++
++std::string getABIName();
++
++/// Create this object with static storage to register mc-related command
++/// line options.
++struct RegisterMCTargetOptionsFlags {
++ RegisterMCTargetOptionsFlags();
++};
++
++MCTargetOptions InitMCTargetOptionsFromFlags();
++
++} // namespace mc
++
++} // namespace llvm
++
++#endif
+diff --git a/llvm/include/llvm/MC/MCTargetOptionsCommandFlags.inc b/llvm/include/llvm/MC/MCTargetOptionsCommandFlags.inc
+deleted file mode 100644
+index 93e21b626eac..000000000000
+--- a/llvm/include/llvm/MC/MCTargetOptionsCommandFlags.inc
++++ /dev/null
+@@ -1,65 +0,0 @@
+-//===-- MCTargetOptionsCommandFlags.h --------------------------*- 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 contains machine code-specific flags that are shared between
+-// different command line tools.
+-//
+-//===----------------------------------------------------------------------===//
+-
+-#ifndef LLVM_MC_MCTARGETOPTIONSCOMMANDFLAGS_H
+-#define LLVM_MC_MCTARGETOPTIONSCOMMANDFLAGS_H
+-
+-#include "llvm/MC/MCTargetOptions.h"
+-#include "llvm/Support/CommandLine.h"
+-using namespace llvm;
+-
+-static cl::opt<bool> RelaxAll("mc-relax-all",
+- cl::desc("When used with filetype=obj, "
+- "relax all fixups in the emitted object file"));
+-
+-static cl::opt<bool> IncrementalLinkerCompatible(
+- "incremental-linker-compatible",
+- cl::desc(
+- "When used with filetype=obj, "
+- "emit an object file which can be used with an incremental linker"));
+-
+-static cl::opt<int> DwarfVersion("dwarf-version", cl::desc("Dwarf version"),
+- cl::init(0));
+-
+-static cl::opt<bool> ShowMCInst("asm-show-inst",
+- cl::desc("Emit internal instruction representation to "
+- "assembly file"));
+-
+-static cl::opt<bool> FatalWarnings("fatal-warnings",
+- cl::desc("Treat warnings as errors"));
+-
+-static cl::opt<bool> NoWarn("no-warn", cl::desc("Suppress all warnings"));
+-static cl::alias NoWarnW("W", cl::desc("Alias for --no-warn"), cl::aliasopt(NoWarn));
+-
+-static cl::opt<bool> NoDeprecatedWarn("no-deprecated-warn",
+- cl::desc("Suppress all deprecated warnings"));
+-
+-static cl::opt<std::string>
+-ABIName("target-abi", cl::Hidden,
+- cl::desc("The name of the ABI to be targeted from the backend."),
+- cl::init(""));
+-
+-static MCTargetOptions InitMCTargetOptionsFromFlags() {
+- MCTargetOptions Options;
+- Options.MCRelaxAll = RelaxAll;
+- Options.MCIncrementalLinkerCompatible = IncrementalLinkerCompatible;
+- Options.DwarfVersion = DwarfVersion;
+- Options.ShowMCInst = ShowMCInst;
+- Options.ABIName = ABIName;
+- Options.MCFatalWarnings = FatalWarnings;
+- Options.MCNoWarn = NoWarn;
+- Options.MCNoDeprecatedWarn = NoDeprecatedWarn;
+- return Options;
+-}
+-
+-#endif
+diff --git a/llvm/include/llvm/module.modulemap b/llvm/include/llvm/module.modulemap
+index d281682ae003..d176b3dfd4be 100644
+--- a/llvm/include/llvm/module.modulemap
++++ b/llvm/include/llvm/module.modulemap
+@@ -29,7 +29,6 @@ module LLVM_Backend {
+ exclude header "CodeGen/LinkAllCodegenComponents.h"
+
+ // These are intended for (repeated) textual inclusion.
+- textual header "CodeGen/CommandFlags.inc"
+ textual header "CodeGen/DIEValue.def"
+ }
+ }
+@@ -308,8 +307,6 @@ module LLVM_MC {
+
+ umbrella "MC"
+ module * { export * }
+-
+- textual header "MC/MCTargetOptionsCommandFlags.inc"
+ }
+
+ // Used by llvm-tblgen
+diff --git a/llvm/lib/CodeGen/CMakeLists.txt b/llvm/lib/CodeGen/CMakeLists.txt
+index a3916b7c6242..c6be91247017 100644
+--- a/llvm/lib/CodeGen/CMakeLists.txt
++++ b/llvm/lib/CodeGen/CMakeLists.txt
+@@ -14,6 +14,7 @@ add_llvm_component_library(LLVMCodeGen
+ CFIInstrInserter.cpp
+ CodeGen.cpp
+ CodeGenPrepare.cpp
++ CommandFlags.cpp
+ CriticalAntiDepBreaker.cpp
+ DeadMachineInstructionElim.cpp
+ DetectDeadLanes.cpp
+diff --git a/llvm/lib/CodeGen/CommandFlags.cpp b/llvm/lib/CodeGen/CommandFlags.cpp
+new file mode 100644
+index 000000000000..7acb84df582f
+--- /dev/null
++++ b/llvm/lib/CodeGen/CommandFlags.cpp
+@@ -0,0 +1,588 @@
++//===-- CommandFlags.cpp - Command Line Flags Interface ---------*- 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 contains codegen-specific flags that are shared between different
++// command line tools. The tools "llc" and "opt" both use this file to prevent
++// flag duplication.
++//
++//===----------------------------------------------------------------------===//
++
++#include "llvm/CodeGen/CommandFlags.h"
++
++using namespace llvm;
++
++#define CGOPT(TY, NAME) \
++ static cl::opt<TY> *NAME##View; \
++ TY codegen::get##NAME() { \
++ assert(NAME##View && "RegisterCodeGenFlags not created."); \
++ return *NAME##View; \
++ }
++
++#define CGLIST(TY, NAME) \
++ static cl::list<TY> *NAME##View; \
++ std::vector<TY> codegen::get##NAME() { \
++ assert(NAME##View && "RegisterCodeGenFlags not created."); \
++ return *NAME##View; \
++ }
++
++#define CGOPT_EXP(TY, NAME) \
++ CGOPT(TY, NAME) \
++ Optional<TY> codegen::getExplicit##NAME() { \
++ if (NAME##View->getNumOccurrences()) { \
++ TY res = *NAME##View; \
++ return res; \
++ } \
++ return None; \
++ }
++
++CGOPT(std::string, MArch)
++CGOPT(std::string, MCPU)
++CGLIST(std::string, MAttrs)
++CGOPT_EXP(Reloc::Model, RelocModel)
++CGOPT(ThreadModel::Model, ThreadModel)
++CGOPT_EXP(CodeModel::Model, CodeModel)
++CGOPT(ExceptionHandling, ExceptionModel)
++CGOPT_EXP(CodeGenFileType, FileType)
++CGOPT(FramePointer::FP, FramePointerUsage)
++CGOPT(bool, EnableUnsafeFPMath)
++CGOPT(bool, EnableNoInfsFPMath)
++CGOPT(bool, EnableNoNaNsFPMath)
++CGOPT(bool, EnableNoSignedZerosFPMath)
++CGOPT(bool, EnableNoTrappingFPMath)
++CGOPT(FPDenormal::DenormalMode, DenormalFPMath)
++CGOPT(bool, EnableHonorSignDependentRoundingFPMath)
++CGOPT(FloatABI::ABIType, FloatABIForCalls)
++CGOPT(FPOpFusion::FPOpFusionMode, FuseFPOps)
++CGOPT(bool, DontPlaceZerosInBSS)
++CGOPT(bool, EnableGuaranteedTailCallOpt)
++CGOPT(bool, DisableTailCalls)
++CGOPT(bool, StackSymbolOrdering)
++CGOPT(unsigned, OverrideStackAlignment)
++CGOPT(bool, StackRealign)
++CGOPT(std::string, TrapFuncName)
++CGOPT(bool, UseCtors)
++CGOPT(bool, RelaxELFRelocations)
++CGOPT_EXP(bool, DataSections)
++CGOPT_EXP(bool, FunctionSections)
++CGOPT(std::string, BBSections)
++CGOPT(unsigned, TLSSize)
++CGOPT(bool, EmulatedTLS)
++CGOPT(bool, UniqueSectionNames)
++CGOPT(bool, UniqueBBSectionNames)
++CGOPT(EABI, EABIVersion)
++CGOPT(DebuggerKind, DebuggerTuningOpt)
++CGOPT(bool, EnableStackSizeSection)
++CGOPT(bool, EnableAddrsig)
++CGOPT(bool, EmitCallSiteInfo)
++CGOPT(bool, EnableDebugEntryValues)
++CGOPT(bool, ForceDwarfFrameSection)
++
++codegen::RegisterCodeGenFlags::RegisterCodeGenFlags() {
++#define CGBINDOPT(NAME) \
++ do { \
++ NAME##View = std::addressof(NAME); \
++ } while (0)
++
++ static cl::opt<std::string> MArch(
++ "march", cl::desc("Architecture to generate code for (see --version)"));
++ CGBINDOPT(MArch);
++
++ static cl::opt<std::string> MCPU(
++ "mcpu", cl::desc("Target a specific cpu type (-mcpu=help for details)"),
++ cl::value_desc("cpu-name"), cl::init(""));
++ CGBINDOPT(MCPU);
++
++ static cl::list<std::string> MAttrs(
++ "mattr", cl::CommaSeparated,
++ cl::desc("Target specific attributes (-mattr=help for details)"),
++ cl::value_desc("a1,+a2,-a3,..."));
++ CGBINDOPT(MAttrs);
++
++ static cl::opt<Reloc::Model> RelocModel(
++ "relocation-model", cl::desc("Choose relocation model"),
++ cl::values(
++ clEnumValN(Reloc::Static, "static", "Non-relocatable code"),
++ clEnumValN(Reloc::PIC_, "pic",
++ "Fully relocatable, position independent code"),
++ clEnumValN(Reloc::DynamicNoPIC, "dynamic-no-pic",
++ "Relocatable external references, non-relocatable code"),
++ clEnumValN(
++ Reloc::ROPI, "ropi",
++ "Code and read-only data relocatable, accessed PC-relative"),
++ clEnumValN(
++ Reloc::RWPI, "rwpi",
++ "Read-write data relocatable, accessed relative to static base"),
++ clEnumValN(Reloc::ROPI_RWPI, "ropi-rwpi",
++ "Combination of ropi and rwpi")));
++ CGBINDOPT(RelocModel);
++
++ static cl::opt<ThreadModel::Model> ThreadModel(
++ "thread-model", cl::desc("Choose threading model"),
++ cl::init(ThreadModel::POSIX),
++ cl::values(
++ clEnumValN(ThreadModel::POSIX, "posix", "POSIX thread model"),
++ clEnumValN(ThreadModel::Single, "single", "Single thread model")));
++ CGBINDOPT(ThreadModel);
++
++ static cl::opt<CodeModel::Model> CodeModel(
++ "code-model", cl::desc("Choose code model"),
++ cl::values(clEnumValN(CodeModel::Tiny, "tiny", "Tiny code model"),
++ clEnumValN(CodeModel::Small, "small", "Small code model"),
++ clEnumValN(CodeModel::Kernel, "kernel", "Kernel code model"),
++ clEnumValN(CodeModel::Medium, "medium", "Medium code model"),
++ clEnumValN(CodeModel::Large, "large", "Large code model")));
++ CGBINDOPT(CodeModel);
++
++ static cl::opt<ExceptionHandling> ExceptionModel(
++ "exception-model", cl::desc("exception model"),
++ cl::init(ExceptionHandling::None),
++ cl::values(
++ clEnumValN(ExceptionHandling::None, "default",
++ "default exception handling model"),
++ clEnumValN(ExceptionHandling::DwarfCFI, "dwarf",
++ "DWARF-like CFI based exception handling"),
++ clEnumValN(ExceptionHandling::SjLj, "sjlj",
++ "SjLj exception handling"),
++ clEnumValN(ExceptionHandling::ARM, "arm", "ARM EHABI exceptions"),
++ clEnumValN(ExceptionHandling::WinEH, "wineh",
++ "Windows exception model"),
++ clEnumValN(ExceptionHandling::Wasm, "wasm",
++ "WebAssembly exception handling")));
++ CGBINDOPT(ExceptionModel);
++
++ static cl::opt<CodeGenFileType> FileType(
++ "filetype", cl::init(CGFT_AssemblyFile),
++ cl::desc(
++ "Choose a file type (not all types are supported by all targets):"),
++ cl::values(
++ clEnumValN(CGFT_AssemblyFile, "asm", "Emit an assembly ('.s') file"),
++ clEnumValN(CGFT_ObjectFile, "obj",
++ "Emit a native object ('.o') file"),
++ clEnumValN(CGFT_Null, "null",
++ "Emit nothing, for performance testing")));
++ CGBINDOPT(FileType);
++
++ static cl::opt<FramePointer::FP> FramePointerUsage(
++ "frame-pointer",
++ cl::desc("Specify frame pointer elimination optimization"),
++ cl::init(FramePointer::None),
++ cl::values(
++ clEnumValN(FramePointer::All, "all",
++ "Disable frame pointer elimination"),
++ clEnumValN(FramePointer::NonLeaf, "non-leaf",
++ "Disable frame pointer elimination for non-leaf frame"),
++ clEnumValN(FramePointer::None, "none",
++ "Enable frame pointer elimination")));
++ CGBINDOPT(FramePointerUsage);
++
++ static cl::opt<bool> EnableUnsafeFPMath(
++ "enable-unsafe-fp-math",
++ cl::desc("Enable optimizations that may decrease FP precision"),
++ cl::init(false));
++ CGBINDOPT(EnableUnsafeFPMath);
++
++ static cl::opt<bool> EnableNoInfsFPMath(
++ "enable-no-infs-fp-math",
++ cl::desc("Enable FP math optimizations that assume no +-Infs"),
++ cl::init(false));
++ CGBINDOPT(EnableNoInfsFPMath);
++
++ static cl::opt<bool> EnableNoNaNsFPMath(
++ "enable-no-nans-fp-math",
++ cl::desc("Enable FP math optimizations that assume no NaNs"),
++ cl::init(false));
++ CGBINDOPT(EnableNoNaNsFPMath);
++
++ static cl::opt<bool> EnableNoSignedZerosFPMath(
++ "enable-no-signed-zeros-fp-math",
++ cl::desc("Enable FP math optimizations that assume "
++ "the sign of 0 is insignificant"),
++ cl::init(false));
++ CGBINDOPT(EnableNoSignedZerosFPMath);
++
++ static cl::opt<bool> EnableNoTrappingFPMath(
++ "enable-no-trapping-fp-math",
++ cl::desc("Enable setting the FP exceptions build "
++ "attribute not to use exceptions"),
++ cl::init(false));
++ CGBINDOPT(EnableNoTrappingFPMath);
++
++ static cl::opt<FPDenormal::DenormalMode> DenormalFPMath(
++ "denormal-fp-math",
++ cl::desc(
++ "Select which denormal numbers the code is permitted to require"),
++ cl::init(FPDenormal::IEEE),
++ cl::values(
++ clEnumValN(FPDenormal::IEEE, "ieee", "IEEE 754 denormal numbers"),
++ clEnumValN(FPDenormal::PreserveSign, "preserve-sign",
++ "the sign of a flushed-to-zero number is preserved "
++ "in the sign of 0"),
++ clEnumValN(FPDenormal::PositiveZero, "positive-zero",
++ "denormals are flushed to positive zero")));
++ CGBINDOPT(DenormalFPMath);
++
++ static cl::opt<bool> EnableHonorSignDependentRoundingFPMath(
++ "enable-sign-dependent-rounding-fp-math", cl::Hidden,
++ cl::desc("Force codegen to assume rounding mode can change dynamically"),
++ cl::init(false));
++ CGBINDOPT(EnableHonorSignDependentRoundingFPMath);
++
++ static cl::opt<FloatABI::ABIType> FloatABIForCalls(
++ "float-abi", cl::desc("Choose float ABI type"),
++ cl::init(FloatABI::Default),
++ cl::values(clEnumValN(FloatABI::Default, "default",
++ "Target default float ABI type"),
++ clEnumValN(FloatABI::Soft, "soft",
++ "Soft float ABI (implied by -soft-float)"),
++ clEnumValN(FloatABI::Hard, "hard",
++ "Hard float ABI (uses FP registers)")));
++ CGBINDOPT(FloatABIForCalls);
++
++ static cl::opt<FPOpFusion::FPOpFusionMode> FuseFPOps(
++ "fp-contract", cl::desc("Enable aggressive formation of fused FP ops"),
++ cl::init(FPOpFusion::Standard),
++ cl::values(
++ clEnumValN(FPOpFusion::Fast, "fast",
++ "Fuse FP ops whenever profitable"),
++ clEnumValN(FPOpFusion::Standard, "on", "Only fuse 'blessed' FP ops."),
++ clEnumValN(FPOpFusion::Strict, "off",
++ "Only fuse FP ops when the result won't be affected.")));
++ CGBINDOPT(FuseFPOps);
++
++ static cl::opt<bool> DontPlaceZerosInBSS(
++ "nozero-initialized-in-bss",
++ cl::desc("Don't place zero-initialized symbols into bss section"),
++ cl::init(false));
++ CGBINDOPT(DontPlaceZerosInBSS);
++
++ static cl::opt<bool> EnableGuaranteedTailCallOpt(
++ "tailcallopt",
++ cl::desc(
++ "Turn fastcc calls into tail calls by (potentially) changing ABI."),
++ cl::init(false));
++ CGBINDOPT(EnableGuaranteedTailCallOpt);
++
++ static cl::opt<bool> DisableTailCalls(
++ "disable-tail-calls", cl::desc("Never emit tail calls"), cl::init(false));
++ CGBINDOPT(DisableTailCalls);
++
++ static cl::opt<bool> StackSymbolOrdering(
++ "stack-symbol-ordering", cl::desc("Order local stack symbols."),
++ cl::init(true));
++ CGBINDOPT(StackSymbolOrdering);
++
++ static cl::opt<unsigned> OverrideStackAlignment(
++ "stack-alignment", cl::desc("Override default stack alignment"),
++ cl::init(0));
++ CGBINDOPT(OverrideStackAlignment);
++
++ static cl::opt<bool> StackRealign(
++ "stackrealign",
++ cl::desc("Force align the stack to the minimum alignment"),
++ cl::init(false));
++ CGBINDOPT(StackRealign);
++
++ static cl::opt<std::string> TrapFuncName(
++ "trap-func", cl::Hidden,
++ cl::desc("Emit a call to trap function rather than a trap instruction"),
++ cl::init(""));
++ CGBINDOPT(TrapFuncName);
++
++ static cl::opt<bool> UseCtors("use-ctors",
++ cl::desc("Use .ctors instead of .init_array."),
++ cl::init(false));
++ CGBINDOPT(UseCtors);
++
++ static cl::opt<bool> RelaxELFRelocations(
++ "relax-elf-relocations",
++ cl::desc(
++ "Emit GOTPCRELX/REX_GOTPCRELX instead of GOTPCREL on x86-64 ELF"),
++ cl::init(false));
++ CGBINDOPT(RelaxELFRelocations);
++
++ static cl::opt<bool> DataSections(
++ "data-sections", cl::desc("Emit data into separate sections"),
++ cl::init(false));
++ CGBINDOPT(DataSections);
++
++ static cl::opt<bool> FunctionSections(
++ "function-sections", cl::desc("Emit functions into separate sections"),
++ cl::init(false));
++ CGBINDOPT(FunctionSections);
++
++ static cl::opt<std::string> BBSections(
++ "basicblock-sections",
++ cl::desc("Emit basic blocks into separate sections"),
++ cl::value_desc("all | <function list (file)> | labels | none"),
++ cl::init("none"));
++ CGBINDOPT(BBSections);
++
++ static cl::opt<unsigned> TLSSize(
++ "tls-size", cl::desc("Bit size of immediate TLS offsets"), cl::init(0));
++ CGBINDOPT(TLSSize);
++
++ static cl::opt<bool> EmulatedTLS(
++ "emulated-tls", cl::desc("Use emulated TLS model"), cl::init(false));
++ CGBINDOPT(EmulatedTLS);
++
++ static cl::opt<bool> UniqueSectionNames(
++ "unique-section-names", cl::desc("Give unique names to every section"),
++ cl::init(true));
++ CGBINDOPT(UniqueSectionNames);
++
++ static cl::opt<bool> UniqueBBSectionNames(
++ "unique-bb-section-names",
++ cl::desc("Give unique names to every basic block section"),
++ cl::init(false));
++ CGBINDOPT(UniqueBBSectionNames);
++
++ static cl::opt<EABI> EABIVersion(
++ "meabi", cl::desc("Set EABI type (default depends on triple):"),
++ cl::init(EABI::Default),
++ cl::values(
++ clEnumValN(EABI::Default, "default", "Triple default EABI version"),
++ clEnumValN(EABI::EABI4, "4", "EABI version 4"),
++ clEnumValN(EABI::EABI5, "5", "EABI version 5"),
++ clEnumValN(EABI::GNU, "gnu", "EABI GNU")));
++ CGBINDOPT(EABIVersion);
++
++ static cl::opt<DebuggerKind> DebuggerTuningOpt(
++ "debugger-tune", cl::desc("Tune debug info for a particular debugger"),
++ cl::init(DebuggerKind::Default),
++ cl::values(
++ clEnumValN(DebuggerKind::GDB, "gdb", "gdb"),
++ clEnumValN(DebuggerKind::LLDB, "lldb", "lldb"),
++ clEnumValN(DebuggerKind::SCE, "sce", "SCE targets (e.g. PS4)")));
++ CGBINDOPT(DebuggerTuningOpt);
++
++ static cl::opt<bool> EnableStackSizeSection(
++ "stack-size-section",
++ cl::desc("Emit a section containing stack size metadata"),
++ cl::init(false));
++ CGBINDOPT(EnableStackSizeSection);
++
++ static cl::opt<bool> EnableAddrsig(
++ "addrsig", cl::desc("Emit an address-significance table"),
++ cl::init(false));
++ CGBINDOPT(EnableAddrsig);
++
++ static cl::opt<bool> EmitCallSiteInfo(
++ "emit-call-site-info",
++ cl::desc(
++ "Emit call site debug information, if debug information is enabled."),
++ cl::init(false));
++ CGBINDOPT(EmitCallSiteInfo);
++
++ static cl::opt<bool> EnableDebugEntryValues(
++ "debug-entry-values",
++ cl::desc("Emit debug info about parameter's entry values"),
++ cl::init(false));
++ CGBINDOPT(EnableDebugEntryValues);
++
++ static cl::opt<bool> ForceDwarfFrameSection(
++ "force-dwarf-frame-section",
++ cl::desc("Always emit a debug frame section."), cl::init(false));
++ CGBINDOPT(ForceDwarfFrameSection);
++
++#undef CGBINDOPT
++
++ mc::RegisterMCTargetOptionsFlags();
++}
++
++llvm::BasicBlockSection
++codegen::getBBSectionsMode(llvm::TargetOptions &Options) {
++ if (getBBSections() == "all")
++ return BasicBlockSection::All;
++ else if (getBBSections() == "labels")
++ return BasicBlockSection::Labels;
++ else if (getBBSections() == "none")
++ return BasicBlockSection::None;
++ else {
++ ErrorOr<std::unique_ptr<MemoryBuffer>> MBOrErr =
++ MemoryBuffer::getFile(getBBSections());
++ if (!MBOrErr) {
++ errs() << "Error loading basic block sections function list file: "
++ << MBOrErr.getError().message() << "\n";
++ } else {
++ Options.BBSectionsFuncListBuf = std::move(*MBOrErr);
++ }
++ return BasicBlockSection::List;
++ }
++}
++
++// Common utility function tightly tied to the options listed here. Initializes
++// a TargetOptions object with CodeGen flags and returns it.
++TargetOptions codegen::InitTargetOptionsFromCodeGenFlags() {
++ TargetOptions Options;
++ Options.AllowFPOpFusion = getFuseFPOps();
++ Options.UnsafeFPMath = getEnableUnsafeFPMath();
++ Options.NoInfsFPMath = getEnableNoInfsFPMath();
++ Options.NoNaNsFPMath = getEnableNoNaNsFPMath();
++ Options.NoSignedZerosFPMath = getEnableNoSignedZerosFPMath();
++ Options.NoTrappingFPMath = getEnableNoTrappingFPMath();
++ Options.FPDenormalMode = getDenormalFPMath();
++ Options.HonorSignDependentRoundingFPMathOption =
++ getEnableHonorSignDependentRoundingFPMath();
++ if (getFloatABIForCalls() != FloatABI::Default)
++ Options.FloatABIType = getFloatABIForCalls();
++ Options.NoZerosInBSS = getDontPlaceZerosInBSS();
++ Options.GuaranteedTailCallOpt = getEnableGuaranteedTailCallOpt();
++ Options.StackAlignmentOverride = getOverrideStackAlignment();
++ Options.StackSymbolOrdering = getStackSymbolOrdering();
++ Options.UseInitArray = !getUseCtors();
++ Options.RelaxELFRelocations = getRelaxELFRelocations();
++ Options.DataSections = getDataSections();
++ Options.FunctionSections = getFunctionSections();
++ Options.BBSections = getBBSectionsMode(Options);
++ Options.UniqueSectionNames = getUniqueSectionNames();
++ Options.UniqueBBSectionNames = getUniqueBBSectionNames();
++ Options.TLSSize = getTLSSize();
++ Options.EmulatedTLS = getEmulatedTLS();
++ Options.ExplicitEmulatedTLS = EmulatedTLSView->getNumOccurrences() > 0;
++ Options.ExceptionModel = getExceptionModel();
++ Options.EmitStackSizeSection = getEnableStackSizeSection();
++ Options.EmitAddrsig = getEnableAddrsig();
++ Options.EmitCallSiteInfo = getEmitCallSiteInfo();
++ Options.EnableDebugEntryValues = getEnableDebugEntryValues();
++ Options.ForceDwarfFrameSection = getForceDwarfFrameSection();
++
++ Options.MCOptions = mc::InitMCTargetOptionsFromFlags();
++
++ Options.ThreadModel = getThreadModel();
++ Options.EABIVersion = getEABIVersion();
++ Options.DebuggerTuning = getDebuggerTuningOpt();
++
++ return Options;
++}
++
++std::string codegen::getCPUStr() {
++ // If user asked for the 'native' CPU, autodetect here. If autodection fails,
++ // this will set the CPU to an empty string which tells the target to
++ // pick a basic default.
++ if (getMCPU() == "native")
++ return std::string(sys::getHostCPUName());
++
++ return getMCPU();
++}
++
++std::string codegen::getFeaturesStr() {
++ SubtargetFeatures Features;
++
++ // If user asked for the 'native' CPU, we need to autodetect features.
++ // This is necessary for x86 where the CPU might not support all the
++ // features the autodetected CPU name lists in the target. For example,
++ // not all Sandybridge processors support AVX.
++ if (getMCPU() == "native") {
++ StringMap<bool> HostFeatures;
++ if (sys::getHostCPUFeatures(HostFeatures))
++ for (auto &F : HostFeatures)
++ Features.AddFeature(F.first(), F.second);
++ }
++
++ for (auto const &MAttr : getMAttrs())
++ Features.AddFeature(MAttr);
++
++ return Features.getString();
++}
++
++std::vector<std::string> codegen::getFeatureList() {
++ SubtargetFeatures Features;
++
++ // If user asked for the 'native' CPU, we need to autodetect features.
++ // This is necessary for x86 where the CPU might not support all the
++ // features the autodetected CPU name lists in the target. For example,
++ // not all Sandybridge processors support AVX.
++ if (getMCPU() == "native") {
++ StringMap<bool> HostFeatures;
++ if (sys::getHostCPUFeatures(HostFeatures))
++ for (auto &F : HostFeatures)
++ Features.AddFeature(F.first(), F.second);
++ }
++
++ for (auto const &MAttr : getMAttrs())
++ Features.AddFeature(MAttr);
++
++ return Features.getFeatures();
++}
++
++void codegen::renderBoolStringAttr(AttrBuilder &B, StringRef Name, bool Val) {
++ B.addAttribute(Name, Val ? "true" : "false");
++}
++
++#define HANDLE_BOOL_ATTR(CL, AttrName) \
++ do { \
++ if (CL->getNumOccurrences() > 0 && !F.hasFnAttribute(AttrName)) \
++ renderBoolStringAttr(NewAttrs, AttrName, *CL); \
++ } while (0)
++
++/// Set function attributes of function \p F based on CPU, Features, and command
++/// line flags.
++void codegen::setFunctionAttributes(StringRef CPU, StringRef Features,
++ Function &F) {
++ auto &Ctx = F.getContext();
++ AttributeList Attrs = F.getAttributes();
++ AttrBuilder NewAttrs;
++
++ if (!CPU.empty() && !F.hasFnAttribute("target-cpu"))
++ NewAttrs.addAttribute("target-cpu", CPU);
++ if (!Features.empty()) {
++ // Append the command line features to any that are already on the function.
++ StringRef OldFeatures =
++ F.getFnAttribute("target-features").getValueAsString();
++ if (OldFeatures.empty())
++ NewAttrs.addAttribute("target-features", Features);
++ else {
++ SmallString<256> Appended(OldFeatures);
++ Appended.push_back(',');
++ Appended.append(Features);
++ NewAttrs.addAttribute("target-features", Appended);
++ }
++ }
++ if (FramePointerUsageView->getNumOccurrences() > 0 &&
++ !F.hasFnAttribute("frame-pointer")) {
++ if (getFramePointerUsage() == FramePointer::All)
++ NewAttrs.addAttribute("frame-pointer", "all");
++ else if (getFramePointerUsage() == FramePointer::NonLeaf)
++ NewAttrs.addAttribute("frame-pointer", "non-leaf");
++ else if (getFramePointerUsage() == FramePointer::None)
++ NewAttrs.addAttribute("frame-pointer", "none");
++ }
++ if (DisableTailCallsView->getNumOccurrences() > 0)
++ NewAttrs.addAttribute("disable-tail-calls",
++ toStringRef(getDisableTailCalls()));
++ if (getStackRealign())
++ NewAttrs.addAttribute("stackrealign");
++
++ HANDLE_BOOL_ATTR(EnableUnsafeFPMathView, "unsafe-fp-math");
++ HANDLE_BOOL_ATTR(EnableNoInfsFPMathView, "no-infs-fp-math");
++ HANDLE_BOOL_ATTR(EnableNoNaNsFPMathView, "no-nans-fp-math");
++ HANDLE_BOOL_ATTR(EnableNoSignedZerosFPMathView, "no-signed-zeros-fp-math");
++
++ if (TrapFuncNameView->getNumOccurrences() > 0)
++ for (auto &B : F)
++ for (auto &I : B)
++ if (auto *Call = dyn_cast<CallInst>(&I))
++ if (const auto *F = Call->getCalledFunction())
++ if (F->getIntrinsicID() == Intrinsic::debugtrap ||
++ F->getIntrinsicID() == Intrinsic::trap)
++ Call->addAttribute(
++ AttributeList::FunctionIndex,
++ Attribute::get(Ctx, "trap-func-name", getTrapFuncName()));
++
++ // Let NewAttrs override Attrs.
++ F.setAttributes(
++ Attrs.addAttributes(Ctx, AttributeList::FunctionIndex, NewAttrs));
++}
++
++/// Set function attributes of functions in Module M based on CPU,
++/// Features, and command line flags.
++void codegen::setFunctionAttributes(StringRef CPU, StringRef Features,
++ Module &M) {
++ for (Function &F : M)
++ setFunctionAttributes(CPU, Features, F);
++}
+diff --git a/llvm/lib/MC/CMakeLists.txt b/llvm/lib/MC/CMakeLists.txt
+index de2e47d8d9b2..ab809daf5273 100644
+--- a/llvm/lib/MC/CMakeLists.txt
++++ b/llvm/lib/MC/CMakeLists.txt
+@@ -44,6 +44,7 @@ add_llvm_component_library(LLVMMC
+ MCSymbol.cpp
+ MCSymbolELF.cpp
+ MCTargetOptions.cpp
++ MCTargetOptionsCommandFlags.cpp
+ MCValue.cpp
+ MCWasmObjectTargetWriter.cpp
+ MCWasmStreamer.cpp
+diff --git a/llvm/lib/MC/MCTargetOptionsCommandFlags.cpp b/llvm/lib/MC/MCTargetOptionsCommandFlags.cpp
+new file mode 100644
+index 000000000000..3ca34061241a
+--- /dev/null
++++ b/llvm/lib/MC/MCTargetOptionsCommandFlags.cpp
+@@ -0,0 +1,105 @@
++//===-- MCTargetOptionsCommandFlags.cpp --------------------------*- 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 contains machine code-specific flags that are shared between
++// different command line tools.
++//
++//===----------------------------------------------------------------------===//
++
++#include "llvm/MC/MCTargetOptionsCommandFlags.h"
++
++using namespace llvm;
++
++#define MCOPT(TY, NAME) \
++ static cl::opt<TY> *NAME##View; \
++ TY llvm::mc::get##NAME() { \
++ assert(NAME##View && "RegisterMCTargetOptionsFlags not created."); \
++ return *NAME##View; \
++ }
++
++#define MCOPT_EXP(TY, NAME) \
++ MCOPT(TY, NAME) \
++ Optional<TY> llvm::mc::getExplicit##NAME() { \
++ if (NAME##View->getNumOccurrences()) { \
++ TY res = *NAME##View; \
++ return res; \
++ } \
++ return None; \
++ }
++
++MCOPT_EXP(bool, RelaxAll)
++MCOPT(bool, IncrementalLinkerCompatible)
++MCOPT(int, DwarfVersion)
++MCOPT(bool, ShowMCInst)
++MCOPT(bool, FatalWarnings)
++MCOPT(bool, NoWarn)
++MCOPT(bool, NoDeprecatedWarn)
++MCOPT(std::string, ABIName)
++
++llvm::mc::RegisterMCTargetOptionsFlags::RegisterMCTargetOptionsFlags() {
++#define MCBINDOPT(NAME) \
++ do { \
++ NAME##View = std::addressof(NAME); \
++ } while (0)
++
++ static cl::opt<bool> RelaxAll(
++ "mc-relax-all", cl::desc("When used with filetype=obj, relax all fixups "
++ "in the emitted object file"));
++ MCBINDOPT(RelaxAll);
++
++ static cl::opt<bool> IncrementalLinkerCompatible(
++ "incremental-linker-compatible",
++ cl::desc(
++ "When used with filetype=obj, "
++ "emit an object file which can be used with an incremental linker"));
++ MCBINDOPT(IncrementalLinkerCompatible);
++
++ static cl::opt<int> DwarfVersion("dwarf-version", cl::desc("Dwarf version"),
++ cl::init(0));
++ MCBINDOPT(DwarfVersion);
++
++ static cl::opt<bool> ShowMCInst(
++ "asm-show-inst",
++ cl::desc("Emit internal instruction representation to assembly file"));
++ MCBINDOPT(ShowMCInst);
++
++ static cl::opt<bool> FatalWarnings("fatal-warnings",
++ cl::desc("Treat warnings as errors"));
++ MCBINDOPT(FatalWarnings);
++
++ static cl::opt<bool> NoWarn("no-warn", cl::desc("Suppress all warnings"));
++ static cl::alias NoWarnW("W", cl::desc("Alias for --no-warn"),
++ cl::aliasopt(NoWarn));
++ MCBINDOPT(NoWarn);
++
++ static cl::opt<bool> NoDeprecatedWarn(
++ "no-deprecated-warn", cl::desc("Suppress all deprecated warnings"));
++ MCBINDOPT(NoDeprecatedWarn);
++
++ static cl::opt<std::string> ABIName(
++ "target-abi", cl::Hidden,
++ cl::desc("The name of the ABI to be targeted from the backend."),
++ cl::init(""));
++ MCBINDOPT(ABIName);
++
++#undef MCBINDOPT
++}
++
++MCTargetOptions llvm::mc::InitMCTargetOptionsFromFlags() {
++ MCTargetOptions Options;
++ Options.MCRelaxAll = getRelaxAll();
++ Options.MCIncrementalLinkerCompatible = getIncrementalLinkerCompatible();
++ Options.DwarfVersion = getDwarfVersion();
++ Options.ShowMCInst = getShowMCInst();
++ Options.ABIName = getABIName();
++ Options.MCFatalWarnings = getFatalWarnings();
++ Options.MCNoWarn = getNoWarn();
++ Options.MCNoDeprecatedWarn = getNoDeprecatedWarn();
++ return Options;
++}
+diff --git a/llvm/tools/dsymutil/DwarfStreamer.cpp b/llvm/tools/dsymutil/DwarfStreamer.cpp
+index 3e132c29eada..eb068effbc71 100644
+--- a/llvm/tools/dsymutil/DwarfStreamer.cpp
++++ b/llvm/tools/dsymutil/DwarfStreamer.cpp
+@@ -13,13 +13,16 @@
+ #include "llvm/DWARFLinker/DWARFLinkerCompileUnit.h"
+ #include "llvm/DebugInfo/DWARF/DWARFContext.h"
+ #include "llvm/MC/MCTargetOptions.h"
+-#include "llvm/MC/MCTargetOptionsCommandFlags.inc"
++#include "llvm/MC/MCTargetOptionsCommandFlags.h"
+ #include "llvm/Support/LEB128.h"
+ #include "llvm/Support/TargetRegistry.h"
+ #include "llvm/Target/TargetMachine.h"
+ #include "llvm/Target/TargetOptions.h"
+
+ namespace llvm {
++
++static mc::RegisterMCTargetOptionsFlags MOF;
++
+ namespace dsymutil {
+
+ /// Retrieve the section named \a SecName in \a Obj.
+@@ -61,7 +64,7 @@ bool DwarfStreamer::init(Triple TheTriple) {
+ if (!MRI)
+ return error(Twine("no register info for target ") + TripleName, Context);
+
+- MCTargetOptions MCOptions = InitMCTargetOptionsFromFlags();
++ MCTargetOptions MCOptions = mc::InitMCTargetOptionsFromFlags();
+ MAI.reset(TheTarget->createMCAsmInfo(*MRI, TripleName, MCOptions));
+ if (!MAI)
+ return error("no asm info for target " + TripleName, Context);
+diff --git a/llvm/tools/gold/gold-plugin.cpp b/llvm/tools/gold/gold-plugin.cpp
+index 406079dad307..95e72b861197 100644
+--- a/llvm/tools/gold/gold-plugin.cpp
++++ b/llvm/tools/gold/gold-plugin.cpp
+@@ -14,7 +14,7 @@
+ #include "llvm/ADT/Statistic.h"
+ #include "llvm/Bitcode/BitcodeReader.h"
+ #include "llvm/Bitcode/BitcodeWriter.h"
+-#include "llvm/CodeGen/CommandFlags.inc"
++#include "llvm/CodeGen/CommandFlags.h"
+ #include "llvm/Config/config.h" // plugin-api.h requires HAVE_STDINT_H
+ #include "llvm/IR/Constants.h"
+ #include "llvm/IR/DiagnosticPrinter.h"
+@@ -50,6 +50,8 @@
+ using namespace llvm;
+ using namespace lto;
+
++static codegen::RegisterCodeGenFlags CodeGenFlags;
++
+ // FIXME: Remove when binutils 2.31 (containing gold 1.16) is the minimum
+ // required version.
+ typedef enum ld_plugin_status (*ld_plugin_get_wrap_symbols)(
+@@ -842,21 +844,21 @@ static std::unique_ptr<LTO> createLTO(IndexWriteCallback OnIndexWrite,
+ ThinBackend Backend;
+
+ Conf.CPU = options::mcpu;
+- Conf.Options = InitTargetOptionsFromCodeGenFlags();
++ Conf.Options = codegen::InitTargetOptionsFromCodeGenFlags();
+
+ // Disable the new X86 relax relocations since gold might not support them.
+ // FIXME: Check the gold version or add a new option to enable them.
+ Conf.Options.RelaxELFRelocations = false;
+
+ // Toggle function/data sections.
+- if (FunctionSections.getNumOccurrences() == 0)
++ if (!codegen::getExplicitFunctionSections())
+ Conf.Options.FunctionSections = SplitSections;
+- if (DataSections.getNumOccurrences() == 0)
++ if (!codegen::getExplicitDataSections())
+ Conf.Options.DataSections = SplitSections;
+
+- Conf.MAttrs = MAttrs;
+- Conf.RelocModel = RelocationModel;
+- Conf.CodeModel = getCodeModel();
++ Conf.MAttrs = codegen::getMAttrs();
++ Conf.RelocModel = codegen::getExplicitRelocModel();
++ Conf.CodeModel = codegen::getExplicitCodeModel();
+ Conf.CGOptLevel = getCGOptLevel();
+ Conf.DisableVerify = options::DisableVerify;
+ Conf.OptLevel = options::OptLevel;
+diff --git a/llvm/tools/llc/CMakeLists.txt b/llvm/tools/llc/CMakeLists.txt
+index 880deefa539c..479bc6b55b27 100644
+--- a/llvm/tools/llc/CMakeLists.txt
++++ b/llvm/tools/llc/CMakeLists.txt
+@@ -26,4 +26,5 @@ add_llvm_tool(llc
+ intrinsics_gen
+ SUPPORT_PLUGINS
+ )
++
+ export_executable_symbols(llc)
+diff --git a/llvm/tools/llc/llc.cpp b/llvm/tools/llc/llc.cpp
+index b35f8e853c30..4c41ed292fdc 100644
+--- a/llvm/tools/llc/llc.cpp
++++ b/llvm/tools/llc/llc.cpp
+@@ -15,7 +15,7 @@
+ #include "llvm/ADT/STLExtras.h"
+ #include "llvm/ADT/Triple.h"
+ #include "llvm/Analysis/TargetLibraryInfo.h"
+-#include "llvm/CodeGen/CommandFlags.inc"
++#include "llvm/CodeGen/CommandFlags.h"
+ #include "llvm/CodeGen/LinkAllAsmWriterComponents.h"
+ #include "llvm/CodeGen/LinkAllCodegenComponents.h"
+ #include "llvm/CodeGen/MIRParser/MIRParser.h"
+@@ -55,6 +55,8 @@
+ #include <memory>
+ using namespace llvm;
+
++static codegen::RegisterCodeGenFlags CGF;
++
+ // General options for llc. Other pass-specific options are specified
+ // within the corresponding llc passes, and target-specific options
+ // and back-end code generation options are specified with the target machine.
+@@ -202,7 +204,7 @@ static std::unique_ptr<ToolOutputFile> GetOutputStream(const char *TargetName,
+ else
+ OutputFilename = IFN;
+
+- switch (FileType) {
++ switch (codegen::getFileType()) {
+ case CGFT_AssemblyFile:
+ if (TargetName[0] == 'c') {
+ if (TargetName[1] == 0)
+@@ -229,7 +231,7 @@ static std::unique_ptr<ToolOutputFile> GetOutputStream(const char *TargetName,
+
+ // Decide if we need "binary" output.
+ bool Binary = false;
+- switch (FileType) {
++ switch (codegen::getFileType()) {
+ case CGFT_AssemblyFile:
+ break;
+ case CGFT_ObjectFile:
+@@ -395,14 +397,16 @@ static int compileModule(char **argv, LLVMContext &Context) {
+ std::unique_ptr<Module> M;
+ std::unique_ptr<MIRParser> MIR;
+ Triple TheTriple;
+- std::string CPUStr = getCPUStr(), FeaturesStr = getFeaturesStr();
++ std::string CPUStr = codegen::getCPUStr(),
++ FeaturesStr = codegen::getFeaturesStr();
+
+ // Set attributes on functions as loaded from MIR from command line arguments.
+ auto setMIRFunctionAttributes = [&CPUStr, &FeaturesStr](Function &F) {
+- setFunctionAttributes(CPUStr, FeaturesStr, F);
++ codegen::setFunctionAttributes(CPUStr, FeaturesStr, F);
+ };
+
+- bool SkipModule = MCPU == "help" ||
++ auto MAttrs = codegen::getMAttrs();
++ bool SkipModule = codegen::getMCPU() == "help" ||
+ (!MAttrs.empty() && MAttrs.front() == "help");
+
+ // If user just wants to list available options, skip module loading
+@@ -433,8 +437,8 @@ static int compileModule(char **argv, LLVMContext &Context) {
+
+ // Get the target specific parser.
+ std::string Error;
+- const Target *TheTarget = TargetRegistry::lookupTarget(MArch, TheTriple,
+- Error);
++ const Target *TheTarget =
++ TargetRegistry::lookupTarget(codegen::getMArch(), TheTriple, Error);
+ if (!TheTarget) {
+ WithColor::error(errs(), argv[0]) << Error;
+ return 1;
+@@ -452,7 +456,7 @@ static int compileModule(char **argv, LLVMContext &Context) {
+ case '3': OLvl = CodeGenOpt::Aggressive; break;
+ }
+
+- TargetOptions Options = InitTargetOptionsFromCodeGenFlags();
++ TargetOptions Options = codegen::InitTargetOptionsFromCodeGenFlags();
+ Options.DisableIntegratedAS = NoIntegratedAssembler;
+ Options.MCOptions.ShowMCEncoding = ShowMCEncoding;
+ Options.MCOptions.MCUseDwarfDirectory = EnableDwarfDirectory;
+@@ -462,8 +466,8 @@ static int compileModule(char **argv, LLVMContext &Context) {
+ Options.MCOptions.SplitDwarfFile = SplitDwarfFile;
+
+ std::unique_ptr<TargetMachine> Target(TheTarget->createTargetMachine(
+- TheTriple.getTriple(), CPUStr, FeaturesStr, Options, getRelocModel(),
+- getCodeModel(), OLvl));
++ TheTriple.getTriple(), CPUStr, FeaturesStr, Options, codegen::getExplicitRelocModel(),
++ codegen::getExplicitCodeModel(), OLvl));
+
+ assert(Target && "Could not allocate target machine!");
+
+@@ -474,8 +478,8 @@ static int compileModule(char **argv, LLVMContext &Context) {
+ return 0;
+
+ assert(M && "Should have exited if we didn't have a module!");
+- if (FloatABIForCalls != FloatABI::Default)
+- Options.FloatABIType = FloatABIForCalls;
++ if (codegen::getFloatABIForCalls() != FloatABI::Default)
++ Options.FloatABIType = codegen::getFloatABIForCalls();
+
+ // Figure out where we are going to send the output.
+ std::unique_ptr<ToolOutputFile> Out =
+@@ -522,10 +526,9 @@ static int compileModule(char **argv, LLVMContext &Context) {
+
+ // Override function attributes based on CPUStr, FeaturesStr, and command line
+ // flags.
+- setFunctionAttributes(CPUStr, FeaturesStr, *M);
++ codegen::setFunctionAttributes(CPUStr, FeaturesStr, *M);
+
+- if (RelaxAll.getNumOccurrences() > 0 &&
+- FileType != CGFT_ObjectFile)
++ if (mc::getExplicitRelaxAll() && codegen::getFileType() != CGFT_ObjectFile)
+ WithColor::warning(errs(), argv[0])
+ << ": warning: ignoring -mc-relax-all because filetype != obj";
+
+@@ -536,7 +539,7 @@ static int compileModule(char **argv, LLVMContext &Context) {
+ // so we can memcmp the contents in CompileTwice mode
+ SmallVector<char, 0> Buffer;
+ std::unique_ptr<raw_svector_ostream> BOS;
+- if ((FileType != CGFT_AssemblyFile &&
++ if ((codegen::getFileType() != CGFT_AssemblyFile &&
+ !Out->os().supportsSeeking()) ||
+ CompileTwice) {
+ BOS = std::make_unique<raw_svector_ostream>(Buffer);
+@@ -575,9 +578,9 @@ static int compileModule(char **argv, LLVMContext &Context) {
+ TPC.setInitialized();
+ PM.add(createPrintMIRPass(*OS));
+ PM.add(createFreeMachineFunctionPass());
+- } else if (Target->addPassesToEmitFile(PM, *OS,
+- DwoOut ? &DwoOut->os() : nullptr,
+- FileType, NoVerify, MMIWP)) {
++ } else if (Target->addPassesToEmitFile(
++ PM, *OS, DwoOut ? &DwoOut->os() : nullptr,
++ codegen::getFileType(), NoVerify, MMIWP)) {
+ WithColor::warning(errs(), argv[0])
+ << "target does not support generation of this"
+ << " file type!\n";
+diff --git a/llvm/tools/lli/CMakeLists.txt b/llvm/tools/lli/CMakeLists.txt
+index db163ad131e8..bc6ef213b8fd 100644
+--- a/llvm/tools/lli/CMakeLists.txt
++++ b/llvm/tools/lli/CMakeLists.txt
+@@ -53,4 +53,5 @@ add_llvm_tool(lli
+ DEPENDS
+ intrinsics_gen
+ )
++
+ export_executable_symbols(lli)
+diff --git a/llvm/tools/lli/lli.cpp b/llvm/tools/lli/lli.cpp
+index 0efd0df2c12b..b6a4e3f2833c 100644
+--- a/llvm/tools/lli/lli.cpp
++++ b/llvm/tools/lli/lli.cpp
+@@ -16,7 +16,7 @@
+ #include "llvm/ADT/StringExtras.h"
+ #include "llvm/ADT/Triple.h"
+ #include "llvm/Bitcode/BitcodeReader.h"
+-#include "llvm/CodeGen/CommandFlags.inc"
++#include "llvm/CodeGen/CommandFlags.h"
+ #include "llvm/CodeGen/LinkAllCodegenComponents.h"
+ #include "llvm/Config/llvm-config.h"
+ #include "llvm/ExecutionEngine/GenericValue.h"
+@@ -67,6 +67,8 @@
+
+ using namespace llvm;
+
++static codegen::RegisterCodeGenFlags CGF;
++
+ #define DEBUG_TYPE "lli"
+
+ namespace {
+@@ -410,13 +412,13 @@ int main(int argc, char **argv, char * const *envp) {
+
+ std::string ErrorMsg;
+ EngineBuilder builder(std::move(Owner));
+- builder.setMArch(MArch);
+- builder.setMCPU(getCPUStr());
+- builder.setMAttrs(getFeatureList());
+- if (RelocModel.getNumOccurrences())
+- builder.setRelocationModel(RelocModel);
+- if (CMModel.getNumOccurrences())
+- builder.setCodeModel(CMModel);
++ builder.setMArch(codegen::getMArch());
++ builder.setMCPU(codegen::getCPUStr());
++ builder.setMAttrs(codegen::getFeatureList());
++ if (auto RM = codegen::getExplicitRelocModel())
++ builder.setRelocationModel(RM.getValue());
++ if (auto CM = codegen::getExplicitCodeModel())
++ builder.setCodeModel(CM.getValue());
+ builder.setErrorStr(&ErrorMsg);
+ builder.setEngineKind(ForceInterpreter
+ ? EngineKind::Interpreter
+@@ -448,9 +450,9 @@ int main(int argc, char **argv, char * const *envp) {
+
+ builder.setOptLevel(getOptLevel());
+
+- TargetOptions Options = InitTargetOptionsFromCodeGenFlags();
+- if (FloatABIForCalls != FloatABI::Default)
+- Options.FloatABIType = FloatABIForCalls;
++ TargetOptions Options = codegen::InitTargetOptionsFromCodeGenFlags();
++ if (codegen::getFloatABIForCalls() != FloatABI::Default)
++ Options.FloatABIType = codegen::getFloatABIForCalls();
+
+ builder.setTargetOptions(Options);
+
+@@ -762,18 +764,15 @@ int runOrcLazyJIT(const char *ProgName) {
+ TT.empty() ? ExitOnErr(orc::JITTargetMachineBuilder::detectHost())
+ : orc::JITTargetMachineBuilder(Triple(TT)));
+
+- if (!MArch.empty())
+- Builder.getJITTargetMachineBuilder()->getTargetTriple().setArchName(MArch);
++ if (!codegen::getMArch().empty())
++ Builder.getJITTargetMachineBuilder()->getTargetTriple().setArchName(
++ codegen::getMArch());
+
+ Builder.getJITTargetMachineBuilder()
+- ->setCPU(getCPUStr())
+- .addFeatures(getFeatureList())
+- .setRelocationModel(RelocModel.getNumOccurrences()
+- ? Optional<Reloc::Model>(RelocModel)
+- : None)
+- .setCodeModel(CMModel.getNumOccurrences()
+- ? Optional<CodeModel::Model>(CMModel)
+- : None);
++ ->setCPU(codegen::getCPUStr())
++ .addFeatures(codegen::getFeatureList())
++ .setRelocationModel(codegen::getExplicitRelocModel())
++ .setCodeModel(codegen::getExplicitCodeModel());
+
+ Builder.setLazyCompileFailureAddr(
+ pointerToJITTargetAddress(exitOnLazyCallThroughFailure));
+diff --git a/llvm/tools/llvm-dwp/llvm-dwp.cpp b/llvm/tools/llvm-dwp/llvm-dwp.cpp
+index 23513ef8fb4e..8cfd433d5da3 100644
+--- a/llvm/tools/llvm-dwp/llvm-dwp.cpp
++++ b/llvm/tools/llvm-dwp/llvm-dwp.cpp
+@@ -27,7 +27,7 @@
+ #include "llvm/MC/MCObjectWriter.h"
+ #include "llvm/MC/MCRegisterInfo.h"
+ #include "llvm/MC/MCStreamer.h"
+-#include "llvm/MC/MCTargetOptionsCommandFlags.inc"
++#include "llvm/MC/MCTargetOptionsCommandFlags.h"
+ #include "llvm/Object/Decompressor.h"
+ #include "llvm/Object/ObjectFile.h"
+ #include "llvm/Support/DataExtractor.h"
+@@ -46,6 +46,8 @@
+ using namespace llvm;
+ using namespace llvm::object;
+
++static mc::RegisterMCTargetOptionsFlags MCTargetOptionsFlags;
++
+ cl::OptionCategory DwpCategory("Specific Options");
+ static cl::list<std::string> InputFiles(cl::Positional, cl::ZeroOrMore,
+ cl::desc("<input files>"),
+@@ -676,7 +678,7 @@ int main(int argc, char **argv) {
+ if (!MRI)
+ return error(Twine("no register info for target ") + TripleName, Context);
+
+- MCTargetOptions MCOptions = InitMCTargetOptionsFromFlags();
++ MCTargetOptions MCOptions = llvm::mc::InitMCTargetOptionsFromFlags();
+ std::unique_ptr<MCAsmInfo> MAI(
+ TheTarget->createMCAsmInfo(*MRI, TripleName, MCOptions));
+ if (!MAI)
+diff --git a/llvm/tools/llvm-isel-fuzzer/llvm-isel-fuzzer.cpp b/llvm/tools/llvm-isel-fuzzer/llvm-isel-fuzzer.cpp
+index b71ed4a70566..627e9ab4c03f 100644
+--- a/llvm/tools/llvm-isel-fuzzer/llvm-isel-fuzzer.cpp
++++ b/llvm/tools/llvm-isel-fuzzer/llvm-isel-fuzzer.cpp
+@@ -14,7 +14,7 @@
+ #include "llvm/Analysis/TargetLibraryInfo.h"
+ #include "llvm/Bitcode/BitcodeReader.h"
+ #include "llvm/Bitcode/BitcodeWriter.h"
+-#include "llvm/CodeGen/CommandFlags.inc"
++#include "llvm/CodeGen/CommandFlags.h"
+ #include "llvm/FuzzMutate/FuzzerCLI.h"
+ #include "llvm/FuzzMutate/IRMutator.h"
+ #include "llvm/FuzzMutate/Operations.h"
+@@ -35,6 +35,8 @@
+
+ using namespace llvm;
+
++static codegen::RegisterCodeGenFlags CGF;
++
+ static cl::opt<char>
+ OptLevel("O",
+ cl::desc("Optimization level. [-O0, -O1, -O2, or -O3] "
+@@ -133,14 +135,15 @@ extern "C" LLVM_ATTRIBUTE_USED int LLVMFuzzerInitialize(int *argc,
+ // Get the target specific parser.
+ std::string Error;
+ const Target *TheTarget =
+- TargetRegistry::lookupTarget(MArch, TheTriple, Error);
++ TargetRegistry::lookupTarget(codegen::getMArch(), TheTriple, Error);
+ if (!TheTarget) {
+ errs() << argv[0] << ": " << Error;
+ return 1;
+ }
+
+ // Set up the pipeline like llc does.
+- std::string CPUStr = getCPUStr(), FeaturesStr = getFeaturesStr();
++ std::string CPUStr = codegen::getCPUStr(),
++ FeaturesStr = codegen::getFeaturesStr();
+
+ CodeGenOpt::Level OLvl = CodeGenOpt::Default;
+ switch (OptLevel) {
+@@ -154,10 +157,10 @@ extern "C" LLVM_ATTRIBUTE_USED int LLVMFuzzerInitialize(int *argc,
+ case '3': OLvl = CodeGenOpt::Aggressive; break;
+ }
+
+- TargetOptions Options = InitTargetOptionsFromCodeGenFlags();
+- TM.reset(TheTarget->createTargetMachine(TheTriple.getTriple(), CPUStr,
+- FeaturesStr, Options, getRelocModel(),
+- getCodeModel(), OLvl));
++ TargetOptions Options = codegen::InitTargetOptionsFromCodeGenFlags();
++ TM.reset(TheTarget->createTargetMachine(
++ TheTriple.getTriple(), CPUStr, FeaturesStr, Options,
++ codegen::getExplicitRelocModel(), codegen::getExplicitCodeModel(), OLvl));
+ assert(TM && "Could not allocate target machine!");
+
+ // Make sure we print the summary and the current unit when LLVM errors out.
+diff --git a/llvm/tools/llvm-lto/CMakeLists.txt b/llvm/tools/llvm-lto/CMakeLists.txt
+index 69868fb870c0..5128e713eecf 100644
+--- a/llvm/tools/llvm-lto/CMakeLists.txt
++++ b/llvm/tools/llvm-lto/CMakeLists.txt
+@@ -5,6 +5,7 @@ set(LLVM_LINK_COMPONENTS
+ AllTargetsInfos
+ BitReader
+ BitWriter
++ CodeGen
+ Core
+ IRReader
+ LTO
+@@ -17,7 +18,5 @@ set(LLVM_LINK_COMPONENTS
+ add_llvm_tool(llvm-lto
+ llvm-lto.cpp
+
+- DEPENDS
+- intrinsics_gen
++ DEPENDS intrinsics_gen
+ )
+-
+diff --git a/llvm/tools/llvm-lto/llvm-lto.cpp b/llvm/tools/llvm-lto/llvm-lto.cpp
+index b47e68e82850..7886426e8945 100644
+--- a/llvm/tools/llvm-lto/llvm-lto.cpp
++++ b/llvm/tools/llvm-lto/llvm-lto.cpp
+@@ -21,7 +21,7 @@
+ #include "llvm/ADT/Twine.h"
+ #include "llvm/Bitcode/BitcodeReader.h"
+ #include "llvm/Bitcode/BitcodeWriter.h"
+-#include "llvm/CodeGen/CommandFlags.inc"
++#include "llvm/CodeGen/CommandFlags.h"
+ #include "llvm/IR/DiagnosticInfo.h"
+ #include "llvm/IR/DiagnosticPrinter.h"
+ #include "llvm/IR/LLVMContext.h"
+@@ -62,6 +62,8 @@
+
+ using namespace llvm;
+
++static codegen::RegisterCodeGenFlags CGF;
++
+ static cl::opt<char>
+ OptLevel("O", cl::desc("Optimization level. [-O0, -O1, -O2, or -O3] "
+ "(default = '-O2')"),
+@@ -521,7 +523,7 @@ public:
+ ThinLTOCodeGenerator ThinGenerator;
+
+ ThinLTOProcessing(const TargetOptions &Options) {
+- ThinGenerator.setCodePICModel(getRelocModel());
++ ThinGenerator.setCodePICModel(codegen::getExplicitRelocModel());
+ ThinGenerator.setTargetOptions(Options);
+ ThinGenerator.setCacheDir(ThinLTOCacheDir);
+ ThinGenerator.setCachePruningInterval(ThinLTOCachePruningInterval);
+@@ -873,7 +875,7 @@ int main(int argc, char **argv) {
+ InitializeAllAsmParsers();
+
+ // set up the TargetOptions for the machine
+- TargetOptions Options = InitTargetOptionsFromCodeGenFlags();
++ TargetOptions Options = codegen::InitTargetOptionsFromCodeGenFlags();
+
+ if (ListSymbolsOnly) {
+ listSymbols(Options);
+@@ -929,7 +931,7 @@ int main(int argc, char **argv) {
+ if (UseDiagnosticHandler)
+ CodeGen.setDiagnosticHandler(handleDiagnostics, nullptr);
+
+- CodeGen.setCodePICModel(getRelocModel());
++ CodeGen.setCodePICModel(codegen::getExplicitRelocModel());
+ CodeGen.setFreestanding(EnableFreestanding);
+
+ CodeGen.setDebugInfo(LTO_DEBUG_MODEL_DWARF);
+@@ -980,22 +982,18 @@ int main(int argc, char **argv) {
+ CodeGen.addMustPreserveSymbol(KeptDSOSyms[i]);
+
+ // Set cpu and attrs strings for the default target/subtarget.
+- CodeGen.setCpu(MCPU.c_str());
++ CodeGen.setCpu(codegen::getMCPU().c_str());
+
+ CodeGen.setOptLevel(OptLevel - '0');
+
+- std::string attrs;
+- for (unsigned i = 0; i < MAttrs.size(); ++i) {
+- if (i > 0)
+- attrs.append(",");
+- attrs.append(MAttrs[i]);
+- }
+-
+- if (!attrs.empty())
++ auto MAttrs = codegen::getMAttrs();
++ if (!MAttrs.empty()) {
++ std::string attrs = join(MAttrs, ",");
+ CodeGen.setAttr(attrs);
++ }
+
+- if (FileType.getNumOccurrences())
+- CodeGen.setFileType(FileType);
++ if (auto FT = codegen::getExplicitFileType())
++ CodeGen.setFileType(FT.getValue());
+
+ if (!OutputFilename.empty()) {
+ if (!CodeGen.optimize(DisableVerify, DisableInline, DisableGVNLoadPRE,
+diff --git a/llvm/tools/llvm-lto2/CMakeLists.txt b/llvm/tools/llvm-lto2/CMakeLists.txt
+index fa2d8624fd94..4d3364175b04 100644
+--- a/llvm/tools/llvm-lto2/CMakeLists.txt
++++ b/llvm/tools/llvm-lto2/CMakeLists.txt
+@@ -4,6 +4,7 @@ set(LLVM_LINK_COMPONENTS
+ AllTargetsDescs
+ AllTargetsInfos
+ BitReader
++ CodeGen
+ Core
+ Linker
+ LTO
+diff --git a/llvm/tools/llvm-lto2/llvm-lto2.cpp b/llvm/tools/llvm-lto2/llvm-lto2.cpp
+index 67a677dd45fb..142ba605aa2a 100644
+--- a/llvm/tools/llvm-lto2/llvm-lto2.cpp
++++ b/llvm/tools/llvm-lto2/llvm-lto2.cpp
+@@ -16,7 +16,7 @@
+ //===----------------------------------------------------------------------===//
+
+ #include "llvm/Bitcode/BitcodeReader.h"
+-#include "llvm/CodeGen/CommandFlags.inc"
++#include "llvm/CodeGen/CommandFlags.h"
+ #include "llvm/IR/DiagnosticPrinter.h"
+ #include "llvm/LTO/Caching.h"
+ #include "llvm/LTO/LTO.h"
+@@ -29,6 +29,8 @@
+ using namespace llvm;
+ using namespace lto;
+
++static codegen::RegisterCodeGenFlags CGF;
++
+ static cl::opt<char>
+ OptLevel("O", cl::desc("Optimization level. [-O0, -O1, -O2, or -O3] "
+ "(default = '-O2')"),
+@@ -217,12 +219,12 @@ static int run(int argc, char **argv) {
+ exit(1);
+ };
+
+- Conf.CPU = MCPU;
+- Conf.Options = InitTargetOptionsFromCodeGenFlags();
+- Conf.MAttrs = MAttrs;
+- if (auto RM = getRelocModel())
+- Conf.RelocModel = *RM;
+- Conf.CodeModel = getCodeModel();
++ Conf.CPU = codegen::getMCPU();
++ Conf.Options = codegen::InitTargetOptionsFromCodeGenFlags();
++ Conf.MAttrs = codegen::getMAttrs();
++ if (auto RM = codegen::getExplicitRelocModel())
++ Conf.RelocModel = RM.getValue();
++ Conf.CodeModel = codegen::getExplicitCodeModel();
+
+ Conf.DebugPassManager = DebugPassManager;
+
+@@ -264,8 +266,8 @@ static int run(int argc, char **argv) {
+ return 1;
+ }
+
+- if (FileType.getNumOccurrences())
+- Conf.CGFileType = FileType;
++ if (auto FT = codegen::getExplicitFileType())
++ Conf.CGFileType = FT.getValue();
+
+ Conf.OverrideTriple = OverrideTriple;
+ Conf.DefaultTriple = DefaultTriple;
+diff --git a/llvm/tools/llvm-mc-assemble-fuzzer/CMakeLists.txt b/llvm/tools/llvm-mc-assemble-fuzzer/CMakeLists.txt
+index fb6befd3c54a..6bbc502e2eee 100644
+--- a/llvm/tools/llvm-mc-assemble-fuzzer/CMakeLists.txt
++++ b/llvm/tools/llvm-mc-assemble-fuzzer/CMakeLists.txt
+@@ -7,6 +7,7 @@ set(LLVM_LINK_COMPONENTS
+ MCParser
+ Support
+ )
++
+ add_llvm_fuzzer(llvm-mc-assemble-fuzzer
+ llvm-mc-assemble-fuzzer.cpp
+ )
+diff --git a/llvm/tools/llvm-mc-assemble-fuzzer/llvm-mc-assemble-fuzzer.cpp b/llvm/tools/llvm-mc-assemble-fuzzer/llvm-mc-assemble-fuzzer.cpp
+index 6c5961f7027c..29699c634bfa 100644
+--- a/llvm/tools/llvm-mc-assemble-fuzzer/llvm-mc-assemble-fuzzer.cpp
++++ b/llvm/tools/llvm-mc-assemble-fuzzer/llvm-mc-assemble-fuzzer.cpp
+@@ -9,7 +9,6 @@
+ //===----------------------------------------------------------------------===//
+
+ #include "llvm-c/Target.h"
+-#include "llvm/MC/SubtargetFeature.h"
+ #include "llvm/MC/MCAsmBackend.h"
+ #include "llvm/MC/MCAsmInfo.h"
+ #include "llvm/MC/MCCodeEmitter.h"
+@@ -24,15 +23,16 @@
+ #include "llvm/MC/MCSectionMachO.h"
+ #include "llvm/MC/MCStreamer.h"
+ #include "llvm/MC/MCSubtargetInfo.h"
+-#include "llvm/MC/MCTargetOptionsCommandFlags.inc"
+-#include "llvm/Support/MemoryBuffer.h"
++#include "llvm/MC/MCTargetOptionsCommandFlags.h"
++#include "llvm/MC/SubtargetFeature.h"
+ #include "llvm/Support/CommandLine.h"
+ #include "llvm/Support/FileUtilities.h"
+-#include "llvm/Support/raw_ostream.h"
++#include "llvm/Support/MemoryBuffer.h"
+ #include "llvm/Support/SourceMgr.h"
+-#include "llvm/Support/TargetSelect.h"
+ #include "llvm/Support/TargetRegistry.h"
++#include "llvm/Support/TargetSelect.h"
+ #include "llvm/Support/ToolOutputFile.h"
++#include "llvm/Support/raw_ostream.h"
+
+ using namespace llvm;
+
+@@ -161,7 +161,7 @@ int AssembleOneInput(const uint8_t *Data, size_t Size) {
+ abort();
+ }
+
+- MCTargetOptions MCOptions = InitMCTargetOptionsFromFlags();
++ MCTargetOptions MCOptions = mc::InitMCTargetOptionsFromFlags();
+ std::unique_ptr<MCAsmInfo> MAI(
+ TheTarget->createMCAsmInfo(*MRI, TripleName, MCOptions));
+ if (!MAI) {
+diff --git a/llvm/tools/llvm-mc/CMakeLists.txt b/llvm/tools/llvm-mc/CMakeLists.txt
+index 15c6dda1b258..94add70b6943 100644
+--- a/llvm/tools/llvm-mc/CMakeLists.txt
++++ b/llvm/tools/llvm-mc/CMakeLists.txt
+@@ -4,6 +4,7 @@ set(LLVM_LINK_COMPONENTS
+ AllTargetsDescs
+ AllTargetsDisassemblers
+ AllTargetsInfos
++ CodeGen
+ MC
+ MCParser
+ Support
+diff --git a/llvm/tools/llvm-mc/llvm-mc.cpp b/llvm/tools/llvm-mc/llvm-mc.cpp
+index 6aa347d98be2..8c1b3cf2cab0 100644
+--- a/llvm/tools/llvm-mc/llvm-mc.cpp
++++ b/llvm/tools/llvm-mc/llvm-mc.cpp
+@@ -25,7 +25,7 @@
+ #include "llvm/MC/MCRegisterInfo.h"
+ #include "llvm/MC/MCStreamer.h"
+ #include "llvm/MC/MCSubtargetInfo.h"
+-#include "llvm/MC/MCTargetOptionsCommandFlags.inc"
++#include "llvm/MC/MCTargetOptionsCommandFlags.h"
+ #include "llvm/Support/CommandLine.h"
+ #include "llvm/Support/Compression.h"
+ #include "llvm/Support/FileUtilities.h"
+@@ -41,6 +41,8 @@
+
+ using namespace llvm;
+
++static mc::RegisterMCTargetOptionsFlags MOF;
++
+ static cl::opt<std::string>
+ InputFilename(cl::Positional, cl::desc("<input file>"), cl::init("-"));
+
+@@ -317,7 +319,7 @@ int main(int argc, char **argv) {
+ cl::AddExtraVersionPrinter(TargetRegistry::printRegisteredTargetsForVersion);
+
+ cl::ParseCommandLineOptions(argc, argv, "llvm machine code playground\n");
+- const MCTargetOptions MCOptions = InitMCTargetOptionsFromFlags();
++ const MCTargetOptions MCOptions = mc::InitMCTargetOptionsFromFlags();
+ setDwarfDebugFlags(argc, argv);
+
+ setDwarfDebugProducer();
+diff --git a/llvm/tools/llvm-mca/llvm-mca.cpp b/llvm/tools/llvm-mca/llvm-mca.cpp
+index fff5906bb59b..eca86768aa5c 100644
+--- a/llvm/tools/llvm-mca/llvm-mca.cpp
++++ b/llvm/tools/llvm-mca/llvm-mca.cpp
+@@ -39,7 +39,7 @@
+ #include "llvm/MC/MCObjectFileInfo.h"
+ #include "llvm/MC/MCRegisterInfo.h"
+ #include "llvm/MC/MCSubtargetInfo.h"
+-#include "llvm/MC/MCTargetOptionsCommandFlags.inc"
++#include "llvm/MC/MCTargetOptionsCommandFlags.h"
+ #include "llvm/MCA/CodeEmitter.h"
+ #include "llvm/MCA/Context.h"
+ #include "llvm/MCA/InstrBuilder.h"
+@@ -62,6 +62,8 @@
+
+ using namespace llvm;
+
++static mc::RegisterMCTargetOptionsFlags MOF;
++
+ static cl::OptionCategory ToolOptions("Tool Options");
+ static cl::OptionCategory ViewOptions("View Options");
+
+@@ -353,7 +355,7 @@ int main(int argc, char **argv) {
+ std::unique_ptr<MCRegisterInfo> MRI(TheTarget->createMCRegInfo(TripleName));
+ assert(MRI && "Unable to create target register info!");
+
+- MCTargetOptions MCOptions = InitMCTargetOptionsFromFlags();
++ MCTargetOptions MCOptions = mc::InitMCTargetOptionsFromFlags();
+ std::unique_ptr<MCAsmInfo> MAI(
+ TheTarget->createMCAsmInfo(*MRI, TripleName, MCOptions));
+ assert(MAI && "Unable to create target asm info!");
+@@ -443,7 +445,7 @@ int main(int argc, char **argv) {
+ TheTarget->createMCCodeEmitter(*MCII, *MRI, Ctx));
+
+ std::unique_ptr<MCAsmBackend> MAB(TheTarget->createMCAsmBackend(
+- *STI, *MRI, InitMCTargetOptionsFromFlags()));
++ *STI, *MRI, mc::InitMCTargetOptionsFromFlags()));
+
+ for (const std::unique_ptr<mca::CodeRegion> &Region : Regions) {
+ // Skip empty code regions.
+diff --git a/llvm/tools/llvm-opt-fuzzer/llvm-opt-fuzzer.cpp b/llvm/tools/llvm-opt-fuzzer/llvm-opt-fuzzer.cpp
+index cd6c7d380cc6..e353e333f580 100644
+--- a/llvm/tools/llvm-opt-fuzzer/llvm-opt-fuzzer.cpp
++++ b/llvm/tools/llvm-opt-fuzzer/llvm-opt-fuzzer.cpp
+@@ -12,7 +12,7 @@
+
+ #include "llvm/Bitcode/BitcodeReader.h"
+ #include "llvm/Bitcode/BitcodeWriter.h"
+-#include "llvm/CodeGen/CommandFlags.inc"
++#include "llvm/CodeGen/CommandFlags.h"
+ #include "llvm/FuzzMutate/FuzzerCLI.h"
+ #include "llvm/FuzzMutate/IRMutator.h"
+ #include "llvm/IR/Verifier.h"
+@@ -24,6 +24,8 @@
+
+ using namespace llvm;
+
++static codegen::RegisterCodeGenFlags CGF;
++
+ static cl::opt<std::string>
+ TargetTripleStr("mtriple", cl::desc("Override target triple for module"));
+
+@@ -124,7 +126,8 @@ extern "C" int LLVMFuzzerTestOneInput(const uint8_t *Data, size_t Size) {
+
+ M->setTargetTriple(TM->getTargetTriple().normalize());
+ M->setDataLayout(TM->createDataLayout());
+- setFunctionAttributes(TM->getTargetCPU(), TM->getTargetFeatureString(), *M);
++ codegen::setFunctionAttributes(TM->getTargetCPU(),
++ TM->getTargetFeatureString(), *M);
+
+ // Create pass pipeline
+ //
+@@ -214,16 +217,17 @@ extern "C" LLVM_ATTRIBUTE_USED int LLVMFuzzerInitialize(
+
+ std::string Error;
+ const Target *TheTarget =
+- TargetRegistry::lookupTarget(MArch, TargetTriple, Error);
++ TargetRegistry::lookupTarget(codegen::getMArch(), TargetTriple, Error);
+ if (!TheTarget) {
+ errs() << *argv[0] << ": " << Error;
+ exit(1);
+ }
+
+- TargetOptions Options = InitTargetOptionsFromCodeGenFlags();
++ TargetOptions Options = codegen::InitTargetOptionsFromCodeGenFlags();
+ TM.reset(TheTarget->createTargetMachine(
+- TargetTriple.getTriple(), getCPUStr(), getFeaturesStr(),
+- Options, getRelocModel(), getCodeModel(), CodeGenOpt::Default));
++ TargetTriple.getTriple(), codegen::getCPUStr(), codegen::getFeaturesStr(),
++ Options, codegen::getExplicitRelocModel(),
++ codegen::getExplicitCodeModel(), CodeGenOpt::Default));
+ assert(TM && "Could not allocate target machine!");
+
+ // Check that pass pipeline is specified and correct
+diff --git a/llvm/tools/lto/CMakeLists.txt b/llvm/tools/lto/CMakeLists.txt
+index b86e4abd01a7..2963f97cad88 100644
+--- a/llvm/tools/lto/CMakeLists.txt
++++ b/llvm/tools/lto/CMakeLists.txt
+@@ -6,6 +6,7 @@ set(LLVM_LINK_COMPONENTS
+ AllTargetsInfos
+ BitReader
+ Core
++ CodeGen
+ LTO
+ MC
+ MCDisassembler
+@@ -20,7 +21,8 @@ set(SOURCES
+
+ set(LLVM_EXPORTED_SYMBOL_FILE ${CMAKE_CURRENT_SOURCE_DIR}/lto.exports)
+
+-add_llvm_library(LTO SHARED INSTALL_WITH_TOOLCHAIN ${SOURCES} DEPENDS intrinsics_gen)
++add_llvm_library(LTO SHARED INSTALL_WITH_TOOLCHAIN ${SOURCES} DEPENDS
++ intrinsics_gen)
+
+ install(FILES ${LLVM_MAIN_INCLUDE_DIR}/llvm-c/lto.h
+ DESTINATION include/llvm-c
+diff --git a/llvm/tools/lto/lto.cpp b/llvm/tools/lto/lto.cpp
+index 9933af94de1e..6d207b76685f 100644
+--- a/llvm/tools/lto/lto.cpp
++++ b/llvm/tools/lto/lto.cpp
+@@ -15,7 +15,7 @@
+ #include "llvm/ADT/STLExtras.h"
+ #include "llvm/ADT/StringExtras.h"
+ #include "llvm/Bitcode/BitcodeReader.h"
+-#include "llvm/CodeGen/CommandFlags.inc"
++#include "llvm/CodeGen/CommandFlags.h"
+ #include "llvm/IR/DiagnosticInfo.h"
+ #include "llvm/IR/DiagnosticPrinter.h"
+ #include "llvm/IR/LLVMContext.h"
+@@ -28,6 +28,10 @@
+ #include "llvm/Support/TargetSelect.h"
+ #include "llvm/Support/raw_ostream.h"
+
++using namespace llvm;
++
++static codegen::RegisterCodeGenFlags CGF;
++
+ // extra command-line flags needed for LTOCodeGenerator
+ static cl::opt<char>
+ OptLevel("O",
+@@ -154,14 +158,9 @@ DEFINE_SIMPLE_CONVERSION_FUNCTIONS(LTOModule, lto_module_t)
+ // Convert the subtarget features into a string to pass to LTOCodeGenerator.
+ static void lto_add_attrs(lto_code_gen_t cg) {
+ LTOCodeGenerator *CG = unwrap(cg);
+- if (MAttrs.size()) {
+- std::string attrs;
+- for (unsigned i = 0; i < MAttrs.size(); ++i) {
+- if (i > 0)
+- attrs.append(",");
+- attrs.append(MAttrs[i]);
+- }
+-
++ auto MAttrs = codegen::getMAttrs();
++ if (!MAttrs.empty()) {
++ std::string attrs = join(MAttrs, ",");
+ CG->setAttr(attrs);
+ }
+
+@@ -219,7 +218,7 @@ lto_module_is_object_file_in_memory_for_target(const void* mem,
+
+ lto_module_t lto_module_create(const char* path) {
+ lto_initialize();
+- llvm::TargetOptions Options = InitTargetOptionsFromCodeGenFlags();
++ llvm::TargetOptions Options = codegen::InitTargetOptionsFromCodeGenFlags();
+ ErrorOr<std::unique_ptr<LTOModule>> M =
+ LTOModule::createFromFile(*LTOContext, StringRef(path), Options);
+ if (!M)
+@@ -229,7 +228,7 @@ lto_module_t lto_module_create(const char* path) {
+
+ lto_module_t lto_module_create_from_fd(int fd, const char *path, size_t size) {
+ lto_initialize();
+- llvm::TargetOptions Options = InitTargetOptionsFromCodeGenFlags();
++ llvm::TargetOptions Options = codegen::InitTargetOptionsFromCodeGenFlags();
+ ErrorOr<std::unique_ptr<LTOModule>> M = LTOModule::createFromOpenFile(
+ *LTOContext, fd, StringRef(path), size, Options);
+ if (!M)
+@@ -242,7 +241,7 @@ lto_module_t lto_module_create_from_fd_at_offset(int fd, const char *path,
+ size_t map_size,
+ off_t offset) {
+ lto_initialize();
+- llvm::TargetOptions Options = InitTargetOptionsFromCodeGenFlags();
++ llvm::TargetOptions Options = codegen::InitTargetOptionsFromCodeGenFlags();
+ ErrorOr<std::unique_ptr<LTOModule>> M = LTOModule::createFromOpenFileSlice(
+ *LTOContext, fd, StringRef(path), map_size, offset, Options);
+ if (!M)
+@@ -252,7 +251,7 @@ lto_module_t lto_module_create_from_fd_at_offset(int fd, const char *path,
+
+ lto_module_t lto_module_create_from_memory(const void* mem, size_t length) {
+ lto_initialize();
+- llvm::TargetOptions Options = InitTargetOptionsFromCodeGenFlags();
++ llvm::TargetOptions Options = codegen::InitTargetOptionsFromCodeGenFlags();
+ ErrorOr<std::unique_ptr<LTOModule>> M =
+ LTOModule::createFromBuffer(*LTOContext, mem, length, Options);
+ if (!M)
+@@ -264,7 +263,7 @@ lto_module_t lto_module_create_from_memory_with_path(const void* mem,
+ size_t length,
+ const char *path) {
+ lto_initialize();
+- llvm::TargetOptions Options = InitTargetOptionsFromCodeGenFlags();
++ llvm::TargetOptions Options = codegen::InitTargetOptionsFromCodeGenFlags();
+ ErrorOr<std::unique_ptr<LTOModule>> M = LTOModule::createFromBuffer(
+ *LTOContext, mem, length, Options, StringRef(path));
+ if (!M)
+@@ -275,7 +274,7 @@ lto_module_t lto_module_create_from_memory_with_path(const void* mem,
+ lto_module_t lto_module_create_in_local_context(const void *mem, size_t length,
+ const char *path) {
+ lto_initialize();
+- llvm::TargetOptions Options = InitTargetOptionsFromCodeGenFlags();
++ llvm::TargetOptions Options = codegen::InitTargetOptionsFromCodeGenFlags();
+
+ // Create a local context. Ownership will be transferred to LTOModule.
+ std::unique_ptr<LLVMContext> Context = std::make_unique<LLVMContext>();
+@@ -294,7 +293,7 @@ lto_module_t lto_module_create_in_codegen_context(const void *mem,
+ const char *path,
+ lto_code_gen_t cg) {
+ lto_initialize();
+- llvm::TargetOptions Options = InitTargetOptionsFromCodeGenFlags();
++ llvm::TargetOptions Options = codegen::InitTargetOptionsFromCodeGenFlags();
+ ErrorOr<std::unique_ptr<LTOModule>> M = LTOModule::createFromBuffer(
+ unwrap(cg)->getContext(), mem, length, Options, StringRef(path));
+ return wrap(M->release());
+@@ -336,7 +335,7 @@ void lto_codegen_set_diagnostic_handler(lto_code_gen_t cg,
+ static lto_code_gen_t createCodeGen(bool InLocalContext) {
+ lto_initialize();
+
+- TargetOptions Options = InitTargetOptionsFromCodeGenFlags();
++ TargetOptions Options = codegen::InitTargetOptionsFromCodeGenFlags();
+
+ LibLTOCodeGenerator *CodeGen =
+ InLocalContext ? new LibLTOCodeGenerator(std::make_unique<LLVMContext>())
+@@ -484,7 +483,7 @@ void lto_codegen_set_should_embed_uselists(lto_code_gen_t cg,
+ thinlto_code_gen_t thinlto_create_codegen(void) {
+ lto_initialize();
+ ThinLTOCodeGenerator *CodeGen = new ThinLTOCodeGenerator();
+- CodeGen->setTargetOptions(InitTargetOptionsFromCodeGenFlags());
++ CodeGen->setTargetOptions(codegen::InitTargetOptionsFromCodeGenFlags());
+ CodeGen->setFreestanding(EnableFreestanding);
+
+ if (OptLevel.getNumOccurrences()) {
+diff --git a/llvm/tools/opt/opt.cpp b/llvm/tools/opt/opt.cpp
+index 75a6cdc3892b..8d619ef76b7a 100644
+--- a/llvm/tools/opt/opt.cpp
++++ b/llvm/tools/opt/opt.cpp
+@@ -22,7 +22,7 @@
+ #include "llvm/Analysis/TargetLibraryInfo.h"
+ #include "llvm/Analysis/TargetTransformInfo.h"
+ #include "llvm/Bitcode/BitcodeWriterPass.h"
+-#include "llvm/CodeGen/CommandFlags.inc"
++#include "llvm/CodeGen/CommandFlags.h"
+ #include "llvm/CodeGen/TargetPassConfig.h"
+ #include "llvm/Config/llvm-config.h"
+ #include "llvm/IR/DataLayout.h"
+@@ -61,6 +61,8 @@
+ using namespace llvm;
+ using namespace opt_tool;
+
++static codegen::RegisterCodeGenFlags CFG;
++
+ // The OptimizationList is automatically populated with registered Passes by the
+ // PassNameParser.
+ //
+@@ -470,16 +472,17 @@ static TargetMachine* GetTargetMachine(Triple TheTriple, StringRef CPUStr,
+ StringRef FeaturesStr,
+ const TargetOptions &Options) {
+ std::string Error;
+- const Target *TheTarget = TargetRegistry::lookupTarget(MArch, TheTriple,
+- Error);
++ const Target *TheTarget =
++ TargetRegistry::lookupTarget(codegen::getMArch(), TheTriple, Error);
+ // Some modules don't specify a triple, and this is okay.
+ if (!TheTarget) {
+ return nullptr;
+ }
+
+- return TheTarget->createTargetMachine(TheTriple.getTriple(), CPUStr,
+- FeaturesStr, Options, getRelocModel(),
+- getCodeModel(), GetCodeGenOptLevel());
++ return TheTarget->createTargetMachine(
++ TheTriple.getTriple(), codegen::getCPUStr(), codegen::getFeaturesStr(),
++ Options, codegen::getExplicitRelocModel(),
++ codegen::getExplicitCodeModel(), GetCodeGenOptLevel());
+ }
+
+ #ifdef BUILD_EXAMPLES
+@@ -659,11 +662,11 @@ int main(int argc, char **argv) {
+ Triple ModuleTriple(M->getTargetTriple());
+ std::string CPUStr, FeaturesStr;
+ TargetMachine *Machine = nullptr;
+- const TargetOptions Options = InitTargetOptionsFromCodeGenFlags();
++ const TargetOptions Options = codegen::InitTargetOptionsFromCodeGenFlags();
+
+ if (ModuleTriple.getArch()) {
+- CPUStr = getCPUStr();
+- FeaturesStr = getFeaturesStr();
++ CPUStr = codegen::getCPUStr();
++ FeaturesStr = codegen::getFeaturesStr();
+ Machine = GetTargetMachine(ModuleTriple, CPUStr, FeaturesStr, Options);
+ } else if (ModuleTriple.getArchName() != "unknown" &&
+ ModuleTriple.getArchName() != "") {
+@@ -676,7 +679,7 @@ int main(int argc, char **argv) {
+
+ // Override function attributes based on CPUStr, FeaturesStr, and command line
+ // flags.
+- setFunctionAttributes(CPUStr, FeaturesStr, *M);
++ codegen::setFunctionAttributes(CPUStr, FeaturesStr, *M);
+
+ // If the output is set to be emitted to standard out, and standard out is a
+ // console, print out a warning message and refuse to do it. We don't
+diff --git a/llvm/unittests/DebugInfo/DWARF/DwarfGenerator.cpp b/llvm/unittests/DebugInfo/DWARF/DwarfGenerator.cpp
+index 472d4dd6ad1e..32d1d3c91ff2 100644
+--- a/llvm/unittests/DebugInfo/DWARF/DwarfGenerator.cpp
++++ b/llvm/unittests/DebugInfo/DWARF/DwarfGenerator.cpp
+@@ -25,7 +25,7 @@
+ #include "llvm/MC/MCRegisterInfo.h"
+ #include "llvm/MC/MCStreamer.h"
+ #include "llvm/MC/MCSubtargetInfo.h"
+-#include "llvm/MC/MCTargetOptionsCommandFlags.inc"
++#include "llvm/MC/MCTargetOptionsCommandFlags.h"
+ #include "llvm/PassAnalysisSupport.h"
+ #include "llvm/Support/TargetRegistry.h"
+ #include "llvm/Support/raw_ostream.h"
+@@ -36,6 +36,8 @@
+ using namespace llvm;
+ using namespace dwarf;
+
++mc::RegisterMCTargetOptionsFlags MOF;
++
+ namespace {} // end anonymous namespace
+
+ //===----------------------------------------------------------------------===//
+@@ -410,7 +412,7 @@ llvm::Error dwarfgen::Generator::init(Triple TheTriple, uint16_t V) {
+ TripleName,
+ inconvertibleErrorCode());
+
+- MCTargetOptions MCOptions = InitMCTargetOptionsFromFlags();
++ MCTargetOptions MCOptions = mc::InitMCTargetOptionsFromFlags();
+ MAI.reset(TheTarget->createMCAsmInfo(*MRI, TripleName, MCOptions));
+ if (!MAI)
+ return make_error<StringError>("no asm info for target " + TripleName,
+--
+2.33.1
+
diff --git a/dynamic-layers/clang-layer/recipes-devtools/clang/files/llvm10-basic-block-sections-support.patch b/dynamic-layers/clang-layer/recipes-devtools/clang/files/llvm10-basic-block-sections-support.patch
new file mode 100644
index 00000000..f90a79ae
--- /dev/null
+++ b/dynamic-layers/clang-layer/recipes-devtools/clang/files/llvm10-basic-block-sections-support.patch
@@ -0,0 +1,237 @@
+From d51fdb9f2986747a56c593fa057d531720b39deb Mon Sep 17 00:00:00 2001
+From: Sriraman Tallam <tmsriram@google.com>
+Date: Fri, 13 Mar 2020 15:58:57 -0700
+Subject: [PATCH] Basic Block Sections Support.
+
+This is the first in a series of patches to enable Basic Block Sections
+in LLVM.
+
+We introduce a new compiler option, -fbasicblock-sections=, which places every
+basic block in a unique ELF text section in the object file along with a
+symbol labeling the basic block. The linker can then order the basic block
+sections in any arbitrary sequence which when done correctly can encapsulate
+block layout, function layout and function splitting optimizations. However,
+there are a couple of challenges to be addressed for this to be feasible:
+
+1) The compiler must not allow any implicit fall-through between any two
+ adjacent basic blocks as they could be reordered at link time to be
+ non-adjacent. In other words, the compiler must make a fall-through
+ between adjacent basic blocks explicit by retaining the direct jump
+ instruction that jumps to the next basic block. These branches can only
+ be removed later by the linker after the blocks have been reordered.
+2) All inter-basic block branch targets would now need to be resolved by
+ the linker as they cannot be calculated during compile time. This is
+ done using static relocations which bloats the size of the object files.
+ Further, the compiler tries to use short branch instructions on some ISAs
+ for branch offsets that can be accommodated in one byte. This is not
+ possible with basic block sections as the offset is not determined at
+ compile time, and long branch instructions have to be used everywhere.
+3) Each additional section bloats object file sizes by tens of bytes. The
+ number of basic blocks can be potentially very large compared to the
+ size of functions and can bloat object sizes significantly. Option
+ fbasicblock-sections= also takes a file path which can be used to
+ specify a subset of basic blocks that needs unique sections to keep
+ the bloats small.
+4) Debug Info and CFI need special handling and will be presented as
+ separate patches.
+
+Basic Block Labels
+
+With -fbasicblock-sections=labels, or when a basic block is placed in a
+unique section, it is labelled with a symbol. This allows easy mapping of
+virtual addresses from PMU profiles back to the corresponding basic blocks.
+Since the number of basic blocks is large, the labeling bloats the symbol
+table sizes and the string table sizes significantly. While the binary size
+does increase, it does not affect performance as the symbol table is not
+loaded in memory during run-time. The string table size bloat is kept very
+minimal using a unary naming scheme that uses string suffix compression.
+The basic blocks for function foo are named "a.BB.foo", "aa.BB.foo", ...
+This turns out to be very good for string table sizes and the bloat in the
+string table size for a very large binary is ~8 %. The naming also allows
+using the --symbol-ordering-file option in LLD to arbitrarily reorder the
+sections.
+
+Differential Revision: https://reviews.llvm.org/D68063
+
+Upstream-Status: Backport [https://github.com/llvm/llvm-project/commit/4dfe92e46542be46d634a7ec24da2f2f889623d0]
+Signed-off-by: Anuj Mittal <anuj.mittal@intel.com>
+---
+ llvm/include/llvm/CodeGen/CommandFlags.inc | 34 ++++++++++++++++++++++
+ llvm/include/llvm/Target/TargetMachine.h | 14 +++++++++
+ llvm/include/llvm/Target/TargetOptions.h | 31 ++++++++++++++++++--
+ 3 files changed, 76 insertions(+), 3 deletions(-)
+
+diff --git a/llvm/include/llvm/CodeGen/CommandFlags.inc b/llvm/include/llvm/CodeGen/CommandFlags.inc
+index 8739b644873d..6475a5b19edb 100644
+--- a/llvm/include/llvm/CodeGen/CommandFlags.inc
++++ b/llvm/include/llvm/CodeGen/CommandFlags.inc
+@@ -238,6 +238,12 @@ static cl::opt<bool>
+ cl::desc("Emit functions into separate sections"),
+ cl::init(false));
+
++static cl::opt<std::string>
++ BBSections("basicblock-sections",
++ cl::desc("Emit basic blocks into separate sections"),
++ cl::value_desc("all | <function list (file)> | labels | none"),
++ cl::init("none"));
++
+ static cl::opt<unsigned> TLSSize("tls-size",
+ cl::desc("Bit size of immediate TLS offsets"),
+ cl::init(0));
+@@ -251,6 +257,11 @@ static cl::opt<bool>
+ cl::desc("Give unique names to every section"),
+ cl::init(true));
+
++static cl::opt<bool> UniqueBBSectionNames(
++ "unique-bb-section-names",
++ cl::desc("Give unique names to every basic block section"),
++ cl::init(false));
++
+ static cl::opt<llvm::EABI>
+ EABIVersion("meabi", cl::desc("Set EABI type (default depends on triple):"),
+ cl::init(EABI::Default),
+@@ -285,6 +296,27 @@ static cl::opt<bool>
+ cl::desc("Always emit a debug frame section."),
+ cl::init(false));
+
++static llvm::BasicBlockSection
++getBBSectionsMode(llvm::TargetOptions &Options) {
++ if (BBSections == "all")
++ return BasicBlockSection::All;
++ else if (BBSections == "labels")
++ return BasicBlockSection::Labels;
++ else if (BBSections == "none")
++ return BasicBlockSection::None;
++ else {
++ ErrorOr<std::unique_ptr<MemoryBuffer>> MBOrErr =
++ MemoryBuffer::getFile(BBSections);
++ if (!MBOrErr) {
++ errs() << "Error loading basic block sections function list file: "
++ << MBOrErr.getError().message() << "\n";
++ } else {
++ Options.BBSectionsFuncListBuf = std::move(*MBOrErr);
++ }
++ return BasicBlockSection::List;
++ }
++}
++
+ // Common utility function tightly tied to the options listed here. Initializes
+ // a TargetOptions object with CodeGen flags and returns it.
+ static TargetOptions InitTargetOptionsFromCodeGenFlags() {
+@@ -308,7 +340,9 @@ static TargetOptions InitTargetOptionsFromCodeGenFlags() {
+ Options.RelaxELFRelocations = RelaxELFRelocations;
+ Options.DataSections = DataSections;
+ Options.FunctionSections = FunctionSections;
++ Options.BBSections = getBBSectionsMode(Options);
+ Options.UniqueSectionNames = UniqueSectionNames;
++ Options.UniqueBBSectionNames = UniqueBBSectionNames;
+ Options.TLSSize = TLSSize;
+ Options.EmulatedTLS = EmulatedTLS;
+ Options.ExplicitEmulatedTLS = EmulatedTLS.getNumOccurrences() > 0;
+diff --git a/llvm/include/llvm/Target/TargetMachine.h b/llvm/include/llvm/Target/TargetMachine.h
+index 176ae39b17a7..4a1f3377f31d 100644
+--- a/llvm/include/llvm/Target/TargetMachine.h
++++ b/llvm/include/llvm/Target/TargetMachine.h
+@@ -242,6 +242,9 @@ public:
+
+ bool getUniqueSectionNames() const { return Options.UniqueSectionNames; }
+
++ /// Return true if unique basic block section names must be generated.
++ bool getUniqueBBSectionNames() const { return Options.UniqueBBSectionNames; }
++
+ /// Return true if data objects should be emitted into their own section,
+ /// corresponds to -fdata-sections.
+ bool getDataSections() const {
+@@ -254,6 +257,17 @@ public:
+ return Options.FunctionSections;
+ }
+
++ /// If basic blocks should be emitted into their own section,
++ /// corresponding to -fbasicblock-sections.
++ llvm::BasicBlockSection getBBSectionsType() const {
++ return Options.BBSections;
++ }
++
++ /// Get the list of functions and basic block ids that need unique sections.
++ const MemoryBuffer *getBBSectionsFuncListBuf() const {
++ return Options.BBSectionsFuncListBuf.get();
++ }
++
+ /// Get a \c TargetIRAnalysis appropriate for the target.
+ ///
+ /// This is used to construct the new pass manager's target IR analysis pass,
+diff --git a/llvm/include/llvm/Target/TargetOptions.h b/llvm/include/llvm/Target/TargetOptions.h
+index 84c6ee2a6387..d27c7b0178f0 100644
+--- a/llvm/include/llvm/Target/TargetOptions.h
++++ b/llvm/include/llvm/Target/TargetOptions.h
+@@ -16,8 +16,11 @@
+
+ #include "llvm/MC/MCTargetOptions.h"
+
++#include <memory>
++
+ namespace llvm {
+ class MachineFunction;
++ class MemoryBuffer;
+ class Module;
+
+ namespace FloatABI {
+@@ -63,6 +66,18 @@ namespace llvm {
+ };
+ }
+
++ enum class BasicBlockSection {
++ All, // Use Basic Block Sections for all basic blocks. A section
++ // for every basic block can significantly bloat object file sizes.
++ List, // Get list of functions & BBs from a file. Selectively enables
++ // basic block sections for a subset of basic blocks which can be
++ // used to control object size bloats from creating sections.
++ Labels, // Do not use Basic Block Sections but label basic blocks. This
++ // is useful when associating profile counts from virtual addresses
++ // to basic blocks.
++ None // Do not use Basic Block Sections.
++ };
++
+ enum class EABI {
+ Unknown,
+ Default, // Default means not specified
+@@ -114,9 +129,9 @@ namespace llvm {
+ EnableFastISel(false), EnableGlobalISel(false), UseInitArray(false),
+ DisableIntegratedAS(false), RelaxELFRelocations(false),
+ FunctionSections(false), DataSections(false),
+- UniqueSectionNames(true), TrapUnreachable(false),
+- NoTrapAfterNoreturn(false), TLSSize(0), EmulatedTLS(false),
+- ExplicitEmulatedTLS(false), EnableIPRA(false),
++ UniqueSectionNames(true), UniqueBBSectionNames(false),
++ TrapUnreachable(false), NoTrapAfterNoreturn(false), TLSSize(0),
++ EmulatedTLS(false), ExplicitEmulatedTLS(false), EnableIPRA(false),
+ EmitStackSizeSection(false), EnableMachineOutliner(false),
+ SupportsDefaultOutlining(false), EmitAddrsig(false),
+ EnableDebugEntryValues(false), ForceDwarfFrameSection(false) {}
+@@ -224,6 +239,9 @@ namespace llvm {
+
+ unsigned UniqueSectionNames : 1;
+
++ /// Use unique names for basic block sections.
++ unsigned UniqueBBSectionNames : 1;
++
+ /// Emit target-specific trap instruction for 'unreachable' IR instructions.
+ unsigned TrapUnreachable : 1;
+
+@@ -256,6 +274,13 @@ namespace llvm {
+ /// Emit address-significance table.
+ unsigned EmitAddrsig : 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 debug info about parameter's entry values.
+ unsigned EnableDebugEntryValues : 1;
+
+--
+2.33.1
+
diff --git a/dynamic-layers/clang-layer/recipes-devtools/clang/llvm-project-source.bbappend b/dynamic-layers/clang-layer/recipes-devtools/clang/llvm-project-source.bbappend
index 96011f1d..9701aca6 100644
--- a/dynamic-layers/clang-layer/recipes-devtools/clang/llvm-project-source.bbappend
+++ b/dynamic-layers/clang-layer/recipes-devtools/clang/llvm-project-source.bbappend
@@ -23,6 +23,9 @@ SRC_URI_LLVM10_PATCHES = " \
file://llvm10-0013-ispc-10_0_k_reg_mov_avx512_i8_i16.patch \
file://llvm10-0014-ispc-10_0_packed_load_store_avx512skx.patch \
file://llvm10-0015-ispc-10_0_vXi1calling_avx512_i8_i16.patch \
+ file://llvm10-basic-block-sections-support.patch \
+ file://llvm10-Enable-the-call-site-info-only-for-g-optimizations.patch \
+ file://llvm10-Replace-MCTargetOptionsCommandFlags.inc-and-CommandF.patch \
"
SRC_URI_LLVM12_PATCHES = " \