Skip to content

Commit fe4c345

Browse files
committed
[ownership] Make OwnershipFixupContext a dump context struct and instead put its RAUW functionality on OwnershipRAUWHelper.
The reason why I am doing this is that I am building up more utilities based on passing around this struct of context that do not want it for RAUWing purposes. So it makes sense on a helper (OwnershipRAUWHelper) that composes with its state. Just a refactor, should be NFC.
1 parent aa38be6 commit fe4c345

File tree

4 files changed

+34
-13
lines changed

4 files changed

+34
-13
lines changed

include/swift/SILOptimizer/Utils/OwnershipOptUtils.h

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
#ifndef SWIFT_SILOPTIMIZER_UTILS_OWNERSHIPOPTUTILS_H
2020
#define SWIFT_SILOPTIMIZER_UTILS_OWNERSHIPOPTUTILS_H
2121

22+
#include "swift/SIL/BasicBlockUtils.h"
2223
#include "swift/SIL/OwnershipUtils.h"
2324
#include "swift/SIL/SILModule.h"
2425
#include "swift/SILOptimizer/Utils/InstOptUtils.h"
@@ -28,6 +29,9 @@ namespace swift {
2829
// Defined in BasicBlockUtils.h
2930
struct JointPostDominanceSetComputer;
3031

32+
/// A struct that contains context shared in between different operation +
33+
/// "ownership fixup" utilities. Please do not put actual methods on this, it is
34+
/// meant to be composed with.
3135
struct OwnershipFixupContext {
3236
Optional<InstModCallbacks> inlineCallbacks;
3337
InstModCallbacks &callbacks;
@@ -44,6 +48,20 @@ struct OwnershipFixupContext {
4448
: inlineCallbacks(InstModCallbacks()), callbacks(*inlineCallbacks),
4549
deBlocks(deBlocks), jointPostDomSetComputer(inputJPDComputer) {}
4650

51+
void clear() {
52+
jointPostDomSetComputer.clear();
53+
}
54+
};
55+
56+
/// A utility composed ontop of OwnershipFixupContext that knows how to RAUW a
57+
/// value or a single value instruction with a new value and then fixup
58+
/// ownership invariants afterwards.
59+
class OwnershipRAUWHelper {
60+
OwnershipFixupContext &ctx;
61+
62+
public:
63+
OwnershipRAUWHelper(OwnershipFixupContext &ctx) : ctx(ctx) {}
64+
4765
SILBasicBlock::iterator
4866
replaceAllUsesAndErase(SingleValueInstruction *oldValue, SILValue newValue);
4967

lib/SILOptimizer/Analysis/SimplifyInstruction.cpp

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -755,7 +755,8 @@ swift::replaceAllSimplifiedUsesAndErase(SILInstruction *i, SILValue result,
755755
if (svi->getFunction()->hasOwnership()) {
756756
JointPostDominanceSetComputer computer(*deadEndBlocks);
757757
OwnershipFixupContext ctx{callbacks, *deadEndBlocks, computer};
758-
return ctx.replaceAllUsesAndErase(svi, result);
758+
OwnershipRAUWHelper helper(ctx);
759+
return helper.replaceAllUsesAndErase(svi, result);
759760
}
760761
return replaceAllUsesAndErase(svi, result, callbacks);
761762
}
@@ -790,7 +791,7 @@ static SILValue simplifyInstruction(SILInstruction *i) {
790791
// this code is not updated at this point in time.
791792
auto *svi = cast<SingleValueInstruction>(i);
792793
if (svi->getFunction()->hasOwnership())
793-
if (!OwnershipFixupContext::canFixUpOwnershipForRAUW(svi, result))
794+
if (!OwnershipRAUWHelper::canFixUpOwnershipForRAUW(svi, result))
794795
return SILValue();
795796

796797
return result;
@@ -813,7 +814,8 @@ SILBasicBlock::iterator swift::simplifyAndReplaceAllSimplifiedUsesAndErase(
813814
if (svi->getFunction()->hasOwnership()) {
814815
JointPostDominanceSetComputer computer(*deadEndBlocks);
815816
OwnershipFixupContext ctx{callbacks, *deadEndBlocks, computer};
816-
return ctx.replaceAllUsesAndErase(svi, result);
817+
OwnershipRAUWHelper helper(ctx);
818+
return helper.replaceAllUsesAndErase(svi, result);
817819
}
818820
return replaceAllUsesAndErase(svi, result, callbacks);
819821
}

lib/SILOptimizer/Transforms/CSE.cpp

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -583,17 +583,17 @@ class CSE {
583583

584584
DeadEndBlocks &DeadEndBBs;
585585

586-
OwnershipFixupContext &FixupCtx;
586+
OwnershipRAUWHelper &RAUWHelper;
587587

588588
/// The set of calls to lazy property getters which can be replace by a direct
589589
/// load of the property value.
590590
llvm::SmallVector<ApplyInst *, 8> lazyPropertyGetters;
591591

592592
CSE(bool RunsOnHighLevelSil, SideEffectAnalysis *SEA,
593593
SILOptFunctionBuilder &FuncBuilder, DeadEndBlocks &DeadEndBBs,
594-
OwnershipFixupContext &FixupCtx)
594+
OwnershipRAUWHelper &RAUWHelper)
595595
: SEA(SEA), FuncBuilder(FuncBuilder), DeadEndBBs(DeadEndBBs),
596-
FixupCtx(FixupCtx), RunsOnHighLevelSil(RunsOnHighLevelSil) {}
596+
RAUWHelper(RAUWHelper), RunsOnHighLevelSil(RunsOnHighLevelSil) {}
597597

598598
bool processFunction(SILFunction &F, DominanceInfo *DT);
599599

@@ -1017,12 +1017,12 @@ bool CSE::processNode(DominanceInfoNode *Node) {
10171017
// extend it here as well
10181018
if (!isa<SingleValueInstruction>(Inst))
10191019
continue;
1020-
if (!OwnershipFixupContext::canFixUpOwnershipForRAUW(
1020+
if (!OwnershipRAUWHelper::canFixUpOwnershipForRAUW(
10211021
cast<SingleValueInstruction>(Inst),
10221022
cast<SingleValueInstruction>(AvailInst)))
10231023
continue;
10241024
// Replace SingleValueInstruction using OSSA RAUW here
1025-
nextI = FixupCtx.replaceAllUsesAndErase(
1025+
nextI = RAUWHelper.replaceAllUsesAndErase(
10261026
cast<SingleValueInstruction>(Inst),
10271027
cast<SingleValueInstruction>(AvailInst));
10281028
Changed = true;
@@ -1399,7 +1399,8 @@ class SILCSE : public SILFunctionTransform {
13991399
JointPostDominanceSetComputer Computer(DeadEndBBs);
14001400
InstModCallbacks callbacks;
14011401
OwnershipFixupContext FixupCtx{callbacks, DeadEndBBs, Computer};
1402-
CSE C(RunsOnHighLevelSil, SEA, FuncBuilder, DeadEndBBs, FixupCtx);
1402+
OwnershipRAUWHelper RAUWHelper(FixupCtx);
1403+
CSE C(RunsOnHighLevelSil, SEA, FuncBuilder, DeadEndBBs, RAUWHelper);
14031404
bool Changed = false;
14041405

14051406
// Perform the traditional CSE.

lib/SILOptimizer/Utils/OwnershipOptUtils.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -644,7 +644,7 @@ SILBasicBlock::iterator OwnershipRAUWUtility::handleGuaranteed() {
644644
SILBasicBlock::iterator OwnershipRAUWUtility::perform() {
645645
assert(oldValue->getFunction()->hasOwnership());
646646
assert(
647-
OwnershipFixupContext::canFixUpOwnershipForRAUW(oldValue, newValue) &&
647+
OwnershipRAUWHelper::canFixUpOwnershipForRAUW(oldValue, newValue) &&
648648
"Should have checked if can perform this operation before calling it?!");
649649
// If our new value is just none, we can pass anything to do it so just RAUW
650650
// and return.
@@ -689,7 +689,7 @@ SILBasicBlock::iterator OwnershipRAUWUtility::perform() {
689689

690690
// All callers of our RAUW routines must ensure that their values return true
691691
// from this.
692-
bool OwnershipFixupContext::canFixUpOwnershipForRAUW(SILValue oldValue,
692+
bool OwnershipRAUWHelper::canFixUpOwnershipForRAUW(SILValue oldValue,
693693
SILValue newValue) {
694694
auto newOwnershipKind = newValue.getOwnershipKind();
695695

@@ -741,8 +741,8 @@ bool OwnershipFixupContext::canFixUpOwnershipForRAUW(SILValue oldValue,
741741
}
742742

743743
SILBasicBlock::iterator
744-
OwnershipFixupContext::replaceAllUsesAndErase(SingleValueInstruction *oldValue,
744+
OwnershipRAUWHelper::replaceAllUsesAndErase(SingleValueInstruction *oldValue,
745745
SILValue newValue) {
746-
OwnershipRAUWUtility utility{oldValue, newValue, *this};
746+
OwnershipRAUWUtility utility{oldValue, newValue, ctx};
747747
return utility.perform();
748748
}

0 commit comments

Comments
 (0)