@@ -15,7 +15,7 @@ The CppInterop comes with using it is a dynamic shared library,
15
15
libInterop = ctypes.CDLL(" ./libclangCppInterOp.so" )
16
16
17
17
The above method of usage is for Python; for C, we can include the headers of
18
- the library. Including this library in our programme enables the user to use
18
+ the library. Including this library in our program enables the user to use
19
19
the abilities of CppInterOp. CppInterOp helps programmers with multiple
20
20
verifications such as isClass, isBoolean, isStruct, and many more in different
21
21
languages. With the interop layer, we can access the scopes, namespaces of
@@ -28,10 +28,10 @@ Using LLVM as external library
28
28
29
29
In CppInterOp, we are leveraging Clang as a library for interoperability purposes.
30
30
To use Clang, we need to pass the Clang configuration to the CMake build system,
31
- so that the build system recognises the configuration and enables usage of Clang
31
+ so that the build system recognizes the configuration and enables usage of Clang
32
32
and LLVM.
33
33
We can consider clang-repl as a state manager, where CppInterOp allows you to
34
- query the state from the state manager. Thereafter, Cppyy uses this to create
34
+ query the state from the state manager. Thereafter, cppyy uses this to create
35
35
Python objects for C++.
36
36
37
37
.. code-block :: bash
@@ -65,35 +65,257 @@ robust (simple function calls, no inheritance, etc.). The goal is to make it as
65
65
close to the compiler API as possible, and each routine should do just one thing.
66
66
that it was designed for.
67
67
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
73
- generates only what is necessary. It requires a compiler (Cling or Clang-REPL).
74
- that can be available during programme runtime.
68
+ How cppyy leverages CppInterOp
69
+ ===============================
70
+
71
+ cppyy is a run-time Python-C++ bindings generator for calling C++ from Python
72
+ and Python from C++. Interestingly, it uses C++ interactively by using the
73
+ compiler as a service. This is made possible by the CppInterOp library.
74
+ Following are some of the ways cppyy leverages CppInterOp for better
75
+ performance and usability.
76
+
77
+ 1. **CppInterOp enables interoperability with C++ code **: CppInterOp provides a
78
+ minimalist and robust interface for language interoperability on the fly,
79
+ which helps CPPYY generate dynamic Python-C++ bindings by using a C++
80
+ interpreter (e.g., Clang-REPL/Cling) and LLVM.
81
+
82
+ 2. **Reducing dependencies **: Reducing domain-specific dependencies of cppyy
83
+ (e.g., on the Cling interpreter and the ROOT framework) to enable more
84
+ generalized usage.
85
+
86
+ 3. **LLVM Integration **: CppInterOp is designed to be used as a part of the
87
+ LLVM toolchain (as part of Clang-REPL) that can then be used as a runtime
88
+ compiler for CPPYY. This simplifies the codebase of CPPYY and enhances its
89
+ performance.
90
+
91
+ 4. **Making C++ More Social **: CppInterOp and cppyy help data scientists that
92
+ are working with legacy C++ code experiment with simpler, more interactive
93
+ languages, while also interacting with larger communities.
94
+
95
+ **CppInterOp enables interoperability with C++ code **
96
+
97
+ cppyy is a major use case for CppInterOp. cppyy is an automatic run-time
98
+ bindings generator for Python and C++, and supports a wide range of C++
99
+ features, including template instantiation. It operates on demand and generates
100
+ only what is necessary. It requires a compiler (Cling or Clang-REPL) that can
101
+ be available during program runtime.
102
+
103
+ **Reducing Dependencies **
104
+
105
+ Recent work done on cppyy has been focused on reducing dependencies on
106
+ domain-specific infrastructure (e.g., the ROOT framework). Using an independent
107
+ library such as CppInterOp helps accomplish that, while also improving the code
108
+ consistency in cppyy.
109
+
110
+ The CppInterOp library can be configured to use the newly developed Clang-Repl
111
+ backend available in LLVM upstream (or to use the Cling legacy backend, for
112
+ compatibility with High Energy Physics applications).
113
+
114
+ Only a small set of APIs are needed to connect to the interpreter (Clang-Repl/
115
+ Cling), since other APIs are already available in the standard compiler. This
116
+ is one of the reasons that led to the creation of CppInterOp (a library of
117
+ helper functions), that can help extract out things that are unnecessary for
118
+ for core cppyy functionality.
119
+
120
+ The cppyy API surface is now incomparably smaller and simpler than what it used
121
+ to be.
122
+
123
+ **LLVM Integration **
75
124
76
125
Once CppInterOp is integrated with LLVM's Clang-REPL component (that can then
77
- be used as a runtime compiler for CPPYY ), it will further enhance CPPYY 's
126
+ be used as a runtime compiler for cppyy ), it will further enhance cppyy 's
78
127
performance in the following ways:
79
128
80
129
81
- **Simpler codebase: ** The removal of string parsing logic will lead to a simpler
82
- code base.
130
+ - *Simpler codebase: * The removal of string parsing logic will lead to a
131
+ simpler code base.
132
+
133
+ - *Built into the LLVM toolchain: * The CppInterOp depends only on the LLVM
134
+ toolchain (as part of Clang-REPL).
135
+
136
+ - *Better C++ Support: * Finer-grained control over template instantiation is
137
+ available through CppInterOp.
138
+
139
+ - *Fewer Lines of Code: * A lot of dependencies and workarounds will be
140
+ removed, reducing the lines of code required to execute cppyy.
141
+
142
+ - *Well tested interoperability Layer: * The CppInterOp interfaces have full
143
+ unit test coverage.
144
+
145
+ **Making C++ More Social **
146
+
147
+ cppyy is the first use case demonstrating how CppInterOp can enable C++ to be
148
+ more easily interoperable with other languages. This helps many data scientists
149
+ that are working with legacy C++ code and would like to use simpler, more
150
+ interactive languages.
151
+
152
+ The goal of these enhancements is to eventually land these interoperability
153
+ tools (including CppInterOp) to broader communities like LLVM and Clang, to
154
+ enable C++ to interact with other languages besides Python.
155
+
156
+ Example: Template Instantiation
157
+ -------------------------------
158
+
159
+ The developmental cppyy version can run basic examples such as the one
160
+ here. Features such as standalone functions and basic classes are also
161
+ supported.
162
+
163
+ C++ code (Tmpl.h)
164
+
165
+ ::
166
+
167
+ template <typename T>
168
+ struct Tmpl {
169
+ T m_num;
170
+ T add (T n) {
171
+ return m_num + n;
172
+ }
173
+ };
174
+
175
+ Python Interpreter
176
+
177
+ ::
178
+
179
+ >>> import cppyy
180
+ >>> cppyy.include("Tmpl.h")
181
+ >>> tmpl = Tmpl[int]()
182
+ >>> tmpl.m_num = 4
183
+ >>> print(tmpl.add(5))
184
+ 9
185
+ >>> tmpl = Tmpl[float]()
186
+ >>> tmpl.m_num = 3.0
187
+ >>> print(tmpl.add(4.0))
188
+ 7.0
189
+
190
+ Where does the cppyy code reside?
191
+ ---------------------------------
192
+
193
+ Following are the main components where cppyy logic (with Compiler Research
194
+ Organization’s customizations started by `sudo-panda `_) resides:
195
+
196
+ - `cppyy <https://github.com/compiler-research/cppyy >`_
197
+ - `cppyy-backend <https://github.com/compiler-research/cppyy-backend >`_
198
+ - `CPyCppyy <https://github.com/compiler-research/CPyCppyy >`_
199
+
200
+ ..
201
+
202
+ Note: These are forks of the `upstream cppyy `_ repos created by `wlav `_.
203
+
204
+ CppInterOp is a separate library that helps these packages communicate with C++
205
+ code.
206
+
207
+ - `CppInterOp <https://github.com/compiler-research/CppInterOp/tree/main >`_
83
208
84
- ** LLVM Integration: ** The CppInterOp interfaces will be part of the LLVM
85
- toolchain (as part of Clang-REPL).
209
+ How cppyy components interact with each other
210
+ ---------------------------------------------
86
211
87
- **Better C++ Support: ** C++ features such as Partial Template Specialisation will
88
- be available through CppInterOp.
212
+ cppyy is made up of the following packages:
89
213
90
- **Fewer Lines of Code: ** A lot of dependencies and workarounds will be removed,
91
- reducing the lines of code required to execute CPPYY.
214
+ - A frontend: cppyy,
92
215
93
- **Well tested interoperability Layer: ** The CppInterOp interfaces have full
94
- unit test coverage.
216
+ - A backend: cppyy-backend, and
95
217
218
+ - An extension: CPyCppyy.
219
+
220
+ Besides these, the ``CppInterOp `` library serves as an additional layer on top
221
+ of Cling/Clang-REPL that helps these packages in communicating with C++ code.
222
+
223
+ **1. cppyy-backend **
224
+
225
+ The `cppyy-backend `_ package forms a layer over ``cppyy ``, for example,
226
+ modifying some functionality to provide the functions required for
227
+ ``CPyCppyy ``.
228
+
229
+ `CPyCppyy `_ is a CPython extension module built on top of the same backend
230
+ API as PyPy/_cppyy. It thus requires the installation of the cppyy-backend
231
+ for use, which will pull in Cling.
232
+
233
+ ``cppyy-backend `` also adds some `utilities `_ to help with repackaging and
234
+ redistribution.
235
+
236
+ For example, ``cppyy-backend `` initializes the interpreter (using the
237
+ ``clingwrapper::ApplicationStarter `` function), adds the required ``include ``
238
+ paths, and adds the headers required for cppyy to work. It also adds some
239
+ checks and combines two or more functions to help CPyCppyy work.
240
+
241
+ These changes help ensure that any change in ``cppyy `` doesn’t directly
242
+ affect ``CPyCppyy ``, and the API for ``CPyCppyy `` remains unchanged.
243
+
244
+ **2. CPyCppyy **
245
+
246
+ The ``CPyCppyy `` package uses the functionality provided by ``cppyy-backend ``
247
+ and provides Python objects for C++ entities. ``CPyCppyy `` uses separate proxy
248
+ classes for each type of object. It also includes helper classes, for example,
249
+ ``Converters.cxx `` helps convert Python type objects to C++ type objects, while
250
+ ``Executors.cxx `` is used to execute a function and convert its return value to
251
+ a Python object, so that it can be used inside Python.
252
+
253
+ **3. cppyy **
254
+
255
+ The cppyy package provides the front-end for Python. It is `included in code `_
256
+ (using ``import cppyy ``) to import cppyy in Python. It initializes things on
257
+ the backend side, provides helper functions (e.g., ``cppdef() ``, ``cppexec() ``,
258
+ etc.) that the user can utilize, and it calls the relevant backend functions
259
+ required to initialize cppyy.
260
+
261
+
262
+ Further Reading
263
+ ---------------
264
+
265
+ - `High-performance Python-C++ bindings with PyPy and
266
+ Cling <http://cern.ch/wlav/Cppyy_LavrijsenDutta_PyHPC16.pdf> `_
267
+
268
+ - `Efficient and Accurate Automatic Python Bindings with cppyy &
269
+ Cling <https://arxiv.org/abs/2304.02712> `_
270
+
271
+ - cppyy documentation:
272
+ `cppyy.readthedocs.io <http://cppyy.readthedocs.io/ >`_.
273
+
274
+ - Notebook-based tutorial: `Cppyy
275
+ Tutorial <https://github.com/wlav/cppyy/blob/master/doc/tutorial/CppyyTutorial.ipynb> `_.
276
+
277
+ - `C++ Language Interoperability
278
+ Layer <https://compiler-research.org/libinterop/> `_
279
+
280
+ **Credits: **
281
+
282
+ - `Wim Lavrijsen <https://github.com/wlav >`_ (Lawrence Berkeley National Lab.)
283
+ for his original work in cppyy and mentorship towards student contributors.
284
+
285
+ - `Vassil Vasilev <https://github.com/vgvassilev >`_ (Princeton University)
286
+ for his mentorship towards Compiler Research Org's student contributors.
287
+
288
+ - `Baidyanath Kundu <https://github.com/sudo-panda >`_ (Princeton University)
289
+ for his research work on cppyy and Numba with `Compiler Research Organization `_
290
+ (as discussed in this document).
291
+
292
+ - `Aaron Jomy <https://github.com/maximusron >`_ (Princeton University) for
293
+ continuing this research work with `Compiler Research Organization `_.
96
294
97
295
In case you haven't already installed CppInterop, please do so before proceeding
98
296
with the Installation And Usage Guide.
99
- :doc: `Installation and usage <InstallationAndUsage >`
297
+ :doc: `Installation and usage <InstallationAndUsage >`
298
+
299
+ .. _Compiler Research Organization : https://compiler-research.org/
300
+
301
+ .. _upstream cppyy : https://github.com/wlav/cppyy
302
+
303
+ .. _wlav : https://github.com/wlav
304
+
305
+ .. _utilities : https://cppyy.readthedocs.io/en/latest/utilities.html
306
+
307
+ .. _included in code : https://cppyy.readthedocs.io/en/latest/starting.html
308
+
309
+ .. _sudo-panda : https://github.com/sudo-panda
310
+
311
+ .. _cppyy : https://cppyy.readthedocs.io/en/latest/index.html
312
+
313
+ .. _CppInterOp : https://github.com/compiler-research/CppInterOp
314
+
315
+ .. _ROOT meta : https://github.com/root-project/root/tree/master/core/meta
316
+
317
+ .. _enhancements in cppyy : https://arxiv.org/abs/2304.02712
318
+
319
+ .. _CPyCppyy : https://github.com/wlav/CPyCppyy
320
+
321
+ .. _cppyy-backend : https://github.com/wlav/cppyy-backend
0 commit comments