Skip to content

Commit ce17c9b

Browse files
committed
Merge remote-tracking branch 'origin/master' into master-rebranch
2 parents 6ace447 + 18af945 commit ce17c9b

File tree

9 files changed

+120
-67
lines changed

9 files changed

+120
-67
lines changed

include/swift/AST/ModuleLoader.h

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
#include "llvm/ADT/SetVector.h"
2525
#include "llvm/ADT/TinyPtrVector.h"
2626
#include "swift/AST/ModuleDependencies.h"
27+
#include <system_error>
2728

2829
namespace llvm {
2930
class FileCollector;
@@ -103,17 +104,18 @@ struct SubCompilerInstanceInfo {
103104

104105
/// Abstract interface to run an action in a sub ASTContext.
105106
struct InterfaceSubContextDelegate {
106-
virtual bool runInSubContext(StringRef moduleName,
107-
StringRef interfacePath,
108-
StringRef outputPath,
109-
SourceLoc diagLoc,
110-
llvm::function_ref<bool(ASTContext&, ModuleDecl*, ArrayRef<StringRef>,
111-
ArrayRef<StringRef>, StringRef)> action) = 0;
112-
virtual bool runInSubCompilerInstance(StringRef moduleName,
113-
StringRef interfacePath,
114-
StringRef outputPath,
115-
SourceLoc diagLoc,
116-
llvm::function_ref<bool(SubCompilerInstanceInfo&)> action) = 0;
107+
virtual std::error_code runInSubContext(StringRef moduleName,
108+
StringRef interfacePath,
109+
StringRef outputPath,
110+
SourceLoc diagLoc,
111+
llvm::function_ref<std::error_code(ASTContext&, ModuleDecl*,
112+
ArrayRef<StringRef>,
113+
ArrayRef<StringRef>, StringRef)> action) = 0;
114+
virtual std::error_code runInSubCompilerInstance(StringRef moduleName,
115+
StringRef interfacePath,
116+
StringRef outputPath,
117+
SourceLoc diagLoc,
118+
llvm::function_ref<std::error_code(SubCompilerInstanceInfo&)> action) = 0;
117119

118120
virtual ~InterfaceSubContextDelegate() = default;
119121
};

include/swift/Frontend/ModuleInterfaceLoader.h

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -415,17 +415,18 @@ struct InterfaceSubContextDelegateImpl: InterfaceSubContextDelegate {
415415
StringRef prebuiltCachePath,
416416
bool serializeDependencyHashes,
417417
bool trackSystemDependencies);
418-
bool runInSubContext(StringRef moduleName,
419-
StringRef interfacePath,
420-
StringRef outputPath,
421-
SourceLoc diagLoc,
422-
llvm::function_ref<bool(ASTContext&, ModuleDecl*, ArrayRef<StringRef>,
423-
ArrayRef<StringRef>, StringRef)> action) override;
424-
bool runInSubCompilerInstance(StringRef moduleName,
425-
StringRef interfacePath,
426-
StringRef outputPath,
427-
SourceLoc diagLoc,
428-
llvm::function_ref<bool(SubCompilerInstanceInfo&)> action) override;
418+
std::error_code runInSubContext(StringRef moduleName,
419+
StringRef interfacePath,
420+
StringRef outputPath,
421+
SourceLoc diagLoc,
422+
llvm::function_ref<std::error_code(ASTContext&, ModuleDecl*,
423+
ArrayRef<StringRef>, ArrayRef<StringRef>,
424+
StringRef)> action) override;
425+
std::error_code runInSubCompilerInstance(StringRef moduleName,
426+
StringRef interfacePath,
427+
StringRef outputPath,
428+
SourceLoc diagLoc,
429+
llvm::function_ref<std::error_code(SubCompilerInstanceInfo&)> action) override;
429430

430431
~InterfaceSubContextDelegateImpl() = default;
431432

lib/Frontend/ModuleInterfaceBuilder.cpp

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -161,10 +161,10 @@ bool ModuleInterfaceBuilder::buildSwiftModuleInternal(
161161
llvm::RestorePrettyStackState(savedInnerPrettyStackState);
162162
};
163163

164-
SubError = subASTDelegate.runInSubCompilerInstance(moduleName,
165-
interfacePath,
166-
OutPath,
167-
diagnosticLoc,
164+
SubError = (bool)subASTDelegate.runInSubCompilerInstance(moduleName,
165+
interfacePath,
166+
OutPath,
167+
diagnosticLoc,
168168
[&](SubCompilerInstanceInfo &info) {
169169
auto &SubInstance = *info.Instance;
170170
auto subInvocation = SubInstance.getInvocation();
@@ -173,7 +173,7 @@ bool ModuleInterfaceBuilder::buildSwiftModuleInternal(
173173
.getModuleInterfaceLoader())->tryEmitForwardingModule(moduleName,
174174
interfacePath,
175175
CompiledCandidates, OutPath)) {
176-
return false;
176+
return std::error_code();
177177
}
178178
FrontendOptions &FEOpts = subInvocation.getFrontendOptions();
179179
const auto &InputInfo = FEOpts.InputsAndOutputs.firstInput();
@@ -208,7 +208,7 @@ bool ModuleInterfaceBuilder::buildSwiftModuleInternal(
208208
SubInstance.performSema();
209209
if (SubInstance.getASTContext().hadError()) {
210210
LLVM_DEBUG(llvm::dbgs() << "encountered errors\n");
211-
return true;
211+
return std::make_error_code(std::errc::not_supported);
212212
}
213213

214214
SILOptions &SILOpts = subInvocation.getSILOptions();
@@ -217,7 +217,7 @@ bool ModuleInterfaceBuilder::buildSwiftModuleInternal(
217217
auto SILMod = performASTLowering(Mod, TC, SILOpts);
218218
if (!SILMod) {
219219
LLVM_DEBUG(llvm::dbgs() << "SILGen did not produce a module\n");
220-
return true;
220+
return std::make_error_code(std::errc::not_supported);
221221
}
222222

223223
// Setup the callbacks for serialization, which can occur during the
@@ -237,7 +237,7 @@ bool ModuleInterfaceBuilder::buildSwiftModuleInternal(
237237
SmallVector<FileDependency, 16> Deps;
238238
bool serializeHashes = FEOpts.SerializeModuleInterfaceDependencyHashes;
239239
if (collectDepsForSerialization(SubInstance, Deps, serializeHashes)) {
240-
return true;
240+
return std::make_error_code(std::errc::not_supported);
241241
}
242242
if (ShouldSerializeDeps)
243243
SerializationOpts.Dependencies = Deps;
@@ -253,9 +253,12 @@ bool ModuleInterfaceBuilder::buildSwiftModuleInternal(
253253
LLVM_DEBUG(llvm::dbgs() << "Running SIL processing passes\n");
254254
if (SubInstance.performSILProcessing(SILMod.get())) {
255255
LLVM_DEBUG(llvm::dbgs() << "encountered errors\n");
256-
return true;
256+
return std::make_error_code(std::errc::not_supported);
257+
}
258+
if (SubInstance.getDiags().hadAnyError()) {
259+
return std::make_error_code(std::errc::not_supported);
257260
}
258-
return SubInstance.getDiags().hadAnyError();
261+
return std::error_code();
259262
});
260263
});
261264
return !RunSuccess || SubError;

lib/Frontend/ModuleInterfaceLoader.cpp

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1393,11 +1393,12 @@ InterfaceSubContextDelegateImpl::getCacheHash(StringRef useInterfacePath) {
13931393
return llvm::APInt(64, H).toString(36, /*Signed=*/false);
13941394
}
13951395

1396-
bool InterfaceSubContextDelegateImpl::runInSubContext(StringRef moduleName,
1397-
StringRef interfacePath,
1398-
StringRef outputPath,
1399-
SourceLoc diagLoc,
1400-
llvm::function_ref<bool(ASTContext&, ModuleDecl*, ArrayRef<StringRef>,
1396+
std::error_code
1397+
InterfaceSubContextDelegateImpl::runInSubContext(StringRef moduleName,
1398+
StringRef interfacePath,
1399+
StringRef outputPath,
1400+
SourceLoc diagLoc,
1401+
llvm::function_ref<std::error_code(ASTContext&, ModuleDecl*, ArrayRef<StringRef>,
14011402
ArrayRef<StringRef>, StringRef)> action) {
14021403
return runInSubCompilerInstance(moduleName, interfacePath, outputPath, diagLoc,
14031404
[&](SubCompilerInstanceInfo &info){
@@ -1409,11 +1410,12 @@ bool InterfaceSubContextDelegateImpl::runInSubContext(StringRef moduleName,
14091410
});
14101411
}
14111412

1412-
bool InterfaceSubContextDelegateImpl::runInSubCompilerInstance(StringRef moduleName,
1413-
StringRef interfacePath,
1414-
StringRef outputPath,
1415-
SourceLoc diagLoc,
1416-
llvm::function_ref<bool(SubCompilerInstanceInfo&)> action) {
1413+
std::error_code
1414+
InterfaceSubContextDelegateImpl::runInSubCompilerInstance(StringRef moduleName,
1415+
StringRef interfacePath,
1416+
StringRef outputPath,
1417+
SourceLoc diagLoc,
1418+
llvm::function_ref<std::error_code(SubCompilerInstanceInfo&)> action) {
14171419
// We are about to mess up the compiler invocation by using the compiler
14181420
// arguments in the textual interface file. So copy to use a new compiler
14191421
// invocation.
@@ -1457,12 +1459,12 @@ bool InterfaceSubContextDelegateImpl::runInSubCompilerInstance(StringRef moduleN
14571459
CompilerVersion,
14581460
interfacePath,
14591461
diagLoc)) {
1460-
return true;
1462+
return std::make_error_code(std::errc::not_supported);
14611463
}
14621464
// Insert arguments collected from the interface file.
14631465
BuildArgs.insert(BuildArgs.end(), SubArgs.begin(), SubArgs.end());
14641466
if (subInvocation.parseArgs(SubArgs, Diags)) {
1465-
return true;
1467+
return std::make_error_code(std::errc::not_supported);
14661468
}
14671469
CompilerInstance subInstance;
14681470
SubCompilerInstanceInfo info;
@@ -1474,7 +1476,7 @@ bool InterfaceSubContextDelegateImpl::runInSubCompilerInstance(StringRef moduleN
14741476
ForwardingDiagnosticConsumer FDC(Diags);
14751477
subInstance.addDiagnosticConsumer(&FDC);
14761478
if (subInstance.setup(subInvocation)) {
1477-
return true;
1479+
return std::make_error_code(std::errc::not_supported);
14781480
}
14791481
info.BuildArguments = BuildArgs;
14801482
info.Hash = CacheHash;

lib/SILOptimizer/Transforms/OptRemarkGenerator.cpp

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,17 @@
3030
#include "swift/SILOptimizer/Analysis/RCIdentityAnalysis.h"
3131
#include "swift/SILOptimizer/PassManager/Passes.h"
3232
#include "swift/SILOptimizer/PassManager/Transforms.h"
33+
#include "llvm/Support/CommandLine.h"
3334
#include "llvm/Support/raw_ostream.h"
3435

3536
using namespace swift;
3637

38+
static llvm::cl::opt<bool> ForceVisitImplicitAutogeneratedFunctions(
39+
"optremarkgen-visit-implicit-autogen-funcs", llvm::cl::Hidden,
40+
llvm::cl::desc(
41+
"Emit opt remarks even on implicit and autogenerated functions"),
42+
llvm::cl::init(false));
43+
3744
//===----------------------------------------------------------------------===//
3845
// Utility
3946
//===----------------------------------------------------------------------===//
@@ -347,6 +354,21 @@ class OptRemarkGenerator : public SILFunctionTransform {
347354
return;
348355

349356
auto *fn = getFunction();
357+
358+
// Skip top level implicit functions and top level autogenerated functions,
359+
// unless we were asked by the user to emit them.
360+
if (!ForceVisitImplicitAutogeneratedFunctions) {
361+
// Skip implicit functions generated by Sema.
362+
if (auto *ctx = fn->getDeclContext())
363+
if (auto *decl = ctx->getAsDecl())
364+
if (decl->isImplicit())
365+
return;
366+
// Skip autogenerated functions generated by SILGen.
367+
if (auto loc = fn->getDebugScope()->getLoc())
368+
if (loc.isAutoGenerated())
369+
return;
370+
}
371+
350372
LLVM_DEBUG(llvm::dbgs() << "Visiting: " << fn->getName() << "\n");
351373
auto &rcfi = *getAnalysis<RCIdentityAnalysis>()->get(fn);
352374
OptRemarkGeneratorInstructionVisitor visitor(*fn, rcfi);

lib/Serialization/ModuleDependencyScanner.cpp

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,12 @@ class ModuleDependencyScanner : public SerializedModuleLoaderBase {
8181
}
8282
}
8383
assert(fs.exists(InPath));
84+
// Use the private interface file if exits.
85+
auto PrivateInPath =
86+
BaseName.getName(file_types::TY_PrivateSwiftModuleInterfaceFile);
87+
if (fs.exists(PrivateInPath)) {
88+
InPath = PrivateInPath;
89+
}
8490
auto dependencies = scanInterfaceFile(InPath, IsFramework);
8591
if (dependencies) {
8692
this->dependencies = std::move(dependencies.get());
@@ -182,9 +188,8 @@ ErrorOr<ModuleDependencies> ModuleDependencyScanner::scanInterfaceFile(
182188
llvm::SmallString<32> modulePath = moduleName.str();
183189
llvm::sys::path::replace_extension(modulePath, newExt);
184190
Optional<ModuleDependencies> Result;
185-
std::error_code code;
186-
187-
auto hasError = astDelegate.runInSubContext(moduleName.str(),
191+
std::error_code code =
192+
astDelegate.runInSubContext(moduleName.str(),
188193
moduleInterfacePath.str(),
189194
StringRef(),
190195
SourceLoc(),
@@ -205,8 +210,7 @@ ErrorOr<ModuleDependencies> ModuleDependencyScanner::scanInterfaceFile(
205210
auto &fs = *Ctx.SourceMgr.getFileSystem();
206211
auto interfaceBuf = fs.getBufferForFile(moduleInterfacePath);
207212
if (!interfaceBuf) {
208-
code = interfaceBuf.getError();
209-
return true;
213+
return interfaceBuf.getError();
210214
}
211215

212216
// Create a source file.
@@ -225,10 +229,10 @@ ErrorOr<ModuleDependencies> ModuleDependencyScanner::scanInterfaceFile(
225229
for (auto name: imInfo.ModuleNames) {
226230
Result->addModuleDependency(name.str(), &alreadyAddedModules);
227231
}
228-
return false;
232+
return std::error_code();
229233
});
230234

231-
if (hasError) {
235+
if (code) {
232236
return code;
233237
}
234238
return *Result;
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// RUN: %target-swiftc_driver -O -Rpass-missed=sil-opt-remark-gen -Xllvm -sil-disable-pass=FunctionSignatureOpts -emit-sil %s -o /dev/null -Xfrontend -verify -Xllvm -optremarkgen-visit-implicit-autogen-funcs=1
2+
3+
class Klass {}
4+
5+
struct KlassPair {
6+
var lhs: Klass // expected-remark {{retain of type 'Klass'}}
7+
// expected-note @-1 {{of 'self.lhs'}}
8+
// expected-remark @-2 {{release of type 'Klass'}}
9+
// expected-note @-3 {{of 'self.lhs'}}
10+
var rhs: Klass // expected-remark {{retain of type 'Klass'}}
11+
// expected-note @-1 {{of 'self.rhs'}}
12+
// expected-remark @-2 {{release of type 'Klass'}}
13+
// expected-note @-3 {{of 'self.rhs'}}
14+
}
15+

test/SILOptimizer/opt-remark-generator.swift

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,7 @@ case third
3131
}
3232

3333
struct StructWithOwner {
34-
// This retain is from the initializers of owner.
35-
//
36-
// TODO: Should we emit this?
37-
var owner = Klass() // expected-remark {{retain of type 'Klass'}}
38-
// expected-note @-1 {{of 'self.owner'}}
39-
// expected-remark @-2 {{release of type 'Klass'}}
40-
// expected-note @-3 {{of 'self.owner'}}
34+
var owner = Klass()
4135
var state = TrivialState.first
4236
}
4337

@@ -66,14 +60,8 @@ func callingAnInitializerStructWithOwner(x: Klass) -> StructWithOwner {
6660
}
6761

6862
struct KlassPair {
69-
var lhs: Klass // expected-remark {{retain of type 'Klass'}}
70-
// expected-note @-1 {{of 'self.lhs'}}
71-
// expected-remark @-2 {{release of type 'Klass'}}
72-
// expected-note @-3 {{of 'self.lhs'}}
73-
var rhs: Klass // expected-remark {{retain of type 'Klass'}}
74-
// expected-note @-1 {{of 'self.rhs'}}
75-
// expected-remark @-2 {{release of type 'Klass'}}
76-
// expected-note @-3 {{of 'self.rhs'}}
63+
var lhs: Klass
64+
var rhs: Klass
7765
}
7866

7967
func printKlassPair(x : KlassPair) {
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// RUN: %empty-directory(%t)
2+
// RUN: mkdir -p %t/clang-module-cache
3+
// RUN: echo "// swift-interface-format-version: 1.0" > %t/Foo.swiftinterface
4+
// RUN: echo "// swift-module-flags: -module-name Foo" >> %t/Foo.swiftinterface
5+
// RUN: echo "public func foo() {}" >> %t/Foo.swiftinterface
6+
7+
// RUN: cp %t/Foo.swiftinterface %t/Foo.private.swiftinterface
8+
9+
// RUN: %target-swift-frontend -scan-dependencies -module-cache-path %t/clang-module-cache %s -o %t/deps.json -I %t -disable-implicit-swift-modules -Xcc -Xclang -Xcc -fno-implicit-modules
10+
11+
// Check the contents of the JSON output
12+
// RUN: %FileCheck %s < %t/deps.json
13+
14+
import Foo
15+
16+
// CHECK: "moduleInterfacePath": "{{.*}}Foo.private.swiftinterface",

0 commit comments

Comments
 (0)