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)

SwiftCompilerSources/Sources/SIL/Type.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,10 @@ public struct Type : CustomStringConvertible, NoReflectionChildren {
3131
return !bridged.isNonTrivialOrContainsRawPointer(function.bridged.getFunction())
3232
}
3333

34+
public func isLoadable(in function: Function) -> Bool {
35+
return bridged.isLoadable(function.bridged.getFunction())
36+
}
37+
3438
public func isReferenceCounted(in function: Function) -> Bool {
3539
return bridged.isReferenceCounted(function.bridged.getFunction())
3640
}

SwiftCompilerSources/Sources/SIL/Value.swift

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,3 +204,9 @@ final class PlaceholderValue : Value {
204204
extension OptionalBridgedValue {
205205
public var value: Value? { obj.getAs(AnyObject.self) as? Value }
206206
}
207+
208+
extension Optional where Wrapped == Value {
209+
public var bridged: OptionalBridgedValue {
210+
OptionalBridgedValue(obj: self?.bridged.obj)
211+
}
212+
}

cmake/modules/AddPureSwift.cmake

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -261,9 +261,15 @@ function(add_pure_swift_host_tool name)
261261
add_executable(${name} ${APSHT_SOURCES})
262262
_add_host_swift_compile_options(${name})
263263

264-
set_property(TARGET ${name}
265-
APPEND PROPERTY INSTALL_RPATH
266-
"@executable_path/../lib/swift/host")
264+
if(${SWIFT_HOST_VARIANT_SDK} IN_LIST SWIFT_DARWIN_PLATFORMS)
265+
set_property(TARGET ${name}
266+
APPEND PROPERTY INSTALL_RPATH
267+
"@executable_path/../lib/swift/host")
268+
else()
269+
set_property(TARGET ${name}
270+
APPEND PROPERTY INSTALL_RPATH
271+
"$ORIGIN/../lib/swift/host")
272+
endif()
267273

268274
set_property(TARGET ${name}
269275
PROPERTY BUILD_WITH_INSTALL_RPATH YES)

cmake/modules/AddSwift.cmake

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -578,7 +578,7 @@ function(_add_swift_runtime_link_flags target relpath_to_lib_dir bootstrapping)
578578
# Make sure we can find the early SwiftSyntax libraries.
579579
target_link_directories(${target} PRIVATE "${SWIFT_PATH_TO_EARLYSWIFTSYNTAX_BUILD_DIR}/lib/swift/host")
580580
581-
# For the "end step" of bootstrapping configurations on Darwin, need to be
581+
# For the "end step" of bootstrapping configurations, we need to be
582582
# able to fall back to the SDK directory for libswiftCore et al.
583583
if (BOOTSTRAPPING_MODE MATCHES "BOOTSTRAPPING.*")
584584
if (NOT "${bootstrapping}" STREQUAL "1")
@@ -592,6 +592,13 @@ function(_add_swift_runtime_link_flags target relpath_to_lib_dir bootstrapping)
592592
get_filename_component(TOOLCHAIN_BIN_DIR ${SWIFT_EXEC_FOR_SWIFT_MODULES} DIRECTORY)
593593
get_filename_component(TOOLCHAIN_LIB_DIR "${TOOLCHAIN_BIN_DIR}/../lib/swift/${SWIFT_SDK_${SWIFT_HOST_VARIANT_SDK}_LIB_SUBDIR}" ABSOLUTE)
594594
target_link_directories(${target} PUBLIC ${TOOLCHAIN_LIB_DIR})
595+
else()
596+
get_filename_component(swift_bin_dir ${SWIFT_EXEC_FOR_SWIFT_MODULES} DIRECTORY)
597+
get_filename_component(swift_dir ${swift_bin_dir} DIRECTORY)
598+
set(host_lib_dir "${swift_dir}/lib/swift/${SWIFT_SDK_${SWIFT_HOST_VARIANT_SDK}_LIB_SUBDIR}")
599+
target_link_directories(${target} PRIVATE ${host_lib_dir})
600+
601+
set(swift_runtime_rpath "${host_lib_dir}")
595602
endif()
596603
endif()
597604
endif()
@@ -928,10 +935,17 @@ function(add_swift_host_tool executable)
928935
endif()
929936
endif()
930937
931-
set_property(
932-
TARGET ${executable}
933-
APPEND PROPERTY INSTALL_RPATH
934-
"@executable_path/../${extra_relative_rpath}lib/swift/host")
938+
if(${SWIFT_HOST_VARIANT_SDK} IN_LIST SWIFT_DARWIN_PLATFORMS)
939+
set_property(
940+
TARGET ${executable}
941+
APPEND PROPERTY INSTALL_RPATH
942+
"@executable_path/../${extra_relative_rpath}lib/swift/host")
943+
else()
944+
set_property(
945+
TARGET ${executable}
946+
APPEND PROPERTY INSTALL_RPATH
947+
"$ORIGIN/../${extra_relative_rpath}lib/swift/host")
948+
endif()
935949
endif()
936950
937951
if(ASHT_THINLTO_LD64_ADD_FLTO_CODEGEN_ONLY)

docs/ReferenceGuides/UnderscoredAttributes.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -832,6 +832,12 @@ Fully bypasses access control, allowing access to private declarations
832832
in the imported module. The imported module needs to be compiled with
833833
`-Xfrontend -enable-private-imports` for this to work.
834834

835+
## `@_section("section_name")`
836+
837+
Places a global variable or a top-level function into a section of the object
838+
file with the given name. It's the equivalent of clang's
839+
`__attribute__((section))`.
840+
835841
## `@_semantics("uniquely.recognized.id")`
836842

837843
Allows the optimizer to make use of some key invariants in performance critical
@@ -994,6 +1000,12 @@ for more details.
9941000

9951001
This `async` function uses the pre-SE-0338 semantics of unsafely inheriting the caller's executor. This is an underscored feature because the right way of inheriting an executor is to pass in the required executor and switch to it. Unfortunately, there are functions in the standard library which need to inherit their caller's executor but cannot change their ABI because they were not defined as `@_alwaysEmitIntoClient` in the initial release.
9961002

1003+
## `@_used`
1004+
1005+
Marks a global variable or a top-level function as "used externally" even if it
1006+
does not have visible users in the compilation unit. It's the equivalent of
1007+
clang's `__attribute__((used))`.
1008+
9971009
## `@_weakLinked`
9981010

9991011
Allows a declaration to be weakly-referenced, i.e., any references emitted by

0 commit comments

Comments
 (0)