Skip to content

Commit 65a9165

Browse files
committed
SILFunction: add getMemoryBehavior
This retrieves the side effect information from the function effects.
1 parent e2c2f20 commit 65a9165

File tree

4 files changed

+35
-2
lines changed

4 files changed

+35
-2
lines changed

SwiftCompilerSources/Sources/SIL/Function.swift

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -301,6 +301,11 @@ final public class Function : CustomStringConvertible, HasShortDescription, Hash
301301
}
302302
}
303303
return BridgedEffectInfo(argumentIndex: -1, isDerived: false, isEmpty: true, isValid: false)
304+
},
305+
// getMemBehaviorFn
306+
{ (f: BridgedFunction, observeRetains: Bool) -> BridgedMemoryBehavior in
307+
let e = f.function.getSideEffects()
308+
return e.getMemBehavior(observeRetains: observeRetains)
304309
}
305310
)
306311
}
@@ -331,3 +336,17 @@ extension BridgedFunction {
331336
extension OptionalBridgedFunction {
332337
public var function: Function? { obj.getAs(Function.self) }
333338
}
339+
340+
public extension SideEffects.GlobalEffects {
341+
func getMemBehavior(observeRetains: Bool) -> BridgedMemoryBehavior {
342+
if allocates || ownership.destroy || (ownership.copy && observeRetains) {
343+
return MayHaveSideEffectsBehavior
344+
}
345+
switch (memory.read, memory.write) {
346+
case (false, false): return NoneBehavior
347+
case (true, false): return MayReadBehavior
348+
case (false, true): return MayWriteBehavior
349+
case (true, true): return MayReadWriteBehavior
350+
}
351+
}
352+
}

include/swift/SIL/SILBridging.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -244,12 +244,14 @@ typedef BridgedParsingError (*_Nonnull FunctionParseFn)(BridgedFunction,
244244
typedef SwiftInt (* _Nonnull FunctionCopyEffectsFn)(BridgedFunction,
245245
BridgedFunction);
246246
typedef BridgedEffectInfo (* _Nonnull FunctionGetEffectInfoFn)(BridgedFunction, SwiftInt);
247+
typedef BridgedMemoryBehavior (* _Nonnull FunctionGetMemBehviorFn)(BridgedFunction, bool);
247248

248249
void Function_register(SwiftMetatype metatype,
249250
FunctionRegisterFn initFn, FunctionRegisterFn destroyFn,
250251
FunctionWriteFn writeFn, FunctionParseFn parseFn,
251252
FunctionCopyEffectsFn copyEffectsFn,
252-
FunctionGetEffectInfoFn effectInfoFn);
253+
FunctionGetEffectInfoFn effectInfoFn,
254+
FunctionGetMemBehviorFn memBehaviorFn);
253255

254256
SwiftInt PassContext_continueWithNextSubpassRun(BridgedPassContext passContext,
255257
OptionalBridgedInstruction inst);

include/swift/SIL/SILFunction.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1055,6 +1055,7 @@ class SILFunction
10551055
void copyEffects(SILFunction *from);
10561056
bool hasArgumentEffects() const;
10571057
void visitArgEffects(std::function<void(int, int, bool)> c) const;
1058+
SILInstruction::MemoryBehavior getMemoryBehavior(bool observeRetains);
10581059

10591060
Purpose getSpecialPurpose() const { return specialPurpose; }
10601061

lib/SIL/IR/SILFunction.cpp

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,7 @@ static FunctionWriteFn writeFunction = nullptr;
146146
static FunctionParseFn parseFunction = nullptr;
147147
static FunctionCopyEffectsFn copyEffectsFunction = nullptr;
148148
static FunctionGetEffectInfoFn getEffectInfoFunction = nullptr;
149+
static FunctionGetMemBehviorFn getMemBehvaiorFunction = nullptr;
149150

150151
SILFunction::SILFunction(
151152
SILModule &Module, SILLinkage Linkage, StringRef Name,
@@ -932,14 +933,16 @@ void Function_register(SwiftMetatype metatype,
932933
FunctionRegisterFn initFn, FunctionRegisterFn destroyFn,
933934
FunctionWriteFn writeFn, FunctionParseFn parseFn,
934935
FunctionCopyEffectsFn copyEffectsFn,
935-
FunctionGetEffectInfoFn effectInfoFn) {
936+
FunctionGetEffectInfoFn effectInfoFn,
937+
FunctionGetMemBehviorFn memBehaviorFn) {
936938
functionMetatype = metatype;
937939
initFunction = initFn;
938940
destroyFunction = destroyFn;
939941
writeFunction = writeFn;
940942
parseFunction = parseFn;
941943
copyEffectsFunction = copyEffectsFn;
942944
getEffectInfoFunction = effectInfoFn;
945+
getMemBehvaiorFunction = memBehaviorFn;
943946
}
944947

945948
std::pair<const char *, int> SILFunction::
@@ -1020,6 +1023,14 @@ visitArgEffects(std::function<void(int, int, bool)> c) const {
10201023
}
10211024
}
10221025

1026+
SILInstruction::MemoryBehavior SILFunction::getMemoryBehavior(bool observeRetains) {
1027+
if (!getMemBehvaiorFunction)
1028+
return SILInstruction::MemoryBehavior::MayHaveSideEffects;
1029+
1030+
auto b = getMemBehvaiorFunction({this}, observeRetains);
1031+
return (SILInstruction::MemoryBehavior)b;
1032+
}
1033+
10231034
SILFunction *SILFunction::getFunction(SILDeclRef ref, SILModule &M) {
10241035
swift::Lowering::SILGenModule SILGenModule(M, ref.getModuleContext());
10251036
return SILGenModule.getFunction(ref, swift::NotForDefinition);

0 commit comments

Comments
 (0)