1195098Sed//===- JITEventListener.h - Exposes events from JIT compilation -*- C++ -*-===//
2195098Sed//
3353358Sdim// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4353358Sdim// See https://llvm.org/LICENSE.txt for license information.
5353358Sdim// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6195098Sed//
7195098Sed//===----------------------------------------------------------------------===//
8195098Sed//
9195098Sed// This file defines the JITEventListener interface, which lets users get
10195098Sed// callbacks when significant events happen during the JIT compilation process.
11195098Sed//
12195098Sed//===----------------------------------------------------------------------===//
13195098Sed
14249423Sdim#ifndef LLVM_EXECUTIONENGINE_JITEVENTLISTENER_H
15249423Sdim#define LLVM_EXECUTIONENGINE_JITEVENTLISTENER_H
16195098Sed
17341825Sdim#include "llvm-c/ExecutionEngine.h"
18249423Sdim#include "llvm/Config/llvm-config.h"
19321369Sdim#include "llvm/ExecutionEngine/RuntimeDyld.h"
20276479Sdim#include "llvm/IR/DebugLoc.h"
21341825Sdim#include "llvm/Support/CBindingWrapping.h"
22314564Sdim#include <cstdint>
23198090Srdivacky#include <vector>
24198090Srdivacky
25195098Sednamespace llvm {
26314564Sdim
27314564Sdimclass IntelJITEventsWrapper;
28198090Srdivackyclass MachineFunction;
29234353Sdimclass OProfileWrapper;
30195098Sed
31280031Sdimnamespace object {
32321369Sdim
33321369Sdimclass ObjectFile;
34321369Sdim
35314564Sdim} // end namespace object
36280031Sdim
37218893Sdim/// JITEventListener - Abstract interface for use by the JIT to notify clients
38218893Sdim/// about significant events during compilation. For example, to notify
39218893Sdim/// profilers and debuggers that need to know where functions have been emitted.
40195098Sed///
41218893Sdim/// The default implementation of each method does nothing.
42195098Sedclass JITEventListener {
43195098Sedpublic:
44344779Sdim  using ObjectKey = uint64_t;
45218893Sdim
46314564Sdim  JITEventListener() = default;
47314564Sdim  virtual ~JITEventListener() = default;
48195098Sed
49344779Sdim  /// notifyObjectLoaded - Called after an object has had its sections allocated
50344779Sdim  /// and addresses assigned to all symbols. Note: Section memory will not have
51344779Sdim  /// been relocated yet. notifyFunctionLoaded will not be called for
52243830Sdim  /// individual functions in the object.
53243830Sdim  ///
54243830Sdim  /// ELF-specific information
55243830Sdim  /// The ObjectImage contains the generated object image
56243830Sdim  /// with section headers updated to reflect the address at which sections
57243830Sdim  /// were loaded and with relocations performed in-place on debug sections.
58344779Sdim  virtual void notifyObjectLoaded(ObjectKey K, const object::ObjectFile &Obj,
59344779Sdim                                  const RuntimeDyld::LoadedObjectInfo &L) {}
60243830Sdim
61344779Sdim  /// notifyFreeingObject - Called just before the memory associated with
62243830Sdim  /// a previously emitted object is released.
63344779Sdim  virtual void notifyFreeingObject(ObjectKey K) {}
64243830Sdim
65280031Sdim  // Get a pointe to the GDB debugger registration listener.
66280031Sdim  static JITEventListener *createGDBRegistrationListener();
67280031Sdim
68314564Sdim#if LLVM_USE_INTEL_JITEVENTS
69234353Sdim  // Construct an IntelJITEventListener
70234353Sdim  static JITEventListener *createIntelJITEventListener();
71234353Sdim
72234353Sdim  // Construct an IntelJITEventListener with a test Intel JIT API implementation
73234353Sdim  static JITEventListener *createIntelJITEventListener(
74234353Sdim                                      IntelJITEventsWrapper* AlternativeImpl);
75234353Sdim#else
76276479Sdim  static JITEventListener *createIntelJITEventListener() { return nullptr; }
77234353Sdim
78234353Sdim  static JITEventListener *createIntelJITEventListener(
79234353Sdim                                      IntelJITEventsWrapper* AlternativeImpl) {
80276479Sdim    return nullptr;
81234353Sdim  }
82234353Sdim#endif // USE_INTEL_JITEVENTS
83234353Sdim
84314564Sdim#if LLVM_USE_OPROFILE
85234353Sdim  // Construct an OProfileJITEventListener
86234353Sdim  static JITEventListener *createOProfileJITEventListener();
87234353Sdim
88234353Sdim  // Construct an OProfileJITEventListener with a test opagent implementation
89234353Sdim  static JITEventListener *createOProfileJITEventListener(
90234353Sdim                                      OProfileWrapper* AlternativeImpl);
91234353Sdim#else
92276479Sdim  static JITEventListener *createOProfileJITEventListener() { return nullptr; }
93234353Sdim
94234353Sdim  static JITEventListener *createOProfileJITEventListener(
95234353Sdim                                      OProfileWrapper* AlternativeImpl) {
96276479Sdim    return nullptr;
97234353Sdim  }
98234353Sdim#endif // USE_OPROFILE
99314564Sdim
100341825Sdim#if LLVM_USE_PERF
101341825Sdim  static JITEventListener *createPerfJITEventListener();
102341825Sdim#else
103341825Sdim  static JITEventListener *createPerfJITEventListener()
104341825Sdim  {
105341825Sdim    return nullptr;
106341825Sdim  }
107341825Sdim#endif // USE_PERF
108341825Sdim
109280031Sdimprivate:
110280031Sdim  virtual void anchor();
111195098Sed};
112195098Sed
113341825SdimDEFINE_SIMPLE_CONVERSION_FUNCTIONS(JITEventListener, LLVMJITEventListenerRef)
114341825Sdim
115314564Sdim} // end namespace llvm
116195098Sed
117314564Sdim#endif // LLVM_EXECUTIONENGINE_JITEVENTLISTENER_H
118