|
68 | 68 | #include "llvm/MC/TargetRegistry.h"
|
69 | 69 | #include "llvm/Object/ObjectFile.h"
|
70 | 70 | #include "llvm/Passes/PassBuilder.h"
|
| 71 | +#include "llvm/Passes/PassPlugin.h" |
71 | 72 | #include "llvm/Passes/StandardInstrumentations.h"
|
72 | 73 | #include "llvm/Support/CommandLine.h"
|
73 | 74 | #include "llvm/Support/Debug.h"
|
@@ -194,7 +195,23 @@ static void align(llvm::Module *Module) {
|
194 | 195 | }
|
195 | 196 | }
|
196 | 197 |
|
| 198 | +template <typename... ArgTypes> |
| 199 | +void diagnoseSync( |
| 200 | + DiagnosticEngine &Diags, llvm::sys::Mutex *DiagMutex, SourceLoc Loc, |
| 201 | + Diag<ArgTypes...> ID, |
| 202 | + typename swift::detail::PassArgument<ArgTypes>::type... Args) { |
| 203 | + if (DiagMutex) |
| 204 | + DiagMutex->lock(); |
| 205 | + |
| 206 | + Diags.diagnose(Loc, ID, std::move(Args)...); |
| 207 | + |
| 208 | + if (DiagMutex) |
| 209 | + DiagMutex->unlock(); |
| 210 | +} |
| 211 | + |
197 | 212 | void swift::performLLVMOptimizations(const IRGenOptions &Opts,
|
| 213 | + DiagnosticEngine &Diags, |
| 214 | + llvm::sys::Mutex *DiagMutex, |
198 | 215 | llvm::Module *Module,
|
199 | 216 | llvm::TargetMachine *TargetMachine,
|
200 | 217 | llvm::raw_pwrite_stream *out) {
|
@@ -236,6 +253,18 @@ void swift::performLLVMOptimizations(const IRGenOptions &Opts,
|
236 | 253 |
|
237 | 254 | PassBuilder PB(TargetMachine, PTO, PGOOpt, &PIC);
|
238 | 255 |
|
| 256 | + // Attempt to load pass plugins and register their callbacks with PB. |
| 257 | + for (const auto &PluginFile : Opts.LLVMPassPlugins) { |
| 258 | + Expected<PassPlugin> PassPlugin = PassPlugin::Load(PluginFile); |
| 259 | + if (PassPlugin) { |
| 260 | + PassPlugin->registerPassBuilderCallbacks(PB); |
| 261 | + } else { |
| 262 | + diagnoseSync(Diags, DiagMutex, SourceLoc(), |
| 263 | + diag::unable_to_load_pass_plugin, PluginFile, |
| 264 | + toString(PassPlugin.takeError())); |
| 265 | + } |
| 266 | + } |
| 267 | + |
239 | 268 | // Register the AA manager first so that our version is the one used.
|
240 | 269 | FAM.registerPass([&] {
|
241 | 270 | auto AA = PB.buildDefaultAAPipeline();
|
@@ -509,20 +538,6 @@ static void countStatsPostIRGen(UnifiedStatsReporter &Stats,
|
509 | 538 | }
|
510 | 539 | }
|
511 | 540 |
|
512 |
| -template<typename ...ArgTypes> |
513 |
| -void |
514 |
| -diagnoseSync(DiagnosticEngine &Diags, llvm::sys::Mutex *DiagMutex, |
515 |
| - SourceLoc Loc, Diag<ArgTypes...> ID, |
516 |
| - typename swift::detail::PassArgument<ArgTypes>::type... Args) { |
517 |
| - if (DiagMutex) |
518 |
| - DiagMutex->lock(); |
519 |
| - |
520 |
| - Diags.diagnose(Loc, ID, std::move(Args)...); |
521 |
| - |
522 |
| - if (DiagMutex) |
523 |
| - DiagMutex->unlock(); |
524 |
| -} |
525 |
| - |
526 | 541 | /// Run the LLVM passes. In multi-threaded compilation this will be done for
|
527 | 542 | /// multiple LLVM modules in parallel.
|
528 | 543 | bool swift::performLLVM(const IRGenOptions &Opts,
|
@@ -591,7 +606,7 @@ bool swift::performLLVM(const IRGenOptions &Opts,
|
591 | 606 | assert(Opts.OutputKind == IRGenOutputKind::Module && "no output specified");
|
592 | 607 | }
|
593 | 608 |
|
594 |
| - performLLVMOptimizations(Opts, Module, TargetMachine, |
| 609 | + performLLVMOptimizations(Opts, Diags, DiagMutex, Module, TargetMachine, |
595 | 610 | OutputFile ? &OutputFile->getOS() : nullptr);
|
596 | 611 |
|
597 | 612 | if (Stats) {
|
@@ -1700,7 +1715,7 @@ GeneratedModule OptimizedIRRequest::evaluate(Evaluator &evaluator,
|
1700 | 1715 | if (!irMod)
|
1701 | 1716 | return irMod;
|
1702 | 1717 |
|
1703 |
| - performLLVMOptimizations(desc.Opts, irMod.getModule(), |
| 1718 | + performLLVMOptimizations(desc.Opts, ctx.Diags, nullptr, irMod.getModule(), |
1704 | 1719 | irMod.getTargetMachine(), desc.out);
|
1705 | 1720 | return irMod;
|
1706 | 1721 | }
|
|
0 commit comments