Skip to content

Commit 8137adf

Browse files
committed
SwiftCompilerSources: support Undef.parentFunction and PlaceholderValue.parentFunction
After support in the C++ SIL, we can implement parentFunction for those values in swift, too. It simplifies a few things.
1 parent 58188fc commit 8137adf

File tree

5 files changed

+29
-12
lines changed

5 files changed

+29
-12
lines changed

SwiftCompilerSources/Sources/Optimizer/Utilities/LifetimeDependenceUtils.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,7 @@ struct LifetimeDependence : CustomStringConvertible {
185185
var parentValue: Value { scope.parentValue }
186186

187187
var function: Function {
188-
dependentValue.parentFunction // dependentValue can't be undef
188+
dependentValue.parentFunction
189189
}
190190

191191
var description: String {

SwiftCompilerSources/Sources/Optimizer/Utilities/OptUtils.swift

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -47,9 +47,6 @@ extension Value {
4747
/// check, because it treats a value_to_bridge_object instruction as "trivial".
4848
/// It can also handle non-trivial enums with trivial cases.
4949
func isTrivial(_ context: some Context) -> Bool {
50-
if self is Undef {
51-
return true
52-
}
5350
var worklist = ValueWorklist(context)
5451
defer { worklist.deinitialize() }
5552

@@ -64,12 +61,12 @@ extension Value {
6461
switch v {
6562
case is ValueToBridgeObjectInst:
6663
break
67-
case is StructInst, is TupleInst:
68-
let inst = (v as! SingleValueInstruction)
69-
worklist.pushIfNotVisited(contentsOf: inst.operands.values.filter { !($0 is Undef) })
64+
case let si as StructInst:
65+
worklist.pushIfNotVisited(contentsOf: si.operands.values)
66+
case let ti as TupleInst:
67+
worklist.pushIfNotVisited(contentsOf: ti.operands.values)
7068
case let en as EnumInst:
71-
if let payload = en.payload,
72-
!(payload is Undef) {
69+
if let payload = en.payload {
7370
worklist.pushIfNotVisited(payload)
7471
}
7572
default:

SwiftCompilerSources/Sources/SIL/Value.swift

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,13 @@ public protocol Value : AnyObject, CustomStringConvertible {
2525
var definingInstruction: Instruction? { get }
2626

2727
/// The block where the value is defined.
28-
///
29-
/// It's not legal to get the definingBlock of an `Undef` value.
3028
var parentBlock: BasicBlock { get }
3129

30+
/// The function where the value lives in.
31+
///
32+
/// It's not legal to get the parentFunction of an instruction in a global initializer.
33+
var parentFunction: Function { get }
34+
3235
/// True if the value has a trivial type.
3336
var hasTrivialType: Bool { get }
3437

@@ -113,6 +116,7 @@ extension Value {
113116

114117
public var uses: UseList { UseList(bridged.getFirstUse()) }
115118

119+
// Default implementation for all values which have a parent block, like instructions and arguments.
116120
public var parentFunction: Function { parentBlock.parentFunction }
117121

118122
public var type: Type { bridged.getType().type }
@@ -206,8 +210,11 @@ extension BridgedValue {
206210
public final class Undef : Value {
207211
public var definingInstruction: Instruction? { nil }
208212

213+
public var parentFunction: Function { bridged.SILUndef_getParentFunction().function }
214+
209215
public var parentBlock: BasicBlock {
210-
fatalError("undef has no defining block")
216+
// By convention, undefs are considered to be defined at the entry of the function.
217+
parentFunction.entryBlock
211218
}
212219

213220
/// Undef has not parent function, therefore the default `hasTrivialType` does not work.
@@ -221,9 +228,12 @@ public final class Undef : Value {
221228

222229
final class PlaceholderValue : Value {
223230
public var definingInstruction: Instruction? { nil }
231+
224232
public var parentBlock: BasicBlock {
225233
fatalError("PlaceholderValue has no defining block")
226234
}
235+
236+
public var parentFunction: Function { bridged.PlaceholderValue_getParentFunction().function }
227237
}
228238

229239
extension OptionalBridgedValue {

include/swift/SIL/SILBridging.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -414,6 +414,8 @@ struct BridgedValue {
414414
SWIFT_IMPORT_UNSAFE BRIDGED_INLINE OptionalBridgedOperand getFirstUse() const;
415415
SWIFT_IMPORT_UNSAFE BRIDGED_INLINE BridgedType getType() const;
416416
BRIDGED_INLINE Ownership getOwnership() const;
417+
SWIFT_IMPORT_UNSAFE BRIDGED_INLINE BridgedFunction SILUndef_getParentFunction() const;
418+
SWIFT_IMPORT_UNSAFE BRIDGED_INLINE BridgedFunction PlaceholderValue_getParentFunction() const;
417419

418420
bool findPointerEscape() const;
419421
};

include/swift/SIL/SILBridgingImpl.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -418,6 +418,14 @@ BridgedValue::Ownership BridgedValue::getOwnership() const {
418418
return castOwnership(getSILValue()->getOwnershipKind());
419419
}
420420

421+
BridgedFunction BridgedValue::SILUndef_getParentFunction() const {
422+
return {llvm::cast<swift::SILUndef>(getSILValue())->getParent()};
423+
}
424+
425+
BridgedFunction BridgedValue::PlaceholderValue_getParentFunction() const {
426+
return {llvm::cast<swift::PlaceholderValue>(getSILValue())->getParent()};
427+
}
428+
421429
//===----------------------------------------------------------------------===//
422430
// BridgedOperand
423431
//===----------------------------------------------------------------------===//

0 commit comments

Comments
 (0)