• Home
  • History
  • Annotate
Name Date Size #Lines LOC

..03-May-2022-

codegen/H25-Feb-2018-1,9471,671

examples/H25-Feb-2018-420300

logo/H03-May-2022-

peachpy/H25-Feb-2018-22,28818,222

sphinx/H25-Feb-2018-9463

test/H03-May-2022-12,7319,837

.gitignoreH A D25-Feb-2018529 3730

.travis.ymlH A D25-Feb-2018219 1514

LICENSE.rstH A D25-Feb-20181.4 KiB1610

MANIFEST.inH A D25-Feb-201868 64

README.rstH A D25-Feb-201813.5 KiB295188

appveyor.ymlH A D25-Feb-2018296 1312

setup.cfgH A D25-Feb-2018325 1110

setup.pyH A D25-Feb-20183.4 KiB9782

README.rst

1.. image:: https://github.com/Maratyszcza/PeachPy/blob/master/logo/peachpy.png
2  :alt: PeachPy logo
3  :align: center
4
5===========================================================================
6Portable Efficient Assembly Code-generator in Higher-level Python (PeachPy)
7===========================================================================
8
9.. image:: https://img.shields.io/badge/License-BSD%202--Clause%20%22Simplified%22%20License-blue.svg
10  :alt: PeachPy License: Simplified BSD
11  :target: https://github.com/Maratyszcza/PeachPy/blob/master/LICENSE.rst
12
13.. image:: https://travis-ci.org/Maratyszcza/PeachPy.svg?branch=master
14  :alt: Travis-CI Build Status
15  :target: https://travis-ci.org/Maratyszcza/PeachPy/
16
17.. image:: https://ci.appveyor.com/api/projects/status/p64ew9in189bu2pl?svg=true
18  :alt: AppVeyor Build Status
19  :target: https://ci.appveyor.com/project/MaratDukhan/peachpy
20
21PeachPy is a Python framework for writing high-performance assembly kernels.
22
23PeachPy aims to simplify writing optimized assembly kernels while preserving all optimization opportunities of traditional assembly. Some PeachPy features:
24
25- Universal assembly syntax for Windows, Unix, and Golang assembly.
26
27  * PeachPy can directly generate ELF, MS COFF and Mach-O object files and assembly listings for Golang toolchain
28
29- Automatic adaption of function to different calling conventions and ABIs.
30
31  * Functions for different platforms can be generated from the same assembly source
32  * Supports Microsoft x64 ABI, System V x86-64 ABI (Linux and OS X), Linux x32 ABI, Native Client x86-64 SFI ABI, Golang AMD64 ABI, Golang AMD64p32 ABI
33
34- Automatic register allocation.
35
36  * PeachPy is flexible and lets mix auto-allocated and hardcoded registers in the same code.
37
38- Automation of routine tasks in assembly programming:
39
40  * Function prolog and epilog and generated by PeachPy
41  * De-duplication of data constants (e.g. `Constant.float32x4(1.0)`)
42  * Analysis of ISA extensions used in a function
43
44- Supports x86-64 instructions up to AVX-512 and SHA
45
46  * Including 3dnow!+, XOP, FMA3, FMA4, TBM and BMI2.
47  * Excluding x87 FPU and most system instructions.
48  * Rigorously tested with `auto-generated tests <https://github.com/Maratyszcza/PeachPy/tree/master/test/x86_64/encoding>`_ to produce the same opcodes as binutils.
49
50- Auto-generation of metadata files
51
52  * Makefile with module dependencies (`-MMD` and `-MF` options)
53  * C header for the generated functions
54  * Function metadata in JSON format
55
56- Python-based metaprogramming and code-generation.
57- Multiplexing of multiple instruction streams (helpful for software pipelining).
58- Compatible with Python 2 and Python 3, CPython and PyPy.
59
60Online Demo
61-----------
62
63You can try online demo on `PeachPy.IO <http://www.peachpy.io>`_
64
65Installation
66------------
67
68PeachPy is actively developed, and thus there are presently no stable releases of 0.2 branch. We recommend that you use the `master` version:
69
70.. code-block:: bash
71
72  pip install --upgrade git+https://github.com/Maratyszcza/PeachPy
73
74Installation for development
75****************************
76
77If you plan to modify PeachPy, we recommend the following installation procedure:
78
79.. code-block:: bash
80
81  git clone https://github.com/Maratyszcza/PeachPy.git
82  cd PeachPy
83  python setup.py develop
84
85
86Using PeachPy as a command-line tool
87------------------------------------
88
89.. code-block:: python
90
91  # These two lines are not needed for PeachPy, but will help you get autocompletion in good code editors
92  from peachpy import *
93  from peachpy.x86_64 import *
94
95  # Lets write a function float DotProduct(const float* x, const float* y)
96
97  # If you want maximum cross-platform compatibility, arguments must have names
98  x = Argument(ptr(const_float_), name="x")
99  # If name is not specified, it is auto-detected
100  y = Argument(ptr(const_float_))
101
102  # Everything inside the `with` statement is function body
103  with Function("DotProduct", (x, y), float_,
104    # Enable instructions up to SSE4.2
105    # PeachPy will report error if you accidentially use a newer instruction
106    target=uarch.default + isa.sse4_2):
107
108    # Request two 64-bit general-purpose registers. No need to specify exact names.
109    reg_x, reg_y = GeneralPurposeRegister64(), GeneralPurposeRegister64()
110
111    # This is a cross-platform way to load arguments. PeachPy will map it to something proper later.
112    LOAD.ARGUMENT(reg_x, x)
113    LOAD.ARGUMENT(reg_y, y)
114
115    # Also request a virtual 128-bit SIMD register...
116    xmm_x = XMMRegister()
117    # ...and fill it with data
118    MOVAPS(xmm_x, [reg_x])
119    # It is fine to mix virtual and physical (xmm0-xmm15) registers in the same code
120    MOVAPS(xmm2, [reg_y])
121
122    # Execute dot product instruction, put result into xmm_x
123    DPPS(xmm_x, xmm2, 0xF1)
124
125    # This is a cross-platform way to return results. PeachPy will take care of ABI specifics.
126    RETURN(xmm_x)
127
128Now you can compile this code into a binary object file that you can link into a program...
129
130.. code-block:: bash
131
132  # Use MS-COFF format with Microsoft ABI for Windows
133  python -m peachpy.x86_64 -mabi=ms -mimage-format=ms-coff -o example.obj example.py
134  # Use Mach-O format with SysV ABI for OS X
135  python -m peachpy.x86_64 -mabi=sysv -mimage-format=mach-o -o example.o example.py
136  # Use ELF format with SysV ABI for Linux x86-64
137  python -m peachpy.x86_64 -mabi=sysv -mimage-format=elf -o example.o example.py
138  # Use ELF format with x32 ABI for Linux x32 (x86-64 with 32-bit pointer)
139  python -m peachpy.x86_64 -mabi=x32 -mimage-format=elf -o example.o example.py
140  # Use ELF format with Native Client x86-64 ABI for Chromium x86-64
141  python -m peachpy.x86_64 -mabi=nacl -mimage-format=elf -o example.o example.py
142
143What else? You can convert the program to Plan 9 assembly for use with Go programming language:
144
145.. code-block:: bash
146
147  # Use Go ABI (asm version) with -S flag to generate assembly for Go x86-64 targets
148  python -m peachpy.x86_64 -mabi=goasm -S -o example_amd64.s example.py
149  # Use Go-p32 ABI (asm version) with -S flag to generate assembly for Go x86-64 targets with 32-bit pointers
150  python -m peachpy.x86_64 -mabi=goasm-p32 -S -o example_amd64p32.s example.py
151
152If Plan 9 assembly is too restrictive for your use-case, generate ``.syso`` objects `which can be linked into Go programs <https://github.com/golang/go/wiki/GcToolchainTricks#use-syso-file-to-embed-arbitrary-self-contained-c-code>`_:
153
154.. code-block:: bash
155
156  # Use Go ABI (syso version) to generate .syso objects for Go x86-64 targets
157  # Image format can be any (ELF/Mach-O/MS-COFF)
158  python -m peachpy.x86_64 -mabi=gosyso -mimage-format=elf -o example_amd64.syso example.py
159  # Use Go-p32 ABI (syso version) to generate .syso objects for Go x86-64 targets with 32-bit pointers
160  # Image format can be any (ELF/Mach-O/MS-COFF)
161  python -m peachpy.x86_64 -mabi=gosyso-p32 -mimage-format=elf -o example_amd64p32.syso example.py
162
163See `examples <https://github.com/Maratyszcza/PeachPy/tree/master/examples>`_ for real-world scenarios of using PeachPy with ``make``, ``nmake`` and ``go generate`` tools.
164
165Using PeachPy as a Python module
166--------------------------------
167
168When command-line tool does not provide sufficient flexibility, Python scripts can import PeachPy objects from ``peachpy`` and ``peachpy.x86_64`` modules and do arbitrary manipulations on output images, program structure, instructions, and bytecodes.
169
170PeachPy as Inline Assembler for Python
171**************************************
172
173PeachPy links assembly and Python: it represents assembly instructions and syntax as Python classes, functions, and objects.
174But it also works the other way around: PeachPy can represent your assembly functions as callable Python functions!
175
176.. code-block:: python
177
178  from peachpy import *
179  from peachpy.x86_64 import *
180
181  x = Argument(int32_t)
182  y = Argument(int32_t)
183
184  with Function("Add", (x, y), int32_t) as asm_function:
185      reg_x = GeneralPurposeRegister32()
186      reg_y = GeneralPurposeRegister32()
187
188      LOAD.ARGUMENT(reg_x, x)
189      LOAD.ARGUMENT(reg_y, y)
190
191      ADD(reg_x, reg_y)
192
193      RETURN(reg_x)
194
195  python_function = asm_function.finalize(abi.detect()).encode().load()
196
197  print(python_function(2, 2)) # -> prints "4"
198
199PeachPy as Instruction Encoder
200******************************
201
202PeachPy can be used to explore instruction length, opcodes, and alternative encodings:
203
204.. code-block:: python
205
206  from peachpy.x86_64 import *
207
208  ADD(eax, 5).encode() # -> bytearray(b'\x83\xc0\x05')
209
210  MOVAPS(xmm0, xmm1).encode_options() # -> [bytearray(b'\x0f(\xc1'), bytearray(b'\x0f)\xc8')]
211
212  VPSLLVD(ymm0, ymm1, [rsi + 8]).encode_length_options() # -> {6: bytearray(b'\xc4\xe2uGF\x08'),
213                                                         #     7: bytearray(b'\xc4\xe2uGD&\x08'),
214                                                         #     9: bytearray(b'\xc4\xe2uG\x86\x08\x00\x00\x00')}
215
216Tutorials
217---------
218
219- `Writing Go assembly functions with PeachPy <https://blog.gopheracademy.com/advent-2016/peachpy/>`_ by `Damian Gryski <https://github.com/dgryski>`_
220
221- `Adventures in JIT compilation (Part 4) <http://eli.thegreenplace.net/2017/adventures-in-jit-compilation-part-4-in-python/>`_ by `Eli Bendersky <https://github.com/eliben>`_
222
223Users
224-----
225
226- `NNPACK <https://github.com/Maratyszcza/NNPACK>`_ -- an acceleration layer for convolutional networks on multi-core CPUs.
227
228- `ChaCha20 <https://git.schwanenlied.me/yawning/chacha20>`_ -- Go implementation of ChaCha20 cryptographic cipher.
229
230- `AEZ <https://git.schwanenlied.me/yawning/aez>`_ -- Go implemenetation of AEZ authenticated-encryption scheme.
231
232- `bp128 <https://github.com/robskie/bp128>`_ -- Go implementation of SIMD-BP128 integer encoding and decoding.
233
234- `go-marvin32 <https://github.com/dgryski/go-marvin32>`_ -- Go implementation of Microsoft's Marvin32 hash function.
235
236- `go-highway <https://github.com/dgryski/go-highway>`_ -- Go implementation of Google's Highway hash function.
237
238- `go-metro <https://github.com/dgryski/go-metro>`_ -- Go implementation of MetroHash function.
239
240- `go-stadtx <https://github.com/dgryski/go-stadtx>`_ -- Go implementation of Stadtx hash function.
241
242- `go-sip13 <https://github.com/dgryski/go-sip13>`_ -- Go implementation of SipHash 1-3 function.
243
244- `go-chaskey <https://github.com/dgryski/go-chaskey>`_ -- Go implementation of Chaskey MAC.
245
246- `go-speck <https://github.com/dgryski/go-speck>`_ -- Go implementation of SPECK cipher.
247
248- `go-bloomindex <https://github.com/dgryski/go-bloomindex>`_ - Go implementation of Bloom-filter based search index.
249
250- `go-groupvariant <https://github.com/dgryski/go-groupvarint>`_ - SSE-optimized group varint integer encoding in Go.
251
252- `Yeppp! <http://www.yeppp.info>`_ performance library. All optimized kernels in Yeppp! are implemented in PeachPy (uses old version of PeachPy with deprecated syntax).
253
254Peer-Reviewed Publications
255--------------------------
256
257- Marat Dukhan "PeachPy: A Python Framework for Developing High-Performance Assembly Kernels", Python for High-Performance Computing (PyHPC) 2013 (`slides <http://www.yeppp.info/resources/peachpy-slides.pdf>`_, `paper <http://www.yeppp.info/resources/peachpy-paper.pdf>`_, code uses deprecated syntax)
258
259- Marat Dukhan "PeachPy meets Opcodes: Direct Machine Code Generation from Python", Python for High-Performance Computing (PyHPC) 2015 (`slides <http://www.peachpy.io/slides/pyhpc2015>`_, `paper on ACM Digital Library <https://dl.acm.org/citation.cfm?id=2835860>`_).
260
261Other Presentations
262-------------------
263
264- Marat Dukhan "Developing Low-Level Assembly Kernels in PeachPy", presentation on `The First BLIS Retreat Workshop <https://www.cs.utexas.edu/users/flame/BLISRetreat/>`_, 2013 (`slides <https://www.cs.utexas.edu/users/flame/BLISRetreat/BLISRetreatTalks/PeachPy.pdf>`_, code uses deprecated syntax)
265
266- Marat Dukhan "Porting BLIS micro-kernels to PeachPy", presentation on `The Third BLIS Retreat Workshop <https://www.cs.utexas.edu/users/flame/BLISRetreat2015/>`_, 2015 (`slides <http://www.peachpy.io/slides/blis-retreat-2015/>`_)
267
268- Marat Dukhan "Accelerating Data Processing in Go with SIMD Instructions", presentation on `Atlanta Go Meetup <http://www.meetup.com/Go-Users-Group-Atlanta>`_, September 16, 2015 (`slides <https://docs.google.com/presentation/d/1MYg8PyhEf0oIvZ9YU2panNkVXsKt5UQBl_vGEaCeB1k/edit?usp=sharing>`_)
269
270Dependencies
271------------
272
273- Nearly all instruction classes in PeachPy are generated from `Opcodes Database <https://github.com/Maratyszcza/Opcodes>`_
274
275- Instruction encodings in PeachPy are validated against `binutils <https://www.gnu.org/software/binutils/>`_ using auto-generated tests
276
277- PeachPy uses `six <https://pythonhosted.org/six/>`_ and `enum34 <https://pypi.python.org/pypi/enum34>`_ packages as a compatibility layer between Python 2 and Python 3
278
279Acknowledgements
280----------------
281
282.. image:: https://github.com/Maratyszcza/PeachPy/blob/master/logo/hpcgarage.png
283  :alt: HPC Garage logo
284  :target: http://hpcgarage.org/
285
286.. image:: https://github.com/Maratyszcza/PeachPy/blob/master/logo/college-of-computing.gif
287  :alt: Georgia Tech College of Computing logo
288  :target: http://www.cse.gatech.edu/
289
290This work is a research project at the HPC Garage lab in the Georgia Institute of Technology, College of Computing, School of Computational Science and Engineering.
291
292The work was supported in part by grants to Prof. Richard Vuduc's research lab, `The HPC Garage <www.hpcgarage.org>`_, from the National Science Foundation (NSF) under NSF CAREER award number 0953100; and a grant from the Defense Advanced Research Projects Agency (DARPA) Computer Science Study Group program
293
294Any opinions, conclusions or recommendations expressed in this software and documentation are those of the authors and not necessarily reflect those of NSF or DARPA.
295