Skip to content

Commit a17061c

Browse files
authored
Merge pull request #1005 from swiftwasm/master
[pull] swiftwasm from master
2 parents eb25a37 + ed17e9d commit a17061c

File tree

482 files changed

+10755
-371
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

482 files changed

+10755
-371
lines changed

benchmark/scripts/run_smoke_bench

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -272,6 +272,7 @@ def report_code_size(opt_level, old_dir, new_dir, platform, output_file):
272272
old_lines = ""
273273
new_lines = ""
274274
for oldfile in files:
275+
new_dir = os.path.join(new_dir, '')
275276
newfile = oldfile.replace(old_dir, new_dir, 1)
276277
if os.path.isfile(newfile):
277278
oldsize = get_codesize(oldfile)

include/swift/AST/GenericSignature.h

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ class GenericSignature {
152152
TypeArrayView<GenericTypeParamType> genericParams,
153153
ArrayRef<Requirement> requirements);
154154
public:
155-
using ConformsToArray = SmallVector<ProtocolDecl *, 2>;
155+
using RequiredProtocols = SmallVector<ProtocolDecl *, 2>;
156156

157157
private:
158158
// Direct comparison is disabled for generic signatures. Canonicalize them
@@ -318,12 +318,13 @@ class alignas(1 << TypeAlignInBits) GenericSignatureImpl final
318318
/// Determine the superclass bound on the given dependent type.
319319
Type getSuperclassBound(Type type) const;
320320

321-
/// Determine the set of protocols to which the given dependent type
322-
/// must conform.
323-
GenericSignature::ConformsToArray getConformsTo(Type type) const;
321+
/// Determine the set of protocols to which the given type parameter is
322+
/// required to conform.
323+
GenericSignature::RequiredProtocols getRequiredProtocols(Type type) const;
324324

325-
/// Determine whether the given dependent type conforms to this protocol.
326-
bool conformsToProtocol(Type type, ProtocolDecl *proto) const;
325+
/// Determine whether the given type parameter is required to conform to
326+
/// the given protocol.
327+
bool requiresProtocol(Type type, ProtocolDecl *proto) const;
327328

328329
/// Determine whether the given dependent type is equal to a concrete type.
329330
bool isConcreteType(Type type) const;

include/swift/AST/Types.h

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2696,10 +2696,10 @@ enum class FunctionTypeRepresentation : uint8_t {
26962696
/// A "thin" function that needs no context.
26972697
Thin,
26982698

2699-
/// A C function pointer, which is thin and also uses the C calling
2700-
/// convention.
2699+
/// A C function pointer (or reference), which is thin and also uses the C
2700+
/// calling convention.
27012701
CFunctionPointer,
2702-
2702+
27032703
/// The value of the greatest AST function representation.
27042704
Last = CFunctionPointer,
27052705
};
@@ -2980,8 +2980,8 @@ class AnyFunctionType : public TypeBase {
29802980
// We preserve a full clang::Type *, not a clang::FunctionType * as:
29812981
// 1. We need to keep sugar in case we need to present an error to the user.
29822982
// 2. The actual type being stored is [ignoring sugar] either a
2983-
// clang::PointerType or a clang::BlockPointerType which points to a
2984-
// clang::FunctionType.
2983+
// clang::PointerType, a clang::BlockPointerType, or a
2984+
// clang::ReferenceType which points to a clang::FunctionType.
29852985
const clang::Type *ClangFunctionType;
29862986

29872987
bool empty() const { return !ClangFunctionType; }

include/swift/SILOptimizer/Utils/LoadStoreOptUtils.h

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -169,14 +169,16 @@ class LSBase {
169169
}
170170

171171
/// Print the LSBase.
172-
virtual void print(llvm::raw_ostream &os, SILModule *Mod,
173-
TypeExpansionContext context) {
172+
virtual void print(llvm::raw_ostream &os) {
174173
os << Base;
175-
Path.getValue().print(os, *Mod, context);
174+
SILFunction *F = Base->getFunction();
175+
if (F) {
176+
Path.getValue().print(os, F->getModule(), TypeExpansionContext(*F));
177+
}
176178
}
177179

178-
virtual void dump(SILModule *Mod, TypeExpansionContext context) {
179-
print(llvm::dbgs(), Mod, context);
180+
virtual void dump() {
181+
print(llvm::dbgs());
180182
}
181183
};
182184

@@ -260,13 +262,12 @@ class LSValue : public LSBase {
260262
return Path.getValue().createExtract(Base, Inst, true);
261263
}
262264

263-
void print(llvm::raw_ostream &os, SILModule *Mod,
264-
TypeExpansionContext context) {
265+
void print(llvm::raw_ostream &os) {
265266
if (CoveringValue) {
266267
os << "Covering Value";
267268
return;
268269
}
269-
LSBase::print(os, Mod, context);
270+
LSBase::print(os);
270271
}
271272

272273
/// Expand this SILValue to all individual fields it contains.

lib/AST/ASTContext.cpp

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3723,11 +3723,8 @@ OpaqueTypeArchetypeType::get(OpaqueTypeDecl *Decl,
37233723
superclass = superclass.subst(Substitutions);
37243724
}
37253725
#endif
3726-
SmallVector<ProtocolDecl*, 4> protos;
3727-
for (auto proto : signature->getConformsTo(opaqueInterfaceTy)) {
3728-
protos.push_back(proto);
3729-
}
3730-
3726+
const auto protos = signature->getRequiredProtocols(opaqueInterfaceTy);
3727+
37313728
auto mem = ctx.Allocate(
37323729
OpaqueTypeArchetypeType::totalSizeToAlloc<ProtocolDecl *, Type, LayoutConstraint>(
37333730
protos.size(), superclass ? 1 : 0, layout ? 1 : 0),

lib/AST/ASTMangler.cpp

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2539,8 +2539,9 @@ void ASTMangler::appendGenericSignatureParts(
25392539
DependentMemberType *
25402540
ASTMangler::dropProtocolFromAssociatedType(DependentMemberType *dmt) {
25412541
auto baseTy = dmt->getBase();
2542-
bool unambiguous = (!dmt->getAssocType() ||
2543-
CurGenericSignature->getConformsTo(baseTy).size() <= 1);
2542+
bool unambiguous =
2543+
(!dmt->getAssocType() ||
2544+
CurGenericSignature->getRequiredProtocols(baseTy).size() <= 1);
25442545

25452546
if (auto *baseDMT = baseTy->getAs<DependentMemberType>())
25462547
baseTy = dropProtocolFromAssociatedType(baseDMT);
@@ -2573,8 +2574,8 @@ void ASTMangler::appendAssociatedTypeName(DependentMemberType *dmt) {
25732574
// If the base type is known to have a single protocol conformance
25742575
// in the current generic context, then we don't need to disambiguate the
25752576
// associated type name by protocol.
2576-
if (!OptimizeProtocolNames || !CurGenericSignature
2577-
|| CurGenericSignature->getConformsTo(dmt->getBase()).size() > 1) {
2577+
if (!OptimizeProtocolNames || !CurGenericSignature ||
2578+
CurGenericSignature->getRequiredProtocols(dmt->getBase()).size() > 1) {
25782579
appendAnyGenericType(assocTy->getProtocol());
25792580
}
25802581
return;

lib/AST/GenericSignature.cpp

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -424,10 +424,10 @@ Type GenericSignatureImpl::getSuperclassBound(Type type) const {
424424
return equivClass->superclass;
425425
}
426426

427-
/// Determine the set of protocols to which the given dependent type
428-
/// must conform.
429-
SmallVector<ProtocolDecl *, 2>
430-
GenericSignatureImpl::getConformsTo(Type type) const {
427+
/// Determine the set of protocols to which the given type parameter is
428+
/// required to conform.
429+
GenericSignature::RequiredProtocols
430+
GenericSignatureImpl::getRequiredProtocols(Type type) const {
431431
if (!type->isTypeParameter()) return { };
432432

433433
auto &builder = *getGenericSignatureBuilder();
@@ -437,12 +437,12 @@ GenericSignatureImpl::getConformsTo(Type type) const {
437437
ArchetypeResolutionKind::CompleteWellFormed);
438438
if (!equivClass) return { };
439439

440-
// If this type was mapped to a concrete type, then there are no
441-
// requirements.
440+
// If this type parameter was mapped to a concrete type, then there
441+
// are no requirements.
442442
if (equivClass->concreteType) return { };
443443

444444
// Retrieve the protocols to which this type conforms.
445-
SmallVector<ProtocolDecl *, 2> result;
445+
GenericSignature::RequiredProtocols result;
446446
for (const auto &conforms : equivClass->conformsTo)
447447
result.push_back(conforms.first);
448448

@@ -452,8 +452,8 @@ GenericSignatureImpl::getConformsTo(Type type) const {
452452
return result;
453453
}
454454

455-
bool GenericSignatureImpl::conformsToProtocol(Type type,
456-
ProtocolDecl *proto) const {
455+
bool GenericSignatureImpl::requiresProtocol(Type type,
456+
ProtocolDecl *proto) const {
457457
assert(type->isTypeParameter() && "Expected a type parameter");
458458

459459
auto &builder = *getGenericSignatureBuilder();
@@ -463,7 +463,11 @@ bool GenericSignatureImpl::conformsToProtocol(Type type,
463463
ArchetypeResolutionKind::CompleteWellFormed);
464464
if (!equivClass) return false;
465465

466-
// FIXME: Deal with concrete conformances here?
466+
// FIXME: Optionally deal with concrete conformances here
467+
// or have a separate method do that additionally?
468+
//
469+
// If this type parameter was mapped to a concrete type, then there
470+
// are no requirements.
467471
if (equivClass->concreteType) return false;
468472

469473
// Check whether the representative conforms to this protocol.
@@ -541,7 +545,7 @@ bool GenericSignatureImpl::isRequirementSatisfied(
541545
auto protocol = protocolType->getDecl();
542546

543547
if (canFirstType->isTypeParameter())
544-
return conformsToProtocol(canFirstType, protocol);
548+
return requiresProtocol(canFirstType, protocol);
545549
else
546550
return (bool)GSB->lookupConformance(/*dependentType=*/CanType(),
547551
canFirstType, protocol);

lib/AST/SubstitutionMap.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -368,7 +368,7 @@ SubstitutionMap::lookupConformance(CanType type, ProtocolDecl *proto) const {
368368

369369
// If the type doesn't conform to this protocol, the result isn't formed
370370
// from these requirements.
371-
if (!genericSig->conformsToProtocol(type, proto))
371+
if (!genericSig->requiresProtocol(type, proto))
372372
return ProtocolConformanceRef::forInvalid();
373373

374374
auto accessPath =

lib/AST/Type.cpp

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3044,12 +3044,21 @@ ProtocolConformanceRef ReplaceOpaqueTypesWithUnderlyingTypes::
30443044
operator()(CanType maybeOpaqueType, Type replacementType,
30453045
ProtocolDecl *protocol) const {
30463046
auto abstractRef = ProtocolConformanceRef(protocol);
3047-
3047+
30483048
auto archetypeAndRoot = getArchetypeAndRootOpaqueArchetype(maybeOpaqueType);
30493049
if (!archetypeAndRoot) {
3050-
assert(maybeOpaqueType->isTypeParameter() ||
3051-
maybeOpaqueType->is<ArchetypeType>());
3052-
return abstractRef;
3050+
if (maybeOpaqueType->isTypeParameter() ||
3051+
maybeOpaqueType->is<ArchetypeType>())
3052+
return abstractRef;
3053+
3054+
// SIL type lowering may have already substituted away the opaque type, in
3055+
// which case we'll end up "substituting" the same type.
3056+
if (maybeOpaqueType->isEqual(replacementType)) {
3057+
return inContext->getParentModule()
3058+
->lookupConformance(replacementType, protocol);
3059+
}
3060+
3061+
llvm_unreachable("origType should have been an opaque type or type parameter");
30533062
}
30543063

30553064
auto archetype = archetypeAndRoot->first;
@@ -3376,7 +3385,8 @@ void AnyFunctionType::ExtInfo::Uncommon::printClangFunctionType(
33763385
void
33773386
AnyFunctionType::ExtInfo::assertIsFunctionType(const clang::Type *type) {
33783387
#ifndef NDEBUG
3379-
if (!(type->isFunctionPointerType() || type->isBlockPointerType())) {
3388+
if (!(type->isFunctionPointerType() || type->isBlockPointerType() ||
3389+
type->isFunctionReferenceType())) {
33803390
SmallString<256> buf;
33813391
llvm::raw_svector_ostream os(buf);
33823392
os << "Expected a Clang function type wrapped in a pointer type or "

lib/ClangImporter/ImportType.cpp

Lines changed: 51 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,30 @@ namespace {
159159
explicit operator bool() const { return (bool) AbstractType; }
160160
};
161161

162+
static ImportResult importFunctionPointerLikeType(const clang::Type &type,
163+
const Type &pointeeType) {
164+
auto funcTy = pointeeType->castTo<FunctionType>();
165+
return {FunctionType::get(
166+
funcTy->getParams(), funcTy->getResult(),
167+
funcTy->getExtInfo()
168+
.withRepresentation(
169+
AnyFunctionType::Representation::CFunctionPointer)
170+
.withClangFunctionType(&type)),
171+
type.isReferenceType() ? ImportHint::None
172+
: ImportHint::CFunctionPointer};
173+
}
174+
175+
static ImportResult importOverAlignedFunctionPointerLikeType(
176+
const clang::Type &type, ClangImporter::Implementation &Impl) {
177+
auto opaquePointer = Impl.SwiftContext.getOpaquePointerDecl();
178+
if (!opaquePointer) {
179+
return Type();
180+
}
181+
return {opaquePointer->getDeclaredType(),
182+
type.isReferenceType() ? ImportHint::None
183+
: ImportHint::OtherPointer};
184+
}
185+
162186
class SwiftTypeConverter :
163187
public clang::TypeVisitor<SwiftTypeConverter, ImportResult>
164188
{
@@ -406,23 +430,11 @@ namespace {
406430
// alignment is greater than the maximum Swift alignment, import as
407431
// OpaquePointer.
408432
if (!pointeeType || Impl.isOverAligned(pointeeQualType)) {
409-
auto opaquePointer = Impl.SwiftContext.getOpaquePointerDecl();
410-
if (!opaquePointer)
411-
return Type();
412-
return {opaquePointer->getDeclaredType(),
413-
ImportHint::OtherPointer};
433+
return importOverAlignedFunctionPointerLikeType(*type, Impl);
414434
}
415-
435+
416436
if (pointeeQualType->isFunctionType()) {
417-
auto funcTy = pointeeType->castTo<FunctionType>();
418-
return {
419-
FunctionType::get(funcTy->getParams(), funcTy->getResult(),
420-
funcTy->getExtInfo()
421-
.withRepresentation(
422-
AnyFunctionType::Representation::CFunctionPointer)
423-
.withClangFunctionType(type)),
424-
ImportHint::CFunctionPointer
425-
};
437+
return importFunctionPointerLikeType(*type, pointeeType);
426438
}
427439

428440
PointerTypeKind pointerKind;
@@ -472,7 +484,29 @@ namespace {
472484
}
473485

474486
ImportResult VisitReferenceType(const clang::ReferenceType *type) {
475-
return Type();
487+
auto pointeeQualType = type->getPointeeType();
488+
auto quals = pointeeQualType.getQualifiers();
489+
Type pointeeType =
490+
Impl.importTypeIgnoreIUO(pointeeQualType, ImportTypeKind::Value,
491+
AllowNSUIntegerAsInt, Bridgeability::None);
492+
493+
if (pointeeQualType->isFunctionType()) {
494+
return importFunctionPointerLikeType(*type, pointeeType);
495+
}
496+
497+
if (Impl.isOverAligned(pointeeQualType)) {
498+
return importOverAlignedFunctionPointerLikeType(*type, Impl);
499+
}
500+
501+
PointerTypeKind pointerKind;
502+
if (quals.hasConst()) {
503+
pointerKind = PTK_UnsafePointer;
504+
} else {
505+
pointerKind = PTK_UnsafeMutablePointer;
506+
}
507+
508+
return {pointeeType->wrapInPointer(pointerKind),
509+
ImportHint::None};
476510
}
477511

478512
ImportResult VisitMemberPointer(const clang::MemberPointerType *type) {
@@ -2416,7 +2450,7 @@ bool ClangImporter::Implementation::matchesHashableBound(Type type) {
24162450
if (auto *generic = genericTy->getDecl()) {
24172451
auto genericSig =
24182452
generic->getDeclContext()->getGenericSignatureOfContext();
2419-
if (genericSig && genericSig->getConformsTo(type).empty()) {
2453+
if (genericSig && genericSig->getRequiredProtocols(type).empty()) {
24202454
type = genericSig->getSuperclassBound(type);
24212455
if (!type)
24222456
return false;

0 commit comments

Comments
 (0)