Skip to content

Commit 0f6f429

Browse files
authored
Merge pull request #34252 from eeckstein/temp-rvalue-opt
SILOptimizer: fix a miscompile in TempRValueOpt and improve MemBehavior
2 parents 87f7097 + 68f4854 commit 0f6f429

15 files changed

+482
-216
lines changed

include/swift/SILOptimizer/Analysis/AliasAnalysis.h

Lines changed: 9 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,6 @@ namespace {
4747
struct MemBehaviorKeyTy {
4848
// The SILValue pair:
4949
size_t V1, V2;
50-
RetainObserveKind InspectionMode;
5150
};
5251
}
5352

@@ -201,24 +200,16 @@ class AliasAnalysis : public SILAnalysis {
201200

202201
/// Use the alias analysis to determine the memory behavior of Inst with
203202
/// respect to V.
204-
///
205-
/// TODO: When ref count behavior is separated from generic memory behavior,
206-
/// the InspectionMode flag will be unnecessary.
207-
MemoryBehavior computeMemoryBehavior(SILInstruction *Inst, SILValue V,
208-
RetainObserveKind);
203+
MemoryBehavior computeMemoryBehavior(SILInstruction *Inst, SILValue V);
209204

210205
/// Use the alias analysis to determine the memory behavior of Inst with
211206
/// respect to V.
212-
///
213-
/// TODO: When ref count behavior is separated from generic memory behavior,
214-
/// the InspectionMode flag will be unnecessary.
215-
MemoryBehavior computeMemoryBehaviorInner(SILInstruction *Inst, SILValue V,
216-
RetainObserveKind);
207+
MemoryBehavior computeMemoryBehaviorInner(SILInstruction *Inst, SILValue V);
217208

218209
/// Returns true if \p Inst may read from memory in a manner that
219210
/// affects V.
220211
bool mayReadFromMemory(SILInstruction *Inst, SILValue V) {
221-
auto B = computeMemoryBehavior(Inst, V, RetainObserveKind::IgnoreRetains);
212+
auto B = computeMemoryBehavior(Inst, V);
222213
return B == MemoryBehavior::MayRead ||
223214
B == MemoryBehavior::MayReadWrite ||
224215
B == MemoryBehavior::MayHaveSideEffects;
@@ -227,7 +218,7 @@ class AliasAnalysis : public SILAnalysis {
227218
/// Returns true if \p Inst may write to memory in a manner that
228219
/// affects V.
229220
bool mayWriteToMemory(SILInstruction *Inst, SILValue V) {
230-
auto B = computeMemoryBehavior(Inst, V, RetainObserveKind::IgnoreRetains);
221+
auto B = computeMemoryBehavior(Inst, V);
231222
return B == MemoryBehavior::MayWrite ||
232223
B == MemoryBehavior::MayReadWrite ||
233224
B == MemoryBehavior::MayHaveSideEffects;
@@ -236,26 +227,10 @@ class AliasAnalysis : public SILAnalysis {
236227
/// Returns true if \p Inst may read or write to memory in a manner that
237228
/// affects V.
238229
bool mayReadOrWriteMemory(SILInstruction *Inst, SILValue V) {
239-
auto B = computeMemoryBehavior(Inst, V, RetainObserveKind::IgnoreRetains);
230+
auto B = computeMemoryBehavior(Inst, V);
240231
return MemoryBehavior::None != B;
241232
}
242233

243-
/// Returns true if Inst may have side effects in a manner that affects V.
244-
bool mayHaveSideEffects(SILInstruction *Inst, SILValue V) {
245-
auto B = computeMemoryBehavior(Inst, V, RetainObserveKind::ObserveRetains);
246-
return B == MemoryBehavior::MayWrite ||
247-
B == MemoryBehavior::MayReadWrite ||
248-
B == MemoryBehavior::MayHaveSideEffects;
249-
}
250-
251-
/// Returns true if Inst may have side effects in a manner that affects
252-
/// V. This is independent of whether or not Inst may write to V and is meant
253-
/// to encode notions such as ref count modifications.
254-
bool mayHavePureSideEffects(SILInstruction *Inst, SILValue V) {
255-
auto B = computeMemoryBehavior(Inst, V, RetainObserveKind::ObserveRetains);
256-
return MemoryBehavior::MayHaveSideEffects == B;
257-
}
258-
259234
/// Returns true if \p Ptr may be released in the function call \p FAS.
260235
bool canApplyDecrementRefCount(FullApplySite FAS, SILValue Ptr);
261236

@@ -268,8 +243,7 @@ class AliasAnalysis : public SILAnalysis {
268243
AliasKeyTy toAliasKey(SILValue V1, SILValue V2, SILType Type1, SILType Type2);
269244

270245
/// Encodes the memory behavior query as a MemBehaviorKeyTy.
271-
MemBehaviorKeyTy toMemoryBehaviorKey(SILInstruction *V1, SILValue V2,
272-
RetainObserveKind K);
246+
MemBehaviorKeyTy toMemoryBehaviorKey(SILInstruction *V1, SILValue V2);
273247

274248
virtual void invalidate() override {
275249
AliasCache.clear();
@@ -330,24 +304,21 @@ namespace llvm {
330304
template <> struct DenseMapInfo<MemBehaviorKeyTy> {
331305
static inline MemBehaviorKeyTy getEmptyKey() {
332306
auto Allone = std::numeric_limits<size_t>::max();
333-
return {0, Allone, RetainObserveKind::RetainObserveKindEnd};
307+
return {0, Allone};
334308
}
335309
static inline MemBehaviorKeyTy getTombstoneKey() {
336310
auto Allone = std::numeric_limits<size_t>::max();
337-
return {Allone, 0, RetainObserveKind::RetainObserveKindEnd};
311+
return {Allone, 0};
338312
}
339313
static unsigned getHashValue(const MemBehaviorKeyTy V) {
340314
unsigned H = 0;
341315
H ^= DenseMapInfo<size_t>::getHashValue(V.V1);
342316
H ^= DenseMapInfo<size_t>::getHashValue(V.V2);
343-
H ^= DenseMapInfo<int>::getHashValue(static_cast<int>(V.InspectionMode));
344317
return H;
345318
}
346319
static bool isEqual(const MemBehaviorKeyTy LHS,
347320
const MemBehaviorKeyTy RHS) {
348-
return LHS.V1 == RHS.V1 &&
349-
LHS.V2 == RHS.V2 &&
350-
LHS.InspectionMode == RHS.InspectionMode;
321+
return LHS.V1 == RHS.V1 && LHS.V2 == RHS.V2;
351322
}
352323
};
353324
}

include/swift/SILOptimizer/Analysis/SideEffectAnalysis.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -389,6 +389,13 @@ class FunctionSideEffects {
389389
/// instructions are considered as side effects.
390390
MemoryBehavior getMemBehavior(RetainObserveKind ScanKind) const;
391391

392+
/// Gets the memory behavior for an argument.
393+
///
394+
/// This is derived from the combined argument and the global effects.
395+
/// Also the argument type and convention are considered.
396+
MemoryBehavior getArgumentBehavior(FullApplySite applySite,
397+
unsigned argIdx);
398+
392399
/// Get the global effects for the function. These are effects which cannot
393400
/// be associated to a specific parameter, e.g. writes to global variables
394401
/// or writes to unknown pointers.

0 commit comments

Comments
 (0)