Compile a CUDA kernel and return compilation results.
(self, kernel_data: Dict[str, Any])
| 90 | return ["-gencode=arch=compute_70,code=sm_70"] |
| 91 | |
| 92 | def compile_kernel(self, kernel_data: Dict[str, Any]) -> Dict[str, Any]: |
| 93 | """Compile a CUDA kernel and return compilation results.""" |
| 94 | kernel_code = kernel_data.get("code", "") |
| 95 | if not kernel_code: |
| 96 | raise ValueError("No kernel code provided") |
| 97 | |
| 98 | with tempfile.TemporaryDirectory() as temp_dir: |
| 99 | temp_path = Path(temp_dir) |
| 100 | |
| 101 | cuda_file = temp_path / "kernel.cu" |
| 102 | ptx_file = temp_path / "kernel.ptx" |
| 103 | cubin_file = temp_path / "kernel.cubin" |
| 104 | |
| 105 | full_code = self._prepare_kernel_code(kernel_code) |
| 106 | |
| 107 | with open(cuda_file, 'w') as f: |
| 108 | f.write(full_code) |
| 109 | |
| 110 | compile_flags = [ |
| 111 | "-ptx", |
| 112 | "-O3", |
| 113 | "-std=c++14", |
| 114 | "-use_fast_math", |
| 115 | "-lineinfo", |
| 116 | f"-o={ptx_file}" |
| 117 | ] + self.arch_flags |
| 118 | |
| 119 | constraints = kernel_data.get("constraints", {}) |
| 120 | if "max_registers" in constraints: |
| 121 | compile_flags.append(f"-maxrregcount={constraints['max_registers']}") |
| 122 | |
| 123 | # CRITICAL FIX: Add -ccbin flag on Windows to explicitly specify x64 compiler |
| 124 | # This prevents cudafe++ ACCESS_VIOLATION errors |
| 125 | ccbin_path = None |
| 126 | if platform.system() == "Windows": |
| 127 | try: |
| 128 | from .utils.detection import ToolchainDetector |
| 129 | detector = ToolchainDetector() |
| 130 | compiler_info = detector.detect_cpp_compiler() |
| 131 | |
| 132 | if compiler_info.available and compiler_info.path: |
| 133 | cl_dir = str(Path(compiler_info.path).parent) |
| 134 | |
| 135 | # Ensure x64 (CUDA 12+ requirement) |
| 136 | if 'x86' in cl_dir.lower() and 'x64' not in cl_dir.lower(): |
| 137 | x64_dir = cl_dir.replace('x86', 'x64').replace('X86', 'X64').replace('Hostx86', 'Hostx64') |
| 138 | if Path(x64_dir, 'cl.exe').exists(): |
| 139 | cl_dir = x64_dir |
| 140 | |
| 141 | # Add -ccbin flag to explicitly specify compiler |
| 142 | compile_flags.insert(0, f"-ccbin={cl_dir}") |
| 143 | ccbin_path = cl_dir |
| 144 | except Exception: |
| 145 | pass |
| 146 | |
| 147 | try: |
| 148 | console.print(f"[cyan]Compiling kernel for {kernel_data.get('operation', 'unknown')}...[/cyan]") |
| 149 |
no test coverage detected