Skip to content

Commit 29cc1b6

Browse files
Revert "[AST] Store Clang type in SILFunctionType for @convention(c) functions."
This reverts commit 5f45820.
1 parent d9a7a7d commit 29cc1b6

File tree

10 files changed

+89
-192
lines changed

10 files changed

+89
-192
lines changed

include/swift/AST/Types.h

Lines changed: 26 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444

4545
namespace clang {
4646
class Type;
47+
class FunctionType;
4748
} // namespace clang
4849

4950
namespace llvm {
@@ -2946,7 +2947,6 @@ class AnyFunctionType : public TypeBase {
29462947
friend ExtInfo;
29472948
friend class AnyFunctionType;
29482949
friend class FunctionType;
2949-
friend class SILUncommonInfo;
29502950
// We preserve a full clang::Type *, not a clang::FunctionType * as:
29512951
// 1. We need to keep sugar in case we need to present an error to the user.
29522952
// 2. The actual type being stored is [ignoring sugar] either a
@@ -3917,24 +3917,6 @@ namespace Lowering {
39173917
class TypeConverter;
39183918
};
39193919

3920-
class SILUncommonInfo {
3921-
friend class SILFunctionType;
3922-
3923-
// Invariant: The FunctionType is canonical.
3924-
// We store a clang::FunctionType * instead of a clang::CanQualType to
3925-
// avoid depending on the Clang AST in this header.
3926-
const clang::Type *ClangFunctionType;
3927-
3928-
bool empty() const { return !ClangFunctionType; }
3929-
SILUncommonInfo(const clang::Type *type) : ClangFunctionType(type) {}
3930-
SILUncommonInfo(AnyFunctionType::ExtInfo::Uncommon uncommon);
3931-
3932-
public:
3933-
/// Analog of AnyFunctionType::ExtInfo::Uncommon::printClangFunctionType.
3934-
void printClangFunctionType(ClangModuleLoader *cml,
3935-
llvm::raw_ostream &os) const;
3936-
};
3937-
39383920
/// SILFunctionType - The lowered type of a function value, suitable
39393921
/// for use by SIL.
39403922
///
@@ -3943,8 +3925,7 @@ class SILUncommonInfo {
39433925
/// function parameter and result types.
39443926
class SILFunctionType final : public TypeBase, public llvm::FoldingSetNode,
39453927
private llvm::TrailingObjects<SILFunctionType, SILParameterInfo,
3946-
SILResultInfo, SILYieldInfo, CanType,
3947-
SILUncommonInfo> {
3928+
SILResultInfo, SILYieldInfo, CanType> {
39483929
friend TrailingObjects;
39493930

39503931
size_t numTrailingObjects(OverloadToken<SILParameterInfo>) const {
@@ -3963,10 +3944,6 @@ class SILFunctionType final : public TypeBase, public llvm::FoldingSetNode,
39633944
return hasResultCache() ? 2 : 0;
39643945
}
39653946

3966-
size_t numTrailingObjects(OverloadToken<SILUncommonInfo>) const {
3967-
return Bits.SILFunctionType.HasUncommonInfo ? 1 : 0;
3968-
}
3969-
39703947
public:
39713948
using Language = SILFunctionLanguage;
39723949
using Representation = SILFunctionTypeRepresentation;
@@ -3991,31 +3968,27 @@ class SILFunctionType final : public TypeBase, public llvm::FoldingSetNode,
39913968

39923969
unsigned Bits; // Naturally sized for speed.
39933970

3994-
// For symmetry with AnyFunctionType::Uncommon
3995-
using Uncommon = SILUncommonInfo;
3971+
class Uncommon {
3972+
friend ExtInfo;
3973+
friend class SILFunctionType;
39963974

3997-
Uncommon Other;
3975+
// Invariant: The FunctionType is canonical.
3976+
// We store a clang::FunctionType * instead of a clang::CanQualType to
3977+
// avoid depending on the Clang AST in this header.
3978+
const clang::FunctionType *ClangFunctionType;
39983979

3999-
static void assertIsFunctionType(const clang::Type *);
3980+
bool empty() const { return !ClangFunctionType; }
3981+
Uncommon(const clang::FunctionType *type) : ClangFunctionType(type) {}
40003982

4001-
ExtInfo(unsigned Bits, Uncommon Other) : Bits(Bits), Other(Other) {
4002-
auto Rep = Representation(Bits & RepresentationMask);
4003-
// TODO: [clang-function-type-serialization] Once we start serializing
4004-
// the Clang type, we should also assert that the pointer is non-null.
4005-
if ((Rep == Representation::CFunctionPointer) && Other.ClangFunctionType)
4006-
assertIsFunctionType(Other.ClangFunctionType);
4007-
}
3983+
public:
3984+
/// Analog of AnyFunctionType::ExtInfo::Uncommon::printClangFunctionType.
3985+
void printClangFunctionType(ClangModuleLoader *cml,
3986+
llvm::raw_ostream &os) const;
3987+
};
40083988

4009-
static constexpr unsigned makeBits(Representation rep,
4010-
bool isPseudogeneric,
4011-
bool isNoEscape,
4012-
DifferentiabilityKind diffKind) {
4013-
return ((unsigned) rep)
4014-
| (isPseudogeneric ? PseudogenericMask : 0)
4015-
| (isNoEscape ? NoEscapeMask : 0)
4016-
| (((unsigned)diffKind << DifferentiabilityMaskOffset)
4017-
& DifferentiabilityMask);
4018-
}
3989+
Uncommon Other;
3990+
3991+
ExtInfo(unsigned Bits, Uncommon Other) : Bits(Bits), Other(Other) {}
40193992

40203993
friend class SILFunctionType;
40213994
public:
@@ -4025,21 +3998,15 @@ class SILFunctionType final : public TypeBase, public llvm::FoldingSetNode,
40253998
// Constructor for polymorphic type.
40263999
ExtInfo(Representation rep, bool isPseudogeneric, bool isNoEscape,
40274000
DifferentiabilityKind diffKind,
4028-
const clang::Type *type)
4029-
: ExtInfo(makeBits(rep, isPseudogeneric, isNoEscape, diffKind),
4001+
const clang::FunctionType *type)
4002+
: ExtInfo(((unsigned) rep)
4003+
| (isPseudogeneric ? PseudogenericMask : 0)
4004+
| (isNoEscape ? NoEscapeMask : 0)
4005+
| (((unsigned)diffKind << DifferentiabilityMaskOffset)
4006+
& DifferentiabilityMask),
40304007
Uncommon(type)) {
40314008
}
40324009

4033-
ExtInfo(AnyFunctionType::ExtInfo info, bool isPseudogeneric)
4034-
: ExtInfo(makeBits(info.getSILRepresentation(),
4035-
isPseudogeneric,
4036-
info.isNoEscape(),
4037-
info.getDifferentiabilityKind()),
4038-
info.getUncommonInfo().hasValue()
4039-
? Uncommon(info.getUncommonInfo().getValue())
4040-
: Uncommon(nullptr)) {
4041-
}
4042-
40434010
static ExtInfo getThin() {
40444011
return ExtInfo(Representation::Thin, false, false,
40454012
DifferentiabilityKind::NonDifferentiable, nullptr);
@@ -4126,9 +4093,6 @@ class SILFunctionType final : public TypeBase, public llvm::FoldingSetNode,
41264093
return ExtInfo(NoEscape ? (Bits | NoEscapeMask) : (Bits & ~NoEscapeMask),
41274094
Other);
41284095
}
4129-
ExtInfo withClangFunctionType(const clang::Type *type) const {
4130-
return ExtInfo(Bits, Uncommon(type));
4131-
}
41324096

41334097
std::pair<unsigned, const void *> getFuncAttrKey() const {
41344098
return std::make_pair(Bits, Other.ClangFunctionType);
@@ -4424,7 +4388,7 @@ class SILFunctionType final : public TypeBase, public llvm::FoldingSetNode,
44244388
return WitnessMethodConformance;
44254389
}
44264390

4427-
const clang::Type *getClangFunctionType() const;
4391+
const clang::FunctionType *getClangFunctionType() const;
44284392

44294393
ExtInfo getExtInfo() const {
44304394
return ExtInfo(Bits.SILFunctionType.ExtInfoBits, getClangFunctionType());

lib/AST/ASTContext.cpp

Lines changed: 5 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -3158,13 +3158,7 @@ ArrayRef<Requirement> GenericFunctionType::getRequirements() const {
31583158
return Signature->getRequirements();
31593159
}
31603160

3161-
SILUncommonInfo::SILUncommonInfo(AnyFunctionType::ExtInfo::Uncommon uncommon) {
3162-
auto *ty = uncommon.ClangFunctionType;
3163-
ClangFunctionType = ty ? ty->getCanonicalTypeInternal().getTypePtr()
3164-
: nullptr;
3165-
}
3166-
3167-
void SILUncommonInfo::printClangFunctionType(
3161+
void SILFunctionType::ExtInfo::Uncommon::printClangFunctionType(
31683162
ClangModuleLoader *cml, llvm::raw_ostream &os) const {
31693163
cml->printClangType(ClangFunctionType, os);
31703164
}
@@ -3230,11 +3224,11 @@ SILFunctionType::SILFunctionType(
32303224

32313225
Bits.SILFunctionType.HasErrorResult = errorResult.hasValue();
32323226
Bits.SILFunctionType.ExtInfoBits = ext.Bits;
3227+
Bits.SILFunctionType.HasUncommonInfo = false;
32333228
// The use of both assert() and static_assert() below is intentional.
32343229
assert(Bits.SILFunctionType.ExtInfoBits == ext.Bits && "Bits were dropped!");
32353230
static_assert(ExtInfo::NumMaskBits == NumSILExtInfoBits,
32363231
"ExtInfo and SILFunctionTypeBitfields must agree on bit size");
3237-
Bits.SILFunctionType.HasUncommonInfo = ext.getUncommonInfo().hasValue();
32383232
Bits.SILFunctionType.CoroutineKind = unsigned(coroutineKind);
32393233
NumParameters = params.size();
32403234
if (coroutineKind == SILCoroutineKind::None) {
@@ -3267,9 +3261,6 @@ SILFunctionType::SILFunctionType(
32673261
getMutableFormalResultsCache() = CanType();
32683262
getMutableAllResultsCache() = CanType();
32693263
}
3270-
if (auto uncommon = ext.getUncommonInfo())
3271-
*getTrailingObjects<ExtInfo::Uncommon>() = uncommon.getValue();
3272-
32733264
#ifndef NDEBUG
32743265
if (ext.getRepresentation() == Representation::WitnessMethod)
32753266
assert(!WitnessMethodConformance.isInvalid() &&
@@ -3369,10 +3360,6 @@ CanSILFunctionType SILFunctionType::get(
33693360
assert(coroutineKind != SILCoroutineKind::None || yields.empty());
33703361
assert(!ext.isPseudogeneric() || genericSig);
33713362

3372-
// FIXME: [clang-function-type-serialization] Don't drop the Clang type...
3373-
if (!ctx.LangOpts.UseClangFunctionTypes)
3374-
ext = ext.withClangFunctionType(nullptr);
3375-
33763363
llvm::FoldingSetNodeID id;
33773364
SILFunctionType::Profile(id, genericSig, ext, coroutineKind, callee, params,
33783365
yields, normalResults, errorResult,
@@ -3389,11 +3376,10 @@ CanSILFunctionType SILFunctionType::get(
33893376

33903377
// See [SILFunctionType-layout]
33913378
bool hasResultCache = normalResults.size() > 1;
3392-
size_t bytes = totalSizeToAlloc<SILParameterInfo, SILResultInfo, SILYieldInfo,
3393-
CanType, SILFunctionType::ExtInfo::Uncommon>(
3379+
size_t bytes =
3380+
totalSizeToAlloc<SILParameterInfo, SILResultInfo, SILYieldInfo, CanType>(
33943381
params.size(), normalResults.size() + (errorResult ? 1 : 0),
3395-
yields.size(), hasResultCache ? 2 : 0,
3396-
ext.getUncommonInfo().hasValue() ? 1 : 0);
3382+
yields.size(), hasResultCache ? 2 : 0);
33973383

33983384
void *mem = ctx.Allocate(bytes, alignof(SILFunctionType));
33993385

lib/AST/ASTDemangler.cpp

Lines changed: 6 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -488,6 +488,12 @@ Type ASTBuilder::createImplFunctionType(
488488
break;
489489
}
490490

491+
// TODO: [store-sil-clang-function-type]
492+
auto einfo = SILFunctionType::ExtInfo(
493+
representation, flags.isPseudogeneric(), !flags.isEscaping(),
494+
DifferentiabilityKind::NonDifferentiable,
495+
/*clangFunctionType*/ nullptr);
496+
491497
llvm::SmallVector<SILParameterInfo, 8> funcParams;
492498
llvm::SmallVector<SILYieldInfo, 8> funcYields;
493499
llvm::SmallVector<SILResultInfo, 8> funcResults;
@@ -510,26 +516,6 @@ Type ASTBuilder::createImplFunctionType(
510516
auto conv = getResultConvention(errorResult->getConvention());
511517
funcErrorResult.emplace(type, conv);
512518
}
513-
514-
const clang::Type *clangFnType = nullptr;
515-
auto incompleteExtInfo = SILFunctionType::ExtInfo(
516-
SILFunctionType::Representation::Thick, flags.isPseudogeneric(),
517-
!flags.isEscaping(), DifferentiabilityKind::NonDifferentiable,
518-
clangFnType);
519-
520-
if (representation == SILFunctionType::Representation::CFunctionPointer) {
521-
assert(funcResults.size() <= 1 && funcYields.size() == 0
522-
&& "@convention(c) functions have at most 1 result and 0 yields.");
523-
auto result = funcResults.empty() ? Optional<SILResultInfo>()
524-
: funcResults[0];
525-
auto &Context = getASTContext();
526-
clangFnType = Context.getCanonicalClangFunctionType(funcParams, result,
527-
incompleteExtInfo,
528-
representation);
529-
}
530-
auto einfo = incompleteExtInfo.withRepresentation(representation)
531-
.withClangFunctionType(clangFnType);
532-
533519
return SILFunctionType::get(genericSig, einfo, funcCoroutineKind,
534520
funcCalleeConvention, funcParams, funcYields,
535521
funcResults, funcErrorResult,

lib/AST/Type.cpp

Lines changed: 5 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3229,7 +3229,8 @@ void AnyFunctionType::ExtInfo::Uncommon::printClangFunctionType(
32293229
cml->printClangType(ClangFunctionType, os);
32303230
}
32313231

3232-
static void assertIsFunctionType(const clang::Type *type) {
3232+
void
3233+
AnyFunctionType::ExtInfo::assertIsFunctionType(const clang::Type *type) {
32333234
#ifndef NDEBUG
32343235
if (!(type->isFunctionPointerType() || type->isBlockPointerType())) {
32353236
SmallString<256> buf;
@@ -3243,10 +3244,6 @@ static void assertIsFunctionType(const clang::Type *type) {
32433244
return;
32443245
}
32453246

3246-
void AnyFunctionType::ExtInfo::assertIsFunctionType(const clang::Type *type) {
3247-
::assertIsFunctionType(type);
3248-
}
3249-
32503247
const clang::Type *AnyFunctionType::getClangFunctionType() const {
32513248
switch (getKind()) {
32523249
case TypeKind::Function:
@@ -3264,16 +3261,9 @@ const clang::Type *AnyFunctionType::getCanonicalClangFunctionType() const {
32643261
return ty ? ty->getCanonicalTypeInternal().getTypePtr() : nullptr;
32653262
}
32663263

3267-
void SILFunctionType::ExtInfo::assertIsFunctionType(const clang::Type *type) {
3268-
::assertIsFunctionType(type);
3269-
}
3270-
3271-
const clang::Type *SILFunctionType::getClangFunctionType() const {
3272-
if (!Bits.SILFunctionType.HasUncommonInfo)
3273-
return nullptr;
3274-
auto *type = getTrailingObjects<ExtInfo::Uncommon>()->ClangFunctionType;
3275-
assert(type && "If the pointer was null, we shouldn't have stored it.");
3276-
return type;
3264+
// TODO: [store-sil-clang-function-type]
3265+
const clang::FunctionType *SILFunctionType::getClangFunctionType() const {
3266+
return nullptr;
32773267
}
32783268

32793269
FunctionType *

lib/ClangImporter/ImportType.cpp

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -416,11 +416,12 @@ namespace {
416416

417417
if (pointeeQualType->isFunctionType()) {
418418
auto funcTy = pointeeType->castTo<FunctionType>();
419-
auto extInfo = funcTy->getExtInfo().withRepresentation(
420-
AnyFunctionType::Representation::CFunctionPointer)
421-
.withClangFunctionType(type);
422419
return {
423-
FunctionType::get(funcTy->getParams(), funcTy->getResult(), extInfo),
420+
FunctionType::get(funcTy->getParams(), funcTy->getResult(),
421+
funcTy->getExtInfo()
422+
.withRepresentation(
423+
AnyFunctionType::Representation::CFunctionPointer)
424+
.withClangFunctionType(type)),
424425
ImportHint::CFunctionPointer
425426
};
426427
}

lib/SIL/SILFunctionType.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1266,7 +1266,10 @@ static CanSILFunctionType getSILFunctionType(
12661266

12671267
// NOTE: SILFunctionType::ExtInfo doesn't track everything that
12681268
// AnyFunctionType::ExtInfo tracks. For example: 'throws' or 'auto-closure'
1269-
SILFunctionType::ExtInfo silExtInfo(extInfo, pseudogeneric);
1269+
auto silExtInfo = SILFunctionType::ExtInfo()
1270+
.withRepresentation(extInfo.getSILRepresentation())
1271+
.withIsPseudogeneric(pseudogeneric)
1272+
.withNoEscape(extInfo.isNoEscape());
12701273

12711274
// Build the substituted generic signature we extracted.
12721275
bool impliedSignature = false;

lib/SILGen/SILGen.cpp

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -452,6 +452,8 @@ SILGenModule::getKeyPathProjectionCoroutine(bool isReadAccess,
452452

453453
SILFunction *SILGenModule::emitTopLevelFunction(SILLocation Loc) {
454454
ASTContext &C = getASTContext();
455+
auto extInfo = SILFunctionType::ExtInfo()
456+
.withRepresentation(SILFunctionType::Representation::CFunctionPointer);
455457

456458
// Use standard library types if we have them; otherwise, fall back to
457459
// builtins.
@@ -482,19 +484,13 @@ SILFunction *SILGenModule::emitTopLevelFunction(SILLocation Loc) {
482484
SILParameterInfo(PtrPtrInt8Ty, ParameterConvention::Direct_Unowned),
483485
};
484486

485-
SILResultInfo results[] = {SILResultInfo(Int32Ty, ResultConvention::Unowned)};
486-
487-
auto rep = SILFunctionType::Representation::CFunctionPointer;
488-
auto incompleteExtInfo = SILFunctionType::ExtInfo();
489-
auto *clangTy = C.getCanonicalClangFunctionType(params, results[0],
490-
incompleteExtInfo, rep);
491-
auto extInfo = incompleteExtInfo.withRepresentation(rep)
492-
.withClangFunctionType(clangTy);
493-
494487
CanSILFunctionType topLevelType = SILFunctionType::get(nullptr, extInfo,
495488
SILCoroutineKind::None,
496489
ParameterConvention::Direct_Unowned,
497-
params, /*yields*/ {}, results, None,
490+
params, /*yields*/ {},
491+
SILResultInfo(Int32Ty,
492+
ResultConvention::Unowned),
493+
None,
498494
SubstitutionMap(), false,
499495
C);
500496

lib/SILGen/SILGenBridging.cpp

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -541,16 +541,9 @@ ManagedValue SILGenFunction::emitFuncToBlock(SILLocation loc,
541541
blockInterfaceTy->getParameters().end(),
542542
std::back_inserter(params));
543543

544-
auto results = blockInterfaceTy->getResults();
545-
auto incompleteExtInfo = SILFunctionType::ExtInfo();
546-
auto *clangFnType = getASTContext().getCanonicalClangFunctionType(
547-
params, results.empty() ? Optional<SILResultInfo>() : results[0],
548-
incompleteExtInfo,
549-
SILFunctionType::Representation::CFunctionPointer);
550-
551-
auto extInfo = incompleteExtInfo
552-
.withRepresentation(SILFunctionType::Representation::CFunctionPointer)
553-
.withClangFunctionType(clangFnType);
544+
auto extInfo =
545+
SILFunctionType::ExtInfo()
546+
.withRepresentation(SILFunctionType::Representation::CFunctionPointer);
554547

555548
CanGenericSignature genericSig;
556549
GenericEnvironment *genericEnv = nullptr;
@@ -575,7 +568,8 @@ ManagedValue SILGenFunction::emitFuncToBlock(SILLocation loc,
575568

576569
auto invokeTy = SILFunctionType::get(
577570
genericSig, extInfo, SILCoroutineKind::None,
578-
ParameterConvention::Direct_Unowned, params, /*yields*/ {}, results,
571+
ParameterConvention::Direct_Unowned, params,
572+
/*yields*/ {}, blockInterfaceTy->getResults(),
579573
blockInterfaceTy->getOptionalErrorResult(), SubstitutionMap(), false,
580574
getASTContext());
581575

0 commit comments

Comments
 (0)