kernelvet 0.1.0__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 (77) hide show
  1. kernelvet/__init__.py +10 -0
  2. kernelvet/__main__.py +3 -0
  3. kernelvet/benchmark/__init__.py +1 -0
  4. kernelvet/benchmark/engine.py +180 -0
  5. kernelvet/benchmark/workloads.py +153 -0
  6. kernelvet/cli/__init__.py +1 -0
  7. kernelvet/cli/app.py +64 -0
  8. kernelvet/cli/commands/__init__.py +1 -0
  9. kernelvet/cli/commands/diagnose.py +244 -0
  10. kernelvet/cli/commands/migrate.py +334 -0
  11. kernelvet/cli/commands/misc.py +216 -0
  12. kernelvet/cli/output.py +110 -0
  13. kernelvet/compatibility/__init__.py +1 -0
  14. kernelvet/compatibility/data/rocm_matrix.json +123 -0
  15. kernelvet/compatibility/engine.py +24 -0
  16. kernelvet/compatibility/matrix.py +128 -0
  17. kernelvet/compatibility/validators.py +405 -0
  18. kernelvet/config/__init__.py +10 -0
  19. kernelvet/config/settings.py +112 -0
  20. kernelvet/diagnose/__init__.py +1 -0
  21. kernelvet/diagnose/engine.py +296 -0
  22. kernelvet/diagnose/hardware.py +336 -0
  23. kernelvet/diagnose/runtime.py +332 -0
  24. kernelvet/diagnose/software.py +221 -0
  25. kernelvet/diagnose/system.py +67 -0
  26. kernelvet/execution/__init__.py +1 -0
  27. kernelvet/execution/compiler.py +275 -0
  28. kernelvet/execution/runner.py +125 -0
  29. kernelvet/execution/sandbox.py +51 -0
  30. kernelvet/history/__init__.py +3 -0
  31. kernelvet/history/store.py +157 -0
  32. kernelvet/main.py +19 -0
  33. kernelvet/migration/__init__.py +17 -0
  34. kernelvet/migration/analyzer.py +78 -0
  35. kernelvet/migration/auditor.py +89 -0
  36. kernelvet/migration/hipify.py +268 -0
  37. kernelvet/migration/planner.py +128 -0
  38. kernelvet/migration/rules.py +564 -0
  39. kernelvet/migration/scanner.py +234 -0
  40. kernelvet/migration/workflow.py +135 -0
  41. kernelvet/models/__init__.py +35 -0
  42. kernelvet/models/common.py +102 -0
  43. kernelvet/models/migration.py +236 -0
  44. kernelvet/models/report.py +126 -0
  45. kernelvet/models/system.py +89 -0
  46. kernelvet/reporting/__init__.py +80 -0
  47. kernelvet/reporting/common.py +74 -0
  48. kernelvet/reporting/html.py +149 -0
  49. kernelvet/reporting/json.py +20 -0
  50. kernelvet/reporting/loader.py +48 -0
  51. kernelvet/reporting/markdown.py +256 -0
  52. kernelvet/reporting/sarif.py +79 -0
  53. kernelvet/reporting/terminal.py +276 -0
  54. kernelvet/tui/__init__.py +1 -0
  55. kernelvet/tui/app.py +177 -0
  56. kernelvet/tui/screens/__init__.py +1 -0
  57. kernelvet/tui/screens/base.py +34 -0
  58. kernelvet/tui/screens/devices.py +211 -0
  59. kernelvet/tui/screens/diagnose.py +212 -0
  60. kernelvet/tui/screens/help.py +47 -0
  61. kernelvet/tui/screens/migration.py +573 -0
  62. kernelvet/tui/screens/overview.py +195 -0
  63. kernelvet/tui/screens/tools.py +357 -0
  64. kernelvet/tui/state.py +55 -0
  65. kernelvet/tui/widgets/__init__.py +1 -0
  66. kernelvet/tui/widgets/common.py +82 -0
  67. kernelvet/tui/workers/__init__.py +25 -0
  68. kernelvet/utils/__init__.py +1 -0
  69. kernelvet/utils/paths.py +48 -0
  70. kernelvet/utils/proc.py +178 -0
  71. kernelvet/utils/redact.py +47 -0
  72. kernelvet/utils/versions.py +34 -0
  73. kernelvet-0.1.0.dist-info/METADATA +275 -0
  74. kernelvet-0.1.0.dist-info/RECORD +77 -0
  75. kernelvet-0.1.0.dist-info/WHEEL +4 -0
  76. kernelvet-0.1.0.dist-info/entry_points.txt +2 -0
  77. kernelvet-0.1.0.dist-info/licenses/LICENSE +197 -0
@@ -0,0 +1,78 @@
1
+ """Migration readiness analysis over a scanned inventory."""
2
+
3
+ from __future__ import annotations
4
+
5
+ from collections import Counter
6
+ from pathlib import Path
7
+
8
+ from kernelvet.migration.rules import iter_matches
9
+ from kernelvet.migration.scanner import read_text, scan_project
10
+ from kernelvet.models.migration import (
11
+ AnalysisResult,
12
+ FileKind,
13
+ Finding,
14
+ FindingCategory,
15
+ ProjectInventory,
16
+ ReadinessScore,
17
+ )
18
+
19
+ SOURCE_KINDS = {
20
+ FileKind.CUDA_SOURCE,
21
+ FileKind.CUDA_HEADER,
22
+ FileKind.CUDA_RELATED_CPP,
23
+ FileKind.CUDA_RELATED_HEADER,
24
+ FileKind.HIP_SOURCE,
25
+ }
26
+ BUILD_KINDS = {FileKind.CMAKE, FileKind.MAKEFILE, FileKind.BUILD_SCRIPT}
27
+
28
+ WEIGHTS = {
29
+ FindingCategory.AUTO_CONVERTIBLE: 1.0,
30
+ FindingCategory.REQUIRES_VERIFICATION: 0.5,
31
+ FindingCategory.MANUAL_INTERVENTION: 0.0,
32
+ FindingCategory.UNSUPPORTED: 0.0,
33
+ }
34
+
35
+
36
+ def scan_findings(root: Path, inv: ProjectInventory, phase: str) -> list[Finding]:
37
+ findings: list[Finding] = []
38
+ for f in inv.files:
39
+ if f.kind in SOURCE_KINDS:
40
+ target = "source"
41
+ elif f.kind in BUILD_KINDS:
42
+ target = "build"
43
+ else:
44
+ continue
45
+ text = read_text(root / f.path)
46
+ if text is None:
47
+ continue
48
+ findings.extend(iter_matches(text, f.path, phase=phase, target=target))
49
+ return findings
50
+
51
+
52
+ def readiness(findings: list[Finding]) -> ReadinessScore:
53
+ counts = Counter(f.category for f in findings)
54
+ by_cat = {c.value: counts.get(c, 0) for c in FindingCategory}
55
+ total = sum(counts.values())
56
+ if total == 0:
57
+ return ReadinessScore(
58
+ score=0,
59
+ total_findings=0,
60
+ by_category=by_cat,
61
+ explanation="No CUDA constructs detected; nothing to convert.",
62
+ )
63
+ weighted = sum(WEIGHTS[c] * n for c, n in counts.items())
64
+ score = round(100 * weighted / total)
65
+ # any unsupported construct caps the score: the project can't be fully converted mechanically
66
+ if counts.get(FindingCategory.UNSUPPORTED):
67
+ score = min(score, 60)
68
+ return ReadinessScore(score=score, total_findings=total, by_category=by_cat)
69
+
70
+
71
+ def analyze_project(path: str | Path) -> AnalysisResult:
72
+ inv = scan_project(path)
73
+ findings = scan_findings(Path(inv.root), inv, phase="analyze")
74
+ return AnalysisResult(inventory=inv, findings=findings, readiness=readiness(findings))
75
+
76
+
77
+ def summarize(findings: list[Finding]) -> dict[str, int]:
78
+ return dict(Counter(f.category.value for f in findings))
@@ -0,0 +1,89 @@
1
+ """Static audit of a migrated (HIP) tree.
2
+
3
+ Finds leftover CUDA, NVIDIA library references, CUDA build configuration,
4
+ risky substitutions and architecture assumptions. A clean audit does **not**
5
+ mean the code is correct; only compiling and running tests can show that.
6
+ """
7
+
8
+ from __future__ import annotations
9
+
10
+ import re
11
+ from pathlib import Path
12
+
13
+ from kernelvet.migration.analyzer import BUILD_KINDS, SOURCE_KINDS
14
+ from kernelvet.migration.rules import RULES, Rule, iter_matches, strip_comments
15
+ from kernelvet.migration.scanner import read_text, scan_project
16
+ from kernelvet.models.migration import AuditResult, FileKind, Finding, FindingCategory, Severity
17
+
18
+ # HIP header -> library family name used by diagnose.software.detect_libraries
19
+ HIP_LIB_HEADERS: dict[str, str] = {
20
+ "hipblas": "hipBLAS",
21
+ "hipblaslt": "hipBLASLt",
22
+ "hipfft": "hipFFT",
23
+ "hiprand": "hipRAND",
24
+ "hipsparse": "hipSPARSE",
25
+ "hipsolver": "hipSOLVER",
26
+ "miopen": "MIOpen",
27
+ "rccl": "RCCL",
28
+ "hipcub": "hipCUB",
29
+ "rocrand": "rocRAND",
30
+ "rocblas": "rocBLAS",
31
+ }
32
+ _INCLUDE = re.compile(r"#\s*include\s*[<\"]([\w/]+)/?[\w.]*[>\"]")
33
+ AUDIT_RULES: list[Rule] = [r for r in RULES if "audit" in r.phases]
34
+
35
+
36
+ def audit_project(path: str | Path, *, installed_libraries: list[str] | None = None) -> AuditResult:
37
+ inv = scan_project(path)
38
+ root = Path(inv.root)
39
+ findings: list[Finding] = []
40
+ scanned = 0
41
+ referenced: dict[str, str] = {}
42
+ for f in inv.files:
43
+ if f.kind in SOURCE_KINDS or f.kind is FileKind.CPP:
44
+ target = "source"
45
+ elif f.kind in BUILD_KINDS:
46
+ target = "build"
47
+ else:
48
+ continue
49
+ text = read_text(root / f.path)
50
+ if text is None:
51
+ continue
52
+ scanned += 1
53
+ findings.extend(iter_matches(text, f.path, phase="audit", target=target, rules=AUDIT_RULES))
54
+ if target == "source":
55
+ for i, line in enumerate(strip_comments(text).splitlines(), 1):
56
+ for m in _INCLUDE.finditer(line):
57
+ head = m.group(1).split("/")[0].lower()
58
+ if head in HIP_LIB_HEADERS:
59
+ referenced.setdefault(HIP_LIB_HEADERS[head], f"{f.path}:{i}")
60
+ if installed_libraries is not None:
61
+ installed = set(installed_libraries)
62
+ for lib, where in sorted(referenced.items()):
63
+ if lib not in installed:
64
+ file, _, line = where.rpartition(":")
65
+ findings.append(
66
+ Finding(
67
+ rule_id="audit.missing_dependency",
68
+ category=FindingCategory.MANUAL_INTERVENTION,
69
+ severity=Severity.HIGH,
70
+ message=f"{lib} is used but was not found in the ROCm install",
71
+ file=file,
72
+ line=int(line),
73
+ symbol=lib,
74
+ suggestion=f"Install the {lib} package for your ROCm release",
75
+ )
76
+ )
77
+ findings.sort(key=lambda x: (x.file, x.line or 0, x.rule_id))
78
+ return AuditResult(project=str(root), files_scanned=scanned, findings=findings)
79
+
80
+
81
+ def migrated_source_of(path: str | Path) -> str | None:
82
+ """Return the CUDA project a migrated tree came from (written by convert_project)."""
83
+ meta = Path(path) / ".kernelvet" / "source.json"
84
+ try:
85
+ import json
86
+
87
+ return json.loads(meta.read_text()).get("source_project")
88
+ except (OSError, ValueError):
89
+ return None
@@ -0,0 +1,268 @@
1
+ """AMD HIPIFY integration (``hipify-clang`` and ``hipify-perl``).
2
+
3
+ KernelVet never translates CUDA itself. This module picks a translator, runs it
4
+ file by file into a separate output tree, copies everything else verbatim and
5
+ records every warning, error and HIPIFY statistic.
6
+
7
+ Guarantees:
8
+
9
+ * the source tree is opened read-only and never written;
10
+ * a file HIPIFY fails on is copied unchanged and marked ``failed`` (so the
11
+ audit flags its leftover CUDA), never silently dropped;
12
+ * the full log is written to ``<output>/.kernelvet/conversion.log``.
13
+ """
14
+
15
+ from __future__ import annotations
16
+
17
+ import json
18
+ import re
19
+ import shutil
20
+ from dataclasses import dataclass
21
+ from enum import StrEnum
22
+ from pathlib import Path
23
+
24
+ from kernelvet.migration.scanner import iter_project_files
25
+ from kernelvet.models.migration import ConversionResult, FileConversion, FileKind, ProjectInventory
26
+ from kernelvet.utils.paths import safe_output_dir
27
+ from kernelvet.utils.proc import HostContext, ProgressCallback, noop_progress
28
+ from kernelvet.utils.versions import version_str
29
+
30
+ TRANSLATE_KINDS = {
31
+ FileKind.CUDA_SOURCE,
32
+ FileKind.CUDA_HEADER,
33
+ FileKind.CUDA_RELATED_CPP,
34
+ FileKind.CUDA_RELATED_HEADER,
35
+ }
36
+ LOG_DIR = ".kernelvet"
37
+
38
+
39
+ class HipifyUnavailable(RuntimeError):
40
+ pass
41
+
42
+
43
+ class Translator(StrEnum):
44
+ AUTO = "auto"
45
+ HIPIFY_CLANG = "hipify-clang"
46
+ HIPIFY_PERL = "hipify-perl"
47
+
48
+
49
+ @dataclass
50
+ class TranslatorChoice:
51
+ name: str | None
52
+ reason: str
53
+ cuda_path: str | None = None
54
+
55
+
56
+ def find_cuda_headers(ctx: HostContext) -> str | None:
57
+ """hipify-clang parses CUDA with clang and needs CUDA toolkit headers."""
58
+ for key in ("CUDA_PATH", "CUDA_HOME", "CUDA_TOOLKIT_ROOT_DIR"):
59
+ p = ctx.env.get(key)
60
+ if p and ctx.exists(f"{p}/include/cuda_runtime.h"):
61
+ return p
62
+ for p in ("/usr/local/cuda", "/opt/cuda", "/usr"):
63
+ if ctx.exists(f"{p}/include/cuda_runtime.h"):
64
+ return p
65
+ return None
66
+
67
+
68
+ def choose_translator(
69
+ ctx: HostContext, preference: str = "auto", *, has_compile_db: bool = False
70
+ ) -> TranslatorChoice:
71
+ if preference not in {t.value for t in Translator}:
72
+ raise ValueError(
73
+ f"unknown translator {preference!r}; choose from {', '.join(t.value for t in Translator)}"
74
+ )
75
+ clang = ctx.which("hipify-clang")
76
+ perl = ctx.which("hipify-perl")
77
+ cuda = find_cuda_headers(ctx)
78
+ if preference == "hipify-clang":
79
+ if not clang:
80
+ return TranslatorChoice(None, "hipify-clang requested but not installed")
81
+ if not cuda:
82
+ return TranslatorChoice(
83
+ None,
84
+ "hipify-clang requested but no CUDA headers found "
85
+ "(set CUDA_PATH); it needs them to parse CUDA",
86
+ )
87
+ return TranslatorChoice("hipify-clang", "requested in configuration", cuda)
88
+ if preference == "hipify-perl":
89
+ if not perl:
90
+ return TranslatorChoice(None, "hipify-perl requested but not installed")
91
+ return TranslatorChoice("hipify-perl", "requested in configuration")
92
+ if clang and cuda:
93
+ why = "hipify-clang is installed and CUDA headers were found; it parses code semantically"
94
+ if has_compile_db:
95
+ why += " (compile_commands.json available for include paths)"
96
+ return TranslatorChoice("hipify-clang", why, cuda)
97
+ if perl:
98
+ why = "hipify-perl is installed (regex-based; no CUDA toolkit needed)"
99
+ if clang and not cuda:
100
+ why += "; hipify-clang is installed but no CUDA headers were found"
101
+ return TranslatorChoice("hipify-perl", why)
102
+ return TranslatorChoice(
103
+ None,
104
+ "neither hipify-clang nor hipify-perl is installed (install the `hipify-clang` package from ROCm)",
105
+ )
106
+
107
+
108
+ _STATS = re.compile(
109
+ r"CONVERTED refs count:\s*(\d+).*?UNCONVERTED refs count:\s*(\d+)", re.IGNORECASE | re.DOTALL
110
+ )
111
+ _WARN = re.compile(r"(?i)\bwarning\b[:\s]")
112
+ _ERR = re.compile(r"(?i)\b(error|fatal)\b[:\s]")
113
+
114
+
115
+ def parse_hipify_output(text: str) -> tuple[int | None, int | None, list[str], list[str]]:
116
+ converted = unconverted = None
117
+ m = _STATS.search(text)
118
+ if m:
119
+ converted, unconverted = int(m.group(1)), int(m.group(2))
120
+ warnings, errors = [], []
121
+ for line in text.splitlines():
122
+ s = line.strip()
123
+ if not s:
124
+ continue
125
+ if _ERR.search(s):
126
+ errors.append(s[:400])
127
+ elif _WARN.search(s):
128
+ warnings.append(s[:400])
129
+ return converted, unconverted, warnings, errors
130
+
131
+
132
+ def _include_flags(root: Path, inv: ProjectInventory) -> list[str]:
133
+ dirs = sorted(
134
+ {
135
+ str((root / f.path).parent)
136
+ for f in inv.files
137
+ if f.kind in (FileKind.CUDA_HEADER, FileKind.CUDA_RELATED_HEADER, FileKind.CPP)
138
+ }
139
+ )
140
+ flags = [f"-I{root}"]
141
+ flags += [f"-I{d}" for d in dirs if d != str(root)]
142
+ for name in ("include", "inc"):
143
+ if (root / name).is_dir():
144
+ flags.append(f"-I{root / name}")
145
+ return flags
146
+
147
+
148
+ def translator_argv(choice: TranslatorChoice, src: Path, out: Path, includes: list[str]) -> list[str]:
149
+ if choice.name == "hipify-perl":
150
+ return ["hipify-perl", "-print-stats", f"-o={out}", str(src)]
151
+ argv = ["hipify-clang", str(src), "-o", str(out), "--print-stats"]
152
+ if choice.cuda_path:
153
+ argv.append(f"--cuda-path={choice.cuda_path}")
154
+ return [*argv, "--", "-std=c++17", *includes]
155
+
156
+
157
+ def translator_version(ctx: HostContext, name: str) -> str | None:
158
+ res = ctx.run([name, "--version"], timeout=20)
159
+ return version_str(res.stdout + res.stderr) if (res.stdout or res.stderr) else None
160
+
161
+
162
+ def convert_project(
163
+ ctx: HostContext,
164
+ inv: ProjectInventory,
165
+ output: str | Path,
166
+ *,
167
+ preference: str = "auto",
168
+ dry_run: bool = False,
169
+ force: bool = False,
170
+ timeout: float = 300.0,
171
+ progress: ProgressCallback = noop_progress,
172
+ ) -> ConversionResult:
173
+ root = Path(inv.root)
174
+ out_root = safe_output_dir(root, output, allow_existing=force)
175
+ choice = choose_translator(ctx, preference, has_compile_db=(root / "compile_commands.json").exists())
176
+ if choice.name is None:
177
+ raise HipifyUnavailable(choice.reason)
178
+
179
+ kinds = {f.path: f.kind for f in inv.files}
180
+ result = ConversionResult(
181
+ project=str(root),
182
+ output=str(out_root),
183
+ translator=choice.name,
184
+ translator_version=None if dry_run else translator_version(ctx, choice.name),
185
+ dry_run=dry_run,
186
+ )
187
+ includes = _include_flags(root, inv)
188
+ files = [p for p in iter_project_files(root)]
189
+ log_lines: list[str] = [
190
+ f"translator: {choice.name} ({choice.reason})",
191
+ f"source: {root}",
192
+ f"output: {out_root}",
193
+ "",
194
+ ]
195
+ for i, src in enumerate(files):
196
+ rel = src.relative_to(root).as_posix()
197
+ dst = out_root / rel
198
+ kind = kinds.get(rel)
199
+ progress(f"{'Planning' if dry_run else 'Converting'} {rel}", (i + 1) / max(len(files), 1))
200
+ if kind in TRANSLATE_KINDS:
201
+ argv = translator_argv(choice, src, dst, includes)
202
+ if dry_run:
203
+ result.files.append(
204
+ FileConversion(source=rel, output=str(dst), translator=choice.name, status="planned")
205
+ )
206
+ log_lines.append("would run: " + " ".join(argv))
207
+ continue
208
+ dst.parent.mkdir(parents=True, exist_ok=True)
209
+ res = ctx.run(argv, timeout=timeout, cwd=root)
210
+ converted, unconverted, warnings, errors = parse_hipify_output(res.stdout + "\n" + res.stderr)
211
+ ok = res.ok and dst.exists()
212
+ if not ok:
213
+ if not res.ok:
214
+ errors.insert(0, res.describe_failure())
215
+ if not dst.exists():
216
+ errors.append("translator produced no output; original copied unchanged for review")
217
+ shutil.copy2(src, dst)
218
+ result.files.append(
219
+ FileConversion(
220
+ source=rel,
221
+ output=str(dst),
222
+ translator=choice.name,
223
+ status="converted" if ok else "failed",
224
+ returncode=res.returncode,
225
+ warnings=warnings,
226
+ errors=errors,
227
+ replacements=converted,
228
+ unconverted=unconverted,
229
+ )
230
+ )
231
+ log_lines += [
232
+ f"$ {' '.join(argv)}",
233
+ f"exit: {res.returncode}",
234
+ res.stdout.strip(),
235
+ res.stderr.strip(),
236
+ "",
237
+ ]
238
+ else:
239
+ if dry_run:
240
+ result.files.append(
241
+ FileConversion(source=rel, output=str(dst), translator="copy", status="planned-copy")
242
+ )
243
+ continue
244
+ dst.parent.mkdir(parents=True, exist_ok=True)
245
+ if src.is_symlink():
246
+ result.files.append(
247
+ FileConversion(
248
+ source=rel,
249
+ output=str(dst),
250
+ translator="copy",
251
+ status="skipped",
252
+ warnings=["symlink not recreated in the output tree"],
253
+ )
254
+ )
255
+ continue
256
+ shutil.copy2(src, dst)
257
+ result.files.append(
258
+ FileConversion(source=rel, output=str(dst), translator="copy", status="copied")
259
+ )
260
+
261
+ if not dry_run:
262
+ log_dir = out_root / LOG_DIR
263
+ log_dir.mkdir(parents=True, exist_ok=True)
264
+ (log_dir / "conversion.log").write_text("\n".join(log_lines) + "\n")
265
+ (log_dir / "conversion.json").write_text(result.model_dump_json(indent=2))
266
+ (log_dir / "source.json").write_text(json.dumps({"source_project": str(root)}))
267
+ result.log_path = str(log_dir / "conversion.log")
268
+ return result
@@ -0,0 +1,128 @@
1
+ """Migration planning: an actionable plan produced before anything is modified."""
2
+
3
+ from __future__ import annotations
4
+
5
+ from pathlib import Path
6
+
7
+ from kernelvet.migration.hipify import TRANSLATE_KINDS, choose_translator
8
+ from kernelvet.models.migration import (
9
+ AnalysisResult,
10
+ FindingCategory,
11
+ MigrationPlan,
12
+ PlanStep,
13
+ Severity,
14
+ )
15
+ from kernelvet.utils.proc import HostContext
16
+
17
+
18
+ def build_plan(
19
+ analysis: AnalysisResult,
20
+ ctx: HostContext | None = None,
21
+ *,
22
+ preference: str = "auto",
23
+ offload_arch: list[str] | None = None,
24
+ ) -> MigrationPlan:
25
+ ctx = ctx or HostContext()
26
+ inv = analysis.inventory
27
+ root = Path(inv.root)
28
+ choice = choose_translator(ctx, preference, has_compile_db=(root / "compile_commands.json").exists())
29
+ to_migrate = [f.path for f in inv.files if f.kind in TRANSLATE_KINDS]
30
+ to_copy = [f.path for f in inv.files if f.kind not in TRANSLATE_KINDS]
31
+
32
+ conversions: dict[str, str] = {}
33
+ for f in analysis.findings:
34
+ if f.category is FindingCategory.AUTO_CONVERTIBLE and f.symbol and f.hip_equivalent:
35
+ conversions.setdefault(f.symbol, f.hip_equivalent)
36
+ manual = [f for f in analysis.findings if f.category is FindingCategory.MANUAL_INTERVENTION]
37
+ blockers = [
38
+ f
39
+ for f in analysis.findings
40
+ if f.category is FindingCategory.UNSUPPORTED
41
+ or (f.category is FindingCategory.MANUAL_INTERVENTION and f.severity is Severity.HIGH)
42
+ ]
43
+ verify = [f for f in analysis.findings if f.category is FindingCategory.REQUIRES_VERIFICATION]
44
+ deps = [d for d in inv.dependencies if d.kind == "nvidia-library"]
45
+
46
+ arch = offload_arch or []
47
+ arch_txt = ";".join(arch) if arch else "<your gfx targets, e.g. gfx942;gfx1100>"
48
+ if any(b.kind == "cmake" for b in inv.build_systems):
49
+ build_rec = (
50
+ f'CMake: enable_language(HIP), set CMAKE_HIP_ARCHITECTURES="{arch_txt}", '
51
+ "find_package(hip REQUIRED) plus per-library packages (hipblas, hipfft, rccl, ...), "
52
+ "set LANGUAGE HIP on .cu sources, and link hip::device. Build with "
53
+ "`cmake -S . -B build -DCMAKE_HIP_COMPILER=$ROCM_PATH/llvm/bin/clang++`."
54
+ )
55
+ elif any(b.kind == "make" for b in inv.build_systems):
56
+ build_rec = (
57
+ f"Make: replace nvcc with hipcc, replace -arch=sm_XX with --offload-arch={arch_txt.split(';')[0]}, "
58
+ "and replace -lcudart/-lcublas/... with -lamdhip64/-lhipblas/...."
59
+ )
60
+ else:
61
+ build_rec = f"No build system detected: compile each translation unit with `hipcc --offload-arch={arch_txt.split(';')[0]}`."
62
+
63
+ steps: list[PlanStep] = []
64
+
65
+ def add(title: str, detail: str, files: list[str] | None = None, blocking: bool = False) -> None:
66
+ steps.append(
67
+ PlanStep(order=len(steps) + 1, title=title, detail=detail, files=files or [], blocking=blocking)
68
+ )
69
+
70
+ if choice.name is None:
71
+ add("Install HIPIFY", choice.reason, blocking=True)
72
+ add(
73
+ "Convert sources with " + (choice.name or "HIPIFY"),
74
+ f"{len(to_migrate)} CUDA-related file(s) are translated into a separate output tree; "
75
+ f"{len(to_copy)} other file(s) are copied unchanged. The original project is not modified.",
76
+ to_migrate,
77
+ )
78
+ for d in deps:
79
+ add(
80
+ f"Replace {d.name}",
81
+ f"Use {d.hip_alternative or 'no direct equivalent'}" + (f" ({d.note})" if d.note else ""),
82
+ [e.split(":")[0] for e in d.evidence],
83
+ )
84
+ if blockers:
85
+ add(
86
+ "Resolve blockers",
87
+ f"{len(blockers)} construct(s) have no mechanical HIP translation "
88
+ "(inline PTX, CUTLASS/TensorRT, legacy textures, cuDNN ...). Port these by hand.",
89
+ sorted({f.file for f in blockers}),
90
+ blocking=True,
91
+ )
92
+ if manual:
93
+ add(
94
+ "Apply manual changes",
95
+ f"{len(manual)} finding(s) need manual edits (build files, NVTX/NVML, cuDNN ...).",
96
+ sorted({f.file for f in manual}),
97
+ )
98
+ if verify:
99
+ add(
100
+ "Review wavefront/architecture assumptions",
101
+ f"{len(verify)} finding(s) depend on warp size 32, 32-bit lane masks, compute capability or "
102
+ "managed memory. HIPIFY translates the calls but not the assumptions.",
103
+ sorted({f.file for f in verify}),
104
+ )
105
+ add("Update the build", build_rec)
106
+ add(
107
+ "Audit converted sources",
108
+ "Run `kernelvet migrate audit <output>` to find leftover CUDA and risky patterns.",
109
+ )
110
+ add("Compile with hipcc", "Run `kernelvet migrate build <output>`.")
111
+ add(
112
+ "Run tests on an AMD GPU",
113
+ "Run `kernelvet migrate test <output>`; only this stage validates runtime behaviour.",
114
+ )
115
+
116
+ return MigrationPlan(
117
+ project=inv.root,
118
+ translator=choice.name,
119
+ translator_reason=choice.reason,
120
+ files_to_migrate=to_migrate,
121
+ files_to_copy=to_copy,
122
+ api_conversions=dict(sorted(conversions.items())),
123
+ dependency_replacements=deps,
124
+ manual_changes=manual,
125
+ blockers=blockers,
126
+ build_recommendation=build_rec,
127
+ steps=steps,
128
+ )