Skip to content

Commit 60609eb

Browse files
committed
Swift SIL: invert Type.isNonTrivialOrContainsRawPointer to Type.isTrivialNonPointer
This is consistent with `Type.isTrivial`. Also, introduce corresponding properties in `Value`: `hasTrivialType` and `hasTrivialNonPointerType`, because 1. It's less to type than `Type.isTrivial(in: function)` because `Value` knows in which function it is. 2. It fixes the corner case where value is an `Undef`, which has not parent function.
1 parent 0a2d233 commit 60609eb

File tree

6 files changed

+36
-15
lines changed

6 files changed

+36
-15
lines changed

SwiftCompilerSources/Sources/Optimizer/FunctionPasses/ComputeEscapeEffects.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,8 @@ let computeEscapeEffects = FunctionPass(name: "compute-escape-effects", {
3535

3636
for arg in function.arguments {
3737
// We are not interested in arguments with trivial types.
38-
if !arg.type.isNonTrivialOrContainsRawPointer(in: function) { continue }
39-
38+
if arg.hasTrivialNonPointerType { continue }
39+
4040
// Also, we don't want to override defined effects.
4141
if argsWithDefinedEffects.contains(arg.index) { continue }
4242

SwiftCompilerSources/Sources/Optimizer/FunctionPasses/ComputeSideEffects.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -376,7 +376,7 @@ private struct ArgumentEscapingWalker : ValueDefUseWalker, AddressDefUseWalker {
376376
mutating func hasUnknownUses(argument: FunctionArgument) -> Bool {
377377
if argument.type.isAddress {
378378
return walkDownUses(ofAddress: argument, path: UnusedWalkingPath()) == .abortWalk
379-
} else if argument.type.isTrivial(in: argument.function) {
379+
} else if argument.hasTrivialType {
380380
return false
381381
} else {
382382
return walkDownUses(ofValue: argument, path: UnusedWalkingPath()) == .abortWalk
@@ -415,22 +415,22 @@ private struct ArgumentEscapingWalker : ValueDefUseWalker, AddressDefUseWalker {
415415
switch inst {
416416
case let copy as CopyAddrInst:
417417
if address == copy.sourceOperand &&
418-
!address.value.type.isTrivial(in: inst.function) &&
418+
!address.value.hasTrivialType &&
419419
(!function.hasOwnership || copy.isTakeOfSrc) {
420420
foundTakingLoad = true
421421
}
422422
return .continueWalk
423423

424424
case let load as LoadInst:
425-
if !address.value.type.isTrivial(in: function) &&
425+
if !address.value.hasTrivialType &&
426426
// In non-ossa SIL we don't know if a load is taking.
427427
(!function.hasOwnership || load.ownership == .take) {
428428
foundTakingLoad = true
429429
}
430430
return .continueWalk
431431

432432
case is LoadWeakInst, is LoadUnownedInst, is LoadBorrowInst:
433-
if !function.hasOwnership && !address.value.type.isTrivial(in: function) {
433+
if !function.hasOwnership && !address.value.hasTrivialType {
434434
foundTakingLoad = true
435435
}
436436
return .continueWalk

SwiftCompilerSources/Sources/Optimizer/FunctionPasses/ReleaseDevirtualizer.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ private struct FindAllocationOfRelease : ValueUseDefWalker {
149149
//
150150
var nonTrivialOperandFound = false
151151
for operand in def.operands {
152-
if !operand.value.type.isTrivial(in: def.function) {
152+
if !operand.value.hasTrivialType {
153153
if nonTrivialOperandFound {
154154
return .abortWalk
155155
}

SwiftCompilerSources/Sources/Optimizer/Utilities/EscapeUtils.swift

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -197,12 +197,11 @@ extension EscapeVisitor {
197197
}
198198

199199
func hasRelevantType(_ value: Value, at path: SmallProjectionPath, analyzeAddresses: Bool) -> Bool {
200-
let type = value.type
201-
if type.isNonTrivialOrContainsRawPointer(in: value.function) { return true }
200+
if !value.hasTrivialNonPointerType { return true }
202201

203202
// For selected addresses we also need to consider trivial types (`value`
204203
// is a selected address if the path does not contain any class projections).
205-
if analyzeAddresses && type.isAddress && !path.hasClassProjection { return true }
204+
if analyzeAddresses && value.type.isAddress && !path.hasClassProjection { return true }
206205
return false
207206
}
208207
}
@@ -542,7 +541,7 @@ fileprivate struct EscapeWalker<V: EscapeVisitor> : ValueDefUseWalker,
542541
let svi = instruction as! SingleValueInstruction
543542

544543
// Even when analyzing addresses, a loaded trivial value can be ignored.
545-
if !svi.type.isNonTrivialOrContainsRawPointer(in: svi.function) { return .continueWalk }
544+
if svi.hasTrivialNonPointerType { return .continueWalk }
546545
return walkDownUses(ofValue: svi, path: path.with(knownType: nil))
547546
case let atp as AddressToPointerInst:
548547
return walkDownUses(ofValue: atp, path: path.with(knownType: nil))

SwiftCompilerSources/Sources/SIL/Type.swift

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,13 @@ public struct Type : CustomStringConvertible, NoReflectionChildren {
2323
return SILType_isTrivial(bridged, function.bridged) != 0
2424
}
2525

26-
public func isReferenceCounted(in function: Function) -> Bool {
27-
return SILType_isReferenceCounted(bridged, function.bridged) != 0
26+
/// Returns true if the type is a trivial type and is and does not contain a Builtin.RawPointer.
27+
public func isTrivialNonPointer(in function: Function) -> Bool {
28+
return SILType_isNonTrivialOrContainsRawPointer(bridged, function.bridged) == 0
2829
}
2930

30-
public func isNonTrivialOrContainsRawPointer(in function: Function) -> Bool {
31-
return SILType_isNonTrivialOrContainsRawPointer(bridged, function.bridged) != 0
31+
public func isReferenceCounted(in function: Function) -> Bool {
32+
return SILType_isReferenceCounted(bridged, function.bridged) != 0
3233
}
3334

3435
public var isNominal: Bool { SILType_isNominal(bridged) != 0 }

SwiftCompilerSources/Sources/SIL/Value.swift

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,12 @@ public protocol Value : AnyObject, CustomStringConvertible {
2727
///
2828
/// It's not legal to get the definingBlock of an `Undef` value.
2929
var definingBlock: BasicBlock { get }
30+
31+
/// True if the value has a trivial type.
32+
var hasTrivialType: Bool { get }
33+
34+
/// True if the value has a trivial type which is and does not contain a Builtin.RawPointer.
35+
var hasTrivialNonPointerType: Bool { get }
3036
}
3137

3238
public enum Ownership {
@@ -93,6 +99,12 @@ extension Value {
9399

94100
public var type: Type { SILValue_getType(bridged).type }
95101

102+
/// True if the value has a trivial type.
103+
public var hasTrivialType: Bool { type.isTrivial(in: function) }
104+
105+
/// True if the value has a trivial type which is and does not contain a Builtin.RawPointer.
106+
public var hasTrivialNonPointerType: Bool { type.isTrivialNonPointer(in: function) }
107+
96108
public var ownership: Ownership { SILValue_getOwnership(bridged).ownership }
97109

98110
public var hashable: HashableValue { ObjectIdentifier(self) }
@@ -165,9 +177,18 @@ extension BridgedValue {
165177

166178
final class Undef : Value {
167179
public var definingInstruction: Instruction? { nil }
180+
168181
public var definingBlock: BasicBlock {
169182
fatalError("undef has no defining block")
170183
}
184+
185+
/// Undef has not parent function, therefore the default `hasTrivialType` does not work.
186+
/// Return the conservative default in this case.
187+
public var hasTrivialType: Bool { false }
188+
189+
/// Undef has not parent function, therefore the default `hasTrivialNonPointerType` does not work.
190+
/// Return the conservative default in this case.
191+
public var hasTrivialNonPointerType: Bool { false }
171192
}
172193

173194
final class PlaceholderValue : Value {

0 commit comments

Comments
 (0)