16#include <llvm/IR/LegacyPassManager.h>
17#include <llvm/IR/Module.h>
18#include <llvm/MC/TargetRegistry.h>
19#include <llvm/Target/TargetMachine.h>
53 llvm::raw_ostream &stderrOS,
bool exitEarly,
bool disableOutput);
62 llvm::orc::ThreadSafeContext &TSC)
66 std::string ErrorString;
68 const llvm::Target *
Target = llvm::TargetRegistry::lookupTarget(
69 PTU.
TheModule->getTargetTriple(), ErrorString);
71 return llvm::make_error<llvm::StringError>(
"Failed to create Wasm Target: ",
72 llvm::inconvertibleErrorCode());
75 llvm::TargetOptions TO = llvm::TargetOptions();
76 llvm::TargetMachine *TargetMachine =
Target->createTargetMachine(
77 PTU.
TheModule->getTargetTriple(),
"",
"", TO, llvm::Reloc::Model::PIC_);
78 PTU.
TheModule->setDataLayout(TargetMachine->createDataLayout());
79 std::string ObjectFileName = PTU.
TheModule->getName().str() +
".o";
80 std::string BinaryFileName = PTU.
TheModule->getName().str() +
".wasm";
82 std::error_code Error;
83 llvm::raw_fd_ostream ObjectFileOutput(llvm::StringRef(ObjectFileName), Error);
85 llvm::legacy::PassManager PM;
86 if (TargetMachine->addPassesToEmitFile(PM, ObjectFileOutput,
nullptr,
87 llvm::CodeGenFileType::ObjectFile)) {
88 return llvm::make_error<llvm::StringError>(
89 "Wasm backend cannot produce object.", llvm::inconvertibleErrorCode());
94 return llvm::make_error<llvm::StringError>(
"Failed to emit Wasm object.",
95 llvm::inconvertibleErrorCode());
98 ObjectFileOutput.close();
100 std::vector<const char *> LinkerArgs = {
"wasm-ld",
103 "--experimental-pic",
106 ObjectFileName.c_str(),
108 BinaryFileName.c_str()};
111 std::vector<lld::DriverDef> WasmDriverArgs;
112 WasmDriverArgs.push_back(WasmDriver);
114 lld::lldMain(LinkerArgs, llvm::outs(), llvm::errs(), WasmDriverArgs);
117 return llvm::make_error<llvm::StringError>(
118 "Failed to link incremental module", llvm::inconvertibleErrorCode());
120 void *LoadedLibModule =
121 dlopen(BinaryFileName.c_str(), RTLD_NOW | RTLD_GLOBAL);
122 if (LoadedLibModule ==
nullptr) {
123 llvm::errs() << dlerror() <<
'\n';
124 return llvm::make_error<llvm::StringError>(
125 "Failed to load incremental module", llvm::inconvertibleErrorCode());
128 return llvm::Error::success();
132 return llvm::make_error<llvm::StringError>(
"Not implemented yet",
133 llvm::inconvertibleErrorCode());
138 return llvm::Error::success();
144 return llvm::Error::success();
llvm::MachO::Target Target
llvm::Error addModule(PartialTranslationUnit &PTU) override
llvm::Error runCtors() const override
~WasmIncrementalExecutor() override
llvm::Error removeModule(PartialTranslationUnit &PTU) override
llvm::Error cleanUp() override
WasmIncrementalExecutor(llvm::orc::ThreadSafeContext &TSC)
The JSON file list parser is used to communicate input to InstallAPI.
@ Result
The result type of a method or function.
bool link(llvm::ArrayRef< const char * > args, llvm::raw_ostream &stdoutOS, llvm::raw_ostream &stderrOS, bool exitEarly, bool disableOutput)
Result lldMain(llvm::ArrayRef< const char * > args, llvm::raw_ostream &stdoutOS, llvm::raw_ostream &stderrOS, llvm::ArrayRef< DriverDef > drivers)
bool(*)(llvm::ArrayRef< const char * >, llvm::raw_ostream &, llvm::raw_ostream &, bool, bool) Driver
The class keeps track of various objects created as part of processing incremental inputs.
std::unique_ptr< llvm::Module > TheModule
The llvm IR produced for the input.