Skip to content

Commit 29246fd

Browse files
committed
AliasAnalysis: add complexity budget for the getMemEffectsFunction
1 parent 2384a0c commit 29246fd

File tree

4 files changed

+18
-17
lines changed

4 files changed

+18
-17
lines changed

SwiftCompilerSources/Sources/Optimizer/Analysis/AliasAnalysis.swift

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ struct AliasAnalysis {
3636
static func register() {
3737
BridgedAliasAnalysis.registerAnalysis(
3838
// getMemEffectsFn
39-
{ (bridgedCtxt: BridgedPassContext, bridgedVal: BridgedValue, bridgedInst: BridgedInstruction) -> swift.MemoryBehavior in
39+
{ (bridgedCtxt: BridgedPassContext, bridgedVal: BridgedValue, bridgedInst: BridgedInstruction, complexityBudget: Int) -> swift.MemoryBehavior in
4040
let context = FunctionPassContext(_bridged: bridgedCtxt)
4141
let inst = bridgedInst.instruction
4242
let val = bridgedVal.value
@@ -47,7 +47,8 @@ struct AliasAnalysis {
4747
case let builtin as BuiltinInst:
4848
return getMemoryEffect(ofBuiltin: builtin, for: val, path: path, context).bridged
4949
default:
50-
if val.at(path).isEscaping(using: EscapesToInstructionVisitor(target: inst, isAddress: true), context) {
50+
if val.at(path).isEscaping(using: EscapesToInstructionVisitor(target: inst, isAddress: true),
51+
complexityBudget: complexityBudget, context) {
5152
return .MayReadWrite
5253
}
5354
return .None

include/swift/SILOptimizer/Analysis/AliasAnalysis.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -217,7 +217,7 @@ class AliasAnalysis {
217217
/// Returns true if \p Ptr may be released by the builtin \p BI.
218218
bool canBuiltinDecrementRefCount(BuiltinInst *BI, SILValue Ptr);
219219

220-
int getEstimatedFunctionSize(SILValue valueInFunction);
220+
int getComplexityBudget(SILValue valueInFunction);
221221

222222
/// Returns true if the object(s of) `obj` can escape to `toInst`.
223223
///

include/swift/SILOptimizer/OptimizerBridging.h

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

3535
typedef swift::MemoryBehavior (* _Nonnull GetMemEffectFn)(
36-
BridgedPassContext context, BridgedValue, BridgedInstruction);
36+
BridgedPassContext context, BridgedValue, BridgedInstruction, SwiftInt);
3737
typedef bool (* _Nonnull Escaping2InstFn)(
3838
BridgedPassContext context, BridgedValue, BridgedInstruction);
3939
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::getMemoryEffectOnEscapedAddress(
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)