|
| 1 | +#ifndef TRITON_PLUGIN_UTILS_H |
| 2 | +#define TRITON_PLUGIN_UTILS_H |
| 3 | + |
| 4 | +#include "mlir/Pass/PassManager.h" |
| 5 | +#include "llvm/Support/DynamicLibrary.h" |
| 6 | +#include "llvm/Support/Error.h" |
| 7 | +#include <cstdint> |
| 8 | + |
| 9 | +extern "C" { |
| 10 | +enum TritonPluginResult { |
| 11 | + TP_SUCCESS = 0, |
| 12 | + TP_GENERIC_FAILURE = 1, |
| 13 | +}; |
| 14 | +}; |
| 15 | + |
| 16 | +#if defined(_WIN32) |
| 17 | +#define EXPORT_FUNC __declspec(dllexport) |
| 18 | +#else |
| 19 | +#define EXPORT_FUNC __attribute__((visibility("default"))) |
| 20 | +#endif |
| 21 | + |
| 22 | +#define TRITON_PLUGIN_API extern "C" EXPORT_FUNC TritonPluginResult |
| 23 | + |
| 24 | +struct TritonPlugin { |
| 25 | + TritonPlugin() = delete; |
| 26 | + TritonPlugin(std::string filename) : filename(filename) {} |
| 27 | + |
| 28 | +private: |
| 29 | + using enumeratePyBindHandlesType = |
| 30 | + std::function<TritonPluginResult(uint32_t *, const char **)>; |
| 31 | + using enumeratePyBindHandlesCType = TritonPluginResult (*)(uint32_t *, |
| 32 | + const char **); |
| 33 | + |
| 34 | + // Put enumerate API names here, these can be involved with |
| 35 | + // enumeratePyBindHandles |
| 36 | + const std::string ENUMERATE_PASSES = "tritonEnumeratePluginPasses"; |
| 37 | + |
| 38 | + const std::string ADD_PASS = "tritonAddPluginPass"; |
| 39 | + using addPassType = |
| 40 | + std::function<TritonPluginResult(mlir::PassManager *, const char *)>; |
| 41 | + using addPassCType = TritonPluginResult (*)(mlir::PassManager *, |
| 42 | + const char *); |
| 43 | + |
| 44 | + const std::string REGISTER_PASS = "tritonRegisterPluginPass"; |
| 45 | + using registerPassType = std::function<TritonPluginResult(const char *)>; |
| 46 | + using registerPassCType = TritonPluginResult (*)(const char *); |
| 47 | + |
| 48 | + llvm::Error checkLibraryValid(const std::string &error) const; |
| 49 | + |
| 50 | + llvm::Expected<intptr_t> getAddressOfSymbol(const std::string &symbol) const; |
| 51 | + |
| 52 | + template <typename T, typename U> |
| 53 | + llvm::Expected<T> getAPI(const std::string &symbol) const { |
| 54 | + llvm::Expected<intptr_t> getDetailsFn = getAddressOfSymbol(symbol); |
| 55 | + if (auto Err = getDetailsFn.takeError()) { |
| 56 | + return Err; |
| 57 | + } |
| 58 | + auto func = reinterpret_cast<U>(*getDetailsFn); |
| 59 | + return func; |
| 60 | + } |
| 61 | + |
| 62 | + llvm::Expected<TritonPluginResult> checkAPIResult(TritonPluginResult result, |
| 63 | + const char *handle) const; |
| 64 | + llvm::Expected<TritonPluginResult> |
| 65 | + enumeratePyBindHandles(enumeratePyBindHandlesType &enumeratePyBindHandles, |
| 66 | + std::vector<const char *> &passNames); |
| 67 | + |
| 68 | +public: |
| 69 | + std::runtime_error err2exp(llvm::Error Err); |
| 70 | + |
| 71 | + llvm::Error loadPlugin(); |
| 72 | + |
| 73 | + llvm::Expected<TritonPluginResult> |
| 74 | + getPassHandles(std::vector<const char *> &handles); |
| 75 | + |
| 76 | + llvm::Expected<TritonPluginResult> addPass(mlir::PassManager *pm, |
| 77 | + const char *passHandle); |
| 78 | + |
| 79 | + llvm::Expected<TritonPluginResult> registerPass(const char *passHandle); |
| 80 | + |
| 81 | +private: |
| 82 | + std::string filename = ""; |
| 83 | + mutable llvm::sys::DynamicLibrary library; |
| 84 | + enumeratePyBindHandlesType enumeratePassesAPI; |
| 85 | + addPassType addPassAPI; |
| 86 | + registerPassType registerPassAPI; |
| 87 | + bool isLoaded = false; |
| 88 | +}; |
| 89 | + |
| 90 | +#endif // TRITON_PLUGIN_UTILS_H |
0 commit comments