[lld] [clang] [llvm] Embed the command line arguments during LTO (PR #79390)

2024-01-24 Thread Duncan Ogilvie via cfe-commits

https://github.com/mrexodia created 
https://github.com/llvm/llvm-project/pull/79390

This was previously marked as `FIXME` in `LTOBackend.cpp`.

List of changes:
- Removed the unused `lto::Config.MllvmArgs` field
- Add a new `lto::Config.EmbedCmdArgs` field (`\0`-separated list, since that's 
how the `.llvmcmd` works currently)
- Completely get rid of the separate `CmdArgs` argument from the various LTO 
functions (since it now uses `Conf.EmbedCmdArgs` internally instead)
- Add a new `cmdArgs` member to the `CommonLinkerContext`
- Store the `opt::ArgList` in the `commonContext()` for every driver

Potentially controversial changes:
1. `LTOBitcodeEmbedding::EmbedOptimized` previously didn't embed the command 
line for unknown reasons, this has been changed
2. The `embedBitcodeInModule` function now always embeds two new module flags: 
`embed.cmd` and `embed.cwd` (naming suggestions welcome).

For my use case I need to be able to reproduce the linking process from just 
the embedded bitcode file. The current working directory is necessary for this, 
since it is controlled by the build system and it cannot always be guessed. The 
original `FIXME` comment seems to agree with my use case, but the `.llvmbc` 
section alone definitely isn't enough (and in my opinion this does not count as 
doing it from just the bitcode):

> [...] the motivation for capturing post-merge bitcode and command line is 
> replicating the compilation environment from bitcode, without needing to 
> understand the dependencies (the functions to be imported). This assumes a 
> clang - based invocation, case in which we have the command line.

>From 5a7c330982873c9198ef8c6f90d10f3ccb9ac5d9 Mon Sep 17 00:00:00 2001
From: Duncan Ogilvie 
Date: Thu, 25 Jan 2024 00:08:49 +0100
Subject: [PATCH] Embed the command line arguments during LTO

---
 clang/lib/CodeGen/BackendUtil.cpp|  3 +-
 lld/COFF/Driver.cpp  |  1 +
 lld/COFF/LTO.cpp |  3 +-
 lld/Common/CommonLinkerContext.cpp   |  9 +
 lld/ELF/Driver.cpp   |  1 +
 lld/ELF/LTO.cpp  |  3 +-
 lld/MachO/Driver.cpp |  1 +
 lld/MachO/LTO.cpp|  3 +-
 lld/MinGW/Driver.cpp |  1 +
 lld/include/lld/Common/CommonLinkerContext.h |  3 ++
 lld/wasm/Driver.cpp  |  1 +
 lld/wasm/LTO.cpp |  2 +
 llvm/include/llvm/LTO/Config.h   |  2 +-
 llvm/include/llvm/LTO/LTOBackend.h   |  6 +--
 llvm/lib/Bitcode/Writer/BitcodeWriter.cpp| 14 +++
 llvm/lib/LTO/LTO.cpp |  3 +-
 llvm/lib/LTO/LTOBackend.cpp  | 39 +---
 llvm/lib/LTO/LTOCodeGenerator.cpp|  3 +-
 18 files changed, 60 insertions(+), 38 deletions(-)

diff --git a/clang/lib/CodeGen/BackendUtil.cpp 
b/clang/lib/CodeGen/BackendUtil.cpp
index ec203f6f28bc173..71aee18e63574e8 100644
--- a/clang/lib/CodeGen/BackendUtil.cpp
+++ b/clang/lib/CodeGen/BackendUtil.cpp
@@ -1208,6 +1208,7 @@ static void runThinLTOBackend(
   Conf.CPU = TOpts.CPU;
   Conf.CodeModel = getCodeModel(CGOpts);
   Conf.MAttrs = TOpts.Features;
+  Conf.EmbedCmdArgs = CGOpts.CmdArgs;
   Conf.RelocModel = CGOpts.RelocationModel;
   std::optional OptLevelOrNone =
   CodeGenOpt::getLevel(CGOpts.OptimizationLevel);
@@ -1269,7 +1270,7 @@ static void runThinLTOBackend(
   if (Error E =
   thinBackend(Conf, -1, AddStream, *M, *CombinedIndex, ImportList,
   ModuleToDefinedGVSummaries[M->getModuleIdentifier()],
-  /* ModuleMap */ nullptr, CGOpts.CmdArgs)) {
+  /* ModuleMap */ nullptr)) {
 handleAllErrors(std::move(E), [&](ErrorInfoBase &EIB) {
   errs() << "Error running ThinLTO backend: " << EIB.message() << '\n';
 });
diff --git a/lld/COFF/Driver.cpp b/lld/COFF/Driver.cpp
index e0afb6b18805b2e..8515aa6889fd020 100644
--- a/lld/COFF/Driver.cpp
+++ b/lld/COFF/Driver.cpp
@@ -1447,6 +1447,7 @@ void LinkerDriver::linkerMain(ArrayRef 
argsArr) {
   // Parse command line options.
   ArgParser parser(ctx);
   opt::InputArgList args = parser.parse(argsArr);
+  commonContext().storeCmdArgs(args);
 
   // Initialize time trace profiler.
   config->timeTraceEnabled = args.hasArg(OPT_time_trace_eq);
diff --git a/lld/COFF/LTO.cpp b/lld/COFF/LTO.cpp
index 7df931911213672..ba6c5c6b241137e 100644
--- a/lld/COFF/LTO.cpp
+++ b/lld/COFF/LTO.cpp
@@ -52,8 +52,7 @@ lto::Config BitcodeCompiler::createConfig() {
   lto::Config c;
   c.Options = initTargetOptionsFromCodeGenFlags();
   c.Options.EmitAddrsig = true;
-  for (StringRef C : ctx.config.mllvmOpts)
-c.MllvmArgs.emplace_back(C.str());
+  c.EmbedCmdArgs = commonContext().cmdArgs;
 
   // Always emit a section per function/datum with LTO. LLVM LTO should get 
most
   // of the benefit of linker GC, but there are still opportunities for ICF.
diff --git a/

[lld] [clang] [llvm] Embed the command line arguments during LTO (PR #79390)

2024-01-24 Thread Duncan Ogilvie via cfe-commits

https://github.com/mrexodia edited 
https://github.com/llvm/llvm-project/pull/79390
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[lld] [clang] [llvm] Embed the command line arguments during LTO (PR #79390)

2024-01-24 Thread Duncan Ogilvie via cfe-commits

https://github.com/mrexodia updated 
https://github.com/llvm/llvm-project/pull/79390

>From d60c2ad5bc88d6b64d7b3d6b179e303e3eeb936c Mon Sep 17 00:00:00 2001
From: Duncan Ogilvie 
Date: Thu, 25 Jan 2024 00:08:49 +0100
Subject: [PATCH] Embed the command line arguments during LTO

---
 clang/lib/CodeGen/BackendUtil.cpp|  3 +-
 lld/COFF/Driver.cpp  |  1 +
 lld/COFF/LTO.cpp |  3 +-
 lld/Common/CommonLinkerContext.cpp   |  9 +
 lld/ELF/Driver.cpp   |  1 +
 lld/ELF/LTO.cpp  |  3 +-
 lld/MachO/Driver.cpp |  1 +
 lld/MachO/LTO.cpp|  3 +-
 lld/MinGW/Driver.cpp |  1 +
 lld/include/lld/Common/CommonLinkerContext.h |  3 ++
 lld/wasm/Driver.cpp  |  1 +
 lld/wasm/LTO.cpp |  2 +
 llvm/include/llvm/LTO/Config.h   |  2 +-
 llvm/include/llvm/LTO/LTOBackend.h   |  6 +--
 llvm/lib/Bitcode/Writer/BitcodeWriter.cpp| 14 +++
 llvm/lib/LTO/LTO.cpp |  3 +-
 llvm/lib/LTO/LTOBackend.cpp  | 39 +---
 llvm/lib/LTO/LTOCodeGenerator.cpp|  3 +-
 18 files changed, 60 insertions(+), 38 deletions(-)

diff --git a/clang/lib/CodeGen/BackendUtil.cpp 
b/clang/lib/CodeGen/BackendUtil.cpp
index ec203f6f28bc17..71aee18e63574e 100644
--- a/clang/lib/CodeGen/BackendUtil.cpp
+++ b/clang/lib/CodeGen/BackendUtil.cpp
@@ -1208,6 +1208,7 @@ static void runThinLTOBackend(
   Conf.CPU = TOpts.CPU;
   Conf.CodeModel = getCodeModel(CGOpts);
   Conf.MAttrs = TOpts.Features;
+  Conf.EmbedCmdArgs = CGOpts.CmdArgs;
   Conf.RelocModel = CGOpts.RelocationModel;
   std::optional OptLevelOrNone =
   CodeGenOpt::getLevel(CGOpts.OptimizationLevel);
@@ -1269,7 +1270,7 @@ static void runThinLTOBackend(
   if (Error E =
   thinBackend(Conf, -1, AddStream, *M, *CombinedIndex, ImportList,
   ModuleToDefinedGVSummaries[M->getModuleIdentifier()],
-  /* ModuleMap */ nullptr, CGOpts.CmdArgs)) {
+  /* ModuleMap */ nullptr)) {
 handleAllErrors(std::move(E), [&](ErrorInfoBase &EIB) {
   errs() << "Error running ThinLTO backend: " << EIB.message() << '\n';
 });
diff --git a/lld/COFF/Driver.cpp b/lld/COFF/Driver.cpp
index e0afb6b18805b2..8515aa6889fd02 100644
--- a/lld/COFF/Driver.cpp
+++ b/lld/COFF/Driver.cpp
@@ -1447,6 +1447,7 @@ void LinkerDriver::linkerMain(ArrayRef 
argsArr) {
   // Parse command line options.
   ArgParser parser(ctx);
   opt::InputArgList args = parser.parse(argsArr);
+  commonContext().storeCmdArgs(args);
 
   // Initialize time trace profiler.
   config->timeTraceEnabled = args.hasArg(OPT_time_trace_eq);
diff --git a/lld/COFF/LTO.cpp b/lld/COFF/LTO.cpp
index 7df93191121367..ba6c5c6b241137 100644
--- a/lld/COFF/LTO.cpp
+++ b/lld/COFF/LTO.cpp
@@ -52,8 +52,7 @@ lto::Config BitcodeCompiler::createConfig() {
   lto::Config c;
   c.Options = initTargetOptionsFromCodeGenFlags();
   c.Options.EmitAddrsig = true;
-  for (StringRef C : ctx.config.mllvmOpts)
-c.MllvmArgs.emplace_back(C.str());
+  c.EmbedCmdArgs = commonContext().cmdArgs;
 
   // Always emit a section per function/datum with LTO. LLVM LTO should get 
most
   // of the benefit of linker GC, but there are still opportunities for ICF.
diff --git a/lld/Common/CommonLinkerContext.cpp 
b/lld/Common/CommonLinkerContext.cpp
index 12f56bc10ec963..57aae8fd0a703d 100644
--- a/lld/Common/CommonLinkerContext.cpp
+++ b/lld/Common/CommonLinkerContext.cpp
@@ -37,6 +37,15 @@ CommonLinkerContext::~CommonLinkerContext() {
   lctx = nullptr;
 }
 
+void CommonLinkerContext::storeCmdArgs(const llvm::opt::ArgList &args) {
+  cmdArgs.clear();
+  for (const llvm::opt::Arg *arg : args) {
+StringRef str(args.getArgString(arg->getIndex()));
+cmdArgs.insert(cmdArgs.end(), str.begin(), str.end());
+cmdArgs.push_back('\0');
+  }
+}
+
 CommonLinkerContext &lld::commonContext() {
   assert(lctx);
   return *lctx;
diff --git a/lld/ELF/Driver.cpp b/lld/ELF/Driver.cpp
index f4b7d1c9d5b973..770ab73a2da689 100644
--- a/lld/ELF/Driver.cpp
+++ b/lld/ELF/Driver.cpp
@@ -573,6 +573,7 @@ constexpr const char *saveTempsValues[] = {
 void LinkerDriver::linkerMain(ArrayRef argsArr) {
   ELFOptTable parser;
   opt::InputArgList args = parser.parse(argsArr.slice(1));
+  commonContext().storeCmdArgs(args);
 
   // Interpret these flags early because error()/warn() depend on them.
   errorHandler().errorLimit = args::getInteger(args, OPT_error_limit, 20);
diff --git a/lld/ELF/LTO.cpp b/lld/ELF/LTO.cpp
index c39c6e6ea74ba3..33927c7f4ea705 100644
--- a/lld/ELF/LTO.cpp
+++ b/lld/ELF/LTO.cpp
@@ -54,8 +54,7 @@ static lto::Config createConfig() {
   // LLD supports the new relocations and address-significance tables.
   c.Options = initTargetOptionsFromCodeGenFlags();
   c.Options.EmitAddrsig = true;
-  for (StringRe

[lld] [clang] [llvm] Embed the command line arguments during LTO (PR #79390)

2024-01-24 Thread Duncan Ogilvie via cfe-commits


@@ -40,6 +41,7 @@ using namespace llvm;
 namespace lld::wasm {
 static std::unique_ptr createLTO() {
   lto::Config c;
+  c.EmbedCmdArgs = commonContext().cmdArgs;

mrexodia wrote:

Thanks for the comment! I don't think it makes a difference, except that 
`context()` needs a `T` (which derives from `CommonLinkerContext` regardless) 
and this would result in more typing to get the exact same instance.

https://github.com/llvm/llvm-project/pull/79390
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[llvm] [lld] [clang] Embed the command line arguments during LTO (PR #79390)

2024-01-24 Thread Duncan Ogilvie via cfe-commits

https://github.com/mrexodia updated 
https://github.com/llvm/llvm-project/pull/79390

>From 9ca7af3aa2401daeecc50772ebbd58f14d2fa3e9 Mon Sep 17 00:00:00 2001
From: Duncan Ogilvie 
Date: Thu, 25 Jan 2024 00:08:49 +0100
Subject: [PATCH] Embed the command line arguments during LTO

---
 clang/lib/CodeGen/BackendUtil.cpp|  3 +-
 lld/COFF/Driver.cpp  |  1 +
 lld/COFF/LTO.cpp |  3 +-
 lld/Common/CommonLinkerContext.cpp   |  9 ++
 lld/ELF/Driver.cpp   |  1 +
 lld/ELF/LTO.cpp  |  3 +-
 lld/MachO/Driver.cpp |  1 +
 lld/MachO/LTO.cpp|  3 +-
 lld/MinGW/Driver.cpp |  1 +
 lld/include/lld/Common/CommonLinkerContext.h |  3 ++
 lld/wasm/Driver.cpp  |  1 +
 lld/wasm/LTO.cpp |  2 ++
 llvm/include/llvm/LTO/Config.h   |  2 +-
 llvm/include/llvm/LTO/LTOBackend.h   |  6 ++--
 llvm/lib/Bitcode/Writer/BitcodeWriter.cpp| 14 ++
 llvm/lib/LTO/LTO.cpp |  3 +-
 llvm/lib/LTO/LTOBackend.cpp  | 29 ++--
 llvm/lib/LTO/LTOCodeGenerator.cpp|  3 +-
 18 files changed, 52 insertions(+), 36 deletions(-)

diff --git a/clang/lib/CodeGen/BackendUtil.cpp 
b/clang/lib/CodeGen/BackendUtil.cpp
index ec203f6f28bc173..71aee18e63574e8 100644
--- a/clang/lib/CodeGen/BackendUtil.cpp
+++ b/clang/lib/CodeGen/BackendUtil.cpp
@@ -1208,6 +1208,7 @@ static void runThinLTOBackend(
   Conf.CPU = TOpts.CPU;
   Conf.CodeModel = getCodeModel(CGOpts);
   Conf.MAttrs = TOpts.Features;
+  Conf.EmbedCmdArgs = CGOpts.CmdArgs;
   Conf.RelocModel = CGOpts.RelocationModel;
   std::optional OptLevelOrNone =
   CodeGenOpt::getLevel(CGOpts.OptimizationLevel);
@@ -1269,7 +1270,7 @@ static void runThinLTOBackend(
   if (Error E =
   thinBackend(Conf, -1, AddStream, *M, *CombinedIndex, ImportList,
   ModuleToDefinedGVSummaries[M->getModuleIdentifier()],
-  /* ModuleMap */ nullptr, CGOpts.CmdArgs)) {
+  /* ModuleMap */ nullptr)) {
 handleAllErrors(std::move(E), [&](ErrorInfoBase &EIB) {
   errs() << "Error running ThinLTO backend: " << EIB.message() << '\n';
 });
diff --git a/lld/COFF/Driver.cpp b/lld/COFF/Driver.cpp
index e0afb6b18805b2e..8515aa6889fd020 100644
--- a/lld/COFF/Driver.cpp
+++ b/lld/COFF/Driver.cpp
@@ -1447,6 +1447,7 @@ void LinkerDriver::linkerMain(ArrayRef 
argsArr) {
   // Parse command line options.
   ArgParser parser(ctx);
   opt::InputArgList args = parser.parse(argsArr);
+  commonContext().storeCmdArgs(args);
 
   // Initialize time trace profiler.
   config->timeTraceEnabled = args.hasArg(OPT_time_trace_eq);
diff --git a/lld/COFF/LTO.cpp b/lld/COFF/LTO.cpp
index 7df931911213672..ba6c5c6b241137e 100644
--- a/lld/COFF/LTO.cpp
+++ b/lld/COFF/LTO.cpp
@@ -52,8 +52,7 @@ lto::Config BitcodeCompiler::createConfig() {
   lto::Config c;
   c.Options = initTargetOptionsFromCodeGenFlags();
   c.Options.EmitAddrsig = true;
-  for (StringRef C : ctx.config.mllvmOpts)
-c.MllvmArgs.emplace_back(C.str());
+  c.EmbedCmdArgs = commonContext().cmdArgs;
 
   // Always emit a section per function/datum with LTO. LLVM LTO should get 
most
   // of the benefit of linker GC, but there are still opportunities for ICF.
diff --git a/lld/Common/CommonLinkerContext.cpp 
b/lld/Common/CommonLinkerContext.cpp
index 12f56bc10ec9631..57aae8fd0a703d8 100644
--- a/lld/Common/CommonLinkerContext.cpp
+++ b/lld/Common/CommonLinkerContext.cpp
@@ -37,6 +37,15 @@ CommonLinkerContext::~CommonLinkerContext() {
   lctx = nullptr;
 }
 
+void CommonLinkerContext::storeCmdArgs(const llvm::opt::ArgList &args) {
+  cmdArgs.clear();
+  for (const llvm::opt::Arg *arg : args) {
+StringRef str(args.getArgString(arg->getIndex()));
+cmdArgs.insert(cmdArgs.end(), str.begin(), str.end());
+cmdArgs.push_back('\0');
+  }
+}
+
 CommonLinkerContext &lld::commonContext() {
   assert(lctx);
   return *lctx;
diff --git a/lld/ELF/Driver.cpp b/lld/ELF/Driver.cpp
index f4b7d1c9d5b9736..770ab73a2da689a 100644
--- a/lld/ELF/Driver.cpp
+++ b/lld/ELF/Driver.cpp
@@ -573,6 +573,7 @@ constexpr const char *saveTempsValues[] = {
 void LinkerDriver::linkerMain(ArrayRef argsArr) {
   ELFOptTable parser;
   opt::InputArgList args = parser.parse(argsArr.slice(1));
+  commonContext().storeCmdArgs(args);
 
   // Interpret these flags early because error()/warn() depend on them.
   errorHandler().errorLimit = args::getInteger(args, OPT_error_limit, 20);
diff --git a/lld/ELF/LTO.cpp b/lld/ELF/LTO.cpp
index c39c6e6ea74ba33..33927c7f4ea7059 100644
--- a/lld/ELF/LTO.cpp
+++ b/lld/ELF/LTO.cpp
@@ -54,8 +54,7 @@ static lto::Config createConfig() {
   // LLD supports the new relocations and address-significance tables.
   c.Options = initTargetOptionsFromCodeGenFlags();
   c.Options.EmitAddrsig = true

[lld] [clang] [llvm] Embed the command line arguments during LTO (PR #79390)

2024-01-24 Thread Duncan Ogilvie via cfe-commits

https://github.com/mrexodia edited 
https://github.com/llvm/llvm-project/pull/79390
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[llvm] [lld] [clang] Embed the command line arguments during LTO (PR #79390)

2024-01-24 Thread Duncan Ogilvie via cfe-commits


@@ -40,6 +41,7 @@ using namespace llvm;
 namespace lld::wasm {
 static std::unique_ptr createLTO() {
   lto::Config c;
+  c.EmbedCmdArgs = commonContext().cmdArgs;

mrexodia wrote:

For most drivers (except ELF) I can access the specialized context directly. 
But when creating the LTO config this isn’t the case. I can add a helper 
function, but it only complicates the code in my opinion. Tomorrow I will take 
another look to see if I missed something.

https://github.com/llvm/llvm-project/pull/79390
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[lld] [llvm] [clang] Embed the command line arguments during LTO (PR #79390)

2024-01-25 Thread Duncan Ogilvie via cfe-commits

https://github.com/mrexodia updated 
https://github.com/llvm/llvm-project/pull/79390

>From 67280cb8a77931271e685f7c92718d45cfea69a8 Mon Sep 17 00:00:00 2001
From: Duncan Ogilvie 
Date: Thu, 25 Jan 2024 00:08:49 +0100
Subject: [PATCH] Embed the command line arguments during LTO

---
 clang/lib/CodeGen/BackendUtil.cpp|  3 +-
 lld/COFF/Driver.cpp  |  1 +
 lld/COFF/LTO.cpp |  3 +-
 lld/Common/CommonLinkerContext.cpp   |  9 ++
 lld/ELF/Driver.cpp   |  1 +
 lld/ELF/LTO.cpp  |  3 +-
 lld/MachO/Driver.cpp |  1 +
 lld/MachO/LTO.cpp|  3 +-
 lld/MinGW/Driver.cpp |  1 +
 lld/include/lld/Common/CommonLinkerContext.h |  3 ++
 lld/wasm/Driver.cpp  |  1 +
 lld/wasm/LTO.cpp |  2 ++
 llvm/include/llvm/LTO/Config.h   |  2 +-
 llvm/include/llvm/LTO/LTOBackend.h   |  6 ++--
 llvm/lib/Bitcode/Writer/BitcodeWriter.cpp| 14 ++
 llvm/lib/LTO/LTO.cpp |  3 +-
 llvm/lib/LTO/LTOBackend.cpp  | 29 ++--
 llvm/lib/LTO/LTOCodeGenerator.cpp|  3 +-
 18 files changed, 52 insertions(+), 36 deletions(-)

diff --git a/clang/lib/CodeGen/BackendUtil.cpp 
b/clang/lib/CodeGen/BackendUtil.cpp
index ec203f6f28bc173..71aee18e63574e8 100644
--- a/clang/lib/CodeGen/BackendUtil.cpp
+++ b/clang/lib/CodeGen/BackendUtil.cpp
@@ -1208,6 +1208,7 @@ static void runThinLTOBackend(
   Conf.CPU = TOpts.CPU;
   Conf.CodeModel = getCodeModel(CGOpts);
   Conf.MAttrs = TOpts.Features;
+  Conf.EmbedCmdArgs = CGOpts.CmdArgs;
   Conf.RelocModel = CGOpts.RelocationModel;
   std::optional OptLevelOrNone =
   CodeGenOpt::getLevel(CGOpts.OptimizationLevel);
@@ -1269,7 +1270,7 @@ static void runThinLTOBackend(
   if (Error E =
   thinBackend(Conf, -1, AddStream, *M, *CombinedIndex, ImportList,
   ModuleToDefinedGVSummaries[M->getModuleIdentifier()],
-  /* ModuleMap */ nullptr, CGOpts.CmdArgs)) {
+  /* ModuleMap */ nullptr)) {
 handleAllErrors(std::move(E), [&](ErrorInfoBase &EIB) {
   errs() << "Error running ThinLTO backend: " << EIB.message() << '\n';
 });
diff --git a/lld/COFF/Driver.cpp b/lld/COFF/Driver.cpp
index e0afb6b18805b2e..941e6851c1bd4c5 100644
--- a/lld/COFF/Driver.cpp
+++ b/lld/COFF/Driver.cpp
@@ -1447,6 +1447,7 @@ void LinkerDriver::linkerMain(ArrayRef 
argsArr) {
   // Parse command line options.
   ArgParser parser(ctx);
   opt::InputArgList args = parser.parse(argsArr);
+  ctx.storeCmdArgs(args);
 
   // Initialize time trace profiler.
   config->timeTraceEnabled = args.hasArg(OPT_time_trace_eq);
diff --git a/lld/COFF/LTO.cpp b/lld/COFF/LTO.cpp
index 7df931911213672..26f5d42e847def9 100644
--- a/lld/COFF/LTO.cpp
+++ b/lld/COFF/LTO.cpp
@@ -52,8 +52,7 @@ lto::Config BitcodeCompiler::createConfig() {
   lto::Config c;
   c.Options = initTargetOptionsFromCodeGenFlags();
   c.Options.EmitAddrsig = true;
-  for (StringRef C : ctx.config.mllvmOpts)
-c.MllvmArgs.emplace_back(C.str());
+  c.EmbedCmdArgs = context().cmdArgs;
 
   // Always emit a section per function/datum with LTO. LLVM LTO should get 
most
   // of the benefit of linker GC, but there are still opportunities for ICF.
diff --git a/lld/Common/CommonLinkerContext.cpp 
b/lld/Common/CommonLinkerContext.cpp
index 12f56bc10ec9631..57aae8fd0a703d8 100644
--- a/lld/Common/CommonLinkerContext.cpp
+++ b/lld/Common/CommonLinkerContext.cpp
@@ -37,6 +37,15 @@ CommonLinkerContext::~CommonLinkerContext() {
   lctx = nullptr;
 }
 
+void CommonLinkerContext::storeCmdArgs(const llvm::opt::ArgList &args) {
+  cmdArgs.clear();
+  for (const llvm::opt::Arg *arg : args) {
+StringRef str(args.getArgString(arg->getIndex()));
+cmdArgs.insert(cmdArgs.end(), str.begin(), str.end());
+cmdArgs.push_back('\0');
+  }
+}
+
 CommonLinkerContext &lld::commonContext() {
   assert(lctx);
   return *lctx;
diff --git a/lld/ELF/Driver.cpp b/lld/ELF/Driver.cpp
index f4b7d1c9d5b9736..e760247f5ad9b0c 100644
--- a/lld/ELF/Driver.cpp
+++ b/lld/ELF/Driver.cpp
@@ -573,6 +573,7 @@ constexpr const char *saveTempsValues[] = {
 void LinkerDriver::linkerMain(ArrayRef argsArr) {
   ELFOptTable parser;
   opt::InputArgList args = parser.parse(argsArr.slice(1));
+  context().storeCmdArgs(args);
 
   // Interpret these flags early because error()/warn() depend on them.
   errorHandler().errorLimit = args::getInteger(args, OPT_error_limit, 20);
diff --git a/lld/ELF/LTO.cpp b/lld/ELF/LTO.cpp
index c39c6e6ea74ba33..02882865727be89 100644
--- a/lld/ELF/LTO.cpp
+++ b/lld/ELF/LTO.cpp
@@ -54,8 +54,7 @@ static lto::Config createConfig() {
   // LLD supports the new relocations and address-significance tables.
   c.Options = initTargetOptionsFromCodeGenFlags();
   c.Options.EmitAddrsig = true;
-  for (StringRef C : 

[lld] [llvm] [clang] Embed the command line arguments during LTO (PR #79390)

2024-01-25 Thread Duncan Ogilvie via cfe-commits


@@ -40,6 +41,7 @@ using namespace llvm;
 namespace lld::wasm {
 static std::unique_ptr createLTO() {
   lto::Config c;
+  c.EmbedCmdArgs = commonContext().cmdArgs;

mrexodia wrote:

I changed all the `commonContext()` to `context()` (or the `ctx` variable 
directly where possible). Feel free to reopen the discussion if you think it 
needs another revision.

https://github.com/llvm/llvm-project/pull/79390
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[lld] [llvm] [clang] Embed the command line arguments during LTO (PR #79390)

2024-01-25 Thread Duncan Ogilvie via cfe-commits

mrexodia wrote:

> I haven't checked closely yet, but it seems like you need to add tests.

Could you provide some guidance on what kind of tests to add and how to 
actually run them locally? First I wanted to get the builtkite job to be green, 
but it seems to be failing on some unrelated test where LLD cannot be found.

https://github.com/llvm/llvm-project/pull/79390
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang-tools-extra] [clangd] support the zig c++ compiler wrapper (PR #100759)

2024-08-09 Thread Duncan Ogilvie via cfe-commits

mrexodia wrote:

@kazutakahirata @kadircet @sam-mccall it looks like you were recently touching 
this file, any chance for a review?

https://github.com/llvm/llvm-project/pull/100759
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [lld] [llvm] Embed the command line arguments during LTO (PR #79390)

2024-08-09 Thread Duncan Ogilvie via cfe-commits

https://github.com/mrexodia updated 
https://github.com/llvm/llvm-project/pull/79390

>From e6737b6e65669160868a85b8a870fe6fd70b94b0 Mon Sep 17 00:00:00 2001
From: Duncan Ogilvie 
Date: Thu, 25 Jan 2024 00:08:49 +0100
Subject: [PATCH] Embed the command line arguments during LTO

---
 clang/lib/CodeGen/BackendUtil.cpp|  3 +-
 lld/COFF/Driver.cpp  |  1 +
 lld/COFF/LTO.cpp |  3 +-
 lld/Common/CommonLinkerContext.cpp   |  9 ++
 lld/ELF/Driver.cpp   |  1 +
 lld/ELF/LTO.cpp  |  3 +-
 lld/MachO/Driver.cpp |  1 +
 lld/MachO/LTO.cpp|  3 +-
 lld/MinGW/Driver.cpp |  1 +
 lld/include/lld/Common/CommonLinkerContext.h |  3 ++
 lld/wasm/Driver.cpp  |  1 +
 lld/wasm/LTO.cpp |  2 ++
 llvm/include/llvm/LTO/Config.h   |  2 +-
 llvm/include/llvm/LTO/LTOBackend.h   |  6 ++--
 llvm/lib/Bitcode/Writer/BitcodeWriter.cpp| 14 ++
 llvm/lib/LTO/LTO.cpp |  3 +-
 llvm/lib/LTO/LTOBackend.cpp  | 29 ++--
 llvm/lib/LTO/LTOCodeGenerator.cpp|  3 +-
 18 files changed, 52 insertions(+), 36 deletions(-)

diff --git a/clang/lib/CodeGen/BackendUtil.cpp 
b/clang/lib/CodeGen/BackendUtil.cpp
index e765bbf637a661..e4f597798b06c8 100644
--- a/clang/lib/CodeGen/BackendUtil.cpp
+++ b/clang/lib/CodeGen/BackendUtil.cpp
@@ -1222,6 +1222,7 @@ static void runThinLTOBackend(
   Conf.CPU = TOpts.CPU;
   Conf.CodeModel = getCodeModel(CGOpts);
   Conf.MAttrs = TOpts.Features;
+  Conf.EmbedCmdArgs = CGOpts.CmdArgs;
   Conf.RelocModel = CGOpts.RelocationModel;
   std::optional OptLevelOrNone =
   CodeGenOpt::getLevel(CGOpts.OptimizationLevel);
@@ -1283,7 +1284,7 @@ static void runThinLTOBackend(
   if (Error E =
   thinBackend(Conf, -1, AddStream, *M, *CombinedIndex, ImportList,
   ModuleToDefinedGVSummaries[M->getModuleIdentifier()],
-  /* ModuleMap */ nullptr, CGOpts.CmdArgs)) {
+  /* ModuleMap */ nullptr)) {
 handleAllErrors(std::move(E), [&](ErrorInfoBase &EIB) {
   errs() << "Error running ThinLTO backend: " << EIB.message() << '\n';
 });
diff --git a/lld/COFF/Driver.cpp b/lld/COFF/Driver.cpp
index 9e28b1c50be504..cc656f5188fea8 100644
--- a/lld/COFF/Driver.cpp
+++ b/lld/COFF/Driver.cpp
@@ -1439,6 +1439,7 @@ void LinkerDriver::linkerMain(ArrayRef 
argsArr) {
   // Parse command line options.
   ArgParser parser(ctx);
   opt::InputArgList args = parser.parse(argsArr);
+  ctx.storeCmdArgs(args);
 
   // Initialize time trace profiler.
   config->timeTraceEnabled = args.hasArg(OPT_time_trace_eq);
diff --git a/lld/COFF/LTO.cpp b/lld/COFF/LTO.cpp
index 5c881bc01c663d..a0b1af33eb8f00 100644
--- a/lld/COFF/LTO.cpp
+++ b/lld/COFF/LTO.cpp
@@ -52,8 +52,7 @@ lto::Config BitcodeCompiler::createConfig() {
   lto::Config c;
   c.Options = initTargetOptionsFromCodeGenFlags();
   c.Options.EmitAddrsig = true;
-  for (StringRef C : ctx.config.mllvmOpts)
-c.MllvmArgs.emplace_back(C.str());
+  c.EmbedCmdArgs = context().cmdArgs;
 
   // Always emit a section per function/datum with LTO. LLVM LTO should get 
most
   // of the benefit of linker GC, but there are still opportunities for ICF.
diff --git a/lld/Common/CommonLinkerContext.cpp 
b/lld/Common/CommonLinkerContext.cpp
index 12f56bc10ec963..57aae8fd0a703d 100644
--- a/lld/Common/CommonLinkerContext.cpp
+++ b/lld/Common/CommonLinkerContext.cpp
@@ -37,6 +37,15 @@ CommonLinkerContext::~CommonLinkerContext() {
   lctx = nullptr;
 }
 
+void CommonLinkerContext::storeCmdArgs(const llvm::opt::ArgList &args) {
+  cmdArgs.clear();
+  for (const llvm::opt::Arg *arg : args) {
+StringRef str(args.getArgString(arg->getIndex()));
+cmdArgs.insert(cmdArgs.end(), str.begin(), str.end());
+cmdArgs.push_back('\0');
+  }
+}
+
 CommonLinkerContext &lld::commonContext() {
   assert(lctx);
   return *lctx;
diff --git a/lld/ELF/Driver.cpp b/lld/ELF/Driver.cpp
index 8aa2380ba3a177..9623a35549537e 100644
--- a/lld/ELF/Driver.cpp
+++ b/lld/ELF/Driver.cpp
@@ -613,6 +613,7 @@ constexpr const char *saveTempsValues[] = {
 void LinkerDriver::linkerMain(ArrayRef argsArr) {
   ELFOptTable parser;
   opt::InputArgList args = parser.parse(argsArr.slice(1));
+  context().storeCmdArgs(args);
 
   // Interpret these flags early because error()/warn() depend on them.
   errorHandler().errorLimit = args::getInteger(args, OPT_error_limit, 20);
diff --git a/lld/ELF/LTO.cpp b/lld/ELF/LTO.cpp
index 935d0a9eab9ee0..7d453cd187b9b4 100644
--- a/lld/ELF/LTO.cpp
+++ b/lld/ELF/LTO.cpp
@@ -54,8 +54,7 @@ static lto::Config createConfig() {
   // LLD supports the new relocations and address-significance tables.
   c.Options = initTargetOptionsFromCodeGenFlags();
   c.Options.EmitAddrsig = true;
-  for (StringRef C : config->mllv

[clang-tools-extra] [clangd] support the zig c++ compiler wrapper (PR #100759)

2024-07-26 Thread Duncan Ogilvie via cfe-commits

https://github.com/mrexodia created 
https://github.com/llvm/llvm-project/pull/100759

When using `zig c++` for cross-compiling `clangd` removes the zig command from 
the command line. Because of this the system include extraction fails. This 
change detects that the driver executable is named `zig` and adds `cc` or `c++` 
back into the command line.

I don't think there is infrastructure to test this (since it would involve 
executing the `zig` executable), so I did not add any tests.

>From cc96b54798cb83de488076599e5e9df4ac64b94f Mon Sep 17 00:00:00 2001
From: Duncan Ogilvie 
Date: Fri, 26 Jul 2024 17:05:45 +0200
Subject: [PATCH] [clangd] support the zig c++ compiler wrapper

---
 clang-tools-extra/clangd/SystemIncludeExtractor.cpp | 12 +++-
 1 file changed, 11 insertions(+), 1 deletion(-)

diff --git a/clang-tools-extra/clangd/SystemIncludeExtractor.cpp 
b/clang-tools-extra/clangd/SystemIncludeExtractor.cpp
index d4b9b173d149d..c01dbea9c0c12 100644
--- a/clang-tools-extra/clangd/SystemIncludeExtractor.cpp
+++ b/clang-tools-extra/clangd/SystemIncludeExtractor.cpp
@@ -389,7 +389,17 @@ extractSystemIncludesAndTarget(const DriverArgs &InputArgs,
 return std::nullopt;
   }
 
-  llvm::SmallVector Args = {Driver, "-E", "-v"};
+  llvm::SmallVector Args = {Driver};
+  // Support the zig compiler wrapper
+  auto DriverExecutable = llvm::sys::path::stem(Driver);
+  if (DriverExecutable == "zig") {
+if (InputArgs.Lang == "c") {
+  Args.push_back("cc");
+} else {
+  Args.push_back("c++");
+}
+  }
+  Args.append({"-E", "-v"});
   Args.append(InputArgs.render());
   // Input needs to go after Lang flags.
   Args.push_back("-");

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang-tools-extra] [clangd] support the zig c++ compiler wrapper (PR #100759)

2024-07-26 Thread Duncan Ogilvie via cfe-commits

https://github.com/mrexodia edited 
https://github.com/llvm/llvm-project/pull/100759
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang-tools-extra] [clangd] support the zig c++ compiler wrapper (PR #100759)

2024-07-26 Thread Duncan Ogilvie via cfe-commits

https://github.com/mrexodia edited 
https://github.com/llvm/llvm-project/pull/100759
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang-tools-extra] [clangd] support the zig c++ compiler wrapper (PR #100759)

2024-07-29 Thread Duncan Ogilvie via cfe-commits

https://github.com/mrexodia edited 
https://github.com/llvm/llvm-project/pull/100759
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang-tools-extra] [clangd] support the zig c++ compiler wrapper (PR #100759)

2024-07-29 Thread Duncan Ogilvie via cfe-commits

https://github.com/mrexodia edited 
https://github.com/llvm/llvm-project/pull/100759
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang-tools-extra] [clangd] support the zig c++ compiler wrapper (PR #100759)

2024-10-03 Thread Duncan Ogilvie via cfe-commits

mrexodia wrote:

Ping!

https://github.com/llvm/llvm-project/pull/100759
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang-tools-extra] [clangd] support the zig c++ compiler wrapper (PR #100759)

2024-12-03 Thread Duncan Ogilvie via cfe-commits

https://github.com/mrexodia closed 
https://github.com/llvm/llvm-project/pull/100759
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang-tools-extra] [clangd] support the zig c++ compiler wrapper (PR #100759)

2024-12-03 Thread Duncan Ogilvie via cfe-commits

mrexodia wrote:

Worked around this by creating a wrapper script `zig-c++`: 
https://github.com/mrexodia/zig-cross/commit/56b384c96675660554a5d87fe5e5a95bf0bfbe7a

https://github.com/llvm/llvm-project/pull/100759
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits