|
| 1 | +Thank you for your interest in contributing to ExecuTorch! |
| 2 | + |
| 3 | +This document (CONTRIBUTING.md) covers some of the more technical aspects of |
| 4 | +contributing. |
| 5 | + |
| 6 | +## Coding Style |
| 7 | + |
| 8 | +Goal: Encourage standards that make it easier to read, edit, maintain, and debug |
| 9 | +the ExecuTorch code. |
| 10 | + |
| 11 | +### Python Style |
| 12 | + |
| 13 | +ExecuTorch Python code follows the style used by the PyTorch core project. |
| 14 | + |
| 15 | +### C++ Style |
| 16 | + |
| 17 | +ExecuTorch code uses the [Google C++ |
| 18 | +Style](https://google.github.io/styleguide/cppguide.html), with modifications. |
| 19 | + |
| 20 | +Rationale: Google style is close to the C++ style used by PyTorch core, although |
| 21 | +PyTorch core does not explicitly document its C++ style. Google style is well |
| 22 | +documented, and has exceptional tooling support. |
| 23 | + |
| 24 | +**Modifications** to the Google C++ style, to make it closer to the code in |
| 25 | +PyTorch core: |
| 26 | +- Function and method names should use `lower_snake_case()`. This follows the |
| 27 | + convention that PyTorch core inherited from its namesake Python, and is the |
| 28 | + biggest modification to the Google C++ style. |
| 29 | +- File names should use `lower_snake_case.cpp` (not `.cc`, and not |
| 30 | + `PascalCase.cpp`). This follows the most common pattern in PyTorch core. |
| 31 | +- Headers should use `#pragma once` instead of manual include guards. This |
| 32 | + follows the most common pattern in PyTorch core. |
| 33 | +- All includes should use `<angle brackets>`, not `"double quotes"`. This |
| 34 | + ensures that headers are included using the compiler's include path, and not |
| 35 | + relative to the local file. |
| 36 | +- Documentation comments should follow Doxygen syntax, either `//** ... */` |
| 37 | + (multi-line) or `/// ...` (single line), with `@`-style parameters like |
| 38 | + `@param`, `@retval`. Public APIs must be documented in the `.h` files that |
| 39 | + declare them. |
| 40 | +- TODOs should prefer to reference a task or issue number like `TODO(#123): |
| 41 | + <description>`, rather than a username. A task can manage much-more-nuanced |
| 42 | + information, and can change ownership as people leave and join the project. |
| 43 | + |
| 44 | +See the rest of this file for other portability- and efficiency-related |
| 45 | +modifications to the Google C++ style guide. |
| 46 | + |
| 47 | +### C++ Portability Guidelines |
| 48 | + |
| 49 | +See also |
| 50 | +[Portable Programming](https://github.com/pytorch/executorch/blob/main/docs/website/docs/contributors/portable_programming.md) |
| 51 | +for detailed advice. |
| 52 | + |
| 53 | +#### C++ language version |
| 54 | + |
| 55 | +**C++11.** |
| 56 | + |
| 57 | +NOTE: The code does not yet fully conform to this, and some files require C++17. |
| 58 | + |
| 59 | +Rationale: This is a compromise between being compatible with older, proprietary |
| 60 | +toolchains, and having access to relatively modern C++ features. |
| 61 | + |
| 62 | +#### C/C++ standard library usage |
| 63 | + |
| 64 | +**Restricted usage of the C++ standard library.** |
| 65 | + |
| 66 | +Rationale: ExecuTorch is intended to be portable to bare-metal systems that lack |
| 67 | +certain features, like dynamic memory, threading, and locking, required by parts |
| 68 | +of the standard library. It is also intended to be as small as possible, and |
| 69 | +some convenient stdlib features may grow the binary size unacceptably. |
| 70 | + |
| 71 | +Generally, do not instantiate types that allocate memory under the hood, like |
| 72 | +`std::vector` or `std::string`. Do not call `new`, `malloc()` or `mmap()`; do |
| 73 | +not use iostreams; do not operate on files. |
| 74 | + |
| 75 | +However, it is convenient and portable (and sometimes necessary) to use static |
| 76 | +standard library concepts like `std::move`, or metaprogramming helpers like |
| 77 | +`std::is_floating_point<>`. Pure code like `<cmath>` and `<cstring>` is fine, |
| 78 | +as long as you stay away from functions that allocate memory (like `strdup()`). |
| 79 | + |
| 80 | +It is also allowed (and sometimes necessary) to use "placement `new`", but be |
| 81 | +careful to also manually destroy objects initialized in this way. |
| 82 | + |
| 83 | +#### C++ language features |
| 84 | + |
| 85 | +**Exceptions: Do not use.** |
| 86 | +- Rationale: Exceptions are not widely supported on some classes of |
| 87 | + microcontrollers and DSPs, and they can significantly increase binary size. |
| 88 | + |
| 89 | +**Threads, thread_local, locking: Do not use, except in optional libraries that |
| 90 | +must work with threading** |
| 91 | +- Rationale: The core runtime must work on systems that do not have threading |
| 92 | + support. |
| 93 | + |
| 94 | +**RTTI, dynamic_cast, and `<typeid>`: Do not use.** |
| 95 | +- Rationale: RTTI adds extra data to every virtual class. ExecuTorch doesn't |
| 96 | + have a strong need for `dynamic_cast` and friends, so it's better to reduce |
| 97 | + the binary size. |
| 98 | + |
| 99 | +**Templates and template metaprogramming: Be careful and avoid if possible.** |
| 100 | +- Rationale: Most templating results in code generation, and is one of the most |
| 101 | + common sources of binary bloat. Some use of templates is fine (e.g. an |
| 102 | + `ArrayRef<T>`, or code that handles multiple `ScalarType` types), but for the |
| 103 | + most part avoid them if possible. |
0 commit comments