Skip to content

Commit

Permalink
Merge pull request #78283 from atrick/remove-trivial-feature
Browse files Browse the repository at this point in the history
Remove the experimental LifetimeDependenceDiagnoseTrivial feature.
  • Loading branch information
atrick authored Dec 19, 2024
2 parents a3d9776 + 5581ab8 commit c347b66
Show file tree
Hide file tree
Showing 44 changed files with 91 additions and 100 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -101,10 +101,11 @@ let lifetimeDependenceDiagnosticsPass = FunctionPass(
private func analyze(dependence: LifetimeDependence, _ context: FunctionPassContext) -> Bool {
log("Dependence scope:\n\(dependence)")

// Early versions of Span in the standard library violate trivial lifetimes. Contemporary versions of the compiler
// simply ignored dependencies on trivial values.
if !context.options.hasFeature(.LifetimeDependenceDiagnoseTrivial) {
if dependence.parentValue.type.objectType.isTrivial(in: dependence.function) {
// Briefly, some versions of Span in the standard library violated trivial lifetimes; versions of the compiler built
// at that time simply ignored dependencies on trivial values. For now, disable trivial dependencies to allow newer
// compilers to build against those older standard libraries. This check is only relevant for ~6 mo (until July 2025).
if dependence.parentValue.type.objectType.isTrivial(in: dependence.function) {
if let sourceFileKind = dependence.function.sourceFileKind, sourceFileKind == .interface {
return true
}
}
Expand Down
21 changes: 21 additions & 0 deletions SwiftCompilerSources/Sources/SIL/Function.swift
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,27 @@ final public class Function : CustomStringConvertible, HasShortDescription, Hash
fatalError()
}
}

public enum SourceFileKind {
case library /// A normal .swift file.
case main /// A .swift file that can have top-level code.
case sil /// Came from a .sil file.
case interface /// Came from a .swiftinterface file, representing another module.
case macroExpansion /// Came from a macro expansion.
case defaultArgument /// Came from default argument at caller side
};

public var sourceFileKind: SourceFileKind? {
switch bridged.getSourceFileKind() {
case .Library: return .library
case .Main: return .main
case .SIL: return .sil
case .Interface: return .interface
case .MacroExpansion: return .macroExpansion
case .DefaultArgument: return .defaultArgument
case .None: return nil
}
}
}

public func == (lhs: Function, rhs: Function) -> Bool { lhs === rhs }
Expand Down
3 changes: 0 additions & 3 deletions include/swift/Basic/Features.def
Original file line number Diff line number Diff line change
Expand Up @@ -362,9 +362,6 @@ EXPERIMENTAL_FEATURE(StructLetDestructuring, true)
/// Enable returning non-escapable types from functions.
EXPERIMENTAL_FEATURE(LifetimeDependence, true)

/// Enable LifetimeDependence diagnostics for trivial types.
EXPERIMENTAL_FEATURE(LifetimeDependenceDiagnoseTrivial, false)

/// Enable the `@_staticExclusiveOnly` attribute.
EXPERIMENTAL_FEATURE(StaticExclusiveOnly, true)

Expand Down
11 changes: 11 additions & 0 deletions include/swift/SIL/SILBridging.h
Original file line number Diff line number Diff line change
Expand Up @@ -477,6 +477,16 @@ struct BridgedFunction {
IsSerializedForPackage
};

enum class OptionalSourceFileKind {
Library,
Main,
SIL,
Interface,
MacroExpansion,
DefaultArgument, // must match swift::SourceFileKind::DefaultArgument
None
};

SWIFT_NAME("init(obj:)")
BridgedFunction(SwiftObject obj) : obj(obj) {}
BridgedFunction() {}
Expand Down Expand Up @@ -529,6 +539,7 @@ struct BridgedFunction {
bool isAutodiffVJP() const;
SwiftInt specializationLevel() const;
SWIFT_IMPORT_UNSAFE BridgedSubstitutionMap getMethodSubstitutions(BridgedSubstitutionMap contextSubs) const;
BRIDGED_INLINE OptionalSourceFileKind getSourceFileKind() const;

enum class ParseEffectsMode {
argumentEffectsFromSource,
Expand Down
9 changes: 9 additions & 0 deletions include/swift/SIL/SILBridgingImpl.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
#include "SILBridging.h"
#include "swift/AST/Builtins.h"
#include "swift/AST/Decl.h"
#include "swift/AST/SourceFile.h"
#include "swift/AST/SubstitutionMap.h"
#include "swift/AST/Types.h"
#include "swift/Basic/BasicBridging.h"
Expand Down Expand Up @@ -924,6 +925,14 @@ swift::SILFunction * _Nullable OptionalBridgedFunction::getFunction() const {
return static_cast<swift::SILFunction *>(obj);
}

BridgedFunction::OptionalSourceFileKind BridgedFunction::getSourceFileKind() const {
if (auto *dc = getFunction()->getDeclContext()) {
if (auto *sourceFile = dc->getParentSourceFile())
return (OptionalSourceFileKind)sourceFile->Kind;
}
return OptionalSourceFileKind::None;
}

//===----------------------------------------------------------------------===//
// BridgedGlobalVar
//===----------------------------------------------------------------------===//
Expand Down
2 changes: 0 additions & 2 deletions lib/AST/FeatureSet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -263,8 +263,6 @@ static bool usesFeatureLifetimeDependence(Decl *decl) {
->hasLifetimeDependencies();
}

UNINTERESTING_FEATURE(LifetimeDependenceDiagnoseTrivial)

UNINTERESTING_FEATURE(DynamicActorIsolation)
UNINTERESTING_FEATURE(NonfrozenEnumExhaustivity)
UNINTERESTING_FEATURE(ClosureIsolation)
Expand Down
27 changes: 24 additions & 3 deletions stdlib/public/Cxx/CxxSpan.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,24 @@ internal func unsafeBitCast<T: ~Escapable, U>(
Builtin.reinterpretCast(x)
}

/// Unsafely discard any lifetime dependency on the `dependent` argument. Return
/// a value identical to `dependent` with a lifetime dependency on the caller's
/// borrow scope of the `source` argument.
@unsafe
@_unsafeNonescapableResult
@_alwaysEmitIntoClient
@_transparent
@lifetime(borrow source)
internal func _overrideLifetime<
T: ~Copyable & ~Escapable, U: ~Copyable & ~Escapable
>(
_ dependent: consuming T, borrowing source: borrowing U
) -> T {
// TODO: Remove @_unsafeNonescapableResult. Instead, the unsafe dependence
// should be expressed by a builtin that is hidden within the function body.
dependent
}

/// A C++ type that is an object that can refer to a contiguous sequence of objects.
///
/// C++ standard library type `std::span` conforms to this protocol.
Expand Down Expand Up @@ -62,12 +80,15 @@ extension CxxSpan {
@available(SwiftStdlib 6.1, *)
extension Span {
@_alwaysEmitIntoClient
@lifetime(immortal)
@unsafe
@lifetime(borrow span)
public init<T: CxxSpan<Element>>(
_unsafeCxxSpan span: T,
_unsafeCxxSpan span: borrowing T,
) {
self.init(_unsafeElements: .init(start: span.__dataUnsafe(), count: Int(span.size())))
let buffer = UnsafeBufferPointer(start: span.__dataUnsafe(), count: Int(span.size()))
let newSpan = Span(_unsafeElements: buffer)
// 'self' is limited to the caller's scope of the variable passed to the 'span' argument.
self = _overrideLifetime(newSpan, borrowing: span)
}
}

Expand Down
2 changes: 0 additions & 2 deletions test/Generics/inverse_generics.swift
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
// RUN: %target-typecheck-verify-swift \
// RUN: -enable-experimental-feature LifetimeDependence \
// RUN: -enable-experimental-feature LifetimeDependenceDiagnoseTrivial \
// RUN: -enable-experimental-feature SuppressedAssociatedTypes

// REQUIRES: swift_feature_LifetimeDependence
// REQUIRES: swift_feature_LifetimeDependenceDiagnoseTrivial
// REQUIRES: swift_feature_SuppressedAssociatedTypes

// expected-note@+1 {{'T' has '~Copyable' constraint preventing implicit 'Copyable' conformance}}
Expand Down
3 changes: 0 additions & 3 deletions test/SIL/Parser/basic2_noncopyable_generics.sil
Original file line number Diff line number Diff line change
@@ -1,16 +1,13 @@
// RUN: %target-sil-opt \
// RUN: %s \
// RUN: -enable-experimental-feature LifetimeDependence \
// RUN: -enable-experimental-feature LifetimeDependenceDiagnoseTrivial \
// RUN: | \
// RUN: %target-sil-opt \
// RUN: -enable-experimental-feature LifetimeDependence \
// RUN: -enable-experimental-feature LifetimeDependenceDiagnoseTrivial \
// RUN: | \
// RUN: %FileCheck %s

// REQUIRES: swift_feature_LifetimeDependence
// REQUIRES: swift_feature_LifetimeDependenceDiagnoseTrivial

// For -enable-experimental-feature LifetimeDependence

Expand Down
2 changes: 0 additions & 2 deletions test/SIL/Parser/lifetime_dependence.sil
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
// RUN: %target-sil-opt %s \
// RUN: -enable-experimental-feature LifetimeDependence \
// RUN: -enable-experimental-feature LifetimeDependenceDiagnoseTrivial \
// RUN: | %FileCheck %s

// REQUIRES: swift_feature_LifetimeDependence
// REQUIRES: swift_feature_LifetimeDependenceDiagnoseTrivial

sil_stage canonical

Expand Down
2 changes: 0 additions & 2 deletions test/SIL/explicit_lifetime_dependence_specifiers.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,9 @@
// RUN: -emit-sil \
// RUN: -enable-builtin-module \
// RUN: -enable-experimental-feature LifetimeDependence \
// RUN: -enable-experimental-feature LifetimeDependenceDiagnoseTrivial \
// RUN: | %FileCheck %s

// REQUIRES: swift_feature_LifetimeDependence
// REQUIRES: swift_feature_LifetimeDependenceDiagnoseTrivial

import Builtin

Expand Down
2 changes: 0 additions & 2 deletions test/SIL/implicit_lifetime_dependence.swift
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
// RUN: %target-swift-frontend %s \
// RUN: -emit-sil -target %target-swift-5.1-abi-triple \
// RUN: -enable-experimental-feature LifetimeDependence \
// RUN: -enable-experimental-feature LifetimeDependenceDiagnoseTrivial \
// RUN: | %FileCheck %s

// REQUIRES: swift_feature_LifetimeDependence
// REQUIRES: swift_feature_LifetimeDependenceDiagnoseTrivial

@_unsafeNonescapableResult
@lifetime(source)
Expand Down
2 changes: 0 additions & 2 deletions test/SIL/lifetime_dependence_generics.swift
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
// RUN: %target-swift-frontend %s -emit-sil \
// RUN: -enable-experimental-feature LifetimeDependence \
// RUN: -enable-experimental-feature LifetimeDependenceDiagnoseTrivial \
// RUN: -enable-experimental-feature SuppressedAssociatedTypes \
// RUN: | %FileCheck %s

// REQUIRES: swift_in_compiler
// REQUIRES: swift_feature_LifetimeDependence
// REQUIRES: swift_feature_LifetimeDependenceDiagnoseTrivial
// REQUIRES: swift_feature_SuppressedAssociatedTypes

protocol P {
Expand Down
4 changes: 1 addition & 3 deletions test/SIL/lifetime_dependence_param_position_test.swift
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
// RUN: %target-swift-frontend %s -emit-silgen \
// RUN: -enable-experimental-feature LifetimeDependence \
// RUN: -enable-experimental-feature LifetimeDependenceDiagnoseTrivial
// RUN: -enable-experimental-feature LifetimeDependence

// REQUIRES: swift_in_compiler
// REQUIRES: swift_feature_LifetimeDependence
// REQUIRES: swift_feature_LifetimeDependenceDiagnoseTrivial


public struct Span<Element> : ~Escapable {
Expand Down
2 changes: 0 additions & 2 deletions test/SIL/lifetime_dependence_span_lifetime_attr.swift
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
// RUN: %target-swift-frontend %s -emit-sil \
// RUN: -enable-experimental-feature LifetimeDependence \
// RUN: -enable-experimental-feature LifetimeDependenceDiagnoseTrivial \
// RUN: | %FileCheck %s

// REQUIRES: swift_in_compiler
// REQUIRES: swift_feature_LifetimeDependence
// REQUIRES: swift_feature_LifetimeDependenceDiagnoseTrivial

// TODO: Use real Range
public struct FakeRange<Bound> {
Expand Down
2 changes: 0 additions & 2 deletions test/SIL/type_lowering_unit.sil
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
// RUN: %target-sil-opt -test-runner \
// RUN: -enable-experimental-feature LifetimeDependence \
// RUN: -enable-experimental-feature LifetimeDependenceDiagnoseTrivial \
// RUN: %s -o /dev/null 2>&1 | %FileCheck %s

// REQUIRES: swift_feature_LifetimeDependence
// REQUIRES: swift_feature_LifetimeDependenceDiagnoseTrivial

sil_stage raw

Expand Down
2 changes: 0 additions & 2 deletions test/SILGen/accessor_borrow.swift
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
// RUN: %target-swift-emit-silgen -module-name accessor_borrow \
// RUN: -enable-experimental-feature LifetimeDependence \
// RUN: -enable-experimental-feature LifetimeDependenceDiagnoseTrivial \
// RUN: %s | %FileCheck %s

// REQUIRES: swift_feature_LifetimeDependence
// REQUIRES: swift_feature_LifetimeDependenceDiagnoseTrivial

struct NE: ~Escapable {}

Expand Down
2 changes: 1 addition & 1 deletion test/SILGen/addressors.swift
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ func test1() -> Int32 {
// CHECK: [[PTR:%.*]] = apply [[ACCESSOR]]({{%.*}}, [[A]]) : $@convention(method) (Int32, A) -> UnsafePointer<Int32>
// CHECK: [[T0:%.*]] = struct_extract [[PTR]] : $UnsafePointer<Int32>, #UnsafePointer._rawValue
// CHECK: [[T1:%.*]] = pointer_to_address [[T0]] : $Builtin.RawPointer to [strict] $*Int32
// CHECK: [[MD:%.*]] = mark_dependence [nonescaping] [[T1]] : $*Int32 on [[A]] : $A
// CHECK: [[MD:%.*]] = mark_dependence [[T1]] : $*Int32 on [[A]] : $A
// CHECK: [[ACCESS:%.*]] = begin_access [read] [unsafe] [[MD]] : $*Int32
// CHECK: [[T2:%.*]] = load [[ACCESS]] : $*Int32
// CHECK: return [[T2]] : $Int32
Expand Down
2 changes: 0 additions & 2 deletions test/SILOptimizer/argument_conventions.sil
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
// RUN: %target-sil-opt -test-runner %s \
// RUN: -sil-verify-all \
// RUN: -enable-experimental-feature LifetimeDependence \
// RUN: -enable-experimental-feature LifetimeDependenceDiagnoseTrivial \
// RUN: 2>&1 | %FileCheck %s

// REQUIRES: swift_in_compiler
// REQUIRES: swift_feature_LifetimeDependence
// REQUIRES: swift_feature_LifetimeDependenceDiagnoseTrivial

import Builtin

Expand Down
2 changes: 0 additions & 2 deletions test/SILOptimizer/capture_promotion_ownership.sil
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
// RUN: %target-sil-opt -sil-print-types -enable-sil-verify-all %s -capture-promotion \
// RUN: -enable-experimental-feature LifetimeDependence \
// RUN: -enable-experimental-feature LifetimeDependenceDiagnoseTrivial \
// RUN: -module-name Swift \
// RUN: | %FileCheck %s

// REQUIRES: swift_feature_LifetimeDependence
// REQUIRES: swift_feature_LifetimeDependenceDiagnoseTrivial

// Check to make sure that the process of promoting closure captures results in
// a correctly cloned and modified closure function body. This test
Expand Down
2 changes: 0 additions & 2 deletions test/SILOptimizer/lifetime_dependence/coroutine.swift
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
// RUN: %target-swift-frontend %s -emit-sil \
// RUN: -enable-experimental-feature LifetimeDependence \
// RUN: -enable-experimental-feature LifetimeDependenceDiagnoseTrivial \
// RUN: | %FileCheck %s

// REQUIRES: swift_in_compiler
// REQUIRES: swift_feature_LifetimeDependence
// REQUIRES: swift_feature_LifetimeDependenceDiagnoseTrivial

struct View : ~Escapable {
let ptr: UnsafeRawBufferPointer
Expand Down
2 changes: 0 additions & 2 deletions test/SILOptimizer/lifetime_dependence/diagnostic_passes.sil
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
// RUN: %target-sil-opt -test-runner %s \
// RUN: -diagnostics -sil-verify-all \
// RUN: -enable-experimental-feature LifetimeDependence \
// RUN: -enable-experimental-feature LifetimeDependenceDiagnoseTrivial \
// RUN: 2>&1 | %FileCheck %s

// Test SIL expected from SILGen output. Run all diagnostic passes which includes lifetime dependence handling:
Expand All @@ -13,7 +12,6 @@

// REQUIRES: swift_in_compiler
// REQUIRES: swift_feature_LifetimeDependence
// REQUIRES: swift_feature_LifetimeDependenceDiagnoseTrivial

sil_stage raw

Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
// RUN: %target-sil-opt -test-runner %s \
// RUN: -diagnostics -sil-verify-all \
// RUN: -enable-experimental-feature LifetimeDependence \
// RUN: -enable-experimental-feature LifetimeDependenceDiagnoseTrivial \
// RUN: 2>&1 | %FileCheck %s

// REQUIRES: swift_feature_LifetimeDependence
// REQUIRES: swift_feature_LifetimeDependenceDiagnoseTrivial

// Test SIL expected from SILGen output. Run all diagnostic passes which includes lifetime dependence handling.
// These tests are not fully implemented.
Expand Down
4 changes: 1 addition & 3 deletions test/SILOptimizer/lifetime_dependence/initializer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,10 @@
// RUN: -verify \
// RUN: -sil-verify-all \
// RUN: -module-name test \
// RUN: -enable-experimental-feature LifetimeDependence \
// RUN: -enable-experimental-feature LifetimeDependenceDiagnoseTrivial
// RUN: -enable-experimental-feature LifetimeDependence

// REQUIRES: swift_in_compiler
// REQUIRES: swift_feature_LifetimeDependence
// REQUIRES: swift_feature_LifetimeDependenceDiagnoseTrivial

struct Span<T>: ~Escapable {
private var base: UnsafePointer<T>
Expand Down
4 changes: 1 addition & 3 deletions test/SILOptimizer/lifetime_dependence/inout.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,10 @@
// RUN: -verify \
// RUN: -sil-verify-all \
// RUN: -module-name test \
// RUN: -enable-experimental-feature LifetimeDependence \
// RUN: -enable-experimental-feature LifetimeDependenceDiagnoseTrivial
// RUN: -enable-experimental-feature LifetimeDependence

// REQUIRES: swift_in_compiler
// REQUIRES: swift_feature_LifetimeDependence
// REQUIRES: swift_feature_LifetimeDependenceDiagnoseTrivial

struct Span<T>: ~Escapable {
private var base: UnsafePointer<T>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,10 @@
// RUN: -o /dev/null \
// RUN: -sil-verify-all \
// RUN: -module-name Swift \
// RUN: -enable-experimental-feature LifetimeDependence \
// RUN: -enable-experimental-feature LifetimeDependenceDiagnoseTrivial
// RUN: -enable-experimental-feature LifetimeDependence

// REQUIRES: swift_in_compiler
// REQUIRES: swift_feature_LifetimeDependence
// REQUIRES: swift_feature_LifetimeDependenceDiagnoseTrivial

// Test the SIL representation for lifetime dependence.

Expand Down
Loading

0 comments on commit c347b66

Please sign in to comment.