Skip to content

Commit 3b8597c

Browse files
committed
Merge branch 'main' into c++20-compatibility
2 parents e13a85c + 0798c05 commit 3b8597c

File tree

499 files changed

+10026
-4188
lines changed

Some content is hidden

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

499 files changed

+10026
-4188
lines changed

SwiftCompilerSources/CMakeLists.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -256,7 +256,6 @@ else()
256256
# The swift modules can now depend on that target.
257257
# Note that this library is unused, i.e. not linked to anything.
258258
add_library(importedHeaderDependencies "${CMAKE_CURRENT_BINARY_DIR}/HeaderDependencies.cpp")
259-
add_dependencies(importedHeaderDependencies swift-ast-generated-headers)
260259
target_include_directories(importedHeaderDependencies PRIVATE "${CMAKE_CURRENT_SOURCE_DIR}/../include/swift")
261260

262261
if(BOOTSTRAPPING_MODE MATCHES "HOSTTOOLS|CROSSCOMPILE")

SwiftCompilerSources/Package.swift

Lines changed: 12 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// swift-tools-version:5.3
1+
// swift-tools-version:5.9
22
//===--- Package.swift.in - SwiftCompiler SwiftPM package -----------------===//
33
//
44
// This source file is part of the Swift.org open source project
@@ -14,20 +14,6 @@
1414
import PackageDescription
1515

1616
private extension Target {
17-
static let defaultSwiftSettings: [SwiftSetting] = [
18-
.unsafeFlags([
19-
"-Xfrontend", "-validate-tbd-against-ir=none",
20-
"-Xfrontend", "-enable-experimental-cxx-interop",
21-
// Bridging modules and headers
22-
"-Xcc", "-I", "-Xcc", "../include",
23-
// LLVM modules and headers
24-
"-Xcc", "-I", "-Xcc", "../../llvm-project/llvm/include",
25-
// Clang modules and headers
26-
"-Xcc", "-I", "-Xcc", "../../llvm-project/clang/include",
27-
"-cross-module-optimization"
28-
]),
29-
]
30-
3117
static func compilerModuleTarget(
3218
name: String,
3319
dependencies: [Dependency],
@@ -40,7 +26,15 @@ private extension Target {
4026
path: path ?? "Sources/\(name)",
4127
exclude: ["CMakeLists.txt"],
4228
sources: sources,
43-
swiftSettings: defaultSwiftSettings + swiftSettings)
29+
cxxSettings: [
30+
.headerSearchPath("../include"),
31+
.headerSearchPath("../../llvm-project/llvm/include"),
32+
.headerSearchPath("../../llvm-project/clang/include"),
33+
],
34+
swiftSettings: [
35+
.interoperabilityMode(.Cxx),
36+
.unsafeFlags(["-cross-module-optimization"]),
37+
] + swiftSettings)
4438
}
4539
}
4640

@@ -86,5 +80,6 @@ let package = Package(
8680
.compilerModuleTarget(
8781
name: "Optimizer",
8882
dependencies: ["Basic", "SIL", "Parse"]),
89-
]
83+
],
84+
cxxLanguageStandard: .cxx17
9085
)

SwiftCompilerSources/Sources/Optimizer/FunctionPasses/AssumeSingleThreaded.swift

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,12 @@
2727

2828
import SIL
2929

30-
let assumeSingleThreadedPass = FunctionPass(
31-
name: "sil-assume-single-threaded", { function, context in
32-
for inst in function.instructions {
33-
guard let rcInst = inst as? RefCountingInst else { continue }
30+
let assumeSingleThreadedPass = FunctionPass(name: "sil-assume-single-threaded") {
31+
(function: Function, context: FunctionPassContext) in
3432

35-
rcInst.setAtomicity(isAtomic: false, context)
36-
}
33+
for inst in function.instructions {
34+
guard let rcInst = inst as? RefCountingInst else { continue }
35+
36+
rcInst.setAtomicity(isAtomic: false, context)
3737
}
38-
)
38+
}

SwiftCompilerSources/Sources/Optimizer/FunctionPasses/ComputeEscapeEffects.swift

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ import SIL
2525
/// ```
2626
/// The pass does not try to change or re-compute _defined_ effects.
2727
///
28-
let computeEscapeEffects = FunctionPass(name: "compute-escape-effects", {
28+
let computeEscapeEffects = FunctionPass(name: "compute-escape-effects") {
2929
(function: Function, context: FunctionPassContext) in
3030

3131
var newEffects = function.effects.escapeEffects.arguments.filter {!$0.isDerived }
@@ -73,8 +73,7 @@ let computeEscapeEffects = FunctionPass(name: "compute-escape-effects", {
7373
context.modifyEffects(in: function) { (effects: inout FunctionEffects) in
7474
effects.escapeEffects.arguments = newEffects
7575
}
76-
})
77-
76+
}
7877

7978
/// Returns true if an argument effect was added.
8079
private

SwiftCompilerSources/Sources/Optimizer/FunctionPasses/ComputeSideEffects.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ import SIL
2424
/// ```
2525
/// are computed.
2626
///
27-
let computeSideEffects = FunctionPass(name: "compute-side-effects", {
27+
let computeSideEffects = FunctionPass(name: "compute-side-effects") {
2828
(function: Function, context: FunctionPassContext) in
2929

3030
if function.isAvailableExternally {
@@ -71,7 +71,7 @@ let computeSideEffects = FunctionPass(name: "compute-side-effects", {
7171
context.modifyEffects(in: function) { (effects: inout FunctionEffects) in
7272
effects.sideEffects = SideEffects(arguments: collectedEffects.argumentEffects, global: collectedEffects.globalEffects)
7373
}
74-
})
74+
}
7575

7676
/// The collected argument and global side effects of the function.
7777
private struct CollectedEffects {

SwiftCompilerSources/Sources/Optimizer/FunctionPasses/ObjCBridgingOptimization.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ import SIL
3131
/// br continue_bb(%5)
3232
/// continue_bb(%bridgedOptionalSwiftValue):
3333
/// ```
34-
let objCBridgingOptimization = FunctionPass(name: "objc-bridging-opt", {
34+
let objCBridgingOptimization = FunctionPass(name: "objc-bridging-opt") {
3535
(function: Function, context: FunctionPassContext) in
3636

3737
if !function.hasOwnership { return }
@@ -54,7 +54,7 @@ let objCBridgingOptimization = FunctionPass(name: "objc-bridging-opt", {
5454
}
5555
}
5656
}
57-
})
57+
}
5858

5959
//===----------------------------------------------------------------------===//
6060
// Top-level optimization functions

SwiftCompilerSources/Sources/Optimizer/FunctionPasses/ReleaseDevirtualizer.swift

Lines changed: 29 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -30,42 +30,42 @@ import SIL
3030
/// The optimization is only done for stack promoted objects because they are
3131
/// known to have no associated objects (which are not explicitly released
3232
/// in the deinit method).
33-
let releaseDevirtualizerPass = FunctionPass(
34-
name: "release-devirtualizer", { function, context in
35-
for block in function.blocks {
36-
// The last `release_value`` or `strong_release`` instruction before the
37-
// deallocation.
38-
var lastRelease: RefCountingInst?
39-
40-
for instruction in block.instructions {
41-
if let release = lastRelease {
42-
// We only do the optimization for stack promoted object, because for
43-
// these we know that they don't have associated objects, which are
44-
// _not_ released by the deinit method.
45-
if let deallocStackRef = instruction as? DeallocStackRefInst {
46-
if !context.continueWithNextSubpassRun(for: release) {
47-
return
48-
}
49-
tryDevirtualizeReleaseOfObject(context, release, deallocStackRef)
50-
lastRelease = nil
51-
continue
33+
let releaseDevirtualizerPass = FunctionPass(name: "release-devirtualizer") {
34+
(function: Function, context: FunctionPassContext) in
35+
36+
for block in function.blocks {
37+
// The last `release_value`` or `strong_release`` instruction before the
38+
// deallocation.
39+
var lastRelease: RefCountingInst?
40+
41+
for instruction in block.instructions {
42+
if let release = lastRelease {
43+
// We only do the optimization for stack promoted object, because for
44+
// these we know that they don't have associated objects, which are
45+
// _not_ released by the deinit method.
46+
if let deallocStackRef = instruction as? DeallocStackRefInst {
47+
if !context.continueWithNextSubpassRun(for: release) {
48+
return
5249
}
50+
tryDevirtualizeReleaseOfObject(context, release, deallocStackRef)
51+
lastRelease = nil
52+
continue
5353
}
54+
}
5455

55-
switch instruction {
56-
case is ReleaseValueInst, is StrongReleaseInst:
57-
lastRelease = instruction as? RefCountingInst
58-
case is DeallocRefInst, is SetDeallocatingInst:
56+
switch instruction {
57+
case is ReleaseValueInst, is StrongReleaseInst:
58+
lastRelease = instruction as? RefCountingInst
59+
case is DeallocRefInst, is SetDeallocatingInst:
60+
lastRelease = nil
61+
default:
62+
if instruction.mayRelease {
5963
lastRelease = nil
60-
default:
61-
if instruction.mayRelease {
62-
lastRelease = nil
63-
}
64-
}
64+
}
6565
}
6666
}
6767
}
68-
)
68+
}
6969

7070
/// Tries to de-virtualize the final release of a stack-promoted object.
7171
private func tryDevirtualizeReleaseOfObject(

SwiftCompilerSources/Sources/Optimizer/InstructionSimplification/SimplifyBuiltin.swift

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,10 @@ extension BuiltinInst : OnoneSimplifyable {
2828
optimizeCanBeClass(context)
2929
case .AssertConf:
3030
optimizeAssertConfig(context)
31+
case .Sizeof,
32+
.Strideof,
33+
.Alignof:
34+
optimizeTargetTypeConst(context)
3135
default:
3236
if let literal = constantFold(context) {
3337
uses.replaceAll(with: literal, context)
@@ -131,6 +135,33 @@ private extension BuiltinInst {
131135
uses.replaceAll(with: literal, context)
132136
context.erase(instruction: self)
133137
}
138+
139+
func optimizeTargetTypeConst(_ context: SimplifyContext) {
140+
guard let ty = substitutionMap.replacementTypes[0] else {
141+
return
142+
}
143+
144+
let value: Int?
145+
switch id {
146+
case .Sizeof:
147+
value = ty.getStaticSize(context: context)
148+
case .Strideof:
149+
value = ty.getStaticStride(context: context)
150+
case .Alignof:
151+
value = ty.getStaticAlignment(context: context)
152+
default:
153+
fatalError()
154+
}
155+
156+
guard let value else {
157+
return
158+
}
159+
160+
let builder = Builder(before: self, context)
161+
let literal = builder.createIntegerLiteral(value, type: type)
162+
uses.replaceAll(with: literal, context)
163+
context.erase(instruction: self)
164+
}
134165
}
135166

136167
private func hasSideEffectForBuiltinOnce(_ instruction: Instruction) -> Bool {

SwiftCompilerSources/Sources/Optimizer/ModulePasses/StackProtection.swift

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -38,22 +38,22 @@ private func log(_ message: @autoclosure () -> String) {
3838
/// inter-procedural analysis. If this is not possible and the `enableMoveInoutStackProtection`
3939
/// option is set, the fallback is to move the argument into a temporary `alloc_stack`
4040
/// and do the unsafe pointer operations on the temporary.
41-
let stackProtection = ModulePass(name: "stack-protection", {
42-
(context: ModulePassContext) in
41+
let stackProtection = ModulePass(name: "stack-protection") {
42+
(context: ModulePassContext) in
4343

4444
if !context.options.enableStackProtection {
4545
return
4646
}
4747

4848
var optimization = StackProtectionOptimization(enableMoveInout: context.options.enableMoveInoutStackProtection)
4949
optimization.processModule(context)
50-
})
50+
}
5151

5252
/// The stack-protection optimization on function-level.
5353
///
5454
/// In contrast to the `stack-protection` pass, this pass doesn't do any inter-procedural
5555
/// analysis. It runs at Onone.
56-
let functionStackProtection = FunctionPass(name: "function-stack-protection", {
56+
let functionStackProtection = FunctionPass(name: "function-stack-protection") {
5757
(function: Function, context: FunctionPassContext) in
5858

5959
if !context.options.enableStackProtection {
@@ -62,7 +62,7 @@ let functionStackProtection = FunctionPass(name: "function-stack-protection", {
6262

6363
var optimization = StackProtectionOptimization(enableMoveInout: context.options.enableMoveInoutStackProtection)
6464
optimization.process(function: function, context)
65-
})
65+
}
6666

6767
/// The optimization algorithm.
6868
private struct StackProtectionOptimization {

SwiftCompilerSources/Sources/Optimizer/PassManager/Context.swift

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -279,6 +279,23 @@ struct SimplifyContext : MutatingContext {
279279
let preserveDebugInfo: Bool
280280
}
281281

282+
extension Type {
283+
func getStaticSize(context: SimplifyContext) -> Int? {
284+
let v = context._bridged.getStaticSize(self.bridged)
285+
return v == -1 ? nil : v
286+
}
287+
288+
func getStaticAlignment(context: SimplifyContext) -> Int? {
289+
let v = context._bridged.getStaticAlignment(self.bridged)
290+
return v == -1 ? nil : v
291+
}
292+
293+
func getStaticStride(context: SimplifyContext) -> Int? {
294+
let v = context._bridged.getStaticStride(self.bridged)
295+
return v == -1 ? nil : v
296+
}
297+
}
298+
282299
//===----------------------------------------------------------------------===//
283300
// Builder initialization
284301
//===----------------------------------------------------------------------===//

SwiftCompilerSources/Sources/Optimizer/TestPasses/AccessDumper.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ import SIL
1919
/// dumps anything, but aborts if the result is wrong.
2020
///
2121
/// This pass is used for testing `AccessUtils`.
22-
let accessDumper = FunctionPass(name: "dump-access", {
22+
let accessDumper = FunctionPass(name: "dump-access") {
2323
(function: Function, context: FunctionPassContext) in
2424
print("Accesses for \(function.name)")
2525

@@ -46,7 +46,7 @@ let accessDumper = FunctionPass(name: "dump-access", {
4646
}
4747

4848
print("End accesses for \(function.name)")
49-
})
49+
}
5050

5151
private struct AccessStoragePathVisitor : ValueUseDefWalker {
5252
var walkUpCache = WalkerCache<Path>()

SwiftCompilerSources/Sources/Optimizer/TestPasses/DeadEndBlockDumper.swift

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,14 @@
1212

1313
import SIL
1414

15-
let deadEndBlockDumper = FunctionPass(name: "dump-deadendblocks", {
15+
let deadEndBlockDumper = FunctionPass(name: "dump-deadendblocks") {
1616
(function: Function, context: FunctionPassContext) in
1717

18-
1918
print("Function \(function.name)")
2019

2120
var deadEndBlocks = DeadEndBlocks(function: function, context)
2221
print(deadEndBlocks)
2322
defer { deadEndBlocks.deinitialize() }
2423

2524
print("end function \(function.name)")
26-
})
25+
}

SwiftCompilerSources/Sources/Optimizer/TestPasses/EscapeInfoDumper.swift

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import SIL
1717
/// Dumps the EscapeInfo query results for all `alloc_stack` instructions in a function.
1818
///
1919
/// This pass is used for testing EscapeInfo.
20-
let escapeInfoDumper = FunctionPass(name: "dump-escape-info", {
20+
let escapeInfoDumper = FunctionPass(name: "dump-escape-info") {
2121
(function: Function, context: FunctionPassContext) in
2222

2323
print("Escape information for \(function.name):")
@@ -60,16 +60,15 @@ let escapeInfoDumper = FunctionPass(name: "dump-escape-info", {
6060
}
6161
}
6262
print("End function \(function.name)\n")
63-
})
64-
63+
}
6564

6665
/// Dumps the results of address-related escape analysis.
6766
///
6867
/// Dumps the EscapeInfo query results for addresses escaping to function calls.
6968
/// The `fix_lifetime` instruction is used as marker for addresses and values to query.
7069
///
7170
/// This pass is used for testing EscapeInfo.
72-
let addressEscapeInfoDumper = FunctionPass(name: "dump-addr-escape-info", {
71+
let addressEscapeInfoDumper = FunctionPass(name: "dump-addr-escape-info") {
7372
(function: Function, context: FunctionPassContext) in
7473

7574
print("Address escape information for \(function.name):")
@@ -159,4 +158,4 @@ let addressEscapeInfoDumper = FunctionPass(name: "dump-addr-escape-info", {
159158
}
160159

161160
print("End function \(function.name)\n")
162-
})
161+
}

SwiftCompilerSources/Sources/Optimizer/TestPasses/FunctionUsesDumper.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212

1313
import SIL
1414

15-
let functionUsesDumper = ModulePass(name: "dump-function-uses", {
15+
let functionUsesDumper = ModulePass(name: "dump-function-uses") {
1616
(context: ModulePassContext) in
1717

1818
var functionUses = FunctionUses()
@@ -25,4 +25,4 @@ let functionUsesDumper = ModulePass(name: "dump-function-uses", {
2525
print(uses)
2626
print("End function \(function.name)\n")
2727
}
28-
})
28+
}

0 commit comments

Comments
 (0)