|
23 | 23 | #include "SILCombiner.h" |
24 | 24 | #include "swift/SIL/BasicBlockDatastructures.h" |
25 | 25 | #include "swift/SIL/DebugUtils.h" |
| 26 | +#include "swift/SIL/SILBridgingUtils.h" |
26 | 27 | #include "swift/SIL/SILBuilder.h" |
27 | 28 | #include "swift/SIL/SILVisitor.h" |
28 | 29 | #include "swift/SILOptimizer/Analysis/AliasAnalysis.h" |
29 | 30 | #include "swift/SILOptimizer/Analysis/DominanceAnalysis.h" |
30 | 31 | #include "swift/SILOptimizer/Analysis/NonLocalAccessBlockAnalysis.h" |
31 | 32 | #include "swift/SILOptimizer/Analysis/SimplifyInstruction.h" |
32 | | -#include "swift/SILOptimizer/PassManager/Passes.h" |
| 33 | +#include "swift/SILOptimizer/PassManager/PassManager.h" |
33 | 34 | #include "swift/SILOptimizer/PassManager/Transforms.h" |
34 | 35 | #include "swift/SILOptimizer/Utils/CanonicalOSSALifetime.h" |
35 | 36 | #include "swift/SILOptimizer/Utils/CanonicalizeInstruction.h" |
@@ -378,6 +379,56 @@ void SILCombiner::eraseInstIncludingUsers(SILInstruction *inst) { |
378 | 379 | eraseInstFromFunction(*inst); |
379 | 380 | } |
380 | 381 |
|
| 382 | +/// Runs an instruction pass in libswift. |
| 383 | +void SILCombiner::runSwiftInstructionPass(SILInstruction *inst, |
| 384 | + void (*runFunction)(BridgedInstructionPassCtxt)) { |
| 385 | + Worklist.setLibswiftPassInvocation(&libswiftPassInvocation); |
| 386 | + runFunction({ {inst->asSILNode()}, {&libswiftPassInvocation} }); |
| 387 | + Worklist.setLibswiftPassInvocation(nullptr); |
| 388 | + libswiftPassInvocation.finishedPassRun(); |
| 389 | +} |
| 390 | + |
| 391 | +/// Registered briged instruction pass run functions. |
| 392 | +static llvm::StringMap<BridgedInstructionPassRunFn> libswiftInstPasses; |
| 393 | +static bool passesRegistered = false; |
| 394 | + |
| 395 | +// Called from libswift's initializeLibSwift(). |
| 396 | +void SILCombine_registerInstructionPass(BridgedStringRef name, |
| 397 | + BridgedInstructionPassRunFn runFn) { |
| 398 | + libswiftInstPasses[getStringRef(name)] = runFn; |
| 399 | + passesRegistered = true; |
| 400 | +} |
| 401 | + |
| 402 | +#define SWIFT_INSTRUCTION_PASS_COMMON(INST, TAG, LEGACY_RUN) \ |
| 403 | +SILInstruction *SILCombiner::visit##INST(INST *inst) { \ |
| 404 | + static BridgedInstructionPassRunFn runFunction = nullptr; \ |
| 405 | + static bool runFunctionSet = false; \ |
| 406 | + if (!runFunctionSet) { \ |
| 407 | + runFunction = libswiftInstPasses[TAG]; \ |
| 408 | + if (!runFunction && passesRegistered) { \ |
| 409 | + llvm::errs() << "Swift pass " << TAG << " is not registered\n"; \ |
| 410 | + abort(); \ |
| 411 | + } \ |
| 412 | + runFunctionSet = true; \ |
| 413 | + } \ |
| 414 | + if (!runFunction) { \ |
| 415 | + LEGACY_RUN; \ |
| 416 | + } \ |
| 417 | + runSwiftInstructionPass(inst, runFunction); \ |
| 418 | + return nullptr; \ |
| 419 | +} \ |
| 420 | + |
| 421 | +#define PASS(ID, TAG, DESCRIPTION) |
| 422 | + |
| 423 | +#define SWIFT_INSTRUCTION_PASS(INST, TAG) \ |
| 424 | + SWIFT_INSTRUCTION_PASS_COMMON(INST, TAG, { return nullptr; }) |
| 425 | + |
| 426 | +#define SWIFT_INSTRUCTION_PASS_WITH_LEGACY(INST, TAG) \ |
| 427 | + SWIFT_INSTRUCTION_PASS_COMMON(INST, TAG, { return legacyVisit##INST(inst); }) |
| 428 | + |
| 429 | +#include "swift/SILOptimizer/PassManager/Passes.def" |
| 430 | + |
| 431 | +#undef SWIFT_INSTRUCTION_PASS_COMMON |
381 | 432 |
|
382 | 433 | //===----------------------------------------------------------------------===// |
383 | 434 | // Entry Points |
@@ -419,3 +470,15 @@ class SILCombine : public SILFunctionTransform { |
419 | 470 | SILTransform *swift::createSILCombine() { |
420 | 471 | return new SILCombine(); |
421 | 472 | } |
| 473 | + |
| 474 | +//===----------------------------------------------------------------------===// |
| 475 | +// SwiftFunctionPassContext |
| 476 | +//===----------------------------------------------------------------------===// |
| 477 | + |
| 478 | +void LibswiftPassInvocation::eraseInstruction(SILInstruction *inst) { |
| 479 | + if (silCombiner) { |
| 480 | + silCombiner->eraseInstFromFunction(*inst); |
| 481 | + } else { |
| 482 | + inst->eraseFromParent(); |
| 483 | + } |
| 484 | +} |
0 commit comments