Skip to content

Commit 8381680

Browse files
authored
Merge pull request #966 from swiftwasm/master
[pull] swiftwasm from master
2 parents dadc16a + 58d163b commit 8381680

File tree

65 files changed

+707
-375
lines changed

Some content is hidden

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

65 files changed

+707
-375
lines changed

include/swift/AST/EducationalNotes.def

+13
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,19 @@
2424
EDUCATIONAL_NOTES(unsupported_existential_type,
2525
"associated-type-requirements.md")
2626

27+
EDUCATIONAL_NOTES(cannot_pass_type_to_non_ephemeral, "temporary-pointers.md")
28+
EDUCATIONAL_NOTES(cannot_pass_type_to_non_ephemeral_warning,
29+
"temporary-pointers.md")
30+
EDUCATIONAL_NOTES(cannot_use_inout_non_ephemeral,
31+
"temporary-pointers.md")
32+
EDUCATIONAL_NOTES(cannot_use_inout_non_ephemeral_warning,
33+
"temporary-pointers.md")
34+
EDUCATIONAL_NOTES(cannot_construct_dangling_pointer, "temporary-pointers.md")
35+
EDUCATIONAL_NOTES(cannot_construct_dangling_pointer_warning,
36+
"temporary-pointers.md")
37+
38+
39+
2740
EDUCATIONAL_NOTES(non_nominal_no_initializers, "nominal-types.md")
2841
EDUCATIONAL_NOTES(non_nominal_extension, "nominal-types.md")
2942
EDUCATIONAL_NOTES(associated_type_witness_conform_impossible,

include/swift/IDE/CodeCompletion.h

+16-6
Original file line numberDiff line numberDiff line change
@@ -594,6 +594,7 @@ class CodeCompletionResult {
594594
unsigned SemanticContext : 3;
595595
unsigned NotRecommended : 1;
596596
unsigned NotRecReason : 3;
597+
unsigned IsSystem : 1;
597598

598599
/// The number of bytes to the left of the code completion point that
599600
/// should be erased first if this completion string is inserted in the
@@ -634,6 +635,7 @@ class CodeCompletionResult {
634635
assert(!isOperator() ||
635636
getOperatorKind() != CodeCompletionOperatorKind::None);
636637
AssociatedKind = 0;
638+
IsSystem = 0;
637639
}
638640

639641
/// Constructs a \c Keyword result.
@@ -651,6 +653,7 @@ class CodeCompletionResult {
651653
TypeDistance(TypeDistance) {
652654
assert(CompletionString);
653655
AssociatedKind = static_cast<unsigned>(Kind);
656+
IsSystem = 0;
654657
}
655658

656659
/// Constructs a \c Literal result.
@@ -667,6 +670,7 @@ class CodeCompletionResult {
667670
NumBytesToErase(NumBytesToErase), CompletionString(CompletionString),
668671
TypeDistance(TypeDistance) {
669672
AssociatedKind = static_cast<unsigned>(LiteralKind);
673+
IsSystem = 0;
670674
assert(CompletionString);
671675
}
672676

@@ -694,6 +698,7 @@ class CodeCompletionResult {
694698
TypeDistance(TypeDistance) {
695699
assert(AssociatedDecl && "should have a decl");
696700
AssociatedKind = unsigned(getCodeCompletionDeclKind(AssociatedDecl));
701+
IsSystem = getDeclIsSystem(AssociatedDecl);
697702
assert(CompletionString);
698703
if (isOperator())
699704
KnownOperatorKind =
@@ -706,8 +711,8 @@ class CodeCompletionResult {
706711
CodeCompletionResult(SemanticContextKind SemanticContext,
707712
unsigned NumBytesToErase,
708713
CodeCompletionString *CompletionString,
709-
CodeCompletionDeclKind DeclKind, StringRef ModuleName,
710-
bool NotRecommended,
714+
CodeCompletionDeclKind DeclKind, bool IsSystem,
715+
StringRef ModuleName, bool NotRecommended,
711716
CodeCompletionResult::NotRecommendedReason NotRecReason,
712717
StringRef BriefDocComment,
713718
ArrayRef<StringRef> AssociatedUSRs,
@@ -718,10 +723,10 @@ class CodeCompletionResult {
718723
KnownOperatorKind(unsigned(KnownOperatorKind)),
719724
SemanticContext(unsigned(SemanticContext)),
720725
NotRecommended(NotRecommended), NotRecReason(NotRecReason),
721-
NumBytesToErase(NumBytesToErase), CompletionString(CompletionString),
722-
ModuleName(ModuleName), BriefDocComment(BriefDocComment),
723-
AssociatedUSRs(AssociatedUSRs), DocWords(DocWords),
724-
TypeDistance(TypeDistance) {
726+
IsSystem(IsSystem), NumBytesToErase(NumBytesToErase),
727+
CompletionString(CompletionString), ModuleName(ModuleName),
728+
BriefDocComment(BriefDocComment), AssociatedUSRs(AssociatedUSRs),
729+
DocWords(DocWords), TypeDistance(TypeDistance) {
725730
AssociatedKind = static_cast<unsigned>(DeclKind);
726731
assert(CompletionString);
727732
assert(!isOperator() ||
@@ -763,6 +768,10 @@ class CodeCompletionResult {
763768
return static_cast<CodeCompletionOperatorKind>(KnownOperatorKind);
764769
}
765770

771+
bool isSystem() const {
772+
return static_cast<bool>(IsSystem);
773+
}
774+
766775
ExpectedTypeRelation getExpectedTypeRelation() const {
767776
return static_cast<ExpectedTypeRelation>(TypeDistance);
768777
}
@@ -810,6 +819,7 @@ class CodeCompletionResult {
810819
getCodeCompletionOperatorKind(StringRef name);
811820
static CodeCompletionOperatorKind
812821
getCodeCompletionOperatorKind(CodeCompletionString *str);
822+
static bool getDeclIsSystem(const Decl *D);
813823
};
814824

815825
struct CodeCompletionResultSink {

lib/AST/GenericSignature.cpp

+4-4
Original file line numberDiff line numberDiff line change
@@ -411,7 +411,8 @@ bool GenericSignatureImpl::requiresClass(Type type) {
411411

412412
/// Determine the superclass bound on the given dependent type.
413413
Type GenericSignatureImpl::getSuperclassBound(Type type) {
414-
if (!type->isTypeParameter()) return nullptr;
414+
assert(type->isTypeParameter() &&
415+
"Only type parameters can have superclass requirements");
415416

416417
auto &builder = *getGenericSignatureBuilder();
417418
auto equivClass =
@@ -457,8 +458,7 @@ GenericSignatureImpl::getConformsTo(Type type) {
457458
}
458459

459460
bool GenericSignatureImpl::conformsToProtocol(Type type, ProtocolDecl *proto) {
460-
// FIXME: Deal with concrete conformances here?
461-
if (!type->isTypeParameter()) return false;
461+
assert(type->isTypeParameter() && "Expected a type parameter");
462462

463463
auto &builder = *getGenericSignatureBuilder();
464464
auto equivClass =
@@ -562,7 +562,7 @@ bool GenericSignatureImpl::isRequirementSatisfied(Requirement requirement) {
562562
// requirement, but it could also be in terms of concrete types if it has
563563
// been substituted/otherwise 'resolved', so we need to handle both.
564564
auto baseType = canFirstType;
565-
if (canFirstType->isTypeParameter()) {
565+
if (baseType->isTypeParameter()) {
566566
auto directSuperclass = getSuperclassBound(baseType);
567567
if (!directSuperclass)
568568
return false;

lib/IDE/CodeCompletion.cpp

+11-3
Original file line numberDiff line numberDiff line change
@@ -539,6 +539,10 @@ CodeCompletionResult::getCodeCompletionDeclKind(const Decl *D) {
539539
llvm_unreachable("invalid DeclKind");
540540
}
541541

542+
bool CodeCompletionResult::getDeclIsSystem(const Decl *D) {
543+
return D->getModuleContext()->isSystemModule();
544+
}
545+
542546
void CodeCompletionResult::printPrefix(raw_ostream &OS) const {
543547
llvm::SmallString<64> Prefix;
544548
switch (getKind()) {
@@ -700,6 +704,8 @@ void CodeCompletionResult::printPrefix(raw_ostream &OS) const {
700704
}
701705
if (NotRecommended)
702706
Prefix.append("/NotRecommended");
707+
if (IsSystem)
708+
Prefix.append("/IsSystem");
703709
if (NumBytesToErase != 0) {
704710
Prefix.append("/Erase[");
705711
Prefix.append(Twine(NumBytesToErase).str());
@@ -4714,9 +4720,11 @@ class CompletionOverrideLookup : public swift::VisibleDeclConsumer {
47144720
else {
47154721
auto dist = Ctx.SourceMgr.getByteDistance(
47164722
introducerLoc, Ctx.SourceMgr.getCodeCompletionLoc());
4717-
Builder.setNumBytesToErase(dist);
4718-
Builder.addOverrideKeyword();
4719-
Builder.addDeclIntroducer(DeclStr.str().substr(0, NameOffset));
4723+
if (dist <= CodeCompletionResult::MaxNumBytesToErase) {
4724+
Builder.setNumBytesToErase(dist);
4725+
Builder.addOverrideKeyword();
4726+
Builder.addDeclIntroducer(DeclStr.str().substr(0, NameOffset));
4727+
}
47204728
}
47214729
}
47224730

lib/IDE/CodeCompletionCache.cpp

+3-1
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,7 @@ static bool readCachedModule(llvm::MemoryBuffer *in,
215215
auto opKind = static_cast<CodeCompletionOperatorKind>(*cursor++);
216216
auto context = static_cast<SemanticContextKind>(*cursor++);
217217
auto notRecommended = static_cast<bool>(*cursor++);
218+
auto isSystem = static_cast<bool>(*cursor++);
218219
auto numBytesToErase = static_cast<unsigned>(*cursor++);
219220
auto oldCursor = cursor;
220221
auto chunkIndex = read32le(cursor);
@@ -248,7 +249,7 @@ static bool readCachedModule(llvm::MemoryBuffer *in,
248249
CodeCompletionResult *result = nullptr;
249250
if (kind == CodeCompletionResult::Declaration) {
250251
result = new (*V.Sink.Allocator) CodeCompletionResult(
251-
context, numBytesToErase, string, declKind, moduleName,
252+
context, numBytesToErase, string, declKind, isSystem, moduleName,
252253
notRecommended, CodeCompletionResult::NotRecommendedReason::NoReason,
253254
briefDocComment, copyStringArray(*V.Sink.Allocator, assocUSRs),
254255
copyStringPairArray(*V.Sink.Allocator, declKeywords),
@@ -371,6 +372,7 @@ static void writeCachedModule(llvm::raw_ostream &out,
371372
LE.write(static_cast<uint8_t>(CodeCompletionOperatorKind::None));
372373
LE.write(static_cast<uint8_t>(R->getSemanticContext()));
373374
LE.write(static_cast<uint8_t>(R->isNotRecommended()));
375+
LE.write(static_cast<uint8_t>(R->isSystem()));
374376
LE.write(static_cast<uint8_t>(R->getNumBytesToErase()));
375377
LE.write(
376378
static_cast<uint32_t>(addCompletionString(R->getCompletionString())));

lib/IRGen/GenConstant.cpp

+37
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,41 @@ llvm::Constant *irgen::emitConstantInt(IRGenModule &IGM,
5656
return llvm::ConstantInt::get(IGM.getLLVMContext(), value);
5757
}
5858

59+
llvm::Constant *irgen::emitConstantZero(IRGenModule &IGM, BuiltinInst *BI) {
60+
assert(IGM.getSILModule().getBuiltinInfo(BI->getName()).ID ==
61+
BuiltinValueKind::ZeroInitializer);
62+
63+
auto helper = [&](CanType astType) -> llvm::Constant * {
64+
if (auto type = astType->getAs<BuiltinIntegerType>()) {
65+
APInt zero(type->getWidth().getLeastWidth(), 0);
66+
return llvm::ConstantInt::get(IGM.getLLVMContext(), zero);
67+
}
68+
69+
if (auto type = astType->getAs<BuiltinFloatType>()) {
70+
const llvm::fltSemantics *sema = nullptr;
71+
switch (type->getFPKind()) {
72+
case BuiltinFloatType::IEEE16: sema = &APFloat::IEEEhalf(); break;
73+
case BuiltinFloatType::IEEE32: sema = &APFloat::IEEEsingle(); break;
74+
case BuiltinFloatType::IEEE64: sema = &APFloat::IEEEdouble(); break;
75+
case BuiltinFloatType::IEEE80: sema = &APFloat::x87DoubleExtended(); break;
76+
case BuiltinFloatType::IEEE128: sema = &APFloat::IEEEquad(); break;
77+
case BuiltinFloatType::PPC128: sema = &APFloat::PPCDoubleDouble(); break;
78+
}
79+
auto zero = APFloat::getZero(*sema);
80+
return llvm::ConstantFP::get(IGM.getLLVMContext(), zero);
81+
}
82+
83+
llvm_unreachable("SIL allowed an unknown type?");
84+
};
85+
86+
if (auto vector = BI->getType().getAs<BuiltinVectorType>()) {
87+
auto zero = helper(vector.getElementType());
88+
return llvm::ConstantVector::getSplat(vector->getNumElements(), zero);
89+
}
90+
91+
return helper(BI->getType().getASTType());
92+
}
93+
5994
llvm::Constant *irgen::emitConstantFP(IRGenModule &IGM, FloatLiteralInst *FLI) {
6095
return llvm::ConstantFP::get(IGM.getLLVMContext(), FLI->getValue());
6196
}
@@ -94,6 +129,8 @@ static llvm::Constant *emitConstantValue(IRGenModule &IGM, SILValue operand) {
94129
return emitAddrOfConstantString(IGM, SLI);
95130
} else if (auto *BI = dyn_cast<BuiltinInst>(operand)) {
96131
switch (IGM.getSILModule().getBuiltinInfo(BI->getName()).ID) {
132+
case BuiltinValueKind::ZeroInitializer:
133+
return emitConstantZero(IGM, BI);
97134
case BuiltinValueKind::PtrToInt: {
98135
llvm::Constant *ptr = emitConstantValue(IGM, BI->getArguments()[0]);
99136
return llvm::ConstantExpr::getPtrToInt(ptr, IGM.IntPtrTy);

lib/IRGen/GenConstant.h

+3
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,9 @@ namespace irgen {
2727
/// Construct a ConstantInt from an IntegerLiteralInst.
2828
llvm::Constant *emitConstantInt(IRGenModule &IGM, IntegerLiteralInst *ILI);
2929

30+
/// Construct a zero from a zero intializer BuiltinInst.
31+
llvm::Constant *emitConstantZero(IRGenModule &IGM, BuiltinInst *Bi);
32+
3033
/// Construct a ConstantFP from a FloatLiteralInst.
3134
llvm::Constant *emitConstantFP(IRGenModule &IGM, FloatLiteralInst *FLI);
3235

lib/SIL/IR/SILGlobalVariable.cpp

+6
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,12 @@ bool SILGlobalVariable::isValidStaticInitializerInst(const SILInstruction *I,
109109
case SILInstructionKind::BuiltinInst: {
110110
auto *bi = cast<BuiltinInst>(I);
111111
switch (M.getBuiltinInfo(bi->getName()).ID) {
112+
case BuiltinValueKind::ZeroInitializer: {
113+
auto type = bi->getType().getASTType();
114+
if (auto vector = dyn_cast<BuiltinVectorType>(type))
115+
type = vector.getElementType();
116+
return isa<BuiltinIntegerType>(type) || isa<BuiltinFloatType>(type);
117+
}
112118
case BuiltinValueKind::PtrToInt:
113119
if (isa<LiteralInst>(bi->getArguments()[0]))
114120
return true;

lib/SILOptimizer/Differentiation/PullbackEmitter.cpp

+14-2
Original file line numberDiff line numberDiff line change
@@ -1331,6 +1331,7 @@ void PullbackEmitter::visitSILInstruction(SILInstruction *inst) {
13311331
AllocStackInst *
13321332
PullbackEmitter::getArrayAdjointElementBuffer(SILValue arrayAdjoint,
13331333
int eltIndex, SILLocation loc) {
1334+
auto &ctx = builder.getASTContext();
13341335
auto arrayTanType = cast<StructType>(arrayAdjoint->getType().getASTType());
13351336
auto arrayType = arrayTanType->getParent()->castTo<BoundGenericStructType>();
13361337
auto eltTanType = arrayType->getGenericArgs().front()->getCanonicalType();
@@ -1340,7 +1341,19 @@ PullbackEmitter::getArrayAdjointElementBuffer(SILValue arrayAdjoint,
13401341
auto *arrayTanStructDecl = arrayTanType->getStructOrBoundGenericStruct();
13411342
auto subscriptLookup =
13421343
arrayTanStructDecl->lookupDirect(DeclBaseName::createSubscript());
1343-
auto *subscriptDecl = cast<SubscriptDecl>(subscriptLookup.front());
1344+
SubscriptDecl *subscriptDecl = nullptr;
1345+
for (auto *candidate : subscriptLookup) {
1346+
auto candidateModule = candidate->getModuleContext();
1347+
if (candidateModule->getName() == ctx.Id_Differentiation ||
1348+
candidateModule->isStdlibModule()) {
1349+
assert(!subscriptDecl && "Multiple `Array.TangentVector.subscript`s");
1350+
subscriptDecl = cast<SubscriptDecl>(candidate);
1351+
#ifdef NDEBUG
1352+
break;
1353+
#endif
1354+
}
1355+
}
1356+
assert(subscriptDecl && "No `Array.TangentVector.subscript`");
13441357
auto *subscriptGetterDecl = subscriptDecl->getAccessor(AccessorKind::Get);
13451358
assert(subscriptGetterDecl && "No `Array.TangentVector.subscript` getter");
13461359
SILOptFunctionBuilder fb(getContext().getTransform());
@@ -1352,7 +1365,6 @@ PullbackEmitter::getArrayAdjointElementBuffer(SILValue arrayAdjoint,
13521365
subscriptGetterFn->getLoweredFunctionType()->getSubstGenericSignature();
13531366
// Apply `Array.TangentVector.subscript.getter` to get array element adjoint
13541367
// buffer.
1355-
auto &ctx = builder.getASTContext();
13561368
// %index_literal = integer_literal $Builtin.IntXX, <index>
13571369
auto builtinIntType =
13581370
SILType::getPrimitiveObjectType(ctx.getIntDecl()

lib/SILOptimizer/SILCombiner/SILCombinerCastVisitors.cpp

+8
Original file line numberDiff line numberDiff line change
@@ -452,6 +452,10 @@ SILCombiner::visitThickToObjCMetatypeInst(ThickToObjCMetatypeInst *TTOCMI) {
452452
if (CastOpt.optimizeMetatypeConversion(TTOCMI, MetatypeRepresentation::Thick))
453453
MadeChange = true;
454454

455+
if (auto *OCTTMI = dyn_cast<ObjCToThickMetatypeInst>(TTOCMI->getOperand())) {
456+
TTOCMI->replaceAllUsesWith(OCTTMI->getOperand());
457+
return eraseInstFromFunction(*TTOCMI);
458+
}
455459
return nullptr;
456460
}
457461

@@ -472,6 +476,10 @@ SILCombiner::visitObjCToThickMetatypeInst(ObjCToThickMetatypeInst *OCTTMI) {
472476
if (CastOpt.optimizeMetatypeConversion(OCTTMI, MetatypeRepresentation::ObjC))
473477
MadeChange = true;
474478

479+
if (auto *TTOCMI = dyn_cast<ThickToObjCMetatypeInst>(OCTTMI->getOperand())) {
480+
OCTTMI->replaceAllUsesWith(TTOCMI->getOperand());
481+
return eraseInstFromFunction(*OCTTMI);
482+
}
475483
return nullptr;
476484
}
477485

lib/SILOptimizer/Transforms/DeadCodeElimination.cpp

+9
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,11 @@ namespace {
4040
// FIXME: Reconcile the similarities between this and
4141
// isInstructionTriviallyDead.
4242
static bool seemsUseful(SILInstruction *I) {
43+
// begin_access is defined to have side effects, but this is not relevant for
44+
// DCE.
45+
if (isa<BeginAccessInst>(I))
46+
return false;
47+
4348
if (I->mayHaveSideEffects())
4449
return true;
4550

@@ -258,6 +263,10 @@ void DCE::markLive(SILFunction &F) {
258263
}
259264
continue;
260265
}
266+
if (auto *endAccess = dyn_cast<EndAccessInst>(&I)) {
267+
addReverseDependency(endAccess->getBeginAccess(), &I);
268+
continue;
269+
}
261270
if (seemsUseful(&I))
262271
markValueLive(&I);
263272
}

stdlib/public/core/Slice.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ public struct Slice<Base: Collection> {
127127
///
128128
/// print(singleNonZeroDigits.count)
129129
/// // Prints "9"
130-
/// prints(singleNonZeroDigits.base.count)
130+
/// print(singleNonZeroDigits.base.count)
131131
/// // Prints "10"
132132
/// print(singleDigits == singleNonZeroDigits.base)
133133
/// // Prints "true"

test/AutoDiff/stdlib/array.swift

+9
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,15 @@ var ArrayAutoDiffTests = TestSuite("ArrayAutoDiff")
88

99
typealias FloatArrayTan = Array<Float>.TangentVector
1010

11+
extension Array.DifferentiableView {
12+
/// A subscript that always fatal errors.
13+
///
14+
/// The differentiation transform should never emit calls to this.
15+
subscript(alwaysFatalError: Int) -> Element {
16+
fatalError("wrong subscript")
17+
}
18+
}
19+
1120
ArrayAutoDiffTests.test("ArrayIdentity") {
1221
func arrayIdentity(_ x: [Float]) -> [Float] {
1322
return x

0 commit comments

Comments
 (0)