Skip to content

Commit c5a7eff

Browse files
authored
Merge pull request #62147 from tshortli/has-symbol-sil-instruction
2 parents 889c543 + 3da18b6 commit c5a7eff

39 files changed

+388
-304
lines changed

docs/SIL.rst

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7882,3 +7882,16 @@ IRGen will replace the application by the constant representing Debug mode (0).
78827882
This happens we can build the standard library .dylib. The generate sil will
78837883
retain the function call but the generated .dylib will contain code with
78847884
assertions enabled.
7885+
7886+
Weak linking support
7887+
~~~~~~~~~~~~~~~~~~~~~~~
7888+
7889+
has_symbol
7890+
```````````````````````````
7891+
::
7892+
7893+
sil-instruction ::= 'has_symbol' sil-decl-ref
7894+
7895+
Returns true if each of the underlying symbol addresses associated with the
7896+
given declaration are non-null. This can be used to determine whether a
7897+
weakly-imported declaration is available at runtime.

include/swift/AST/Decl.h

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

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

include/swift/AST/DiagnosticsSema.def

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6029,9 +6029,6 @@ ERROR(availability_macro_in_inlinable, none,
60296029
"availability macro cannot be used in " FRAGILE_FUNC_KIND "0",
60306030
(unsigned))
60316031

6032-
ERROR(has_symbol_condition_in_inlinable, none,
6033-
"'#_hasSymbol' cannot be used in " FRAGILE_FUNC_KIND "0", (unsigned))
6034-
60356032
#undef FRAGILE_FUNC_KIND
60366033

60376034
NOTE(resilience_decl_declared_here_public,

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/SILBuilder.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2725,6 +2725,14 @@ class SILBuilder {
27252725
FunctionType));
27262726
}
27272727

2728+
//===--------------------------------------------------------------------===//
2729+
// Weak linking support
2730+
//===--------------------------------------------------------------------===//
2731+
HasSymbolInst *createHasSymbol(SILLocation Loc, ValueDecl *Decl) {
2732+
return insert(new (getModule()) HasSymbolInst(
2733+
getModule(), getSILDebugLocation(Loc), Decl));
2734+
}
2735+
27282736
//===--------------------------------------------------------------------===//
27292737
// Private Helper Methods
27302738
//===--------------------------------------------------------------------===//

include/swift/SIL/SILCloner.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3103,6 +3103,14 @@ ::visitExtractExecutorInst(ExtractExecutorInst *Inst) {
31033103
getOpValue(Inst->getExpectedExecutor())));
31043104
}
31053105

3106+
template <typename ImplClass>
3107+
void SILCloner<ImplClass>::visitHasSymbolInst(HasSymbolInst *Inst) {
3108+
getBuilder().setCurrentDebugScope(getOpScope(Inst->getDebugScope()));
3109+
recordClonedInstruction(
3110+
Inst, getBuilder().createHasSymbol(getOpLocation(Inst->getLoc()),
3111+
Inst->getDecl()));
3112+
}
3113+
31063114
} // end namespace swift
31073115

31083116
#endif

include/swift/SIL/SILInstruction.h

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4086,6 +4086,32 @@ class StringLiteralInst final
40864086
MutableArrayRef<Operand> getAllOperands() { return {}; }
40874087
};
40884088

4089+
/// HasSymbolInst - Determines whether a weakly-imported declaration is
4090+
/// available at runtime. Produces true if each of the underlying symbol
4091+
/// addresses associated with a given declaration are non-null, false otherwise.
4092+
class HasSymbolInst final : public LiteralInst {
4093+
private:
4094+
friend SILBuilder;
4095+
4096+
ValueDecl *Decl;
4097+
4098+
public:
4099+
HasSymbolInst(SILModule &M, SILDebugLocation Loc, ValueDecl *Decl)
4100+
: LiteralInst(SILInstructionKind::HasSymbolInst, Loc,
4101+
SILType::getBuiltinIntegerType(1, Decl->getASTContext())),
4102+
Decl{Decl} {}
4103+
4104+
ValueDecl *getDecl() const { return Decl; }
4105+
void getReferencedFunctions(llvm::SmallVector<SILFunction *, 4> &fns) const;
4106+
4107+
ArrayRef<Operand> getAllOperands() const { return {}; }
4108+
MutableArrayRef<Operand> getAllOperands() { return {}; }
4109+
4110+
static bool classof(SILNodePointer node) {
4111+
return node->getKind() == SILNodeKind::HasSymbolInst;
4112+
}
4113+
};
4114+
40894115
//===----------------------------------------------------------------------===//
40904116
// Memory instructions.
40914117
//===----------------------------------------------------------------------===//

include/swift/SIL/SILModule.h

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -315,9 +315,6 @@ class SILModule {
315315
/// This is the set of undef values we've created, for uniquing purposes.
316316
llvm::DenseMap<SILType, SILUndef *> UndefValues;
317317

318-
/// The list of decls that require query functions for #_hasSymbol conditions.
319-
llvm::SetVector<ValueDecl *> hasSymbolDecls;
320-
321318
llvm::DenseMap<std::pair<Decl *, VarDecl *>, unsigned> fieldIndices;
322319
llvm::DenseMap<EnumElementDecl *, unsigned> enumCaseIndices;
323320

@@ -703,12 +700,6 @@ class SILModule {
703700
return externallyVisible.count(decl) != 0;
704701
}
705702

706-
void addHasSymbolDecl(ValueDecl *decl) { hasSymbolDecls.insert(decl); }
707-
708-
ArrayRef<ValueDecl *> getHasSymbolDecls() {
709-
return hasSymbolDecls.getArrayRef();
710-
}
711-
712703
using sil_global_iterator = GlobalListType::iterator;
713704
using sil_global_const_iterator = GlobalListType::const_iterator;
714705
GlobalListType &getSILGlobalList() { return silGlobals; }

include/swift/SIL/SILNodes.def

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -347,7 +347,9 @@ ABSTRACT_VALUE_AND_INST(SingleValueInstruction, ValueBase, SILInstruction)
347347
LiteralInst, None, DoesNotRelease)
348348
SINGLE_VALUE_INST(StringLiteralInst, string_literal,
349349
LiteralInst, None, DoesNotRelease)
350-
SINGLE_VALUE_INST_RANGE(LiteralInst, FunctionRefInst, StringLiteralInst)
350+
SINGLE_VALUE_INST(HasSymbolInst, has_symbol,
351+
LiteralInst, MayRead, DoesNotRelease)
352+
SINGLE_VALUE_INST_RANGE(LiteralInst, FunctionRefInst, HasSymbolInst)
351353

352354
// Dynamic Dispatch
353355
ABSTRACT_SINGLE_VALUE_INST(MethodInst, SingleValueInstruction)

0 commit comments

Comments
 (0)