lib/accy/src/kernel/compile/execution.zig
daab053ee43316e1809a84551d573ddd1e5bf3d2
1 const std = @import("std");
2 const gpu = @import("gpu");
3 const sys = @import("sys");
4 const exec = @import("../../executable/root.zig");
5 const artifact = @import("root.zig").artifact;
6 const program = @import("../program/root.zig");
7
8 const FragmentCompilerOptions = exec.FragmentCompilerOptions;
9 const ArtifactOptions = exec.KernelCompilerOptions;
10
11 pub fn compileFragment(
12 allocator: std.mem.Allocator,
13 handle: gpu.BackendHandle,
14 graph: *program.Program,
15 options: FragmentCompilerOptions,
16 ) !*exec.CompiledFragment {
17 const artifact_start = nowNs();
18 const artifact_job = try artifact.createJob(
19 allocator,
20 handle,
21 graph,
22 artifactPlanOptions(options),
23 );
24 defer artifact_job.deinit();
25 try options.instrumentation.record(.plan_create_backend_artifacts, artifact_start);
26 return try exec.compileFragmentFromArtifactJob(allocator, artifact_job);
27 }
28
29 pub fn createArtifact(
30 allocator: std.mem.Allocator,
31 handle: gpu.BackendHandle,
32 graph: *program.Program,
33 options: ArtifactOptions,
34 ) !gpu.KernelArtifact {
35 const compiled = try compileFragment(allocator, handle, graph, .{
36 .artifact_format = options.artifact_format,
37 .authored_kernel_diagnostic_id = options.authored_kernel_diagnostic_id,
38 .instrumentation = options.instrumentation,
39 });
40 defer compiled.deinit();
41 if (compiled.kernelCount() != 1) return error.InvalidArtifact;
42 return try exec.candidate.copyKernelArtifactToAllocator(
43 allocator,
44 compiled.artifactPlan().kernels.items[0].artifact,
45 );
46 }
47
48 fn artifactPlanOptions(options: FragmentCompilerOptions) artifact.Options {
49 return .{
50 .format = options.artifact_format,
51 .kernel_plan = .{ .diagnostic_id = options.authored_kernel_diagnostic_id },
52 };
53 }
54
55 fn nowNs() i128 {
56 return sys.time.nanoTimestamp();
57 }