Skip to content

Commit 5e1b9fc

Browse files
authored
Merge pull request #67188 from atrick/membehavior-api
Cleanup and document SIL memory behavior APIs.
2 parents 4760b95 + 5bae855 commit 5e1b9fc

File tree

4 files changed

+59
-23
lines changed

4 files changed

+59
-23
lines changed

include/swift/SIL/SILNodes.def

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -137,8 +137,13 @@
137137
/// NAME is the name of the instruction in SIL assembly.
138138
/// The argument will be a bare identifier, not a string literal.
139139
///
140-
/// MEMBEHAVIOR is an enum value that reflects the memory behavior of
141-
/// the instruction.
140+
/// MEMBEHAVIOR is an enum value that reflects the memory behavior of the
141+
/// instruction. It is only used in the implementation of
142+
/// SILInstruction::getMemoryBehavior(). Memory behavior is relative, so
143+
/// whenever possible, clients should instead use
144+
/// AliasAnalysis::computeMemoryBehavior(SILInstruction, SILValue) which only
145+
/// defaults to SILInstruction::getMemoryBehavior() when AliasAnalysis cannot
146+
/// disambiguate the instruction's effects from the value of interest.
142147
///
143148
/// MAYRELEASE indicates whether the execution of the
144149
/// instruction may result in memory being released.

include/swift/SILOptimizer/Analysis/AliasAnalysis.h

Lines changed: 41 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,10 @@ namespace swift {
2222

2323
/// This class is a simple wrapper around an alias analysis cache. This is
2424
/// needed since we do not have an "analysis" infrastructure.
25+
///
26+
/// This wrapper sits above the SwiftCompilerSource implementation of
27+
/// AliasAnalysis. The implementation calls into AliasAnalysis.swift via
28+
/// BridgedAliasAnalysis whenever the result may depend on escape analysis.
2529
class AliasAnalysis {
2630
public:
2731

@@ -159,14 +163,24 @@ class AliasAnalysis {
159163
return alias(V1, V2, TBAAType1, TBAAType2) == AliasResult::MayAlias;
160164
}
161165

162-
/// Use the alias analysis to determine the memory behavior of Inst with
163-
/// respect to V.
166+
/// Compute the effects of Inst's memory behavior on the memory pointed to by
167+
/// the value V.
168+
///
169+
/// This is the top-level API for memory behavior.
170+
///
171+
/// 1. MemoryBehaviorVisitor overrides select instruction types. Types that
172+
/// have no override default to SILInstruction::getMemoryBehavior(), which is
173+
/// not specific to the memory pointed to by V.
174+
///
175+
/// 2. For instruction types overridden by MemoryBehaviorVisitor, this uses
176+
/// alias analysis to disambiguate the Inst's memory effects from the memory
177+
/// pointed to by value V. 'mayAlias' is used for memory operations and
178+
/// 'getMemoryEffectOnEscapedAddress' is used for calls and releases.
179+
///
180+
/// 3. For calls, alias analysis uses callee analysis to retrieve function
181+
/// side effects which provides the memory behavior of each argument.
164182
MemoryBehavior computeMemoryBehavior(SILInstruction *Inst, SILValue V);
165183

166-
/// Use the alias analysis to determine the memory behavior of Inst with
167-
/// respect to V.
168-
MemoryBehavior computeMemoryBehaviorInner(SILInstruction *Inst, SILValue V);
169-
170184
/// Returns true if \p Inst may read from memory at address \p V.
171185
///
172186
/// For details see MemoryBehavior::MayRead.
@@ -203,20 +217,37 @@ class AliasAnalysis {
203217
/// Returns true if \p Ptr may be released by the builtin \p BI.
204218
bool canBuiltinDecrementRefCount(BuiltinInst *BI, SILValue Ptr);
205219

206-
/// Returns true if the address(es of) `addr` can escape to `toInst`.
207-
MemoryBehavior getMemoryBehaviorOfInst(SILValue addr, SILInstruction *toInst);
220+
int getEstimatedFunctionSize(SILValue valueInFunction);
208221

209222
/// Returns true if the object(s of) `obj` can escape to `toInst`.
223+
///
224+
/// Special entry point into BridgedAliasAnalysis (escape analysis) for use in
225+
/// ARC analysis.
210226
bool isObjectReleasedByInst(SILValue obj, SILInstruction *toInst);
211227

212228
/// Is the `addr` within all reachable objects/addresses, when start walking
213229
/// from `obj`?
230+
///
231+
/// Special entry point into BridgedAliasAnalysis (escape analysis) for use in
232+
/// ARC analysis.
214233
bool isAddrVisibleFromObject(SILValue addr, SILValue obj);
215234

235+
/// MARK: implementation helpers for MemBehaviorVisitor.
236+
237+
/// If the address(es of) `addr` can escape to `toInst` (based on escape
238+
/// analysis), return the memory effect of `toInst` on the escaped memory.
239+
///
240+
/// This should not be called directly; it is an implementation helper for
241+
/// querying escape analysis.
242+
MemoryBehavior getMemoryEffectOnEscapedAddress(SILValue addr, SILInstruction *toInst);
243+
244+
protected:
245+
/// Use the alias analysis to determine the memory behavior of Inst with
246+
/// respect to V.
247+
MemoryBehavior computeMemoryBehaviorInner(SILInstruction *Inst, SILValue V);
248+
216249
/// Returns true if `lhs` can reference the same field as `rhs`.
217250
bool canReferenceSameField(SILValue lhs, SILValue rhs);
218-
219-
int getEstimatedFunctionSize(SILValue valueInFunction);
220251
};
221252

222253

lib/SILOptimizer/Analysis/AliasAnalysis.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -711,7 +711,7 @@ void BridgedAliasAnalysis::registerAnalysis(GetMemEffectFn getMemEffectsFn,
711711
canReferenceSameFieldFunction = canReferenceSameFieldFn;
712712
}
713713

714-
MemoryBehavior AliasAnalysis::getMemoryBehaviorOfInst(
714+
MemoryBehavior AliasAnalysis::getMemoryEffectOnEscapedAddress(
715715
SILValue addr, SILInstruction *toInst) {
716716
if (getMemEffectsFunction) {
717717
return (MemoryBehavior)getMemEffectsFunction({PM->getSwiftPassInvocation()}, {addr},

lib/SILOptimizer/Analysis/MemoryBehavior.cpp

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -317,50 +317,50 @@ MemBehavior MemoryBehaviorVisitor::visitMarkUnresolvedMoveAddrInst(
317317
MemBehavior MemoryBehaviorVisitor::visitBuiltinInst(BuiltinInst *BI) {
318318
MemBehavior mb = BI->getMemoryBehavior();
319319
if (mb != MemBehavior::None) {
320-
return AA->getMemoryBehaviorOfInst(V, BI);
320+
return AA->getMemoryEffectOnEscapedAddress(V, BI);
321321
}
322322
return MemBehavior::None;
323323
}
324324

325325
MemBehavior MemoryBehaviorVisitor::visitTryApplyInst(TryApplyInst *AI) {
326-
return AA->getMemoryBehaviorOfInst(V, AI);
326+
return AA->getMemoryEffectOnEscapedAddress(V, AI);
327327
}
328328

329329
MemBehavior MemoryBehaviorVisitor::visitApplyInst(ApplyInst *AI) {
330-
return AA->getMemoryBehaviorOfInst(V, AI);
330+
return AA->getMemoryEffectOnEscapedAddress(V, AI);
331331
}
332332

333333
MemBehavior MemoryBehaviorVisitor::visitBeginApplyInst(BeginApplyInst *AI) {
334-
return AA->getMemoryBehaviorOfInst(V, AI);
334+
return AA->getMemoryEffectOnEscapedAddress(V, AI);
335335
}
336336

337337
MemBehavior MemoryBehaviorVisitor::visitEndApplyInst(EndApplyInst *EAI) {
338-
return AA->getMemoryBehaviorOfInst(V, EAI->getBeginApply());
338+
return AA->getMemoryEffectOnEscapedAddress(V, EAI->getBeginApply());
339339
}
340340

341341
MemBehavior MemoryBehaviorVisitor::visitAbortApplyInst(AbortApplyInst *AAI) {
342-
return AA->getMemoryBehaviorOfInst(V, AAI->getBeginApply());
342+
return AA->getMemoryEffectOnEscapedAddress(V, AAI->getBeginApply());
343343
}
344344

345345
MemBehavior
346346
MemoryBehaviorVisitor::visitStrongReleaseInst(StrongReleaseInst *SI) {
347-
return AA->getMemoryBehaviorOfInst(V, SI);
347+
return AA->getMemoryEffectOnEscapedAddress(V, SI);
348348
}
349349

350350
#define ALWAYS_OR_SOMETIMES_LOADABLE_CHECKED_REF_STORAGE(Name, ...) \
351351
MemBehavior \
352352
MemoryBehaviorVisitor::visit##Name##ReleaseInst(Name##ReleaseInst *SI) { \
353-
return AA->getMemoryBehaviorOfInst(V, SI); \
353+
return AA->getMemoryEffectOnEscapedAddress(V, SI); \
354354
}
355355
#include "swift/AST/ReferenceStorage.def"
356356

357357
MemBehavior MemoryBehaviorVisitor::visitReleaseValueInst(ReleaseValueInst *SI) {
358-
return AA->getMemoryBehaviorOfInst(V, SI);
358+
return AA->getMemoryEffectOnEscapedAddress(V, SI);
359359
}
360360

361361
MemBehavior
362362
MemoryBehaviorVisitor::visitDestroyValueInst(DestroyValueInst *DVI) {
363-
return AA->getMemoryBehaviorOfInst(V, DVI);
363+
return AA->getMemoryEffectOnEscapedAddress(V, DVI);
364364
}
365365

366366
MemBehavior MemoryBehaviorVisitor::visitSetDeallocatingInst(SetDeallocatingInst *SDI) {

0 commit comments

Comments
 (0)