Skip to content

Commit 47ae9bd

Browse files
authored
Merge pull request #71723 from eeckstein/swift-verifier
SwiftCompilerSources: refactor and verify conformances to ForwardingInstruction
2 parents bfb6a58 + a13a6e0 commit 47ae9bd

File tree

23 files changed

+510
-204
lines changed

23 files changed

+510
-204
lines changed

SwiftCompilerSources/README.md

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -129,8 +129,6 @@ For example, to add a new instruction class:
129129
* if needed, add bridging functions to access the instruction's data fields.
130130

131131

132-
No yet implemented instruction classes are mapped to a "placeholder" instruction, e.g `UnimplementedInstruction`. This ensures that optimizations can process any kind of SIL, even if some instructions don't have a representation in Swift yet.
133-
134132
## The Optimizer
135133

136134
Similar to SIL, the optimizer also uses a small bridging layer (`OptimizerBridging.h`).

SwiftCompilerSources/Sources/Optimizer/PassManager/PassRegistration.swift

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import OptimizerBridging
1717
public func initializeSwiftModules() {
1818
registerSILClasses()
1919
registerSwiftAnalyses()
20+
registerUtilities()
2021
registerSwiftPasses()
2122
registerOptimizerTests()
2223
}
@@ -120,3 +121,7 @@ private func registerSwiftAnalyses() {
120121
AliasAnalysis.register()
121122
CalleeAnalysis.register()
122123
}
124+
125+
private func registerUtilities() {
126+
registerVerifier()
127+
}

SwiftCompilerSources/Sources/Optimizer/Utilities/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,5 @@ swift_compiler_sources(Optimizer
2020
SSAUpdater.swift
2121
StaticInitCloner.swift
2222
Test.swift
23+
Verifier.swift
2324
)
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
//===--- Verifier.swift ---------------------------------------------------===//
2+
//
3+
// This source file is part of the Swift.org open source project
4+
//
5+
// Copyright (c) 2014 - 2024 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+
import OptimizerBridging
15+
16+
private protocol VerifyableInstruction : Instruction {
17+
func verify(_ context: FunctionPassContext)
18+
}
19+
20+
private func require(_ condition: Bool, _ message: @autoclosure () -> String) {
21+
if !condition {
22+
fatalError(message())
23+
}
24+
}
25+
26+
extension Function {
27+
func verify(_ context: FunctionPassContext) {
28+
for block in blocks {
29+
for inst in block.instructions {
30+
31+
inst.checkForwardingConformance()
32+
33+
if let verifyableInst = inst as? VerifyableInstruction {
34+
verifyableInst.verify(context)
35+
}
36+
}
37+
}
38+
}
39+
}
40+
41+
private extension Instruction {
42+
func checkForwardingConformance() {
43+
if bridged.shouldBeForwarding() {
44+
require(self is ForwardingInstruction, "instruction \(self)\nshould conform to ForwardingInstruction")
45+
} else {
46+
require(!(self is ForwardingInstruction), "instruction \(self)\nshould not conform to ForwardingInstruction")
47+
}
48+
}
49+
}
50+
51+
func registerVerifier() {
52+
BridgedUtilities.registerVerifier(
53+
{ (bridgedCtxt: BridgedPassContext, bridgedFunction: BridgedFunction) in
54+
let context = FunctionPassContext(_bridged: bridgedCtxt)
55+
bridgedFunction.function.verify(context)
56+
}
57+
)
58+
}

0 commit comments

Comments
 (0)