Skip to content

Commit 0dfbbb5

Browse files
authored
Merge pull request swiftlang#84621 from gottesmm/pr-95c1206164fa7277e19f427016042d63ae44703b
[sil-llvm-gen] Allow for -Xllvm options to be passed to sil-llvm-gen.
2 parents 5945606 + d0d2163 commit 0dfbbb5

File tree

1 file changed

+41
-5
lines changed

1 file changed

+41
-5
lines changed

lib/DriverTool/sil_llvm_gen_main.cpp

Lines changed: 41 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -219,22 +219,58 @@ static std::optional<bool> toOptionalBool(llvm::cl::boolOrDefault defaultable) {
219219
llvm_unreachable("Bad case for llvm::cl::boolOrDefault!");
220220
}
221221

222+
namespace {
223+
class LLVMOptionFilter {
224+
/// Options with -Xllvm removed. We let through -Xllvm options so that LLVM's
225+
/// processing can find it.
226+
SmallVector<const char *, 32> filteredOptions;
227+
228+
/// Options with the -Xllvm prefix. We have that here so we can process them
229+
/// separately.
230+
SmallVector<const char *, 32> llvmOptions;
231+
232+
public:
233+
LLVMOptionFilter(ArrayRef<const char *> argv) {
234+
for (unsigned i = 0; i < argv.size(); ++i) {
235+
// Not a -Xllvm option... just let it through.
236+
if (StringRef(argv[i]) != "-Xllvm") {
237+
filteredOptions.push_back(argv[i]);
238+
continue;
239+
}
240+
241+
assert(i + 1 < argv.size() && "-Xllvm without a corresponding option");
242+
++i;
243+
filteredOptions.push_back(argv[i]);
244+
llvmOptions.push_back(argv[i]);
245+
}
246+
}
247+
248+
ArrayRef<const char *> getFilteredOptions() const { return filteredOptions; }
249+
ArrayRef<const char *> getLLVMOptions() const { return llvmOptions; }
250+
};
251+
} // namespace
252+
222253
int sil_llvm_gen_main(ArrayRef<const char *> argv, void *MainAddr) {
223254
INITIALIZE_LLVM();
224255

225256
llvm::setBugReportMsg(SWIFT_CRASH_BUG_REPORT_MESSAGE "\n");
226257
llvm::EnablePrettyStackTraceOnSigInfoForThisThread();
227258

228259
SILLLVMGenOptions options;
229-
230-
llvm::cl::ParseCommandLineOptions(argv.size(), argv.data(), "Swift LLVM IR Generator\n");
231-
232-
if (options.PrintStats)
233-
llvm::EnableStatistics();
260+
LLVMOptionFilter filteredOptions(argv);
234261

235262
CompilerInvocation Invocation;
236263

237264
Invocation.setMainExecutablePath(llvm::sys::fs::getMainExecutable(argv[0], MainAddr));
265+
copy(filteredOptions.getLLVMOptions(),
266+
std::back_inserter(Invocation.getFrontendOptions().LLVMArgs));
267+
268+
llvm::cl::ParseCommandLineOptions(filteredOptions.getFilteredOptions().size(),
269+
filteredOptions.getFilteredOptions().data(),
270+
"Swift LLVM IR Generator\n");
271+
272+
if (options.PrintStats)
273+
llvm::EnableStatistics();
238274

239275
// Give the context the list of search paths to use for modules.
240276
std::vector<SearchPathOptions::SearchPath> ImportPaths;

0 commit comments

Comments
 (0)