Skip to content

Commit 7e4d476

Browse files
Merge remote-tracking branch 'origin/main' into root/merge-main-2023-05-24
2 parents d33bd9f + ccae422 commit 7e4d476

File tree

15 files changed

+275
-103
lines changed

15 files changed

+275
-103
lines changed

Diff for: benchmark/CMakeLists.txt

+1
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ set(SWIFT_BENCH_MODULES
3737
single-source/ArrayOfGenericRef
3838
single-source/ArrayOfPOD
3939
single-source/ArrayOfRef
40+
single-source/ArrayRemoveAll
4041
single-source/ArraySetElement
4142
single-source/ArraySubscript
4243
single-source/BinaryFloatingPointConversionFromBinaryInteger

Diff for: benchmark/single-source/ArrayRemoveAll.swift

+55
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
// This test checks the performance of removeAll
2+
// on a non-uniquely referenced array.
3+
4+
import TestsUtils
5+
6+
public let benchmarks = [
7+
BenchmarkInfo(
8+
name: "Array.removeAll.keepingCapacity.Int",
9+
runFunction: run_ArrayRemoveAll_Class,
10+
tags: [.validation, .api, .Array],
11+
setUpFunction: { blackHole(inputArray_Class) }
12+
),
13+
BenchmarkInfo(
14+
name: "Array.removeAll.keepingCapacity.Object",
15+
runFunction: run_ArrayRemoveAll_Int,
16+
tags: [.validation, .api, .Array],
17+
setUpFunction: { blackHole(inputArray_Int) }
18+
)
19+
]
20+
21+
class Slow {
22+
public var num: Int
23+
24+
init(num: Int) {
25+
self.num = num
26+
}
27+
}
28+
29+
let inputArray_Int: [Int] = Array(0..<500_000)
30+
let inputArray_Class: [Slow] = (0..<50_000).map(Slow.init(num:))
31+
32+
@inline(never)
33+
func removeAll<T>(_ arr: [T]) -> [T] {
34+
var copy = arr
35+
copy.removeAll(keepingCapacity: true)
36+
return copy
37+
}
38+
39+
@inline(never)
40+
func run_ArrayRemoveAll_Class(_ n: Int) {
41+
var copy = removeAll(inputArray_Class);
42+
for _ in 1..<n {
43+
copy = removeAll(inputArray_Class)
44+
}
45+
check(copy.capacity == inputArray_Class.capacity)
46+
}
47+
48+
@inline(never)
49+
func run_ArrayRemoveAll_Int(_ n: Int) {
50+
var copy = removeAll(inputArray_Int);
51+
for _ in 1..<n {
52+
copy = removeAll(inputArray_Int)
53+
}
54+
check(copy.capacity == inputArray_Int.capacity)
55+
}

Diff for: benchmark/utils/main.swift

+2
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import ArrayOfGenericPOD
2525
import ArrayOfGenericRef
2626
import ArrayOfPOD
2727
import ArrayOfRef
28+
import ArrayRemoveAll
2829
import ArraySetElement
2930
import ArraySubscript
3031
import BinaryFloatingPointConversionFromBinaryInteger
@@ -217,6 +218,7 @@ register(ArrayOfGenericPOD.benchmarks)
217218
register(ArrayOfGenericRef.benchmarks)
218219
register(ArrayOfPOD.benchmarks)
219220
register(ArrayOfRef.benchmarks)
221+
register(ArrayRemoveAll.benchmarks)
220222
register(ArraySetElement.benchmarks)
221223
register(ArraySubscript.benchmarks)
222224
register(BinaryFloatingPointConversionFromBinaryInteger.benchmarks)

Diff for: include/module.modulemap

+48
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
module BasicBridging {
2+
header "swift/Basic/BridgedSwiftObject.h"
3+
header "swift/Basic/BasicBridging.h"
4+
header "swift/Basic/SourceLoc.h"
5+
requires cplusplus
6+
export *
7+
}
8+
9+
module CBasicBridging {
10+
header "swift/Basic/CBasicBridging.h"
11+
}
12+
13+
module ASTBridging {
14+
header "swift/AST/AnyFunctionRef.h"
15+
header "swift/AST/ASTBridging.h"
16+
header "swift/AST/Builtins.h"
17+
header "swift/AST/DiagnosticEngine.h"
18+
header "swift/AST/DiagnosticConsumer.h"
19+
header "swift/AST/ForeignAsyncConvention.h"
20+
header "swift/AST/ForeignErrorConvention.h"
21+
header "swift/AST/SubstitutionMap.h"
22+
23+
textual header "swift/AST/Builtins.def"
24+
25+
requires cplusplus
26+
export *
27+
}
28+
29+
module CASTBridging {
30+
header "swift/AST/CASTBridging.h"
31+
}
32+
33+
module SILBridging {
34+
header "swift/SIL/SILBridging.h"
35+
header "swift/SIL/SILLocation.h"
36+
requires cplusplus
37+
export *
38+
}
39+
40+
module OptimizerBridging {
41+
header "swift/SILOptimizer/OptimizerBridging.h"
42+
export *
43+
}
44+
45+
module _RegexParserBridging {
46+
header "swift/Parse/RegexParserBridging.h"
47+
export *
48+
}

Diff for: include/swift/ClangImporter/ClangImporter.h

+3-2
Original file line numberDiff line numberDiff line change
@@ -397,7 +397,8 @@ class ClangImporter final : public ClangModuleLoader {
397397
/// replica.
398398
///
399399
/// \sa clang::GeneratePCHAction
400-
bool emitBridgingPCH(StringRef headerPath, StringRef outputPCHPath);
400+
bool emitBridgingPCH(StringRef headerPath, StringRef outputPCHPath,
401+
bool cached);
401402

402403
/// Returns true if a clang CompilerInstance can successfully read in a PCH,
403404
/// assuming it exists, with the current options. This can be used to find out
@@ -529,7 +530,7 @@ class ClangImporter final : public ClangModuleLoader {
529530

530531
Optional<std::string>
531532
getOrCreatePCH(const ClangImporterOptions &ImporterOptions,
532-
StringRef SwiftPCHHash);
533+
StringRef SwiftPCHHash, bool Cached);
533534
Optional<std::string>
534535
/// \param isExplicit true if the PCH filename was passed directly
535536
/// with -import-objc-header option.

Diff for: include/swift/module.modulemap

-48
This file was deleted.

Diff for: lib/ClangImporter/ClangImporter.cpp

+28-19
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,8 @@ namespace {
194194
Importer.addSearchPath(path, /*isFramework*/false, /*isSystem=*/false);
195195
}
196196

197-
auto PCH = Importer.getOrCreatePCH(ImporterOpts, SwiftPCHHash);
197+
auto PCH =
198+
Importer.getOrCreatePCH(ImporterOpts, SwiftPCHHash, /*Cached=*/true);
198199
if (PCH.has_value()) {
199200
Impl.getClangInstance()->getPreprocessorOpts().ImplicitPCHInclude =
200201
PCH.value();
@@ -582,8 +583,10 @@ importer::getNormalInvocationArguments(
582583
});
583584
}
584585

585-
if (auto path = getCxxShimModuleMapPath(searchPathOpts, triple)) {
586-
invocationArgStrs.push_back((Twine("-fmodule-map-file=") + *path).str());
586+
if (LangOpts.EnableCXXInterop) {
587+
if (auto path = getCxxShimModuleMapPath(searchPathOpts, triple)) {
588+
invocationArgStrs.push_back((Twine("-fmodule-map-file=") + *path).str());
589+
}
587590
}
588591

589592
// Set C language options.
@@ -996,8 +999,9 @@ ClangImporter::getPCHFilename(const ClangImporterOptions &ImporterOptions,
996999
return PCHFilename.str().str();
9971000
}
9981001

999-
Optional<std::string> ClangImporter::getOrCreatePCH(
1000-
const ClangImporterOptions &ImporterOptions, StringRef SwiftPCHHash) {
1002+
Optional<std::string>
1003+
ClangImporter::getOrCreatePCH(const ClangImporterOptions &ImporterOptions,
1004+
StringRef SwiftPCHHash, bool Cached) {
10011005
bool isExplicit;
10021006
auto PCHFilename = getPCHFilename(ImporterOptions, SwiftPCHHash,
10031007
isExplicit);
@@ -1013,8 +1017,8 @@ Optional<std::string> ClangImporter::getOrCreatePCH(
10131017
<< EC.message();
10141018
return None;
10151019
}
1016-
auto FailedToEmit =
1017-
emitBridgingPCH(ImporterOptions.BridgingHeader, PCHFilename.value());
1020+
auto FailedToEmit = emitBridgingPCH(ImporterOptions.BridgingHeader,
1021+
PCHFilename.value(), Cached);
10181022
if (FailedToEmit) {
10191023
return None;
10201024
}
@@ -1026,9 +1030,10 @@ Optional<std::string> ClangImporter::getOrCreatePCH(
10261030
std::vector<std::string>
10271031
ClangImporter::getClangArguments(ASTContext &ctx, bool ignoreClangTarget) {
10281032
std::vector<std::string> invocationArgStrs;
1029-
// Clang expects this to be like an actual command line. So we need to pass in
1030-
// "clang" for argv[0]
1031-
invocationArgStrs.push_back(ctx.ClangImporterOpts.clangPath);
1033+
// When creating from driver commands, clang expects this to be like an actual
1034+
// command line. So we need to pass in "clang" for argv[0]
1035+
if (!ctx.ClangImporterOpts.DirectClangCC1ModuleBuild)
1036+
invocationArgStrs.push_back(ctx.ClangImporterOpts.clangPath);
10321037
if (ctx.ClangImporterOpts.ExtraArgsOnly) {
10331038
invocationArgStrs.insert(invocationArgStrs.end(),
10341039
ctx.ClangImporterOpts.ExtraArgs.begin(),
@@ -1777,13 +1782,13 @@ ClangImporter::cloneCompilerInstanceForPrecompiling() {
17771782
}
17781783

17791784
bool ClangImporter::emitBridgingPCH(
1780-
StringRef headerPath, StringRef outputPCHPath) {
1785+
StringRef headerPath, StringRef outputPCHPath, bool cached) {
17811786
auto emitInstance = cloneCompilerInstanceForPrecompiling();
17821787
auto &invocation = emitInstance->getInvocation();
17831788

17841789
auto LangOpts = invocation.getLangOpts();
17851790
LangOpts->NeededByPCHOrCompilationUsesPCH = true;
1786-
LangOpts->CacheGeneratedPCH = true;
1791+
LangOpts->CacheGeneratedPCH = cached;
17871792

17881793
auto language = getLanguageFromOptions(LangOpts);
17891794
auto inputFile = clang::FrontendInputFile(headerPath, language);
@@ -2034,12 +2039,16 @@ ModuleDecl *ClangImporter::Implementation::loadModuleClang(
20342039
auto &clangHeaderSearch = getClangPreprocessor().getHeaderSearchInfo();
20352040
auto realModuleName = SwiftContext.getRealModuleName(path.front().Item).str();
20362041

2037-
// Look up the top-level module first, to see if it exists at all.
2038-
clang::Module *clangModule = clangHeaderSearch.lookupModule(
2039-
realModuleName, /*ImportLoc=*/clang::SourceLocation(),
2040-
/*AllowSearch=*/true, /*AllowExtraModuleMapSearch=*/true);
2041-
if (!clangModule)
2042-
return nullptr;
2042+
// For explicit module build, module should always exist but module map might
2043+
// not be exist. Go straight to module loader.
2044+
if (Instance->getInvocation().getLangOpts()->ImplicitModules) {
2045+
// Look up the top-level module first, to see if it exists at all.
2046+
clang::Module *clangModule = clangHeaderSearch.lookupModule(
2047+
realModuleName, /*ImportLoc=*/clang::SourceLocation(),
2048+
/*AllowSearch=*/true, /*AllowExtraModuleMapSearch=*/true);
2049+
if (!clangModule)
2050+
return nullptr;
2051+
}
20432052

20442053
// Convert the Swift import path over to a Clang import path.
20452054
SmallVector<std::pair<clang::IdentifierInfo *, clang::SourceLocation>, 4>
@@ -2094,7 +2103,7 @@ ModuleDecl *ClangImporter::Implementation::loadModuleClang(
20942103

20952104
// Now load the top-level module, so that we can check if the submodule
20962105
// exists without triggering a fatal error.
2097-
clangModule = loadModule(clangPath.front(), clang::Module::AllVisible);
2106+
auto clangModule = loadModule(clangPath.front(), clang::Module::AllVisible);
20982107
if (!clangModule)
20992108
return nullptr;
21002109

Diff for: lib/FrontendTool/FrontendTool.cpp

+8-2
Original file line numberDiff line numberDiff line change
@@ -369,12 +369,13 @@ static bool precompileBridgingHeader(const CompilerInstance &Instance) {
369369
if (!PCHOutDir.empty()) {
370370
// Create or validate a persistent PCH.
371371
auto SwiftPCHHash = Invocation.getPCHHash();
372-
auto PCH = clangImporter->getOrCreatePCH(ImporterOpts, SwiftPCHHash);
372+
auto PCH = clangImporter->getOrCreatePCH(ImporterOpts, SwiftPCHHash,
373+
/*cached=*/false);
373374
return !PCH.has_value();
374375
}
375376
return clangImporter->emitBridgingPCH(
376377
opts.InputsAndOutputs.getFilenameOfFirstInput(),
377-
opts.InputsAndOutputs.getSingleOutputFilename());
378+
opts.InputsAndOutputs.getSingleOutputFilename(), /*cached=*/false);
378379
}
379380

380381
static bool precompileClangModule(const CompilerInstance &Instance) {
@@ -2072,6 +2073,11 @@ int swift::performFrontend(ArrayRef<const char *> Args,
20722073
auto finishDiagProcessing = [&](int retValue, bool verifierEnabled) -> int {
20732074
FinishDiagProcessingCheckRAII.CalledFinishDiagProcessing = true;
20742075
PDC.setSuppressOutput(false);
2076+
if (auto *CDP = Instance->getCachingDiagnosticsProcessor()) {
2077+
// Don't cache if build failed.
2078+
if (retValue)
2079+
CDP->endDiagnosticCapture();
2080+
}
20752081
bool diagnosticsError = Instance->getDiags().finishProcessing();
20762082
// If the verifier is enabled and did not encounter any verification errors,
20772083
// return 0 even if the compile failed. This behavior isn't ideal, but large

Diff for: lib/SILOptimizer/Mandatory/MoveOnlyAddressCheckerUtils.cpp

+12
Original file line numberDiff line numberDiff line change
@@ -1867,6 +1867,18 @@ bool GatherUsesVisitor::visitUse(Operand *op) {
18671867
if (!leafRange)
18681868
return false;
18691869

1870+
// Now check if we have a destructure through deinit. If we do, emit an
1871+
// error.
1872+
unsigned numDiagnostics =
1873+
moveChecker.diagnosticEmitter.getDiagnosticCount();
1874+
checkForDestructureThroughDeinit(markedValue, op, *leafRange,
1875+
diagnosticEmitter);
1876+
if (numDiagnostics != moveChecker.diagnosticEmitter.getDiagnosticCount()) {
1877+
LLVM_DEBUG(llvm::dbgs()
1878+
<< "Emitting destructure through deinit error!\n");
1879+
return true;
1880+
}
1881+
18701882
LLVM_DEBUG(llvm::dbgs() << "Pure consuming use: " << *user);
18711883
useState.takeInsts.insert({user, *leafRange});
18721884
return true;

Diff for: test/CAS/loc-directive-diagnostics.swift

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// RUN: %empty-directory(%t)
2-
// RUN: not %target-swift-frontend -emit-module -emit-module-path %t/test.module \
2+
// RUN: %target-swift-frontend -emit-module -emit-module-path %t/test.module \
33
// RUN: -enable-cas -cas-path %t/cas -allow-unstable-cache-key-for-testing %s 2>&1 | %FileCheck %s
44
// RUN: %cache-tool -cas-path %t/cas -cache-tool-action print-output-keys -- \
55
// RUN: %target-swift-frontend -emit-module -emit-module-path %t/test.module -enable-cas -cas-path %t/cas \
@@ -9,7 +9,7 @@
99
// RUN: -allow-unstable-cache-key-for-testing %s 2>&1 | %FileCheck %s
1010

1111
#sourceLocation(file: "anything.swift", line: 1)
12-
func 1() {}
12+
#warning("this is a warning")
1313
#sourceLocation()
1414

15-
// CHECK: anything.swift:1:6: error: function name
15+
// CHECK: anything.swift:1:10: warning: this is a warning

Diff for: test/ClangImporter/pcm-emit-direct-cc1-mode.swift

+2-2
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@
99
// CHECK-DUMP: Module map file: {{.*[/\\]}}Inputs{{/|\\}}custom-modules{{/|\\}}module.map
1010

1111
// Verify that the clang command-line used is cc1
12-
// RUN: %FileCheck -check-prefix CHECK-CLANG %s < %t.diags.txt
13-
// CHECK-CLANG: '{{.*[/\\]}}clang'{{.*}}'-fmodules'
12+
// RUN: %FileCheck -check-prefix CHECK-CLANG -DTRIPLE=%target-triple %s < %t.diags.txt
13+
// CHECK-CLANG: '{{.*[/\\]}}module.map' '-o' '{{.*[/\\]}}script.pcm' '-fmodules' '-triple' '[[TRIPLE]]' '-x' 'objective-c'
1414

1515
import script
1616
var _ : ScriptTy

0 commit comments

Comments
 (0)