1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
|
const std = @import("std");
const Allocator = std.mem.Allocator;
const Path = std.Build.Cache.Path;
const assert = std.debug.assert;
const log = std.log.scoped(.link);
const Zcu = @import("../Zcu.zig");
const InternPool = @import("../InternPool.zig");
const Compilation = @import("../Compilation.zig");
const link = @import("../link.zig");
const Air = @import("../Air.zig");
const Type = @import("../Type.zig");
const CodeGen = @import("../codegen/spirv/CodeGen.zig");
const Module = @import("../codegen/spirv/Module.zig");
const trace = @import("../tracy.zig").trace;
const BinaryModule = @import("SpirV/BinaryModule.zig");
const lower_invocation_globals = @import("SpirV/lower_invocation_globals.zig");
const spec = @import("../codegen/spirv/spec.zig");
const Id = spec.Id;
const Word = spec.Word;
const Linker = @This();
base: link.File,
module: Module,
cg: CodeGen,
pub fn createEmpty(
arena: Allocator,
comp: *Compilation,
emit: Path,
options: link.File.OpenOptions,
) !*Linker {
const gpa = comp.gpa;
const target = &comp.root_mod.resolved_target.result;
assert(!comp.config.use_lld); // Caught by Compilation.Config.resolve
assert(!comp.config.use_llvm); // Caught by Compilation.Config.resolve
assert(target.ofmt == .spirv); // Caught by Compilation.Config.resolve
switch (target.cpu.arch) {
.spirv32, .spirv64 => {},
else => unreachable, // Caught by Compilation.Config.resolve.
}
switch (target.os.tag) {
.opencl, .opengl, .vulkan => {},
else => unreachable, // Caught by Compilation.Config.resolve.
}
const linker = try arena.create(Linker);
linker.* = .{
.base = .{
.tag = .spirv,
.comp = comp,
.emit = emit,
.gc_sections = options.gc_sections orelse false,
.print_gc_sections = options.print_gc_sections,
.stack_size = options.stack_size orelse 0,
.allow_shlib_undefined = options.allow_shlib_undefined orelse false,
.file = null,
.build_id = options.build_id,
},
.module = .{
.gpa = gpa,
.arena = arena,
.zcu = comp.zcu.?,
},
.cg = .{
// These fields are populated in generate()
.pt = undefined,
.air = undefined,
.liveness = undefined,
.owner_nav = undefined,
.module = undefined,
.control_flow = .{ .structured = .{} },
.base_line = undefined,
},
};
errdefer linker.deinit();
linker.base.file = try emit.root_dir.handle.createFile(emit.sub_path, .{
.truncate = true,
.read = true,
});
return linker;
}
pub fn open(
arena: Allocator,
comp: *Compilation,
emit: Path,
options: link.File.OpenOptions,
) !*Linker {
return createEmpty(arena, comp, emit, options);
}
pub fn deinit(linker: *Linker) void {
linker.cg.deinit();
linker.module.deinit();
}
fn generate(
linker: *Linker,
pt: Zcu.PerThread,
nav_index: InternPool.Nav.Index,
air: Air,
liveness: Air.Liveness,
do_codegen: bool,
) !void {
const zcu = pt.zcu;
const gpa = zcu.gpa;
const structured_cfg = zcu.navFileScope(nav_index).mod.?.structured_cfg;
linker.cg.control_flow.deinit(gpa);
linker.cg.args.clearRetainingCapacity();
linker.cg.inst_results.clearRetainingCapacity();
linker.cg.id_scratch.clearRetainingCapacity();
linker.cg.prologue.reset();
linker.cg.body.reset();
linker.cg = .{
.pt = pt,
.air = air,
.liveness = liveness,
.owner_nav = nav_index,
.module = &linker.module,
.control_flow = switch (structured_cfg) {
true => .{ .structured = .{} },
false => .{ .unstructured = .{} },
},
.base_line = zcu.navSrcLine(nav_index),
.args = linker.cg.args,
.inst_results = linker.cg.inst_results,
.id_scratch = linker.cg.id_scratch,
.prologue = linker.cg.prologue,
.body = linker.cg.body,
};
linker.cg.genNav(do_codegen) catch |err| switch (err) {
error.CodegenFail => switch (zcu.codegenFailMsg(nav_index, linker.cg.error_msg.?)) {
error.CodegenFail => {},
error.OutOfMemory => |e| return e,
},
else => |other| {
// There might be an error that happened *after* linker.error_msg
// was already allocated, so be sure to free it.
if (linker.cg.error_msg) |error_msg| {
error_msg.deinit(gpa);
}
return other;
},
};
}
pub fn updateFunc(
linker: *Linker,
pt: Zcu.PerThread,
func_index: InternPool.Index,
air: *const Air,
liveness: *const ?Air.Liveness,
) !void {
const nav = pt.zcu.funcInfo(func_index).owner_nav;
// TODO: Separate types for generating decls and functions?
try linker.generate(pt, nav, air.*, liveness.*.?, true);
}
pub fn updateNav(linker: *Linker, pt: Zcu.PerThread, nav: InternPool.Nav.Index) link.File.UpdateNavError!void {
const ip = &pt.zcu.intern_pool;
log.debug("lowering nav {f}({d})", .{ ip.getNav(nav).fqn.fmt(ip), nav });
try linker.generate(pt, nav, undefined, undefined, false);
}
pub fn updateExports(
linker: *Linker,
pt: Zcu.PerThread,
exported: Zcu.Exported,
export_indices: []const Zcu.Export.Index,
) !void {
const zcu = pt.zcu;
const ip = &zcu.intern_pool;
const nav_index = switch (exported) {
.nav => |nav| nav,
.uav => |uav| {
_ = uav;
@panic("TODO: implement Linker linker code for exporting a constant value");
},
};
const nav_ty = ip.getNav(nav_index).typeOf(ip);
const target = zcu.getTarget();
if (ip.isFunctionType(nav_ty)) {
const spv_decl_index = try linker.module.resolveNav(ip, nav_index);
const cc = Type.fromInterned(nav_ty).fnCallingConvention(zcu);
const exec_model: spec.ExecutionModel = switch (target.os.tag) {
.vulkan, .opengl => switch (cc) {
.spirv_vertex => .vertex,
.spirv_fragment => .fragment,
.spirv_kernel => .gl_compute,
// TODO: We should integrate with the Linkage capability and export this function
.spirv_device => return,
else => unreachable,
},
.opencl => switch (cc) {
.spirv_kernel => .kernel,
// TODO: We should integrate with the Linkage capability and export this function
.spirv_device => return,
else => unreachable,
},
else => unreachable,
};
for (export_indices) |export_idx| {
const exp = export_idx.ptr(zcu);
try linker.module.declareEntryPoint(
spv_decl_index,
exp.opts.name.toSlice(ip),
exec_model,
null,
);
}
}
// TODO: Export regular functions, variables, etc using Linkage attributes.
}
pub fn flush(
linker: *Linker,
arena: Allocator,
tid: Zcu.PerThread.Id,
prog_node: std.Progress.Node,
) link.File.FlushError!void {
// The goal is to never use this because it's only needed if we need to
// write to InternPool, but flush is too late to be writing to the
// InternPool.
_ = tid;
const tracy = trace(@src());
defer tracy.end();
const sub_prog_node = prog_node.start("Flush Module", 0);
defer sub_prog_node.end();
const comp = linker.base.comp;
const diags = &comp.link_diags;
const gpa = comp.gpa;
// We need to export the list of error names somewhere so that we can pretty-print them in the
// executor. This is not really an important thing though, so we can just dump it in any old
// nonsemantic instruction. For now, just put it in OpSourceExtension with a special name.
var error_info: std.Io.Writer.Allocating = .init(linker.module.gpa);
defer error_info.deinit();
error_info.writer.writeAll("zig_errors:") catch return error.OutOfMemory;
const ip = &linker.base.comp.zcu.?.intern_pool;
for (ip.global_error_set.getNamesFromMainThread()) |name| {
// Errors can contain pretty much any character - to encode them in a string we must escape
// them somehow. Easiest here is to use some established scheme, one which also preseves the
// name if it contains no strange characters is nice for debugging. URI encoding fits the bill.
// We're using : as separator, which is a reserved character.
error_info.writer.writeByte(':') catch return error.OutOfMemory;
std.Uri.Component.percentEncode(
&error_info.writer,
name.toSlice(ip),
struct {
fn isValidChar(c: u8) bool {
return switch (c) {
0, '%', ':' => false,
else => true,
};
}
}.isValidChar,
) catch return error.OutOfMemory;
}
try linker.module.sections.debug_strings.emit(gpa, .OpSourceExtension, .{
.extension = error_info.written(),
});
const module = try linker.module.finalize(arena);
errdefer arena.free(module);
const linked_module = linkModule(arena, module, sub_prog_node) catch |err| switch (err) {
error.OutOfMemory => return error.OutOfMemory,
else => |other| return diags.fail("error while linking: {s}", .{@errorName(other)}),
};
// TODO endianness bug. use file writer and call writeSliceEndian instead
linker.base.file.?.writeAll(@ptrCast(linked_module)) catch |err|
return diags.fail("failed to write: {s}", .{@errorName(err)});
}
fn linkModule(arena: Allocator, module: []Word, progress: std.Progress.Node) ![]Word {
var parser = try BinaryModule.Parser.init(arena);
defer parser.deinit();
var binary = try parser.parse(module);
try lower_invocation_globals.run(&parser, &binary, progress);
return binary.finalize(arena);
}
|