Skip to content

Commit 9e056a1

Browse files
committed
[SILGen] NFC: Convert intrinsic function lookup into a request
1 parent e7475c8 commit 9e056a1

File tree

5 files changed

+72
-126
lines changed

5 files changed

+72
-126
lines changed

include/swift/AST/NameLookupRequests.h

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -931,6 +931,24 @@ class ImplementsAttrProtocolRequest
931931
bool isCached() const { return true; }
932932
};
933933

934+
class LookupIntrinsicRequest
935+
: public SimpleRequest<LookupIntrinsicRequest,
936+
FuncDecl *(ModuleDecl *, Identifier),
937+
RequestFlags::Cached> {
938+
public:
939+
using SimpleRequest::SimpleRequest;
940+
941+
private:
942+
friend SimpleRequest;
943+
944+
// Evaluation.
945+
FuncDecl *evaluate(Evaluator &evaluator, ModuleDecl *module,
946+
Identifier funcName) const;
947+
948+
public:
949+
bool isCached() const { return true; }
950+
};
951+
934952
#define SWIFT_TYPEID_ZONE NameLookup
935953
#define SWIFT_TYPEID_HEADER "swift/AST/NameLookupTypeIDZone.def"
936954
#include "swift/Basic/DefineTypeIDZone.h"

include/swift/AST/NameLookupTypeIDZone.def

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,3 +111,5 @@ SWIFT_REQUEST(NameLookup, PotentialMacroExpansionsInContextRequest,
111111
PotentialMacroExpansions(TypeOrExtension), Cached, NoLocationInfo)
112112
SWIFT_REQUEST(NameLookup, ImplementsAttrProtocolRequest,
113113
ProtocolDecl *(const ImplementsAttr *, DeclContext *), Cached, NoLocationInfo)
114+
SWIFT_REQUEST(NameLookup, LookupIntrinsicRequest,
115+
FuncDecl *(ModuleDecl *, Identifier), Cached, NoLocationInfo)

lib/AST/NameLookup.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3888,6 +3888,19 @@ ProtocolDecl *ImplementsAttrProtocolRequest::evaluate(
38883888
return dyn_cast<ProtocolDecl>(nominalTypes.front());
38893889
}
38903890

3891+
FuncDecl *LookupIntrinsicRequest::evaluate(Evaluator &evaluator,
3892+
ModuleDecl *module,
3893+
Identifier funcName) const {
3894+
llvm::SmallVector<ValueDecl *, 1> decls;
3895+
module->lookupQualified(module, DeclNameRef(funcName), SourceLoc(),
3896+
NL_QualifiedDefault | NL_IncludeUsableFromInline,
3897+
decls);
3898+
if (decls.size() != 1)
3899+
return nullptr;
3900+
3901+
return dyn_cast<FuncDecl>(decls[0]);
3902+
}
3903+
38913904
void FindLocalVal::checkPattern(const Pattern *Pat, DeclVisibilityKind Reason) {
38923905
Pat->forEachVariable([&](VarDecl *VD) { checkValueDecl(VD, Reason); });
38933906
}

lib/SILGen/SILGen.cpp

Lines changed: 39 additions & 101 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
#include "swift/AST/GenericEnvironment.h"
2525
#include "swift/AST/Initializer.h"
2626
#include "swift/AST/NameLookup.h"
27+
#include "swift/AST/NameLookupRequests.h"
2728
#include "swift/AST/ParameterList.h"
2829
#include "swift/AST/PrettyStackTrace.h"
2930
#include "swift/AST/PropertyWrappers.h"
@@ -364,164 +365,101 @@ SILGenModule::getConformanceToBridgedStoredNSError(SILLocation loc, Type type) {
364365
return SwiftModule->lookupConformance(type, proto);
365366
}
366367

367-
static FuncDecl *lookupIntrinsic(ModuleDecl &module,
368-
llvm::Optional<FuncDecl *> &cache,
369-
Identifier name) {
370-
if (cache)
371-
return *cache;
372-
373-
SmallVector<ValueDecl *, 1> decls;
374-
module.lookupQualified(&module, DeclNameRef(name), SourceLoc(),
375-
NL_QualifiedDefault | NL_IncludeUsableFromInline,
376-
decls);
377-
if (decls.size() != 1) {
378-
cache = nullptr;
379-
return nullptr;
380-
}
381-
auto func = dyn_cast<FuncDecl>(decls[0]);
382-
cache = func;
383-
return func;
384-
}
385-
386-
static FuncDecl *lookupConcurrencyIntrinsic(ASTContext &C,
387-
llvm::Optional<FuncDecl *> &cache,
388-
StringRef name) {
368+
static FuncDecl *lookupConcurrencyIntrinsic(ASTContext &C, StringRef name) {
389369
auto *module = C.getLoadedModule(C.Id_Concurrency);
390-
if (!module) {
391-
cache = nullptr;
370+
if (!module)
392371
return nullptr;
393-
}
394372

395-
return lookupIntrinsic(*module, cache, C.getIdentifier(name));
373+
return evaluateOrDefault(
374+
C.evaluator, LookupIntrinsicRequest{module, C.getIdentifier(name)},
375+
/*default=*/nullptr);
396376
}
397377

398-
FuncDecl *
399-
SILGenModule::getAsyncLetStart() {
400-
return lookupConcurrencyIntrinsic(getASTContext(),
401-
AsyncLetStart,
402-
"_asyncLetStart");
378+
FuncDecl *SILGenModule::getAsyncLetStart() {
379+
return lookupConcurrencyIntrinsic(getASTContext(), "_asyncLetStart");
403380
}
404381

405-
FuncDecl *
406-
SILGenModule::getAsyncLetGet() {
407-
return lookupConcurrencyIntrinsic(getASTContext(),
408-
AsyncLetGet,
409-
"_asyncLet_get");
382+
FuncDecl *SILGenModule::getAsyncLetGet() {
383+
return lookupConcurrencyIntrinsic(getASTContext(), "_asyncLet_get");
410384
}
411385

412-
FuncDecl *
413-
SILGenModule::getAsyncLetGetThrowing() {
414-
return lookupConcurrencyIntrinsic(getASTContext(),
415-
AsyncLetGetThrowing,
416-
"_asyncLet_get_throwing");
386+
FuncDecl *SILGenModule::getAsyncLetGetThrowing() {
387+
return lookupConcurrencyIntrinsic(getASTContext(), "_asyncLet_get_throwing");
417388
}
418389

419-
FuncDecl *
420-
SILGenModule::getFinishAsyncLet() {
421-
return lookupConcurrencyIntrinsic(getASTContext(),
422-
EndAsyncLet,
423-
"_asyncLet_finish");
390+
FuncDecl *SILGenModule::getFinishAsyncLet() {
391+
return lookupConcurrencyIntrinsic(getASTContext(), "_asyncLet_finish");
424392
}
425393

426-
FuncDecl *
427-
SILGenModule::getTaskFutureGet() {
428-
return lookupConcurrencyIntrinsic(getASTContext(),
429-
TaskFutureGet,
430-
"_taskFutureGet");
394+
FuncDecl *SILGenModule::getTaskFutureGet() {
395+
return lookupConcurrencyIntrinsic(getASTContext(), "_taskFutureGet");
431396
}
432397

433-
FuncDecl *
434-
SILGenModule::getTaskFutureGetThrowing() {
435-
return lookupConcurrencyIntrinsic(getASTContext(),
436-
TaskFutureGetThrowing,
437-
"_taskFutureGetThrowing");
398+
FuncDecl *SILGenModule::getTaskFutureGetThrowing() {
399+
return lookupConcurrencyIntrinsic(getASTContext(), "_taskFutureGetThrowing");
438400
}
439401

440-
FuncDecl *
441-
SILGenModule::getResumeUnsafeContinuation() {
402+
FuncDecl *SILGenModule::getResumeUnsafeContinuation() {
442403
return lookupConcurrencyIntrinsic(getASTContext(),
443-
ResumeUnsafeContinuation,
444404
"_resumeUnsafeContinuation");
445405
}
446-
FuncDecl *
447-
SILGenModule::getResumeUnsafeThrowingContinuation() {
406+
FuncDecl *SILGenModule::getResumeUnsafeThrowingContinuation() {
448407
return lookupConcurrencyIntrinsic(getASTContext(),
449-
ResumeUnsafeThrowingContinuation,
450408
"_resumeUnsafeThrowingContinuation");
451409
}
452-
FuncDecl *
453-
SILGenModule::getResumeUnsafeThrowingContinuationWithError() {
454-
return lookupConcurrencyIntrinsic(getASTContext(),
455-
ResumeUnsafeThrowingContinuationWithError,
456-
"_resumeUnsafeThrowingContinuationWithError");
410+
FuncDecl *SILGenModule::getResumeUnsafeThrowingContinuationWithError() {
411+
return lookupConcurrencyIntrinsic(
412+
getASTContext(), "_resumeUnsafeThrowingContinuationWithError");
457413
}
458-
FuncDecl *
459-
SILGenModule::getRunTaskForBridgedAsyncMethod() {
414+
FuncDecl *SILGenModule::getRunTaskForBridgedAsyncMethod() {
460415
return lookupConcurrencyIntrinsic(getASTContext(),
461-
RunTaskForBridgedAsyncMethod,
462416
"_runTaskForBridgedAsyncMethod");
463417
}
464-
FuncDecl *
465-
SILGenModule::getCheckExpectedExecutor() {
466-
return lookupConcurrencyIntrinsic(getASTContext(), CheckExpectedExecutor,
467-
"_checkExpectedExecutor");
418+
FuncDecl *SILGenModule::getCheckExpectedExecutor() {
419+
return lookupConcurrencyIntrinsic(getASTContext(), "_checkExpectedExecutor");
468420
}
469421

470-
FuncDecl *
471-
SILGenModule::getCreateCheckedContinuation() {
422+
FuncDecl *SILGenModule::getCreateCheckedContinuation() {
472423
return lookupConcurrencyIntrinsic(getASTContext(),
473-
CreateCheckedContinuation,
474424
"_createCheckedContinuation");
475425
}
476-
FuncDecl *
477-
SILGenModule::getCreateCheckedThrowingContinuation() {
426+
FuncDecl *SILGenModule::getCreateCheckedThrowingContinuation() {
478427
return lookupConcurrencyIntrinsic(getASTContext(),
479-
CreateCheckedThrowingContinuation,
480428
"_createCheckedThrowingContinuation");
481429
}
482-
FuncDecl *
483-
SILGenModule::getResumeCheckedContinuation() {
430+
FuncDecl *SILGenModule::getResumeCheckedContinuation() {
484431
return lookupConcurrencyIntrinsic(getASTContext(),
485-
ResumeCheckedContinuation,
486432
"_resumeCheckedContinuation");
487433
}
488-
FuncDecl *
489-
SILGenModule::getResumeCheckedThrowingContinuation() {
434+
FuncDecl *SILGenModule::getResumeCheckedThrowingContinuation() {
490435
return lookupConcurrencyIntrinsic(getASTContext(),
491-
ResumeCheckedThrowingContinuation,
492436
"_resumeCheckedThrowingContinuation");
493437
}
494-
FuncDecl *
495-
SILGenModule::getResumeCheckedThrowingContinuationWithError() {
438+
FuncDecl *SILGenModule::getResumeCheckedThrowingContinuationWithError() {
496439
return lookupConcurrencyIntrinsic(
497-
getASTContext(), ResumeCheckedThrowingContinuationWithError,
498-
"_resumeCheckedThrowingContinuationWithError");
440+
getASTContext(), "_resumeCheckedThrowingContinuationWithError");
499441
}
500442

501443
FuncDecl *SILGenModule::getAsyncMainDrainQueue() {
502-
return lookupConcurrencyIntrinsic(getASTContext(), AsyncMainDrainQueue,
503-
"_asyncMainDrainQueue");
444+
return lookupConcurrencyIntrinsic(getASTContext(), "_asyncMainDrainQueue");
504445
}
505446

506447
FuncDecl *SILGenModule::getSwiftJobRun() {
507-
return lookupConcurrencyIntrinsic(getASTContext(), SwiftJobRun,
508-
"_swiftJobRun");
448+
return lookupConcurrencyIntrinsic(getASTContext(), "_swiftJobRun");
509449
}
510450

511451
FuncDecl *SILGenModule::getExit() {
512-
if (ExitFunc)
513-
return *ExitFunc;
514-
515452
ASTContext &C = getASTContext();
516453
ModuleDecl *concurrencyShims =
517454
C.getModuleByIdentifier(C.getIdentifier("_SwiftConcurrencyShims"));
518455

519-
if (!concurrencyShims) {
520-
ExitFunc = nullptr;
456+
if (!concurrencyShims)
521457
return nullptr;
522-
}
523458

524-
return lookupIntrinsic(*concurrencyShims, ExitFunc, C.getIdentifier("exit"));
459+
return evaluateOrDefault(
460+
C.evaluator,
461+
LookupIntrinsicRequest{concurrencyShims, C.getIdentifier("exit")},
462+
/*default=*/nullptr);
525463
}
526464

527465
ProtocolConformance *SILGenModule::getNSErrorConformanceToError() {

lib/SILGen/SILGen.h

Lines changed: 0 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -114,31 +114,6 @@ class LLVM_LIBRARY_VISIBILITY SILGenModule : public ASTVisitor<SILGenModule> {
114114

115115
llvm::Optional<ProtocolConformance *> NSErrorConformanceToError;
116116

117-
llvm::Optional<FuncDecl *> AsyncLetStart;
118-
llvm::Optional<FuncDecl *> AsyncLetGet;
119-
llvm::Optional<FuncDecl *> AsyncLetGetThrowing;
120-
llvm::Optional<FuncDecl *> EndAsyncLet;
121-
122-
llvm::Optional<FuncDecl *> TaskFutureGet;
123-
llvm::Optional<FuncDecl *> TaskFutureGetThrowing;
124-
125-
llvm::Optional<FuncDecl *> RunTaskForBridgedAsyncMethod;
126-
llvm::Optional<FuncDecl *> ResumeUnsafeContinuation;
127-
llvm::Optional<FuncDecl *> ResumeUnsafeThrowingContinuation;
128-
llvm::Optional<FuncDecl *> ResumeUnsafeThrowingContinuationWithError;
129-
llvm::Optional<FuncDecl *> CheckExpectedExecutor;
130-
131-
llvm::Optional<FuncDecl*> CreateCheckedContinuation;
132-
llvm::Optional<FuncDecl*> CreateCheckedThrowingContinuation;
133-
llvm::Optional<FuncDecl*> ResumeCheckedContinuation;
134-
llvm::Optional<FuncDecl*> ResumeCheckedThrowingContinuation;
135-
llvm::Optional<FuncDecl*> ResumeCheckedThrowingContinuationWithError;
136-
137-
llvm::Optional<FuncDecl *> AsyncMainDrainQueue;
138-
llvm::Optional<FuncDecl *> GetMainExecutor;
139-
llvm::Optional<FuncDecl *> SwiftJobRun;
140-
llvm::Optional<FuncDecl *> ExitFunc;
141-
142117
public:
143118
SILGenModule(SILModule &M, ModuleDecl *SM);
144119

0 commit comments

Comments
 (0)