Skip to content

Commit 560eb9d

Browse files
authored
Merge pull request #85954 from jckarter/sil-require-require-instruction
SIL verifier: The `atInstruction`/`atArgument` parameter to `require` should not be optional.
2 parents f9fcd88 + da4e72c commit 560eb9d

File tree

3 files changed

+42
-19
lines changed

3 files changed

+42
-19
lines changed

SwiftCompilerSources/Sources/SIL/Utilities/Verifier.swift

Lines changed: 35 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ private protocol VerifiableInstruction : Instruction {
1818
func verify(_ context: VerifierContext)
1919
}
2020

21-
private func require(_ condition: Bool, _ message: @autoclosure () -> String, atInstruction: Instruction? = nil) {
21+
private func require(_ condition: Bool, _ message: @autoclosure () -> String, atInstruction: Instruction) {
2222
if !condition {
2323
let msg = message()
2424
msg._withBridgedStringRef { stringRef in
@@ -27,6 +27,15 @@ private func require(_ condition: Bool, _ message: @autoclosure () -> String, at
2727
}
2828
}
2929

30+
private func require(_ condition: Bool, _ message: @autoclosure () -> String, atArgument: Argument) {
31+
if !condition {
32+
let msg = message()
33+
msg._withBridgedStringRef { stringRef in
34+
BridgedVerifier.verifierError(stringRef, atArgument.bridged)
35+
}
36+
}
37+
}
38+
3039
struct VerifierContext: Context {
3140
let _bridged: BridgedContext
3241
}
@@ -53,16 +62,21 @@ extension Function {
5362
private extension Instruction {
5463
func checkForwardingConformance() {
5564
if bridged.shouldBeForwarding() {
56-
require(self is ForwardingInstruction, "instruction \(self)\nshould conform to ForwardingInstruction")
65+
require(self is ForwardingInstruction,
66+
"instruction \(self)\nshould conform to ForwardingInstruction",
67+
atInstruction: self)
5768
} else {
58-
require(!(self is ForwardingInstruction), "instruction \(self)\nshould not conform to ForwardingInstruction")
69+
require(!(self is ForwardingInstruction),
70+
"instruction \(self)\nshould not conform to ForwardingInstruction",
71+
atInstruction: self)
5972
}
6073
}
6174

6275
func checkGuaranteedResults() {
6376
for result in results where result.ownership == .guaranteed {
6477
require(BeginBorrowValue(result) != nil || self is ForwardingInstruction || result.isGuaranteedApplyResult,
65-
"\(result) must either be a BeginBorrowValue or a ForwardingInstruction")
78+
"\(result) must either be a BeginBorrowValue or a ForwardingInstruction",
79+
atInstruction: self)
6680
}
6781
}
6882
}
@@ -89,22 +103,27 @@ private extension Phi {
89103
var forwardingBorrowedFromFound = false
90104
for use in value.uses {
91105
require(use.instruction is BorrowedFromInst,
92-
"guaranteed phi: \(self)\n has non borrowed-from use: \(use)")
106+
"guaranteed phi: \(self)\n has non borrowed-from use: \(use)",
107+
atArgument: self.value)
93108
if use.index == 0 {
94-
require(!forwardingBorrowedFromFound, "phi \(self) has multiple forwarding borrowed-from uses")
109+
require(!forwardingBorrowedFromFound, "phi \(self) has multiple forwarding borrowed-from uses",
110+
atArgument: self.value)
95111
forwardingBorrowedFromFound = true
96112
}
97113
}
98114
require(forwardingBorrowedFromFound,
99-
"missing forwarding borrowed-from user of guaranteed phi \(self)")
115+
"missing forwarding borrowed-from user of guaranteed phi \(self)",
116+
atArgument: self.value)
100117
}
101118
}
102119

103120
extension BorrowedFromInst : VerifiableInstruction {
104121
func verify(_ context: VerifierContext) {
105122

106123
for ev in enclosingValues {
107-
require(ev.isValidEnclosingValueInBorrowedFrom, "invalid enclosing value in borrowed-from: \(ev)")
124+
require(ev.isValidEnclosingValueInBorrowedFrom,
125+
"invalid enclosing value in borrowed-from: \(ev)",
126+
atInstruction: self)
108127
}
109128

110129
var computedEVs = Stack<Value>(context)
@@ -120,7 +139,8 @@ extension BorrowedFromInst : VerifiableInstruction {
120139

121140
for computedEV in computedEVs {
122141
require(existingEVs.contains(computedEV),
123-
"\(computedEV)\n missing in enclosing values of \(self)")
142+
"\(computedEV)\n missing in enclosing values of \(self)",
143+
atInstruction: self)
124144
}
125145
}
126146
}
@@ -189,10 +209,12 @@ extension BeginAccessInst : VerifiableInstruction {
189209
extension VectorBaseAddrInst : VerifiableInstruction {
190210
func verify(_ context: VerifierContext) {
191211
require(vector.type.isBuiltinFixedArray,
192-
"vector operand of vector_element_addr must be a Builtin.FixedArray")
212+
"vector operand of vector_element_addr must be a Builtin.FixedArray",
213+
atInstruction: self)
193214
require(type == vector.type.builtinFixedArrayElementType(in: parentFunction,
194215
maximallyAbstracted: true).addressType,
195-
"result of vector_element_addr has wrong type")
216+
"result of vector_element_addr has wrong type",
217+
atInstruction: self)
196218
}
197219
}
198220

@@ -235,7 +257,8 @@ private struct MutatingUsesWalker : AddressDefUseWalker {
235257
if let bf = phi.borrowedFrom {
236258
linearLiveranges.pushIfNotVisited(bf)
237259
} else {
238-
require(false, "missing borrowed-from for \(phi.value)")
260+
require(false, "missing borrowed-from for \(phi.value)",
261+
atArgument: phi.value)
239262
}
240263
}
241264
}

include/swift/SIL/SILBridging.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1629,11 +1629,11 @@ struct BridgedVerifier {
16291629

16301630
static void registerVerifier(VerifyFunctionFn verifyFunctionFn);
16311631
static void verifierError(BridgedStringRef message,
1632-
OptionalBridgedInstruction atInstruction);
1632+
BridgedInstruction atInstruction);
16331633
static void verifierError(BridgedStringRef message,
1634-
OptionalBridgedArgument atArgument);
1634+
BridgedArgument atArgument);
16351635
static void verifierError(BridgedStringRef message,
1636-
OptionalBridgedValue atValue);
1636+
BridgedValue atValue);
16371637
};
16381638

16391639
struct BridgedUtilities {

lib/SIL/Utils/SILBridging.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -918,19 +918,19 @@ void BridgedVerifier::runSwiftFunctionVerification(SILFunction * _Nonnull f, SIL
918918
}
919919

920920
void BridgedVerifier::verifierError(BridgedStringRef message,
921-
OptionalBridgedInstruction atInstruction) {
921+
BridgedInstruction atInstruction) {
922922
verificationFailure(message.unbridged(), atInstruction.unbridged(),
923923
/*extraContext=*/nullptr);
924924
}
925925

926926
void BridgedVerifier::verifierError(BridgedStringRef message,
927-
OptionalBridgedArgument atArgument) {
928-
verificationFailure(message.unbridged(), atArgument.unbridged(),
927+
BridgedArgument atArgument) {
928+
verificationFailure(message.unbridged(), atArgument.getArgument(),
929929
/*extraContext=*/nullptr);
930930
}
931931

932932
void BridgedVerifier::verifierError(BridgedStringRef message,
933-
OptionalBridgedValue atValue) {
933+
BridgedValue atValue) {
934934
verificationFailure(message.unbridged(), SILValue(atValue.getSILValue()),
935935
/*extraContext=*/nullptr);
936936
}

0 commit comments

Comments
 (0)