Skip to content

Commit eb76d9a

Browse files
committed
Merge remote-tracking branch 'origin/main' into next
2 parents bb5c03e + 2d76ec4 commit eb76d9a

File tree

8 files changed

+93
-128
lines changed

8 files changed

+93
-128
lines changed

SwiftCompilerSources/Sources/Optimizer/PassManager/PassRegistration.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@ private func registerSwiftPasses() {
9292
registerPass(addressEscapeInfoDumper, { addressEscapeInfoDumper.run($0) })
9393
registerPass(accessDumper, { accessDumper.run($0) })
9494
registerPass(deadEndBlockDumper, { deadEndBlockDumper.run($0) })
95+
registerPass(memBehaviorDumper, { memBehaviorDumper.run($0) })
9596
registerPass(rangeDumper, { rangeDumper.run($0) })
9697
registerPass(runUnitTests, { runUnitTests.run($0) })
9798
registerPass(testInstructionIteration, { testInstructionIteration.run($0) })

SwiftCompilerSources/Sources/Optimizer/TestPasses/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ swift_compiler_sources(Optimizer
1111
AccessDumper.swift
1212
DeadEndBlockDumper.swift
1313
EscapeInfoDumper.swift
14+
MemBehaviorDumper.swift
1415
SILPrinter.swift
1516
RangeDumper.swift
1617
RunUnitTests.swift
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
//===--- MemBehaviorDumper.swift ------------------------------------------===//
2+
//
3+
// This source file is part of the Swift.org open source project
4+
//
5+
// Copyright (c) 2014 - 2023 Apple Inc. and the Swift project authors
6+
// Licensed under Apache License v2.0 with Runtime Library Exception
7+
//
8+
// See https://swift.org/LICENSE.txt for license information
9+
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
10+
//
11+
//===----------------------------------------------------------------------===//
12+
13+
import SIL
14+
15+
/// Prints the memory behavior of relevant instructions in relation to address values of the function.
16+
let memBehaviorDumper = FunctionPass(name: "dump-mem-behavior") {
17+
(function: Function, context: FunctionPassContext) in
18+
19+
let aliasAnalysis = context.aliasAnalysis
20+
21+
print("@\(function.name)")
22+
23+
let values = function.allValues
24+
25+
var currentPair = 0
26+
for inst in function.instructions where inst.shouldTest {
27+
28+
for value in values where value.definingInstruction != inst {
29+
30+
if value.type.isAddress || value is AddressToPointerInst {
31+
let read = aliasAnalysis.mayRead(inst, fromAddress: value)
32+
let write = aliasAnalysis.mayWrite(inst, toAddress: value)
33+
print("PAIR #\(currentPair).")
34+
print(" \(inst)")
35+
print(" \(value)")
36+
print(" r=\(read ? 1 : 0),w=\(write ? 1 : 0)")
37+
currentPair += 1
38+
}
39+
}
40+
}
41+
print()
42+
}
43+
44+
private extension Function {
45+
var allValues: [Value] {
46+
var values: [Value] = []
47+
for block in blocks {
48+
values.append(contentsOf: block.arguments.map { $0 })
49+
for inst in block.instructions {
50+
values.append(contentsOf: inst.results)
51+
}
52+
}
53+
return values
54+
}
55+
}
56+
57+
private extension Instruction {
58+
var shouldTest: Bool {
59+
switch self {
60+
case is ApplyInst,
61+
is TryApplyInst,
62+
is EndApplyInst,
63+
is BeginApplyInst,
64+
is AbortApplyInst,
65+
is BeginAccessInst,
66+
is EndAccessInst,
67+
is EndCOWMutationInst,
68+
is CopyValueInst,
69+
is DestroyValueInst,
70+
is EndBorrowInst,
71+
is LoadInst,
72+
is StoreInst,
73+
is CopyAddrInst,
74+
is BuiltinInst:
75+
return true
76+
default:
77+
return false
78+
}
79+
}
80+
}

include/swift/SILOptimizer/PassManager/Passes.def

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -301,7 +301,7 @@ PASS(MandatoryInlining, "mandatory-inlining",
301301
"Mandatory Inlining of Transparent Functions")
302302
PASS(Mem2Reg, "mem2reg",
303303
"Memory to SSA Value Conversion to Remove Stack Allocation")
304-
PASS(MemBehaviorDumper, "mem-behavior-dump",
304+
SWIFT_FUNCTION_PASS(MemBehaviorDumper, "dump-mem-behavior",
305305
"Print SIL Instruction MemBehavior from Alias Analysis over all Pairs")
306306
PASS(LSLocationPrinter, "lslocation-dump",
307307
"Print Load-Store Location Results Covering all Accesses")

lib/SILOptimizer/UtilityPasses/CMakeLists.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ target_sources(swiftSILOptimizer PRIVATE
2323
LoopCanonicalizer.cpp
2424
LoopInfoPrinter.cpp
2525
LoopRegionPrinter.cpp
26-
MemBehaviorDumper.cpp
2726
ModulePrinter.cpp
2827
RCIdentityDumper.cpp
2928
SerializeSILPass.cpp

lib/SILOptimizer/UtilityPasses/MemBehaviorDumper.cpp

Lines changed: 0 additions & 121 deletions
This file was deleted.

stdlib/public/runtime/HeapObject.cpp

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -815,11 +815,16 @@ void swift::swift_deallocClassInstance(HeapObject *object,
815815
size_t allocatedSize,
816816
size_t allocatedAlignMask) {
817817
size_t retainCount = swift_retainCount(object);
818-
if (SWIFT_UNLIKELY(retainCount > 1))
818+
if (SWIFT_UNLIKELY(retainCount > 1)) {
819+
auto descriptor = object->metadata->getTypeContextDescriptor();
820+
819821
swift::fatalError(0,
820-
"Object %p deallocated with retain count %zd, reference "
821-
"may have escaped from deinit.\n",
822-
object, retainCount);
822+
"Object %p of class %s deallocated with retain count %zd, "
823+
"reference may have escaped from deinit.\n",
824+
object,
825+
descriptor ? descriptor->Name.get() : "<unknown>",
826+
retainCount);
827+
}
823828

824829
#if SWIFT_OBJC_INTEROP
825830
// We need to let the ObjC runtime clean up any associated objects or weak

test/SILOptimizer/mem-behavior.sil

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// RUN: %target-sil-opt %s -aa-kind=basic-aa -mem-behavior-dump -o /dev/null | %FileCheck %s
1+
// RUN: %target-sil-opt %s -aa-kind=basic-aa -dump-mem-behavior -o /dev/null | %FileCheck %s
22

33
// REQUIRES: asserts
44
// REQUIRES: swift_in_compiler

0 commit comments

Comments
 (0)