Skip to content

Commit 45b97f1

Browse files
authored
Merge branch 'main' into wip-experimental-isolated-deinit
2 parents aeb71c4 + bf6bb65 commit 45b97f1

File tree

307 files changed

+6007
-2540
lines changed

Some content is hidden

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

307 files changed

+6007
-2540
lines changed

.github/CODEOWNERS

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@
7070
/include/swift/AST/Evaluator* @CodaFi @slavapestov
7171
/include/swift/Basic/ @DougGregor
7272
/include/swift/Basic/Features.def @DougGregor @hborla
73-
/include/swift/ClangImporter @zoecarver @hyp @egorzhdan @beccadax @ian-twilightcoder
73+
/include/swift/ClangImporter @zoecarver @hyp @egorzhdan @beccadax @ian-twilightcoder @Xazax-hun
7474
/include/swift/DependencyScan @artemcm
7575
/include/swift/Driver*/ @artemcm
7676
/include/swift/Frontend*/ @artemcm @tshortli
@@ -81,7 +81,7 @@
8181
/include/swift/Migrator/ @nkcsgexi
8282
/include/swift/Option/*Options* @tshortli
8383
/include/swift/Parse/ @ahoppen @bnbarham @CodaFi @DougGregor @hamishknight @rintaro
84-
/include/swift/PrintAsClang @zoecarver @hyp @egorzhdan
84+
/include/swift/PrintAsClang @zoecarver @hyp @egorzhdan @Xazax-hun
8585
/include/swift/Refactoring @ahoppen @bnbarham @hamishknight @rintaro
8686
/include/swift/Runtime/ @rjmccall
8787
/include/swift/SIL/ @jckarter
@@ -116,7 +116,7 @@
116116
/lib/ASTGen/ @ahoppen @bnbarham @CodaFi @hamishknight @rintaro
117117
/lib/Basic/ @DougGregor
118118
/lib/Basic/Windows @compnerd
119-
/lib/ClangImporter @zoecarver @hyp @egorzhdan @beccadax @ian-twilightcoder
119+
/lib/ClangImporter @zoecarver @hyp @egorzhdan @beccadax @ian-twilightcoder @Xazax-hun
120120
/lib/ClangImporter/DWARFImporter* @adrian-prantl
121121
/lib/DependencyScan @artemcm
122122
/lib/Driver*/ @artemcm
@@ -135,7 +135,7 @@
135135
/lib/Markup/ @nkcsgexi
136136
/lib/Migrator/ @nkcsgexi
137137
/lib/Parse/ @ahoppen @bnbarham @CodaFi @DougGregor @hamishknight @rintaro
138-
/lib/PrintAsClang @zoecarver @hyp @egorzhdan
138+
/lib/PrintAsClang @zoecarver @hyp @egorzhdan @Xazax-hun
139139
/lib/Refactoring/ @ahoppen @bnbarham @hamishknight @rintaro
140140
/lib/SIL/ @jckarter
141141
/lib/SIL/**/*DebugInfo* @adrian-prantl
@@ -177,7 +177,7 @@
177177
/stdlib/public/*Demangl*/ @rjmccall
178178
/stdlib/public/Backtracing/ @al45tair
179179
/stdlib/public/Concurrency/ @ktoso
180-
/stdlib/public/Cxx/ @zoecarver @hyp @egorzhdan
180+
/stdlib/public/Cxx/ @zoecarver @hyp @egorzhdan @Xazax-hun
181181
/stdlib/public/Distributed/ @ktoso
182182
/stdlib/public/Observation/ @phausler
183183
/stdlib/public/SwiftRemoteMirror/ @slavapestov
@@ -202,7 +202,7 @@
202202
/test/IDE/ @ahoppen @bnbarham @hamishknight @rintaro
203203
/test/IRGen/ @rjmccall
204204
/test/Index/ @ahoppen @bnbarham @hamishknight @rintaro
205-
/test/Interop/ @zoecarver @hyp @egorzhdan
205+
/test/Interop/ @zoecarver @hyp @egorzhdan @Xazax-hun
206206
/test/Migrator/ @nkcsgexi
207207
/test/Parse/ @ahoppen @bnbarham @CodaFi @DougGregor @hamishknight @rintaro
208208
/test/Profiler @ahoppen @bnbarham @hamishknight @rintaro

CHANGELOG.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,22 @@
55
66
## Swift (next)
77

8+
* [SE-0444][]:
9+
When the upcoming feature `MemberImportVisibility` is enabled, Swift will
10+
require that a module be directly imported in a source file when resolving
11+
member declarations from that module:
12+
13+
```swift
14+
let recipe = "2 slices of bread, 1.5 tbs peanut butter".parse()
15+
// error: instance method 'parse()' is inaccessible due to missing import of
16+
// defining module 'RecipeKit'
17+
// note: add import of module 'RecipeKit'
18+
```
19+
20+
This new behavior prevents ambiguities from arising when a transitively
21+
imported module declares a member that conflicts with a member of a directly
22+
imported module.
23+
824
* Syntactic SourceKit queries no longer attempt to provide information
925
within the inactive `#if` regions. For example, given:
1026

@@ -10629,6 +10645,7 @@ using the `.dynamicType` member to retrieve the type of an expression should mig
1062910645
[SE-0428]: https://github.com/apple/swift-evolution/blob/main/proposals/0428-resolve-distributed-actor-protocols.md
1063010646
[SE-0431]: https://github.com/apple/swift-evolution/blob/main/proposals/0431-isolated-any-functions.md
1063110647
[SE-0442]: https://github.com/swiftlang/swift-evolution/blob/main/proposals/0442-allow-taskgroup-childtaskresult-type-to-be-inferred.md
10648+
[SE-0444]: https://github.com/swiftlang/swift-evolution/blob/main/proposals/0444-member-import-visibility.md
1063210649
[#64927]: <https://github.com/apple/swift/issues/64927>
1063310650
[#42697]: <https://github.com/apple/swift/issues/42697>
1063410651
[#42728]: <https://github.com/apple/swift/issues/42728>

CMakeLists.txt

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -434,9 +434,8 @@ if("${CMAKE_SYSTEM_NAME}" STREQUAL "Windows" AND BOOTSTRAPPING_MODE STREQUAL "HO
434434
endif()
435435

436436
if(BRIDGING_MODE STREQUAL "DEFAULT" OR NOT BRIDGING_MODE)
437-
if(CMAKE_BUILD_TYPE STREQUAL "Debug" OR "${SWIFT_HOST_VARIANT_SDK}" STREQUAL "WINDOWS" OR (CMAKE_Swift_COMPILER AND CMAKE_Swift_COMPILER_VERSION VERSION_LESS 5.8))
437+
if(CMAKE_BUILD_TYPE STREQUAL "Debug" OR (CMAKE_Swift_COMPILER AND CMAKE_Swift_COMPILER_VERSION VERSION_LESS 5.8))
438438
# In debug builds, to workaround a problem with LLDB's `po` command (rdar://115770255).
439-
# On Windows, to workaround a build problem.
440439
# If the host Swift version is less than 5.8, use pure mode to workaround a C++ interop compiler crash.
441440
set(BRIDGING_MODE "PURE")
442441
else()

SwiftCompilerSources/CMakeLists.txt

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,8 @@ function(add_swift_compiler_modules_library name)
177177
if(CMAKE_SYSTEM_NAME STREQUAL "Windows")
178178
list(APPEND swift_compile_options "-static")
179179
list(APPEND sdk_option "-sdk" "${SWIFT_PATH_TO_SWIFT_SDK}")
180+
# For "swift/shims/*.h".
181+
list(APPEND sdk_option "-I" "${SWIFT_PATH_TO_SWIFT_SDK}/usr/lib")
180182

181183
# Workaround a crash in the LoadableByAddress pass
182184
# https://github.com/apple/swift/issues/73254
@@ -188,6 +190,12 @@ function(add_swift_compiler_modules_library name)
188190
if (CMAKE_Swift_COMPILER_VERSION VERSION_LESS 6.0)
189191
list(APPEND swift_compile_options "-Xcc" "-D_ALLOW_COMPILER_AND_STL_VERSION_MISMATCH")
190192
endif()
193+
194+
# Make 'offsetof()' a const value.
195+
list(APPEND swift_compile_options "-Xcc" "-D_CRT_USE_BUILTIN_OFFSETOF")
196+
197+
# Workaround for https://github.com/swiftlang/llvm-project/issues/7172
198+
list(APPEND swift_compile_options "-Xcc" "-Xclang" "-Xcc" "-fmodule-format=raw")
191199
else()
192200
list(APPEND sdk_option "-I" "${swift_exec_bin_dir}/../lib" "-I" "${sdk_path}/usr/lib")
193201
endif()

SwiftCompilerSources/Package.swift

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,9 +58,6 @@ private extension Target {
5858
let package = Package(
5959
name: "SwiftCompilerSources",
6060
platforms: [
61-
// We need at least macOS 13 here to avoid hitting an availability error
62-
// for CxxStdlib. It's only needed for the package though, the CMake build
63-
// works fine with a lower deployment target.
6461
.macOS(.v13),
6562
],
6663
products: [

SwiftCompilerSources/Sources/Optimizer/DataStructures/FunctionUses.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -128,21 +128,21 @@ struct FunctionUses {
128128

129129
for vTable in context.vTables {
130130
for entry in vTable.entries {
131-
markUnknown(entry.function)
131+
markUnknown(entry.implementation)
132132
}
133133
}
134134

135135
for witnessTable in context.witnessTables {
136136
for entry in witnessTable.entries {
137-
if entry.kind == .Method, let f = entry.methodFunction {
137+
if entry.kind == .method, let f = entry.methodFunction {
138138
markUnknown(f)
139139
}
140140
}
141141
}
142142

143143
for witnessTable in context.defaultWitnessTables {
144144
for entry in witnessTable.entries {
145-
if entry.kind == .Method, let f = entry.methodFunction {
145+
if entry.kind == .method, let f = entry.methodFunction {
146146
markUnknown(f)
147147
}
148148
}

SwiftCompilerSources/Sources/Optimizer/InstructionSimplification/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
# See http://swift.org/CONTRIBUTORS.txt for Swift project authors
88

99
swift_compiler_sources(Optimizer
10+
SimplifyAllocRefDynamic.swift
1011
SimplifyApply.swift
1112
SimplifyBeginBorrow.swift
1213
SimplifyBeginCOWMutation.swift
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
//===--- SimplifyAllocRefDynamic.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 AllocRefDynamicInst : OnoneSimplifyable {
16+
func simplify(_ context: SimplifyContext) {
17+
/// Optimize alloc_ref_dynamic of a known type to alloc_ref:
18+
///
19+
/// %3 = metatype SubClass.Type
20+
/// %4 = upcast %3 : SubClass.Type to BaseClass.Type
21+
/// %6 = alloc_ref_dynamic [...] %4 : BaseClass.Type, $BaseClass
22+
/// %8 = (... some use of ...) %6 : $BaseClass
23+
/// ->
24+
/// %6 = alloc_ref [...] $SubClass
25+
/// %7 = upcast %6 : $SubClass to $BaseClass
26+
/// %8 = (... some use of ...) %7 : $BaseClass
27+
28+
let type: Type
29+
let emitUpcast: Bool
30+
if let metatypeInst = metatypeOperand.value as? MetatypeInst {
31+
type = metatypeInst.type.loweredInstanceTypeOfMetatype(in: parentFunction)
32+
emitUpcast = false
33+
} else if let upcastInst = metatypeOperand.value as? UpcastInst,
34+
let metatypeInst = upcastInst.operands[0].value as? MetatypeInst {
35+
type = metatypeInst.type.loweredInstanceTypeOfMetatype(in: parentFunction)
36+
emitUpcast = true
37+
} else {
38+
return
39+
}
40+
41+
let builder = Builder(before: self, context)
42+
let newAlloc = builder.createAllocRef(type, isObjC: self.isObjC, canAllocOnStack: self.canAllocOnStack, isBare: false,
43+
tailAllocatedTypes: self.tailAllocatedTypes, tailAllocatedCounts: Array(self.tailAllocatedCounts.values))
44+
45+
let result: Value
46+
if emitUpcast {
47+
result = builder.createUpcast(from: newAlloc, to: self.type)
48+
} else {
49+
result = newAlloc
50+
}
51+
uses.replaceAll(with: result, context)
52+
context.erase(instruction: self)
53+
}
54+
}

SwiftCompilerSources/Sources/Optimizer/InstructionSimplification/SimplifyRefCasts.swift

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,10 @@ private func insertCompensatingInstructions(for inst: Instruction, in failureBlo
116116
let newInst: SingleValueInstruction
117117
switch inst {
118118
case let ier as InitExistentialRefInst:
119-
newInst = builder.createInitExistentialRef(instance: newArg, existentialType: ier.type, useConformancesOf: ier)
119+
newInst = builder.createInitExistentialRef(instance: newArg,
120+
existentialType: ier.type,
121+
formalConcreteType: ier.formalConcreteType,
122+
conformances: ier.conformances)
120123
case let uc as UpcastInst:
121124
newInst = builder.createUpcast(from: newArg, to: uc.type)
122125
default:

SwiftCompilerSources/Sources/Optimizer/ModulePasses/MandatoryPerformanceOptimizations.swift

Lines changed: 81 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -33,13 +33,23 @@ let mandatoryPerformanceOptimizations = ModulePass(name: "mandatory-performance-
3333
// For embedded Swift, optimize all the functions (there cannot be any
3434
// generics, type metadata, etc.)
3535
if moduleContext.options.enableEmbeddedSwift {
36+
// We need to specialize all vtables which are referenced from non-generic contexts. Beside
37+
// `alloc_ref`s of generic classes in non-generic functions, we also need to specialize generic
38+
// superclasses of non-generic classes. E.g. `class Derived : Base<Int> {}`
39+
specializeVTablesOfSuperclasses(moduleContext)
40+
3641
worklist.addAllNonGenericFunctions(of: moduleContext)
3742
} else {
3843
worklist.addAllPerformanceAnnotatedFunctions(of: moduleContext)
3944
worklist.addAllAnnotatedGlobalInitOnceFunctions(of: moduleContext)
4045
}
4146

4247
optimizeFunctionsTopDown(using: &worklist, moduleContext)
48+
49+
if moduleContext.options.enableEmbeddedSwift {
50+
// Print errors for generic functions in vtables, which is not allowed in embedded Swift.
51+
checkVTablesForGenericFunctions(moduleContext)
52+
}
4353
}
4454

4555
private func optimizeFunctionsTopDown(using worklist: inout FunctionWorklist,
@@ -92,17 +102,26 @@ private func optimize(function: Function, _ context: FunctionPassContext, _ modu
92102
// Embedded Swift specific transformations
93103
case let alloc as AllocRefInst:
94104
if context.options.enableEmbeddedSwift {
95-
specializeVTableAndAddEntriesToWorklist(for: alloc.type, in: function, context, moduleContext, &worklist)
105+
specializeVTableAndAddEntriesToWorklist(for: alloc.type, in: function,
106+
errorLocation: alloc.location,
107+
moduleContext, &worklist)
96108
}
97109
case let metatype as MetatypeInst:
98110
if context.options.enableEmbeddedSwift {
99-
specializeVTableAndAddEntriesToWorklist(for: metatype.type, in: function, context, moduleContext, &worklist)
111+
specializeVTableAndAddEntriesToWorklist(for: metatype.type, in: function,
112+
errorLocation: metatype.location,
113+
moduleContext, &worklist)
100114
}
101115
case let classMethod as ClassMethodInst:
102116
if context.options.enableEmbeddedSwift {
103117
_ = context.specializeClassMethodInst(classMethod)
104118
}
105119

120+
case let initExRef as InitExistentialRefInst:
121+
if context.options.enableEmbeddedSwift {
122+
specializeWitnessTables(for: initExRef, moduleContext, &worklist)
123+
}
124+
106125
// We need to de-virtualize deinits of non-copyable types to be able to specialize the deinitializers.
107126
case let destroyValue as DestroyValueInst:
108127
if !devirtualizeDeinits(of: destroyValue, simplifyCtxt) {
@@ -144,19 +163,24 @@ private func optimize(function: Function, _ context: FunctionPassContext, _ modu
144163
}
145164

146165
private func specializeVTableAndAddEntriesToWorklist(for type: Type, in function: Function,
147-
_ context: FunctionPassContext, _ moduleContext: ModulePassContext,
166+
errorLocation: Location,
167+
_ moduleContext: ModulePassContext,
148168
_ worklist: inout FunctionWorklist) {
149169
let vTablesCountBefore = moduleContext.vTables.count
150170

151-
guard context.specializeVTable(for: type, in: function) != nil else {
171+
guard specializeVTable(forClassType: type, errorLocation: errorLocation, moduleContext) != nil else {
152172
return
153173
}
154174

155175
// More than one new vtable might have been created (superclasses), process them all
156176
let vTables = moduleContext.vTables
157177
for i in vTablesCountBefore ..< vTables.count {
158-
for entry in vTables[i].entries {
159-
worklist.pushIfNotVisited(entry.function)
178+
for entry in vTables[i].entries
179+
// A new vtable can still contain a generic function if the method couldn't be specialized for some reason
180+
// and an error has been printed. Exclude generic functions to not run into an assert later.
181+
where !entry.implementation.isGeneric
182+
{
183+
worklist.pushIfNotVisited(entry.implementation)
160184
}
161185
}
162186
}
@@ -240,6 +264,45 @@ private func shouldInline(apply: FullApplySite, callee: Function, alreadyInlined
240264
return false
241265
}
242266

267+
private func specializeWitnessTables(for initExRef: InitExistentialRefInst, _ context: ModulePassContext,
268+
_ worklist: inout FunctionWorklist)
269+
{
270+
for conformance in initExRef.conformances where conformance.isConcrete {
271+
let origWitnessTable = context.lookupWitnessTable(for: conformance)
272+
if conformance.isSpecialized {
273+
if origWitnessTable == nil {
274+
let wt = specializeWitnessTable(forConformance: conformance, errorLocation: initExRef.location, context)
275+
worklist.addWitnessMethods(of: wt)
276+
}
277+
} else if let origWitnessTable {
278+
checkForGenericMethods(in: origWitnessTable, errorLocation: initExRef.location, context)
279+
}
280+
}
281+
}
282+
283+
private func checkForGenericMethods(in witnessTable: WitnessTable,
284+
errorLocation: Location,
285+
_ context: ModulePassContext)
286+
{
287+
for entry in witnessTable.entries where entry.kind == .method {
288+
if let method = entry.methodFunction,
289+
method.isGeneric
290+
{
291+
context.diagnosticEngine.diagnose(errorLocation.sourceLoc, .cannot_specialize_witness_method,
292+
entry.methodRequirement)
293+
return
294+
}
295+
}
296+
}
297+
298+
private func checkVTablesForGenericFunctions(_ context: ModulePassContext) {
299+
for vTable in context.vTables where !vTable.class.isGenericAtAnyLevel {
300+
for entry in vTable.entries where entry.implementation.isGeneric {
301+
context.diagnosticEngine.diagnose(entry.methodDecl.location.sourceLoc, .non_final_generic_class_function)
302+
}
303+
}
304+
}
305+
243306
private extension FullApplySite {
244307
func resultIsUsedInGlobalInitialization() -> SmallProjectionPath? {
245308
guard parentFunction.isGlobalInitOnceFunction,
@@ -445,6 +508,18 @@ fileprivate struct FunctionWorklist {
445508
}
446509
}
447510

511+
mutating func addWitnessMethods(of witnessTable: WitnessTable) {
512+
for entry in witnessTable.entries where entry.kind == .method {
513+
if let method = entry.methodFunction,
514+
// A new witness table can still contain a generic function if the method couldn't be specialized for
515+
// some reason and an error has been printed. Exclude generic functions to not run into an assert later.
516+
!method.isGeneric
517+
{
518+
pushIfNotVisited(method)
519+
}
520+
}
521+
}
522+
448523
mutating func pushIfNotVisited(_ element: Function) {
449524
if pushedFunctions.insert(element).inserted {
450525
functions.append(element)

0 commit comments

Comments
 (0)