Skip to content

Commit a741374

Browse files
authored
Merge pull request swiftlang#31106 from CodaFi/globe-star
[NFC] Drastically Reduce The Scope of the "Global LLVMContext"
2 parents 840fce5 + 1f2346d commit a741374

File tree

14 files changed

+93
-125
lines changed

14 files changed

+93
-125
lines changed

include/swift/Basic/LLVMContext.h

Lines changed: 0 additions & 30 deletions
This file was deleted.

include/swift/SIL/SILModule.h

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -233,9 +233,6 @@ class SILModule {
233233
// The list of SILProperties in the module.
234234
PropertyListType properties;
235235

236-
/// The remark output stream used to record SIL remarks to a file.
237-
std::unique_ptr<llvm::raw_fd_ostream> silRemarkStream;
238-
239236
/// The remark streamer used to serialize SIL remarks to a file.
240237
std::unique_ptr<swift::SILRemarkStreamer> silRemarkStreamer;
241238

@@ -526,9 +523,7 @@ class SILModule {
526523
swift::SILRemarkStreamer *getSILRemarkStreamer() {
527524
return silRemarkStreamer.get();
528525
}
529-
void setSILRemarkStreamer(
530-
std::unique_ptr<llvm::raw_fd_ostream> &&remarkStream,
531-
std::unique_ptr<swift::SILRemarkStreamer> &&remarkStreamer);
526+
void installSILRemarkStreamer();
532527

533528
// This is currently limited to VarDecl because the visibility of global
534529
// variables and class properties is straightforward, while the visibility of

include/swift/SIL/SILRemarkStreamer.h

Lines changed: 25 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -25,31 +25,39 @@
2525
namespace swift {
2626

2727
class SILRemarkStreamer {
28-
llvm::remarks::RemarkStreamer &llvmStreamer;
28+
private:
29+
/// The \c LLVMContext the underlying streamer uses for scratch space.
30+
std::unique_ptr<llvm::LLVMContext> streamerContext;
31+
/// The remark output stream used to record SIL remarks to a file.
32+
std::unique_ptr<llvm::raw_fd_ostream> remarkStream;
33+
2934
// Source manager for resolving source locations.
30-
const SourceManager &srcMgr;
35+
const ASTContext &ctx;
3136
/// Convert diagnostics into LLVM remark objects.
3237
/// The lifetime of the members of the result is bound to the lifetime of
3338
/// the SIL remarks.
3439
template <typename RemarkT>
3540
llvm::remarks::Remark
3641
toLLVMRemark(const OptRemark::Remark<RemarkT> &remark) const;
3742

43+
SILRemarkStreamer(std::unique_ptr<llvm::remarks::RemarkStreamer> &&streamer,
44+
std::unique_ptr<llvm::raw_fd_ostream> &&stream,
45+
const ASTContext &Ctx);
46+
47+
public:
48+
static std::unique_ptr<SILRemarkStreamer> create(SILModule &silModule);
49+
3850
public:
39-
SILRemarkStreamer(llvm::remarks::RemarkStreamer &llvmStreamer,
40-
const SourceManager &srcMgr)
41-
: llvmStreamer(llvmStreamer), srcMgr(srcMgr) {}
51+
llvm::remarks::RemarkStreamer &getLLVMStreamer();
52+
const llvm::remarks::RemarkStreamer &getLLVMStreamer() const;
53+
54+
const ASTContext &getASTContext() const { return ctx; }
55+
4256
/// Emit a remark through the streamer.
4357
template <typename RemarkT>
4458
void emit(const OptRemark::Remark<RemarkT> &remark);
4559
};
4660

47-
std::pair<std::unique_ptr<llvm::raw_fd_ostream>,
48-
std::unique_ptr<SILRemarkStreamer>>
49-
createSILRemarkStreamer(SILModule &srcMgr, StringRef filename, StringRef passes,
50-
llvm::remarks::Format format,
51-
DiagnosticEngine &diagEngine, SourceManager &sourceMgr);
52-
5361
// Implementation for template member functions.
5462

5563
// OptRemark type -> llvm::remarks::Type
@@ -80,24 +88,26 @@ llvm::remarks::Remark SILRemarkStreamer::toLLVMRemark(
8088
llvmRemark.PassName = optRemark.getPassName();
8189
llvmRemark.RemarkName = optRemark.getIdentifier();
8290
llvmRemark.FunctionName = optRemark.getDemangledFunctionName();
83-
llvmRemark.Loc = toRemarkLocation(optRemark.getLocation(), srcMgr);
91+
llvmRemark.Loc =
92+
toRemarkLocation(optRemark.getLocation(), getASTContext().SourceMgr);
8493

8594
for (const OptRemark::Argument &arg : optRemark.getArgs()) {
8695
llvmRemark.Args.emplace_back();
8796
llvmRemark.Args.back().Key = arg.key;
8897
llvmRemark.Args.back().Val = arg.val;
89-
llvmRemark.Args.back().Loc = toRemarkLocation(arg.loc, srcMgr);
98+
llvmRemark.Args.back().Loc =
99+
toRemarkLocation(arg.loc, getASTContext().SourceMgr);
90100
}
91101

92102
return llvmRemark;
93103
}
94104

95105
template <typename RemarkT>
96106
void SILRemarkStreamer::emit(const OptRemark::Remark<RemarkT> &optRemark) {
97-
if (!llvmStreamer.matchesFilter(optRemark.getPassName()))
107+
if (!getLLVMStreamer().matchesFilter(optRemark.getPassName()))
98108
return;
99109

100-
return llvmStreamer.getSerializer().emit(toLLVMRemark(optRemark));
110+
return getLLVMStreamer().getSerializer().emit(toLLVMRemark(optRemark));
101111
}
102112

103113
} // namespace swift

lib/AST/Builtins.cpp

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,17 +20,30 @@
2020
#include "swift/AST/Module.h"
2121
#include "swift/AST/ParameterList.h"
2222
#include "swift/AST/TypeCheckRequests.h"
23-
#include "swift/Basic/LLVMContext.h"
2423
#include "swift/Strings.h"
2524
#include "llvm/ADT/SmallString.h"
2625
#include "llvm/ADT/StringSwitch.h"
2726
#include "llvm/IR/Attributes.h"
2827
#include "llvm/IR/Instructions.h"
2928
#include "llvm/IR/Intrinsics.h"
3029
#include "llvm/IR/LLVMContext.h"
30+
#include "llvm/Support/ManagedStatic.h"
3131
#include <tuple>
32+
3233
using namespace swift;
3334

35+
// FIXME: The "Global Context" is a holdover from LLVM's removal of the global
36+
// context. LLVM's global context was used as scratch space to allocate some
37+
// attributes for the routines in this file, so we just made our own to work
38+
// around its removal. Really, it doesn't matter where we allocate these
39+
// attributes, but it's somewhat convenient for now that they're all in one
40+
// place instead of requiring a fresh context for every call. If we can
41+
// sequester ownership of a context (e.g. in SILModule) we could do away with
42+
// this, but there are callers of \c swift::getSwiftFunctionTypeForIntrinsic
43+
// all over the compiler.
44+
static llvm::ManagedStatic<llvm::LLVMContext> GlobalContext;
45+
static llvm::LLVMContext &getGlobalLLVMContext() { return *GlobalContext; }
46+
3447
struct BuiltinExtraInfoTy {
3548
const char *Attributes;
3649
};

lib/Basic/CMakeLists.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,6 @@ add_swift_host_library(swiftBasic STATIC
4949
FileTypes.cpp
5050
JSONSerialization.cpp
5151
LangOptions.cpp
52-
LLVMContext.cpp
5352
Located.cpp
5453
Mangler.cpp
5554
OutputFileMap.cpp

lib/Basic/LLVMContext.cpp

Lines changed: 0 additions & 21 deletions
This file was deleted.

lib/FrontendTool/FrontendTool.cpp

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,6 @@
4141
#include "swift/Basic/Edit.h"
4242
#include "swift/Basic/FileSystem.h"
4343
#include "swift/Basic/JSONSerialization.h"
44-
#include "swift/Basic/LLVMContext.h"
4544
#include "swift/Basic/LLVMInitialize.h"
4645
#include "swift/Basic/Platform.h"
4746
#include "swift/Basic/PrettyStackTrace.h"
@@ -779,8 +778,6 @@ static bool buildModuleFromInterface(const CompilerInvocation &Invocation,
779778

780779
static bool compileLLVMIR(const CompilerInvocation &Invocation,
781780
CompilerInstance &Instance) {
782-
auto &LLVMContext = getGlobalLLVMContext();
783-
784781
// Load in bitcode file.
785782
assert(Invocation.getFrontendOptions().InputsAndOutputs.hasSingleInput() &&
786783
"We expect a single input for bitcode input!");
@@ -800,8 +797,9 @@ static bool compileLLVMIR(const CompilerInvocation &Invocation,
800797
llvm::MemoryBuffer *MainFile = FileBufOrErr.get().get();
801798

802799
llvm::SMDiagnostic Err;
800+
auto LLVMContext = std::make_unique<llvm::LLVMContext>();
803801
std::unique_ptr<llvm::Module> Module =
804-
llvm::parseIR(MainFile->getMemBufferRef(), Err, LLVMContext);
802+
llvm::parseIR(MainFile->getMemBufferRef(), Err, *LLVMContext.get());
805803
if (!Module) {
806804
// TODO: Translate from the diagnostic info to the SourceManager location
807805
// if available.
@@ -1558,7 +1556,6 @@ static bool performCompileStepsPostSILGen(
15581556
FrontendOptions opts = Invocation.getFrontendOptions();
15591557
FrontendOptions::ActionType Action = opts.RequestedAction;
15601558
const ASTContext &Context = Instance.getASTContext();
1561-
const SILOptions &SILOpts = Invocation.getSILOptions();
15621559
const IRGenOptions &IRGenOpts = Invocation.getIRGenOptions();
15631560

15641561
Optional<BufferIndirectlyCausingDiagnosticRAII> ricd;
@@ -1582,10 +1579,7 @@ static bool performCompileStepsPostSILGen(
15821579
return Context.hadError();
15831580
}
15841581

1585-
auto pair = createSILRemarkStreamer(
1586-
*SM, SILOpts.OptRecordFile, SILOpts.OptRecordPasses,
1587-
SILOpts.OptRecordFormat, Instance.getDiags(), Instance.getSourceMgr());
1588-
SM->setSILRemarkStreamer(std::move(pair.first), std::move(pair.second));
1582+
SM->installSILRemarkStreamer();
15891583

15901584
// This is the action to be used to serialize SILModule.
15911585
// It may be invoked multiple times, but it will perform

lib/Immediate/Immediate.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@
2525
#include "swift/AST/IRGenRequests.h"
2626
#include "swift/AST/Module.h"
2727
#include "swift/Basic/LLVM.h"
28-
#include "swift/Basic/LLVMContext.h"
2928
#include "swift/Frontend/Frontend.h"
3029
#include "swift/IRGen/IRGenPublic.h"
3130
#include "swift/SILOptimizer/PassManager/Passes.h"

lib/Immediate/REPL.cpp

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222
#include "swift/AST/Module.h"
2323
#include "swift/AST/NameLookup.h"
2424
#include "swift/AST/NameLookupRequests.h"
25-
#include "swift/Basic/LLVMContext.h"
2625
#include "swift/Frontend/Frontend.h"
2726
#include "swift/IDE/REPLCodeCompletion.h"
2827
#include "swift/IDE/Utils.h"
@@ -765,13 +764,13 @@ static void printOrDumpDecl(Decl *d, PrintOrDump which) {
765764

766765
/// The compiler and execution environment for the REPL.
767766
class REPLEnvironment {
767+
std::unique_ptr<llvm::LLVMContext> LLVMContext;
768768
CompilerInstance &CI;
769769
ModuleDecl *MostRecentModule;
770770
ProcessCmdLine CmdLine;
771771
llvm::SmallPtrSet<swift::ModuleDecl *, 8> ImportedModules;
772772
SmallVector<llvm::Function*, 8> InitFns;
773773
bool RanGlobalInitializers;
774-
llvm::LLVMContext &LLVMContext;
775774
llvm::Module *Module;
776775
llvm::StringSet<> FuncsAlreadyGenerated;
777776
llvm::StringSet<> GlobalsAlreadyEmitted;
@@ -1028,15 +1027,14 @@ class REPLEnvironment {
10281027
public:
10291028
REPLEnvironment(CompilerInstance &CI,
10301029
const ProcessCmdLine &CmdLine,
1031-
llvm::LLVMContext &LLVMCtx,
10321030
bool ParseStdlib)
1033-
: CI(CI),
1031+
: LLVMContext(std::make_unique<llvm::LLVMContext>()),
1032+
CI(CI),
10341033
MostRecentModule(CI.getMainModule()),
10351034
CmdLine(CmdLine),
10361035
RanGlobalInitializers(false),
1037-
LLVMContext(LLVMCtx),
1038-
Module(new llvm::Module("REPL", LLVMContext)),
1039-
DumpModule("REPL", LLVMContext),
1036+
Module(new llvm::Module("REPL", *LLVMContext.get())),
1037+
DumpModule("REPL", *LLVMContext.get()),
10401038
IRGenOpts(),
10411039
SILOpts(),
10421040
Input(*this)
@@ -1281,7 +1279,7 @@ void PrettyStackTraceREPL::print(llvm::raw_ostream &out) const {
12811279

12821280
void swift::runREPL(CompilerInstance &CI, const ProcessCmdLine &CmdLine,
12831281
bool ParseStdlib) {
1284-
REPLEnvironment env(CI, CmdLine, getGlobalLLVMContext(), ParseStdlib);
1282+
REPLEnvironment env(CI, CmdLine, ParseStdlib);
12851283
if (CI.getASTContext().hadError())
12861284
return;
12871285

lib/SIL/IR/SILModule.cpp

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
#include "llvm/ADT/SmallString.h"
2929
#include "llvm/ADT/Statistic.h"
3030
#include "llvm/ADT/StringSwitch.h"
31+
#include "llvm/IR/LLVMContext.h"
3132
#include "llvm/Support/Debug.h"
3233
#include "llvm/Support/YAMLTraits.h"
3334
#include <functional>
@@ -698,11 +699,9 @@ void SILModule::serialize() {
698699
setSerialized();
699700
}
700701

701-
void SILModule::setSILRemarkStreamer(
702-
std::unique_ptr<llvm::raw_fd_ostream> &&remarkStream,
703-
std::unique_ptr<swift::SILRemarkStreamer> &&remarkStreamer) {
704-
silRemarkStream = std::move(remarkStream);
705-
silRemarkStreamer = std::move(remarkStreamer);
702+
void SILModule::installSILRemarkStreamer() {
703+
assert(!silRemarkStreamer && "SIL Remark Streamer is already installed!");
704+
silRemarkStreamer = SILRemarkStreamer::create(*this);
706705
}
707706

708707
bool SILModule::isStdlibModule() const {

0 commit comments

Comments
 (0)