Skip to content

Commit 49fb9e4

Browse files
committed
How cppyy relates to CppInterOp and how it works
- Reducing cppyy Dependencies - Code Example - cppyy components - cppyy repos (enhanced forks+upstream) - Credits to Devs - Further Reading Links New commit: - Added some changes after David's Review (see comment responses)
1 parent 1fd3ae4 commit 49fb9e4

File tree

1 file changed

+224
-16
lines changed

1 file changed

+224
-16
lines changed

Diff for: docs/UsingCppInterOp.rst

+224-16
Original file line numberDiff line numberDiff line change
@@ -65,35 +65,243 @@ robust (simple function calls, no inheritance, etc.). The goal is to make it as
6565
close to the compiler API as possible, and each routine should do just one thing.
6666
that it was designed for.
6767

68-
Further Enhancing the Dynamic/Automatic bindings in CPPYY
69-
=========================================================
70-
The main use case for CppInterOp is the CPPYY service. CPPYY is an
71-
automatic run-time bindings generator for Python and C++, and supports a wide
72-
range of C++ features (e.g., template instantiation). It operates on demand and
68+
How cppyy relates to CppInterOp and how it works
69+
================================================
70+
71+
CppInterOp ties into several `enhancements in cppyy`_, following are a few:
72+
73+
1. **CppInterOp is a cppyy-inspired Library** that enables interoperability
74+
with C++ code, bringing the speed and efficiency of C++ to simpler, more
75+
interactive languages like Python.
76+
77+
2. **Reducing dependencies** of cppyy to minimize code bloat and make it
78+
faster.
79+
80+
3. **LLVM Integration**: CppInterOp's integration with LLVM's Clang-REPL
81+
component (that can then be used as a runtime compiler for CPPYY), will
82+
further enhance CPPYY's performance.
83+
84+
**CppInterOp is a cppyy-inspired Library**
85+
86+
A major use case for CppInterOp is the CPPYY service. CPPYY is an automatic
87+
run-time bindings generator for Python and C++, and supports a wide range of
88+
C++ features (e.g., template instantiation). It operates on demand and
7389
generates only what is necessary. It requires a compiler (Cling or Clang-REPL).
7490
that can be available during programme runtime.
7591

92+
**Reducing Dependencies**
93+
94+
Recent work done on cppyy has been focused on removing unnecessary
95+
dependencies on domain-specific infrastructure (e.g., the ROOT
96+
framework). The idea was to convert the cppyy-backend to use Cling
97+
directly (instead of `ROOT meta`_), and then use it in cppyy.
98+
99+
Only a small set of APIs are needed to connect to the interpreter (Cling),
100+
since other APIs are already available in the standard compiler. This is what
101+
led to the creation of CppInterOp (a library of helper functions), that helped
102+
extract out things that were unnecessary for cppyy, etc.
103+
104+
The cppyy API surface is now incomparably smaller and simpler than what it used
105+
to be.
106+
107+
**LLVM Integration**
108+
76109
Once CppInterOp is integrated with LLVM's Clang-REPL component (that can then
77110
be used as a runtime compiler for CPPYY), it will further enhance CPPYY's
78111
performance in the following ways:
79112

80113

81-
**Simpler codebase:** The removal of string parsing logic will lead to a simpler
82-
code base.
114+
- *Simpler codebase:* The removal of string parsing logic will lead to a
115+
simpler code base.
116+
117+
- *Built into the LLVM toolchain:* The CppInterOp interfaces will be part of
118+
the LLVM toolchain (as part of Clang-REPL).
119+
120+
- *Better C++ Support:* C++ features such as Partial Template Specialisation
121+
will be available through CppInterOp.
122+
123+
- *Fewer Lines of Code:* A lot of dependencies and workarounds will be
124+
removed, reducing the lines of code required to execute CPPYY.
125+
126+
- *Well tested interoperability Layer:* The CppInterOp interfaces have full
127+
unit test coverage.
128+
129+
**Making C++ More Social**
130+
131+
cppyy is the first use case demonstrating how CppInterOp can enable C++ to be
132+
more easily interoperable with other languages. This helps many data scientists
133+
that are working with legacy C++ code and would like to use simpler, more
134+
interactive languages.
135+
136+
The goal of these enhancements is to eventually land these interoperability
137+
tools (including CppInterOp) to greater communities like LLVM and Clang, to
138+
enable C++ to interact with other languages besides Python.
139+
140+
Example: Template Instantiation
141+
-------------------------------
142+
143+
The developmental Cppyy version can run basic examples such as the one
144+
here. Features such as standalone functions and basic classes are also
145+
supported.
146+
147+
C++ code (Tmpl.h)
148+
149+
::
150+
151+
template <typename T>
152+
struct Tmpl {
153+
T m_num;
154+
T add (T n) {
155+
return m_num + n;
156+
}
157+
};
158+
159+
Python Interpreter
160+
161+
::
162+
163+
>>> import cppyy
164+
>>> import cppyy.gbl as Cpp
165+
>>> cppyy.include("Tmpl.h")
166+
>>> tmpl = Tmpl[int]()
167+
>>> tmpl.m_num = 4
168+
>>> print(tmpl.add(5))
169+
9
170+
>>> tmpl = Tmpl[float]()
171+
>>> tmpl.m_num = 3.0
172+
>>> print(tmpl.add(4.0))
173+
7.0
174+
175+
Where does the cppyy code reside?
176+
---------------------------------
177+
178+
Following are the main components where cppyy logic (with Compiler Research
179+
Organization’s customizations started by `sudo-panda`_) resides:
180+
181+
- `cppyy <https://github.com/compiler-research/cppyy>`_
182+
- `cppyy-backend <https://github.com/compiler-research/cppyy-backend>`_
183+
- `CPyCppyy <https://github.com/compiler-research/CPyCppyy>`_
184+
185+
..
186+
187+
Note: These are forks of the `upstream cppyy`_ repos created by `wlav`_.
188+
189+
CppInterOp is a separate library that helps these packages communicate with C++
190+
code.
191+
192+
- `CppInterOp <https://github.com/compiler-research/CppInterOp/tree/main>`_
193+
194+
How cppyy components interact with each other
195+
---------------------------------------------
83196

84-
**LLVM Integration:** The CppInterOp interfaces will be part of the LLVM
85-
toolchain (as part of Clang-REPL).
197+
cppyy is made up of the following packages:
86198

87-
**Better C++ Support:** C++ features such as Partial Template Specialisation will
88-
be available through CppInterOp.
199+
- A frontend: cppyy,
89200

90-
**Fewer Lines of Code:** A lot of dependencies and workarounds will be removed,
91-
reducing the lines of code required to execute CPPYY.
201+
- A backend: cppyy-backend, and
92202

93-
**Well tested interoperability Layer:** The CppInterOp interfaces have full
94-
unit test coverage.
203+
- An extension: CPyCppyy.
95204

205+
Besides these, the ``CppInterOp`` library serves as an additional layer on top
206+
of Cling/Clang-REPL that helps these packages in communicating with C++ code.
207+
208+
**1. cppyy-backend**
209+
210+
The `cppyy-backend`_ pacakge forms a layer over ``cppyy``, for example,
211+
modifying some functionality to provide the functions required for
212+
``CPyCppyy``.
213+
214+
`CPyCppyy`_ is a CPython extension module built on top of the same backend
215+
API as PyPy/_cppyy. It thus requires the installation of the cppyy-backend
216+
for use, which will pull in Cling.
217+
218+
``cppyy-backend`` also adds some `utilities`_ to help with repackaging and
219+
redistribution.
220+
221+
For example, it initializes the interpreter (using the
222+
``clingwrapper::ApplicationStarter`` function), adds the required
223+
``include`` paths, and adds the headers required for cppyy to work. It
224+
also adds some checks and combines two or more functions to help
225+
CPyCppyy work.
226+
227+
These changes help ensure that any change in ``cppyy`` doesn’t directly
228+
affect ``CPyCppyy``, and the API for ``CPyCppyy`` remains unchanged.
229+
230+
**2. CPyCppyy**
231+
232+
The ``CPyCppyy`` package uses the functionality provided by ``cppyy-backend``
233+
and provides Python objects for C++ entities. ``CPyCppyy`` uses separate proxy
234+
classes for each type of object. It also includes helper classes, for example,
235+
``Converters.cxx`` helps convert Python type objects to C++ type objects, while
236+
``Executors.cxx`` is used to execute a function and convert its return value to
237+
a Python object, so that it can be used inside Python.
238+
239+
**3. cppyy**
240+
241+
The cppyy package provides the front-end for Python. It is `included in code`_
242+
(using ``import cppyy``) to import cppyy in Python. It initializes things on
243+
the backend side, provides helper functions (e.g., ``cppdef()``, ``cppexec()``,
244+
etc.) that the user can utilize, and it calls the relevant backend functions
245+
required to initialize cppyy.
246+
247+
248+
Further Reading
249+
---------------
250+
251+
- `High-performance Python-C++ bindings with PyPy and
252+
Cling <http://cern.ch/wlav/Cppyy_LavrijsenDutta_PyHPC16.pdf>`_
253+
254+
- `Efficient and Accurate Automatic Python Bindings with cppyy &
255+
Cling <https://arxiv.org/abs/2304.02712>`_
256+
257+
- cppyy documentation:
258+
`cppyy.readthedocs.io <http://cppyy.readthedocs.io/>`_.
259+
260+
- Notebook-based tutorial: `Cppyy
261+
Tutorial <https://github.com/wlav/cppyy/blob/master/doc/tutorial/CppyyTutorial.ipynb>`_.
262+
263+
- `C++ Language Interoperability
264+
Layer <https://compiler-research.org/libinterop/>`_
265+
266+
**Credits:**
267+
268+
- `Wim Lavrijsen <https://github.com/wlav>`_ (Lawrence Berkeley National Lab.)
269+
for his original work in cppyy and mentorship towards student contributors.
270+
271+
- `Vassil Vasilev <https://github.com/vgvassilev>`_ (Princeton University)
272+
for his mentorship towards Compiler Research Org's student contributors.
273+
274+
- `Baidyanath Kundu <https://github.com/sudo-panda>`_ (Princeton University)
275+
for his research work on cppyy and Numba with `Compiler Research Organization`_
276+
(as discussed in this document).
277+
278+
- `Aaron Jomy <https://github.com/maximusron>`_ (Princeton University) for
279+
continuing this research work with `Compiler Research Organization`_.
96280

97281
In case you haven't already installed CppInterop, please do so before proceeding
98282
with the Installation And Usage Guide.
99-
:doc:`Installation and usage <InstallationAndUsage>`
283+
:doc:`Installation and usage <InstallationAndUsage>`
284+
285+
.. _Compiler Research Organization: https://compiler-research.org/
286+
287+
.. _upstream cppyy: https://github.com/wlav/cppyy
288+
289+
.. _wlav: https://github.com/wlav
290+
291+
.. _utilities: https://cppyy.readthedocs.io/en/latest/utilities.html
292+
293+
.. _included in code: https://cppyy.readthedocs.io/en/latest/starting.html
294+
295+
.. _sudo-panda: https://github.com/sudo-panda
296+
297+
.. _cppyy: https://cppyy.readthedocs.io/en/latest/index.html
298+
299+
.. _CppInterOp: https://github.com/compiler-research/CppInterOp
300+
301+
.. _ROOT meta: https://github.com/root-project/root/tree/master/core/meta
302+
303+
.. _enhancements in cppyy: https://arxiv.org/abs/2304.02712
304+
305+
.. _CPyCppyy: https://github.com/wlav/CPyCppyy
306+
307+
.. _cppyy-backend: https://github.com/wlav/cppyy-backend

0 commit comments

Comments
 (0)