Skip to content

Commit 510649f

Browse files
committed
SwiftCompilerSources: move WalkUtils and AccessUtils from the Optimizer module to the SIL module
Those utilities are conceptually part of the SIL definition.
1 parent 4071659 commit 510649f

File tree

7 files changed

+101
-88
lines changed

7 files changed

+101
-88
lines changed

SwiftCompilerSources/Sources/Optimizer/Utilities/CMakeLists.txt

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,6 @@ swift_compiler_sources(Optimizer
1212
EscapeUtils.swift
1313
OptUtils.swift
1414
ForwardingUtils.swift
15-
WalkUtils.swift
16-
AccessUtils.swift
1715
SSAUpdater.swift
1816
StaticInitCloner.swift
1917
Test.swift

SwiftCompilerSources/Sources/Optimizer/Utilities/OptUtils.swift

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -322,22 +322,6 @@ extension LoadInst {
322322
}
323323
}
324324

325-
extension SmallProjectionPath {
326-
/// Returns true if the path only contains projections which can be materialized as
327-
/// SIL struct or tuple projection instructions - for values or addresses.
328-
var isMaterializable: Bool {
329-
let (kind, _, subPath) = pop()
330-
switch kind {
331-
case .root:
332-
return true
333-
case .structField, .tupleField:
334-
return subPath.isMaterializable
335-
default:
336-
return false
337-
}
338-
}
339-
}
340-
341325
extension FunctionPassContext {
342326
/// Returns true if any blocks were removed.
343327
func removeDeadBlocks(in function: Function) -> Bool {

SwiftCompilerSources/Sources/SIL/CMakeLists.txt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,12 @@ add_swift_compiler_module(SIL
2222
Location.swift
2323
Operand.swift
2424
Registration.swift
25-
SmallProjectionPath.swift
2625
SubstitutionMap.swift
2726
Type.swift
2827
Utils.swift
2928
Value.swift
3029
VTable.swift
3130
WitnessTable.swift)
31+
32+
add_subdirectory(Utilities)
33+

SwiftCompilerSources/Sources/Optimizer/Utilities/AccessUtils.swift renamed to SwiftCompilerSources/Sources/SIL/Utilities/AccessUtils.swift

Lines changed: 31 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,6 @@
2929
// ```
3030
//===----------------------------------------------------------------------===//
3131

32-
import SIL
33-
3432
/// AccessBase describes the base address of a memory access (e.g. of a `load` or `store``).
3533
/// The "base address" is defined as the address which is obtained from the access address by
3634
/// looking through all address projections.
@@ -48,7 +46,7 @@ import SIL
4846
/// ```
4947
///
5048
/// The base address is never inside an access scope.
51-
enum AccessBase : CustomStringConvertible, Hashable {
49+
public enum AccessBase : CustomStringConvertible, Hashable {
5250

5351
/// The address of a boxed variable, i.e. a field of an `alloc_box`.
5452
case box(ProjectBoxInst)
@@ -78,7 +76,7 @@ enum AccessBase : CustomStringConvertible, Hashable {
7876
/// This should be a very rare situation.
7977
case unidentified
8078

81-
init(baseAddress: Value) {
79+
public init(baseAddress: Value) {
8280
switch baseAddress {
8381
case let rea as RefElementAddrInst : self = .class(rea)
8482
case let rta as RefTailAddrInst : self = .tail(rta)
@@ -97,7 +95,7 @@ enum AccessBase : CustomStringConvertible, Hashable {
9795
}
9896
}
9997

100-
var description: String {
98+
public var description: String {
10199
switch self {
102100
case .unidentified: return "?"
103101
case .box(let pbi): return "box - \(pbi)"
@@ -112,7 +110,7 @@ enum AccessBase : CustomStringConvertible, Hashable {
112110
}
113111

114112
/// True, if this is an access to a class instance.
115-
var isObjectAccess: Bool {
113+
public var isObjectAccess: Bool {
116114
switch self {
117115
case .class, .tail:
118116
return true
@@ -122,7 +120,7 @@ enum AccessBase : CustomStringConvertible, Hashable {
122120
}
123121

124122
/// The reference value if this is an access to a referenced objecct (class, box, tail).
125-
var reference: Value? {
123+
public var reference: Value? {
126124
switch self {
127125
case .box(let pbi): return pbi.box
128126
case .class(let rea): return rea.instance
@@ -141,7 +139,7 @@ enum AccessBase : CustomStringConvertible, Hashable {
141139
///
142140
/// This is not true for scoped storage such as alloc_stack and @in arguments.
143141
///
144-
var hasLocalOwnershipLifetime: Bool {
142+
public var hasLocalOwnershipLifetime: Bool {
145143
if let reference = reference {
146144
// Conservatively assume that everything which is a ref-counted object is within an ownership scope.
147145
// TODO: we could e.g. exclude guaranteed function arguments.
@@ -151,7 +149,7 @@ enum AccessBase : CustomStringConvertible, Hashable {
151149
}
152150

153151
/// True, if the baseAddress is of an immutable property or global variable
154-
var isLet: Bool {
152+
public var isLet: Bool {
155153
switch self {
156154
case .class(let rea): return rea.fieldIsLet
157155
case .global(let g): return g.isLet
@@ -161,7 +159,7 @@ enum AccessBase : CustomStringConvertible, Hashable {
161159
}
162160

163161
/// True, if the address is immediately produced by an allocation in its function.
164-
var isLocal: Bool {
162+
public var isLocal: Bool {
165163
switch self {
166164
case .box(let pbi): return pbi.box is AllocBoxInst
167165
case .class(let rea): return rea.instance is AllocRefInstBase
@@ -173,7 +171,7 @@ enum AccessBase : CustomStringConvertible, Hashable {
173171
}
174172

175173
/// True, if the kind of storage of the access is known (e.g. a class property, or global variable).
176-
var hasKnownStorageKind: Bool {
174+
public var hasKnownStorageKind: Bool {
177175
switch self {
178176
case .box, .class, .tail, .stack, .global:
179177
return true
@@ -187,7 +185,7 @@ enum AccessBase : CustomStringConvertible, Hashable {
187185
/// `isEqual` abstracts away the projection instructions that are included as part of the AccessBase:
188186
/// multiple `project_box` and `ref_element_addr` instructions are equivalent bases as long as they
189187
/// refer to the same variable or class property.
190-
func isEqual(to other: AccessBase) -> Bool {
188+
public func isEqual(to other: AccessBase) -> Bool {
191189
switch (self, other) {
192190
case (.box(let pb1), .box(let pb2)):
193191
return pb1.box.referenceRoot == pb2.box.referenceRoot
@@ -213,8 +211,8 @@ enum AccessBase : CustomStringConvertible, Hashable {
213211
}
214212

215213
/// Returns `true` if the two access bases do not alias.
216-
func isDistinct(from other: AccessBase) -> Bool {
217-
214+
public func isDistinct(from other: AccessBase) -> Bool {
215+
218216
func isDifferentAllocation(_ lhs: Value, _ rhs: Value) -> Bool {
219217
switch (lhs, rhs) {
220218
case (is Allocation, is Allocation):
@@ -273,21 +271,21 @@ enum AccessBase : CustomStringConvertible, Hashable {
273271

274272
/// An `AccessPath` is a pair of a `base: AccessBase` and a `projectionPath: Path`
275273
/// which denotes the offset of the access from the base in terms of projections.
276-
struct AccessPath : CustomStringConvertible {
277-
let base: AccessBase
274+
public struct AccessPath : CustomStringConvertible {
275+
public let base: AccessBase
278276

279277
/// address projections only
280-
let projectionPath: SmallProjectionPath
278+
public let projectionPath: SmallProjectionPath
281279

282-
static func unidentified() -> AccessPath {
280+
public static func unidentified() -> AccessPath {
283281
return AccessPath(base: .unidentified, projectionPath: SmallProjectionPath())
284282
}
285283

286-
var description: String {
284+
public var description: String {
287285
"\(projectionPath): \(base)"
288286
}
289287

290-
func isDistinct(from other: AccessPath) -> Bool {
288+
public func isDistinct(from other: AccessPath) -> Bool {
291289
if base.isDistinct(from: other.base) {
292290
// We can already derived from the bases that there is no alias.
293291
// No need to look at the projection paths.
@@ -308,11 +306,11 @@ struct AccessPath : CustomStringConvertible {
308306
/// Note that this access _contains_ `other` if `other` has a _larger_ projection path than this acccess.
309307
/// For example:
310308
/// `%value.s0` contains `%value.s0.s1`
311-
func isEqualOrContains(_ other: AccessPath) -> Bool {
309+
public func isEqualOrContains(_ other: AccessPath) -> Bool {
312310
return getProjection(to: other) != nil
313311
}
314312

315-
var materializableProjectionPath: SmallProjectionPath? {
313+
public var materializableProjectionPath: SmallProjectionPath? {
316314
if projectionPath.isMaterializable {
317315
return projectionPath
318316
}
@@ -325,7 +323,7 @@ struct AccessPath : CustomStringConvertible {
325323
/// `%value.s0`.getProjection(to: `%value.s0.s1`)
326324
/// yields
327325
/// `s1`
328-
func getProjection(to other: AccessPath) -> SmallProjectionPath? {
326+
public func getProjection(to other: AccessPath) -> SmallProjectionPath? {
329327
if !base.isEqual(to: other.base) {
330328
return nil
331329
}
@@ -339,7 +337,7 @@ struct AccessPath : CustomStringConvertible {
339337
}
340338

341339
/// Like `getProjection`, but also requires that the resulting projection path is materializable.
342-
func getMaterializableProjection(to other: AccessPath) -> SmallProjectionPath? {
340+
public func getMaterializableProjection(to other: AccessPath) -> SmallProjectionPath? {
343341
if let projectionPath = getProjection(to: other),
344342
projectionPath.isMaterializable {
345343
return projectionPath
@@ -364,7 +362,7 @@ private func canBeOperandOfIndexAddr(_ value: Value) -> Bool {
364362
/// %ptr = address_to_pointer %orig_addr
365363
/// %addr = pointer_to_address %ptr
366364
/// ```
367-
extension PointerToAddressInst {
365+
private extension PointerToAddressInst {
368366
var originatingAddress: Value? {
369367

370368
struct Walker : ValueUseDefWalker {
@@ -426,7 +424,7 @@ extension PointerToAddressInst {
426424
/// %l3 = load %a3 : $*Int64
427425
/// end_access %a3 : $*Int64
428426
/// ```
429-
enum EnclosingScope {
427+
public enum EnclosingScope {
430428
case scope(BeginAccessInst)
431429
case base(AccessBase)
432430
}
@@ -512,30 +510,30 @@ extension Value {
512510
// go through phi-arguments, the AccessPathWalker will allocate memnory in its cache.
513511

514512
/// Computes the access base of this address value.
515-
var accessBase: AccessBase { accessPath.base }
513+
public var accessBase: AccessBase { accessPath.base }
516514

517515
/// Computes the access path of this address value.
518-
var accessPath: AccessPath {
516+
public var accessPath: AccessPath {
519517
var walker = AccessPathWalker()
520518
walker.walk(startAt: self)
521519
return walker.result
522520
}
523521

524-
func getAccessPath(fromInitialPath: SmallProjectionPath) -> AccessPath {
522+
public func getAccessPath(fromInitialPath: SmallProjectionPath) -> AccessPath {
525523
var walker = AccessPathWalker()
526524
walker.walk(startAt: self, initialPath: fromInitialPath)
527525
return walker.result
528526
}
529527

530528
/// Computes the access path of this address value and also returns the scope.
531-
var accessPathWithScope: (AccessPath, scope: BeginAccessInst?) {
529+
public var accessPathWithScope: (AccessPath, scope: BeginAccessInst?) {
532530
var walker = AccessPathWalker()
533531
walker.walk(startAt: self)
534532
return (walker.result, walker.foundBeginAccess)
535533
}
536534

537535
/// Computes the enclosing access scope of this address value.
538-
var enclosingAccessScope: EnclosingScope {
536+
public var enclosingAccessScope: EnclosingScope {
539537
var walker = AccessPathWalker()
540538
walker.walk(startAt: self)
541539
if let ba = walker.foundBeginAccess {
@@ -545,7 +543,7 @@ extension Value {
545543
}
546544

547545
/// The root definition of a reference, obtained by skipping casts, etc.
548-
var referenceRoot: Value {
546+
public var referenceRoot: Value {
549547
var value: Value = self
550548
while true {
551549
switch value {
@@ -583,7 +581,7 @@ extension ValueUseDefWalker where Path == SmallProjectionPath {
583581
/// the `visit` function for all storage roots with a the corresponding path.
584582
/// Returns true on success.
585583
/// Returns false if not all storage roots could be identified or if `accessPath` has not a "reference" base.
586-
mutating func visitAccessStorageRoots(of accessPath: AccessPath) -> Bool {
584+
public mutating func visitAccessStorageRoots(of accessPath: AccessPath) -> Bool {
587585
walkUpCache.clear()
588586
let path = accessPath.projectionPath
589587
switch accessPath.base {
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# This source file is part of the Swift.org open source project
2+
#
3+
# Copyright (c) 2014 - 2021 Apple Inc. and the Swift project authors
4+
# Licensed under Apache License v2.0 with Runtime Library Exception
5+
#
6+
# See http://swift.org/LICENSE.txt for license information
7+
# See http://swift.org/CONTRIBUTORS.txt for Swift project authors
8+
9+
swift_compiler_sources(SIL
10+
AccessUtils.swift
11+
SmallProjectionPath.swift
12+
WalkUtils.swift
13+
)
14+

SwiftCompilerSources/Sources/SIL/SmallProjectionPath.swift renamed to SwiftCompilerSources/Sources/SIL/Utilities/SmallProjectionPath.swift

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -502,6 +502,20 @@ public struct SmallProjectionPath : Hashable, CustomStringConvertible, NoReflect
502502
return nil
503503
}
504504
}
505+
506+
/// Returns true if the path only contains projections which can be materialized as
507+
/// SIL struct or tuple projection instructions - for values or addresses.
508+
public var isMaterializable: Bool {
509+
let (kind, _, subPath) = pop()
510+
switch kind {
511+
case .root:
512+
return true
513+
case .structField, .tupleField:
514+
return subPath.isMaterializable
515+
default:
516+
return false
517+
}
518+
}
505519
}
506520

507521
//===----------------------------------------------------------------------===//

0 commit comments

Comments
 (0)