Skip to content

Commit fccbb2d

Browse files
D3D12: ports keyed by (name, code object), so one kernel name can have ports of several builds (#143)
2 parents 34197ba + 0cae366 commit fccbb2d

4 files changed

Lines changed: 67 additions & 22 deletions

File tree

‎clean/dx12/README.md‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,10 @@ WG_KERNEL(exposure_auto_diet) { WG_SETUP(); ... } // entry name = HIP kernel n
109109
* `co` (required) = file name of the HIP code object whose build of the kernel the port reproduces. The device uses
110110
the port only while the runtime has exactly that code object loaded (untagged, or under the name's tag); any other
111111
build variant of the same kernel name (`post_w64.co` / `post_w64__q5.co` under `w64`, a `swin.co` variant,
112-
`dec1_dec0__pack12__div*.co` under `d1p12`, ...) runs on HIP, logged once.
112+
`dec1_dec0__pack12__div*.co` under `d1p12`, ...) runs on HIP, logged once. One name may have ports of several
113+
builds (one manifest entry per name and `co`, for example `w64/post_tf_f32_p12_diet_vsit` of `post_w64__orig.co`
114+
and of `post_w64.co`): the device runs the port whose code object is loaded. Every port of a name but the first
115+
sets `file=<stem>`, its DXIL file name (`<stem>.dxil`).
113116
* `args` = the HIP kernel's arguments in order: `p` pointer (DWORD: arena byte offset), `u` any 32-bit scalar,
114117
`b<N>` struct by value (N bytes = N/4 DWORDs, copied verbatim). At most 57 DWORDs. The `WG_ARGS_BEGIN` block must
115118
declare exactly these DWORDs in order, **32-bit scalars only** (no vectors / arrays / structs: cbuffer packing).

‎clean/dx12/build_dx12.py‎

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,10 @@
1313
the tag. co is the file name of the HIP code object whose build of the kernel this port reproduces ("swin.co",
1414
"post_w64__orig.co"): the device runs the port only while the runtime has exactly that code object loaded (untagged, or
1515
under the name's tag), so a different build variant of the same kernel name keeps running on HIP. co=- marks a
16-
D3D12-only kernel with no HIP counterpart (the device self-test's kernels): it always runs on D3D12. args lists the HIP kernel's arguments in order: p = pointer (a uint byte offset into the arena; 0 = null),
16+
D3D12-only kernel with no HIP counterpart (the device self-test's kernels): it always runs on D3D12. One name may have
17+
ports of several builds of its HIP kernel (for example w64/post_tf_f32_p12_diet_vsit of post_w64__orig.co and of
18+
post_w64.co): one manifest entry per (name, co), and the device runs the one whose code object is loaded. Every port but
19+
the first of a name sets file=<stem> (its DXIL is <stem>.dxil; default <tag>__<kernel>). args lists the HIP kernel's arguments in order: p = pointer (a uint byte offset into the arena; 0 = null),
1720
u = any 32-bit scalar, b<N> = a struct of N bytes passed by value (N / 4 uint constants).
1821
1922
Arithmetic mode ("exact", README.md): DXC compiles with IEEE strictness (-Gis: no fast-math flags, so no contraction, no
@@ -158,6 +161,9 @@ def kernel_decls(path):
158161
d["gscale"] = int(kv.get("gscale", d["block"] // d["group"]))
159162
if d["gscale"] < 1 or d["gscale"] > d["block"] // d["group"]:
160163
raise SystemExit(f"{path}: {name}: gscale {d['gscale']}")
164+
d["file"] = kv.get("file", "") # DXIL file stem of a second port of the same name (module doc)
165+
if d["file"] and not re.fullmatch(r"[A-Za-z0-9_]+", d["file"]):
166+
raise SystemExit(f"{path}: {name}: file={d['file']} (letters, digits and _ only)")
161167
d["rs"] = kv.get("rs", "buffer")
162168
if d["rs"] not in ("buffer", "tex", "direct"):
163169
raise SystemExit(f"{path}: {name}: unsupported root-signature kind {d['rs']}")
@@ -261,8 +267,8 @@ def main(argv=None):
261267
old = {}
262268
if only and os.path.exists(man_path): # partial build: keep the other kernels' entries
263269
with open(man_path) as f:
264-
old = {k["name"]: k for k in json.load(f)["kernels"]}
265-
kernels = dict(old)
270+
old = {(k["name"], k.get("co", "")): k for k in json.load(f)["kernels"]}
271+
kernels = dict(old) # (name, co) -> manifest entry
266272
built = []
267273
# generated kernels (kernels/fused_*_gen.py: the fused forms of swin_block.hlsl / bneck.hlsl) are regenerated from
268274
# the live sources first, so that no build compiles a stale copy
@@ -277,10 +283,15 @@ def main(argv=None):
277283
for d in kernel_decls(src):
278284
if only and d["name"] not in only:
279285
continue
280-
if d["name"] in kernels and d["name"] not in old:
281-
raise SystemExit(f"kernel {d['name']} declared twice")
282-
kernels[d["name"]] = None # placeholder: the declared-twice check sees it before any compile starts
283-
jobs.append((src, d, os.path.join(dst, output_name(d["name"]))))
286+
key = (d["name"], d["co"])
287+
if key in kernels and key not in old:
288+
raise SystemExit(f"kernel {d['name']} ({d['co']}) declared twice")
289+
kernels[key] = None # placeholder: the declared-twice check sees it before any compile starts
290+
out = os.path.join(dst, d["file"] + ".dxil" if d["file"] else output_name(d["name"]))
291+
if any(j[2] == out for j in jobs):
292+
raise SystemExit(f"kernel {d['name']} ({d['co']}): DXIL file {os.path.basename(out)} already used "
293+
"(a second port of a name needs file=<stem>)")
294+
jobs.append((src, d, out))
284295

285296
def build_one(job):
286297
src, d, out = job
@@ -308,16 +319,17 @@ def build_one(job):
308319
futures = [pool.submit(build_one, j) for j in jobs]
309320
try:
310321
for (src, d, out), fut in zip(jobs, futures):
311-
kernels[d["name"]] = fut.result()
322+
kernels[(d["name"], d["co"])] = fut.result()
312323
built.append((d["name"], out))
313324
print(f"{d['name']:40s} -> {os.path.basename(out)} block {d['block']} wave {d['wave']} "
314325
f"args {d['args']}", flush=True)
315326
except BaseException:
316327
for fut in futures:
317328
fut.cancel() # kernels not started yet; the running ones finish their current child process
318329
raise
319-
if only and not (only <= set(kernels)):
320-
raise SystemExit(f"not declared in clean/dx12/kernels: {sorted(only - set(kernels))}")
330+
names = {n for n, _ in kernels}
331+
if only and not (only <= names):
332+
raise SystemExit(f"not declared in clean/dx12/kernels: {sorted(only - names)}")
321333
vgpr_check(built)
322334
with open(man_path, "w") as f:
323335
json.dump({"format": FORMAT, "dxc": os.path.basename(os.path.dirname(os.path.dirname(DXC_DIR))),

‎clean/runtime/device_d3d12.cpp‎

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -604,10 +604,19 @@ class NativeD3D12Device final : public Device, public NativeD3D12Ext {
604604
tagged_cos_[tag] = co; return true;
605605
}
606606
void unordered_next() override { unordered_next_ = true; }
607+
// The port of `name` that the loaded code objects select. One name can have ports of several builds of its HIP kernel
608+
// (manifest entries with the same name and different "co", for example post_w64.co and post_w64__orig.co); without
609+
// a selected one, the first port (variant_loaded then names what it requires).
610+
NativeKernel* find_port(const std::string& name) {
611+
auto range = kernels_.equal_range(name);
612+
for (auto i = range.first; i != range.second; ++i)
613+
if (variant_matches(i->second)) return &i->second;
614+
return range.first == range.second ? nullptr : &range.first->second;
615+
}
607616
bool launch(const std::string& name, unsigned grid, unsigned block, void** args) override {
608-
auto i = kernels_.find(name);
609-
if (i == kernels_.end()) return fail("native D3D12 unsupported kernel: " + name + "; no HIP fallback");
610-
NativeKernel& k = i->second;
617+
NativeKernel* port = find_port(name);
618+
if (!port) return fail("native D3D12 unsupported kernel: " + name + "; no HIP fallback");
619+
NativeKernel& k = *port;
611620
// Legacy tex ports have a different ABI and remain unsupported. A direct port needs this frame's typed
612621
// descriptors before variant/argument inspection, including for a caller with no argument array.
613622
const bool direct = k.rs == "direct";
@@ -1278,7 +1287,10 @@ class NativeD3D12Device final : public Device, public NativeD3D12Ext {
12781287
}
12791288
if (words != k.dwords) return fail("native D3D12 manifest: argument DWORD count mismatch for " + k.name);
12801289
k.file = directory + filename; const std::string key = k.name;
1281-
if (!kernels_.emplace(key, std::move(k)).second) return fail("native D3D12 manifest: duplicate kernel " + key);
1290+
auto same = kernels_.equal_range(key); // one port per (name, code object)
1291+
for (auto i = same.first; i != same.second; ++i)
1292+
if (i->second.co == k.co) return fail("native D3D12 manifest: duplicate kernel " + key + " (" + k.co + ")");
1293+
kernels_.emplace(key, std::move(k));
12821294
}
12831295
return true;
12841296
}
@@ -1659,7 +1671,7 @@ class NativeD3D12Device final : public Device, public NativeD3D12Ext {
16591671
std::map<uint64_t, uint64_t> free_;
16601672
std::map<uint64_t, NativeAllocation> used_;
16611673
std::map<uintptr_t, Host> hosts_;
1662-
std::map<std::string, NativeKernel> kernels_;
1674+
std::multimap<std::string, NativeKernel> kernels_; // by name; several ports of one name differ in "co"
16631675
std::set<std::string> untagged_cos_;
16641676
std::map<std::string, std::string> tagged_cos_;
16651677
std::vector<NativeEvent> events_;

‎clean/runtime/device_dx12.cpp‎

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -239,12 +239,12 @@ class Dx12Device : public Device {
239239

240240
// ---- launches ----------------------------------------------------------------------------------------------------
241241
bool launch(const std::string& kernel, unsigned grid, unsigned block, void** args) override {
242-
auto it = kernels_.find(kernel);
242+
Kernel* port = find_port(kernel);
243243
uint32_t c[kRuntimeConstants + kMaxArgDwords];
244-
if (it == kernels_.end() || !arena_ || !variant_loaded(it->second) || !pack_args(it->second, args, c + kRuntimeConstants)) {
244+
if (!port || !arena_ || !variant_loaded(*port) || !pack_args(*port, args, c + kRuntimeConstants)) {
245245
return to_hip() && fwd(hip_->launch(kernel, grid, block, args));
246246
}
247-
Kernel& k = it->second;
247+
Kernel& k = *port;
248248
if (block != k.block)
249249
return fail("D3D12 kernel " + kernel + " is built for " + std::to_string(k.block) + " lanes, launched with " +
250250
std::to_string(block));
@@ -272,8 +272,8 @@ class Dx12Device : public Device {
272272
}
273273
bool launch_timed(const std::string& kernel, unsigned grid, unsigned block, void** args, int start_event,
274274
int stop_event) override {
275-
auto it = kernels_.find(kernel);
276-
if (it == kernels_.end() || !arena_ || !variant_loaded(it->second))
275+
Kernel* port = find_port(kernel);
276+
if (!port || !arena_ || !variant_loaded(*port))
277277
return to_hip() && fwd(hip_->launch_timed(kernel, grid, block, args, start_event, stop_event));
278278
return Device::launch_timed(kernel, grid, block, args, start_event, stop_event);
279279
}
@@ -297,6 +297,24 @@ class Dx12Device : public Device {
297297

298298
static std::string base_name(const std::string& path) { return path.substr(path.find_last_of("/\\") + 1); }
299299
// Is the HIP code object this port reproduces the one the runtime loaded for the kernel's name?
300+
// Whether the runtime's loaded code objects select port k (variant_loaded without its cache and note).
301+
bool variant_selected(const Kernel& k) const {
302+
if (k.co == "-") return true;
303+
const size_t slash = k.name.find('/');
304+
if (slash == std::string::npos) return !k.co.empty() && untagged_cos_.count(k.co);
305+
auto t = tagged_cos_.find(k.name.substr(0, slash));
306+
return !k.co.empty() && t != tagged_cos_.end() && t->second == k.co;
307+
}
308+
// The port of `kernel` that the loaded code objects select. One name can have ports of several builds of its HIP
309+
// kernel (manifest entries with the same name and different "co"); without a selected one, the first port
310+
// (variant_loaded then notes once that the kernel runs on HIP).
311+
Kernel* find_port(const std::string& kernel) {
312+
auto range = kernels_.equal_range(kernel);
313+
for (auto i = range.first; i != range.second; ++i)
314+
if (variant_selected(i->second)) return &i->second;
315+
return range.first == range.second ? nullptr : &range.first->second;
316+
}
317+
300318
bool variant_loaded(Kernel& k) {
301319
if (k.co_gen != module_gen_) {
302320
k.co_gen = module_gen_;
@@ -732,7 +750,7 @@ class Dx12Device : public Device {
732750
DevPtr arena_hip_ = 0;
733751
uint64_t arena_bytes_ = 0, in_use_ = 0, fence_value_ = 0;
734752
std::map<uint64_t, uint64_t> free_, used_; // offset -> bytes
735-
std::map<std::string, Kernel> kernels_;
753+
std::multimap<std::string, Kernel> kernels_; // by name; several ports of one name differ in "co"
736754
std::set<std::string> untagged_cos_; // file names of the code objects loaded without a tag
737755
std::map<std::string, std::string> tagged_cos_; // tag -> file name of the code object loaded under it
738756
unsigned module_gen_ = 0;

0 commit comments

Comments
 (0)