Skip to content

Commit 0a0215b

Browse files
Merge pull request #1724 from swiftwasm/katei-merge-5.3
Upstream merge 5.3
2 parents 335c349 + 58673c1 commit 0a0215b

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

55 files changed

+725
-113
lines changed

cmake/modules/DarwinSDKs.cmake

-17
Original file line numberDiff line numberDiff line change
@@ -16,23 +16,6 @@ set(SUPPORTED_WATCHOS_ARCHS "armv7k")
1616
set(SUPPORTED_WATCHOS_SIMULATOR_ARCHS "i386;arm64")
1717
set(SUPPORTED_OSX_ARCHS "x86_64;arm64;arm64e")
1818

19-
# Get the SDK version from SDKSettings.
20-
execute_process(
21-
COMMAND "defaults" "read" "${CMAKE_OSX_SYSROOT}/SDKSettings.plist" "Version"
22-
OUTPUT_VARIABLE SWIFT_OSX_SDK_VERSION
23-
OUTPUT_STRIP_TRAILING_WHITESPACE)
24-
25-
# Remove the last component, if any. e.g. 10.15.26 -> 10.15
26-
string(REGEX REPLACE "\([0-9]*[.][0-9]*\)[.][0-9]*" "\\1"
27-
SWIFT_OSX_SDK_VERSION "${SWIFT_OSX_SDK_VERSION}")
28-
29-
if (${SWIFT_OSX_SDK_VERSION} STREQUAL "10.14" OR
30-
${SWIFT_OSX_SDK_VERSION} STREQUAL "10.15")
31-
set(SUPPORTED_OSX_ARCHS "x86_64")
32-
else()
33-
set(SUPPORTED_OSX_ARCHS "x86_64;arm64e")
34-
endif()
35-
3619
is_sdk_requested(OSX swift_build_osx)
3720
if(swift_build_osx)
3821
configure_sdk_darwin(

include/swift/AST/DiagnosticEngine.h

+13-4
Original file line numberDiff line numberDiff line change
@@ -1025,10 +1025,19 @@ namespace swift {
10251025
}
10261026
}
10271027

1028-
bool hasDiagnostics() const {
1029-
return std::distance(Engine.TentativeDiagnostics.begin() +
1030-
PrevDiagnostics,
1031-
Engine.TentativeDiagnostics.end()) > 0;
1028+
bool hasErrors() const {
1029+
ArrayRef<Diagnostic> diagnostics(Engine.TentativeDiagnostics.begin() +
1030+
PrevDiagnostics,
1031+
Engine.TentativeDiagnostics.end());
1032+
1033+
for (auto &diagnostic : diagnostics) {
1034+
auto behavior = Engine.state.determineBehavior(diagnostic.getID());
1035+
if (behavior == DiagnosticState::Behavior::Fatal ||
1036+
behavior == DiagnosticState::Behavior::Error)
1037+
return true;
1038+
}
1039+
1040+
return false;
10321041
}
10331042

10341043
/// Abort and close this transaction and erase all diagnostics

include/swift/AST/DiagnosticsFrontend.def

+3
Original file line numberDiff line numberDiff line change
@@ -301,6 +301,9 @@ ERROR(error_optimization_remark_pattern, none, "%0 in '%1'",
301301
ERROR(error_invalid_debug_prefix_map, none,
302302
"invalid argument '%0' to -debug-prefix-map; it must be of the form "
303303
"'original=remapped'", (StringRef))
304+
ERROR(error_invalid_coverage_prefix_map, none,
305+
"invalid argument '%0' to -coverage-prefix-map; it must be of the form "
306+
"'original=remapped'", (StringRef))
304307

305308

306309
ERROR(error_unable_to_write_swift_ranges_file, none,

include/swift/AST/IRGenOptions.h

+3
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,9 @@ class IRGenOptions {
169169
/// Path prefixes that should be rewritten in debug info.
170170
PathRemapper DebugPrefixMap;
171171

172+
/// Path prefixes that should be rewritten in coverage info.
173+
PathRemapper CoveragePrefixMap;
174+
172175
/// What level of debug info to generate.
173176
IRGenDebugInfoLevel DebugInfoLevel : 2;
174177

include/swift/Frontend/Frontend.h

+7-3
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,8 @@ class CompilerInvocation {
128128
bool parseArgs(ArrayRef<const char *> Args, DiagnosticEngine &Diags,
129129
SmallVectorImpl<std::unique_ptr<llvm::MemoryBuffer>>
130130
*ConfigurationFileBuffers = nullptr,
131-
StringRef workingDirectory = {});
131+
StringRef workingDirectory = {},
132+
StringRef mainExecutablePath = {});
132133

133134
/// Sets specific options based on the given serialized Swift binary data.
134135
///
@@ -213,8 +214,11 @@ class CompilerInvocation {
213214
/// Computes the runtime resource path relative to the given Swift
214215
/// executable.
215216
static void computeRuntimeResourcePathFromExecutablePath(
216-
StringRef mainExecutablePath,
217-
llvm::SmallString<128> &runtimeResourcePath);
217+
StringRef mainExecutablePath, bool shared,
218+
llvm::SmallVectorImpl<char> &runtimeResourcePath);
219+
220+
/// Appends `lib/swift[_static]` to the given path
221+
static void appendSwiftLibDir(llvm::SmallVectorImpl<char> &path, bool shared);
218222

219223
void setSDKPath(const std::string &Path);
220224

include/swift/Frontend/FrontendOptions.h

+5
Original file line numberDiff line numberDiff line change
@@ -301,6 +301,11 @@ class FrontendOptions {
301301
/// Indicates whether the action will immediately run code.
302302
static bool isActionImmediate(ActionType);
303303

304+
/// Determines whether the static or shared resource folder is used.
305+
/// When set to `true`, the default resource folder will be set to
306+
/// '.../lib/swift', otherwise '.../lib/swift_static'.
307+
bool UseSharedResourceFolder = true;
308+
304309
/// \return true if action only parses without doing other compilation steps.
305310
static bool shouldActionOnlyParse(ActionType);
306311

include/swift/Option/FrontendOptions.td

+4
Original file line numberDiff line numberDiff line change
@@ -719,4 +719,8 @@ def target_sdk_version : Separate<["-"], "target-sdk-version">,
719719
def target_variant_sdk_version : Separate<["-"], "target-variant-sdk-version">,
720720
HelpText<"The version of target variant SDK used for compilation">;
721721

722+
723+
def use_static_resource_dir
724+
: Flag<["-"], "use-static-resource-dir">,
725+
HelpText<"Use resources in the static resource directory">;
722726
} // end let Flags = [FrontendOption, NoDriverOption, HelpHidden]

include/swift/Option/Options.td

+3
Original file line numberDiff line numberDiff line change
@@ -708,6 +708,9 @@ def gdwarf_types : Flag<["-"], "gdwarf-types">,
708708
def debug_prefix_map : Separate<["-"], "debug-prefix-map">,
709709
Flags<[FrontendOption]>,
710710
HelpText<"Remap source paths in debug info">;
711+
def coverage_prefix_map : Separate<["-"], "coverage-prefix-map">,
712+
Flags<[FrontendOption]>,
713+
HelpText<"Remap source paths in coverage info">;
711714

712715
def debug_info_format : Joined<["-"], "debug-info-format=">,
713716
Flags<[FrontendOption]>,

lib/AST/Type.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -3059,8 +3059,8 @@ operator()(SubstitutableType *maybeOpaqueType) const {
30593059
}))
30603060
return maybeOpaqueType;
30613061

3062-
// If the type still contains opaque types, recur.
3063-
if (substTy->hasOpaqueArchetype()) {
3062+
// If the type changed, but still contains opaque types, recur.
3063+
if (!substTy->isEqual(maybeOpaqueType) && substTy->hasOpaqueArchetype()) {
30643064
return ::substOpaqueTypesWithUnderlyingTypes(
30653065
substTy, inContext, contextExpansion, isContextWholeModule);
30663066
}

lib/Driver/Driver.cpp

+13
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,12 @@ static void validateDebugInfoArgs(DiagnosticEngine &diags,
179179
for (auto A : args.getAllArgValues(options::OPT_debug_prefix_map))
180180
if (A.find('=') == StringRef::npos)
181181
diags.diagnose(SourceLoc(), diag::error_invalid_debug_prefix_map, A);
182+
183+
// Check for any -coverage-prefix-map options that aren't of the form
184+
// 'original=remapped' (either side can be empty, however).
185+
for (auto A : args.getAllArgValues(options::OPT_coverage_prefix_map))
186+
if (A.find('=') == StringRef::npos)
187+
diags.diagnose(SourceLoc(), diag::error_invalid_coverage_prefix_map, A);
182188
}
183189

184190
static void validateVerifyIncrementalDependencyArgs(DiagnosticEngine &diags,
@@ -2233,6 +2239,13 @@ bool Driver::handleImmediateArgs(const ArgList &Args, const ToolChain &TC) {
22332239
commandLine.push_back(resourceDirArg->getValue());
22342240
}
22352241

2242+
if (Args.hasFlag(options::OPT_static_executable,
2243+
options::OPT_no_static_executable, false) ||
2244+
Args.hasFlag(options::OPT_static_stdlib, options::OPT_no_static_stdlib,
2245+
false)) {
2246+
commandLine.push_back("-use-static-resource-dir");
2247+
}
2248+
22362249
std::string executable = getSwiftProgramPath();
22372250

22382251
// FIXME: This bypasses mechanisms like -v and -###. (SR-12119)

lib/Driver/ToolChains.cpp

+13-10
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
#include "swift/Driver/Compilation.h"
2323
#include "swift/Driver/Driver.h"
2424
#include "swift/Driver/Job.h"
25+
#include "swift/Frontend/Frontend.h"
2526
#include "swift/Option/Options.h"
2627
#include "clang/Basic/Version.h"
2728
#include "clang/Driver/Util.h"
@@ -264,6 +265,7 @@ void ToolChain::addCommonFrontendArgs(const OutputInfo &OI,
264265

265266
// Pass on file paths that should be remapped in debug info.
266267
inputArgs.AddAllArgs(arguments, options::OPT_debug_prefix_map);
268+
inputArgs.AddAllArgs(arguments, options::OPT_coverage_prefix_map);
267269

268270
// Pass through the values passed to -Xfrontend.
269271
inputArgs.AddAllArgValues(arguments, options::OPT_Xfrontend);
@@ -523,6 +525,13 @@ ToolChain::constructInvocation(const CompileJobAction &job,
523525
Arguments.push_back("-track-system-dependencies");
524526
}
525527

528+
if (context.Args.hasFlag(options::OPT_static_executable,
529+
options::OPT_no_static_executable, false) ||
530+
context.Args.hasFlag(options::OPT_static_stdlib,
531+
options::OPT_no_static_stdlib, false)) {
532+
Arguments.push_back("-use-static-resource-dir");
533+
}
534+
526535
context.Args.AddLastArg(
527536
Arguments,
528537
options::
@@ -1256,25 +1265,19 @@ void ToolChain::getClangLibraryPath(const ArgList &Args,
12561265
void ToolChain::getResourceDirPath(SmallVectorImpl<char> &resourceDirPath,
12571266
const llvm::opt::ArgList &args,
12581267
bool shared) const {
1259-
// FIXME: Duplicated from CompilerInvocation, but in theory the runtime
1260-
// library link path and the standard library module import path don't
1261-
// need to be the same.
12621268
if (const Arg *A = args.getLastArg(options::OPT_resource_dir)) {
12631269
StringRef value = A->getValue();
12641270
resourceDirPath.append(value.begin(), value.end());
12651271
} else if (!getTriple().isOSDarwin() && !getTriple().isOSWASI() && args.hasArg(options::OPT_sdk)) {
12661272
// for WASI, sdk option points to wasi-sysroot which doesn't have Swift toolchain
12671273
StringRef value = args.getLastArg(options::OPT_sdk)->getValue();
12681274
resourceDirPath.append(value.begin(), value.end());
1269-
llvm::sys::path::append(resourceDirPath, "usr", "lib",
1270-
shared ? "swift" : "swift_static");
1275+
llvm::sys::path::append(resourceDirPath, "usr");
1276+
CompilerInvocation::appendSwiftLibDir(resourceDirPath, shared);
12711277
} else {
12721278
auto programPath = getDriver().getSwiftProgramPath();
1273-
resourceDirPath.append(programPath.begin(), programPath.end());
1274-
llvm::sys::path::remove_filename(resourceDirPath); // remove /swift
1275-
llvm::sys::path::remove_filename(resourceDirPath); // remove /bin
1276-
llvm::sys::path::append(resourceDirPath, "lib",
1277-
shared ? "swift" : "swift_static");
1279+
CompilerInvocation::computeRuntimeResourcePathFromExecutablePath(
1280+
programPath, shared, resourceDirPath);
12781281
}
12791282

12801283
StringRef libSubDir = getPlatformNameForTriple(getTriple());

lib/Frontend/ArgsToFrontendOptionsConverter.cpp

+1
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,7 @@ bool ArgsToFrontendOptionsConverter::convert(
187187
Opts.EnableSourceImport |= Args.hasArg(OPT_enable_source_import);
188188
Opts.ImportUnderlyingModule |= Args.hasArg(OPT_import_underlying_module);
189189
Opts.EnableIncrementalDependencyVerifier |= Args.hasArg(OPT_verify_incremental_dependencies);
190+
Opts.UseSharedResourceFolder = !Args.hasArg(OPT_use_static_resource_dir);
190191

191192
computeImportObjCHeaderOptions();
192193
computeImplicitImportModuleNames();

lib/Frontend/CompilerInvocation.cpp

+24-7
Original file line numberDiff line numberDiff line change
@@ -39,16 +39,25 @@ swift::CompilerInvocation::CompilerInvocation() {
3939
}
4040

4141
void CompilerInvocation::computeRuntimeResourcePathFromExecutablePath(
42-
StringRef mainExecutablePath, llvm::SmallString<128> &runtimeResourcePath) {
43-
runtimeResourcePath.assign(mainExecutablePath);
42+
StringRef mainExecutablePath, bool shared,
43+
llvm::SmallVectorImpl<char> &runtimeResourcePath) {
44+
runtimeResourcePath.append(mainExecutablePath.begin(),
45+
mainExecutablePath.end());
46+
4447
llvm::sys::path::remove_filename(runtimeResourcePath); // Remove /swift
4548
llvm::sys::path::remove_filename(runtimeResourcePath); // Remove /bin
46-
llvm::sys::path::append(runtimeResourcePath, "lib", "swift");
49+
appendSwiftLibDir(runtimeResourcePath, shared);
50+
}
51+
52+
void CompilerInvocation::appendSwiftLibDir(llvm::SmallVectorImpl<char> &path,
53+
bool shared) {
54+
llvm::sys::path::append(path, "lib", shared ? "swift" : "swift_static");
4755
}
4856

4957
void CompilerInvocation::setMainExecutablePath(StringRef Path) {
5058
llvm::SmallString<128> LibPath;
51-
computeRuntimeResourcePathFromExecutablePath(Path, LibPath);
59+
computeRuntimeResourcePathFromExecutablePath(
60+
Path, FrontendOpts.UseSharedResourceFolder, LibPath);
5261
setRuntimeResourcePath(LibPath.str());
5362

5463
llvm::SmallString<128> DiagnosticDocsPath(Path);
@@ -1258,6 +1267,11 @@ static bool ParseIRGenArgs(IRGenOptions &Opts, ArgList &Args,
12581267
Opts.DebugPrefixMap.addMapping(SplitMap.first, SplitMap.second);
12591268
}
12601269

1270+
for (auto A : Args.getAllArgValues(options::OPT_coverage_prefix_map)) {
1271+
auto SplitMap = StringRef(A).split('=');
1272+
Opts.CoveragePrefixMap.addMapping(SplitMap.first, SplitMap.second);
1273+
}
1274+
12611275
for (const Arg *A : Args.filtered(OPT_Xcc)) {
12621276
StringRef Opt = A->getValue();
12631277
if (Opt.startswith("-D") || Opt.startswith("-U"))
@@ -1584,11 +1598,10 @@ static bool ParseMigratorArgs(MigratorOptions &Opts,
15841598
}
15851599

15861600
bool CompilerInvocation::parseArgs(
1587-
ArrayRef<const char *> Args,
1588-
DiagnosticEngine &Diags,
1601+
ArrayRef<const char *> Args, DiagnosticEngine &Diags,
15891602
SmallVectorImpl<std::unique_ptr<llvm::MemoryBuffer>>
15901603
*ConfigurationFileBuffers,
1591-
StringRef workingDirectory) {
1604+
StringRef workingDirectory, StringRef mainExecutablePath) {
15921605
using namespace options;
15931606

15941607
if (Args.empty())
@@ -1619,6 +1632,10 @@ bool CompilerInvocation::parseArgs(
16191632
return true;
16201633
}
16211634

1635+
if (!mainExecutablePath.empty()) {
1636+
setMainExecutablePath(mainExecutablePath);
1637+
}
1638+
16221639
ParseModuleInterfaceArgs(ModuleInterfaceOpts, ParsedArgs);
16231640
SaveModuleInterfaceArgs(ModuleInterfaceOpts, FrontendOpts, ParsedArgs, Diags);
16241641

lib/FrontendTool/FrontendTool.cpp

+5-4
Original file line numberDiff line numberDiff line change
@@ -2078,17 +2078,18 @@ int swift::performFrontend(ArrayRef<const char *> Args,
20782078
}
20792079

20802080
CompilerInvocation Invocation;
2081-
std::string MainExecutablePath = llvm::sys::fs::getMainExecutable(Argv0,
2082-
MainAddr);
2083-
Invocation.setMainExecutablePath(MainExecutablePath);
20842081

20852082
SmallString<128> workingDirectory;
20862083
llvm::sys::fs::current_path(workingDirectory);
20872084

2085+
std::string MainExecutablePath =
2086+
llvm::sys::fs::getMainExecutable(Argv0, MainAddr);
2087+
20882088
// Parse arguments.
20892089
SmallVector<std::unique_ptr<llvm::MemoryBuffer>, 4> configurationFileBuffers;
20902090
if (Invocation.parseArgs(Args, Instance->getDiags(),
2091-
&configurationFileBuffers, workingDirectory)) {
2091+
&configurationFileBuffers, workingDirectory,
2092+
MainExecutablePath)) {
20922093
return finishDiagProcessing(1, /*verifierEnabled*/ false);
20932094
}
20942095

lib/IRGen/GenClangDecl.cpp

+37-10
Original file line numberDiff line numberDiff line change
@@ -34,18 +34,43 @@ class ClangDeclRefFinder
3434
return true;
3535
}
3636
};
37+
38+
// If any (re)declaration of `decl` contains executable code, returns that
39+
// redeclaration; otherwise, returns nullptr.
40+
// In the case of a function, executable code is contained in the function
41+
// definition. In the case of a variable, executable code can be contained in
42+
// the initializer of the variable.
43+
clang::Decl *getDeclWithExecutableCode(clang::Decl *decl) {
44+
if (auto fd = dyn_cast<clang::FunctionDecl>(decl)) {
45+
const clang::FunctionDecl *definition;
46+
if (fd->hasBody(definition)) {
47+
return const_cast<clang::FunctionDecl *>(definition);
48+
}
49+
} else if (auto vd = dyn_cast<clang::VarDecl>(decl)) {
50+
clang::VarDecl *initializingDecl = vd->getInitializingDeclaration();
51+
if (initializingDecl) {
52+
return initializingDecl;
53+
}
54+
}
55+
56+
return nullptr;
57+
}
58+
3759
} // end anonymous namespace
3860

3961
void IRGenModule::emitClangDecl(const clang::Decl *decl) {
40-
auto valueDecl = dyn_cast<clang::ValueDecl>(decl);
41-
if (!valueDecl || valueDecl->isExternallyVisible()) {
62+
// Ignore this decl if we've seen it before.
63+
if (!GlobalClangDecls.insert(decl->getCanonicalDecl()).second)
64+
return;
65+
66+
// Fast path for the case where `decl` doesn't contain executable code, so it
67+
// can't reference any other declarations that we would need to emit.
68+
if (getDeclWithExecutableCode(const_cast<clang::Decl *>(decl)) == nullptr) {
4269
ClangCodeGen->HandleTopLevelDecl(
4370
clang::DeclGroupRef(const_cast<clang::Decl*>(decl)));
4471
return;
4572
}
4673

47-
if (!GlobalClangDecls.insert(decl->getCanonicalDecl()).second)
48-
return;
4974
SmallVector<const clang::Decl *, 8> stack;
5075
stack.push_back(decl);
5176

@@ -69,13 +94,15 @@ void IRGenModule::emitClangDecl(const clang::Decl *decl) {
6994

7095
while (!stack.empty()) {
7196
auto *next = const_cast<clang::Decl *>(stack.pop_back_val());
72-
if (auto fn = dyn_cast<clang::FunctionDecl>(next)) {
73-
const clang::FunctionDecl *definition;
74-
if (fn->hasBody(definition)) {
75-
refFinder.TraverseDecl(const_cast<clang::FunctionDecl *>(definition));
76-
next = const_cast<clang::FunctionDecl *>(definition);
77-
}
97+
if (clang::Decl *executableDecl = getDeclWithExecutableCode(next)) {
98+
refFinder.TraverseDecl(executableDecl);
99+
next = executableDecl;
78100
}
101+
102+
if (auto var = dyn_cast<clang::VarDecl>(next))
103+
if (!var->isFileVarDecl())
104+
continue;
105+
79106
ClangCodeGen->HandleTopLevelDecl(clang::DeclGroupRef(next));
80107
}
81108
}

0 commit comments

Comments
 (0)