interplane.Get Omega
Read in your language

CUDApp

CUDA++: AIEN’s GPU machine-code toolchain, runtime and driver client for NVIDIA GPUs without the CUDA toolkit. Hardware coverage and driver replacement limits are explicit.

Brought by drakestapleton

branch main Branches Tags History

CUDApp / tools / extract / graph.sh

155 lines · 7394 bytes Raw Blame History
1#!/usr/bin/env bash
2# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
3# graph.sh: decide which Omega files belong to CUDA++ (the GPU half) from the
4# include graph plus the header/source link rule.
5#
6# tools/extract/graph.sh OMEGA_CHECKOUT OUT_DIR
7#
8# Inputs next to this script: stay.txt (Omega bridges the walk must not cross)
9# and shared.txt (shared base kept in both repos).
10#
11# Seeds: src/omega_blackwell_* and src/omega_gpu_* (docs/OMEGA.md section 1:
12# GPU toolchain, GPU runtime, libraries). From the seeds it follows every
13# `#include "x"` and, for every header it reaches, the matching .c file (the link
14# rule: a header without its body does not build). Files of the languages
15# (src/compiler, src/checker2, tools/omegese) are never pulled across; if the GPU
16# part needs one, it is written to conflicts.txt and the run fails.
17#
18# Tests: a file under tests/ moves when it includes at least one GPU file and
19# everything it includes is GPU, test-local, or the driver client. Tests that
20# also include Omega-only files stay in Omega (they are integration tests of the
21# two halves) and are listed in tests-stay.txt.
22#
23# Output (OUT_DIR): gpu-src.txt, gpu-tests.txt, tests-stay.txt, consumers.txt
24# (Omega files that include a GPU header and stay), driver-includes.txt,
25# unresolved.txt, conflicts.txt, filemap.tsv (old path -> new path).
26set -euo pipefail
27export LC_ALL=C
28
29root=${1:?omega checkout}
30out=${2:?output dir}
31mkdir -p "$out"
32here=$(cd "$(dirname "$0")" && pwd)
33cd "$root"
34declare -A stay=() shared=()
35while read -r p; do [[ -n $p && $p != \#* ]] && stay[$p]=1; done < "$here/stay.txt"
36while read -r p; do [[ -n $p && $p != \#* ]] && shared[$p]=1; done < "$here/shared.txt"
37
38git ls-files > "$out/.all"
39declare -A tracked
40while IFS= read -r f; do tracked[$f]=1; done < "$out/.all"
41
42is_lang() { case $1 in src/compiler/*|src/checker2/*|tools/omegese/*) return 0;; esac; return 1; }
43# Headers that come from the physics driver client or the NVIDIA open headers.
44is_driver() {
45 case $1 in nvrm.h|m16_native.h|m16_gpu_wait.h|nvos.h|nv-ioctl*.h|nv_escape.h|uvm_*.h|nv-unix-*.h|class/*|ctrl/*|alloc/*) return 0;; esac
46 return 1
47}
48
49# includes_of FILE -> one resolved path per line, or "DRIVER:x" / "UNRESOLVED:x"
50includes_of() {
51 local f=$1 d inc
52 d=$(dirname "$f")
53 { grep -hoE '^[[:space:]]*#[[:space:]]*include[[:space:]]*"[^"]+"' "$f" 2>/dev/null || true; } |
54 sed -E 's/.*"([^"]+)".*/\1/' | while IFS= read -r inc; do
55 local c
56 for c in "$d/$inc" "src/$inc" "$inc" "tests/$inc"; do
57 c=$(realpath -m --relative-to=. "$c")
58 if [[ -n ${tracked[$c]:-} ]]; then echo "$c"; continue 2; fi
59 done
60 if is_driver "$inc"; then echo "DRIVER:$inc"; else echo "UNRESOLVED:$f:$inc"; fi
61 done
62}
63
64declare -A gpu=()
65queue=()
66for f in $(grep -E "^src/omega_(blackwell|gpu)_" "$out/.all"); do [[ -n ${stay[$f]:-} ]] && continue; gpu[$f]=1; queue+=("$f"); done
67
68: > "$out/conflicts.txt"; : > "$out/unresolved.txt"; : > "$out/driver-includes.txt"
69while ((${#queue[@]})); do
70 f=${queue[0]}; queue=("${queue[@]:1}")
71 cand=$(includes_of "$f")
72 # link rule: header -> its .c
73 if [[ $f == *.h ]]; then
74 c=${f%.h}.c
75 [[ -n ${tracked[$c]:-} ]] && cand+=$'\n'"$c"
76 fi
77 while IFS= read -r g; do
78 [[ -z $g ]] && continue
79 case $g in
80 DRIVER:*) echo "$f ${g#DRIVER:}" >> "$out/driver-includes.txt"; continue;;
81 UNRESOLVED:*) echo "${g#UNRESOLVED:}" >> "$out/unresolved.txt"; continue;;
82 esac
83 if is_lang "$g"; then echo "$f -> $g" >> "$out/conflicts.txt"; continue; fi
84 if [[ -n ${stay[$g]:-} ]]; then echo "$f -> $g (stays in Omega)" >> "$out/conflicts.txt"; continue; fi
85 if [[ -z ${gpu[$g]:-} ]]; then gpu[$g]=1; queue+=("$g"); fi
86 done <<< "$cand"
87done
88# core = what CUDA++ owns; shared = the shared base it reached.
89printf "%s\n" "${!gpu[@]}" | sort | while read -r p; do if [[ -n ${shared[$p]:-} ]]; then echo "$p"; fi; done > "$out/shared-src.txt"
90printf "%s\n" "${!gpu[@]}" | sort | while read -r p; do if [[ -z ${shared[$p]:-} ]]; then echo "$p"; fi; done > "$out/gpu-src.txt"
91declare -A core=()
92while read -r p; do core[$p]=1; done < "$out/gpu-src.txt"
93
94# Tests and consumers.
95: > "$out/gpu-tests.txt"; : > "$out/tests-stay.txt"; : > "$out/consumers.txt"
96while IFS= read -r f; do
97 [[ $f == *.c || $f == *.h ]] || continue
98 [[ -n ${gpu[$f]:-} || -n ${stay[$f]:-} ]] && continue
99 incs=$(includes_of "$f")
100 hit=0 other=""
101 while IFS= read -r g; do
102 [[ -z $g ]] && continue
103 case $g in DRIVER:*|UNRESOLVED:*) continue;; esac
104 if [[ -n ${core[$g]:-} ]]; then hit=1
105 elif [[ -n ${shared[$g]:-} ]]; then :
106 elif [[ $g == tests/* ]]; then :
107 else other+=" $g"; fi
108 done <<< "$incs"
109 ((hit)) || continue
110 if [[ $f == tests/* && -z $other ]]; then echo "$f" >> "$out/gpu-tests.txt"
111 elif [[ $f == tests/* ]]; then echo "$f stays: includes$other" >> "$out/tests-stay.txt"
112 else echo "$f" >> "$out/consumers.txt"; fi
113done < "$out/.all"
114
115# Test-local helpers: a test moves only if every tests/ file it includes is
116# itself pure (includes only CUDA++ files, shared base, driver headers or other
117# pure test files); the pure helpers come along. Others stay in Omega.
118declare -A purity=()
119pure() {
120 local f=$1 g
121 [[ -n ${purity[$f]:-} ]] && { [[ ${purity[$f]} == y ]]; return; }
122 purity[$f]=n # cycle guard
123 while IFS= read -r g; do
124 [[ -z $g ]] && continue
125 case $g in DRIVER:*) continue;; UNRESOLVED:*) return 1;; esac
126 [[ -n ${core[$g]:-} || -n ${shared[$g]:-} ]] && continue
127 [[ $g == tests/* ]] && pure "$g" && continue
128 return 1
129 done <<< "$(includes_of "$f")"
130 purity[$f]=y
131}
132: > "$out/.keep"
133while IFS= read -r t; do
134 if pure "$t"; then
135 echo "$t" >> "$out/.keep"
136 while IFS= read -r g; do [[ $g == tests/* ]] && echo "$g" >> "$out/.keep"; done <<< "$(includes_of "$t")"
137 else
138 echo "$t stays: includes an Omega-only test helper" >> "$out/tests-stay.txt"
139 fi
140done < "$out/gpu-tests.txt"
141# a test helper header brings its body (tests/fake_m16_native.h -> .c)
142while read -r t; do c=${t%.h}.c; if [[ $t == *.h && -n ${tracked[$c]:-} ]]; then echo "$c"; fi; done < "$out/.keep" >> "$out/.keep2"
143cat "$out/.keep" "$out/.keep2" > "$out/gpu-tests.txt"; rm -f "$out/.keep" "$out/.keep2"
144sort -u -o "$out/gpu-tests.txt" "$out/gpu-tests.txt"
145sort -u -o "$out/driver-includes.txt" "$out/driver-includes.txt"
146
147# Same relative paths on both sides: an Omega branch patch then applies to CUDA++
148# unchanged (tools/extract/carry.sh). docs/NAMING.md holds the later rename plan.
149grep -v "^#" "$here/extra-paths.txt" | grep . > "$out/extra-paths.txt"
150cat "$out/gpu-src.txt" "$out/shared-src.txt" "$out/gpu-tests.txt" "$out/extra-paths.txt" > "$out/keep-paths.txt"
151awk '{print $1"\t"$1}' "$out/keep-paths.txt" > "$out/filemap.tsv"
152rm -f "$out/.all"
153
154echo "gpu-src $(wc -l < "$out/gpu-src.txt") shared $(wc -l < "$out/shared-src.txt") gpu-tests $(wc -l < "$out/gpu-tests.txt") tests-stay $(wc -l < "$out/tests-stay.txt") consumers $(wc -l < "$out/consumers.txt") unresolved $(wc -l < "$out/unresolved.txt") conflicts $(wc -l < "$out/conflicts.txt")"
155if [[ -s $out/conflicts.txt ]]; then echo "CONFLICT: the GPU part includes language or stay files (conflicts.txt)" >&2; exit 2; fi