Skip to content

Commit 76b30e3

Browse files
committed
SILVerifier - allow rebind_memory token to cross functions
Remove a completely unnecessary assertion to handle reasonable use cases.
1 parent 3eba144 commit 76b30e3

File tree

3 files changed

+24
-4
lines changed

3 files changed

+24
-4
lines changed

lib/SIL/Utils/MemAccessUtils.cpp

+1
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

-3
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

+23-1
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)