Commit 17b5543
authored
Add intent-based ref handling and out-return support for bindings (#278)
In ISO C++, whether an argument is mutated or not is not directly
inferable from the signature itself. For example,
```c++
void set_as_42(int &a) { a = 42; }
const int& operator+ (const int&, const int&);
```
both accepts a reference as input, but whether the function modifies the
argument is unknown until further analysis into the body is performed.
This creates confusions for language bindings on whether the arguments
should be passed by reference or value, as other language may dictate
the argument passing semantics differently from C++.
Certain compiler provides additional annotation features to denote them.
Such as
[SAL](https://learn.microsoft.com/en-us/cpp/code-quality/best-practices-and-examples-sal?view=msvc-170).
In this PR, Numbast introduces an `argument intent` option that allows
user to configure argument passing mode on per-function, per-parameter
basis. Per argument, the following options are available:
```
- in (default, arguments are passed as-is by value)
- inout_ptr (argument is both used as an input mutated, exposed in Numba as a CPointer type to itself)
- out_ptr (argument is used to store output, thus mutated; exposed in Numba as a CPointer type to itself)
- out_return (return arguments are pre-allocated on stack, returned in Numba bindings)
```
Take the following C++ signature as an example:
```c++
// A C++ function that processes `input`, writes result to `out`, and returns an exit code.
int mutative(int &out, int input);
```
A typical argument intent setup for Numbast looks like:
```
{"mutative": {"out": "out_return"}}
```
This indicates that argument `out` is returned as the functions return
value in the corresponding binding. And since this function already has
a return value, the binding will now return a tuple of ints, with first
corresponds to the exit code, and the second corresponds to the result.
The Python binding signature:
```python
def mutative(input: numba.int32) -> Tuple[numba.int32, numba.int32]: ...
```
Alternatively, if intent is set to: `"out": "out_ptr"`, the signature
becomes:
```python
def mutative(out: CPointer(int32), input: int32) -> int32: ...
```
<!-- This is an auto-generated comment: release notes by coderabbit.ai
-->
## Summary by CodeRabbit
* **New Features**
* Introduced a per-function argument-intent system to control parameter
semantics (in, inout, out, out-return) and pointer-passing behavior.
* Support for returning multiple values via out-parameters alongside
regular returns.
* Static binding generation and rendering now accept and propagate
per-function intent overrides.
* **Tests**
* Added end-to-end tests, fixtures and test data exercising
out-parameters, in/out-pointer semantics, out-return behavior, and
mutative device functions.
<sub>✏️ Tip: You can customize this high-level summary in your review
settings.</sub>
<!-- end of auto-generated comment: release notes by coderabbit.ai -->
---------
Co-authored-by: Michael Wang <isVoid@users.noreply.github.com>1 parent 036f2ad commit 17b5543
File tree
21 files changed
+1277
-90
lines changed- numbast
- src/numbast
- static
- tests
- data
- src
- tools
- tests
- data
21 files changed
+1277
-90
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
37 | 37 | | |
38 | 38 | | |
39 | 39 | | |
40 | | - | |
| 40 | + | |
41 | 41 | | |
42 | 42 | | |
43 | 43 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
7 | 7 | | |
8 | 8 | | |
9 | 9 | | |
10 | | - | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
11 | 14 | | |
12 | 15 | | |
13 | 16 | | |
| |||
28 | 31 | | |
29 | 32 | | |
30 | 33 | | |
31 | | - | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
1 | 4 | | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
2 | 8 | | |
3 | 9 | | |
4 | 10 | | |
| |||
36 | 42 | | |
37 | 43 | | |
38 | 44 | | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
39 | 64 | | |
40 | | - | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
41 | 72 | | |
42 | | - | |
| 73 | + | |
43 | 74 | | |
44 | 75 | | |
45 | 76 | | |
46 | 77 | | |
47 | | - | |
| 78 | + | |
48 | 79 | | |
49 | 80 | | |
50 | 81 | | |
51 | | - | |
52 | | - | |
53 | | - | |
54 | | - | |
55 | | - | |
56 | | - | |
57 | | - | |
58 | | - | |
59 | | - | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
| 93 | + | |
| 94 | + | |
| 95 | + | |
| 96 | + | |
| 97 | + | |
| 98 | + | |
| 99 | + | |
| 100 | + | |
| 101 | + | |
| 102 | + | |
| 103 | + | |
| 104 | + | |
| 105 | + | |
| 106 | + | |
| 107 | + | |
| 108 | + | |
| 109 | + | |
| 110 | + | |
| 111 | + | |
| 112 | + | |
| 113 | + | |
| 114 | + | |
| 115 | + | |
| 116 | + | |
| 117 | + | |
| 118 | + | |
| 119 | + | |
| 120 | + | |
| 121 | + | |
| 122 | + | |
| 123 | + | |
| 124 | + | |
| 125 | + | |
| 126 | + | |
| 127 | + | |
| 128 | + | |
| 129 | + | |
| 130 | + | |
| 131 | + | |
| 132 | + | |
| 133 | + | |
| 134 | + | |
| 135 | + | |
| 136 | + | |
| 137 | + | |
| 138 | + | |
| 139 | + | |
| 140 | + | |
| 141 | + | |
| 142 | + | |
| 143 | + | |
| 144 | + | |
| 145 | + | |
| 146 | + | |
| 147 | + | |
| 148 | + | |
| 149 | + | |
| 150 | + | |
| 151 | + | |
| 152 | + | |
| 153 | + | |
| 154 | + | |
| 155 | + | |
| 156 | + | |
| 157 | + | |
| 158 | + | |
| 159 | + | |
| 160 | + | |
| 161 | + | |
| 162 | + | |
| 163 | + | |
| 164 | + | |
| 165 | + | |
| 166 | + | |
| 167 | + | |
| 168 | + | |
| 169 | + | |
| 170 | + | |
| 171 | + | |
60 | 172 | | |
61 | 173 | | |
62 | 174 | | |
| |||
71 | 183 | | |
72 | 184 | | |
73 | 185 | | |
74 | | - | |
75 | | - | |
76 | | - | |
| 186 | + | |
| 187 | + | |
| 188 | + | |
| 189 | + | |
| 190 | + | |
| 191 | + | |
77 | 192 | | |
78 | | - | |
| 193 | + | |
| 194 | + | |
| 195 | + | |
| 196 | + | |
| 197 | + | |
| 198 | + | |
| 199 | + | |
| 200 | + | |
| 201 | + | |
| 202 | + | |
| 203 | + | |
| 204 | + | |
| 205 | + | |
| 206 | + | |
| 207 | + | |
| 208 | + | |
| 209 | + | |
| 210 | + | |
| 211 | + | |
| 212 | + | |
| 213 | + | |
| 214 | + | |
| 215 | + | |
| 216 | + | |
79 | 217 | | |
| 218 | + | |
| 219 | + | |
| 220 | + | |
| 221 | + | |
0 commit comments