You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
On many architectures, aligned vector loads (e.g. loading a float4 with 16 byte alignment) is often more efficient than ordinary unaligned loads. Slang's pointer type does not encode any additional alignment info, and all pointer read/writes are by default assuming the alignment of the underlying pointee type, which is 4 bytes for float4 vectors. This means that loading from a `float4*` will result in unaligned load instructions.
16
+
17
+
This proposal attempts to provide a way for performance sensitive code to specify an aligned load/store through Slang pointers.
18
+
19
+
20
+
Proposed Approach
21
+
------------
22
+
23
+
We propose to add intrinsic functions to perform aligned load/store through a pointer:
24
+
25
+
```
26
+
T loadAligned<int alignment, T>(T* ptr);
27
+
void storeAligned<int alignment, T>(T* ptr, T value);
28
+
```
29
+
30
+
Example:
31
+
32
+
```
33
+
uniform float4* data;
34
+
35
+
[numthreads(1,1,1)]
36
+
void computeMain()
37
+
{
38
+
var v = loadAligned<8>(data);
39
+
storeAligned<16>(data+1, v);
40
+
}
41
+
```
42
+
43
+
Related Work
44
+
------------
45
+
46
+
### GLSL ###
47
+
48
+
GLSL supports the `align` layout on a `buffer_reference` block to specify the alignment of the buffer pointer.
49
+
50
+
### SPIRV ###
51
+
52
+
In SPIRV, the alignment can either be encoded as a decoration on the pointer type, or as a memory operand on the OpLoad and OpStore operations.
53
+
54
+
### Other Languages ###
55
+
56
+
Most C-like languages allow users to put additional attributes on types to specify the alignment of the type. All loads/stores through pointers of the type will use the alignment.
57
+
58
+
Instead of introducing type modifiers on data or pointer types, Slang should explicitly provide a `loadAligned` and `storeAligned` intrinsic functions to leads to `OpLoad` and `OpStore` with the `Aligned` memory operand when generating SPIRV. This way we don't have to deal with the complexity around rules of handling type coercion between modified/unmodified types and recalculate alignment for pointers representing an access chain. Developers writing performance sentisitive code can always be assured that the alignment specified on each critical load or store will be assumed, without having to work backwards through type modifications and thinking about the typing rules associated with such modifiers.
0 commit comments