Skip to content

Commit 71d53b4

Browse files
committed
AliasAnalysis: add complexity budget for the getMemEffectsFunction
1 parent f42cd3b commit 71d53b4

File tree

4 files changed

+19
-16
lines changed

4 files changed

+19
-16
lines changed

SwiftCompilerSources/Sources/Optimizer/Analysis/AliasAnalysis.swift

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ struct AliasAnalysis {
6363
static func register() {
6464
BridgedAliasAnalysis.registerAnalysis(
6565
// getMemEffectsFn
66-
{ (bridgedCtxt: BridgedPassContext, bridgedVal: BridgedValue, bridgedInst: BridgedInstruction) -> swift.MemoryBehavior in
66+
{ (bridgedCtxt: BridgedPassContext, bridgedVal: BridgedValue, bridgedInst: BridgedInstruction, complexityBudget: Int) -> swift.MemoryBehavior in
6767
let context = FunctionPassContext(_bridged: bridgedCtxt)
6868
let inst = bridgedInst.instruction
6969
let val = bridgedVal.value
@@ -77,7 +77,8 @@ struct AliasAnalysis {
7777
case (true, true): return .MayReadWrite
7878
}
7979
}
80-
if val.at(path).isEscaping(using: EscapesToInstructionVisitor(target: inst, isAddress: true), context) {
80+
if val.at(path).isEscaping(using: EscapesToInstructionVisitor(target: inst, isAddress: true),
81+
complexityBudget: complexityBudget, context) {
8182
return .MayReadWrite
8283
}
8384
return .None

include/swift/SILOptimizer/Analysis/AliasAnalysis.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,8 @@ class AliasAnalysis {
206206
/// Returns true if the address(es of) `addr` can escape to `toInst`.
207207
MemoryBehavior getMemoryBehaviorOfInst(SILValue addr, SILInstruction *toInst);
208208

209+
int getComplexityBudget(SILValue valueInFunction);
210+
209211
/// Returns true if the object(s of) `obj` can escape to `toInst`.
210212
bool isObjectReleasedByInst(SILValue obj, SILInstruction *toInst);
211213

include/swift/SILOptimizer/OptimizerBridging.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ struct BridgedAliasAnalysis {
3232
}
3333

3434
typedef swift::MemoryBehavior (* _Nonnull GetMemEffectFn)(
35-
BridgedPassContext context, BridgedValue, BridgedInstruction);
35+
BridgedPassContext context, BridgedValue, BridgedInstruction, SwiftInt);
3636
typedef bool (* _Nonnull Escaping2InstFn)(
3737
BridgedPassContext context, BridgedValue, BridgedInstruction);
3838
typedef bool (* _Nonnull Escaping2ValFn)(

lib/SILOptimizer/Analysis/AliasAnalysis.cpp

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -715,7 +715,8 @@ MemoryBehavior AliasAnalysis::getMemoryBehaviorOfInst(
715715
SILValue addr, SILInstruction *toInst) {
716716
if (getMemEffectsFunction) {
717717
return (MemoryBehavior)getMemEffectsFunction({PM->getSwiftPassInvocation()}, {addr},
718-
{toInst->asSILNode()});
718+
{toInst->asSILNode()},
719+
getComplexityBudget(addr));
719720
}
720721
return MemoryBehavior::MayHaveSideEffects;
721722
}
@@ -729,16 +730,8 @@ bool AliasAnalysis::isObjectReleasedByInst(SILValue obj, SILInstruction *inst) {
729730

730731
bool AliasAnalysis::isAddrVisibleFromObject(SILValue addr, SILValue obj) {
731732
if (isAddrVisibleFromObjFunction) {
732-
// This function is called a lot from ARCSequenceOpt and ReleaseHoisting.
733-
// To avoid quadratic complexity for large functions, we limit the amount
734-
// of work what the EscapeUtils are allowed to to.
735-
// This keeps the complexity linear.
736-
//
737-
// This arbitrary limit is good enough for almost all functions. It lets
738-
// the EscapeUtils do several hundred up/down walks which is much more than
739-
// needed in most cases.
740-
SwiftInt complexityLimit = 1000000 / getEstimatedFunctionSize(addr);
741-
return isAddrVisibleFromObjFunction({PM->getSwiftPassInvocation()}, {addr}, {obj}, complexityLimit) != 0;
733+
return isAddrVisibleFromObjFunction({PM->getSwiftPassInvocation()}, {addr}, {obj},
734+
getComplexityBudget(addr)) != 0;
742735
}
743736
return true;
744737
}
@@ -750,7 +743,14 @@ bool AliasAnalysis::canReferenceSameField(SILValue lhs, SILValue rhs) {
750743
return true;
751744
}
752745

753-
int AliasAnalysis::getEstimatedFunctionSize(SILValue valueInFunction) {
746+
// To avoid quadratic complexity for large functions, we limit the amount
747+
// of work what the EscapeUtils are allowed to to.
748+
// This keeps the complexity linear.
749+
//
750+
// This arbitrary limit is good enough for almost all functions. It lets
751+
// the EscapeUtils do several hundred up/down walks which is much more than
752+
// needed in most cases.
753+
int AliasAnalysis::getComplexityBudget(SILValue valueInFunction) {
754754
if (estimatedFunctionSize < 0) {
755755
int numInsts = 0;
756756
SILFunction *f = valueInFunction->getFunction();
@@ -759,5 +759,5 @@ int AliasAnalysis::getEstimatedFunctionSize(SILValue valueInFunction) {
759759
}
760760
estimatedFunctionSize = numInsts;
761761
}
762-
return estimatedFunctionSize;
762+
return 1000000 / estimatedFunctionSize;
763763
}

0 commit comments

Comments
 (0)