Skip to content

Commit 90815f4

Browse files
authored
Merge pull request swiftlang#30351 from atrick/fix-escape-assert
AliasAnalysis: add a check for address-type builtin arguments
2 parents 4febc24 + 9bf4386 commit 90815f4

File tree

2 files changed

+49
-2
lines changed

2 files changed

+49
-2
lines changed

lib/SILOptimizer/Analysis/AliasAnalysis.cpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -701,8 +701,11 @@ bool AliasAnalysis::canBuiltinDecrementRefCount(BuiltinInst *BI, SILValue Ptr) {
701701
continue;
702702

703703
// A builtin can only release an object if it can escape to one of the
704-
// builtin's arguments.
705-
if (EA->mayReleaseContent(Arg, Ptr))
704+
// builtin's arguments. 'EscapeAnalysis::mayReleaseContent()' expects 'Arg'
705+
// to be an owned reference and disallows addresses. Conservatively handle
706+
// address type arguments as and conservatively treat all other values
707+
// potential owned references.
708+
if (Arg->getType().isAddress() || EA->mayReleaseContent(Arg, Ptr))
706709
return true;
707710
}
708711
return false;

test/SILOptimizer/arcsequenceopts.sil

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2251,3 +2251,47 @@ bb0:
22512251
%9999 = tuple()
22522252
return %9999 : $()
22532253
}
2254+
2255+
//===----------------------------------------------------------------------===//
2256+
// Test invoking EscapeAnalysis:mayReleaseContent on a builtin that takes
2257+
// an address. This should simply not assert.
2258+
//
2259+
// <rdar://problem/60190962> Escape analysis crashes with
2260+
// "an address is never a reference" error with -O -thread=sanitize
2261+
//===----------------------------------------------------------------------===//
2262+
2263+
// bufferMemory
2264+
sil_global hidden @$s4test12bufferMemorySpys5UInt8VGvp : $UnsafeMutablePointer<UInt8>
2265+
2266+
// x
2267+
sil_global hidden @$s4test1xypvp : $Any
2268+
2269+
sil [serialized] [always_inline] [readonly] [_semantics "string.makeUTF8"] @$sSS21_builtinStringLiteral17utf8CodeUnitCount7isASCIISSBp_BwBi1_tcfC : $@convention(method) (Builtin.RawPointer, Builtin.Word, Builtin.Int1, @thin String.Type) -> @owned String
2270+
2271+
// CHECK-LABEL: sil @testTsanInoutAccess : $@convention(thin) () -> () {
2272+
// CHECK: alloc_global @$s4test12bufferMemorySpys5UInt8VGvp
2273+
// CHECK: [[GLOBAL:%.*]] = global_addr @$s4test12bufferMemorySpys5UInt8VGvp : $*UnsafeMutablePointer<UInt8>
2274+
// CHECK: [[ACCESS:%.*]] = begin_access [modify] [dynamic] [[GLOBAL]] : $*UnsafeMutablePointer<UInt8>
2275+
// CHECK: builtin "tsanInoutAccess"([[ACCESS]] : $*UnsafeMutablePointer<UInt8>) : $()
2276+
// CHECK-LABEL: } // end sil function 'testTsanInoutAccess'
2277+
sil @testTsanInoutAccess : $@convention(thin) () -> () {
2278+
bb0:
2279+
alloc_global @$s4test12bufferMemorySpys5UInt8VGvp
2280+
%1 = global_addr @$s4test12bufferMemorySpys5UInt8VGvp : $*UnsafeMutablePointer<UInt8>
2281+
%2 = integer_literal $Builtin.Int1, -1
2282+
alloc_global @$s4test1xypvp
2283+
%4 = global_addr @$s4test1xypvp : $*Any
2284+
%5 = string_literal utf8 ""
2285+
%6 = integer_literal $Builtin.Word, 0
2286+
%7 = metatype $@thin String.Type
2287+
// function_ref String.init(_builtinStringLiteral:utf8CodeUnitCount:isASCII:)
2288+
%8 = function_ref @$sSS21_builtinStringLiteral17utf8CodeUnitCount7isASCIISSBp_BwBi1_tcfC : $@convention(method) (Builtin.RawPointer, Builtin.Word, Builtin.Int1, @thin String.Type) -> @owned String
2289+
%9 = apply %8(%5, %6, %2, %7) : $@convention(method) (Builtin.RawPointer, Builtin.Word, Builtin.Int1, @thin String.Type) -> @owned String
2290+
%10 = init_existential_addr %4 : $*Any, $String
2291+
store %9 to %10 : $*String
2292+
%12 = begin_access [modify] [dynamic] %1 : $*UnsafeMutablePointer<UInt8>
2293+
%13 = builtin "tsanInoutAccess"(%12 : $*UnsafeMutablePointer<UInt8>) : $()
2294+
end_access %12 : $*UnsafeMutablePointer<UInt8>
2295+
%15 = tuple ()
2296+
return %15 : $()
2297+
}

0 commit comments

Comments
 (0)