|
| 1 | +# SP #13: Extended Length Vectors |
| 2 | + |
| 3 | +This proposal introduces support for vectors with 0 or more than 4 components in |
| 4 | +Slang, extending the current vector type system while maintaining compatibility |
| 5 | +with existing features. |
| 6 | + |
| 7 | +## Status |
| 8 | + |
| 9 | +Status: Design Review |
| 10 | + |
| 11 | +Implementation: N/A |
| 12 | + |
| 13 | +Author: Ellie Hermaszewska |
| 14 | + |
| 15 | +Reviewer: TBD |
| 16 | + |
| 17 | +## Background |
| 18 | + |
| 19 | +Currently, Slang supports vectors between 1 and 4 components (float1, float2, |
| 20 | +float3, float4, (etc for other element types)), following HLSL conventions. |
| 21 | +This limitation stems from historical GPU hardware constraints and HLSL's |
| 22 | +graphics-focused heritage. However, modern compute applications may require |
| 23 | +working with longer vectors for tasks like machine learning, scientific |
| 24 | +computing, and select graphics tasks. |
| 25 | + |
| 26 | +Related Work |
| 27 | + |
| 28 | +- C++: std::array provides fixed-size array containers but lacks |
| 29 | + vector-specific operations. SIMD types like std::simd (C++23) support |
| 30 | + hardware-specific vector lengths. |
| 31 | + |
| 32 | +- CUDA: While CUDA doesn't provide native long vectors, libraries like Thrust |
| 33 | + implement vector abstractions. Built-in vector types are limited to 1-4 |
| 34 | + components (float1, float2, float3, float4). |
| 35 | + |
| 36 | +- OpenCL: Provides vector types up to 16 components (float2, float4, float8, |
| 37 | + float16, etc.) with full arithmetic and logical operations. |
| 38 | + |
| 39 | +- Modern CPU SIMD: Hardware support for longer vectors continues to grow: |
| 40 | + - Intel AVX-512: 512-bit vectors (16 float32s) |
| 41 | + - ARM SVE/SVE2: Scalable vector lengths up to 2048 bits |
| 42 | + - RISC-V Vector Extensions: Variable-length vector support |
| 43 | + |
| 44 | +These approaches demonstrate different strategies for handling longer vectors, |
| 45 | +from fixed-size containers to hardware-specific implementations. |
| 46 | + |
| 47 | +## Motivation |
| 48 | + |
| 49 | +The primary motivation for extended length vectors is to support mathematical |
| 50 | +operations and algorithms that naturally operate on higher-dimensional vectors. |
| 51 | +While Slang already supports arrays for data storage, certain computations |
| 52 | +specifically require vector semantics and operations that arrays don't provide. |
| 53 | + |
| 54 | +A key principle of this proposal is consistency: there is no fundamental |
| 55 | +mathematical reason to limit vectors to 4 components. While the current limit |
| 56 | +stems from graphics hardware history, modern compute applications shouldn't be |
| 57 | +constrained by this arbitrary boundary. Supporting any natural length N |
| 58 | +provides a clean, orthogonal design that follows mathematical principles rather |
| 59 | +than historical limitations. |
| 60 | + |
| 61 | +Some example use cases: |
| 62 | + |
| 63 | +- Geometric Algebra and Clifford Algebras: |
| 64 | + |
| 65 | + - 6D vectors for Plücker coordinates (representing lines in 3D space) |
| 66 | + - 6D vectors for screw theory in robotics (combining rotation and translation) |
| 67 | + - 8D vectors for dual quaternions in their vector representation |
| 68 | + - Higher-dimensional geometric products and outer products |
| 69 | + |
| 70 | +- Machine Learning: |
| 71 | + |
| 72 | + - Neural network feature vectors where vector operations (dot products, |
| 73 | + normalization) are fundamental |
| 74 | + - Distance metrics in high-dimensional embedding spaces |
| 75 | + - Principal Component Analysis with multiple components |
| 76 | + |
| 77 | +- Scientific Computing: |
| 78 | + - Spherical harmonics: Vector operations on coefficient spaces beyond 4D |
| 79 | + - Quantum computing: State vectors in higher-dimensional Hilbert spaces |
| 80 | + |
| 81 | +This extension maintains Slang's mathematical vector semantics while enabling |
| 82 | +computations that naturally operate in higher dimensions. The focus is not on |
| 83 | +data storage (which arrays already handle) but on preserving vector-specific |
| 84 | +operations and mathematical properties in higher dimensions and improving |
| 85 | +consistency in the language. |
| 86 | + |
| 87 | +## Proposed Approach |
| 88 | + |
| 89 | +We propose extending Slang's vector type system to support vectors of arbitrary |
| 90 | +length, supporting as many of the operations available to 4-vectors as |
| 91 | +possible. Hardware limitations will prohibit a complete implementation, for |
| 92 | +example certain atomic operations may not be possible on longer vectors. |
| 93 | + |
| 94 | +Key aspects: |
| 95 | + |
| 96 | +- Support vectors of any length that is a natural number, subject to the same |
| 97 | + limits as fixed-length arrays |
| 98 | +- Maintain existing syntax for vectors up to length 4 |
| 99 | +- Maintain numeric swizzling operators. |
| 100 | +- Support standard vector operations (add, multiply, etc.) |
| 101 | + |
| 102 | +## Detailed Explanation |
| 103 | + |
| 104 | +### Type System Integration |
| 105 | + |
| 106 | +There are no type system changes required, as the vector type constructor is |
| 107 | +already parameterized over arbitrary vector length. |
| 108 | + |
| 109 | +### Operations |
| 110 | + |
| 111 | +#### Component Access: |
| 112 | + |
| 113 | +```slang |
| 114 | +vector<float, 8> v; |
| 115 | +float f0 = v.x; // First component |
| 116 | +float f1 = v[1]; // Array-style access |
| 117 | +float2 f3 = v_6_7; // last two member |
| 118 | +vector<float, 8> = float2(1,2).xxxxxxxx; // extended swizzling example |
| 119 | +``` |
| 120 | + |
| 121 | +#### Arithmetic Operations: |
| 122 | + |
| 123 | +Any operations currently supported and generic over restricted vector length |
| 124 | +will be made unrestricted. |
| 125 | + |
| 126 | +Part of the scope of this work is to generate a precise list. |
| 127 | + |
| 128 | +For example all componentwise operations will be supported, as well as |
| 129 | +reductions. |
| 130 | + |
| 131 | +Cross product will remain restricted to 3-vectors, and will not be overloaded |
| 132 | +to 0-vectors or 7-vectors; this is due to worse type inference and error |
| 133 | +messages should overloads be added. |
| 134 | + |
| 135 | +#### Atomic Operations: |
| 136 | + |
| 137 | +Not supported |
| 138 | + |
| 139 | +### Storage Operations: |
| 140 | + |
| 141 | +Most platforms restrict the type of data able to be stored in textures, this |
| 142 | +proposal does not intend to work-around these restrictions. |
| 143 | + |
| 144 | +### Implementation Details |
| 145 | + |
| 146 | +Memory Layout: |
| 147 | + |
| 148 | +- Vectors are stored in contiguous memory |
| 149 | +- Alignment follows platform requirements |
| 150 | +- Padding may be required for certain lengths, for example padding to the |
| 151 | + nearest multiple of 4 |
| 152 | + |
| 153 | +Performance Considerations: |
| 154 | + |
| 155 | +- No performance degredation for 1,2,3,4-vectors |
| 156 | +- SIMD implementation work possible, not initially required |
| 157 | + |
| 158 | +## Alternatives Considered |
| 159 | + |
| 160 | +Fixed Maximum Length: |
| 161 | + |
| 162 | +- Could limit to common sizes (8, 16, 32) |
| 163 | +- Simpler implementation but less flexible |
| 164 | +- Rejected due to limiting future use cases |
| 165 | + |
| 166 | +Do nothing: |
| 167 | + |
| 168 | +- See motivation section |
| 169 | + |
| 170 | +## Additional notes |
| 171 | + |
| 172 | +### Zero-Length Vectors |
| 173 | + |
| 174 | +This proposal includes support for zero-length vectors. While seemingly |
| 175 | +unusual, zero-length vectors are mathematically well-defined and provide |
| 176 | +important completeness properties: |
| 177 | + |
| 178 | +- They are the natural result of certain slicing operations |
| 179 | +- They serve as the identity element for vector concatenation |
| 180 | + |
| 181 | +### Extended Matrices |
| 182 | + |
| 183 | +While this proposal focuses on vectors, it naturally suggests a future |
| 184 | +extension to matrices beyond the current 4x4 limit. Extended matrices would |
| 185 | +follow similar principles. |
| 186 | + |
| 187 | +However, extended matrices introduce additional considerations: |
| 188 | + |
| 189 | +- Memory layout and padding strategies for large matrices |
| 190 | +- Optimization of common operations (multiplication, transpose) |
| 191 | + |
| 192 | +These matrix-specific concerns are best addressed in a separate proposal that |
| 193 | +can build upon the extended vector foundation established here. |
0 commit comments