Skip to content

Commit 7208cf5

Browse files
authored
Merge pull request #65269 from tshortli/unavailable-decl-optimization-stub-5.9
[5.9] Introduce `-unavailable-decl-optimization=stub`
2 parents f8cc3d4 + 8c45bb2 commit 7208cf5

39 files changed

+477
-9
lines changed

docs/ABI/Mangling.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -301,6 +301,8 @@ are always non-polymorphic ``<impl-function-type>`` types.
301301
VALUE-WITNESS-KIND ::= 'ug' // getEnumTag
302302
VALUE-WITNESS-KIND ::= 'up' // destructiveProjectEnumData
303303
VALUE-WITNESS-KIND ::= 'ui' // destructiveInjectEnumTag
304+
VALUE-WITNESS-KIND ::= 'et' // getEnumTagSinglePayload
305+
VALUE-WITNESS-KIND ::= 'st' // storeEnumTagSinglePayload
304306

305307
``<VALUE-WITNESS-KIND>`` differentiates the kinds of value
306308
witness functions for a type.

include/swift/AST/KnownDecls.def

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ FUNC_DECL(DiagnoseUnexpectedError, "_unexpectedError")
6868
FUNC_DECL(DiagnoseUnexpectedNilOptional, "_diagnoseUnexpectedNilOptional")
6969
FUNC_DECL(DiagnoseUnexpectedEnumCase, "_diagnoseUnexpectedEnumCase")
7070
FUNC_DECL(DiagnoseUnexpectedEnumCaseValue, "_diagnoseUnexpectedEnumCaseValue")
71+
FUNC_DECL(DiagnoseUnavailableCodeReached, "_diagnoseUnavailableCodeReached")
7172

7273
FUNC_DECL(GetErrorEmbeddedNSError, "_getErrorEmbeddedNSError")
7374

include/swift/Basic/LangOptions.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,12 @@ namespace swift {
112112
/// resulting binary by default in this mode.
113113
None,
114114

115+
/// Stub out code associated with unavailable declarations.
116+
///
117+
/// For example, the bodies of unavailable functions should be compiled as
118+
/// if they just contained a call to fatalError().
119+
Stub,
120+
115121
/// Avoid generating any code for unavailable declarations.
116122
///
117123
/// NOTE: This optimization can be ABI breaking for a library evolution

include/swift/SIL/ApplySite.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -241,6 +241,12 @@ class ApplySite {
241241
FOREACH_IMPL_RETURN(isCalleeKnownProgramTerminationPoint());
242242
}
243243

244+
/// Returns true if the callee function is annotated with
245+
/// @_semantics("unavailable_code_reached")
246+
bool isCalleeUnavailableCodeReached() const {
247+
FOREACH_IMPL_RETURN(isCalleeUnavailableCodeReached());
248+
}
249+
244250
/// Check if this is a call of a never-returning function.
245251
bool isCalleeNoReturn() const { FOREACH_IMPL_RETURN(isCalleeNoReturn()); }
246252

include/swift/SIL/SILInstruction.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2777,6 +2777,14 @@ class ApplyInstBase<Impl, Base, false> : public Base {
27772777
return calleeFn->hasSemanticsAttr(SEMANTICS_PROGRAMTERMINATION_POINT);
27782778
}
27792779

2780+
/// Returns true if the callee function is annotated with
2781+
/// @_semantics("unavailable_code_reached")
2782+
bool isCalleeUnavailableCodeReached() const {
2783+
auto calleeFn = getCalleeFunction();
2784+
if (!calleeFn) return false;
2785+
return calleeFn->hasSemanticsAttr(SEMANTICS_UNAVAILABLE_CODE_REACHED);
2786+
}
2787+
27802788
/// True if this application has generic substitutions.
27812789
bool hasSubstitutions() const {
27822790
return Substitutions.hasAnySubstitutableParams();

include/swift/SIL/SILModule.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1083,6 +1083,10 @@ LLVM_LIBRARY_VISIBILITY bool usesObjCAllocator(ClassDecl *theClass);
10831083
/// A declaration may not require lowering if, for example, it is annotated as
10841084
/// unavailable and optimization settings allow it to be omitted.
10851085
LLVM_LIBRARY_VISIBILITY bool shouldSkipLowering(Decl *D);
1086+
1087+
/// Returns true if SIL/IR lowering for the given declaration should produce
1088+
/// a stub that traps at runtime because the code ought to be unreachable.
1089+
LLVM_LIBRARY_VISIBILITY bool shouldLowerToUnavailableCodeStub(Decl *D);
10861090
} // namespace Lowering
10871091

10881092
/// Apply the given function to each ABI member of \c D skipping the members

include/swift/Strings.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,9 @@ constexpr static const StringLiteral SEMANTICS_PROGRAMTERMINATION_POINT =
6060
constexpr static const StringLiteral SEMANTICS_DEFAULT_ACTOR =
6161
"defaultActor";
6262

63+
constexpr static const StringLiteral SEMANTICS_UNAVAILABLE_CODE_REACHED =
64+
"unavailable_code_reached";
65+
6366
constexpr static const StringLiteral DEFAULT_ACTOR_STORAGE_FIELD_NAME =
6467
"$defaultActor";
6568

lib/ClangImporter/ClangImporter.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6566,7 +6566,6 @@ CxxRecordSemanticsKind
65666566
CxxRecordSemantics::evaluate(Evaluator &evaluator,
65676567
CxxRecordSemanticsDescriptor desc) const {
65686568
const auto *decl = desc.decl;
6569-
auto &clangSema = desc.ctx.getClangModuleLoader()->getClangSema();
65706569

65716570
if (hasImportAsRefAttr(decl)) {
65726571
return CxxRecordSemanticsKind::Reference;

lib/Frontend/CompilerInvocation.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -568,6 +568,7 @@ static bool ParseLangArgs(LangOptions &Opts, ArgList &Args,
568568
auto value =
569569
llvm::StringSwitch<Optional<UnavailableDeclOptimization>>(A->getValue())
570570
.Case("none", UnavailableDeclOptimization::None)
571+
.Case("stub", UnavailableDeclOptimization::Stub)
571572
.Case("complete", UnavailableDeclOptimization::Complete)
572573
.Default(None);
573574

lib/SIL/IR/SILModule.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -965,3 +965,13 @@ bool Lowering::shouldSkipLowering(Decl *D) {
965965
// -unavailable-decl-optimization=complete is specified.
966966
return D->getSemanticUnavailableAttr() != None;
967967
}
968+
969+
bool Lowering::shouldLowerToUnavailableCodeStub(Decl *D) {
970+
if (D->getASTContext().LangOpts.UnavailableDeclOptimizationMode !=
971+
UnavailableDeclOptimization::Stub)
972+
return false;
973+
974+
// Unavailable declarations should trap at runtime if
975+
// -unavailable-decl-optimization=stub is specified.
976+
return D->getSemanticUnavailableAttr() != None;
977+
}

0 commit comments

Comments
 (0)