From 8a1ce59601c2b17597227a69e5478560665a8013 Mon Sep 17 00:00:00 2001 From: Meghana Gupta Date: Fri, 6 May 2022 09:35:10 -0700 Subject: [PATCH 1/4] AddressLowering: Don't generate an end_borrow for store_borrow store_borrow returns the address to be borrowed and not a token Don't generate an end_borrow until this is fixed first --- lib/SILOptimizer/Mandatory/AddressLowering.cpp | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/lib/SILOptimizer/Mandatory/AddressLowering.cpp b/lib/SILOptimizer/Mandatory/AddressLowering.cpp index e17672a0b6d37..b0366e888542c 100644 --- a/lib/SILOptimizer/Mandatory/AddressLowering.cpp +++ b/lib/SILOptimizer/Mandatory/AddressLowering.cpp @@ -1784,14 +1784,12 @@ void CallArgRewriter::rewriteIndirectArgument(Operand *operand) { }); } else { auto borrow = argBuilder.emitBeginBorrowOperation(callLoc, argValue); - auto *storeInst = - argBuilder.emitStoreBorrowOperation(callLoc, borrow, allocInst); + argBuilder.emitStoreBorrowOperation(callLoc, borrow, allocInst); apply.insertAfterFullEvaluation([&](SILBuilder &callBuilder) { - if (auto *storeBorrow = dyn_cast(storeInst)) { - callBuilder.emitEndBorrowOperation(callLoc, storeBorrow); + if (borrow != argValue) { + callBuilder.emitEndBorrowOperation(callLoc, borrow); } - callBuilder.emitEndBorrowOperation(callLoc, borrow); callBuilder.createDeallocStack(callLoc, allocInst); }); } From f0e52dbc56d667726d44cbcf87642396c5ffd520 Mon Sep 17 00:00:00 2001 From: Meghana Gupta Date: Thu, 12 May 2022 14:36:39 -0700 Subject: [PATCH 2/4] AddressLowering: Handle opaque use in ValueMetatypeInst --- lib/SILOptimizer/Mandatory/AddressLowering.cpp | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/lib/SILOptimizer/Mandatory/AddressLowering.cpp b/lib/SILOptimizer/Mandatory/AddressLowering.cpp index b0366e888542c..e3571cb709fa9 100644 --- a/lib/SILOptimizer/Mandatory/AddressLowering.cpp +++ b/lib/SILOptimizer/Mandatory/AddressLowering.cpp @@ -2598,6 +2598,12 @@ class UseRewriter : SILInstructionVisitor { yield->setOperand(0, addr); } + void visitValueMetatypeInst(ValueMetatypeInst *vmi) { + auto opAddr = + pass.valueStorageMap.getStorage(vmi->getOperand()).storageAddress; + vmi->setOperand(opAddr); + } + void visitAssignInst(AssignInst *assignInst); void visitBeginBorrowInst(BeginBorrowInst *borrow); From 25186fffbddfe0dd77744376eae7bd8a73bb8d0e Mon Sep 17 00:00:00 2001 From: Meghana Gupta Date: Thu, 12 May 2022 20:17:37 -0700 Subject: [PATCH 3/4] Use AddressMaterialization::materializeAddress instead of directly using storageAddres --- lib/SILOptimizer/Mandatory/AddressLowering.cpp | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/lib/SILOptimizer/Mandatory/AddressLowering.cpp b/lib/SILOptimizer/Mandatory/AddressLowering.cpp index e3571cb709fa9..ea74766f6a01a 100644 --- a/lib/SILOptimizer/Mandatory/AddressLowering.cpp +++ b/lib/SILOptimizer/Mandatory/AddressLowering.cpp @@ -2367,8 +2367,11 @@ class CheckedCastBrRewriter { /// Return the storageAddress if \p value is opaque, otherwise create and /// return a stack temporary. SILValue getAddressForCastEntity(SILValue value, bool needsInit) { - if (value->getType().isAddressOnly(*func)) - return pass.valueStorageMap.getStorage(value).storageAddress; + if (value->getType().isAddressOnly(*func)) { + auto builder = pass.getBuilder(ccb->getIterator()); + AddressMaterialization addrMat(pass, builder); + return addrMat.materializeAddress(value); + } // Create a stack temporary for a loadable value auto *addr = termBuilder.createAllocStack(castLoc, value->getType()); @@ -2593,14 +2596,12 @@ class UseRewriter : SILInstructionVisitor { } void visitYieldInst(YieldInst *yield) { - SILValue addr = - pass.valueStorageMap.getStorage(yield->getOperand(0)).storageAddress; + SILValue addr = addrMat.materializeAddress(use->get()); yield->setOperand(0, addr); } void visitValueMetatypeInst(ValueMetatypeInst *vmi) { - auto opAddr = - pass.valueStorageMap.getStorage(vmi->getOperand()).storageAddress; + SILValue opAddr = addrMat.materializeAddress(use->get()); vmi->setOperand(opAddr); } From fb0be6ad97f684935641d22736ac65d2ececc392 Mon Sep 17 00:00:00 2001 From: Meghana Gupta Date: Tue, 31 May 2022 14:46:35 -0700 Subject: [PATCH 4/4] Run AddressLowering after DI and RawSILInstLowering --- lib/SILOptimizer/Mandatory/AddressLowering.cpp | 9 +-------- lib/SILOptimizer/PassManager/PassPipeline.cpp | 2 +- 2 files changed, 2 insertions(+), 9 deletions(-) diff --git a/lib/SILOptimizer/Mandatory/AddressLowering.cpp b/lib/SILOptimizer/Mandatory/AddressLowering.cpp index ea74766f6a01a..d3af47b371388 100644 --- a/lib/SILOptimizer/Mandatory/AddressLowering.cpp +++ b/lib/SILOptimizer/Mandatory/AddressLowering.cpp @@ -339,7 +339,7 @@ static bool isStoreCopy(SILValue value) { return false; auto *user = value->getSingleUse()->getUser(); - return isa(user) || isa(user); + return isa(user); } void ValueStorageMap::insertValue(SILValue value, SILValue storageAddress) { @@ -2605,8 +2605,6 @@ class UseRewriter : SILInstructionVisitor { vmi->setOperand(opAddr); } - void visitAssignInst(AssignInst *assignInst); - void visitBeginBorrowInst(BeginBorrowInst *borrow); void visitEndBorrowInst(EndBorrowInst *end) {} @@ -2837,11 +2835,6 @@ void UseRewriter::visitStoreInst(StoreInst *storeInst) { rewriteStore(storeInst->getSrc(), storeInst->getDest(), isInit); } -void UseRewriter::visitAssignInst(AssignInst *assignInst) { - rewriteStore(assignInst->getSrc(), assignInst->getDest(), - IsNotInitialization); -} - /// Emit end_borrows for a an incomplete BorrowedValue with only nonlifetime /// ending uses. This function inserts end_borrows on the lifetime boundary. void UseRewriter::emitEndBorrows(SILValue value) { diff --git a/lib/SILOptimizer/PassManager/PassPipeline.cpp b/lib/SILOptimizer/PassManager/PassPipeline.cpp index e0b87f32be099..19dd1ff5b62ba 100644 --- a/lib/SILOptimizer/PassManager/PassPipeline.cpp +++ b/lib/SILOptimizer/PassManager/PassPipeline.cpp @@ -116,7 +116,6 @@ static void addDefiniteInitialization(SILPassPipelinePlan &P) { static void addMandatoryDiagnosticOptPipeline(SILPassPipelinePlan &P) { P.startPipeline("Mandatory Diagnostic Passes + Enabling Optimization Passes"); P.addSILGenCleanup(); - P.addAddressLowering(); P.addDiagnoseInvalidEscapingCaptures(); P.addDiagnoseStaticExclusivity(); P.addNestedSemanticFunctionCheck(); @@ -129,6 +128,7 @@ static void addMandatoryDiagnosticOptPipeline(SILPassPipelinePlan &P) { P.addAllocBoxToStack(); P.addNoReturnFolding(); addDefiniteInitialization(P); + P.addAddressLowering(); P.addFlowIsolation();