Skip to content

Commit 319f49a

Browse files
committed
SwiftCompilerSources: move the Verifier to the SIL module
1 parent 1eb49ec commit 319f49a

File tree

9 files changed

+69
-42
lines changed

9 files changed

+69
-42
lines changed

SwiftCompilerSources/Sources/Optimizer/PassManager/PassRegistration.swift

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import OptimizerBridging
1717
@_cdecl("initializeSwiftModules")
1818
public func initializeSwiftModules() {
1919
registerAST()
20-
registerSILClasses()
20+
registerSIL()
2121
registerSwiftAnalyses()
2222
registerUtilities()
2323
registerSwiftPasses()
@@ -160,6 +160,5 @@ private func registerSwiftAnalyses() {
160160
}
161161

162162
private func registerUtilities() {
163-
registerVerifier()
164163
registerPhiUpdater()
165164
}

SwiftCompilerSources/Sources/Optimizer/Utilities/CMakeLists.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,5 +20,4 @@ swift_compiler_sources(Optimizer
2020
OwnershipLiveness.swift
2121
PhiUpdater.swift
2222
StaticInitCloner.swift
23-
Verifier.swift
2423
)

SwiftCompilerSources/Sources/SIL/Registration.swift

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,19 @@
1313
import Basic
1414
import SILBridging
1515

16+
public func registerSIL() {
17+
registerSILClasses()
18+
registerUtilities()
19+
}
20+
1621
private func register<T: AnyObject>(_ cl: T.Type) {
1722
"\(cl)"._withBridgedStringRef { nameStr in
1823
let metatype = unsafeBitCast(cl, to: SwiftMetatype.self)
1924
registerBridgedClass(nameStr, metatype)
2025
}
2126
}
2227

23-
public func registerSILClasses() {
28+
private func registerSILClasses() {
2429
Function.register()
2530
register(BasicBlock.self)
2631
register(GlobalVariable.self)
@@ -262,3 +267,7 @@ public func registerSILClasses() {
262267
register(MergeIsolationRegionInst.self)
263268
register(IgnoredUseInst.self)
264269
}
270+
271+
private func registerUtilities() {
272+
registerVerifier()
273+
}

SwiftCompilerSources/Sources/SIL/Utilities/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,5 +15,6 @@ swift_compiler_sources(SIL
1515
SSAUpdater.swift
1616
Test.swift
1717
WalkUtils.swift
18+
Verifier.swift
1819
)
1920

SwiftCompilerSources/Sources/Optimizer/Utilities/Verifier.swift renamed to SwiftCompilerSources/Sources/SIL/Utilities/Verifier.swift

Lines changed: 23 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -10,24 +10,29 @@
1010
//
1111
//===----------------------------------------------------------------------===//
1212

13-
import SIL
14-
import OptimizerBridging
13+
import SILBridging
1514

15+
/// To add verification for a specific instruction, let the instruction class conform to
16+
/// this protocol and implement the `verify` method.
1617
private protocol VerifiableInstruction : Instruction {
17-
func verify(_ context: FunctionPassContext)
18+
func verify(_ context: VerifierContext)
1819
}
1920

2021
private func require(_ condition: Bool, _ message: @autoclosure () -> String, atInstruction: Instruction? = nil) {
2122
if !condition {
2223
let msg = message()
2324
msg._withBridgedStringRef { stringRef in
24-
verifierError(stringRef, atInstruction.bridged, Optional<Argument>.none.bridged)
25+
BridgedVerifier.verifierError(stringRef, atInstruction.bridged, Optional<Argument>.none.bridged)
2526
}
2627
}
2728
}
2829

30+
struct VerifierContext: Context {
31+
let _bridged: BridgedContext
32+
}
33+
2934
extension Function {
30-
func verify(_ context: FunctionPassContext) {
35+
func verify(_ context: VerifierContext) {
3136
for block in blocks {
3237
for arg in block.arguments {
3338
arg.verify(context)
@@ -63,16 +68,17 @@ private extension Instruction {
6368
}
6469

6570
private extension Argument {
66-
func verify(_ context: FunctionPassContext) {
71+
func verify(_ context: VerifierContext) {
6772
if let phi = Phi(self), phi.value.ownership == .guaranteed {
6873

6974
phi.verifyBorrowedFromUse()
7075

71-
require(phi.isReborrow == phi.hasBorrowEndingUse ||
72-
// In a dead-end block an end_borrow might have been deleted.
73-
// TODO: this check is not needed anymore once we have complete OSSA lifetimes.
74-
(isReborrow && context.deadEndBlocks.isDeadEnd(parentBlock)),
76+
// TODO: enable this check once we have complete OSSA lifetimes.
77+
// In a dead-end block an end_borrow might have been deleted.
78+
/*
79+
require(phi.isReborrow == phi.hasBorrowEndingUse,
7580
"\(self) has stale reborrow flag");
81+
*/
7682
}
7783
}
7884

@@ -95,7 +101,7 @@ private extension Phi {
95101
}
96102

97103
extension BorrowedFromInst : VerifiableInstruction {
98-
func verify(_ context: FunctionPassContext) {
104+
func verify(_ context: VerifierContext) {
99105

100106
for ev in enclosingValues {
101107
require(ev.isValidEnclosingValueInBorrowedFrom, "invalid enclosing value in borrowed-from: \(ev)")
@@ -135,7 +141,7 @@ private extension Value {
135141
}
136142

137143
extension LoadBorrowInst : VerifiableInstruction {
138-
func verify(_ context: FunctionPassContext) {
144+
func verify(_ context: VerifierContext) {
139145
if isUnchecked {
140146
return
141147
}
@@ -149,7 +155,7 @@ extension LoadBorrowInst : VerifiableInstruction {
149155
}
150156

151157
extension VectorBaseAddrInst : VerifiableInstruction {
152-
func verify(_ context: FunctionPassContext) {
158+
func verify(_ context: VerifierContext) {
153159
require(vector.type.isBuiltinFixedArray,
154160
"vector operand of vector_element_addr must be a Builtin.FixedArray")
155161
require(type == vector.type.builtinFixedArrayElementType(in: parentFunction,
@@ -164,10 +170,10 @@ extension VectorBaseAddrInst : VerifiableInstruction {
164170
// Otherwise the risk would be too big for false alarms. It also means that this verification is not perfect and
165171
// might miss some subtle violations.
166172
private struct MutatingUsesWalker : AddressDefUseWalker {
167-
let context: FunctionPassContext
173+
let context: VerifierContext
168174
var mutatingInstructions: InstructionSet
169175

170-
init(_ context: FunctionPassContext) {
176+
init(_ context: VerifierContext) {
171177
self.context = context
172178
self.mutatingInstructions = InstructionSet(context)
173179
}
@@ -268,9 +274,9 @@ private extension Operand {
268274
}
269275

270276
func registerVerifier() {
271-
BridgedUtilities.registerVerifier(
277+
BridgedVerifier.registerVerifier(
272278
{ (bridgedCtxt: BridgedContext, bridgedFunction: BridgedFunction) in
273-
let context = FunctionPassContext(_bridged: bridgedCtxt)
279+
let context = VerifierContext(_bridged: bridgedCtxt)
274280
bridgedFunction.function.verify(context)
275281
}
276282
)

include/swift/SIL/SILBridging.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1494,6 +1494,16 @@ struct BridgedContext {
14941494
SWIFT_IMPORT_UNSAFE BRIDGED_INLINE Slab freeSlab(Slab slab) const;
14951495
};
14961496

1497+
struct BridgedVerifier {
1498+
typedef void (* _Nonnull VerifyFunctionFn)(BridgedContext, BridgedFunction);
1499+
1500+
static void runSwiftFunctionVerification(swift::SILFunction * _Nonnull f, swift::SILContext * _Nonnull context);
1501+
1502+
static void registerVerifier(VerifyFunctionFn verifyFunctionFn);
1503+
static void verifierError(BridgedStringRef message, OptionalBridgedInstruction atInstruction,
1504+
OptionalBridgedArgument atArgument);
1505+
};
1506+
14971507
namespace swift::test {
14981508
struct Arguments;
14991509
class FunctionTest;

include/swift/SILOptimizer/OptimizerBridging.h

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -120,11 +120,9 @@ struct BridgedPostDomTree {
120120
};
121121

122122
struct BridgedUtilities {
123-
typedef void (* _Nonnull VerifyFunctionFn)(BridgedContext, BridgedFunction);
124123
typedef void (* _Nonnull UpdateFunctionFn)(BridgedContext, BridgedFunction);
125124
typedef void (* _Nonnull UpdatePhisFn)(BridgedContext, BridgedArrayRef);
126125

127-
static void registerVerifier(VerifyFunctionFn verifyFunctionFn);
128126
static void registerPhiUpdater(UpdateFunctionFn updateBorrowedFromFn,
129127
UpdatePhisFn updateBorrowedFromPhisFn,
130128
UpdatePhisFn replacePhisWithIncomingValuesFn);
@@ -279,8 +277,6 @@ BridgedDynamicCastResult classifyDynamicCastBridged(BridgedCanType sourceTy, Bri
279277

280278
BridgedDynamicCastResult classifyDynamicCastBridged(BridgedInstruction inst);
281279

282-
void verifierError(BridgedStringRef message, OptionalBridgedInstruction atInstruction, OptionalBridgedArgument atArgument);
283-
284280
//===----------------------------------------------------------------------===//
285281
// Pass registration
286282
//===----------------------------------------------------------------------===//

lib/SIL/Utils/SILBridging.cpp

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -775,3 +775,26 @@ void SILContext::freeOperandSet(OperandSet *set) {
775775
}
776776
}
777777

778+
//===----------------------------------------------------------------------===//
779+
// BridgedVerifier
780+
//===----------------------------------------------------------------------===//
781+
782+
static BridgedVerifier::VerifyFunctionFn verifyFunctionFunction = nullptr;
783+
784+
void BridgedVerifier::registerVerifier(VerifyFunctionFn verifyFunctionFn) {
785+
verifyFunctionFunction = verifyFunctionFn;
786+
}
787+
788+
void BridgedVerifier::runSwiftFunctionVerification(SILFunction * _Nonnull f, SILContext * _Nonnull context) {
789+
if (!verifyFunctionFunction)
790+
return;
791+
792+
verifyFunctionFunction({context}, {f});
793+
}
794+
795+
void BridgedVerifier::verifierError(BridgedStringRef message,
796+
OptionalBridgedInstruction atInstruction,
797+
OptionalBridgedArgument atArgument) {
798+
Twine msg(message.unbridged());
799+
verificationFailure(msg, atInstruction.unbridged(), atArgument.unbridged(), /*extraContext=*/nullptr);
800+
}

lib/SILOptimizer/Utils/OptimizerBridging.cpp

Lines changed: 1 addition & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -49,16 +49,7 @@ llvm::cl::opt<bool> DisableSwiftVerification(
4949
// PassManager
5050
//===----------------------------------------------------------------------===//
5151

52-
static BridgedUtilities::VerifyFunctionFn verifyFunctionFunction;
53-
54-
void BridgedUtilities::registerVerifier(VerifyFunctionFn verifyFunctionFn) {
55-
verifyFunctionFunction = verifyFunctionFn;
56-
}
57-
5852
void SILPassManager::runSwiftFunctionVerification(SILFunction *f) {
59-
if (!verifyFunctionFunction)
60-
return;
61-
6253
if (f->getModule().getOptions().VerifyNone)
6354
return;
6455

@@ -70,7 +61,7 @@ void SILPassManager::runSwiftFunctionVerification(SILFunction *f) {
7061
}
7162

7263
getSwiftPassInvocation()->beginVerifyFunction(f);
73-
verifyFunctionFunction({getSwiftPassInvocation()}, {f});
64+
BridgedVerifier::runSwiftFunctionVerification(f, getSwiftPassInvocation());
7465
getSwiftPassInvocation()->endVerifyFunction();
7566
}
7667

@@ -507,13 +498,6 @@ void BridgedBuilder::destroyCapturedArgs(BridgedInstruction partialApply) const
507498
}
508499
}
509500

510-
void verifierError(BridgedStringRef message,
511-
OptionalBridgedInstruction atInstruction,
512-
OptionalBridgedArgument atArgument) {
513-
Twine msg(message.unbridged());
514-
verificationFailure(msg, atInstruction.unbridged(), atArgument.unbridged(), /*extraContext=*/nullptr);
515-
}
516-
517501
//===----------------------------------------------------------------------===//
518502
// Test
519503
//===----------------------------------------------------------------------===//

0 commit comments

Comments
 (0)