Skip to content

Commit e7ba87f

Browse files
author
David Ungar
authored
Merge pull request #14230 from davidungar/PR-18-5-lldb-interface
[Batch Mode] Pass PrimarySpecificPaths through compiler. (4)
2 parents 15e8441 + 026b850 commit e7ba87f

24 files changed

+297
-148
lines changed

include/swift/AST/IRGenOptions.h

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -64,9 +64,6 @@ enum class IRGenEmbedMode : unsigned {
6464
/// The set of options supported by IR generation.
6565
class IRGenOptions {
6666
public:
67-
/// The name of the first input file, used by the debug info.
68-
std::string MainInputFilename;
69-
std::vector<std::string> OutputFilenames;
7067
std::string ModuleName;
7168

7269
/// The compilation directory for the debug info.
@@ -188,19 +185,6 @@ class IRGenOptions {
188185
UseSwiftCall(false), GenerateProfile(false), CmdArgs(),
189186
SanitizeCoverage(llvm::SanitizerCoverageOptions()) {}
190187

191-
/// Gets the name of the specified output filename.
192-
/// If multiple files are specified, the last one is returned.
193-
/// This function is used by (at least)
194-
/// lldb/source/Symbol/SwiftASTContext.cpp:4603
195-
/// FIXME: This function should go away in favor of
196-
/// Instance.getFrontendOptions().InputsAndOutputs.getSingleOutputFilename
197-
/// when batch mode handles all contingencies.
198-
StringRef getSingleOutputFilename() const {
199-
if (OutputFilenames.size() >= 1)
200-
return OutputFilenames.back();
201-
return StringRef();
202-
}
203-
204188
// Get a hash of all options which influence the llvm compilation but are not
205189
// reflected in the llvm module itself.
206190
unsigned getLLVMCodeGenOptionsHash() {
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
//===--- PrimarySpecificPaths.h ---------------------------------*- C++ -*-===//
2+
//
3+
// This source file is part of the Swift.org open source project
4+
//
5+
// Copyright (c) 2014 - 2018 Apple Inc. and the Swift project authors
6+
// Licensed under Apache License v2.0 with Runtime Library Exception
7+
//
8+
// See https://swift.org/LICENSE.txt for license information
9+
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
10+
//
11+
//===----------------------------------------------------------------------===//
12+
13+
#ifndef SWIFT_BASIC_PRIMARYSPECIFICPATHS_H
14+
#define SWIFT_BASIC_PRIMARYSPECIFICPATHS_H
15+
16+
#include "swift/Basic/LLVM.h"
17+
#include "swift/Basic/SupplementaryOutputPaths.h"
18+
19+
#include <string>
20+
21+
namespace swift {
22+
class PrimarySpecificPaths {
23+
public:
24+
std::string OutputFilename;
25+
SupplementaryOutputPaths SupplementaryOutputs;
26+
27+
/// The name of the "main" input file, used by the debug info.
28+
std::string MainInputFilenameForDebugInfo;
29+
30+
PrimarySpecificPaths(
31+
std::string OutputFilename = std::string(),
32+
std::string MainInputFilenameForDebugInfo = std::string(),
33+
SupplementaryOutputPaths SupplementaryOutputs =
34+
SupplementaryOutputPaths())
35+
: OutputFilename(OutputFilename),
36+
SupplementaryOutputs(SupplementaryOutputs),
37+
MainInputFilenameForDebugInfo(MainInputFilenameForDebugInfo) {}
38+
};
39+
} // namespace swift
40+
41+
#endif /* SWIFT_BASIC_PRIMARYSPECIFICPATHS_H */

include/swift/Frontend/SupplementaryOutputPaths.h renamed to include/swift/Basic/SupplementaryOutputPaths.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,14 @@ struct SupplementaryOutputPaths {
4747

4848
SupplementaryOutputPaths() = default;
4949
SupplementaryOutputPaths(const SupplementaryOutputPaths &) = default;
50+
51+
bool empty() const {
52+
return ObjCHeaderOutputPath.empty() && ModuleOutputPath.empty() &&
53+
ModuleDocOutputPath.empty() && DependenciesFilePath.empty() &&
54+
ReferenceDependenciesFilePath.empty() &&
55+
SerializedDiagnosticsPath.empty() && LoadedModuleTracePath.empty() &&
56+
TBDPath.empty();
57+
}
5058
};
5159
} // namespace swift
5260

include/swift/Frontend/ArgsToFrontendOutputsConverter.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@
1616
#include "swift/AST/DiagnosticConsumer.h"
1717
#include "swift/AST/DiagnosticEngine.h"
1818
#include "swift/Basic/LLVM.h"
19+
#include "swift/Basic/SupplementaryOutputPaths.h"
1920
#include "swift/Frontend/FrontendOptions.h"
20-
#include "swift/Frontend/SupplementaryOutputPaths.h"
2121
#include "swift/Option/Options.h"
2222
#include "llvm/Option/ArgList.h"
2323

include/swift/Frontend/Frontend.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -301,6 +301,9 @@ class CompilerInvocation {
301301
bool hasSerializedAST() {
302302
return FrontendOpts.InputKind == InputFileKind::IFK_Swift_Library;
303303
}
304+
305+
PrimarySpecificPaths getPrimarySpecificPathsForAtMostOnePrimary();
306+
PrimarySpecificPaths getPrimarySpecificPathsForPrimary(StringRef filename);
304307
};
305308

306309
/// A class which manages the state and execution of the compiler.
@@ -578,6 +581,11 @@ class CompilerInstance {
578581
OptionSet<TypeCheckingFlags> TypeCheckOptions);
579582

580583
void finishTypeChecking(OptionSet<TypeCheckingFlags> TypeCheckOptions);
584+
585+
public:
586+
PrimarySpecificPaths getPrimarySpecificPathsForWholeModuleOptimizationMode();
587+
PrimarySpecificPaths getPrimarySpecificPathsForPrimary(StringRef filename);
588+
PrimarySpecificPaths getPrimarySpecificPathsForAtMostOnePrimary();
581589
};
582590

583591
} // namespace swift

include/swift/Frontend/FrontendInputsAndOutputs.h

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,9 @@
1414
#define SWIFT_FRONTEND_FRONTENDINPUTS_H
1515

1616
#include "swift/AST/Module.h"
17+
#include "swift/Basic/PrimarySpecificPaths.h"
18+
#include "swift/Basic/SupplementaryOutputPaths.h"
1719
#include "swift/Frontend/InputFile.h"
18-
#include "swift/Frontend/SupplementaryOutputPaths.h"
1920
#include "llvm/ADT/Hashing.h"
2021
#include "llvm/ADT/MapVector.h"
2122

@@ -57,6 +58,12 @@ class FrontendInputsAndOutputs {
5758
return SupplementaryOutputs;
5859
}
5960

61+
/// When performing a compilation for zero or one primary input file,
62+
/// this will hold the PrimarySpecificPaths.
63+
/// In a future PR, each InputFile will hold its own PrimarySpecificPaths and
64+
/// this will go away.
65+
PrimarySpecificPaths PrimarySpecificPathsForAtMostOnePrimary;
66+
6067
FrontendInputsAndOutputs() = default;
6168
FrontendInputsAndOutputs(const FrontendInputsAndOutputs &other);
6269
FrontendInputsAndOutputs &operator=(const FrontendInputsAndOutputs &other);
@@ -181,6 +188,10 @@ class FrontendInputsAndOutputs {
181188
public:
182189
unsigned countOfInputsProducingMainOutputs() const;
183190

191+
bool hasInputsProducingMainOutputs() const {
192+
return countOfInputsProducingMainOutputs() != 0;
193+
}
194+
184195
const InputFile &firstInputProducingOutput() const;
185196
const InputFile &lastInputProducingOutput() const;
186197

@@ -210,6 +221,17 @@ class FrontendInputsAndOutputs {
210221
void forEachInputProducingSupplementaryOutput(
211222
llvm::function_ref<void(const InputFile &)> fn) const;
212223

224+
/// Assumes there is not more than one primary input file, if any.
225+
/// Otherwise, you would need to call getPrimarySpecificPathsForPrimary
226+
/// to tell it which primary input you wanted the outputs for.
227+
///
228+
/// Must not be constructed on-the-fly because some parts of the compiler
229+
/// receive StringRefs to its components, so it must live as long as the
230+
/// compiler.
231+
PrimarySpecificPaths &getPrimarySpecificPathsForAtMostOnePrimary();
232+
233+
PrimarySpecificPaths &getPrimarySpecificPathsForPrimary(StringRef filename);
234+
213235
bool hasDependenciesPath() const;
214236
bool hasReferenceDependenciesPath() const;
215237
bool hasObjCHeaderOutputPath() const;

include/swift/Frontend/FrontendOptions.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -275,6 +275,9 @@ class FrontendOptions {
275275
InputsAndOutputs.hasSingleInput();
276276
}
277277

278+
PrimarySpecificPaths getPrimarySpecificPathsForAtMostOnePrimary();
279+
PrimarySpecificPaths getPrimarySpecificPathsForPrimary(StringRef);
280+
278281
private:
279282
static bool canActionEmitDependencies(ActionType);
280283
static bool canActionEmitObjCHeader(ActionType);

include/swift/IRGen/IRGenPublic.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,9 @@ class IRGenModule;
2626

2727
/// Create an IRGen module.
2828
std::pair<IRGenerator *, IRGenModule *>
29-
createIRGenModule(SILModule *SILMod, llvm::LLVMContext &LLVMContext);
29+
createIRGenModule(SILModule *SILMod, StringRef OutputFilename,
30+
StringRef MainInputFilenameForDebugInfo,
31+
llvm::LLVMContext &LLVMContext);
3032

3133
/// Delete the IRGenModule and IRGenerator obtained by the above call.
3234
void deleteIRGenModule(std::pair<IRGenerator *, IRGenModule *> &Module);

include/swift/Subsystems.h

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919

2020
#include "swift/Basic/LLVM.h"
2121
#include "swift/Basic/OptionSet.h"
22+
#include "swift/Basic/PrimarySpecificPaths.h"
2223
#include "swift/Basic/Version.h"
2324
#include "llvm/IR/LLVMContext.h"
2425
#include "llvm/ADT/ArrayRef.h"
@@ -259,7 +260,9 @@ namespace swift {
259260
std::unique_ptr<llvm::Module>
260261
performIRGeneration(IRGenOptions &Opts, ModuleDecl *M,
261262
std::unique_ptr<SILModule> SILMod,
262-
StringRef ModuleName, llvm::LLVMContext &LLVMContext,
263+
StringRef ModuleName, const PrimarySpecificPaths &PSPs,
264+
llvm::LLVMContext &LLVMContext,
265+
ArrayRef<std::string> parallelOutputFilenames,
263266
llvm::GlobalVariable **outModuleHash = nullptr);
264267

265268
/// Turn the given Swift module into either LLVM IR or native code
@@ -268,7 +271,8 @@ namespace swift {
268271
std::unique_ptr<llvm::Module>
269272
performIRGeneration(IRGenOptions &Opts, SourceFile &SF,
270273
std::unique_ptr<SILModule> SILMod,
271-
StringRef ModuleName, llvm::LLVMContext &LLVMContext,
274+
StringRef ModuleName, const PrimarySpecificPaths &PSPs,
275+
llvm::LLVMContext &LLVMContext,
272276
unsigned StartElem = 0,
273277
llvm::GlobalVariable **outModuleHash = nullptr);
274278

@@ -283,8 +287,8 @@ namespace swift {
283287
StringRef OutputPath);
284288

285289
/// Turn the given LLVM module into native code and return true on error.
286-
bool performLLVM(IRGenOptions &Opts, ASTContext &Ctx,
287-
llvm::Module *Module,
290+
bool performLLVM(IRGenOptions &Opts, ASTContext &Ctx, llvm::Module *Module,
291+
StringRef OutputFilename,
288292
UnifiedStatsReporter *Stats=nullptr);
289293

290294
/// Run the LLVM passes. In multi-threaded compilation this will be done for

lib/Frontend/CompilerInvocation.cpp

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -829,18 +829,6 @@ static bool ParseIRGenArgs(IRGenOptions &Opts, ArgList &Args,
829829
if (Args.hasArg(OPT_autolink_force_load))
830830
Opts.ForceLoadSymbolName = Args.getLastArgValue(OPT_module_link_name);
831831

832-
// TODO: investigate whether these should be removed, in favor of definitions
833-
// in other classes.
834-
if (!SILOpts.SILOutputFileNameForDebugging.empty()) {
835-
Opts.MainInputFilename = SILOpts.SILOutputFileNameForDebugging;
836-
} else if (const InputFile *input =
837-
FrontendOpts.InputsAndOutputs.getUniquePrimaryInput()) {
838-
Opts.MainInputFilename = input->file();
839-
} else if (FrontendOpts.InputsAndOutputs.hasSingleInput()) {
840-
Opts.MainInputFilename =
841-
FrontendOpts.InputsAndOutputs.getFilenameOfFirstInput();
842-
}
843-
Opts.OutputFilenames = FrontendOpts.InputsAndOutputs.copyOutputFilenames();
844832
Opts.ModuleName = FrontendOpts.ModuleName;
845833

846834
if (Args.hasArg(OPT_use_jit))

lib/Frontend/Frontend.cpp

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,16 @@ std::string CompilerInvocation::getPCHHash() const {
5353
return llvm::APInt(64, Code).toString(36, /*Signed=*/false);
5454
}
5555

56+
PrimarySpecificPaths
57+
CompilerInvocation::getPrimarySpecificPathsForAtMostOnePrimary() {
58+
return getFrontendOptions().getPrimarySpecificPathsForAtMostOnePrimary();
59+
}
60+
61+
PrimarySpecificPaths
62+
CompilerInvocation::getPrimarySpecificPathsForPrimary(StringRef filename) {
63+
return getFrontendOptions().getPrimarySpecificPathsForPrimary(filename);
64+
}
65+
5666
void CompilerInstance::createSILModule() {
5767
assert(MainModule && "main module not created yet");
5868
// Assume WMO if a -primary-file option was not provided.
@@ -825,3 +835,16 @@ void CompilerInstance::freeASTContext() {
825835
}
826836

827837
void CompilerInstance::freeSILModule() { TheSILModule.reset(); }
838+
839+
PrimarySpecificPaths
840+
CompilerInstance::getPrimarySpecificPathsForWholeModuleOptimizationMode() {
841+
return getPrimarySpecificPathsForAtMostOnePrimary();
842+
}
843+
PrimarySpecificPaths
844+
CompilerInstance::getPrimarySpecificPathsForAtMostOnePrimary() {
845+
return Invocation.getPrimarySpecificPathsForAtMostOnePrimary();
846+
}
847+
PrimarySpecificPaths
848+
CompilerInstance::getPrimarySpecificPathsForPrimary(StringRef filename) {
849+
return Invocation.getPrimarySpecificPathsForPrimary(filename);
850+
}

lib/Frontend/FrontendInputsAndOutputs.cpp

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#include "swift/Frontend/FrontendInputsAndOutputs.h"
1414

1515
#include "swift/AST/DiagnosticsFrontend.h"
16+
#include "swift/Basic/PrimarySpecificPaths.h"
1617
#include "swift/Frontend/FrontendOptions.h"
1718
#include "swift/Option/Options.h"
1819
#include "swift/Parse/Lexer.h"
@@ -36,6 +37,8 @@ FrontendInputsAndOutputs::FrontendInputsAndOutputs(
3637
addInput(input);
3738
IsSingleThreadedWMO = other.IsSingleThreadedWMO;
3839
SupplementaryOutputs = other.SupplementaryOutputs;
40+
PrimarySpecificPathsForAtMostOnePrimary =
41+
other.PrimarySpecificPathsForAtMostOnePrimary;
3942
}
4043

4144
FrontendInputsAndOutputs &FrontendInputsAndOutputs::
@@ -45,6 +48,8 @@ operator=(const FrontendInputsAndOutputs &other) {
4548
addInput(input);
4649
IsSingleThreadedWMO = other.IsSingleThreadedWMO;
4750
SupplementaryOutputs = other.SupplementaryOutputs;
51+
PrimarySpecificPathsForAtMostOnePrimary =
52+
other.PrimarySpecificPathsForAtMostOnePrimary;
4853
return *this;
4954
}
5055

@@ -303,6 +308,18 @@ void FrontendInputsAndOutputs::setMainAndSupplementaryOutputs(
303308
AllInputs[i].setOutputFilename(outputFiles[i]);
304309
}
305310
SupplementaryOutputs = supplementaryOutputs;
311+
312+
if (hasUniquePrimaryInput() || (hasInputs() && isWholeModule())) {
313+
// When batch mode is fully implemented, each InputFile will own
314+
// a PrimarySpecificPaths.
315+
PrimarySpecificPathsForAtMostOnePrimary.OutputFilename =
316+
getSingleOutputFilename();
317+
PrimarySpecificPathsForAtMostOnePrimary.MainInputFilenameForDebugInfo =
318+
hasInputsProducingMainOutputs() ? firstInputProducingOutput().file()
319+
: StringRef();
320+
PrimarySpecificPathsForAtMostOnePrimary.SupplementaryOutputs =
321+
supplementaryOutputs;
322+
}
306323
}
307324

308325
std::vector<std::string> FrontendInputsAndOutputs::copyOutputFilenames() const {
@@ -379,3 +396,14 @@ bool FrontendInputsAndOutputs::hasDependencyTrackerPath() const {
379396
return hasDependenciesPath() || hasReferenceDependenciesPath() ||
380397
hasLoadedModuleTracePath();
381398
}
399+
400+
PrimarySpecificPaths &
401+
FrontendInputsAndOutputs::getPrimarySpecificPathsForAtMostOnePrimary() {
402+
return PrimarySpecificPathsForAtMostOnePrimary;
403+
}
404+
405+
PrimarySpecificPaths &
406+
FrontendInputsAndOutputs::getPrimarySpecificPathsForPrimary(
407+
StringRef filename) {
408+
return getPrimarySpecificPathsForAtMostOnePrimary(); // just a stub for now
409+
}

lib/Frontend/FrontendOptions.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -386,3 +386,13 @@ bool FrontendOptions::doesActionProduceTextualOutput(ActionType action) {
386386
return true;
387387
}
388388
}
389+
390+
PrimarySpecificPaths
391+
FrontendOptions::getPrimarySpecificPathsForAtMostOnePrimary() {
392+
return InputsAndOutputs.getPrimarySpecificPathsForAtMostOnePrimary();
393+
}
394+
395+
PrimarySpecificPaths
396+
FrontendOptions::getPrimarySpecificPathsForPrimary(StringRef filename) {
397+
return InputsAndOutputs.getPrimarySpecificPathsForPrimary(filename);
398+
}

0 commit comments

Comments
 (0)