Skip to content

Commit f7ddc20

Browse files
Merge pull request #4544 from swiftwasm/main
[pull] swiftwasm from main
2 parents 37e25be + 0c33688 commit f7ddc20

28 files changed

+1319
-1053
lines changed

include/swift/AST/Attr.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1938,6 +1938,10 @@ class DerivativeAttr final
19381938
friend TrailingObjects;
19391939
friend class DerivativeAttrOriginalDeclRequest;
19401940

1941+
/// The declaration on which the `@derivative` attribute is declared.
1942+
/// May not be a valid declaration for `@derivative` attributes.
1943+
/// Resolved during parsing and deserialization.
1944+
Decl *OriginalDeclaration = nullptr;
19411945
/// The base type for the referenced original declaration. This field is
19421946
/// non-null only for parsed attributes that reference a qualified original
19431947
/// declaration. This field is not serialized; type-checking uses it to
@@ -1991,6 +1995,12 @@ class DerivativeAttr final
19911995
DeclNameRefWithLoc original,
19921996
IndexSubset *parameterIndices);
19931997

1998+
Decl *getOriginalDeclaration() const { return OriginalDeclaration; }
1999+
2000+
/// Sets the original declaration on which this attribute is declared.
2001+
/// Should only be used by parsing and deserialization.
2002+
void setOriginalDeclaration(Decl *originalDeclaration);
2003+
19942004
TypeRepr *getBaseTypeRepr() const { return BaseTypeRepr; }
19952005
DeclNameRefWithLoc getOriginalFunctionName() const {
19962006
return OriginalFunctionName;

include/swift/AST/Decl.h

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6265,8 +6265,11 @@ class AbstractFunctionDecl : public GenericContext, public ValueDecl {
62656265
DerivativeFunctionConfigurationList *DerivativeFunctionConfigs = nullptr;
62666266

62676267
public:
6268-
/// Get all derivative function configurations.
6269-
ArrayRef<AutoDiffConfig> getDerivativeFunctionConfigurations();
6268+
/// Get all derivative function configurations. If `lookInNonPrimarySources`
6269+
/// is true then lookup is done in non-primary sources as well. Note that
6270+
/// such lookup might end in cycles if done during sema stages.
6271+
ArrayRef<AutoDiffConfig>
6272+
getDerivativeFunctionConfigurations(bool lookInNonPrimarySources = true);
62706273

62716274
/// Add the given derivative function configuration.
62726275
void addDerivativeFunctionConfiguration(const AutoDiffConfig &config);

include/swift/Basic/SourceManager.h

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
#include "swift/Basic/FileSystem.h"
1717
#include "swift/Basic/SourceLoc.h"
1818
#include "clang/Basic/FileManager.h"
19+
#include "llvm/ADT/DenseSet.h"
1920
#include "llvm/ADT/Optional.h"
2021
#include "llvm/Support/SourceMgr.h"
2122
#include <map>
@@ -55,6 +56,10 @@ class SourceManager {
5556
/// reusing compilation.
5657
llvm::DenseMap<SourceRange, SourceRange> ReplacedRanges;
5758

59+
/// The starting source locations of regex literals written in source. This
60+
/// is an unfortunate hack needed to allow for correct re-lexing.
61+
llvm::DenseSet<SourceLoc> RegexLiteralStartLocs;
62+
5863
std::map<const char *, VirtualFile> VirtualFiles;
5964
mutable std::pair<const char *, const VirtualFile*> CachedVFile = {nullptr, nullptr};
6065

@@ -107,6 +112,17 @@ class SourceManager {
107112
ReplacedRanges[Orig] = New;
108113
}
109114

115+
/// Record the starting source location of a regex literal.
116+
void recordRegexLiteralStartLoc(SourceLoc loc) {
117+
RegexLiteralStartLocs.insert(loc);
118+
}
119+
120+
/// Checks whether a given source location is for the start of a regex
121+
/// literal.
122+
bool isRegexLiteralStart(SourceLoc loc) const {
123+
return RegexLiteralStartLocs.contains(loc);
124+
}
125+
110126
/// Returns true if \c LHS is before \c RHS in the source buffer.
111127
bool isBeforeInBuffer(SourceLoc LHS, SourceLoc RHS) const {
112128
return LHS.Value.getPointer() < RHS.Value.getPointer();

lib/AST/Attr.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2107,6 +2107,13 @@ void DerivativeAttr::setOriginalFunctionResolver(
21072107
ResolverContextData = resolverContextData;
21082108
}
21092109

2110+
void DerivativeAttr::setOriginalDeclaration(Decl *originalDeclaration) {
2111+
assert(originalDeclaration && "Original declaration must be non-null");
2112+
assert(!OriginalDeclaration &&
2113+
"Original declaration cannot have already been set");
2114+
OriginalDeclaration = originalDeclaration;
2115+
}
2116+
21102117
TransposeAttr::TransposeAttr(bool implicit, SourceLoc atLoc,
21112118
SourceRange baseRange, TypeRepr *baseTypeRepr,
21122119
DeclNameRefWithLoc originalName,

lib/AST/Decl.cpp

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
#include "swift/AST/ASTContext.h"
2121
#include "swift/AST/ASTWalker.h"
2222
#include "swift/AST/ASTMangler.h"
23+
#include "swift/AST/Attr.h"
2324
#include "swift/AST/CaptureInfo.h"
2425
#include "swift/AST/DiagnosticEngine.h"
2526
#include "swift/AST/DiagnosticsSema.h"
@@ -8311,7 +8312,7 @@ void AbstractFunctionDecl::prepareDerivativeFunctionConfigurations() {
83118312
}
83128313

83138314
ArrayRef<AutoDiffConfig>
8314-
AbstractFunctionDecl::getDerivativeFunctionConfigurations() {
8315+
AbstractFunctionDecl::getDerivativeFunctionConfigurations(bool lookInNonPrimarySources) {
83158316
prepareDerivativeFunctionConfigurations();
83168317

83178318
// Resolve derivative function configurations from `@differentiable`
@@ -8334,6 +8335,37 @@ AbstractFunctionDecl::getDerivativeFunctionConfigurations() {
83348335
ctx.loadDerivativeFunctionConfigurations(this, previousGeneration,
83358336
*DerivativeFunctionConfigs);
83368337
}
8338+
8339+
class DerivativeFinder : public ASTWalker {
8340+
const AbstractFunctionDecl *AFD;
8341+
public:
8342+
DerivativeFinder(const AbstractFunctionDecl *afd) : AFD(afd) {}
8343+
8344+
bool walkToDeclPre(Decl *D) override {
8345+
if (auto *afd = dyn_cast<AbstractFunctionDecl>(D)) {
8346+
for (auto *derAttr : afd->getAttrs().getAttributes<DerivativeAttr>()) {
8347+
// Resolve derivative function configurations from `@derivative`
8348+
// attributes by type-checking them.
8349+
if (AFD->getName().matchesRef(
8350+
derAttr->getOriginalFunctionName().Name.getFullName())) {
8351+
(void)derAttr->getOriginalFunction(afd->getASTContext());
8352+
return false;
8353+
}
8354+
}
8355+
}
8356+
8357+
return true;
8358+
}
8359+
};
8360+
8361+
// Load derivative configurations from @derivative attributes defined in
8362+
// non-primary sources. Note that it might trigger lookup cycles if called
8363+
// from inside Sema stages.
8364+
if (lookInNonPrimarySources) {
8365+
DerivativeFinder finder(this);
8366+
getParent()->walkContext(finder);
8367+
}
8368+
83378369
return DerivativeFunctionConfigs->getArrayRef();
83388370
}
83398371

lib/IDE/CodeCompletionResultPrinter.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -496,6 +496,10 @@ static void printCodeCompletionResultFilterName(
496496
case ChunkKind::DeclIntroducer:
497497
++i;
498498
continue;
499+
case ChunkKind::ParameterDeclExternalName:
500+
// Skip '_' parameter external name.
501+
shouldPrint = shouldPrint && C.hasText() && C.getText() != "_";
502+
break;
499503
case ChunkKind::CallArgumentTypeBegin:
500504
case ChunkKind::ParameterDeclTypeBegin:
501505
case ChunkKind::TypeAnnotationBegin:

lib/Parse/Lexer.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2677,6 +2677,16 @@ Token Lexer::getTokenAtLocation(const SourceManager &SM, SourceLoc Loc,
26772677
// we need to lex just the comment token.
26782678
Lexer L(FakeLangOpts, SM, BufferID, nullptr, LexerMode::Swift,
26792679
HashbangMode::Allowed, CRM);
2680+
2681+
if (SM.isRegexLiteralStart(Loc)) {
2682+
// HACK: If this was previously lexed as a regex literal, make sure we
2683+
// re-lex with forward slash regex literals enabled to make sure we get an
2684+
// accurate length. We can force EnableExperimentalStringProcessing on, as
2685+
// we know it must have been enabled to parse the regex in the first place.
2686+
FakeLangOpts.EnableExperimentalStringProcessing = true;
2687+
L.ForwardSlashRegexMode = LexerForwardSlashRegexMode::Always;
2688+
}
2689+
26802690
L.restoreState(State(Loc));
26812691
return L.peekNextToken();
26822692
}

lib/Parse/ParseDecl.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4457,6 +4457,8 @@ setOriginalDeclarationForDifferentiableAttributes(DeclAttributes attrs,
44574457
Decl *D) {
44584458
for (auto *attr : attrs.getAttributes<DifferentiableAttr>())
44594459
const_cast<DifferentiableAttr *>(attr)->setOriginalDeclaration(D);
4460+
for (auto *attr : attrs.getAttributes<DerivativeAttr>())
4461+
const_cast<DerivativeAttr *>(attr)->setOriginalDeclaration(D);
44604462
}
44614463

44624464
/// Parse a single syntactic declaration and return a list of decl

lib/Parse/ParseRegex.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,8 @@ ParserResult<Expr> Parser::parseExprRegexLiteral() {
5454
/*diagBaseLoc*/ getBridgedSourceLoc(Tok.getLoc()),
5555
getBridgedDiagnosticEngine(&Diags));
5656
auto loc = consumeToken();
57+
SourceMgr.recordRegexLiteralStartLoc(loc);
58+
5759
if (hadError) {
5860
return makeParserResult(new (Context) ErrorExpr(loc));
5961
}

lib/SIL/IR/SILPrinter.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2774,6 +2774,10 @@ void SILInstruction::print(raw_ostream &OS) const {
27742774
SILPrinter(Ctx).print(this);
27752775
}
27762776

2777+
void NonSingleValueInstruction::dump() const {
2778+
SILNode::dump();
2779+
}
2780+
27772781
/// Pretty-print the SILBasicBlock to errs.
27782782
void SILBasicBlock::dump() const {
27792783
print(llvm::errs());

lib/SILOptimizer/Utils/InstOptUtils.cpp

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1801,13 +1801,17 @@ void swift::salvageDebugInfo(SILInstruction *I) {
18011801

18021802
if (auto *SI = dyn_cast<StoreInst>(I)) {
18031803
if (SILValue DestVal = SI->getDest())
1804-
// TODO: Generalize this into "get the attached debug info
1805-
// on `DestVal`".
18061804
if (auto *ASI = dyn_cast_or_null<AllocStackInst>(
18071805
DestVal.getDefiningInstruction())) {
1808-
if (auto VarInfo = ASI->getVarInfo())
1806+
if (auto VarInfo = ASI->getVarInfo()) {
1807+
// Always propagate destination location for incoming arguments (as
1808+
// their location must be unique) as well as when store source
1809+
// location is compiler-generated.
1810+
bool UseDestLoc = VarInfo->ArgNo || SI->getLoc().isAutoGenerated();
18091811
SILBuilder(SI, ASI->getDebugScope())
1810-
.createDebugValue(SI->getLoc(), SI->getSrc(), *VarInfo);
1812+
.createDebugValue(UseDestLoc ? ASI->getLoc() : SI->getLoc(),
1813+
SI->getSrc(), *VarInfo);
1814+
}
18111815
}
18121816
}
18131817

lib/Sema/PlaygroundTransform.cpp

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -576,12 +576,14 @@ class Instrumenter : InstrumenterBase {
576576
} else if (auto *D = Element.dyn_cast<Decl *>()) {
577577
D->walk(CF);
578578
if (auto *PBD = dyn_cast<PatternBindingDecl>(D)) {
579-
if (VarDecl *VD = PBD->getSingleVar()) {
580-
if (VD->getParentInitializer()) {
581-
Added<Stmt *> Log = logVarDecl(VD);
582-
if (*Log) {
583-
Elements.insert(Elements.begin() + (EI + 1), *Log);
584-
++EI;
579+
if (!PBD->isAsyncLet()) {
580+
if (VarDecl *VD = PBD->getSingleVar()) {
581+
if (VD->getParentInitializer()) {
582+
Added<Stmt *> Log = logVarDecl(VD);
583+
if (*Log) {
584+
Elements.insert(Elements.begin() + (EI + 1), *Log);
585+
++EI;
586+
}
585587
}
586588
}
587589
}

lib/Sema/TypeCheckAttr.cpp

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4956,10 +4956,11 @@ void AttributeChecker::visitDifferentiableAttr(DifferentiableAttr *attr) {
49564956
/// - Stores the attribute in `ASTContext::DerivativeAttrs`.
49574957
///
49584958
/// \returns true on error, false on success.
4959-
static bool typeCheckDerivativeAttr(ASTContext &Ctx, Decl *D,
4960-
DerivativeAttr *attr) {
4959+
static bool typeCheckDerivativeAttr(DerivativeAttr *attr) {
49614960
// Note: Implementation must be idempotent because it may be called multiple
49624961
// times for the same attribute.
4962+
Decl *D = attr->getOriginalDeclaration();
4963+
auto &Ctx = D->getASTContext();
49634964
auto &diags = Ctx.Diags;
49644965
// `@derivative` attribute requires experimental differentiable programming
49654966
// to be enabled.
@@ -5372,13 +5373,18 @@ static bool typeCheckDerivativeAttr(ASTContext &Ctx, Decl *D,
53725373
}
53735374

53745375
void AttributeChecker::visitDerivativeAttr(DerivativeAttr *attr) {
5375-
if (typeCheckDerivativeAttr(Ctx, D, attr))
5376+
if (typeCheckDerivativeAttr(attr))
53765377
attr->setInvalid();
53775378
}
53785379

53795380
AbstractFunctionDecl *
53805381
DerivativeAttrOriginalDeclRequest::evaluate(Evaluator &evaluator,
53815382
DerivativeAttr *attr) const {
5383+
// Try to resolve the original function.
5384+
if (attr->isValid() && attr->OriginalFunction.isNull())
5385+
if (typeCheckDerivativeAttr(attr))
5386+
attr->setInvalid();
5387+
53825388
// If the typechecker has resolved the original function, return it.
53835389
if (auto *FD = attr->OriginalFunction.dyn_cast<AbstractFunctionDecl *>())
53845390
return FD;

lib/Sema/TypeCheckProtocol.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -379,7 +379,8 @@ matchWitnessDifferentiableAttr(DeclContext *dc, ValueDecl *req,
379379
bool foundExactConfig = false;
380380
Optional<AutoDiffConfig> supersetConfig = None;
381381
for (auto witnessConfig :
382-
witnessAFD->getDerivativeFunctionConfigurations()) {
382+
witnessAFD->getDerivativeFunctionConfigurations(
383+
/*lookInNonPrimarySources*/ false)) {
383384
// All the witness's derivative generic requirements must be satisfied
384385
// by the requirement's derivative generic requirements OR by the
385386
// conditional conformance requirements.

lib/Serialization/DeclTypeRecordNodes.def

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,6 @@ TYPE(METATYPE)
9191
TYPE(PRIMARY_ARCHETYPE)
9292
TYPE(OPENED_ARCHETYPE)
9393
TYPE(OPAQUE_ARCHETYPE)
94-
TYPE(NESTED_ARCHETYPE)
9594
TYPE(SEQUENCE_ARCHETYPE)
9695
TYPE(PROTOCOL_COMPOSITION)
9796
TYPE(PARAMETERIZED_PROTOCOL)
@@ -107,7 +106,6 @@ TYPE(OPTIONAL)
107106
TYPE(VARIADIC_SEQUENCE)
108107
TYPE(SIL_FUNCTION)
109108
TYPE(DYNAMIC_SELF)
110-
TYPE(OPENED_EXISTENTIAL)
111109
TYPE(EXISTENTIAL_METATYPE)
112110
TYPE(SIL_BLOCK_STORAGE)
113111
TYPE(SIL_BOX)

0 commit comments

Comments
 (0)