Skip to content

Commit 0366c42

Browse files
authored
Merge pull request #65557 from tshortli/unavailable-decl-optimization-stub
Introduce `stub` mode for `-unavailable-decl-optimization`
2 parents b12537f + 12a122a commit 0366c42

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
@@ -1087,6 +1087,10 @@ LLVM_LIBRARY_VISIBILITY bool usesObjCAllocator(ClassDecl *theClass);
10871087
/// A declaration may not require lowering if, for example, it is annotated as
10881088
/// unavailable and optimization settings allow it to be omitted.
10891089
LLVM_LIBRARY_VISIBILITY bool shouldSkipLowering(Decl *D);
1090+
1091+
/// Returns true if SIL/IR lowering for the given declaration should produce
1092+
/// a stub that traps at runtime because the code ought to be unreachable.
1093+
LLVM_LIBRARY_VISIBILITY bool shouldLowerToUnavailableCodeStub(Decl *D);
10901094
} // namespace Lowering
10911095

10921096
/// 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
@@ -6560,7 +6560,6 @@ CxxRecordSemanticsKind
65606560
CxxRecordSemantics::evaluate(Evaluator &evaluator,
65616561
CxxRecordSemanticsDescriptor desc) const {
65626562
const auto *decl = desc.decl;
6563-
auto &clangSema = desc.ctx.getClangModuleLoader()->getClangSema();
65646563

65656564
if (hasImportAsRefAttr(decl)) {
65666565
return CxxRecordSemanticsKind::Reference;

lib/Frontend/CompilerInvocation.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -614,6 +614,7 @@ static bool ParseLangArgs(LangOptions &Opts, ArgList &Args,
614614
auto value =
615615
llvm::StringSwitch<Optional<UnavailableDeclOptimization>>(A->getValue())
616616
.Case("none", UnavailableDeclOptimization::None)
617+
.Case("stub", UnavailableDeclOptimization::Stub)
617618
.Case("complete", UnavailableDeclOptimization::Complete)
618619
.Default(None);
619620

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)