Skip to content

[5.9] IRGen: Assorted -unavailable-decl-optimization fixes #66225

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
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
4 changes: 2 additions & 2 deletions include/swift/SIL/SILModule.h
Original file line number Diff line number Diff line change
Expand Up @@ -1082,11 +1082,11 @@ LLVM_LIBRARY_VISIBILITY bool usesObjCAllocator(ClassDecl *theClass);
/// Returns true if SIL/IR lowering for the given declaration should be skipped.
/// A declaration may not require lowering if, for example, it is annotated as
/// unavailable and optimization settings allow it to be omitted.
LLVM_LIBRARY_VISIBILITY bool shouldSkipLowering(Decl *D);
LLVM_LIBRARY_VISIBILITY bool shouldSkipLowering(const Decl *D);

/// Returns true if SIL/IR lowering for the given declaration should produce
/// a stub that traps at runtime because the code ought to be unreachable.
LLVM_LIBRARY_VISIBILITY bool shouldLowerToUnavailableCodeStub(Decl *D);
LLVM_LIBRARY_VISIBILITY bool shouldLowerToUnavailableCodeStub(const Decl *D);
} // namespace Lowering

/// Apply the given function to each ABI member of \c D skipping the members
Expand Down
6 changes: 5 additions & 1 deletion lib/AST/Availability.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -181,8 +181,12 @@ AvailabilityInference::parentDeclForInferredAvailability(const Decl *D) {
return NTD;
}

if (auto *PBD = dyn_cast<PatternBindingDecl>(D))
if (auto *PBD = dyn_cast<PatternBindingDecl>(D)) {
if (PBD->getNumPatternEntries() < 1)
return nullptr;

return PBD->getAnchoringVarDecl(0);
}

if (auto *OTD = dyn_cast<OpaqueTypeDecl>(D))
return OTD->getNamingDecl();
Expand Down
3 changes: 3 additions & 0 deletions lib/IRGen/GenClass.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1831,6 +1831,9 @@ namespace {

void buildMethod(ConstantArrayBuilder &descriptors,
AbstractFunctionDecl *method) {
if (Lowering::shouldSkipLowering(method))
return;

auto accessor = dyn_cast<AccessorDecl>(method);
if (!accessor)
return emitObjCMethodDescriptor(IGM, descriptors, method);
Expand Down
8 changes: 7 additions & 1 deletion lib/IRGen/GenDecl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1568,7 +1568,9 @@ void IRGenerator::noteUseOfTypeGlobals(NominalTypeDecl *type,
RequireMetadata_t requireMetadata) {
if (!type)
return;


assert(!Lowering::shouldSkipLowering(type));

// Force emission of ObjC protocol descriptors used by type refs.
if (auto proto = dyn_cast<ProtocolDecl>(type)) {
if (proto->isObjC()) {
Expand Down Expand Up @@ -5484,13 +5486,17 @@ static Address getAddrOfSimpleVariable(IRGenModule &IGM,
/// The result is always a GlobalValue.
Address IRGenModule::getAddrOfFieldOffset(VarDecl *var,
ForDefinition_t forDefinition) {
assert(!Lowering::shouldSkipLowering(var));

LinkEntity entity = LinkEntity::forFieldOffset(var);
return getAddrOfSimpleVariable(*this, GlobalVars, entity,
forDefinition);
}

Address IRGenModule::getAddrOfEnumCase(EnumElementDecl *Case,
ForDefinition_t forDefinition) {
assert(!Lowering::shouldSkipLowering(Case));

LinkEntity entity = LinkEntity::forEnumCase(Case);
auto addr = getAddrOfSimpleVariable(*this, GlobalVars, entity, forDefinition);

Expand Down
10 changes: 10 additions & 0 deletions lib/IRGen/GenEnum.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,9 @@ EnumImplStrategy::getTagIndex(EnumElementDecl *Case) const {
static void emitResilientTagIndex(IRGenModule &IGM,
const EnumImplStrategy *strategy,
EnumElementDecl *Case) {
if (Lowering::shouldSkipLowering(Case))
return;

auto resilientIdx = strategy->getTagIndex(Case);
auto *global = cast<llvm::GlobalVariable>(
IGM.getAddrOfEnumCase(Case, ForDefinition).getAddress());
Expand Down Expand Up @@ -6040,6 +6043,13 @@ EnumImplStrategy::get(TypeConverter &TC, SILType type, EnumDecl *theEnum) {
continue;
}

// For the purposes of memory layout, treat unavailable cases as if they do
// not have a payload.
if (Lowering::shouldSkipLowering(elt)) {
elementsWithNoPayload.push_back({elt, nullptr, nullptr});
continue;
}

// If the payload is indirect, we can use the NativeObject type metadata
// without recurring. The box won't affect loadability or fixed-ness.
if (elt->isIndirect() || theEnum->isIndirect()) {
Expand Down
7 changes: 5 additions & 2 deletions lib/IRGen/GenReflection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -856,8 +856,11 @@ class FieldTypeMetadataBuilder : public ReflectionMetadataBuilder {
if (hasPayload && (decl->isIndirect() || enumDecl->isIndirect()))
flags.setIsIndirectCase();

addField(flags, decl->getArgumentInterfaceType(),
decl->getBaseIdentifier().str());
Type interfaceType = Lowering::shouldSkipLowering(decl)
? nullptr
: decl->getArgumentInterfaceType();

addField(flags, interfaceType, decl->getBaseIdentifier().str());
}

void layoutEnum() {
Expand Down
3 changes: 3 additions & 0 deletions lib/IRGen/IRGenModule.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
#include "swift/IRGen/ValueWitness.h"
#include "swift/SIL/RuntimeEffect.h"
#include "swift/SIL/SILFunction.h"
#include "swift/SIL/SILModule.h"
#include "llvm/ADT/DenseMap.h"
#include "llvm/ADT/DenseSet.h"
#include "llvm/ADT/Hashing.h"
Expand Down Expand Up @@ -455,6 +456,7 @@ class IRGenerator {
}

void noteLazyReemissionOfNominalTypeDescriptor(NominalTypeDecl *decl) {
assert(!Lowering::shouldSkipLowering(decl));
LazilyReemittedTypeContextDescriptors.insert(decl);
}

Expand All @@ -464,6 +466,7 @@ class IRGenerator {
}

void noteUseOfMetadataAccessor(NominalTypeDecl *decl) {
assert(!Lowering::shouldSkipLowering(decl));
if (LazyMetadataAccessors.count(decl) == 0) {
LazyMetadataAccessors.insert(decl);
}
Expand Down
1 change: 1 addition & 0 deletions lib/IRGen/MetadataLayout.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
#include "TupleMetadataVisitor.h"

#include "swift/Basic/LLVM.h"
#include "swift/SIL/SILModule.h"
#include "llvm/ADT/Optional.h"

using namespace swift;
Expand Down
4 changes: 2 additions & 2 deletions lib/SIL/IR/SILModule.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -956,7 +956,7 @@ bool Lowering::usesObjCAllocator(ClassDecl *theClass) {
return theClass->getObjectModel() == ReferenceCounting::ObjC;
}

bool Lowering::shouldSkipLowering(Decl *D) {
bool Lowering::shouldSkipLowering(const Decl *D) {
if (D->getASTContext().LangOpts.UnavailableDeclOptimizationMode !=
UnavailableDeclOptimization::Complete)
return false;
Expand All @@ -966,7 +966,7 @@ bool Lowering::shouldSkipLowering(Decl *D) {
return D->getSemanticUnavailableAttr() != None;
}

bool Lowering::shouldLowerToUnavailableCodeStub(Decl *D) {
bool Lowering::shouldLowerToUnavailableCodeStub(const Decl *D) {
if (D->getASTContext().LangOpts.UnavailableDeclOptimizationMode !=
UnavailableDeclOptimization::Stub)
return false;
Expand Down
34 changes: 21 additions & 13 deletions test/IRGen/unavailable_decl_optimization_complete_enum.swift
Original file line number Diff line number Diff line change
@@ -1,48 +1,56 @@
// RUN: %target-swift-frontend -parse-as-library -module-name Test -validate-tbd-against-ir=missing %s -emit-ir | %FileCheck %s --check-prefixes=CHECK,CHECK-NO-STRIP
// RUN: %target-swift-frontend -parse-as-library -module-name Test -validate-tbd-against-ir=missing -enable-library-evolution %s -emit-ir | %FileCheck %s --check-prefixes=CHECK,CHECK-NO-STRIP,CHECK-RESILIENT,CHECK-NO-STRIP-RESILIENT

// RUN: %target-swift-frontend -parse-as-library -module-name Test -validate-tbd-against-ir=missing -unavailable-decl-optimization=complete %s -emit-ir | %FileCheck %s --check-prefixes=CHECK,CHECK-STRIP
// RUN: %target-swift-frontend -parse-as-library -module-name Test -validate-tbd-against-ir=missing -enable-library-evolution -unavailable-decl-optimization=complete %s -emit-ir | %FileCheck %s --check-prefixes=CHECK,CHECK-STRIP,CHECK-RESILIENT,CHECK-STRIP-RESILIENT

// CHECK: private constant [27 x i8] c"availableEnumAvailableCase\00"

// FIXME: Should this reflection metadata for an unavailable case be stripped?
// CHECK: private constant [29 x i8] c"availableEnumUnavailableCase\00"

// CHECK-NO-STRIP-RESILIENT: @"$s4Test13AvailableEnumO09availableC34UnavailableCaseWithAssociatedValueyAcA0E6StructVcACmFWC" = {{.*}}constant
// CHECK-STRIP-RESILIENT-NOT: @"$s4Test13AvailableEnumO09availableC34UnavailableCaseWithAssociatedValueyAcA0E6StructVcACmFWC" =

// CHECK-RESILIENT: @"$s4Test13AvailableEnumO09availablecB4CaseyA2CmFWC" = {{.*}}constant

// CHECK-NO-STRIP-RESILIENT: @"$s4Test13AvailableEnumO09availableC15UnavailableCaseyA2CmFWC" = {{.*}}constant
// CHECK-STRIP-RESILIENT-NOT: @"$s4Test13AvailableEnumO09availableC15UnavailableCaseyA2CmFWC" =

// CHECK-NO-STRIP: private constant [25 x i8] c"unavailableEnumFirstCase\00"
// CHECK-STRIP-NOT: private constant [25 x i8] c"unavailableEnumFirstCase\00"

// CHECK-NO-STRIP-RESILIENT: @"$s4Test15UnavailableEnumO011unavailableC9FirstCaseyA2CmFWC" = {{.*}}constant
// CHECK-STRIP-RESILIENT-NOT: @"$s4Test15UnavailableEnumO011unavailableC9FirstCaseyA2CmFWC"

@available(*, unavailable)
public struct UnavailableStruct {}

public enum AvailableEnum {
case availableEnumAvailableCase

@available(*, unavailable)
case availableEnumUnavailableCase

@available(*, unavailable)
case availableEnumUnavailableCaseWithAssociatedValue(UnavailableStruct)

// CHECK-NO-STRIP: s4Test13AvailableEnumO17unavailableMethodyyF
// CHECK-STRIP-NOT: s4Test13AvailableEnumO17unavailableMethodyyF
@available(*, unavailable)
public func unavailableMethod() {}

// CHECK: s4Test13AvailableEnumO21__derived_enum_equalsySbAC_ACtFZ
// CHECK: s4Test13AvailableEnumO4hash4intoys6HasherVz_tF
// CHECK: s4Test13AvailableEnumO9hashValueSivg
}

// CHECK-NO-STRIP: s4Test17UnavailableStructVMa
// CHECK-STRIP-NOT: s4Test17UnavailableStructVMa

@available(*, unavailable)
public enum UnavailableEnum {
case unavailableEnumFirstCase

// CHECK-NO-STRIP: s4Test15UnavailableEnumO6methodyyF
// CHECK-STRIP-NOT: s4Test15UnavailableEnumO6methodyyF
public func method() {}

// CHECK-NO-STRIP: s4Test15UnavailableEnumO21__derived_enum_equalsySbAC_ACtFZ
// CHECK-STRIP-NOT: s4Test15UnavailableEnumO21__derived_enum_equalsySbAC_ACtFZ

// CHECK-NO-STRIP: s4Test15UnavailableEnumO4hash4intoys6HasherVz_tF
// CHECK-STRIP-NOT: s4Test15UnavailableEnumO4hash4intoys6HasherVz_tF

// CHECK-NO-STRIP: s4Test15UnavailableEnumO9hashValueSivg
// CHECK-STRIP-NOT: s4Test15UnavailableEnumO9hashValueSivg
}

// CHECK: s4Test13AvailableEnumOwug
Expand Down
17 changes: 17 additions & 0 deletions test/IRGen/unavailable_decl_optimization_complete_objc.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// RUN: %target-swift-frontend(mock-sdk: %clang-importer-sdk) -parse-as-library -module-name Test -validate-tbd-against-ir=missing %s -emit-ir | %FileCheck %s --check-prefixes=CHECK,CHECK-NO-STRIP

// RUN: %target-swift-frontend(mock-sdk: %clang-importer-sdk) -parse-as-library -module-name Test -validate-tbd-against-ir=missing -unavailable-decl-optimization=complete %s -emit-ir | %FileCheck %s --check-prefixes=CHECK,CHECK-STRIP

// REQUIRES: objc_interop

import Foundation

public class Class: NSObject {
// CHECK-NO-STRIP: define {{.*}} @"$s4Test5ClassC3fooyyF"
// CHECK-STRIP-NOT: define {{.*}} @"$s4Test5ClassC3fooyyF"

// CHECK-NO-STRIP: define {{.*}} @"$s4Test5ClassC3fooyyFTo"
// CHECK-STRIP-NOT: define {{.*}} @"$s4Test5ClassC3fooyyFTo"
@available(*, unavailable)
@objc public func foo() {}
}
58 changes: 58 additions & 0 deletions test/Interpreter/enum_unavailable_cases.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
// RUN: %empty-directory(%t)
// RUN: %target-build-swift %s -Xfrontend -unavailable-decl-optimization=complete -o %t/a.out
// RUN: %target-codesign %t/a.out
// RUN: %target-run %t/a.out
// REQUIRES: executable_test

import StdlibUnittest

@available(*, unavailable)
class UnavailableClass {
var x: UInt8 = 0
}

enum SingletonTrivial {
@available(*, unavailable)
case unavailable(UInt8)
}

enum SingletonClass {
@available(*, unavailable)
case unavailable(UnavailableClass)
}

enum NoPayload {
case x
@available(*, unavailable)
case unavailable
case y
}

enum SinglePayloadTrivial {
case x
@available(*, unavailable)
case unavailable(UInt8)
case y
}

enum MultiPayloadTrivial {
case x(UInt8)
@available(*, unavailable)
case unavailable(UInt8, UInt8)
case y
}

enum MultiPayloadGeneric<T, U> {
case x(T)
@available(*, unavailable)
case unavailable(T, U)
case y
}

expectEqual(MemoryLayout<SingletonTrivial>.size, 0)
expectEqual(MemoryLayout<SingletonClass>.size, 0)
expectEqual(MemoryLayout<NoPayload>.size, 1)
expectEqual(MemoryLayout<SinglePayloadTrivial>.size, 1)
expectEqual(MemoryLayout<MultiPayloadTrivial>.size, 2)
expectEqual(MemoryLayout<MultiPayloadGeneric<UInt8, UInt8>>.size, 2)
expectEqual(MemoryLayout<MultiPayloadGeneric<UInt32, UInt32>>.size, 5)