Skip to content
Open
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
82 changes: 43 additions & 39 deletions lib/SIL/IR/SILFunctionBuilder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -66,46 +66,50 @@ void SILFunctionBuilder::addFunctionAttributes(
M.getOptions().EnableGlobalAssemblyVision)
F->addSemanticsAttr(semantics::FORCE_EMIT_OPT_REMARK_PREFIX);

// Propagate @_specialize.
for (auto *A : Attrs.getAttributes<AbstractSpecializeAttr>()) {
auto *SA = cast<AbstractSpecializeAttr>(A);
auto kind =
SA->getSpecializationKind() == SpecializeAttr::SpecializationKind::Full
? SILSpecializeAttr::SpecializationKind::Full
: SILSpecializeAttr::SpecializationKind::Partial;
assert(!constant.isNull());
SILFunction *targetFunction = nullptr;
auto *attributedFuncDecl = constant.getAbstractFunctionDecl();
auto *targetFunctionDecl = SA->getTargetFunctionDecl(attributedFuncDecl);
// Filter out _spi.
auto spiGroups = SA->getSPIGroups();
bool hasSPI = !spiGroups.empty();
if (hasSPI) {
if (attributedFuncDecl->getModuleContext() != M.getSwiftModule() &&
!M.getSwiftModule()->isImportedAsSPI(SA, attributedFuncDecl)) {
continue;
if (F->getRepresentation() != SILFunctionTypeRepresentation::ObjCMethod) {
// Propagate @_specialize.
for (auto *A : Attrs.getAttributes<AbstractSpecializeAttr>()) {
auto *SA = cast<AbstractSpecializeAttr>(A);
auto kind = SA->getSpecializationKind() ==
SpecializeAttr::SpecializationKind::Full
? SILSpecializeAttr::SpecializationKind::Full
: SILSpecializeAttr::SpecializationKind::Partial;
assert(!constant.isNull());
SILFunction *targetFunction = nullptr;
auto *attributedFuncDecl = constant.getAbstractFunctionDecl();
auto *targetFunctionDecl = SA->getTargetFunctionDecl(attributedFuncDecl);
// Filter out _spi.
auto spiGroups = SA->getSPIGroups();
bool hasSPI = !spiGroups.empty();
if (hasSPI) {
if (attributedFuncDecl->getModuleContext() != M.getSwiftModule() &&
!M.getSwiftModule()->isImportedAsSPI(SA, attributedFuncDecl)) {
continue;
}
}
assert(spiGroups.size() <= 1 &&
"SIL does not support multiple SPI groups");
Identifier spiGroupIdent;
if (hasSPI) {
spiGroupIdent = spiGroups[0];
}
auto availability = AvailabilityInference::annotatedAvailableRangeForAttr(
attributedFuncDecl, SA, M.getSwiftModule()->getASTContext());
auto specializedSignature =
SA->getSpecializedSignature(attributedFuncDecl);
if (targetFunctionDecl) {
SILDeclRef declRef(targetFunctionDecl, constant.kind, false);
targetFunction = getOrCreateDeclaration(targetFunctionDecl, declRef);
F->addSpecializeAttr(SILSpecializeAttr::create(
M, specializedSignature, SA->getTypeErasedParams(),
SA->isExported(), kind, targetFunction, spiGroupIdent,
attributedFuncDecl->getModuleContext(), availability));
} else {
F->addSpecializeAttr(SILSpecializeAttr::create(
M, specializedSignature, SA->getTypeErasedParams(),
SA->isExported(), kind, nullptr, spiGroupIdent,
attributedFuncDecl->getModuleContext(), availability));
}
}
assert(spiGroups.size() <= 1 && "SIL does not support multiple SPI groups");
Identifier spiGroupIdent;
if (hasSPI) {
spiGroupIdent = spiGroups[0];
}
auto availability = AvailabilityInference::annotatedAvailableRangeForAttr(
attributedFuncDecl, SA, M.getSwiftModule()->getASTContext());
auto specializedSignature = SA->getSpecializedSignature(attributedFuncDecl);
if (targetFunctionDecl) {
SILDeclRef declRef(targetFunctionDecl, constant.kind, false);
targetFunction = getOrCreateDeclaration(targetFunctionDecl, declRef);
F->addSpecializeAttr(SILSpecializeAttr::create(
M, specializedSignature, SA->getTypeErasedParams(),
SA->isExported(), kind, targetFunction, spiGroupIdent,
attributedFuncDecl->getModuleContext(), availability));
} else {
F->addSpecializeAttr(SILSpecializeAttr::create(
M, specializedSignature, SA->getTypeErasedParams(),
SA->isExported(), kind, nullptr, spiGroupIdent,
attributedFuncDecl->getModuleContext(), availability));
}
}

Expand Down
14 changes: 14 additions & 0 deletions test/SILOptimizer/Inputs/NoObjCSpecialization.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#import <Foundation/Foundation.h>

NS_ASSUME_NONNULL_BEGIN

extern __attribute__((visibility("default")))
@protocol NoObjCSpecialization<NSObject>

- (BOOL)fooWithValue:(id)value
andBool:(BOOL)boolValue
error:(NSError *_Nullable *)error;

@end

NS_ASSUME_NONNULL_END
25 changes: 25 additions & 0 deletions test/SILOptimizer/no_objc_specialization.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// RUN: %target-swift-frontend -import-objc-header %S/Inputs/NoObjCSpecialization.h -O -emit-sil %s | %FileCheck %s

// REQUIRES: objc_interop

import Foundation

final class NoObjCSpecializationImpl<Value>: NSObject, NoObjCSpecialization {

// CHECK-NOT: @$s22no_objc_specialization24NoObjCSpecializationImplC3foo9withValue7andBoolyyp_SbtKFToyt_Tg5 : $@convention(objc_method)
// CHECK-LABEL: sil private [thunk] @$s22no_objc_specialization24NoObjCSpecializationImplC3foo9withValue7andBoolyyp_SbtKFTo : $@convention(objc_method)
// CHECK: [[FUNC:%.*]] = function_ref @$s22no_objc_specialization24NoObjCSpecializationImplC3foo9withValue7andBoolyyp_SbtKFTf4nnd_n : $@convention(thin) <τ_0_0> (@in_guaranteed Any, Bool) -> @error any Error
// CHECK: try_apply [[FUNC]]<Value>({{%.*}}, {{%.*}}) : $@convention(thin) <τ_0_0> (@in_guaranteed Any, Bool) -> @error any Error
// CHECK-LABEL: sil shared @$s22no_objc_specialization24NoObjCSpecializationImplC3foo9withValue7andBoolyyp_SbtKFyt_Tg5Tf4nnd_n : $@convention(thin) (@in_guaranteed Any, Bool) -> @error any Error {
// CHECK-LABEL: sil shared @$s22no_objc_specialization24NoObjCSpecializationImplC3foo9withValue7andBoolyyp_SbtKFTf4nnd_n : $@convention(thin) <Value> (@in_guaranteed Any, Bool) -> @error any Error {
// CHECK: [[FUNC:%.*]] = function_ref @$s22no_objc_specialization24NoObjCSpecializationImplC3foo9withValue7andBoolyyp_SbtKFyt_Tg5Tf4nnd_n : $@convention(thin) (@in_guaranteed Any, Bool) -> @error any Error
// CHECK: apply [nothrow] [[FUNC]]({{%.*}}, {{%.*}}) : $@convention(thin) (@in_guaranteed Any, Bool) -> @error any Error
@_specialize(where Value == Void)
func foo(withValue value: Any, andBool bool: Bool) throws {
if Value.self == Void.self {
print("Is Void with \(bool)")
} else {
print("Is \(value) with \(bool)")
}
}
}