Skip to content

Commit 698196b

Browse files
committed
SILFunction: add getMemoryBehavior
This retrieves the side effect information from the function effects.
1 parent 143d432 commit 698196b

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
@@ -305,6 +305,11 @@ final public class Function : CustomStringConvertible, HasShortDescription, Hash
305305
}
306306
}
307307
return BridgedEffectInfo(argumentIndex: -1, isDerived: false, isEmpty: true, isValid: false)
308+
},
309+
// getMemBehaviorFn
310+
{ (f: BridgedFunction, observeRetains: Bool) -> BridgedMemoryBehavior in
311+
let e = f.function.getSideEffects()
312+
return e.getMemBehavior(observeRetains: observeRetains)
308313
}
309314
)
310315
}
@@ -335,3 +340,17 @@ extension BridgedFunction {
335340
extension OptionalBridgedFunction {
336341
public var function: Function? { obj.getAs(Function.self) }
337342
}
343+
344+
public extension SideEffects.GlobalEffects {
345+
func getMemBehavior(observeRetains: Bool) -> BridgedMemoryBehavior {
346+
if allocates || ownership.destroy || (ownership.copy && observeRetains) {
347+
return MayHaveSideEffectsBehavior
348+
}
349+
switch (memory.read, memory.write) {
350+
case (false, false): return NoneBehavior
351+
case (true, false): return MayReadBehavior
352+
case (false, true): return MayWriteBehavior
353+
case (true, true): return MayReadWriteBehavior
354+
}
355+
}
356+
}

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)