Skip to content

Commit 0a24042

Browse files
committed
SIL: Introduce the has_symbol SIL instruction.
1 parent 1a9a15f commit 0a24042

File tree

17 files changed

+87
-2
lines changed

17 files changed

+87
-2
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/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: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4086,6 +4086,31 @@ 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() { return Decl; }
4105+
4106+
ArrayRef<Operand> getAllOperands() const { return {}; }
4107+
MutableArrayRef<Operand> getAllOperands() { return {}; }
4108+
4109+
static bool classof(SILNodePointer node) {
4110+
return node->getKind() == SILNodeKind::HasSymbolInst;
4111+
}
4112+
};
4113+
40894114
//===----------------------------------------------------------------------===//
40904115
// Memory instructions.
40914116
//===----------------------------------------------------------------------===//

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)

lib/IRGen/IRGenSIL.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1391,6 +1391,8 @@ class IRGenSILFunction :
13911391
llvm_unreachable("test-only instruction in Lowered SIL?!");
13921392
}
13931393

1394+
void visitHasSymbolInst(HasSymbolInst *i);
1395+
13941396
#define LOADABLE_REF_STORAGE_HELPER(Name) \
13951397
void visitRefTo##Name##Inst(RefTo##Name##Inst *i); \
13961398
void visit##Name##ToRefInst(Name##ToRefInst *i); \
@@ -2611,6 +2613,10 @@ void IRGenSILFunction::visitDifferentiabilityWitnessFunctionInst(
26112613
i, FunctionPointer::createUnsigned(fnType, diffWitness, signature, true));
26122614
}
26132615

2616+
void IRGenSILFunction::visitHasSymbolInst(HasSymbolInst *i) {
2617+
llvm_unreachable("unimplemented"); // FIXME: implement lowering
2618+
}
2619+
26142620
FunctionPointer::Kind irgen::classifyFunctionPointerKind(SILFunction *fn) {
26152621
using SpecialKind = FunctionPointer::SpecialKind;
26162622

lib/SIL/IR/OperandOwnership.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,7 @@ SHOULD_NEVER_VISIT_INST(PreviousDynamicFunctionRef)
109109
SHOULD_NEVER_VISIT_INST(GlobalAddr)
110110
SHOULD_NEVER_VISIT_INST(GlobalValue)
111111
SHOULD_NEVER_VISIT_INST(BaseAddrForOffset)
112+
SHOULD_NEVER_VISIT_INST(HasSymbol)
112113
SHOULD_NEVER_VISIT_INST(IntegerLiteral)
113114
SHOULD_NEVER_VISIT_INST(Metatype)
114115
SHOULD_NEVER_VISIT_INST(ObjCProtocol)

lib/SIL/IR/SILPrinter.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2860,6 +2860,11 @@ class SILPrinter : public SILInstructionVisitor<SILPrinter> {
28602860
*this << dwfi->getType();
28612861
}
28622862
}
2863+
2864+
void visitHasSymbolInst(HasSymbolInst *hsi) {
2865+
*this << "#";
2866+
printValueDecl(hsi->getDecl(), PrintState.OS);
2867+
}
28632868
};
28642869

28652870
} // namespace swift

lib/SIL/IR/ValueOwnership.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,7 @@ CONSTANT_OWNERSHIP_INST(None, DynamicFunctionRef)
107107
CONSTANT_OWNERSHIP_INST(None, PreviousDynamicFunctionRef)
108108
CONSTANT_OWNERSHIP_INST(None, GlobalAddr)
109109
CONSTANT_OWNERSHIP_INST(None, BaseAddrForOffset)
110+
CONSTANT_OWNERSHIP_INST(None, HasSymbol)
110111
CONSTANT_OWNERSHIP_INST(None, IndexAddr)
111112
CONSTANT_OWNERSHIP_INST(None, IndexRawPointer)
112113
CONSTANT_OWNERSHIP_INST(None, InitEnumDataAddr)

lib/SIL/Parser/ParseSIL.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6118,6 +6118,10 @@ bool SILParser::parseSpecificSILInstruction(SILBuilder &B,
61186118
}
61196119
break;
61206120
}
6121+
case SILInstructionKind::HasSymbolInst: {
6122+
llvm_unreachable("unimplemented"); // FIXME: implement SIL parsing
6123+
break;
6124+
}
61216125

61226126
}
61236127

0 commit comments

Comments
 (0)