| 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 "DeviceOffload.h" |
| 15 | #include "IncrementalAction.h" |
| 16 | #include "IncrementalParser.h" |
| 17 | #include "InterpreterUtils.h" |
| 18 | |
| 19 | #include "clang/AST/ASTConsumer.h" |
| 20 | #include "clang/AST/ASTContext.h" |
| 21 | #include "clang/AST/Mangle.h" |
| 22 | #include "clang/AST/TypeVisitor.h" |
| 23 | #include "clang/Basic/DiagnosticParse.h" |
| 24 | #include "clang/Basic/DiagnosticSema.h" |
| 25 | #include "clang/Basic/FileManager.h" |
| 26 | #include "clang/Basic/TargetInfo.h" |
| 27 | #include "clang/CodeGen/CodeGenAction.h" |
| 28 | #include "clang/CodeGen/ObjectFilePCHContainerWriter.h" |
| 29 | #include "clang/Driver/Compilation.h" |
| 30 | #include "clang/Driver/Driver.h" |
| 31 | #include "clang/Driver/Job.h" |
| 32 | #include "clang/Driver/Tool.h" |
| 33 | #include "clang/Frontend/CompilerInstance.h" |
| 34 | #include "clang/Frontend/FrontendAction.h" |
| 35 | #include "clang/Frontend/FrontendOptions.h" |
| 36 | #include "clang/Frontend/MultiplexConsumer.h" |
| 37 | #include "clang/Frontend/TextDiagnosticBuffer.h" |
| 38 | #include "clang/FrontendTool/Utils.h" |
| 39 | #include "clang/Interpreter/IncrementalExecutor.h" |
| 40 | #include "clang/Interpreter/Interpreter.h" |
| 41 | #include "clang/Interpreter/Value.h" |
| 42 | #include "clang/Lex/Pragma.h" |
| 43 | #include "clang/Lex/Preprocessor.h" |
| 44 | #include "clang/Lex/PreprocessorOptions.h" |
| 45 | #include "clang/Options/OptionUtils.h" |
| 46 | #include "clang/Options/Options.h" |
| 47 | #include "clang/Sema/Lookup.h" |
| 48 | #include "clang/Serialization/ASTReader.h" |
| 49 | #include "clang/Serialization/ModuleCache.h" |
| 50 | #include "clang/Serialization/ObjectFilePCHContainerReader.h" |
| 51 | #include "llvm/ADT/StringExtras.h" |
| 52 | #include "llvm/ADT/StringSwitch.h" |
| 53 | #include "llvm/ExecutionEngine/JITSymbol.h" |
| 54 | #include "llvm/ExecutionEngine/Orc/EPCDynamicLibrarySearchGenerator.h" |
| 55 | #include "llvm/ExecutionEngine/Orc/LLJIT.h" |
| 56 | #include "llvm/IR/Module.h" |
| 57 | #include "llvm/Support/Errc.h" |
| 58 | #include "llvm/Support/ErrorHandling.h" |
| 59 | #include "llvm/Support/Process.h" |
| 60 | #include "llvm/Support/VirtualFileSystem.h" |
| 61 | #include "llvm/Support/raw_ostream.h" |
| 62 | #include "llvm/TargetParser/Host.h" |
| 63 | #include "llvm/TargetParser/Triple.h" |
| 64 | #include "llvm/Transforms/Utils/Cloning.h" // for CloneModule |
| 65 | |
| 66 | #define DEBUG_TYPE "clang-repl" |
| 67 | |
| 68 | using namespace clang; |
| 69 | // FIXME: Figure out how to unify with namespace init_convenience from |
| 70 | // tools/clang-import-test/clang-import-test.cpp |
| 71 | namespace { |
| 72 | /// Retrieves the clang CC1 specific flags out of the compilation's jobs. |
| 73 | /// \returns NULL on error. |
| 74 | static llvm::Expected<const llvm::opt::ArgStringList *> |
| 75 | GetCC1Arguments(DiagnosticsEngine *Diagnostics, |
| 76 | driver::Compilation *Compilation) { |
| 77 | // We expect to get back exactly one Command job, if we didn't something |
| 78 | // failed. Extract that job from the Compilation. |
| 79 | const driver::JobList &Jobs = Compilation->getJobs(); |
| 80 | if (!Jobs.size()) |
| 81 | return llvm::createStringError(EC: llvm::errc::not_supported, |
| 82 | S: "Driver initialization failed. " |
| 83 | "Unable to create a driver job" ); |
| 84 | |
| 85 | // The driver creates one device compile job per architecture (for example if |
| 86 | // --offload-arch=native finds several GPUs). Only one can be used. |
| 87 | unsigned NumDeviceJobs = llvm::count_if(Range: Jobs, P: [](const driver::Command &C) { |
| 88 | return llvm::StringRef(C.getCreator().getName()) == "clang" && |
| 89 | llvm::is_contained(Range: C.getArguments(), |
| 90 | Element: llvm::StringRef("-fcuda-is-device" )); |
| 91 | }); |
| 92 | if (NumDeviceJobs > 1) |
| 93 | return llvm::createStringError( |
| 94 | EC: llvm::errc::not_supported, |
| 95 | S: "Multiple device architectures are not supported. Pass a single " |
| 96 | "--offload-arch" ); |
| 97 | |
| 98 | // The one job we find should be to invoke clang again. |
| 99 | const driver::Command *Cmd = &*Jobs.begin(); |
| 100 | if (llvm::StringRef(Cmd->getCreator().getName()) != "clang" ) |
| 101 | return llvm::createStringError(EC: llvm::errc::not_supported, |
| 102 | S: "Driver initialization failed" ); |
| 103 | |
| 104 | return &Cmd->getArguments(); |
| 105 | } |
| 106 | |
| 107 | // ASTReaderListener that captures the PIC level stored in a serialized AST |
| 108 | // file (PCH or PCM) so the interpreter can compare it against its own. |
| 109 | class PICLevelReader : public ASTReaderListener { |
| 110 | unsigned &PICLevel; |
| 111 | |
| 112 | public: |
| 113 | PICLevelReader(unsigned &PICLevel) : PICLevel(PICLevel) {} |
| 114 | |
| 115 | bool ReadLanguageOptions(const LangOptions &LangOpts, |
| 116 | StringRef ModuleFilename, bool Complain, |
| 117 | bool AllowCompatibleDifferences) override { |
| 118 | PICLevel = LangOpts.PICLevel; |
| 119 | return false; |
| 120 | } |
| 121 | }; |
| 122 | |
| 123 | // clang-repl always compiles position-independent code (it injects -fPIC), so a |
| 124 | // PCH/PCM that was built with a different PIC level is incompatible: mixing the |
| 125 | // two leads to relocations that may be out of range once the JIT maps code more |
| 126 | // than 2GB away. PICLevel is a "compatible" language option, so the ASTReader |
| 127 | // would otherwise accept the mismatch silently. Reject it up front. |
| 128 | // |
| 129 | // The file is probed with its own FileManager and ModuleCache (sharing only the |
| 130 | // VFS) so this read leaves the CompilerInstance's state untouched for the real |
| 131 | // load performed later by ExecuteAction(). |
| 132 | static llvm::Error checkASTFilePICLevel(CompilerInstance &Clang, |
| 133 | StringRef Filename) { |
| 134 | llvm::IntrusiveRefCntPtr<FileManager> FileMgr(new FileManager( |
| 135 | Clang.getFileSystemOpts(), Clang.getVirtualFileSystemPtr())); |
| 136 | std::shared_ptr<ModuleCache> ModCache = createCrossProcessModuleCache(); |
| 137 | unsigned ASTPICLevel = 0; |
| 138 | PICLevelReader Reader(ASTPICLevel); |
| 139 | if (!ASTReader::readASTFileControlBlock( |
| 140 | Filename, FileMgr&: *FileMgr, ModCache: *ModCache, PCHContainerRdr: Clang.getPCHContainerReader(), |
| 141 | /*FindModuleFileExtensions=*/false, Listener&: Reader, |
| 142 | /*ValidateDiagnosticOptions=*/false) && |
| 143 | ASTPICLevel != Clang.getLangOpts().PICLevel) |
| 144 | return llvm::createStringError( |
| 145 | EC: llvm::errc::not_supported, |
| 146 | Fmt: "AST file '%s' was built with PIC level %u, which is incompatible " |
| 147 | "with clang-repl's PIC level %u" , |
| 148 | Vals: Filename.str().c_str(), Vals: ASTPICLevel, Vals: Clang.getLangOpts().PICLevel); |
| 149 | return llvm::Error::success(); |
| 150 | } |
| 151 | |
| 152 | static llvm::Expected<std::unique_ptr<CompilerInstance>> |
| 153 | CreateCI(const llvm::opt::ArgStringList &Argv) { |
| 154 | std::unique_ptr<CompilerInstance> Clang(new CompilerInstance()); |
| 155 | |
| 156 | // Register the support for object-file-wrapped Clang modules. |
| 157 | // FIXME: Clang should register these container operations automatically. |
| 158 | auto PCHOps = Clang->getPCHContainerOperations(); |
| 159 | PCHOps->registerWriter(Writer: std::make_unique<ObjectFilePCHContainerWriter>()); |
| 160 | PCHOps->registerReader(Reader: std::make_unique<ObjectFilePCHContainerReader>()); |
| 161 | |
| 162 | // Buffer diagnostics from argument parsing so that we can output them using |
| 163 | // a well formed diagnostic object. |
| 164 | DiagnosticOptions DiagOpts; |
| 165 | TextDiagnosticBuffer *DiagsBuffer = new TextDiagnosticBuffer; |
| 166 | DiagnosticsEngine Diags(DiagnosticIDs::create(), DiagOpts, DiagsBuffer); |
| 167 | bool Success = CompilerInvocation::CreateFromArgs( |
| 168 | Res&: Clang->getInvocation(), CommandLineArgs: llvm::ArrayRef(Argv.begin(), Argv.size()), Diags); |
| 169 | |
| 170 | // Infer the builtin include path if unspecified. |
| 171 | if (Clang->getHeaderSearchOpts().UseBuiltinIncludes && |
| 172 | Clang->getHeaderSearchOpts().ResourceDir.empty()) |
| 173 | Clang->getHeaderSearchOpts().ResourceDir = |
| 174 | GetResourcesPath(Argv0: Argv[0], MainAddr: nullptr); |
| 175 | |
| 176 | Clang->createVirtualFileSystem(); |
| 177 | |
| 178 | // Create the actual diagnostics engine. |
| 179 | Clang->createDiagnostics(); |
| 180 | |
| 181 | DiagsBuffer->FlushDiagnostics(Diags&: Clang->getDiagnostics()); |
| 182 | if (!Success) |
| 183 | return llvm::createStringError(EC: llvm::errc::not_supported, |
| 184 | S: "Initialization failed. " |
| 185 | "Unable to flush diagnostics" ); |
| 186 | |
| 187 | // FIXME: Merge with CompilerInstance::ExecuteAction. |
| 188 | llvm::MemoryBuffer *MB = llvm::MemoryBuffer::getMemBuffer(InputData: "" ).release(); |
| 189 | Clang->getPreprocessorOpts().addRemappedFile(From: "<<< inputs >>>" , To: MB); |
| 190 | |
| 191 | Clang->setTarget(TargetInfo::CreateTargetInfo( |
| 192 | Diags&: Clang->getDiagnostics(), Opts&: Clang->getInvocation().getTargetOpts())); |
| 193 | if (!Clang->hasTarget()) |
| 194 | return llvm::createStringError(EC: llvm::errc::not_supported, |
| 195 | S: "Initialization failed. " |
| 196 | "Target is missing" ); |
| 197 | |
| 198 | Clang->getTarget().adjust(Diags&: Clang->getDiagnostics(), Opts&: Clang->getLangOpts(), |
| 199 | Aux: Clang->getAuxTarget()); |
| 200 | |
| 201 | // Don't clear the AST before backend codegen since we do codegen multiple |
| 202 | // times, reusing the same AST. |
| 203 | Clang->getCodeGenOpts().ClearASTBeforeBackend = false; |
| 204 | |
| 205 | Clang->getFrontendOpts().DisableFree = false; |
| 206 | Clang->getCodeGenOpts().DisableFree = false; |
| 207 | |
| 208 | // Reject any precompiled input (PCH or PCM) built with a PIC level that |
| 209 | // differs from clang-repl's own, before any Interpreter/FrontendAction is |
| 210 | // constructed. See checkASTFilePICLevel for the rationale. |
| 211 | StringRef PCHInclude = Clang->getPreprocessorOpts().ImplicitPCHInclude; |
| 212 | if (!PCHInclude.empty()) |
| 213 | if (llvm::Error Err = checkASTFilePICLevel(Clang&: *Clang, Filename: PCHInclude)) |
| 214 | return std::move(Err); |
| 215 | |
| 216 | // Explicitly loaded modules: -fmodule-file=<path> and |
| 217 | // -fmodule-file=<name>=<path>. |
| 218 | for (StringRef ModuleFile : Clang->getFrontendOpts().ModuleFiles) |
| 219 | if (llvm::Error Err = checkASTFilePICLevel(Clang&: *Clang, Filename: ModuleFile)) |
| 220 | return std::move(Err); |
| 221 | for (const auto &NameAndFile : |
| 222 | Clang->getHeaderSearchOpts().PrebuiltModuleFiles) |
| 223 | if (llvm::Error Err = checkASTFilePICLevel(Clang&: *Clang, Filename: NameAndFile.second)) |
| 224 | return std::move(Err); |
| 225 | |
| 226 | return std::move(Clang); |
| 227 | } |
| 228 | |
| 229 | static llvm::Error ExecuteIncrementalAction(CompilerInstance &CI, |
| 230 | IncrementalAction &Act) { |
| 231 | // Parse -mllvm before the action gives CI's library options to the |
| 232 | // LLVMContext. |
| 233 | std::string Msg; |
| 234 | llvm::raw_string_ostream OS(Msg); |
| 235 | if (!CI.parseLLVMArgs(Errs: &OS)) { |
| 236 | // Without a preprocessor, the SourceManager never adopts the buffers. |
| 237 | PreprocessorOptions &PPOpts = CI.getPreprocessorOpts(); |
| 238 | if (!PPOpts.RetainRemappedFileBuffers) |
| 239 | for (auto &RB : PPOpts.RemappedFileBuffers) |
| 240 | delete RB.second; |
| 241 | PPOpts.clearRemappedFiles(); |
| 242 | return llvm::createStringError(S: llvm::StringRef(Msg).rtrim()); |
| 243 | } |
| 244 | if (!CI.ExecuteAction(Act) || CI.getDiagnostics().hasErrorOccurred()) { |
| 245 | return llvm::createStringError(EC: llvm::errc::not_supported, |
| 246 | S: "Failed to execute incremental action" ); |
| 247 | } |
| 248 | return llvm::Error::success(); |
| 249 | } |
| 250 | |
| 251 | // '#pragma clang repl optimize(<opt>)' sets the optimization level and size |
| 252 | // used to emit modules for input parsed after the pragma. <opt> is an -O flag |
| 253 | // spelled without the leading dash: O0, O1, O2, O3, Os or Oz. |
| 254 | struct PragmaReplHandler : public PragmaHandler { |
| 255 | Interpreter &Interp; |
| 256 | |
| 257 | PragmaReplHandler(Interpreter &Interp) |
| 258 | : PragmaHandler("repl" ), Interp(Interp) {} |
| 259 | |
| 260 | void HandlePragma(Preprocessor &PP, PragmaIntroducer Introducer, |
| 261 | Token &FirstToken) override { |
| 262 | Token Tok; |
| 263 | PP.LexUnexpandedToken(Result&: Tok); |
| 264 | if (Tok.isNot(K: tok::identifier) || |
| 265 | !Tok.getIdentifierInfo()->isStr(Str: "optimize" )) { |
| 266 | PP.Diag(Loc: Tok.getLocation(), DiagID: diag::warn_pragma_expected_identifier) |
| 267 | << "clang repl" ; |
| 268 | return; |
| 269 | } |
| 270 | |
| 271 | PP.LexUnexpandedToken(Result&: Tok); |
| 272 | if (Tok.isNot(K: tok::l_paren)) { |
| 273 | PP.Diag(Loc: Tok.getLocation(), DiagID: diag::warn_pragma_expected_lparen) |
| 274 | << "clang repl optimize" ; |
| 275 | return; |
| 276 | } |
| 277 | |
| 278 | PP.LexUnexpandedToken(Result&: Tok); |
| 279 | std::string Opt = PP.getSpelling(Tok); |
| 280 | // The OptimizationLevel and OptimizeSize that each -O flag sets. |
| 281 | std::optional<std::pair<unsigned, unsigned>> Level = |
| 282 | llvm::StringSwitch<std::optional<std::pair<unsigned, unsigned>>>(Opt) |
| 283 | .Case(S: "O0" , Value: std::pair(0U, 0U)) |
| 284 | .Case(S: "O1" , Value: std::pair(1U, 0U)) |
| 285 | .Case(S: "O2" , Value: std::pair(2U, 0U)) |
| 286 | .Case(S: "O3" , Value: std::pair(3U, 0U)) |
| 287 | .Case(S: "Os" , Value: std::pair(2U, 1U)) |
| 288 | .Case(S: "Oz" , Value: std::pair(2U, 2U)) |
| 289 | .Default(Value: std::nullopt); |
| 290 | if (!Level) { |
| 291 | PP.Diag(Loc: Tok.getLocation(), DiagID: diag::warn_pragma_invalid_argument) |
| 292 | << Opt << "clang repl optimize" << /*Expected=*/true |
| 293 | << "'O0', 'O1', 'O2', 'O3', 'Os' or 'Oz'" ; |
| 294 | return; |
| 295 | } |
| 296 | |
| 297 | PP.LexUnexpandedToken(Result&: Tok); |
| 298 | if (Tok.isNot(K: tok::r_paren)) { |
| 299 | PP.Diag(Loc: Tok.getLocation(), DiagID: diag::warn_pragma_expected_rparen) |
| 300 | << "clang repl optimize" ; |
| 301 | return; |
| 302 | } |
| 303 | |
| 304 | PP.LexUnexpandedToken(Result&: Tok); |
| 305 | if (Tok.isNot(K: tok::eod)) { |
| 306 | PP.Diag(Loc: Tok.getLocation(), DiagID: diag::warn_pragma_extra_tokens_at_eol) |
| 307 | << "clang repl optimize" ; |
| 308 | return; |
| 309 | } |
| 310 | |
| 311 | Interp.setOptLevel(OptLevel: Level->first, OptSize: Level->second); |
| 312 | } |
| 313 | }; |
| 314 | |
| 315 | } // anonymous namespace |
| 316 | |
| 317 | namespace clang { |
| 318 | |
| 319 | llvm::Expected<std::unique_ptr<CompilerInstance>> |
| 320 | IncrementalCompilerBuilder::create(std::string TT, |
| 321 | std::vector<const char *> &ClangArgv) { |
| 322 | |
| 323 | // If we don't know ClangArgv0 or the address of main() at this point, try |
| 324 | // to guess it anyway (it's possible on some platforms). |
| 325 | std::string MainExecutableName = |
| 326 | llvm::sys::fs::getMainExecutable(argv0: nullptr, MainExecAddr: nullptr); |
| 327 | |
| 328 | ClangArgv.insert(position: ClangArgv.begin(), x: MainExecutableName.c_str()); |
| 329 | |
| 330 | // Compile as position-independent code. This prevents the frontend from |
| 331 | // marking external symbols (e.g. C++ type-info such as _ZTIPKc used for |
| 332 | // exception handling) as dso_local and emitting direct PC-relative |
| 333 | // references. JITLink can place the GOT entry near the JIT'd code, keeping |
| 334 | // the relocation in range. Without -fPIC, a direct Delta32 relocation to a |
| 335 | // host symbol may be out of range when the JIT memory is mapped more than |
| 336 | // 2GB away (as on FreeBSD), breaking tests such as |
| 337 | // Interpreter/simple-exception.cpp. Insert before user arguments so it can |
| 338 | // still be overridden. On Windows (excluding Cygwin/MinGW) an explicit |
| 339 | // -fPIC is an unsupported driver option that would drop non-x86_64 targets |
| 340 | // to PIC level 0; PIC is already the forced default there where relevant, |
| 341 | // so don't inject it. |
| 342 | llvm::Triple TargetTriple(TT); |
| 343 | if (!TargetTriple.isOSWindows() || TargetTriple.isOSCygMing()) |
| 344 | ClangArgv.insert(position: ClangArgv.begin() + 1, x: "-fPIC" ); |
| 345 | |
| 346 | // Prepending -c to force the driver to do something if no action was |
| 347 | // specified. By prepending we allow users to override the default |
| 348 | // action and use other actions in incremental mode. |
| 349 | // FIXME: Print proper driver diagnostics if the driver flags are wrong. |
| 350 | // We do C++ by default; append right after argv[0] if no "-x" given |
| 351 | ClangArgv.insert(position: ClangArgv.end(), x: "-Xclang" ); |
| 352 | ClangArgv.insert(position: ClangArgv.end(), x: "-fincremental-extensions" ); |
| 353 | ClangArgv.insert(position: ClangArgv.end(), x: "-c" ); |
| 354 | |
| 355 | // Put a dummy C++ file on to ensure there's at least one compile job for the |
| 356 | // driver to construct. |
| 357 | ClangArgv.push_back(x: "<<< inputs >>>" ); |
| 358 | |
| 359 | // Buffer diagnostics from argument parsing so that we can output them using a |
| 360 | // well formed diagnostic object. |
| 361 | std::unique_ptr<DiagnosticOptions> DiagOpts = |
| 362 | CreateAndPopulateDiagOpts(Argv: ClangArgv); |
| 363 | TextDiagnosticBuffer *DiagsBuffer = new TextDiagnosticBuffer; |
| 364 | DiagnosticsEngine Diags(DiagnosticIDs::create(), *DiagOpts, DiagsBuffer); |
| 365 | |
| 366 | driver::Driver Driver(/*MainBinaryName=*/ClangArgv[0], TT, Diags); |
| 367 | Driver.setCheckInputsExist(false); // the input comes from mem buffers |
| 368 | llvm::ArrayRef<const char *> RF = llvm::ArrayRef(ClangArgv); |
| 369 | std::unique_ptr<driver::Compilation> Compilation(Driver.BuildCompilation(Args: RF)); |
| 370 | |
| 371 | if (CompilationCB) |
| 372 | if (auto Err = (*CompilationCB)(*Compilation.get())) |
| 373 | return std::move(Err); |
| 374 | |
| 375 | if (Compilation->getArgs().hasArg(Ids: options::OPT_v)) |
| 376 | Compilation->getJobs().Print(OS&: llvm::errs(), Terminator: "\n" , /*Quote=*/false); |
| 377 | |
| 378 | auto ErrOrCC1Args = GetCC1Arguments(Diagnostics: &Diags, Compilation: Compilation.get()); |
| 379 | if (auto Err = ErrOrCC1Args.takeError()) |
| 380 | return std::move(Err); |
| 381 | |
| 382 | return CreateCI(Argv: **ErrOrCC1Args); |
| 383 | } |
| 384 | |
| 385 | llvm::Expected<std::unique_ptr<CompilerInstance>> |
| 386 | IncrementalCompilerBuilder::CreateCpp() { |
| 387 | std::string TT = TargetTriple ? *TargetTriple : llvm::sys::getProcessTriple(); |
| 388 | |
| 389 | std::vector<const char *> Argv; |
| 390 | Argv.reserve(n: 5 + 1 + UserArgs.size()); |
| 391 | Argv.push_back(x: "-xc++" ); |
| 392 | #ifdef __EMSCRIPTEN__ |
| 393 | Argv.push_back("-target" ); |
| 394 | Argv.push_back(TT.c_str()); |
| 395 | Argv.push_back("-fvisibility=default" ); |
| 396 | #endif |
| 397 | llvm::append_range(C&: Argv, R&: UserArgs); |
| 398 | |
| 399 | return IncrementalCompilerBuilder::create(TT, ClangArgv&: Argv); |
| 400 | } |
| 401 | |
| 402 | llvm::Expected<std::unique_ptr<CompilerInstance>> |
| 403 | IncrementalCompilerBuilder::createOffload(bool device, OffloadType Type) { |
| 404 | if (Type == OffloadType::Detect) |
| 405 | return llvm::make_error<llvm::StringError>( |
| 406 | Args: "Detecting the device vendor is not supported yet." , |
| 407 | Args: llvm::inconvertibleErrorCode()); |
| 408 | |
| 409 | constexpr size_t NumDriverArgs = 6; |
| 410 | |
| 411 | const bool HipEnabled = Type == OffloadType::HIP; |
| 412 | std::vector<const char *> Argv; |
| 413 | Argv.reserve(n: NumDriverArgs + UserArgs.size()); |
| 414 | Argv.push_back(x: HipEnabled ? "-xhip" : "-xcuda" ); |
| 415 | Argv.push_back(x: device ? "--cuda-device-only" : "--cuda-host-only" ); |
| 416 | |
| 417 | std::string SDKPathArg = HipEnabled ? "--rocm-path=" : "--cuda-path=" ; |
| 418 | if (!DeviceSDKPath.empty()) { |
| 419 | SDKPathArg += DeviceSDKPath; |
| 420 | Argv.push_back(x: SDKPathArg.c_str()); |
| 421 | } |
| 422 | |
| 423 | std::string ArchArg = "--offload-arch=" ; |
| 424 | if (!OffloadArch.empty()) { |
| 425 | ArchArg += OffloadArch; |
| 426 | Argv.push_back(x: ArchArg.c_str()); |
| 427 | } |
| 428 | |
| 429 | if (OffloadCUID.empty()) |
| 430 | OffloadCUID = llvm::utohexstr(X: llvm::sys::Process::GetRandomNumber(), |
| 431 | /*LowerCase=*/true); |
| 432 | std::string CUIDArg = "-cuid=" + OffloadCUID; |
| 433 | Argv.push_back(x: CUIDArg.c_str()); |
| 434 | |
| 435 | // Unoptimized HIP kernels request hostcall support, which needs PCIe atomics |
| 436 | // and fails to dispatch without them. A -O in UserArgs comes later and wins. |
| 437 | if (device && HipEnabled) |
| 438 | Argv.push_back(x: "-O3" ); |
| 439 | |
| 440 | llvm::append_range(C&: Argv, R&: UserArgs); |
| 441 | |
| 442 | std::string TT = TargetTriple ? *TargetTriple : llvm::sys::getProcessTriple(); |
| 443 | return IncrementalCompilerBuilder::create(TT, ClangArgv&: Argv); |
| 444 | } |
| 445 | |
| 446 | llvm::Expected<std::unique_ptr<CompilerInstance>> |
| 447 | IncrementalCompilerBuilder::CreateDevice(OffloadType Type) { |
| 448 | return IncrementalCompilerBuilder::createOffload(/*device=*/true, Type); |
| 449 | } |
| 450 | |
| 451 | llvm::Expected<std::unique_ptr<CompilerInstance>> |
| 452 | IncrementalCompilerBuilder::CreateHost(OffloadType Type) { |
| 453 | return IncrementalCompilerBuilder::createOffload(/*device=*/false, Type); |
| 454 | } |
| 455 | |
| 456 | Interpreter::Interpreter(std::unique_ptr<CompilerInstance> Instance, |
| 457 | llvm::Error &ErrOut, |
| 458 | std::unique_ptr<IncrementalExecutorBuilder> IEB, |
| 459 | std::unique_ptr<clang::ASTConsumer> Consumer) |
| 460 | : IncrExecutorBuilder(std::move(IEB)) { |
| 461 | CI = std::move(Instance); |
| 462 | llvm::ErrorAsOutParameter EAO(&ErrOut); |
| 463 | auto LLVMCtx = std::make_unique<llvm::LLVMContext>(); |
| 464 | TSCtx = std::make_unique<llvm::orc::ThreadSafeContext>(args: std::move(LLVMCtx)); |
| 465 | |
| 466 | Act = TSCtx->withContextDo(F: [&](llvm::LLVMContext *Ctx) { |
| 467 | return std::make_unique<IncrementalAction>(args&: *CI, args&: *Ctx, args&: ErrOut, args&: *this, |
| 468 | args: std::move(Consumer)); |
| 469 | }); |
| 470 | |
| 471 | if (ErrOut) |
| 472 | return; |
| 473 | |
| 474 | if (llvm::Error E = ExecuteIncrementalAction(CI&: *CI, Act&: *Act)) { |
| 475 | ErrOut = joinErrors(E1: std::move(ErrOut), E2: std::move(E)); |
| 476 | return; |
| 477 | } |
| 478 | |
| 479 | IncrParser = |
| 480 | std::make_unique<IncrementalParser>(args&: *CI, args: Act.get(), args&: ErrOut, args&: PTUs); |
| 481 | |
| 482 | if (ErrOut) |
| 483 | return; |
| 484 | |
| 485 | CI->getPreprocessor().AddPragmaHandler(Namespace: "clang" , Handler: new PragmaReplHandler(*this)); |
| 486 | |
| 487 | if (Act->getCodeGen()) { |
| 488 | Act->CacheCodeGenModule(); |
| 489 | // The initial PTU is filled by `-include`/`-include-pch` or by CUDA |
| 490 | // includes automatically. |
| 491 | if (!CI->getPreprocessorOpts().Includes.empty() || |
| 492 | !CI->getPreprocessorOpts().ImplicitPCHInclude.empty()) { |
| 493 | // We can't really directly pass the CachedInCodeGenModule to the Jit |
| 494 | // because it will steal it, causing dangling references as explained in |
| 495 | // Interpreter::Execute |
| 496 | auto M = llvm::CloneModule(M: *Act->getCachedCodeGenModule()); |
| 497 | ASTContext &C = CI->getASTContext(); |
| 498 | IncrParser->RegisterPTU(TU: C.getTranslationUnitDecl(), M: std::move(M)); |
| 499 | } |
| 500 | if (llvm::Error Err = CreateExecutor()) { |
| 501 | ErrOut = joinErrors(E1: std::move(ErrOut), E2: std::move(Err)); |
| 502 | return; |
| 503 | } |
| 504 | } |
| 505 | |
| 506 | // Not all frontends support code-generation, e.g. ast-dump actions don't |
| 507 | if (Act->getCodeGen()) { |
| 508 | // Process the PTUs that came from initialization. For example -include will |
| 509 | // give us a header that's processed at initialization of the preprocessor. |
| 510 | for (PartialTranslationUnit &PTU : PTUs) |
| 511 | if (llvm::Error Err = Execute(T&: PTU)) { |
| 512 | ErrOut = joinErrors(E1: std::move(ErrOut), E2: std::move(Err)); |
| 513 | return; |
| 514 | } |
| 515 | } |
| 516 | } |
| 517 | |
| 518 | Interpreter::~Interpreter() { |
| 519 | IncrParser.reset(); |
| 520 | Act->FinalizeAction(); |
| 521 | if (DeviceParser) |
| 522 | DeviceParser.reset(); |
| 523 | if (DeviceAct) |
| 524 | DeviceAct->FinalizeAction(); |
| 525 | if (IncrExecutor) { |
| 526 | if (llvm::Error Err = IncrExecutor->cleanUp()) |
| 527 | llvm::report_fatal_error( |
| 528 | reason: llvm::Twine("Failed to clean up IncrementalExecutor: " ) + |
| 529 | toString(E: std::move(Err))); |
| 530 | } |
| 531 | } |
| 532 | |
| 533 | // These better to put in a runtime header but we can't. This is because we |
| 534 | // can't find the precise resource directory in unittests so we have to hard |
| 535 | // code them. |
| 536 | const char *const Runtimes = R"( |
| 537 | #define __CLANG_REPL__ 1 |
| 538 | #ifdef __cplusplus |
| 539 | #define EXTERN_C extern "C" |
| 540 | struct __clang_Interpreter_NewTag{} __ci_newtag; |
| 541 | void* operator new(__SIZE_TYPE__, void* __p, __clang_Interpreter_NewTag) noexcept; |
| 542 | template <class T, class = T (*)() /*disable for arrays*/> |
| 543 | void __clang_Interpreter_SetValueCopyArr(const T* Src, void* Placement, unsigned long Size) { |
| 544 | for (unsigned long Idx = 0; Idx < Size; ++Idx) |
| 545 | new ((void*)(((T*)Placement) + Idx), __ci_newtag) T(Src[Idx]); |
| 546 | } |
| 547 | template <class T, unsigned long N> |
| 548 | void __clang_Interpreter_SetValueCopyArr(const T (*Src)[N], void* Placement, unsigned long Size) { |
| 549 | __clang_Interpreter_SetValueCopyArr(Src[0], Placement, Size); |
| 550 | } |
| 551 | #else |
| 552 | #if __STDC_VERSION__ < 199901L |
| 553 | #define CI_RESTRICT |
| 554 | #define CI_INLINE |
| 555 | #else |
| 556 | #define CI_RESTRICT restrict |
| 557 | #define CI_INLINE inline |
| 558 | #endif |
| 559 | #define EXTERN_C extern |
| 560 | EXTERN_C void *memcpy(void *CI_RESTRICT dst, const void *CI_RESTRICT src, __SIZE_TYPE__ n); |
| 561 | EXTERN_C CI_INLINE void __clang_Interpreter_SetValueCopyArr(const void* Src, void* Placement, unsigned long Size) { |
| 562 | memcpy(Placement, Src, Size); |
| 563 | } |
| 564 | #endif // __cplusplus |
| 565 | EXTERN_C void *__clang_Interpreter_SetValueWithAlloc(void*, void*, void*); |
| 566 | EXTERN_C void __clang_Interpreter_SetValueNoAlloc(void *This, void *OutVal, void *OpaqueType, ...); |
| 567 | )" ; |
| 568 | |
| 569 | llvm::Expected<std::unique_ptr<Interpreter>> Interpreter::create( |
| 570 | std::unique_ptr<CompilerInstance> CI, |
| 571 | std::unique_ptr<IncrementalExecutorBuilder> IEB /*=nullptr*/) { |
| 572 | llvm::Error Err = llvm::Error::success(); |
| 573 | |
| 574 | auto Interp = std::unique_ptr<Interpreter>(new Interpreter( |
| 575 | std::move(CI), Err, std::move(IEB), /*Consumer=*/nullptr)); |
| 576 | if (auto E = std::move(Err)) |
| 577 | return std::move(E); |
| 578 | |
| 579 | // Add runtime code and set a marker to hide it from user code. Undo will not |
| 580 | // go through that. |
| 581 | if (auto E = Interp->ParseAndExecute(Code: Runtimes)) |
| 582 | return std::move(E); |
| 583 | |
| 584 | Interp->markUserCodeStart(); |
| 585 | |
| 586 | return std::move(Interp); |
| 587 | } |
| 588 | |
| 589 | llvm::Expected<std::unique_ptr<Interpreter>> |
| 590 | Interpreter::createWithDevice(std::unique_ptr<CompilerInstance> CI, |
| 591 | std::unique_ptr<CompilerInstance> DCI, |
| 592 | OffloadType Type) { |
| 593 | if (Type == OffloadType::Detect) |
| 594 | return llvm::make_error<llvm::StringError>( |
| 595 | Args: "Detecting the device vendor is not supported yet." , |
| 596 | Args: llvm::inconvertibleErrorCode()); |
| 597 | |
| 598 | // avoid writing fat binary to disk using an in-memory virtual file system |
| 599 | llvm::IntrusiveRefCntPtr<llvm::vfs::InMemoryFileSystem> IMVFS = |
| 600 | std::make_unique<llvm::vfs::InMemoryFileSystem>(); |
| 601 | llvm::IntrusiveRefCntPtr<llvm::vfs::OverlayFileSystem> OverlayVFS = |
| 602 | std::make_unique<llvm::vfs::OverlayFileSystem>( |
| 603 | args: llvm::vfs::getRealFileSystem()); |
| 604 | OverlayVFS->pushOverlay(FS: IMVFS); |
| 605 | CI->createVirtualFileSystem(BaseFS: OverlayVFS); |
| 606 | CI->createFileManager(); |
| 607 | |
| 608 | llvm::Expected<std::unique_ptr<Interpreter>> InterpOrErr = |
| 609 | Interpreter::create(CI: std::move(CI)); |
| 610 | if (!InterpOrErr) |
| 611 | return InterpOrErr; |
| 612 | |
| 613 | std::unique_ptr<Interpreter> Interp = std::move(*InterpOrErr); |
| 614 | |
| 615 | llvm::Error Err = llvm::Error::success(); |
| 616 | |
| 617 | auto DeviceAct = Interp->TSCtx->withContextDo(F: [&](llvm::LLVMContext *Ctx) { |
| 618 | return std::make_unique<IncrementalAction>(args&: *DCI, args&: *Ctx, args&: Err, args&: *Interp); |
| 619 | }); |
| 620 | |
| 621 | if (Err) |
| 622 | return std::move(Err); |
| 623 | |
| 624 | Interp->DeviceAct = std::move(DeviceAct); |
| 625 | |
| 626 | if (llvm::Error E = ExecuteIncrementalAction(CI&: *DCI, Act&: *Interp->DeviceAct)) |
| 627 | return std::move(E); |
| 628 | |
| 629 | // Set the finalized initial device module aside, as the host path does. |
| 630 | Interp->DeviceAct->CacheCodeGenModule(); |
| 631 | |
| 632 | Interp->DeviceCI = std::move(DCI); |
| 633 | |
| 634 | std::unique_ptr<IncrementalDeviceParser> DeviceParser; |
| 635 | if (Type == OffloadType::HIP) |
| 636 | DeviceParser = std::make_unique<IncrementalHIPDeviceParser>( |
| 637 | args&: *Interp->DeviceCI, args&: *Interp->getCompilerInstance(), |
| 638 | args: Interp->DeviceAct.get(), args&: IMVFS, args&: Err, args&: Interp->PTUs); |
| 639 | else |
| 640 | DeviceParser = std::make_unique<IncrementalCUDADeviceParser>( |
| 641 | args&: *Interp->DeviceCI, args&: *Interp->getCompilerInstance(), |
| 642 | args: Interp->DeviceAct.get(), args&: IMVFS, args&: Err, args&: Interp->PTUs); |
| 643 | |
| 644 | if (Err) |
| 645 | return std::move(Err); |
| 646 | |
| 647 | Interp->DeviceParser = std::move(DeviceParser); |
| 648 | return std::move(Interp); |
| 649 | } |
| 650 | |
| 651 | CompilerInstance *Interpreter::getCompilerInstance() { return CI.get(); } |
| 652 | const CompilerInstance *Interpreter::getCompilerInstance() const { |
| 653 | return const_cast<Interpreter *>(this)->getCompilerInstance(); |
| 654 | } |
| 655 | |
| 656 | llvm::Expected<IncrementalExecutor &> Interpreter::getExecutionEngine() { |
| 657 | if (!IncrExecutor) { |
| 658 | if (auto Err = CreateExecutor()) |
| 659 | return std::move(Err); |
| 660 | } |
| 661 | |
| 662 | return *IncrExecutor.get(); |
| 663 | } |
| 664 | |
| 665 | void Interpreter::setOptLevel(unsigned OptLevel, unsigned OptSize) { |
| 666 | CodeGenOptions &CGO = getCompilerInstance()->getCodeGenOpts(); |
| 667 | CGO.OptimizationLevel = OptLevel; |
| 668 | CGO.OptimizeSize = OptSize; |
| 669 | CGO.setInlining(OptLevel ? CodeGenOptions::NormalInlining |
| 670 | : CodeGenOptions::OnlyAlwaysInlining); |
| 671 | } |
| 672 | |
| 673 | ASTContext &Interpreter::getASTContext() { |
| 674 | return getCompilerInstance()->getASTContext(); |
| 675 | } |
| 676 | |
| 677 | const ASTContext &Interpreter::getASTContext() const { |
| 678 | return getCompilerInstance()->getASTContext(); |
| 679 | } |
| 680 | |
| 681 | void Interpreter::markUserCodeStart() { |
| 682 | assert(!InitPTUSize && "We only do this once" ); |
| 683 | InitPTUSize = PTUs.size(); |
| 684 | } |
| 685 | |
| 686 | size_t Interpreter::getEffectivePTUSize() const { |
| 687 | assert(PTUs.size() >= InitPTUSize && "empty PTU list?" ); |
| 688 | return PTUs.size() - InitPTUSize; |
| 689 | } |
| 690 | |
| 691 | llvm::Expected<PartialTranslationUnit &> |
| 692 | Interpreter::Parse(llvm::StringRef Code) { |
| 693 | // If we have a device parser, parse it first. The generated code will be |
| 694 | // included in the host compilation |
| 695 | if (DeviceParser) { |
| 696 | llvm::Expected<TranslationUnitDecl *> DeviceTU = DeviceParser->Parse(Input: Code); |
| 697 | if (auto E = DeviceTU.takeError()) |
| 698 | return std::move(E); |
| 699 | |
| 700 | PartialTranslationUnit &DevicePTU = DeviceParser->RegisterPTU(TU: *DeviceTU); |
| 701 | |
| 702 | // Under -emit-llvm, print the device module IR before it is lowered. |
| 703 | if (DevicePTU.TheModule && |
| 704 | DeviceCI->getFrontendOpts().ProgramAction == frontend::EmitLLVM) |
| 705 | DevicePTU.TheModule->print(OS&: llvm::outs(), /*AAW=*/nullptr); |
| 706 | |
| 707 | llvm::Error Err = DeviceParser->GenerateOffloadBinary(); |
| 708 | if (Err) |
| 709 | return std::move(Err); |
| 710 | } |
| 711 | |
| 712 | // Tell the interpreter sliently ignore unused expressions since value |
| 713 | // printing could cause it. |
| 714 | getCompilerInstance()->getDiagnostics().setSeverity( |
| 715 | Diag: clang::diag::warn_unused_expr, Map: diag::Severity::Ignored, Loc: SourceLocation()); |
| 716 | |
| 717 | llvm::Expected<TranslationUnitDecl *> TuOrErr = IncrParser->Parse(Input: Code); |
| 718 | if (!TuOrErr) |
| 719 | return TuOrErr.takeError(); |
| 720 | |
| 721 | PartialTranslationUnit &LastPTU = IncrParser->RegisterPTU(TU: *TuOrErr); |
| 722 | |
| 723 | // Under -emit-llvm, print the module IR. |
| 724 | if (InitPTUSize && LastPTU.TheModule && |
| 725 | getCompilerInstance()->getFrontendOpts().ProgramAction == |
| 726 | frontend::EmitLLVM) |
| 727 | LastPTU.TheModule->print(OS&: llvm::outs(), /*AAW=*/nullptr); |
| 728 | |
| 729 | return LastPTU; |
| 730 | } |
| 731 | |
| 732 | llvm::Error Interpreter::CreateExecutor() { |
| 733 | if (IncrExecutor) |
| 734 | return llvm::make_error<llvm::StringError>(Args: "Operation failed. " |
| 735 | "Execution engine exists" , |
| 736 | Args: std::error_code()); |
| 737 | if (!Act->getCodeGen()) |
| 738 | return llvm::make_error<llvm::StringError>(Args: "Operation failed. " |
| 739 | "No code generator available" , |
| 740 | Args: std::error_code()); |
| 741 | |
| 742 | if (!IncrExecutorBuilder) |
| 743 | IncrExecutorBuilder = IncrementalExecutorBuilder::createDefault(); |
| 744 | |
| 745 | auto ExecutorOrErr = IncrExecutorBuilder->create(TSC&: *TSCtx, TI: CI->getTarget()); |
| 746 | if (ExecutorOrErr) |
| 747 | IncrExecutor = std::move(*ExecutorOrErr); |
| 748 | |
| 749 | return ExecutorOrErr.takeError(); |
| 750 | } |
| 751 | |
| 752 | llvm::Error Interpreter::Execute(PartialTranslationUnit &T) { |
| 753 | assert(T.TheModule); |
| 754 | LLVM_DEBUG( |
| 755 | llvm::dbgs() << "execute-ptu " |
| 756 | << (llvm::is_contained(PTUs, T) |
| 757 | ? std::distance(PTUs.begin(), llvm::find(PTUs, T)) |
| 758 | : -1) |
| 759 | << ": [TU=" << T.TUPart << ", M=" << T.TheModule.get() |
| 760 | << " (" << T.TheModule->getName() << ")]\n" ); |
| 761 | if (!IncrExecutor) { |
| 762 | auto Err = CreateExecutor(); |
| 763 | if (Err) |
| 764 | return Err; |
| 765 | } |
| 766 | // FIXME: Add a callback to retain the llvm::Module once the JIT is done. |
| 767 | if (auto Err = IncrExecutor->addModule(PTU&: T)) |
| 768 | return Err; |
| 769 | |
| 770 | if (auto Err = IncrExecutor->runCtors()) |
| 771 | return Err; |
| 772 | |
| 773 | return llvm::Error::success(); |
| 774 | } |
| 775 | |
| 776 | llvm::Error Interpreter::ParseAndExecute(llvm::StringRef Code, Value *V) { |
| 777 | |
| 778 | auto PTU = Parse(Code); |
| 779 | if (!PTU) |
| 780 | return PTU.takeError(); |
| 781 | if (PTU->TheModule) |
| 782 | if (llvm::Error Err = Execute(T&: *PTU)) |
| 783 | return Err; |
| 784 | |
| 785 | if (LastValue.isValid()) { |
| 786 | if (!V) { |
| 787 | LastValue.dump(); |
| 788 | LastValue.clear(); |
| 789 | } else |
| 790 | *V = std::move(LastValue); |
| 791 | } |
| 792 | return llvm::Error::success(); |
| 793 | } |
| 794 | |
| 795 | llvm::Expected<llvm::orc::ExecutorAddr> |
| 796 | Interpreter::getSymbolAddress(GlobalDecl GD) const { |
| 797 | if (!IncrExecutor) |
| 798 | return llvm::make_error<llvm::StringError>(Args: "Operation failed. " |
| 799 | "No execution engine" , |
| 800 | Args: std::error_code()); |
| 801 | llvm::StringRef MangledName = Act->getCodeGen()->GetMangledName(GD); |
| 802 | return getSymbolAddress(IRName: MangledName); |
| 803 | } |
| 804 | |
| 805 | llvm::Expected<llvm::orc::ExecutorAddr> |
| 806 | Interpreter::getSymbolAddress(llvm::StringRef IRName) const { |
| 807 | if (!IncrExecutor) |
| 808 | return llvm::make_error<llvm::StringError>(Args: "Operation failed. " |
| 809 | "No execution engine" , |
| 810 | Args: std::error_code()); |
| 811 | |
| 812 | return IncrExecutor->getSymbolAddress(Name: IRName, NameKind: IncrementalExecutor::IRName); |
| 813 | } |
| 814 | |
| 815 | llvm::Expected<llvm::orc::ExecutorAddr> |
| 816 | Interpreter::getSymbolAddressFromLinkerName(llvm::StringRef Name) const { |
| 817 | if (!IncrExecutor) |
| 818 | return llvm::make_error<llvm::StringError>(Args: "Operation failed. " |
| 819 | "No execution engine" , |
| 820 | Args: std::error_code()); |
| 821 | |
| 822 | return IncrExecutor->getSymbolAddress(Name, NameKind: IncrementalExecutor::LinkerName); |
| 823 | } |
| 824 | |
| 825 | llvm::Error Interpreter::Undo(unsigned N) { |
| 826 | |
| 827 | if (getEffectivePTUSize() == 0) { |
| 828 | return llvm::make_error<llvm::StringError>(Args: "Operation failed. " |
| 829 | "No input left to undo" , |
| 830 | Args: std::error_code()); |
| 831 | } else if (N > getEffectivePTUSize()) { |
| 832 | return llvm::make_error<llvm::StringError>( |
| 833 | Args: llvm::formatv( |
| 834 | Fmt: "Operation failed. Wanted to undo {0} inputs, only have {1}." , Vals&: N, |
| 835 | Vals: getEffectivePTUSize()), |
| 836 | Args: std::error_code()); |
| 837 | } |
| 838 | |
| 839 | for (unsigned I = 0; I < N; I++) { |
| 840 | if (IncrExecutor) { |
| 841 | if (llvm::Error Err = IncrExecutor->removeModule(PTU&: PTUs.back())) |
| 842 | return Err; |
| 843 | } |
| 844 | |
| 845 | IncrParser->CleanUpPTU(MostRecentTU: PTUs.back().TUPart); |
| 846 | PTUs.pop_back(); |
| 847 | } |
| 848 | return llvm::Error::success(); |
| 849 | } |
| 850 | |
| 851 | llvm::Error Interpreter::LoadDynamicLibrary(const char *name) { |
| 852 | auto EEOrErr = getExecutionEngine(); |
| 853 | if (!EEOrErr) |
| 854 | return EEOrErr.takeError(); |
| 855 | |
| 856 | return EEOrErr->LoadDynamicLibrary(name); |
| 857 | } |
| 858 | } // end namespace clang |
| 859 | |