|
67 | 67 | #include "llvm/MC/TargetRegistry.h"
|
68 | 68 | #include "llvm/Object/ObjectFile.h"
|
69 | 69 | #include "llvm/Passes/PassBuilder.h"
|
| 70 | +#include "llvm/Passes/PassPlugin.h" |
70 | 71 | #include "llvm/Passes/StandardInstrumentations.h"
|
71 | 72 | #include "llvm/Support/CommandLine.h"
|
72 | 73 | #include "llvm/Support/Debug.h"
|
@@ -188,7 +189,23 @@ static void align(llvm::Module *Module) {
|
188 | 189 | }
|
189 | 190 | }
|
190 | 191 |
|
| 192 | +template <typename... ArgTypes> |
| 193 | +void diagnoseSync( |
| 194 | + DiagnosticEngine &Diags, llvm::sys::Mutex *DiagMutex, SourceLoc Loc, |
| 195 | + Diag<ArgTypes...> ID, |
| 196 | + typename swift::detail::PassArgument<ArgTypes>::type... Args) { |
| 197 | + if (DiagMutex) |
| 198 | + DiagMutex->lock(); |
| 199 | + |
| 200 | + Diags.diagnose(Loc, ID, std::move(Args)...); |
| 201 | + |
| 202 | + if (DiagMutex) |
| 203 | + DiagMutex->unlock(); |
| 204 | +} |
| 205 | + |
191 | 206 | void swift::performLLVMOptimizations(const IRGenOptions &Opts,
|
| 207 | + DiagnosticEngine &Diags, |
| 208 | + llvm::sys::Mutex *DiagMutex, |
192 | 209 | llvm::Module *Module,
|
193 | 210 | llvm::TargetMachine *TargetMachine,
|
194 | 211 | llvm::raw_pwrite_stream *out) {
|
@@ -238,6 +255,18 @@ void swift::performLLVMOptimizations(const IRGenOptions &Opts,
|
238 | 255 |
|
239 | 256 | PassBuilder PB(TargetMachine, PTO, PGOOpt, &PIC);
|
240 | 257 |
|
| 258 | + // Attempt to load pass plugins and register their callbacks with PB. |
| 259 | + for (const auto &PluginFile : Opts.LLVMPassPlugins) { |
| 260 | + Expected<PassPlugin> PassPlugin = PassPlugin::Load(PluginFile); |
| 261 | + if (PassPlugin) { |
| 262 | + PassPlugin->registerPassBuilderCallbacks(PB); |
| 263 | + } else { |
| 264 | + diagnoseSync(Diags, DiagMutex, SourceLoc(), |
| 265 | + diag::unable_to_load_pass_plugin, PluginFile, |
| 266 | + toString(PassPlugin.takeError())); |
| 267 | + } |
| 268 | + } |
| 269 | + |
241 | 270 | // Register the AA manager first so that our version is the one used.
|
242 | 271 | FAM.registerPass([&] {
|
243 | 272 | auto AA = PB.buildDefaultAAPipeline();
|
@@ -512,20 +541,6 @@ static void countStatsPostIRGen(UnifiedStatsReporter &Stats,
|
512 | 541 | }
|
513 | 542 | }
|
514 | 543 |
|
515 |
| -template<typename ...ArgTypes> |
516 |
| -void |
517 |
| -diagnoseSync(DiagnosticEngine &Diags, llvm::sys::Mutex *DiagMutex, |
518 |
| - SourceLoc Loc, Diag<ArgTypes...> ID, |
519 |
| - typename swift::detail::PassArgument<ArgTypes>::type... Args) { |
520 |
| - if (DiagMutex) |
521 |
| - DiagMutex->lock(); |
522 |
| - |
523 |
| - Diags.diagnose(Loc, ID, std::move(Args)...); |
524 |
| - |
525 |
| - if (DiagMutex) |
526 |
| - DiagMutex->unlock(); |
527 |
| -} |
528 |
| - |
529 | 544 | /// Run the LLVM passes. In multi-threaded compilation this will be done for
|
530 | 545 | /// multiple LLVM modules in parallel.
|
531 | 546 | bool swift::performLLVM(const IRGenOptions &Opts,
|
@@ -594,7 +609,7 @@ bool swift::performLLVM(const IRGenOptions &Opts,
|
594 | 609 | assert(Opts.OutputKind == IRGenOutputKind::Module && "no output specified");
|
595 | 610 | }
|
596 | 611 |
|
597 |
| - performLLVMOptimizations(Opts, Module, TargetMachine, |
| 612 | + performLLVMOptimizations(Opts, Diags, DiagMutex, Module, TargetMachine, |
598 | 613 | OutputFile ? &OutputFile->getOS() : nullptr);
|
599 | 614 |
|
600 | 615 | if (Stats) {
|
@@ -1683,7 +1698,7 @@ GeneratedModule OptimizedIRRequest::evaluate(Evaluator &evaluator,
|
1683 | 1698 | if (!irMod)
|
1684 | 1699 | return irMod;
|
1685 | 1700 |
|
1686 |
| - performLLVMOptimizations(desc.Opts, irMod.getModule(), |
| 1701 | + performLLVMOptimizations(desc.Opts, ctx.Diags, nullptr, irMod.getModule(), |
1687 | 1702 | irMod.getTargetMachine(), desc.out);
|
1688 | 1703 | return irMod;
|
1689 | 1704 | }
|
|
0 commit comments