Skip to content

Commit 0b618fe

Browse files
author
Zak Kent
committed
[Immediate] Replace uses of SILMaterializationUnit with EagerSwiftMaterializationUnit
1 parent 473a18d commit 0b618fe

File tree

1 file changed

+2
-222
lines changed

1 file changed

+2
-222
lines changed

lib/Immediate/Immediate.cpp

Lines changed: 2 additions & 222 deletions
Original file line numberDiff line numberDiff line change
@@ -82,18 +82,6 @@ static void *loadRuntimeLib(StringRef sharedLibName,
8282
return nullptr;
8383
}
8484

85-
static void DumpLLVMIR(const llvm::Module &M) {
86-
std::string path = (M.getName() + ".ll").str();
87-
for (size_t count = 0; llvm::sys::fs::exists(path); )
88-
path = (M.getName() + llvm::utostr(count++) + ".ll").str();
89-
90-
std::error_code error;
91-
llvm::raw_fd_ostream stream(path, error);
92-
if (error)
93-
return;
94-
M.print(stream, /*AssemblyAnnotationWriter=*/nullptr);
95-
}
96-
9785
void *swift::immediate::loadSwiftRuntime(ArrayRef<std::string>
9886
runtimeLibPaths) {
9987
#if defined(_WIN32)
@@ -258,219 +246,11 @@ bool swift::immediate::autolinkImportedModules(ModuleDecl *M,
258246
return false;
259247
}
260248

261-
/// The suffix appended to function bodies when creating lazy reexports
262-
static const std::string ManglingSuffix = "$impl";
263-
264-
/// Mangle a function for a lazy reexport
265-
static std::string mangleFunctionBody(const StringRef Unmangled) {
266-
return Unmangled.str() + ManglingSuffix;
267-
}
268-
269-
/// Creates an `LLJIT` instance with the given target options and an
270-
/// attached generator that resolves symbols from the current process
271-
static llvm::Expected<std::unique_ptr<llvm::orc::LLJIT>>
272-
createLLJIT(const IRGenOptions &IRGenOpts, ASTContext &Ctx) {
273-
llvm::TargetOptions TargetOpt;
274-
std::string CPU;
275-
std::string Triple;
276-
std::vector<std::string> Features;
277-
std::tie(TargetOpt, CPU, Features, Triple) =
278-
getIRTargetOptions(IRGenOpts, Ctx);
279-
auto JTMB = llvm::orc::JITTargetMachineBuilder(llvm::Triple(Triple))
280-
.setRelocationModel(llvm::Reloc::PIC_)
281-
.setOptions(std::move(TargetOpt))
282-
.setCPU(std::move(CPU))
283-
.addFeatures(Features)
284-
.setCodeGenOptLevel(llvm::CodeGenOpt::Default);
285-
auto J = llvm::orc::LLJITBuilder()
286-
.setJITTargetMachineBuilder(std::move(JTMB))
287-
.create();
288-
if (!J)
289-
return J.takeError();
290-
auto G = llvm::orc::DynamicLibrarySearchGenerator::GetForCurrentProcess(
291-
(*J)->getDataLayout().getGlobalPrefix());
292-
if (!G)
293-
return G.takeError();
294-
(*J)->getMainJITDylib().addGenerator(std::move(*G));
295-
return J;
296-
}
297-
298-
/// Dump the contents of `Module` if requested
299-
static void dumpJIT(llvm::orc::LLJIT &JIT, const llvm::Module &Module,
300-
const IRGenOptions &IRGenOpts) {
301-
LLVM_DEBUG(llvm::dbgs() << "Module to be executed:\n"; Module.dump());
302-
switch (IRGenOpts.DumpJIT) {
303-
case JITDebugArtifact::None:
304-
break;
305-
case JITDebugArtifact::LLVMIR:
306-
DumpLLVMIR(Module);
307-
break;
308-
case JITDebugArtifact::Object:
309-
JIT.getObjTransformLayer().setTransform(llvm::orc::DumpObjects());
310-
break;
311-
}
312-
}
313-
314-
/// IRGen the provided `SILModule` with the specified options.
315-
/// Returns `std::nullopt` if a compiler error is encountered
316-
static std::optional<GeneratedModule>
317-
generateModule(const CompilerInstance &CI, std::unique_ptr<SILModule> SM) {
318-
// TODO: Use OptimizedIRRequest for this.
319-
const auto &Context = CI.getASTContext();
320-
auto *swiftModule = CI.getMainModule();
321-
const auto PSPs = CI.getPrimarySpecificPathsForAtMostOnePrimary();
322-
const auto &Invocation = CI.getInvocation();
323-
const auto &TBDOpts = Invocation.getTBDGenOptions();
324-
const auto &IRGenOpts = Invocation.getIRGenOptions();
325-
326-
// Lower the SIL module to LLVM IR
327-
auto GenModule = performIRGeneration(
328-
swiftModule, IRGenOpts, TBDOpts, std::move(SM),
329-
swiftModule->getName().str(), PSPs, ArrayRef<std::string>());
330-
331-
if (Context.hadError()) {
332-
return std::nullopt;
333-
}
334-
335-
assert(GenModule && "Emitted no diagnostics but IR generation failed?");
336-
auto *Module = GenModule.getModule();
337-
338-
// Run LLVM passes on the resulting module
339-
performLLVM(IRGenOpts, Context.Diags, /*diagMutex*/ nullptr,
340-
/*hash*/ nullptr, Module, GenModule.getTargetMachine(),
341-
CI.getPrimarySpecificPathsForAtMostOnePrimary().OutputFilename,
342-
CI.getOutputBackend(), Context.Stats);
343-
344-
if (Context.hadError()) {
345-
return std::nullopt;
346-
}
347-
348-
return GenModule;
349-
}
350-
351249
/// Log a compilation error to standard error
352250
static void logError(llvm::Error Err) {
353251
logAllUnhandledErrors(std::move(Err), llvm::errs(), "");
354252
}
355253

356-
/// Lazily materializes an entire SIL module
357-
class SILMaterializationUnit : public llvm::orc::MaterializationUnit {
358-
public:
359-
SILMaterializationUnit(SwiftJIT &JIT, const CompilerInstance &CI,
360-
const IRGenOptions &IRGenOpts,
361-
std::unique_ptr<SILModule> SM)
362-
: MaterializationUnit(getInterface(JIT, CI)), JIT(JIT), CI(CI),
363-
IRGenOpts(IRGenOpts), SM(std::move(SM)) {}
364-
365-
void materialize(
366-
std::unique_ptr<llvm::orc::MaterializationResponsibility> R) override {
367-
368-
auto GenModule = generateModule(CI, std::move(SM));
369-
370-
if (!GenModule) {
371-
R->failMaterialization();
372-
return;
373-
}
374-
375-
auto *Module = GenModule->getModule();
376-
377-
// Dump IR if requested
378-
dumpJIT(*Module);
379-
380-
// Now we must register all other public symbols defined by
381-
// the module with the JIT
382-
llvm::orc::SymbolFlagsMap Symbols;
383-
// Register all global values, including global
384-
// variables and functions
385-
for (const auto &GV : Module->global_values()) {
386-
addGlobal(Symbols, GV);
387-
}
388-
// Register the symbols we have discovered with the JIT
389-
if (auto Err = R->defineMaterializing(Symbols)) {
390-
logError(std::move(Err));
391-
}
392-
auto TSM = std::move(*GenModule).intoThreadSafeContext();
393-
JIT.getIRCompileLayer().emit(std::move(R), std::move(TSM));
394-
}
395-
396-
StringRef getName() const override { return "SILMaterializationUnit"; }
397-
398-
private:
399-
/// Dump the contents of `Module` if requested
400-
void dumpJIT(const llvm::Module &Module) {
401-
::dumpJIT(JIT.getJIT(), Module, IRGenOpts);
402-
}
403-
404-
/// All global value `Global` to `Symbols` if it is a public definition
405-
void addGlobal(llvm::orc::SymbolFlagsMap &Symbols,
406-
const llvm::GlobalValue &Global) {
407-
// Ignore all symbols that will not appear in symbol table
408-
if (Global.hasLocalLinkage() || Global.isDeclaration() ||
409-
Global.hasAppendingLinkage())
410-
return;
411-
auto Name = Global.getName();
412-
// The entry point is already registered up front with the
413-
// interface, so ignore it as well
414-
if (Name == CI.getASTContext().getEntryPointFunctionName())
415-
return;
416-
auto MangledName = JIT.mangleAndIntern(Name);
417-
// Register this symbol with the proper flags
418-
Symbols[MangledName] = llvm::JITSymbolFlags::fromGlobalValue(Global);
419-
}
420-
421-
void discard(const llvm::orc::JITDylib &JD,
422-
const llvm::orc::SymbolStringPtr &Sym) override {}
423-
424-
/// Get the public interface of the main module, which for a script just
425-
/// comprises the entry point
426-
static MaterializationUnit::Interface
427-
getInterface(SwiftJIT &JIT, const CompilerInstance &CI) {
428-
const auto &EntryPoint = CI.getASTContext().getEntryPointFunctionName();
429-
auto MangledEntryPoint =
430-
JIT.mangleAndIntern(mangleFunctionBody(EntryPoint));
431-
auto Flags =
432-
llvm::JITSymbolFlags::Callable | llvm::JITSymbolFlags::Exported;
433-
llvm::orc::SymbolFlagsMap Symbols{{MangledEntryPoint, Flags}};
434-
return {std::move(Symbols), nullptr};
435-
}
436-
437-
SwiftJIT &JIT;
438-
const CompilerInstance &CI;
439-
const IRGenOptions &IRGenOpts;
440-
std::unique_ptr<SILModule> SM;
441-
};
442-
443-
/// Lookup the entry point in `J` and run it with the given command line
444-
/// arguments `CmdLine`. Returns `-1` if failed to compile, or the status
445-
/// returned by the entry point following execution.
446-
static int runMain(llvm::orc::LLJIT &J, const ProcessCmdLine &CmdLine) {
447-
LLVM_DEBUG(llvm::dbgs() << "Running static constructors\n");
448-
if (auto Err = J.initialize(J.getMainJITDylib())) {
449-
logError(std::move(Err));
450-
return -1;
451-
}
452-
453-
auto MainSym = J.lookup("main");
454-
if (!MainSym) {
455-
logError(MainSym.takeError());
456-
return -1;
457-
}
458-
459-
using MainFnTy = int (*)(int, char *[]);
460-
MainFnTy JITMain = MainSym->toPtr<MainFnTy>();
461-
462-
LLVM_DEBUG(llvm::dbgs() << "Running main\n");
463-
int Result = llvm::orc::runAsMain(JITMain, CmdLine);
464-
465-
LLVM_DEBUG(llvm::dbgs() << "Running static destructors\n");
466-
if (auto Err = J.deinitialize(J.getMainJITDylib())) {
467-
logError(std::move(Err));
468-
return -1;
469-
}
470-
471-
return Result;
472-
}
473-
474254
int swift::RunImmediately(CompilerInstance &CI, const ProcessCmdLine &CmdLine,
475255
const IRGenOptions &IRGenOpts,
476256
const SILOptions &SILOpts,
@@ -538,8 +318,8 @@ int swift::RunImmediately(CompilerInstance &CI, const ProcessCmdLine &CmdLine,
538318
return -1;
539319
}
540320

541-
auto MU = std::make_unique<SILMaterializationUnit>(**JIT, CI, IRGenOpts,
542-
std::move(SM));
321+
auto MU = std::make_unique<EagerSwiftMaterializationUnit>(
322+
**JIT, CI, IRGenOpts, std::move(SM));
543323
if (auto Err = (*JIT)->addSwift((*JIT)->getMainJITDylib(), std::move(MU))) {
544324
logError(std::move(Err));
545325
return -1;

0 commit comments

Comments
 (0)