Skip to content

Commit c226bf3

Browse files
committed
[AST][IRGen] Add -min-runtime-version option and IRGen support code.
Add a `-min-runtime-version` option that can be used to avoid problems when building on Linux and Windows where because the runtime isn't part of the OS, availability doesn't solve the problem of trying to build the compiler against an older runtime. Also add functions to IRGen to make it easy to test feature availability using both the runtime version and the existing Darwin availability support. rdar://121522431
1 parent c2c4f87 commit c226bf3

File tree

9 files changed

+69
-10
lines changed

9 files changed

+69
-10
lines changed

include/swift/AST/Availability.h

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -238,11 +238,15 @@ class AvailabilityContext {
238238

239239
/// Creates a context that imposes the constraints of the ASTContext's
240240
/// deployment target.
241-
static AvailabilityContext forDeploymentTarget(ASTContext &Ctx);
241+
static AvailabilityContext forDeploymentTarget(const ASTContext &Ctx);
242242

243243
/// Creates a context that imposes the constraints of the ASTContext's
244244
/// inlining target (i.e. minimum inlining version).
245-
static AvailabilityContext forInliningTarget(ASTContext &Ctx);
245+
static AvailabilityContext forInliningTarget(const ASTContext &Ctx);
246+
247+
/// Creates a context that imposes the constraints of the ASTContext's
248+
/// minimum runtime version.
249+
static AvailabilityContext forRuntimeTarget(const ASTContext &Ctx);
246250

247251
/// Creates a context that imposes no constraints.
248252
///

include/swift/Basic/LangOptions.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,9 @@ namespace swift {
174174
/// User-overridable language version to compile for.
175175
version::Version EffectiveLanguageVersion = version::Version::getCurrentLanguageVersion();
176176

177+
/// Swift runtime version to compile for.
178+
version::Version RuntimeVersion = version::Version::getCurrentLanguageVersion();
179+
177180
/// PackageDescription version to compile for.
178181
version::Version PackageDescriptionVersion;
179182

include/swift/Basic/Version.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,9 @@ class Version {
6464
/// Create a literal version from a list of components.
6565
Version(std::initializer_list<unsigned> Values) : Components(Values) {}
6666

67+
/// Create a version from an llvm::VersionTuple.
68+
Version(const llvm::VersionTuple &version);
69+
6770
/// Return a string to be used as an internal preprocessor define.
6871
///
6972
/// The components of the version are multiplied element-wise by

include/swift/Option/Options.td

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1349,6 +1349,11 @@ def min_inlining_target_version : Separate<["-"], "target-min-inlining-version">
13491349
HelpText<"Require inlinable code with no '@available' attribute to back-deploy "
13501350
"to this version of the '-target' OS">;
13511351

1352+
def min_runtime_version : Separate<["-"], "min-runtime-version">,
1353+
Flags<[FrontendOption, NoInteractiveOption, HelpHidden]>,
1354+
HelpText<"Specify the minimum runtime version to build force on non-Darwin "
1355+
"systems">;
1356+
13521357
def profile_generate : Flag<["-"], "profile-generate">,
13531358
Flags<[FrontendOption, NoInteractiveOption]>,
13541359
HelpText<"Generate instrumented code to collect execution counts">;

lib/AST/Availability.cpp

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,16 +28,21 @@
2828

2929
using namespace swift;
3030

31-
AvailabilityContext AvailabilityContext::forDeploymentTarget(ASTContext &Ctx) {
31+
AvailabilityContext AvailabilityContext::forDeploymentTarget(const ASTContext &Ctx) {
3232
return AvailabilityContext(
3333
VersionRange::allGTE(Ctx.LangOpts.getMinPlatformVersion()));
3434
}
3535

36-
AvailabilityContext AvailabilityContext::forInliningTarget(ASTContext &Ctx) {
36+
AvailabilityContext AvailabilityContext::forInliningTarget(const ASTContext &Ctx) {
3737
return AvailabilityContext(
3838
VersionRange::allGTE(Ctx.LangOpts.MinimumInliningTargetVersion));
3939
}
4040

41+
AvailabilityContext AvailabilityContext::forRuntimeTarget(const ASTContext &Ctx) {
42+
return AvailabilityContext(
43+
VersionRange::allGTE(Ctx.LangOpts.RuntimeVersion));
44+
}
45+
4146
namespace {
4247

4348
/// The inferred availability required to access a group of declarations

lib/Basic/Version.cpp

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,23 @@ Version::preprocessorDefinition(StringRef macroName,
100100
return define;
101101
}
102102

103+
Version::Version(const llvm::VersionTuple &version) {
104+
if (version.empty())
105+
return;
106+
107+
Components.emplace_back(version.getMajor());
108+
109+
if (auto minor = version.getMinor()) {
110+
Components.emplace_back(*minor);
111+
if (auto subminor = version.getSubminor()) {
112+
Components.emplace_back(*subminor);
113+
if (auto build = version.getBuild()) {
114+
Components.emplace_back(*build);
115+
}
116+
}
117+
}
118+
}
119+
103120
Version::operator llvm::VersionTuple() const
104121
{
105122
switch (Components.size()) {

lib/Frontend/CompilerInvocation.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1213,6 +1213,9 @@ static bool ParseLangArgs(LangOptions &Opts, ArgList &Args,
12131213
// FIXME: Should we diagnose if it's below the default?
12141214
Opts.MinimumInliningTargetVersion = *vers;
12151215

1216+
if (auto vers = parseVersionArg(OPT_min_runtime_version))
1217+
Opts.RuntimeVersion = version::Version(*vers);
1218+
12161219
if (auto vers = parseVersionArg(OPT_target_sdk_version))
12171220
Opts.SDKVersion = *vers;
12181221

lib/IRGen/IRGenModule.cpp

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1998,6 +1998,20 @@ void IRGenModule::error(SourceLoc loc, const Twine &message) {
19981998

19991999
bool IRGenModule::useDllStorage() { return ::useDllStorage(Triple); }
20002000

2001+
#define FEATURE(N, V) \
2002+
bool IRGenModule::is##N##FeatureAvailable(const ASTContext &context) { \
2003+
auto deploymentAvailability \
2004+
= AvailabilityContext::forDeploymentTarget(context); \
2005+
auto runtimeAvailability \
2006+
= AvailabilityContext::forRuntimeTarget(context); \
2007+
return deploymentAvailability.isContainedIn( \
2008+
context.get##N##Availability()) \
2009+
&& runtimeAvailability.isContainedIn( \
2010+
context.get##N##RuntimeAvailability()); \
2011+
}
2012+
2013+
#include "swift/AST/FeatureAvailability.def"
2014+
20012015
bool IRGenModule::shouldPrespecializeGenericMetadata() {
20022016
auto canPrespecializeTarget =
20032017
(Triple.isOSDarwin() || Triple.isOSWindows() ||
@@ -2017,11 +2031,9 @@ bool IRGenModule::shouldPrespecializeGenericMetadata() {
20172031
bool IRGenModule::canUseObjCSymbolicReferences() {
20182032
if (!IRGen.Opts.EnableObjectiveCProtocolSymbolicReferences)
20192033
return false;
2020-
auto &context = getSwiftModule()->getASTContext();
2021-
auto deploymentAvailability =
2022-
AvailabilityContext::forDeploymentTarget(context);
2023-
return deploymentAvailability.isContainedIn(
2024-
context.getObjCSymbolicReferencesAvailability());
2034+
return isObjCSymbolicReferencesFeatureAvailable(
2035+
getSwiftModule()->getASTContext()
2036+
);
20252037
}
20262038

20272039
bool IRGenModule::canMakeStaticObjectReadOnly(SILType objectType) {
@@ -2043,7 +2055,7 @@ bool IRGenModule::canMakeStaticObjectReadOnly(SILType objectType) {
20432055
if (clDecl->getNameStr() != "_ContiguousArrayStorage")
20442056
return false;
20452057

2046-
if (!getAvailabilityContext().isContainedIn(Context.getStaticReadOnlyArraysAvailability()))
2058+
if (!isStaticReadOnlyArraysFeatureAvailable())
20472059
return false;
20482060

20492061
if (!getStaticArrayStorageDecl())

lib/IRGen/IRGenModule.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -934,6 +934,13 @@ class IRGenModule {
934934

935935
ClassDecl *getStaticArrayStorageDecl();
936936

937+
#define FEATURE(N, V) \
938+
bool is##N##FeatureAvailable(const ASTContext &context); \
939+
inline bool is##N##FeatureAvailable() { \
940+
return is##N##FeatureAvailable(Context); \
941+
}
942+
#include "swift/AST/FeatureAvailability.def"
943+
937944
bool canUseObjCSymbolicReferences();
938945

939946
Size getAtomicBoolSize() const { return AtomicBoolSize; }

0 commit comments

Comments
 (0)