Skip to content

Commit abdc4d9

Browse files
committed
SILGen: Use has_symbol instruction in SILGen.
1 parent 0a24042 commit abdc4d9

File tree

8 files changed

+62
-172
lines changed

8 files changed

+62
-172
lines changed

include/swift/AST/Decl.h

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2790,11 +2790,6 @@ class ValueDecl : public Decl {
27902790
/// 'func foo(Int) -> () -> Self?'.
27912791
GenericParameterReferenceInfo findExistentialSelfReferences(
27922792
Type baseTy, bool treatNonResultCovariantSelfAsInvariant) const;
2793-
2794-
/// Returns a synthesized declaration for a query function that provides
2795-
/// the boolean value for a `if #_hasSymbol(...)` condition. The interface
2796-
/// type of the function is `() -> Builtin.Int1`.
2797-
FuncDecl *getHasSymbolQueryDecl() const;
27982793
};
27992794

28002795
/// This is a common base class for declarations which declare a type.

include/swift/AST/TypeCheckRequests.h

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -3715,24 +3715,6 @@ class GetTypeWrapperInitializer
37153715
bool isCached() const { return true; }
37163716
};
37173717

3718-
/// Synthesizes and returns a `#_hasSymbol` query function for the given
3719-
/// `ValueDecl`. The function has an interface type of `() -> Builtin.Int1`.
3720-
class SynthesizeHasSymbolQueryRequest
3721-
: public SimpleRequest<SynthesizeHasSymbolQueryRequest,
3722-
FuncDecl *(const ValueDecl *),
3723-
RequestFlags::Cached> {
3724-
public:
3725-
using SimpleRequest::SimpleRequest;
3726-
3727-
private:
3728-
friend SimpleRequest;
3729-
3730-
FuncDecl *evaluate(Evaluator &evaluator, const ValueDecl *decl) const;
3731-
3732-
public:
3733-
bool isCached() const { return true; }
3734-
};
3735-
37363718
/// Lookup all macros with the given name that are visible from the given
37373719
/// module.
37383720
class MacroLookupRequest

include/swift/AST/TypeCheckerTypeIDZone.def

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -437,9 +437,6 @@ SWIFT_REQUEST(TypeChecker, SynthesizeLocalVariableForTypeWrapperStorage,
437437
SWIFT_REQUEST(TypeChecker, GetTypeWrapperInitializer,
438438
ConstructorDecl *(NominalTypeDecl *),
439439
Cached, NoLocationInfo)
440-
SWIFT_REQUEST(TypeChecker, SynthesizeHasSymbolQueryRequest,
441-
FuncDecl *(ValueDecl *),
442-
Cached, NoLocationInfo)
443440
SWIFT_REQUEST(TypeChecker, MacroLookupRequest,
444441
ArrayRef<Macro *>(Identifier, ModuleDecl *),
445442
Cached, NoLocationInfo)

include/swift/SIL/SILSymbolVisitor.h

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
#include "swift/AST/ProtocolAssociations.h"
1818
#include "swift/AST/ProtocolConformance.h"
1919
#include "swift/SIL/SILDeclRef.h"
20+
#include "swift/SIL/SILModule.h"
2021

2122
namespace swift {
2223

@@ -126,6 +127,33 @@ class SILSymbolVisitor {
126127
virtual void addTypeMetadataAddress(CanType T) {}
127128
};
128129

130+
template <typename F>
131+
void enumerateFunctionsForHasSymbol(SILModule &M, ValueDecl *D, F Handler) {
132+
class SymbolVisitor : public SILSymbolVisitor {
133+
F Handler;
134+
135+
public:
136+
SymbolVisitor(F Handler) : Handler{Handler} {};
137+
138+
void addFunction(SILDeclRef declRef) override { Handler(declRef); }
139+
140+
virtual void addFunction(StringRef name, SILDeclRef declRef) override {
141+
// The kinds of functions which go through this callback (e.g.
142+
// differentiability witnesses) have custom manglings and are incompatible
143+
// with #_hasSymbol currently.
144+
//
145+
// Ideally, this callback will be removed entirely in favor of SILDeclRef
146+
// being able to represent all function variants with no special cases
147+
// required.
148+
}
149+
};
150+
151+
SILSymbolVisitorOptions opts;
152+
opts.VisitMembers = false;
153+
auto visitorCtx = SILSymbolVisitorContext(M.getSwiftModule(), opts);
154+
SymbolVisitor(Handler).visitDecl(D, visitorCtx);
155+
}
156+
129157
} // end namespace swift
130158

131159
#endif

lib/SILGen/SILGenDecl.cpp

Lines changed: 5 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1616,43 +1616,17 @@ void SILGenFunction::emitStmtCondition(StmtCondition Cond, JumpDest FalseDest,
16161616
assert(declRef);
16171617

16181618
auto decl = declRef.getDecl();
1619-
getModule().addHasSymbolDecl(decl);
1620-
1621-
SILFunction *silFn = SGM.getFunction(
1622-
SILDeclRef(decl->getHasSymbolQueryDecl(), SILDeclRef::Kind::Func),
1623-
NotForDefinition);
1624-
SILValue fnRef = B.createFunctionRefFor(loc, silFn);
1625-
booleanTestValue = B.createApply(loc, fnRef, {}, {});
1619+
booleanTestValue = B.createHasSymbol(expr, decl);
16261620
booleanTestValue = emitUnwrapIntegerResult(expr, booleanTestValue);
16271621
booleanTestLoc = expr;
16281622

16291623
// Ensure that function declarations for each function associated with
16301624
// the decl are emitted so that they can be referenced during IRGen.
1631-
class SymbolVisitor : public SILSymbolVisitor {
1632-
SILGenModule &SGM;
1633-
1634-
public:
1635-
SymbolVisitor(SILGenModule &SGM) : SGM{SGM} {};
1636-
1637-
void addFunction(SILDeclRef declRef) override {
1638-
(void)SGM.getFunction(declRef, NotForDefinition);
1639-
}
1640-
1641-
virtual void addFunction(StringRef name, SILDeclRef declRef) override {
1642-
// The kinds of functions which go through this callback (e.g.
1643-
// differentiability witnesses) can't be forward declared with a
1644-
// SILDeclRef alone. For now, just ignore them.
1645-
//
1646-
// Ideally, this callback will be removed entirely in favor of
1647-
// SILDeclRef being able to represent all function variants.
1648-
}
1649-
};
1625+
enumerateFunctionsForHasSymbol(
1626+
getModule(), decl, [this](SILDeclRef declRef) {
1627+
(void)SGM.getFunction(declRef, NotForDefinition);
1628+
});
16501629

1651-
SILSymbolVisitorOptions opts;
1652-
opts.VisitMembers = false;
1653-
auto visitorCtx =
1654-
SILSymbolVisitorContext(getModule().getSwiftModule(), opts);
1655-
SymbolVisitor(SGM).visitDecl(decl, visitorCtx);
16561630
break;
16571631
}
16581632
}

lib/Sema/CodeSynthesis.cpp

Lines changed: 0 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1798,32 +1798,3 @@ ConstructorDecl *SynthesizeTypeWrappedTypeMemberwiseInitializer::evaluate(
17981798
ctor->setBodySynthesizer(synthesizeTypeWrappedTypeMemberwiseInitializerBody);
17991799
return ctor;
18001800
}
1801-
1802-
FuncDecl *ValueDecl::getHasSymbolQueryDecl() const {
1803-
return evaluateOrDefault(getASTContext().evaluator,
1804-
SynthesizeHasSymbolQueryRequest{this}, nullptr);
1805-
}
1806-
1807-
FuncDecl *
1808-
SynthesizeHasSymbolQueryRequest::evaluate(Evaluator &evaluator,
1809-
const ValueDecl *decl) const {
1810-
auto &ctx = decl->getASTContext();
1811-
auto dc = decl->getModuleContext();
1812-
1813-
Mangle::ASTMangler mangler;
1814-
auto mangledName = ctx.AllocateCopy(mangler.mangleHasSymbolQuery(decl));
1815-
1816-
ParameterList *params = ParameterList::createEmpty(ctx);
1817-
1818-
DeclName funcName =
1819-
DeclName(ctx, DeclBaseName(ctx.getIdentifier(mangledName)),
1820-
/*argumentNames=*/ArrayRef<Identifier>());
1821-
1822-
auto i1 = BuiltinIntegerType::get(1, ctx);
1823-
FuncDecl *func = FuncDecl::createImplicit(
1824-
ctx, swift::StaticSpellingKind::None, funcName, SourceLoc(),
1825-
/*async=*/false, /*throws=*/false, nullptr, params, i1, dc);
1826-
1827-
func->getAttrs().add(new (ctx) SILGenNameAttr(mangledName, IsImplicit));
1828-
return func;
1829-
}

test/AutoDiff/SILGen/has_symbol.swift

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,11 @@ public func bar(_ x: Float) -> (value: Float, pullback: (Float) -> Float) {
2323

2424
// CHECK: sil hidden [ossa] @$s4test0A15GlobalFunctionsyyF : $@convention(thin) () -> ()
2525
func testGlobalFunctions() {
26-
// CHECK: [[QUERY:%[0-9]+]] = function_ref @$s7Library3fooyS2fFTwS : $@convention(thin) () -> Builtin.Int1
27-
// CHECK: [[RES:%[0-9]+]] = apply [[QUERY]]() : $@convention(thin) () -> Builtin.Int1
26+
// CHECK: [[RES:%[0-9]+]] = has_symbol #foo
2827
// CHECK: cond_br [[RES]], bb{{[0-9]+}}, bb{{[0-9]+}}
2928
if #_hasSymbol(foo(_:)) {}
3029

31-
// CHECK: [[QUERY:%[0-9]+]] = function_ref @$s7Library3barySf5value_S2fc8pullbacktSfFTwS : $@convention(thin) () -> Builtin.Int1
32-
// CHECK: [[RES:%[0-9]+]] = apply [[QUERY]]() : $@convention(thin) () -> Builtin.Int1
30+
// CHECK: [[RES:%[0-9]+]] = has_symbol #bar
3331
// CHECK: cond_br [[RES]], bb{{[0-9]+}}, bb{{[0-9]+}}
3432
if #_hasSymbol(bar(_:)) {}
3533
}
@@ -41,6 +39,5 @@ func testGlobalFunctions() {
4139
// FIXME: missing reverse-mode differentiability witness for foo(_:)
4240

4341
// --- bar(_:) ---
44-
// CHECK: sil hidden_external @$s7Library3barySf5value_S2fc8pullbacktSfFTwS : $@convention(thin) () -> Builtin.Int1
4542
// CHECK: sil @$s7Library3barySf5value_S2fc8pullbacktSfF : $@convention(thin) (Float) -> (Float, @owned @callee_guaranteed (Float) -> Float)
4643
// FIXME: missing reverse-mode differentiability witness for foo(_:)

0 commit comments

Comments
 (0)