[clang] [lld] [llvm] [RFC] Initial reference pass-plugin in LLVM (PR #171111)
via llvm-commits
llvm-commits at lists.llvm.org
Mon Dec 8 05:04:02 PST 2025
================
@@ -0,0 +1,464 @@
+//===- tools/plugins-shlib/pypass.cpp -------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#include "llvm/ADT/ScopeExit.h"
+#include "llvm/Passes/PassBuilder.h"
+#include "llvm/Passes/PassPlugin.h"
+#include "llvm/Support/DynamicLibrary.h"
+#include "llvm/Support/raw_ostream.h"
+
+#include <algorithm>
+#include <cstdlib>
+#include <filesystem>
+#include <memory>
+#include <optional>
+#include <string>
+
+using namespace llvm;
+
+static cl::opt<std::string>
+ DylibPath("pypass-dylib", cl::desc("Path to the Python shared library"),
+ cl::init(""));
+
+static cl::opt<std::string>
+ ScriptPath("pypass-script", cl::desc("Path to the Python script to run"),
+ cl::init(""));
+
+static std::string findPython() {
+ if (!DylibPath.empty())
+ return DylibPath;
+ if (const char *Path = std::getenv("LLVM_PYPASS_DYLIB"))
+ return std::string(Path);
+ // TODO: Run Python from PATH and use a script to query the shared lib
+ return std::string{};
+}
+
+static std::string findScript() {
+ if (!ScriptPath.empty())
+ return ScriptPath;
+ if (const char *Path = std::getenv("LLVM_PYPASS_SCRIPT"))
+ return std::string(Path);
+ return std::string{};
+}
+
+struct PythonAPI {
+ using Py_InitializeEx_t = void(int);
+ using Py_Initialize_t = void(void);
+ using Py_FinalizeEx_t = int(void);
+ using Py_Finalize_t = void(void);
+ using PyDict_GetItemString_t = void *(void *, const char *);
+ using PyGILStateEnsure_t = int();
+ using PyGILStateRelease_t = void(int);
+ using PyImport_AddModule_t = void *(const char *);
+ using PyLong_FromVoidPtr_t = void *(void *);
+ using PyUnicode_FromString_t = void *(const char *);
+ using PyModule_GetDict_t = void *(void *);
+ using PyObject_CallObject_t = void *(void *, void *);
+ using PyObject_IsTrue_t = int(void *);
+ using PyRun_SimpleString_t = int(const char *);
+ using PyTuple_SetItem_t = int(void *, long, void *);
+ using PyTuple_New_t = void *(long);
+ using PyTypeObject_t = void *;
+
+ // pylifecycle.h
+ Py_InitializeEx_t *Py_InitializeEx;
+ Py_Initialize_t *Py_Initialize;
+ Py_FinalizeEx_t *Py_FinalizeEx;
+ Py_Finalize_t *Py_Finalize;
+
+ // pythonrun.h
+ PyRun_SimpleString_t *PyRun_SimpleString;
+
+ // pystate.h
+ PyGILStateEnsure_t *PyGILState_Ensure;
+ PyGILStateRelease_t *PyGILState_Release;
+
+ // import.h
+ PyImport_AddModule_t *PyImport_AddModule;
+
+ // object.h
+ PyObject_IsTrue_t *PyObject_IsTrue;
+
+ // moduleobject.h
+ PyModule_GetDict_t *PyModule_GetDict;
+
+ // dictobject.h
+ PyDict_GetItemString_t *PyDict_GetItemString;
+
+ // abstract.h
+ PyObject_CallObject_t *PyObject_CallObject;
+
+ // longobject.h
+ PyLong_FromVoidPtr_t *PyLong_FromVoidPtr;
+
+ // unicodeobject.h
+ PyUnicode_FromString_t *PyUnicode_FromString;
+
+ // tupleobject.h
+ PyTuple_SetItem_t *PyTuple_SetItem;
+ PyTuple_New_t *PyTuple_New;
+
+ PythonAPI() : Ready(false) {
+ if (!loadDylib(findPython()))
+ return;
+ if (!resolveSymbols())
+ return;
+ if (Py_InitializeEx) {
----------------
serge-sans-paille wrote:
Can this happen? It's part of the stable API, and `resolve_symbols` would return `False` if this fails, right?
https://github.com/llvm/llvm-project/pull/171111
More information about the llvm-commits
mailing list