Skip to content

Commit 46e8971

Browse files
Merge pull request #5486 from swiftwasm/main
[pull] swiftwasm from main
2 parents 3a6de89 + 4eb06c2 commit 46e8971

File tree

236 files changed

+8705
-2268
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

236 files changed

+8705
-2268
lines changed

SwiftCompilerSources/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -227,7 +227,7 @@ else()
227227
add_subdirectory(Sources)
228228

229229
# TODO: generate this dynamically through the modulemap; this cannot use `sed`
230-
# as that is not available on all paltforms (e.g. Windows).
230+
# as that is not available on all platforms (e.g. Windows).
231231
#
232232
# step 1: generate a dummy source file, which just includes all headers
233233
# defined in include/swift/module.modulemap

SwiftCompilerSources/Sources/Optimizer/FunctionPasses/InitializeStaticGlobals.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ let initializeStaticGlobalsPass = FunctionPass(name: "initialize-static-globals"
6363

6464
context.erase(instruction: allocInst)
6565
context.erase(instruction: storeToGlobal)
66+
context.removeTriviallyDeadInstructionsIgnoringDebugUses(in: function)
6667
}
6768

6869
/// Analyses the global initializer function and returns the `alloc_global` and `store`

SwiftCompilerSources/Sources/Optimizer/InstructionSimplification/CMakeLists.txt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,11 @@ swift_compiler_sources(Optimizer
1616
SimplifyDebugStep.swift
1717
SimplifyDestructure.swift
1818
SimplifyGlobalValue.swift
19+
SimplifyInitEnumDataAddr.swift
1920
SimplifyLoad.swift
2021
SimplifyPartialApply.swift
2122
SimplifyStrongRetainRelease.swift
2223
SimplifyStructExtract.swift
2324
SimplifyTupleExtract.swift
24-
SimplifyUncheckedEnumData.swift)
25+
SimplifyUncheckedEnumData.swift
26+
SimplifyValueToBridgeObject.swift)
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
//===--- SimplifyInitEnumDataAddr.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+
extension InitEnumDataAddrInst : OnoneSimplifyable {
16+
func simplify(_ context: SimplifyContext) {
17+
18+
// Optimize the sequence
19+
// ```
20+
// %1 = init_enum_data_addr %enum_addr, #someCaseWithPayload
21+
// store %payload to %1
22+
// inject_enum_addr %enum_addr, #someCaseWithPayload
23+
// ```
24+
// to
25+
// ```
26+
// %1 = enum $E, #someCaseWithPayload, %payload
27+
// store %1 to %enum_addr
28+
// ```
29+
// This sequence of three instructions must appear in consecutive order.
30+
// But usually this is the case, because it's generated this way by SILGen.
31+
//
32+
if let nextInst = self.next,
33+
let store = nextInst as? StoreInst,
34+
store.destination == self,
35+
let singleUse = self.uses.singleUse,
36+
singleUse.instruction == store,
37+
let nextAfterStore = store.next,
38+
let inject = nextAfterStore as? InjectEnumAddrInst,
39+
inject.enum == self.enum,
40+
inject.enum.type.isLoadable(in: parentFunction) {
41+
42+
assert(self.caseIndex == inject.caseIndex, "mismatching case indices when creating an enum")
43+
44+
let builder = Builder(before: store, context)
45+
let enumInst = builder.createEnum(caseIndex: self.caseIndex, payload: store.source, enumType: self.enum.type.objectType)
46+
let storeOwnership = StoreInst.Ownership(for: self.enum.type, in: parentFunction, initialize: true)
47+
builder.createStore(source: enumInst, destination: self.enum, ownership: storeOwnership)
48+
context.erase(instruction: store)
49+
context.erase(instruction: inject)
50+
context.erase(instruction: self)
51+
}
52+
}
53+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
//===--- SimplifyValueToBridgeObject.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+
extension ValueToBridgeObjectInst : OnoneSimplifyable {
16+
func simplify(_ context: SimplifyContext) {
17+
18+
// Optimize the sequence
19+
// ```
20+
// %1 = value_to_bridge_object %0
21+
// %2 = struct $SomeInt (%1)
22+
// %3 = unchecked_trivial_bit_cast %2
23+
// %4 = struct_extract %3, #valueFieldInInt
24+
// %5 = value_to_bridge_object %4
25+
// ```
26+
// to
27+
// ```
28+
// %5 = value_to_bridge_object %0
29+
// ```
30+
// This sequence comes up in the code for constructing an empty string literal.
31+
//
32+
if let se = self.value as? StructExtractInst,
33+
let utbc = se.struct as? UncheckedTrivialBitCastInst,
34+
let vtbo = utbc.fromValue as? ValueToBridgeObjectInst {
35+
self.operand.set(to: vtbo.value, context)
36+
}
37+
}
38+
}

SwiftCompilerSources/Sources/Optimizer/PassManager/Context.swift

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -168,8 +168,7 @@ struct FunctionPassContext : MutatingContext {
168168
var notifyInstructionChanged: (Instruction) -> () { return { inst in } }
169169

170170
func continueWithNextSubpassRun(for inst: Instruction? = nil) -> Bool {
171-
let bridgedInst = OptionalBridgedInstruction(inst?.bridged.obj)
172-
return _bridged.continueWithNextSubpassRun(bridgedInst)
171+
return _bridged.continueWithNextSubpassRun(inst.bridged)
173172
}
174173

175174
func createSimplifyContext(preserveDebugInfo: Bool, notifyInstructionChanged: @escaping (Instruction) -> ()) -> SimplifyContext {

SwiftCompilerSources/Sources/SIL/Builder.swift

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ public struct Builder {
113113
return notifyNew(cast.getAs(UpcastInst.self))
114114
}
115115

116-
public func createLoad(fromAddress: Value, ownership: LoadInst.LoadOwnership) -> LoadInst {
116+
public func createLoad(fromAddress: Value, ownership: LoadInst.Ownership) -> LoadInst {
117117
let load = bridged.createLoad(fromAddress.bridged, ownership.rawValue)
118118
return notifyNew(load.getAs(LoadInst.self))
119119
}
@@ -184,6 +184,12 @@ public struct Builder {
184184
let ued = bridged.createUncheckedEnumData(enumVal.bridged, caseIndex, resultType.bridged)
185185
return notifyNew(ued.getAs(UncheckedEnumDataInst.self))
186186
}
187+
188+
public func createEnum(caseIndex: Int, payload: Value?, enumType: Type) -> EnumInst {
189+
let enumInst = bridged.createEnum(caseIndex, payload.bridged, enumType.bridged)
190+
return notifyNew(enumInst.getAs(EnumInst.self))
191+
}
192+
187193
@discardableResult
188194
public func createSwitchEnum(enum enumVal: Value,
189195
cases: [(Int, BasicBlock)],
@@ -225,20 +231,23 @@ public struct Builder {
225231
return notifyNew(bridged.createGlobalValue(global.bridged).getAs(GlobalValueInst.self))
226232
}
227233

228-
@discardableResult
229234
public func createStruct(type: Type, elements: [Value]) -> StructInst {
230235
let structInst = elements.withBridgedValues { valuesRef in
231236
return bridged.createStruct(type.bridged, valuesRef)
232237
}
233238
return notifyNew(structInst.getAs(StructInst.self))
234239
}
235240

236-
@discardableResult
237241
public func createTuple(type: Type, elements: [Value]) -> TupleInst {
238242
let tuple = elements.withBridgedValues { valuesRef in
239243
return bridged.createTuple(type.bridged, valuesRef)
240244
}
241245
return notifyNew(tuple.getAs(TupleInst.self))
242246
}
243247

248+
@discardableResult
249+
public func createStore(source: Value, destination: Value, ownership: StoreInst.Ownership) -> StoreInst {
250+
let store = bridged.createStore(source.bridged, destination.bridged, ownership.rawValue)
251+
return notifyNew(store.getAs(StoreInst.self))
252+
}
244253
}

SwiftCompilerSources/Sources/SIL/GlobalVariable.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,7 @@ extension Instruction {
123123
return !fri.referencedFunction.isAsync
124124
case is StructInst,
125125
is TupleInst,
126+
is EnumInst,
126127
is IntegerLiteralInst,
127128
is FloatLiteralInst,
128129
is ObjectInst,

SwiftCompilerSources/Sources/SIL/Instruction.swift

Lines changed: 32 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,12 @@ extension OptionalBridgedInstruction {
148148
}
149149
}
150150

151+
extension Optional where Wrapped == Instruction {
152+
public var bridged: OptionalBridgedInstruction {
153+
OptionalBridgedInstruction(self?.bridged.obj)
154+
}
155+
}
156+
151157
public class SingleValueInstruction : Instruction, Value {
152158
final public var definingInstruction: Instruction? { self }
153159

@@ -222,11 +228,23 @@ extension StoringInstruction {
222228

223229
final public class StoreInst : Instruction, StoringInstruction {
224230
// must match with enum class StoreOwnershipQualifier
225-
public enum StoreOwnership: Int {
231+
public enum Ownership: Int {
226232
case unqualified = 0, initialize = 1, assign = 2, trivial = 3
233+
234+
public init(for type: Type, in function: Function, initialize: Bool) {
235+
if function.hasOwnership {
236+
if type.isTrivial(in: function) {
237+
self = .trivial
238+
} else {
239+
self = initialize ? .initialize : .assign
240+
}
241+
} else {
242+
self = .unqualified
243+
}
244+
}
227245
}
228-
public var destinationOwnership: StoreOwnership {
229-
StoreOwnership(rawValue: bridged.StoreInst_getStoreOwnership())!
246+
public var destinationOwnership: Ownership {
247+
Ownership(rawValue: bridged.StoreInst_getStoreOwnership())!
230248
}
231249
}
232250

@@ -307,6 +325,7 @@ final public class DestroyAddrInst : Instruction, UnaryInstruction {
307325
}
308326

309327
final public class InjectEnumAddrInst : Instruction, UnaryInstruction, EnumInstruction {
328+
public var `enum`: Value { operand.value }
310329
public var caseIndex: Int { bridged.InjectEnumAddrInst_caseIndex() }
311330
}
312331

@@ -351,11 +370,11 @@ final public class LoadInst : SingleValueInstruction, UnaryInstruction {
351370
public var address: Value { operand.value }
352371

353372
// must match with enum class LoadOwnershipQualifier
354-
public enum LoadOwnership: Int {
373+
public enum Ownership: Int {
355374
case unqualified = 0, take = 1, copy = 2, trivial = 3
356375
}
357-
public var ownership: LoadOwnership {
358-
LoadOwnership(rawValue: bridged.LoadInst_getLoadOwnership())!
376+
public var ownership: Ownership {
377+
Ownership(rawValue: bridged.LoadInst_getLoadOwnership())!
359378
}
360379
}
361380

@@ -388,6 +407,10 @@ final public class UncheckedAddrCastInst : SingleValueInstruction, UnaryInstruct
388407
public var fromAddress: Value { operand.value }
389408
}
390409

410+
final public class UncheckedTrivialBitCastInst : SingleValueInstruction, UnaryInstruction {
411+
public var fromValue: Value { operand.value }
412+
}
413+
391414
final public
392415
class RawPointerToRefInst : SingleValueInstruction, UnaryInstruction {
393416
public var pointer: Value { operand.value }
@@ -626,7 +649,9 @@ final public
626649
class ObjCMetatypeToObjectInst : SingleValueInstruction, UnaryInstruction {}
627650

628651
final public
629-
class ValueToBridgeObjectInst : SingleValueInstruction, UnaryInstruction {}
652+
class ValueToBridgeObjectInst : SingleValueInstruction, UnaryInstruction {
653+
public var value: Value { return operand.value }
654+
}
630655

631656
final public
632657
class MarkDependenceInst : SingleValueInstruction {

SwiftCompilerSources/Sources/SIL/Registration.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ public func registerSILClasses() {
7777
register(UpcastInst.self)
7878
register(UncheckedRefCastInst.self)
7979
register(UncheckedAddrCastInst.self)
80+
register(UncheckedTrivialBitCastInst.self)
8081
register(MarkMustCheckInst.self)
8182
register(ObjectInst.self)
8283
register(RawPointerToRefInst.self)

0 commit comments

Comments
 (0)