Skip to content

MemoryLifetimeVerifier: use CalleeCache instead of AliasAnalysis #70155

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Dec 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions SwiftCompilerSources/Sources/Basic/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,5 @@
add_swift_compiler_module(Basic
SOURCES
SourceLoc.swift
StringParser.swift
Utils.swift)
75 changes: 75 additions & 0 deletions SwiftCompilerSources/Sources/Basic/StringParser.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
//===--- StringParser.swift -----------------------------------------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2023 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//

/// A simple utility to parse a string.
public struct StringParser {
private var s: Substring
private let originalLength: Int

private mutating func consumeWhitespace() {
s = s.drop { $0.isWhitespace }
}

public init(_ string: String) {
s = Substring(string)
originalLength = string.count
}

public mutating func isEmpty() -> Bool {
consumeWhitespace()
return s.isEmpty
}

public mutating func consume(_ str: String) -> Bool {
consumeWhitespace()
if !s.starts(with: str) { return false }
s = s.dropFirst(str.count)
return true
}

public mutating func consumeInt(withWhiteSpace: Bool = true) -> Int? {
if withWhiteSpace {
consumeWhitespace()
}
var intStr = ""
s = s.drop {
if $0.isNumber {
intStr.append($0)
return true
}
return false
}
return Int(intStr)
}

public mutating func consumeIdentifier() -> String? {
consumeWhitespace()
var name = ""
s = s.drop {
if $0.isLetter {
name.append($0)
return true
}
return false
}
return name.isEmpty ? nil : name
}

public func throwError(_ message: StaticString) throws -> Never {
throw ParsingError(message: message, position: originalLength - s.count)
}
}

public struct ParsingError : Error {
public let message: StaticString
public let position: Int
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@ swift_compiler_sources(Optimizer
EscapeUtils.swift
OptUtils.swift
ForwardingUtils.swift
WalkUtils.swift
AccessUtils.swift
SSAUpdater.swift
StaticInitCloner.swift
Test.swift
Expand Down
16 changes: 0 additions & 16 deletions SwiftCompilerSources/Sources/Optimizer/Utilities/OptUtils.swift
Original file line number Diff line number Diff line change
Expand Up @@ -322,22 +322,6 @@ extension LoadInst {
}
}

extension SmallProjectionPath {
/// Returns true if the path only contains projections which can be materialized as
/// SIL struct or tuple projection instructions - for values or addresses.
var isMaterializable: Bool {
let (kind, _, subPath) = pop()
switch kind {
case .root:
return true
case .structField, .tupleField:
return subPath.isMaterializable
default:
return false
}
}
}

extension FunctionPassContext {
/// Returns true if any blocks were removed.
func removeDeadBlocks(in function: Function) -> Bool {
Expand Down
6 changes: 4 additions & 2 deletions SwiftCompilerSources/Sources/SIL/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,12 @@ add_swift_compiler_module(SIL
Location.swift
Operand.swift
Registration.swift
SmallProjectionPath.swift
SILStage.swift
SubstitutionMap.swift
Type.swift
Utils.swift
Value.swift
VTable.swift
WitnessTable.swift)

add_subdirectory(Utilities)

18 changes: 18 additions & 0 deletions SwiftCompilerSources/Sources/SIL/Function.swift
Original file line number Diff line number Diff line change
Expand Up @@ -368,6 +368,24 @@ final public class Function : CustomStringConvertible, HasShortDescription, Hash
{ (f: BridgedFunction, observeRetains: Bool) -> BridgedMemoryBehavior in
let e = f.function.getSideEffects()
return e.getMemBehavior(observeRetains: observeRetains)
},
// argumentMayRead (used by the MemoryLifetimeVerifier)
{ (f: BridgedFunction, bridgedArgOp: BridgedOperand, bridgedAddr: BridgedValue) -> Bool in
let argOp = Operand(bridged: bridgedArgOp)
let addr = bridgedAddr.value
let applySite = argOp.instruction as! FullApplySite
let addrPath = addr.accessPath
let argIdx = argOp.index - ApplyOperands.firstArgumentIndex
let calleeArgIdx = applySite.calleeArgIndex(callerArgIndex: argIdx)
let convention = applySite.getArgumentConvention(calleeArgIndex: calleeArgIdx)
assert(convention.isIndirectIn || convention.isInout)
let argPath = argOp.value.accessPath
assert(!argPath.isDistinct(from: addrPath))
let path = argPath.getProjection(to: addrPath) ?? SmallProjectionPath()
let effects = f.function.getSideEffects(forArgument: argOp.value.at(path),
atIndex: calleeArgIdx,
withConvention: convention)
return effects.memory.read
}
)
}
Expand Down
40 changes: 40 additions & 0 deletions SwiftCompilerSources/Sources/SIL/SILStage.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
//===--- SILStage.swift ---------------------------------------------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2023 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//

public enum SILStage: Int {
/// "Raw" SIL, emitted by SILGen, but not yet run through guaranteed
/// optimization and diagnostic passes.
///
/// Raw SIL does not have fully-constructed SSA and may contain undiagnosed
/// dataflow errors.
case raw

/// Canonical SIL, which has been run through at least the guaranteed
/// optimization and diagnostic passes.
///
/// Canonical SIL has stricter invariants than raw SIL. It must not contain
/// dataflow errors, and some instructions must be canonicalized to simpler
/// forms.
case canonical

/// Lowered SIL, which has been prepared for IRGen and will no longer
/// be passed to canonical SIL transform passes.
///
/// In lowered SIL, the SILType of all SILValues is its SIL storage
/// type. Explicit storage is required for all address-only and resilient
/// types.
///
/// Generating the initial Raw SIL is typically referred to as lowering (from
/// the AST). To disambiguate, refer to the process of generating the lowered
/// stage of SIL as "address lowering".
case lowered
}
Loading