Coverage Report

Created: 2023-09-30 09:22

/Users/buildslave/jenkins/workspace/coverage/llvm-project/clang/lib/Interpreter/IncrementalExecutor.cpp
Line
Count
Source (jump to first uncovered line)
1
//===--- IncrementalExecutor.cpp - Incremental Execution --------*- C++ -*-===//
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 class which performs incremental code execution.
10
//
11
//===----------------------------------------------------------------------===//
12
13
#include "IncrementalExecutor.h"
14
15
#include "clang/Basic/TargetInfo.h"
16
#include "clang/Basic/TargetOptions.h"
17
#include "clang/Interpreter/PartialTranslationUnit.h"
18
#include "llvm/ExecutionEngine/ExecutionEngine.h"
19
#include "llvm/ExecutionEngine/Orc/CompileUtils.h"
20
#include "llvm/ExecutionEngine/Orc/Debugging/DebuggerSupport.h"
21
#include "llvm/ExecutionEngine/Orc/ExecutionUtils.h"
22
#include "llvm/ExecutionEngine/Orc/IRCompileLayer.h"
23
#include "llvm/ExecutionEngine/Orc/LLJIT.h"
24
#include "llvm/ExecutionEngine/Orc/RTDyldObjectLinkingLayer.h"
25
#include "llvm/ExecutionEngine/Orc/TargetProcess/JITLoaderGDB.h"
26
#include "llvm/ExecutionEngine/SectionMemoryManager.h"
27
#include "llvm/IR/Module.h"
28
#include "llvm/Support/ManagedStatic.h"
29
#include "llvm/Support/TargetSelect.h"
30
31
// Force linking some of the runtimes that helps attaching to a debugger.
32
0
LLVM_ATTRIBUTE_USED void linkComponents() {
33
0
  llvm::errs() << (void *)&llvm_orc_registerJITLoaderGDBWrapper
34
0
               << (void *)&llvm_orc_registerJITLoaderGDBAllocAction;
35
0
}
36
37
namespace clang {
38
39
IncrementalExecutor::IncrementalExecutor(llvm::orc::ThreadSafeContext &TSC,
40
                                         llvm::Error &Err,
41
                                         const clang::TargetInfo &TI)
42
29
    : TSCtx(TSC) {
43
29
  using namespace llvm::orc;
44
29
  llvm::ErrorAsOutParameter EAO(&Err);
45
46
29
  auto JTMB = JITTargetMachineBuilder(TI.getTriple());
47
29
  JTMB.addFeatures(TI.getTargetOpts().Features);
48
29
  LLJITBuilder Builder;
49
29
  Builder.setJITTargetMachineBuilder(JTMB);
50
29
  Builder.setPrePlatformSetup(
51
29
      [](LLJIT &J) {
52
        // Try to enable debugging of JIT'd code (only works with JITLink for
53
        // ELF and MachO).
54
29
        consumeError(enableDebuggerSupport(J));
55
29
        return llvm::Error::success();
56
29
      });
57
58
29
  if (auto JitOrErr = Builder.create())
59
29
    Jit = std::move(*JitOrErr);
60
0
  else {
61
0
    Err = JitOrErr.takeError();
62
0
    return;
63
0
  }
64
29
}
65
66
22
IncrementalExecutor::~IncrementalExecutor() {}
67
68
289
llvm::Error IncrementalExecutor::addModule(PartialTranslationUnit &PTU) {
69
289
  llvm::orc::ResourceTrackerSP RT =
70
289
      Jit->getMainJITDylib().createResourceTracker();
71
289
  ResourceTrackers[&PTU] = RT;
72
73
289
  return Jit->addIRModule(RT, {std::move(PTU.TheModule), TSCtx});
74
289
}
75
76
3
llvm::Error IncrementalExecutor::removeModule(PartialTranslationUnit &PTU) {
77
78
3
  llvm::orc::ResourceTrackerSP RT = std::move(ResourceTrackers[&PTU]);
79
3
  if (!RT)
80
0
    return llvm::Error::success();
81
82
3
  ResourceTrackers.erase(&PTU);
83
3
  if (llvm::Error Err = RT->remove())
84
0
    return Err;
85
3
  return llvm::Error::success();
86
3
}
87
88
// Clean up the JIT instance.
89
22
llvm::Error IncrementalExecutor::cleanUp() {
90
  // This calls the global dtors of registered modules.
91
22
  return Jit->deinitialize(Jit->getMainJITDylib());
92
22
}
93
94
289
llvm::Error IncrementalExecutor::runCtors() const {
95
289
  return Jit->initialize(Jit->getMainJITDylib());
96
289
}
97
98
llvm::Expected<llvm::orc::ExecutorAddr>
99
IncrementalExecutor::getSymbolAddress(llvm::StringRef Name,
100
6
                                      SymbolNameKind NameKind) const {
101
6
  using namespace llvm::orc;
102
6
  auto SO = makeJITDylibSearchOrder({&Jit->getMainJITDylib(),
103
6
                                     Jit->getPlatformJITDylib().get(),
104
6
                                     Jit->getProcessSymbolsJITDylib().get()});
105
106
6
  ExecutionSession &ES = Jit->getExecutionSession();
107
108
6
  auto SymOrErr =
109
6
      ES.lookup(SO, (NameKind == LinkerName) ? 
ES.intern(Name)0
110
6
                                             : Jit->mangleAndIntern(Name));
111
6
  if (auto Err = SymOrErr.takeError())
112
0
    return std::move(Err);
113
6
  return SymOrErr->getAddress();
114
6
}
115
116
} // end namespace clang