File size: 3,665 Bytes
9b0c4ec
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
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
"""
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 IR kinds (extend, don't replace)
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 space types
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",
}

# Platform detection
CUDA_PATTERNS = [
    "__global__", "__device__", "__host__",
    "threadIdx", "blockIdx", "blockDim", "gridDim",
    "__shared__", "__constant__", "__syncthreads",
    "<<<", ">>>",  # kernel launch syntax
]

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
    
    # Common index patterns
    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