PassPlugin.h 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. #pragma once
  2. #ifdef __GNUC__
  3. #pragma GCC diagnostic push
  4. #pragma GCC diagnostic ignored "-Wunused-parameter"
  5. #endif
  6. //===- llvm/Passes/PassPlugin.h - Public Plugin API -----------------------===//
  7. //
  8. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  9. // See https://llvm.org/LICENSE.txt for license information.
  10. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  11. //
  12. //===----------------------------------------------------------------------===//
  13. //
  14. // This defines the public entry point for new-PM pass plugins.
  15. //
  16. //===----------------------------------------------------------------------===//
  17. #ifndef LLVM_PASSES_PASSPLUGIN_H
  18. #define LLVM_PASSES_PASSPLUGIN_H
  19. #include "llvm/ADT/StringRef.h"
  20. #include "llvm/Support/Compiler.h"
  21. #include "llvm/Support/DynamicLibrary.h"
  22. #include "llvm/Support/Error.h"
  23. #include <cstdint>
  24. #include <string>
  25. namespace llvm {
  26. class PassBuilder;
  27. /// \macro LLVM_PLUGIN_API_VERSION
  28. /// Identifies the API version understood by this plugin.
  29. ///
  30. /// When a plugin is loaded, the driver will check it's supported plugin version
  31. /// against that of the plugin. A mismatch is an error. The supported version
  32. /// will be incremented for ABI-breaking changes to the \c PassPluginLibraryInfo
  33. /// struct, i.e. when callbacks are added, removed, or reordered.
  34. #define LLVM_PLUGIN_API_VERSION 1
  35. extern "C" {
  36. /// Information about the plugin required to load its passes
  37. ///
  38. /// This struct defines the core interface for pass plugins and is supposed to
  39. /// be filled out by plugin implementors. LLVM-side users of a plugin are
  40. /// expected to use the \c PassPlugin class below to interface with it.
  41. struct PassPluginLibraryInfo {
  42. /// The API version understood by this plugin, usually \c
  43. /// LLVM_PLUGIN_API_VERSION
  44. uint32_t APIVersion;
  45. /// A meaningful name of the plugin.
  46. const char *PluginName;
  47. /// The version of the plugin.
  48. const char *PluginVersion;
  49. /// The callback for registering plugin passes with a \c PassBuilder
  50. /// instance
  51. void (*RegisterPassBuilderCallbacks)(PassBuilder &);
  52. };
  53. }
  54. /// A loaded pass plugin.
  55. ///
  56. /// An instance of this class wraps a loaded pass plugin and gives access to
  57. /// its interface defined by the \c PassPluginLibraryInfo it exposes.
  58. class PassPlugin {
  59. public:
  60. /// Attempts to load a pass plugin from a given file.
  61. ///
  62. /// \returns Returns an error if either the library cannot be found or loaded,
  63. /// there is no public entry point, or the plugin implements the wrong API
  64. /// version.
  65. static Expected<PassPlugin> Load(const std::string &Filename);
  66. /// Get the filename of the loaded plugin.
  67. StringRef getFilename() const { return Filename; }
  68. /// Get the plugin name
  69. StringRef getPluginName() const { return Info.PluginName; }
  70. /// Get the plugin version
  71. StringRef getPluginVersion() const { return Info.PluginVersion; }
  72. /// Get the plugin API version
  73. uint32_t getAPIVersion() const { return Info.APIVersion; }
  74. /// Invoke the PassBuilder callback registration
  75. void registerPassBuilderCallbacks(PassBuilder &PB) const {
  76. Info.RegisterPassBuilderCallbacks(PB);
  77. }
  78. private:
  79. PassPlugin(const std::string &Filename, const sys::DynamicLibrary &Library)
  80. : Filename(Filename), Library(Library), Info() {}
  81. std::string Filename;
  82. sys::DynamicLibrary Library;
  83. PassPluginLibraryInfo Info;
  84. };
  85. }
  86. /// The public entry point for a pass plugin.
  87. ///
  88. /// When a plugin is loaded by the driver, it will call this entry point to
  89. /// obtain information about this plugin and about how to register its passes.
  90. /// This function needs to be implemented by the plugin, see the example below:
  91. ///
  92. /// ```
  93. /// extern "C" ::llvm::PassPluginLibraryInfo LLVM_ATTRIBUTE_WEAK
  94. /// llvmGetPassPluginInfo() {
  95. /// return {
  96. /// LLVM_PLUGIN_API_VERSION, "MyPlugin", "v0.1", [](PassBuilder &PB) { ... }
  97. /// };
  98. /// }
  99. /// ```
  100. extern "C" ::llvm::PassPluginLibraryInfo LLVM_ATTRIBUTE_WEAK
  101. llvmGetPassPluginInfo();
  102. #endif /* LLVM_PASSES_PASSPLUGIN_H */
  103. #ifdef __GNUC__
  104. #pragma GCC diagnostic pop
  105. #endif