From ad0ecebf432fcb80cb666034ea44f75b81e55f95 Mon Sep 17 00:00:00 2001 From: Augie Fackler Date: Mon, 16 Sep 2024 16:36:35 -0400 Subject: [PATCH 1/2] rustc_llvm: adapt to flattened CLI args in LLVM This changed in llvm/llvm-project@e190d074a0a77c9f8a7d7938a8187a7e2076e290. I decided to stick with more duplication between the ifdef blocks to make the code easier to read for the next two years before we can plausibly drop LLVM 19. @rustbot label: +llvm-main --- .../rustc_llvm/llvm-wrapper/PassWrapper.cpp | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/compiler/rustc_llvm/llvm-wrapper/PassWrapper.cpp b/compiler/rustc_llvm/llvm-wrapper/PassWrapper.cpp index da27db29c87..88bbb5da11e 100644 --- a/compiler/rustc_llvm/llvm-wrapper/PassWrapper.cpp +++ b/compiler/rustc_llvm/llvm-wrapper/PassWrapper.cpp @@ -498,6 +498,21 @@ extern "C" LLVMTargetMachineRef LLVMRustCreateTargetMachine( Options.EmitStackSizeSection = EmitStackSizeSection; if (ArgsCstrBuff != nullptr) { +#if LLVM_VERSION_GE(20, 0) + int buffer_offset = 0; + assert(ArgsCstrBuff[ArgsCstrBuffLen - 1] == '\0'); + auto Arg0 = std::string(ArgsCstrBuff); + buffer_offset = Arg0.size() + 1; + auto ArgsCppStr = std::string(ArgsCstrBuff + buffer_offset, ArgsCstrBuffLen - 1); + auto i = 0; + while (i != std::string::npos) { + i = ArgsCppStr.find('\0', i + 1); + if (i != std::string::npos) + ArgsCppStr.replace(i, i + 1, " "); + } + Options.MCOptions.Argv0 = Arg0; + Options.MCOptions.CommandlineArgs = ArgsCppStr; +#else int buffer_offset = 0; assert(ArgsCstrBuff[ArgsCstrBuffLen - 1] == '\0'); @@ -523,6 +538,7 @@ extern "C" LLVMTargetMachineRef LLVMRustCreateTargetMachine( Options.MCOptions.Argv0 = arg0; Options.MCOptions.CommandLineArgs = llvm::ArrayRef(cmd_arg_strings, num_cmd_arg_strings); +#endif } TargetMachine *TM = TheTarget->createTargetMachine( @@ -531,10 +547,11 @@ extern "C" LLVMTargetMachineRef LLVMRustCreateTargetMachine( } extern "C" void LLVMRustDisposeTargetMachine(LLVMTargetMachineRef TM) { - +#if LLVM_VERSION_LT(20, 0) MCTargetOptions &MCOptions = unwrap(TM)->Options.MCOptions; delete[] MCOptions.Argv0; delete[] MCOptions.CommandLineArgs.data(); +#endif delete unwrap(TM); } From 86d67b79338d5df0d34e94aed03a9d8788709839 Mon Sep 17 00:00:00 2001 From: Augie Fackler Date: Mon, 16 Sep 2024 20:06:50 -0400 Subject: [PATCH 2/2] PassWrapper: clang-format has spoken --- compiler/rustc_llvm/llvm-wrapper/PassWrapper.cpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/compiler/rustc_llvm/llvm-wrapper/PassWrapper.cpp b/compiler/rustc_llvm/llvm-wrapper/PassWrapper.cpp index 88bbb5da11e..32b23c3fbad 100644 --- a/compiler/rustc_llvm/llvm-wrapper/PassWrapper.cpp +++ b/compiler/rustc_llvm/llvm-wrapper/PassWrapper.cpp @@ -503,7 +503,8 @@ extern "C" LLVMTargetMachineRef LLVMRustCreateTargetMachine( assert(ArgsCstrBuff[ArgsCstrBuffLen - 1] == '\0'); auto Arg0 = std::string(ArgsCstrBuff); buffer_offset = Arg0.size() + 1; - auto ArgsCppStr = std::string(ArgsCstrBuff + buffer_offset, ArgsCstrBuffLen - 1); + auto ArgsCppStr = + std::string(ArgsCstrBuff + buffer_offset, ArgsCstrBuffLen - 1); auto i = 0; while (i != std::string::npos) { i = ArgsCppStr.find('\0', i + 1);