Skip to content

Commit 0d195d7

Browse files
authored
Merge pull request #40195 from atrick/verify-rawrebound
SILVerifier - allow rebind_memory token to cross functions
2 parents b6b9006 + 6b15585 commit 0d195d7

File tree

4 files changed

+32
-6
lines changed

4 files changed

+32
-6
lines changed

lib/IRGen/IRGenSIL.cpp

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6789,9 +6789,15 @@ void IRGenSILFunction::visitCopyAddrInst(swift::CopyAddrInst *i) {
67896789
// bind_memory and rebind_memory are no-ops because Swift TBAA info is not
67906790
// lowered to LLVM IR TBAA, and the output token is ignored except for
67916791
// verification.
6792-
void IRGenSILFunction::visitBindMemoryInst(swift::BindMemoryInst *) {}
6792+
void IRGenSILFunction::visitBindMemoryInst(swift::BindMemoryInst *i) {
6793+
LoweredValue &token = getUndefLoweredValue(i->getType());
6794+
setLoweredValue(i, std::move(token));
6795+
}
67936796

6794-
void IRGenSILFunction::visitRebindMemoryInst(swift::RebindMemoryInst *) {}
6797+
void IRGenSILFunction::visitRebindMemoryInst(swift::RebindMemoryInst *i) {
6798+
LoweredValue &token = getUndefLoweredValue(i->getType());
6799+
setLoweredValue(i, std::move(token));
6800+
}
67956801

67966802
void IRGenSILFunction::visitDestroyAddrInst(swift::DestroyAddrInst *i) {
67976803
SILType addrTy = i->getOperand()->getType();

lib/SIL/Utils/MemAccessUtils.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2387,6 +2387,7 @@ void swift::visitAccessedAddress(SILInstruction *I,
23872387
case SILInstructionKind::EndCOWMutationInst:
23882388
case SILInstructionKind::BeginUnpairedAccessInst:
23892389
case SILInstructionKind::BindMemoryInst:
2390+
case SILInstructionKind::RebindMemoryInst:
23902391
case SILInstructionKind::CheckedCastValueBranchInst:
23912392
case SILInstructionKind::CondFailInst:
23922393
case SILInstructionKind::CopyBlockInst:

lib/SIL/Verifier/SILVerifier.cpp

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3046,9 +3046,6 @@ class SILVerifier : public SILVerifierBase<SILVerifier> {
30463046
requireSameType(rbi->getInToken()->getType(),
30473047
SILType::getBuiltinWordType(F.getASTContext()),
30483048
"rebind_memory token must be a Builtin.Int64");
3049-
require(isa<BindMemoryInst>(rbi->getInToken())
3050-
|| isa<RebindMemoryInst>(rbi->getInToken()),
3051-
"rebind_memory token must originate from bind_memory");
30523049
}
30533050

30543051
void checkIndexAddrInst(IndexAddrInst *IAI) {

test/SILOptimizer/pointer_conversion.swift

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
1-
// RUN: %target-swift-frontend -emit-sil -O %s | %FileCheck %s
1+
// RUN: %target-swift-frontend -parse-stdlib -parse-as-library -emit-sil -O %s | %FileCheck %s
22
// REQUIRES: optimized_stdlib,swift_stdlib_no_asserts
33

4+
import Swift
5+
46
// Opaque, unoptimizable functions to call.
57
@_silgen_name("takesConstRawPointer")
68
func takesConstRawPointer(_ x: UnsafeRawPointer)
@@ -99,3 +101,23 @@ public func arrayLiteralPromotion() {
99101
// CHECK: apply [[FN]]([[PTR]])
100102
}
101103
104+
// Test sil verification at -O with bind_memory and rebind_memory
105+
// where the rebind is in a defer block.
106+
//
107+
// CHECK-LABEL: sil @$s18pointer_conversion21testWithMemoryRebound6rawPtr2to8capacity_q_Bp_xmSiq_SPyxGKXEtKr0_lF
108+
// CHECK: [[BIND:%.*]] = bind_memory %1 : $Builtin.RawPointer, %{{.*}} : $Builtin.Word to $*T
109+
// CHECK: rebind_memory %1 : $Builtin.RawPointer to [[BIND]] : $Builtin.Word
110+
// CHECK-LABEL: } // end sil function '$s18pointer_conversion21testWithMemoryRebound6rawPtr2to8capacity_q_Bp_xmSiq_SPyxGKXEtKr0_lF'
111+
public func testWithMemoryRebound<T, Result>(
112+
rawPtr: Builtin.RawPointer,
113+
to type: T.Type,
114+
capacity count: Int,
115+
_ body: (_ pointer: UnsafePointer<T>) throws -> Result
116+
) rethrows -> Result {
117+
let binding =
118+
Builtin.bindMemory(rawPtr, count._builtinWordValue, T.self)
119+
defer {
120+
Builtin.rebindMemory(rawPtr, binding)
121+
}
122+
return try body(.init(rawPtr))
123+
}

0 commit comments

Comments
 (0)