1 //===------ Interpreter.cpp - Incremental Compilation and Execution -------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file implements the component which performs incremental code
10 // compilation and execution.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #include "clang/Interpreter/Interpreter.h"
15
16 #include "IncrementalExecutor.h"
17 #include "IncrementalParser.h"
18
19 #include "clang/AST/ASTContext.h"
20 #include "clang/Basic/TargetInfo.h"
21 #include "clang/CodeGen/ModuleBuilder.h"
22 #include "clang/CodeGen/ObjectFilePCHContainerOperations.h"
23 #include "clang/Driver/Compilation.h"
24 #include "clang/Driver/Driver.h"
25 #include "clang/Driver/Job.h"
26 #include "clang/Driver/Options.h"
27 #include "clang/Driver/Tool.h"
28 #include "clang/Frontend/CompilerInstance.h"
29 #include "clang/Frontend/TextDiagnosticBuffer.h"
30 #include "clang/Lex/PreprocessorOptions.h"
31
32 #include "llvm/IR/Module.h"
33 #include "llvm/Support/Errc.h"
34 #include "llvm/Support/Host.h"
35
36 using namespace clang;
37
38 // FIXME: Figure out how to unify with namespace init_convenience from
39 // tools/clang-import-test/clang-import-test.cpp
40 namespace {
41 /// Retrieves the clang CC1 specific flags out of the compilation's jobs.
42 /// \returns NULL on error.
43 static llvm::Expected<const llvm::opt::ArgStringList *>
GetCC1Arguments(DiagnosticsEngine * Diagnostics,driver::Compilation * Compilation)44 GetCC1Arguments(DiagnosticsEngine *Diagnostics,
45 driver::Compilation *Compilation) {
46 // We expect to get back exactly one Command job, if we didn't something
47 // failed. Extract that job from the Compilation.
48 const driver::JobList &Jobs = Compilation->getJobs();
49 if (!Jobs.size() || !isa<driver::Command>(*Jobs.begin()))
50 return llvm::createStringError(llvm::errc::not_supported,
51 "Driver initialization failed. "
52 "Unable to create a driver job");
53
54 // The one job we find should be to invoke clang again.
55 const driver::Command *Cmd = cast<driver::Command>(&(*Jobs.begin()));
56 if (llvm::StringRef(Cmd->getCreator().getName()) != "clang")
57 return llvm::createStringError(llvm::errc::not_supported,
58 "Driver initialization failed");
59
60 return &Cmd->getArguments();
61 }
62
63 static llvm::Expected<std::unique_ptr<CompilerInstance>>
CreateCI(const llvm::opt::ArgStringList & Argv)64 CreateCI(const llvm::opt::ArgStringList &Argv) {
65 std::unique_ptr<CompilerInstance> Clang(new CompilerInstance());
66 IntrusiveRefCntPtr<DiagnosticIDs> DiagID(new DiagnosticIDs());
67
68 // Register the support for object-file-wrapped Clang modules.
69 // FIXME: Clang should register these container operations automatically.
70 auto PCHOps = Clang->getPCHContainerOperations();
71 PCHOps->registerWriter(std::make_unique<ObjectFilePCHContainerWriter>());
72 PCHOps->registerReader(std::make_unique<ObjectFilePCHContainerReader>());
73
74 // Buffer diagnostics from argument parsing so that we can output them using
75 // a well formed diagnostic object.
76 IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts = new DiagnosticOptions();
77 TextDiagnosticBuffer *DiagsBuffer = new TextDiagnosticBuffer;
78 DiagnosticsEngine Diags(DiagID, &*DiagOpts, DiagsBuffer);
79 bool Success = CompilerInvocation::CreateFromArgs(
80 Clang->getInvocation(), llvm::ArrayRef(Argv.begin(), Argv.size()), Diags);
81
82 // Infer the builtin include path if unspecified.
83 if (Clang->getHeaderSearchOpts().UseBuiltinIncludes &&
84 Clang->getHeaderSearchOpts().ResourceDir.empty())
85 Clang->getHeaderSearchOpts().ResourceDir =
86 CompilerInvocation::GetResourcesPath(Argv[0], nullptr);
87
88 // Create the actual diagnostics engine.
89 Clang->createDiagnostics();
90 if (!Clang->hasDiagnostics())
91 return llvm::createStringError(llvm::errc::not_supported,
92 "Initialization failed. "
93 "Unable to create diagnostics engine");
94
95 DiagsBuffer->FlushDiagnostics(Clang->getDiagnostics());
96 if (!Success)
97 return llvm::createStringError(llvm::errc::not_supported,
98 "Initialization failed. "
99 "Unable to flush diagnostics");
100
101 // FIXME: Merge with CompilerInstance::ExecuteAction.
102 llvm::MemoryBuffer *MB = llvm::MemoryBuffer::getMemBuffer("").release();
103 Clang->getPreprocessorOpts().addRemappedFile("<<< inputs >>>", MB);
104
105 Clang->setTarget(TargetInfo::CreateTargetInfo(
106 Clang->getDiagnostics(), Clang->getInvocation().TargetOpts));
107 if (!Clang->hasTarget())
108 return llvm::createStringError(llvm::errc::not_supported,
109 "Initialization failed. "
110 "Target is missing");
111
112 Clang->getTarget().adjust(Clang->getDiagnostics(), Clang->getLangOpts());
113
114 // Don't clear the AST before backend codegen since we do codegen multiple
115 // times, reusing the same AST.
116 Clang->getCodeGenOpts().ClearASTBeforeBackend = false;
117
118 Clang->getFrontendOpts().DisableFree = false;
119 Clang->getCodeGenOpts().DisableFree = false;
120
121 return std::move(Clang);
122 }
123
124 } // anonymous namespace
125
126 llvm::Expected<std::unique_ptr<CompilerInstance>>
create(std::vector<const char * > & ClangArgv)127 IncrementalCompilerBuilder::create(std::vector<const char *> &ClangArgv) {
128
129 // If we don't know ClangArgv0 or the address of main() at this point, try
130 // to guess it anyway (it's possible on some platforms).
131 std::string MainExecutableName =
132 llvm::sys::fs::getMainExecutable(nullptr, nullptr);
133
134 ClangArgv.insert(ClangArgv.begin(), MainExecutableName.c_str());
135
136 // Prepending -c to force the driver to do something if no action was
137 // specified. By prepending we allow users to override the default
138 // action and use other actions in incremental mode.
139 // FIXME: Print proper driver diagnostics if the driver flags are wrong.
140 // We do C++ by default; append right after argv[0] if no "-x" given
141 ClangArgv.insert(ClangArgv.end(), "-xc++");
142 ClangArgv.insert(ClangArgv.end(), "-Xclang");
143 ClangArgv.insert(ClangArgv.end(), "-fincremental-extensions");
144 ClangArgv.insert(ClangArgv.end(), "-c");
145
146 // Put a dummy C++ file on to ensure there's at least one compile job for the
147 // driver to construct.
148 ClangArgv.push_back("<<< inputs >>>");
149
150 // Buffer diagnostics from argument parsing so that we can output them using a
151 // well formed diagnostic object.
152 IntrusiveRefCntPtr<DiagnosticIDs> DiagID(new DiagnosticIDs());
153 IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts =
154 CreateAndPopulateDiagOpts(ClangArgv);
155 TextDiagnosticBuffer *DiagsBuffer = new TextDiagnosticBuffer;
156 DiagnosticsEngine Diags(DiagID, &*DiagOpts, DiagsBuffer);
157
158 driver::Driver Driver(/*MainBinaryName=*/ClangArgv[0],
159 llvm::sys::getProcessTriple(), Diags);
160 Driver.setCheckInputsExist(false); // the input comes from mem buffers
161 llvm::ArrayRef<const char *> RF = llvm::ArrayRef(ClangArgv);
162 std::unique_ptr<driver::Compilation> Compilation(Driver.BuildCompilation(RF));
163
164 if (Compilation->getArgs().hasArg(driver::options::OPT_v))
165 Compilation->getJobs().Print(llvm::errs(), "\n", /*Quote=*/false);
166
167 auto ErrOrCC1Args = GetCC1Arguments(&Diags, Compilation.get());
168 if (auto Err = ErrOrCC1Args.takeError())
169 return std::move(Err);
170
171 return CreateCI(**ErrOrCC1Args);
172 }
173
Interpreter(std::unique_ptr<CompilerInstance> CI,llvm::Error & Err)174 Interpreter::Interpreter(std::unique_ptr<CompilerInstance> CI,
175 llvm::Error &Err) {
176 llvm::ErrorAsOutParameter EAO(&Err);
177 auto LLVMCtx = std::make_unique<llvm::LLVMContext>();
178 TSCtx = std::make_unique<llvm::orc::ThreadSafeContext>(std::move(LLVMCtx));
179 IncrParser = std::make_unique<IncrementalParser>(std::move(CI),
180 *TSCtx->getContext(), Err);
181 }
182
~Interpreter()183 Interpreter::~Interpreter() {
184 if (IncrExecutor) {
185 if (llvm::Error Err = IncrExecutor->cleanUp())
186 llvm::report_fatal_error(
187 llvm::Twine("Failed to clean up IncrementalExecutor: ") +
188 toString(std::move(Err)));
189 }
190 }
191
192 llvm::Expected<std::unique_ptr<Interpreter>>
create(std::unique_ptr<CompilerInstance> CI)193 Interpreter::create(std::unique_ptr<CompilerInstance> CI) {
194 llvm::Error Err = llvm::Error::success();
195 auto Interp =
196 std::unique_ptr<Interpreter>(new Interpreter(std::move(CI), Err));
197 if (Err)
198 return std::move(Err);
199 return std::move(Interp);
200 }
201
getCompilerInstance() const202 const CompilerInstance *Interpreter::getCompilerInstance() const {
203 return IncrParser->getCI();
204 }
205
getExecutionEngine() const206 const llvm::orc::LLJIT *Interpreter::getExecutionEngine() const {
207 if (IncrExecutor)
208 return IncrExecutor->getExecutionEngine();
209 return nullptr;
210 }
211
212 llvm::Expected<PartialTranslationUnit &>
Parse(llvm::StringRef Code)213 Interpreter::Parse(llvm::StringRef Code) {
214 return IncrParser->Parse(Code);
215 }
216
Execute(PartialTranslationUnit & T)217 llvm::Error Interpreter::Execute(PartialTranslationUnit &T) {
218 assert(T.TheModule);
219 if (!IncrExecutor) {
220 const clang::TargetInfo &TI =
221 getCompilerInstance()->getASTContext().getTargetInfo();
222 llvm::Error Err = llvm::Error::success();
223 IncrExecutor = std::make_unique<IncrementalExecutor>(*TSCtx, Err, TI);
224
225 if (Err)
226 return Err;
227 }
228 // FIXME: Add a callback to retain the llvm::Module once the JIT is done.
229 if (auto Err = IncrExecutor->addModule(T))
230 return Err;
231
232 if (auto Err = IncrExecutor->runCtors())
233 return Err;
234
235 return llvm::Error::success();
236 }
237
238 llvm::Expected<llvm::JITTargetAddress>
getSymbolAddress(GlobalDecl GD) const239 Interpreter::getSymbolAddress(GlobalDecl GD) const {
240 if (!IncrExecutor)
241 return llvm::make_error<llvm::StringError>("Operation failed. "
242 "No execution engine",
243 std::error_code());
244 llvm::StringRef MangledName = IncrParser->GetMangledName(GD);
245 return getSymbolAddress(MangledName);
246 }
247
248 llvm::Expected<llvm::JITTargetAddress>
getSymbolAddress(llvm::StringRef IRName) const249 Interpreter::getSymbolAddress(llvm::StringRef IRName) const {
250 if (!IncrExecutor)
251 return llvm::make_error<llvm::StringError>("Operation failed. "
252 "No execution engine",
253 std::error_code());
254
255 return IncrExecutor->getSymbolAddress(IRName, IncrementalExecutor::IRName);
256 }
257
258 llvm::Expected<llvm::JITTargetAddress>
getSymbolAddressFromLinkerName(llvm::StringRef Name) const259 Interpreter::getSymbolAddressFromLinkerName(llvm::StringRef Name) const {
260 if (!IncrExecutor)
261 return llvm::make_error<llvm::StringError>("Operation failed. "
262 "No execution engine",
263 std::error_code());
264
265 return IncrExecutor->getSymbolAddress(Name, IncrementalExecutor::LinkerName);
266 }
267
Undo(unsigned N)268 llvm::Error Interpreter::Undo(unsigned N) {
269
270 std::list<PartialTranslationUnit> &PTUs = IncrParser->getPTUs();
271 if (N > PTUs.size())
272 return llvm::make_error<llvm::StringError>("Operation failed. "
273 "Too many undos",
274 std::error_code());
275 for (unsigned I = 0; I < N; I++) {
276 if (IncrExecutor) {
277 if (llvm::Error Err = IncrExecutor->removeModule(PTUs.back()))
278 return Err;
279 }
280
281 IncrParser->CleanUpPTU(PTUs.back());
282 PTUs.pop_back();
283 }
284 return llvm::Error::success();
285 }
286