wafer-lsp 0.1.13__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (44) hide show
  1. wafer_lsp/__init__.py +1 -0
  2. wafer_lsp/__main__.py +9 -0
  3. wafer_lsp/analyzers/__init__.py +0 -0
  4. wafer_lsp/analyzers/compiler_integration.py +16 -0
  5. wafer_lsp/analyzers/docs_index.py +36 -0
  6. wafer_lsp/handlers/__init__.py +30 -0
  7. wafer_lsp/handlers/code_action.py +48 -0
  8. wafer_lsp/handlers/code_lens.py +48 -0
  9. wafer_lsp/handlers/completion.py +6 -0
  10. wafer_lsp/handlers/diagnostics.py +41 -0
  11. wafer_lsp/handlers/document_symbol.py +176 -0
  12. wafer_lsp/handlers/hip_diagnostics.py +303 -0
  13. wafer_lsp/handlers/hover.py +251 -0
  14. wafer_lsp/handlers/inlay_hint.py +245 -0
  15. wafer_lsp/handlers/semantic_tokens.py +224 -0
  16. wafer_lsp/handlers/workspace_symbol.py +87 -0
  17. wafer_lsp/languages/README.md +195 -0
  18. wafer_lsp/languages/__init__.py +17 -0
  19. wafer_lsp/languages/converter.py +88 -0
  20. wafer_lsp/languages/detector.py +107 -0
  21. wafer_lsp/languages/parser_manager.py +33 -0
  22. wafer_lsp/languages/registry.py +120 -0
  23. wafer_lsp/languages/types.py +37 -0
  24. wafer_lsp/parsers/__init__.py +36 -0
  25. wafer_lsp/parsers/base_parser.py +9 -0
  26. wafer_lsp/parsers/cuda_parser.py +95 -0
  27. wafer_lsp/parsers/cutedsl_parser.py +114 -0
  28. wafer_lsp/parsers/hip_parser.py +688 -0
  29. wafer_lsp/server.py +58 -0
  30. wafer_lsp/services/__init__.py +38 -0
  31. wafer_lsp/services/analysis_service.py +22 -0
  32. wafer_lsp/services/docs_service.py +40 -0
  33. wafer_lsp/services/document_service.py +20 -0
  34. wafer_lsp/services/hip_docs.py +806 -0
  35. wafer_lsp/services/hip_hover_service.py +412 -0
  36. wafer_lsp/services/hover_service.py +237 -0
  37. wafer_lsp/services/language_registry_service.py +26 -0
  38. wafer_lsp/services/position_service.py +77 -0
  39. wafer_lsp/utils/__init__.py +0 -0
  40. wafer_lsp/utils/lsp_helpers.py +79 -0
  41. wafer_lsp-0.1.13.dist-info/METADATA +60 -0
  42. wafer_lsp-0.1.13.dist-info/RECORD +44 -0
  43. wafer_lsp-0.1.13.dist-info/WHEEL +4 -0
  44. wafer_lsp-0.1.13.dist-info/entry_points.txt +2 -0
@@ -0,0 +1,806 @@
1
+ """
2
+ HIP API Documentation Database.
3
+
4
+ Comprehensive documentation for HIP (AMD GPU) APIs, intrinsics,
5
+ memory qualifiers, and AMD architecture-specific information.
6
+
7
+ This enables rich hover documentation for developers writing HIP kernels
8
+ without needing to constantly reference external ROCm documentation.
9
+ """
10
+
11
+ from dataclasses import dataclass, field
12
+ from typing import Any
13
+
14
+
15
+ @dataclass(frozen=True)
16
+ class HIPAPIDoc:
17
+ """Documentation for a HIP API function."""
18
+ name: str
19
+ signature: str
20
+ description: str
21
+ parameters: list[tuple[str, str]] # (param_name, description)
22
+ return_value: str
23
+ amd_notes: str | None = None # AMD-specific behavior notes
24
+ example: str | None = None
25
+ related: list[str] = field(default_factory=list)
26
+ doc_url: str | None = None
27
+
28
+
29
+ @dataclass(frozen=True)
30
+ class MemoryQualifierDoc:
31
+ """Documentation for a memory qualifier."""
32
+ name: str
33
+ description: str
34
+ amd_details: str
35
+ performance_tips: str
36
+ example: str | None = None
37
+
38
+
39
+ @dataclass(frozen=True)
40
+ class IntrinsicDoc:
41
+ """Documentation for a wavefront/warp intrinsic."""
42
+ name: str
43
+ signature: str
44
+ description: str
45
+ amd_behavior: str # AMD-specific behavior (especially wavefront size)
46
+ parameters: list[tuple[str, str]]
47
+ return_value: str
48
+ example: str | None = None
49
+ cuda_equivalent: str | None = None
50
+
51
+
52
+ @dataclass(frozen=True)
53
+ class ThreadIndexDoc:
54
+ """Documentation for thread indexing variables."""
55
+ name: str
56
+ description: str
57
+ amd_context: str
58
+ common_patterns: list[str]
59
+
60
+
61
+ # ============================================================================
62
+ # HIP Runtime API Documentation
63
+ # ============================================================================
64
+
65
+ HIP_API_DOCS: dict[str, HIPAPIDoc] = {
66
+ # Memory Management
67
+ "hipMalloc": HIPAPIDoc(
68
+ name="hipMalloc",
69
+ signature="hipError_t hipMalloc(void** ptr, size_t size)",
70
+ description="Allocates device memory.",
71
+ parameters=[
72
+ ("ptr", "Pointer to allocated device memory"),
73
+ ("size", "Requested allocation size in bytes"),
74
+ ],
75
+ return_value="hipSuccess on success, or an error code on failure",
76
+ amd_notes="Memory is allocated on the currently active GPU. For multi-GPU systems, use hipSetDevice() first.",
77
+ example='float* d_data;\\nhipMalloc(&d_data, N * sizeof(float));',
78
+ related=["hipFree", "hipMallocManaged", "hipMallocAsync"],
79
+ doc_url="https://rocm.docs.amd.com/projects/HIP/en/latest/reference/hip_runtime_api.html",
80
+ ),
81
+ "hipMallocManaged": HIPAPIDoc(
82
+ name="hipMallocManaged",
83
+ signature="hipError_t hipMallocManaged(void** devPtr, size_t size, unsigned int flags = hipMemAttachGlobal)",
84
+ description="Allocates managed memory accessible from both host and device.",
85
+ parameters=[
86
+ ("devPtr", "Pointer to allocated managed memory"),
87
+ ("size", "Requested allocation size in bytes"),
88
+ ("flags", "Memory attachment flags (hipMemAttachGlobal or hipMemAttachHost)"),
89
+ ],
90
+ return_value="hipSuccess on success, or an error code on failure",
91
+ amd_notes="Unified memory on AMD GPUs uses page migration. Performance may differ from explicit memory management depending on access patterns.",
92
+ example='float* data;\\nhipMallocManaged(&data, N * sizeof(float));',
93
+ related=["hipMalloc", "hipFree", "hipMemPrefetchAsync"],
94
+ ),
95
+ "hipMallocAsync": HIPAPIDoc(
96
+ name="hipMallocAsync",
97
+ signature="hipError_t hipMallocAsync(void** devPtr, size_t size, hipStream_t stream)",
98
+ description="Allocates device memory asynchronously in a stream.",
99
+ parameters=[
100
+ ("devPtr", "Pointer to allocated device memory"),
101
+ ("size", "Requested allocation size in bytes"),
102
+ ("stream", "Stream to use for allocation"),
103
+ ],
104
+ return_value="hipSuccess on success, or an error code on failure",
105
+ amd_notes="Uses HIP's stream-ordered memory allocator for better performance in streaming workloads.",
106
+ related=["hipFreeAsync", "hipMalloc"],
107
+ ),
108
+ "hipFree": HIPAPIDoc(
109
+ name="hipFree",
110
+ signature="hipError_t hipFree(void* ptr)",
111
+ description="Frees device memory.",
112
+ parameters=[
113
+ ("ptr", "Device pointer to free"),
114
+ ],
115
+ return_value="hipSuccess on success, or an error code on failure",
116
+ amd_notes="Synchronizes the device before freeing. Use hipFreeAsync() for non-blocking free.",
117
+ related=["hipMalloc", "hipFreeAsync"],
118
+ ),
119
+ "hipFreeAsync": HIPAPIDoc(
120
+ name="hipFreeAsync",
121
+ signature="hipError_t hipFreeAsync(void* devPtr, hipStream_t stream)",
122
+ description="Frees device memory asynchronously in a stream.",
123
+ parameters=[
124
+ ("devPtr", "Device pointer to free"),
125
+ ("stream", "Stream to use for deallocation"),
126
+ ],
127
+ return_value="hipSuccess on success, or an error code on failure",
128
+ related=["hipMallocAsync", "hipFree"],
129
+ ),
130
+
131
+ # Memory Copy
132
+ "hipMemcpy": HIPAPIDoc(
133
+ name="hipMemcpy",
134
+ signature="hipError_t hipMemcpy(void* dst, const void* src, size_t sizeBytes, hipMemcpyKind kind)",
135
+ description="Copies data between host and device memory.",
136
+ parameters=[
137
+ ("dst", "Destination memory address"),
138
+ ("src", "Source memory address"),
139
+ ("sizeBytes", "Size of memory copy in bytes"),
140
+ ("kind", "Type of transfer: hipMemcpyHostToDevice, hipMemcpyDeviceToHost, hipMemcpyDeviceToDevice, hipMemcpyHostToHost"),
141
+ ],
142
+ return_value="hipSuccess on success, or an error code on failure",
143
+ amd_notes="This is a synchronous operation. For async transfers, use hipMemcpyAsync().",
144
+ example='hipMemcpy(d_data, h_data, N * sizeof(float), hipMemcpyHostToDevice);',
145
+ related=["hipMemcpyAsync", "hipMemcpy2D", "hipMemcpy3D"],
146
+ ),
147
+ "hipMemcpyAsync": HIPAPIDoc(
148
+ name="hipMemcpyAsync",
149
+ signature="hipError_t hipMemcpyAsync(void* dst, const void* src, size_t sizeBytes, hipMemcpyKind kind, hipStream_t stream)",
150
+ description="Copies data between host and device memory asynchronously.",
151
+ parameters=[
152
+ ("dst", "Destination memory address"),
153
+ ("src", "Source memory address"),
154
+ ("sizeBytes", "Size of memory copy in bytes"),
155
+ ("kind", "Type of transfer"),
156
+ ("stream", "Stream for async execution"),
157
+ ],
158
+ return_value="hipSuccess on success, or an error code on failure",
159
+ amd_notes="For best performance with async copies, use pinned host memory allocated with hipHostMalloc().",
160
+ related=["hipMemcpy", "hipHostMalloc"],
161
+ ),
162
+ "hipMemset": HIPAPIDoc(
163
+ name="hipMemset",
164
+ signature="hipError_t hipMemset(void* dst, int value, size_t sizeBytes)",
165
+ description="Fills device memory with a constant byte value.",
166
+ parameters=[
167
+ ("dst", "Pointer to device memory"),
168
+ ("value", "Value to set (only the lowest byte is used)"),
169
+ ("sizeBytes", "Number of bytes to set"),
170
+ ],
171
+ return_value="hipSuccess on success, or an error code on failure",
172
+ related=["hipMemsetAsync", "hipMemset2D"],
173
+ ),
174
+ "hipMemsetAsync": HIPAPIDoc(
175
+ name="hipMemsetAsync",
176
+ signature="hipError_t hipMemsetAsync(void* dst, int value, size_t sizeBytes, hipStream_t stream)",
177
+ description="Fills device memory with a constant byte value asynchronously.",
178
+ parameters=[
179
+ ("dst", "Pointer to device memory"),
180
+ ("value", "Value to set (only the lowest byte is used)"),
181
+ ("sizeBytes", "Number of bytes to set"),
182
+ ("stream", "Stream for async execution"),
183
+ ],
184
+ return_value="hipSuccess on success, or an error code on failure",
185
+ related=["hipMemset"],
186
+ ),
187
+
188
+ # Pinned Memory
189
+ "hipHostMalloc": HIPAPIDoc(
190
+ name="hipHostMalloc",
191
+ signature="hipError_t hipHostMalloc(void** ptr, size_t size, unsigned int flags = hipHostMallocDefault)",
192
+ description="Allocates pinned (page-locked) host memory.",
193
+ parameters=[
194
+ ("ptr", "Pointer to allocated host memory"),
195
+ ("size", "Requested allocation size in bytes"),
196
+ ("flags", "Allocation flags (hipHostMallocDefault, hipHostMallocPortable, hipHostMallocMapped, hipHostMallocWriteCombined)"),
197
+ ],
198
+ return_value="hipSuccess on success, or an error code on failure",
199
+ amd_notes="Pinned memory enables faster DMA transfers and is required for async memory copies. Use sparingly as it reduces available system memory.",
200
+ example='float* h_pinned;\\nhipHostMalloc(&h_pinned, N * sizeof(float), hipHostMallocDefault);',
201
+ related=["hipHostFree", "hipMemcpyAsync"],
202
+ ),
203
+ "hipHostFree": HIPAPIDoc(
204
+ name="hipHostFree",
205
+ signature="hipError_t hipHostFree(void* ptr)",
206
+ description="Frees pinned host memory.",
207
+ parameters=[
208
+ ("ptr", "Host pointer to free"),
209
+ ],
210
+ return_value="hipSuccess on success, or an error code on failure",
211
+ related=["hipHostMalloc"],
212
+ ),
213
+
214
+ # Synchronization
215
+ "hipDeviceSynchronize": HIPAPIDoc(
216
+ name="hipDeviceSynchronize",
217
+ signature="hipError_t hipDeviceSynchronize(void)",
218
+ description="Blocks until the device has completed all preceding requested tasks.",
219
+ parameters=[],
220
+ return_value="hipSuccess on success, or an error code on failure",
221
+ amd_notes="This is a heavy synchronization point. For stream-level sync, use hipStreamSynchronize(). For event-based sync, use hipEventSynchronize().",
222
+ related=["hipStreamSynchronize", "hipEventSynchronize"],
223
+ ),
224
+ "hipStreamSynchronize": HIPAPIDoc(
225
+ name="hipStreamSynchronize",
226
+ signature="hipError_t hipStreamSynchronize(hipStream_t stream)",
227
+ description="Blocks until the stream has completed all operations.",
228
+ parameters=[
229
+ ("stream", "Stream to synchronize"),
230
+ ],
231
+ return_value="hipSuccess on success, or an error code on failure",
232
+ amd_notes="More efficient than hipDeviceSynchronize() when only one stream needs to complete.",
233
+ related=["hipDeviceSynchronize", "hipStreamWaitEvent"],
234
+ ),
235
+
236
+ # Streams
237
+ "hipStreamCreate": HIPAPIDoc(
238
+ name="hipStreamCreate",
239
+ signature="hipError_t hipStreamCreate(hipStream_t* stream)",
240
+ description="Creates a new asynchronous stream.",
241
+ parameters=[
242
+ ("stream", "Pointer to new stream"),
243
+ ],
244
+ return_value="hipSuccess on success, or an error code on failure",
245
+ amd_notes="AMD GPUs can execute multiple streams concurrently on different compute units. Use streams for overlapping compute and memory transfers.",
246
+ related=["hipStreamDestroy", "hipStreamCreateWithFlags"],
247
+ ),
248
+ "hipStreamDestroy": HIPAPIDoc(
249
+ name="hipStreamDestroy",
250
+ signature="hipError_t hipStreamDestroy(hipStream_t stream)",
251
+ description="Destroys an asynchronous stream.",
252
+ parameters=[
253
+ ("stream", "Stream to destroy"),
254
+ ],
255
+ return_value="hipSuccess on success, or an error code on failure",
256
+ related=["hipStreamCreate"],
257
+ ),
258
+
259
+ # Events
260
+ "hipEventCreate": HIPAPIDoc(
261
+ name="hipEventCreate",
262
+ signature="hipError_t hipEventCreate(hipEvent_t* event)",
263
+ description="Creates an event.",
264
+ parameters=[
265
+ ("event", "Pointer to new event"),
266
+ ],
267
+ return_value="hipSuccess on success, or an error code on failure",
268
+ related=["hipEventDestroy", "hipEventRecord", "hipEventSynchronize"],
269
+ ),
270
+ "hipEventRecord": HIPAPIDoc(
271
+ name="hipEventRecord",
272
+ signature="hipError_t hipEventRecord(hipEvent_t event, hipStream_t stream)",
273
+ description="Records an event in a stream.",
274
+ parameters=[
275
+ ("event", "Event to record"),
276
+ ("stream", "Stream in which to record (0 for default stream)"),
277
+ ],
278
+ return_value="hipSuccess on success, or an error code on failure",
279
+ related=["hipEventCreate", "hipEventElapsedTime"],
280
+ ),
281
+ "hipEventSynchronize": HIPAPIDoc(
282
+ name="hipEventSynchronize",
283
+ signature="hipError_t hipEventSynchronize(hipEvent_t event)",
284
+ description="Blocks until the event has been recorded.",
285
+ parameters=[
286
+ ("event", "Event to wait for"),
287
+ ],
288
+ return_value="hipSuccess on success, or an error code on failure",
289
+ related=["hipEventRecord", "hipStreamWaitEvent"],
290
+ ),
291
+ "hipEventElapsedTime": HIPAPIDoc(
292
+ name="hipEventElapsedTime",
293
+ signature="hipError_t hipEventElapsedTime(float* ms, hipEvent_t start, hipEvent_t stop)",
294
+ description="Computes the elapsed time between two events.",
295
+ parameters=[
296
+ ("ms", "Pointer to elapsed time in milliseconds"),
297
+ ("start", "Starting event"),
298
+ ("stop", "Ending event"),
299
+ ],
300
+ return_value="hipSuccess on success, or an error code on failure",
301
+ example='hipEventRecord(start, stream);\\nkernel<<<...>>>();\\nhipEventRecord(stop, stream);\\nhipEventSynchronize(stop);\\nfloat ms;\\nhipEventElapsedTime(&ms, start, stop);',
302
+ related=["hipEventCreate", "hipEventRecord"],
303
+ ),
304
+
305
+ # Device Management
306
+ "hipSetDevice": HIPAPIDoc(
307
+ name="hipSetDevice",
308
+ signature="hipError_t hipSetDevice(int deviceId)",
309
+ description="Sets the current device for the calling thread.",
310
+ parameters=[
311
+ ("deviceId", "Device ID to use"),
312
+ ],
313
+ return_value="hipSuccess on success, or an error code on failure",
314
+ amd_notes="Required before allocating memory in multi-GPU setups. Each thread can have its own active device.",
315
+ related=["hipGetDevice", "hipGetDeviceCount"],
316
+ ),
317
+ "hipGetDevice": HIPAPIDoc(
318
+ name="hipGetDevice",
319
+ signature="hipError_t hipGetDevice(int* deviceId)",
320
+ description="Gets the current device for the calling thread.",
321
+ parameters=[
322
+ ("deviceId", "Pointer to device ID"),
323
+ ],
324
+ return_value="hipSuccess on success, or an error code on failure",
325
+ related=["hipSetDevice"],
326
+ ),
327
+ "hipGetDeviceCount": HIPAPIDoc(
328
+ name="hipGetDeviceCount",
329
+ signature="hipError_t hipGetDeviceCount(int* count)",
330
+ description="Returns the number of available HIP devices.",
331
+ parameters=[
332
+ ("count", "Pointer to device count"),
333
+ ],
334
+ return_value="hipSuccess on success, or an error code on failure",
335
+ related=["hipSetDevice", "hipGetDeviceProperties"],
336
+ ),
337
+ "hipGetDeviceProperties": HIPAPIDoc(
338
+ name="hipGetDeviceProperties",
339
+ signature="hipError_t hipGetDeviceProperties(hipDeviceProp_t* prop, int deviceId)",
340
+ description="Returns device properties.",
341
+ parameters=[
342
+ ("prop", "Pointer to device properties struct"),
343
+ ("deviceId", "Device ID"),
344
+ ],
345
+ return_value="hipSuccess on success, or an error code on failure",
346
+ amd_notes="Key properties for AMD GPUs: warpSize (64 for CDNA), maxSharedMemoryPerBlock, multiProcessorCount (compute units), gcnArchName.",
347
+ related=["hipGetDeviceCount"],
348
+ ),
349
+
350
+ # Kernel Launch
351
+ "hipLaunchKernelGGL": HIPAPIDoc(
352
+ name="hipLaunchKernelGGL",
353
+ signature="void hipLaunchKernelGGL(F kernel, dim3 gridDim, dim3 blockDim, size_t sharedMem, hipStream_t stream, Args... args)",
354
+ description="Launches a kernel using HIP's macro syntax.",
355
+ parameters=[
356
+ ("kernel", "Kernel function to launch"),
357
+ ("gridDim", "Grid dimensions (number of blocks)"),
358
+ ("blockDim", "Block dimensions (threads per block)"),
359
+ ("sharedMem", "Dynamic shared memory size in bytes"),
360
+ ("stream", "Stream for async execution (0 for default)"),
361
+ ("args", "Kernel arguments"),
362
+ ],
363
+ return_value="void",
364
+ amd_notes="Alternative to <<<>>> syntax. Provides better compile-time checking and is the native HIP way to launch kernels.",
365
+ example='hipLaunchKernelGGL(myKernel, dim3(gridSize), dim3(blockSize), 0, stream, arg1, arg2);',
366
+ related=["hipLaunchCooperativeKernel"],
367
+ ),
368
+
369
+ # Error Handling
370
+ "hipGetLastError": HIPAPIDoc(
371
+ name="hipGetLastError",
372
+ signature="hipError_t hipGetLastError(void)",
373
+ description="Returns the last error from a HIP runtime call and resets it to hipSuccess.",
374
+ parameters=[],
375
+ return_value="Last error code",
376
+ amd_notes="Use after kernel launches to check for launch errors. Kernel launches are asynchronous, so errors may appear on subsequent sync operations.",
377
+ related=["hipPeekAtLastError", "hipGetErrorString"],
378
+ ),
379
+ "hipGetErrorString": HIPAPIDoc(
380
+ name="hipGetErrorString",
381
+ signature="const char* hipGetErrorString(hipError_t error)",
382
+ description="Returns a string description of an error code.",
383
+ parameters=[
384
+ ("error", "HIP error code"),
385
+ ],
386
+ return_value="Pointer to error string",
387
+ example='hipError_t err = hipGetLastError();\\nif (err != hipSuccess) printf("Error: %s\\n", hipGetErrorString(err));',
388
+ related=["hipGetLastError"],
389
+ ),
390
+ }
391
+
392
+ # ============================================================================
393
+ # Memory Qualifier Documentation
394
+ # ============================================================================
395
+
396
+ MEMORY_QUALIFIER_DOCS: dict[str, MemoryQualifierDoc] = {
397
+ "__device__": MemoryQualifierDoc(
398
+ name="__device__",
399
+ description="Declares a variable that resides in device (global) memory, or marks a function as callable only from device code.",
400
+ amd_details="On AMD GPUs, __device__ variables reside in HBM (High Bandwidth Memory) with ~1-2 TB/s bandwidth on MI300X. Access latency is ~400-600 cycles.",
401
+ performance_tips="• Coalesce accesses: adjacent threads should access adjacent memory\\n• Use vectorized loads (float4, etc.) when possible\\n• Consider __shared__ for frequently accessed data",
402
+ example='__device__ float global_array[1024];\\n\\n__device__ float helper_function(float x) {\\n return x * x;\\n}',
403
+ ),
404
+ "__shared__": MemoryQualifierDoc(
405
+ name="__shared__",
406
+ description="Declares a variable in Local Data Share (LDS) memory, shared by all threads in a block.",
407
+ amd_details="On AMD CDNA GPUs:\\n• LDS is on-chip memory with ~100x lower latency than global memory\\n• 64 KB per Compute Unit (shared across all active wavefronts)\\n• Organized in 32 banks of 4 bytes each\\n• Shared across all 64 threads in a wavefront",
408
+ performance_tips="• Avoid bank conflicts by having threads access different banks\\n• Use padding for arrays: float data[32][33] instead of float data[32][32]\\n• LDS is a limited resource - high usage reduces occupancy",
409
+ example='__shared__ float tile[BLOCK_SIZE][BLOCK_SIZE];\\n\\n// With padding to avoid bank conflicts\\n__shared__ float tile_padded[BLOCK_SIZE][BLOCK_SIZE + 1];',
410
+ ),
411
+ "__constant__": MemoryQualifierDoc(
412
+ name="__constant__",
413
+ description="Declares a variable in constant memory, which is cached and read-only from device code.",
414
+ amd_details="On AMD GPUs, constant memory is cached in the L1/L2 cache hierarchy. All threads reading the same address get the data broadcast efficiently.",
415
+ performance_tips="• Best for data that all threads read (e.g., coefficients, lookup tables)\\n• Limited to 64 KB total\\n• Writes must happen from host before kernel launch",
416
+ example='__constant__ float filter_coefficients[256];',
417
+ ),
418
+ "__global__": MemoryQualifierDoc(
419
+ name="__global__",
420
+ description="Declares a function as a GPU kernel that can be launched from host code.",
421
+ amd_details="On AMD GPUs, kernels are compiled to AMDGPU ISA and executed on Compute Units. Each CU has 64 SIMD lanes (wavefront size of 64 threads).",
422
+ performance_tips="• Use __launch_bounds__ to hint the compiler about resource usage\\n• Consider grid-stride loops for flexibility\\n• Profile with rocprof to identify bottlenecks",
423
+ example='__global__ void vector_add(float* a, float* b, float* c, int n) {\\n int idx = blockIdx.x * blockDim.x + threadIdx.x;\\n if (idx < n) c[idx] = a[idx] + b[idx];\\n}',
424
+ ),
425
+ "__host__": MemoryQualifierDoc(
426
+ name="__host__",
427
+ description="Declares a function as callable from host code. This is the default for functions.",
428
+ amd_details="Can be combined with __device__ to compile a function for both host and device.",
429
+ performance_tips="Use __host__ __device__ for utility functions that work on both CPU and GPU.",
430
+ example='__host__ __device__ float utility_func(float x) {\\n return sqrtf(x);\\n}',
431
+ ),
432
+ "__launch_bounds__": MemoryQualifierDoc(
433
+ name="__launch_bounds__",
434
+ description="Specifies the maximum number of threads per block and optionally minimum blocks per multiprocessor.",
435
+ amd_details="Helps the AMD GPU compiler optimize register allocation. On CDNA, each CU has 65536 32-bit registers shared among wavefronts.",
436
+ performance_tips="• Set maxThreadsPerBlock to your actual block size\\n• Higher minBlocksPerMultiprocessor increases occupancy but limits registers per thread\\n• Profile different configurations to find optimal settings",
437
+ example='__global__ __launch_bounds__(256, 4)\\nvoid my_kernel(...) {\\n // Max 256 threads/block, at least 4 blocks/CU\\n}',
438
+ ),
439
+ "__restrict__": MemoryQualifierDoc(
440
+ name="__restrict__",
441
+ description="Hints that a pointer is the only way to access the pointed-to memory (no aliasing).",
442
+ amd_details="Enables the compiler to perform more aggressive optimizations by assuming no pointer aliasing.",
443
+ performance_tips="• Use on kernel parameters when pointers don't overlap\\n• Can significantly improve memory access scheduling",
444
+ example='__global__ void kernel(float* __restrict__ input, float* __restrict__ output) {\\n // Compiler knows input and output don\'t overlap\\n}',
445
+ ),
446
+ }
447
+
448
+ # ============================================================================
449
+ # Wavefront Intrinsic Documentation
450
+ # ============================================================================
451
+
452
+ INTRINSIC_DOCS: dict[str, IntrinsicDoc] = {
453
+ # Shuffle operations
454
+ "__shfl": IntrinsicDoc(
455
+ name="__shfl",
456
+ signature="T __shfl(T var, int srcLane, int width = warpSize)",
457
+ description="Reads a variable from another lane in the wavefront.",
458
+ amd_behavior="On AMD GPUs, operates on 64-thread wavefronts (CDNA) or 32/64 (RDNA). The width parameter defaults to 64. No mask parameter - all lanes participate.",
459
+ parameters=[
460
+ ("var", "Value to share"),
461
+ ("srcLane", "Lane to read from (0-63 on CDNA)"),
462
+ ("width", "Logical wavefront width (default: warpSize = 64)"),
463
+ ],
464
+ return_value="Value from the source lane",
465
+ example='// Broadcast lane 0 to all lanes\\nfloat val = __shfl(my_val, 0);',
466
+ cuda_equivalent="__shfl_sync(0xFFFFFFFFFFFFFFFF, var, srcLane, width)",
467
+ ),
468
+ "__shfl_down": IntrinsicDoc(
469
+ name="__shfl_down",
470
+ signature="T __shfl_down(T var, unsigned int delta, int width = warpSize)",
471
+ description="Reads a variable from a lane with higher index (lane + delta).",
472
+ amd_behavior="On AMD CDNA, operates on 64-thread wavefronts. Common for reductions: each lane reads from lane + delta. For warp reduction, loop from delta=32 down to delta=1.",
473
+ parameters=[
474
+ ("var", "Value to share"),
475
+ ("delta", "Offset to add to current lane ID"),
476
+ ("width", "Logical wavefront width (default: 64)"),
477
+ ],
478
+ return_value="Value from lane (current_lane + delta)",
479
+ example='// Wavefront reduction\\nfor (int offset = 32; offset > 0; offset >>= 1) {\\n val += __shfl_down(val, offset);\\n}',
480
+ cuda_equivalent="__shfl_down_sync(mask, var, delta, width)",
481
+ ),
482
+ "__shfl_up": IntrinsicDoc(
483
+ name="__shfl_up",
484
+ signature="T __shfl_up(T var, unsigned int delta, int width = warpSize)",
485
+ description="Reads a variable from a lane with lower index (lane - delta).",
486
+ amd_behavior="On AMD CDNA, operates on 64-thread wavefronts. Useful for prefix sums (scan operations).",
487
+ parameters=[
488
+ ("var", "Value to share"),
489
+ ("delta", "Offset to subtract from current lane ID"),
490
+ ("width", "Logical wavefront width (default: 64)"),
491
+ ],
492
+ return_value="Value from lane (current_lane - delta), or own value if delta > current_lane",
493
+ cuda_equivalent="__shfl_up_sync(mask, var, delta, width)",
494
+ ),
495
+ "__shfl_xor": IntrinsicDoc(
496
+ name="__shfl_xor",
497
+ signature="T __shfl_xor(T var, int laneMask, int width = warpSize)",
498
+ description="Reads a variable from a lane determined by XOR of current lane and mask.",
499
+ amd_behavior="On AMD CDNA, operates on 64-thread wavefronts. Used for butterfly reductions and pair exchanges.",
500
+ parameters=[
501
+ ("var", "Value to share"),
502
+ ("laneMask", "XOR mask applied to lane ID"),
503
+ ("width", "Logical wavefront width (default: 64)"),
504
+ ],
505
+ return_value="Value from lane (current_lane XOR laneMask)",
506
+ example='// Butterfly reduction\\nfor (int mask = 32; mask > 0; mask >>= 1) {\\n val += __shfl_xor(val, mask);\\n}',
507
+ cuda_equivalent="__shfl_xor_sync(mask, var, laneMask, width)",
508
+ ),
509
+
510
+ # Ballot and vote operations
511
+ "__ballot": IntrinsicDoc(
512
+ name="__ballot",
513
+ signature="uint64_t __ballot(int predicate)",
514
+ description="Returns a bitmask where bit N is set if lane N's predicate is non-zero.",
515
+ amd_behavior="**CRITICAL**: On AMD CDNA, returns uint64_t (64 bits for 64 threads). On CUDA, __ballot_sync returns uint32_t. Code comparing to 0xFFFFFFFF will break!",
516
+ parameters=[
517
+ ("predicate", "Non-zero value sets the corresponding bit"),
518
+ ],
519
+ return_value="64-bit mask on AMD (uint64_t), 32-bit on NVIDIA (unsigned int)",
520
+ example='uint64_t active = __ballot(threadIdx.x < limit);\\n// Check if all lanes are active:\\nif (active == 0xFFFFFFFFFFFFFFFFull) { ... }',
521
+ cuda_equivalent="__ballot_sync(mask, predicate) - returns unsigned int",
522
+ ),
523
+ "__any": IntrinsicDoc(
524
+ name="__any",
525
+ signature="int __any(int predicate)",
526
+ description="Returns non-zero if any lane's predicate is non-zero.",
527
+ amd_behavior="On AMD CDNA, checks all 64 lanes in the wavefront.",
528
+ parameters=[
529
+ ("predicate", "Condition to test"),
530
+ ],
531
+ return_value="Non-zero if any lane's predicate is true",
532
+ example='if (__any(idx < boundary)) {\\n // At least one thread needs to do work\\n}',
533
+ cuda_equivalent="__any_sync(mask, predicate)",
534
+ ),
535
+ "__all": IntrinsicDoc(
536
+ name="__all",
537
+ signature="int __all(int predicate)",
538
+ description="Returns non-zero if all lanes' predicates are non-zero.",
539
+ amd_behavior="On AMD CDNA, checks all 64 lanes in the wavefront.",
540
+ parameters=[
541
+ ("predicate", "Condition to test"),
542
+ ],
543
+ return_value="Non-zero if all lanes' predicates are true",
544
+ example='if (__all(idx < N)) {\\n // All threads have valid indices, no bounds check needed\\n}',
545
+ cuda_equivalent="__all_sync(mask, predicate)",
546
+ ),
547
+ "__activemask": IntrinsicDoc(
548
+ name="__activemask",
549
+ signature="uint64_t __activemask()",
550
+ description="Returns a mask of all currently active lanes.",
551
+ amd_behavior="Returns uint64_t on AMD CDNA (64 bits for 64 threads). Use __popcll() to count active lanes.",
552
+ parameters=[],
553
+ return_value="Bitmask of active lanes (64-bit on AMD)",
554
+ example='uint64_t active = __activemask();\\nint num_active = __popcll(active);',
555
+ cuda_equivalent="__activemask() - but returns 32-bit on NVIDIA",
556
+ ),
557
+
558
+ # Bit counting
559
+ "__popc": IntrinsicDoc(
560
+ name="__popc",
561
+ signature="int __popc(unsigned int x)",
562
+ description="Counts the number of set bits (population count) in a 32-bit integer.",
563
+ amd_behavior="Maps directly to AMD GPU VALU instruction.",
564
+ parameters=[
565
+ ("x", "32-bit value to count bits in"),
566
+ ],
567
+ return_value="Number of set bits (0-32)",
568
+ cuda_equivalent="__popc(x)",
569
+ ),
570
+ "__popcll": IntrinsicDoc(
571
+ name="__popcll",
572
+ signature="int __popcll(unsigned long long x)",
573
+ description="Counts the number of set bits in a 64-bit integer.",
574
+ amd_behavior="Essential for counting active lanes from __ballot() on AMD (which returns 64-bit).",
575
+ parameters=[
576
+ ("x", "64-bit value to count bits in"),
577
+ ],
578
+ return_value="Number of set bits (0-64)",
579
+ example='uint64_t mask = __ballot(predicate);\\nint count = __popcll(mask); // Count how many lanes match',
580
+ cuda_equivalent="__popcll(x)",
581
+ ),
582
+ "__ffs": IntrinsicDoc(
583
+ name="__ffs",
584
+ signature="int __ffs(int x)",
585
+ description="Find first set bit (1-indexed from LSB). Returns 0 if no bit is set.",
586
+ amd_behavior="Maps directly to AMD GPU instruction.",
587
+ parameters=[
588
+ ("x", "Value to search"),
589
+ ],
590
+ return_value="Position of first set bit (1-32) or 0 if none",
591
+ cuda_equivalent="__ffs(x)",
592
+ ),
593
+ "__ffsll": IntrinsicDoc(
594
+ name="__ffsll",
595
+ signature="int __ffsll(long long x)",
596
+ description="Find first set bit in 64-bit integer (1-indexed from LSB).",
597
+ amd_behavior="Useful for finding first active lane from __ballot() result on AMD.",
598
+ parameters=[
599
+ ("x", "64-bit value to search"),
600
+ ],
601
+ return_value="Position of first set bit (1-64) or 0 if none",
602
+ cuda_equivalent="__ffsll(x)",
603
+ ),
604
+ "__clz": IntrinsicDoc(
605
+ name="__clz",
606
+ signature="int __clz(int x)",
607
+ description="Count leading zeros in a 32-bit integer.",
608
+ amd_behavior="Maps directly to AMD GPU instruction.",
609
+ parameters=[
610
+ ("x", "Value to count leading zeros in"),
611
+ ],
612
+ return_value="Number of leading zero bits (0-32)",
613
+ cuda_equivalent="__clz(x)",
614
+ ),
615
+
616
+ # Synchronization
617
+ "__syncthreads": IntrinsicDoc(
618
+ name="__syncthreads",
619
+ signature="void __syncthreads()",
620
+ description="Synchronizes all threads in a block. All threads must reach this point before any continue.",
621
+ amd_behavior="On AMD, this is a barrier for all wavefronts in the workgroup. Wavefronts execute in lockstep, but workgroup barriers ensure all wavefronts sync.",
622
+ parameters=[],
623
+ return_value="void",
624
+ example='// Load data to shared memory\\nshared_data[threadIdx.x] = global_data[idx];\\n__syncthreads();\\n// Now all threads see the complete shared data\\nuse_shared_data(shared_data);',
625
+ cuda_equivalent="__syncthreads()",
626
+ ),
627
+ "__syncwarp": IntrinsicDoc(
628
+ name="__syncwarp",
629
+ signature="void __syncwarp(uint64_t mask = 0xFFFFFFFFFFFFFFFF)",
630
+ description="Synchronizes threads within a wavefront.",
631
+ amd_behavior="On AMD CDNA, wavefronts execute in lockstep (SIMD), so this is typically a no-op. However, it still provides a memory fence.",
632
+ parameters=[
633
+ ("mask", "Bitmask of participating lanes (64-bit on AMD)"),
634
+ ],
635
+ return_value="void",
636
+ cuda_equivalent="__syncwarp(mask) - with 32-bit mask on NVIDIA",
637
+ ),
638
+
639
+ # Atomic operations
640
+ "atomicAdd": IntrinsicDoc(
641
+ name="atomicAdd",
642
+ signature="T atomicAdd(T* address, T val)",
643
+ description="Atomically adds val to *address and returns the old value.",
644
+ amd_behavior="AMD GPUs have hardware atomic units. Performance depends on contention - use hierarchical atomics (warp-level reduction then global atomic) for best performance.",
645
+ parameters=[
646
+ ("address", "Pointer to value to update"),
647
+ ("val", "Value to add"),
648
+ ],
649
+ return_value="Original value at address before the add",
650
+ example='// Histogram update\\natomicAdd(&histogram[bin], 1);\\n\\n// Warp-level reduction then atomic (better performance)\\nfloat sum = warp_reduce(local_val);\\nif (lane_id == 0) atomicAdd(global_sum, sum);',
651
+ cuda_equivalent="atomicAdd(address, val)",
652
+ ),
653
+ "atomicCAS": IntrinsicDoc(
654
+ name="atomicCAS",
655
+ signature="T atomicCAS(T* address, T compare, T val)",
656
+ description="Compare-and-swap: if *address == compare, set *address = val. Returns original value.",
657
+ amd_behavior="Fundamental atomic for implementing lock-free data structures. AMD GPUs support 32-bit and 64-bit CAS.",
658
+ parameters=[
659
+ ("address", "Pointer to value to update"),
660
+ ("compare", "Expected value"),
661
+ ("val", "New value if comparison succeeds"),
662
+ ],
663
+ return_value="Original value at address (compare to see if CAS succeeded)",
664
+ example='// Lock-free update\\nunsigned int old, assumed, new_val;\\nold = *addr;\\ndo {\\n assumed = old;\\n new_val = compute_new(assumed);\\n old = atomicCAS(addr, assumed, new_val);\\n} while (assumed != old);',
665
+ cuda_equivalent="atomicCAS(address, compare, val)",
666
+ ),
667
+ "atomicMax": IntrinsicDoc(
668
+ name="atomicMax",
669
+ signature="T atomicMax(T* address, T val)",
670
+ description="Atomically computes max(*address, val) and stores the result.",
671
+ amd_behavior="Supported for int, unsigned int, long long, unsigned long long.",
672
+ parameters=[
673
+ ("address", "Pointer to value to update"),
674
+ ("val", "Value to compare"),
675
+ ],
676
+ return_value="Original value at address",
677
+ cuda_equivalent="atomicMax(address, val)",
678
+ ),
679
+ "atomicMin": IntrinsicDoc(
680
+ name="atomicMin",
681
+ signature="T atomicMin(T* address, T val)",
682
+ description="Atomically computes min(*address, val) and stores the result.",
683
+ amd_behavior="Supported for int, unsigned int, long long, unsigned long long.",
684
+ parameters=[
685
+ ("address", "Pointer to value to update"),
686
+ ("val", "Value to compare"),
687
+ ],
688
+ return_value="Original value at address",
689
+ cuda_equivalent="atomicMin(address, val)",
690
+ ),
691
+ "atomicExch": IntrinsicDoc(
692
+ name="atomicExch",
693
+ signature="T atomicExch(T* address, T val)",
694
+ description="Atomically exchanges *address with val and returns the old value.",
695
+ amd_behavior="Efficient atomic swap operation.",
696
+ parameters=[
697
+ ("address", "Pointer to value to update"),
698
+ ("val", "New value"),
699
+ ],
700
+ return_value="Original value at address",
701
+ cuda_equivalent="atomicExch(address, val)",
702
+ ),
703
+ }
704
+
705
+ # ============================================================================
706
+ # Thread Indexing Documentation
707
+ # ============================================================================
708
+
709
+ THREAD_INDEX_DOCS: dict[str, ThreadIndexDoc] = {
710
+ "threadIdx": ThreadIndexDoc(
711
+ name="threadIdx",
712
+ description="Built-in variable containing the thread's index within its block (uint3 with .x, .y, .z components).",
713
+ amd_context="On AMD CDNA GPUs, threads are grouped into 64-thread wavefronts. threadIdx.x ranges from 0 to blockDim.x-1. The first 64 threads (threadIdx.x = 0-63) form wavefront 0, etc.",
714
+ common_patterns=[
715
+ "Global index: int idx = blockIdx.x * blockDim.x + threadIdx.x;",
716
+ "Lane within wavefront: int lane = threadIdx.x % warpSize; // warpSize is 64 on AMD",
717
+ "Wavefront within block: int warp_id = threadIdx.x / warpSize;",
718
+ "2D indexing: int row = blockIdx.y * blockDim.y + threadIdx.y;",
719
+ ],
720
+ ),
721
+ "blockIdx": ThreadIndexDoc(
722
+ name="blockIdx",
723
+ description="Built-in variable containing the block's index within the grid (uint3 with .x, .y, .z components).",
724
+ amd_context="Blocks are distributed across AMD GPU Compute Units. Multiple blocks can run concurrently on a CU depending on resource usage.",
725
+ common_patterns=[
726
+ "Global thread index: int idx = blockIdx.x * blockDim.x + threadIdx.x;",
727
+ "Block ID for 2D grid: int block_id = blockIdx.y * gridDim.x + blockIdx.x;",
728
+ ],
729
+ ),
730
+ "blockDim": ThreadIndexDoc(
731
+ name="blockDim",
732
+ description="Built-in variable containing the dimensions of the block (dim3 with .x, .y, .z components).",
733
+ amd_context="On AMD GPUs, blockDim.x * blockDim.y * blockDim.z should ideally be a multiple of 64 (wavefront size) for best occupancy.",
734
+ common_patterns=[
735
+ "Total threads in block: int block_size = blockDim.x * blockDim.y * blockDim.z;",
736
+ "Linear thread ID in block: int tid = threadIdx.z * blockDim.x * blockDim.y + threadIdx.y * blockDim.x + threadIdx.x;",
737
+ ],
738
+ ),
739
+ "gridDim": ThreadIndexDoc(
740
+ name="gridDim",
741
+ description="Built-in variable containing the dimensions of the grid (dim3 with .x, .y, .z components).",
742
+ amd_context="Total grid dimensions. For best GPU utilization, launch enough blocks to saturate all Compute Units.",
743
+ common_patterns=[
744
+ "Grid-stride loop: for (int i = idx; i < N; i += gridDim.x * blockDim.x) { ... }",
745
+ "Total blocks: int total_blocks = gridDim.x * gridDim.y * gridDim.z;",
746
+ ],
747
+ ),
748
+ "warpSize": ThreadIndexDoc(
749
+ name="warpSize",
750
+ description="Built-in constant for the wavefront/warp size. Use this instead of hard-coding 32 or 64.",
751
+ amd_context="On AMD CDNA GPUs (MI100, MI200, MI300), warpSize is 64. On AMD RDNA GPUs, it can be 32 or 64. Always use warpSize for portable code.",
752
+ common_patterns=[
753
+ "Lane ID: int lane_id = threadIdx.x % warpSize;",
754
+ "Warp ID: int warp_id = threadIdx.x / warpSize;",
755
+ "Number of warps in block: int num_warps = (blockDim.x + warpSize - 1) / warpSize;",
756
+ ],
757
+ ),
758
+ }
759
+
760
+
761
+ # ============================================================================
762
+ # HIP Docs Service
763
+ # ============================================================================
764
+
765
+ class HIPDocsService:
766
+ """Service for looking up HIP API documentation."""
767
+
768
+ def get_api_doc(self, name: str) -> HIPAPIDoc | None:
769
+ """Get documentation for a HIP API function."""
770
+ return HIP_API_DOCS.get(name)
771
+
772
+ def get_memory_qualifier_doc(self, name: str) -> MemoryQualifierDoc | None:
773
+ """Get documentation for a memory qualifier."""
774
+ return MEMORY_QUALIFIER_DOCS.get(name)
775
+
776
+ def get_intrinsic_doc(self, name: str) -> IntrinsicDoc | None:
777
+ """Get documentation for a wavefront intrinsic."""
778
+ return INTRINSIC_DOCS.get(name)
779
+
780
+ def get_thread_index_doc(self, name: str) -> ThreadIndexDoc | None:
781
+ """Get documentation for a thread indexing variable."""
782
+ # Handle both "threadIdx" and "threadIdx.x" style
783
+ base_name = name.split('.')[0]
784
+ return THREAD_INDEX_DOCS.get(base_name)
785
+
786
+ def search_apis(self, query: str) -> list[HIPAPIDoc]:
787
+ """Search for APIs matching a query."""
788
+ query_lower = query.lower()
789
+ results = []
790
+ for name, doc in HIP_API_DOCS.items():
791
+ if query_lower in name.lower() or query_lower in doc.description.lower():
792
+ results.append(doc)
793
+ return results
794
+
795
+ def get_all_api_names(self) -> list[str]:
796
+ """Get all known API function names."""
797
+ return list(HIP_API_DOCS.keys())
798
+
799
+ def get_all_intrinsic_names(self) -> list[str]:
800
+ """Get all known intrinsic names."""
801
+ return list(INTRINSIC_DOCS.keys())
802
+
803
+
804
+ def create_hip_docs_service() -> HIPDocsService:
805
+ """Create a HIP documentation service instance."""
806
+ return HIPDocsService()