| """ |
| GraphLang Parallel IR — GPU/HPC extension. |
| |
| Extends the 12-core IR with 5 parallel-specific kinds: |
| KERNEL — parallel entry point (GPU kernel, compute shader) |
| THREAD_MODEL — thread/block/grid dimensions |
| PARALLEL_REGION — code region executed in parallel across threads |
| MEMORY_SPACE — memory qualifier (global, shared, local, constant) |
| SYNC — synchronization barrier |
| |
| These sit ALONGSIDE the existing 12 kinds. The core IR is not modified. |
| """ |
|
|
| |
| PARALLEL_KINDS = { |
| "kernel": "Parallel entry point (__global__, compute shader entry)", |
| "thread_model": "Thread organization (blockDim, gridDim, threadIdx, blockIdx)", |
| "parallel_region": "Code region with parallel execution semantics", |
| "memory_space": "Memory qualifier (global, shared, local, constant, texture)", |
| "sync": "Synchronization barrier (__syncthreads, memory fence)", |
| } |
|
|
| |
| MEMORY_SPACES = { |
| "global": "GPU global memory (default for kernel params)", |
| "shared": "GPU shared memory (__shared__, workgroup local)", |
| "local": "GPU local memory (per-thread)", |
| "constant": "GPU constant memory (__constant__)", |
| "texture": "GPU texture memory", |
| } |
|
|
| |
| CUDA_PATTERNS = [ |
| "__global__", "__device__", "__host__", |
| "threadIdx", "blockIdx", "blockDim", "gridDim", |
| "__shared__", "__constant__", "__syncthreads", |
| "<<<", ">>>", |
| ] |
|
|
| OPENCL_PATTERNS = [ |
| "__kernel", "__global", "__local", "__constant", "__private", |
| "get_global_id", "get_local_id", "get_group_id", |
| "barrier", "mem_fence", |
| ] |
|
|
| METAL_PATTERNS = [ |
| "kernel void", "device ", "threadgroup ", |
| "thread_position_in_grid", "threadgroup_position_in_grid", |
| "threadgroup_barrier", "simdgroup_barrier", |
| ] |
|
|
|
|
| def detect_parallel_platform(code: str) -> str | None: |
| """Detect which GPU platform the code targets.""" |
| if any(p in code for p in CUDA_PATTERNS): |
| return "cuda" |
| if any(p in code for p in OPENCL_PATTERNS): |
| return "opencl" |
| if any(p in code for p in METAL_PATTERNS): |
| return "metal" |
| return None |
|
|
|
|
| def normalize_thread_index(code: str) -> str: |
| """ |
| Normalize thread indexing to platform-agnostic form. |
| |
| CUDA: threadIdx.x + blockIdx.x * blockDim.x → THREAD_ID[linear] |
| OpenCL: get_global_id(0) → THREAD_ID[linear] |
| Metal: thread_position_in_grid.x → THREAD_ID[linear] |
| """ |
| platform = detect_parallel_platform(code) |
| if not platform: |
| return code |
| |
| |
| replacements = { |
| "cuda": [ |
| ("threadIdx.x + blockIdx.x * blockDim.x", "THREAD_ID[linear]"), |
| ("threadIdx.x", "THREAD_ID[x]"), |
| ("threadIdx.y", "THREAD_ID[y]"), |
| ("threadIdx.z", "THREAD_ID[z]"), |
| ("blockIdx.x", "BLOCK_ID[x]"), |
| ("blockIdx.y", "BLOCK_ID[y]"), |
| ("blockDim.x", "BLOCK_DIM[x]"), |
| ("gridDim.x", "GRID_DIM[x]"), |
| ], |
| "opencl": [ |
| ("get_global_id(0)", "THREAD_ID[linear]"), |
| ("get_global_id(1)", "THREAD_ID[y]"), |
| ("get_local_id(0)", "THREAD_ID[local]"), |
| ("get_group_id(0)", "BLOCK_ID[x]"), |
| ], |
| "metal": [ |
| ("thread_position_in_grid.x", "THREAD_ID[linear]"), |
| ("thread_position_in_grid.y", "THREAD_ID[y]"), |
| ("threadgroup_position_in_grid.x", "BLOCK_ID[x]"), |
| ], |
| } |
| |
| result = code |
| for old, new in replacements.get(platform, []): |
| result = result.replace(old, new) |
| |
| return result |
|
|