aboutsummaryrefslogtreecommitdiff
path: root/src/link/SpirV.zig
diff options
context:
space:
mode:
authormlugg <mlugg@mlugg.co.uk>2024-07-28 17:09:14 +0100
committermlugg <mlugg@mlugg.co.uk>2024-08-11 07:29:41 +0100
commit548a087fafeda5b07d2237d5137906b8d07da699 (patch)
tree69135f129b84ab5b65f443d0a52899b232696e2b /src/link/SpirV.zig
parent531cd177e89c1edfcd2e52f74f220eb186a25f78 (diff)
downloadzig-548a087fafeda5b07d2237d5137906b8d07da699.tar.gz
zig-548a087fafeda5b07d2237d5137906b8d07da699.zip
compiler: split Decl into Nav and Cau
The type `Zcu.Decl` in the compiler is problematic: over time it has gained many responsibilities. Every source declaration, container type, generic instantiation, and `@extern` has a `Decl`. The functions of these `Decl`s are in some cases entirely disjoint. After careful analysis, I determined that the two main responsibilities of `Decl` are as follows: * A `Decl` acts as the "subject" of semantic analysis at comptime. A single unit of analysis is either a runtime function body, or a `Decl`. It registers incremental dependencies, tracks analysis errors, etc. * A `Decl` acts as a "global variable": a pointer to it is consistent, and it may be lowered to a specific symbol by the codegen backend. This commit eliminates `Decl` and introduces new types to model these responsibilities: `Cau` (Comptime Analysis Unit) and `Nav` (Named Addressable Value). Every source declaration, and every container type requiring resolution (so *not* including `opaque`), has a `Cau`. For a source declaration, this `Cau` performs the resolution of its value. (When #131 is implemented, it is unsolved whether type and value resolution will share a `Cau` or have two distinct `Cau`s.) For a type, this `Cau` is the context in which type resolution occurs. Every non-`comptime` source declaration, every generic instantiation, and every distinct `extern` has a `Nav`. These are sent to codegen/link: the backends by definition do not care about `Cau`s. This commit has some minor technically-breaking changes surrounding `usingnamespace`. I don't think they'll impact anyone, since the changes are fixes around semantics which were previously inconsistent (the behavior changed depending on hashmap iteration order!). Aside from that, this changeset has no significant user-facing changes. Instead, it is an internal refactor which makes it easier to correctly model the responsibilities of different objects, particularly regarding incremental compilation. The performance impact should be negligible, but I will take measurements before merging this work into `master`. Co-authored-by: Jacob Young <jacobly0@users.noreply.github.com> Co-authored-by: Jakub Konka <kubkon@jakubkonka.com>
Diffstat (limited to 'src/link/SpirV.zig')
-rw-r--r--src/link/SpirV.zig40
1 files changed, 20 insertions, 20 deletions
diff --git a/src/link/SpirV.zig b/src/link/SpirV.zig
index ce7e25824c..e97c80c3fe 100644
--- a/src/link/SpirV.zig
+++ b/src/link/SpirV.zig
@@ -36,6 +36,7 @@ const trace = @import("../tracy.zig").trace;
const build_options = @import("build_options");
const Air = @import("../Air.zig");
const Liveness = @import("../Liveness.zig");
+const Type = @import("../Type.zig");
const Value = @import("../Value.zig");
const SpvModule = @import("../codegen/spirv/Module.zig");
@@ -50,8 +51,6 @@ base: link.File,
object: codegen.Object,
-pub const base_tag: link.File.Tag = .spirv;
-
pub fn createEmpty(
arena: Allocator,
comp: *Compilation,
@@ -128,22 +127,22 @@ pub fn updateFunc(self: *SpirV, pt: Zcu.PerThread, func_index: InternPool.Index,
@panic("Attempted to compile for architecture that was disabled by build configuration");
}
+ const ip = &pt.zcu.intern_pool;
const func = pt.zcu.funcInfo(func_index);
- const decl = pt.zcu.declPtr(func.owner_decl);
- log.debug("lowering function {}", .{decl.name.fmt(&pt.zcu.intern_pool)});
+ log.debug("lowering function {}", .{ip.getNav(func.owner_nav).name.fmt(ip)});
try self.object.updateFunc(pt, func_index, air, liveness);
}
-pub fn updateDecl(self: *SpirV, pt: Zcu.PerThread, decl_index: InternPool.DeclIndex) !void {
+pub fn updateNav(self: *SpirV, pt: Zcu.PerThread, nav: InternPool.Nav.Index) !void {
if (build_options.skip_non_native) {
@panic("Attempted to compile for architecture that was disabled by build configuration");
}
- const decl = pt.zcu.declPtr(decl_index);
- log.debug("lowering declaration {}", .{decl.name.fmt(&pt.zcu.intern_pool)});
+ const ip = &pt.zcu.intern_pool;
+ log.debug("lowering declaration {}", .{ip.getNav(nav).name.fmt(ip)});
- try self.object.updateDecl(pt, decl_index);
+ try self.object.updateNav(pt, nav);
}
pub fn updateExports(
@@ -152,19 +151,20 @@ pub fn updateExports(
exported: Zcu.Exported,
export_indices: []const u32,
) !void {
- const mod = pt.zcu;
- const decl_index = switch (exported) {
- .decl_index => |i| i,
- .value => |val| {
- _ = val;
+ const zcu = pt.zcu;
+ const ip = &zcu.intern_pool;
+ const nav_index = switch (exported) {
+ .nav => |nav| nav,
+ .uav => |uav| {
+ _ = uav;
@panic("TODO: implement SpirV linker code for exporting a constant value");
},
};
- const decl = mod.declPtr(decl_index);
- if (decl.val.isFuncBody(mod)) {
- const target = mod.getTarget();
- const spv_decl_index = try self.object.resolveDecl(mod, decl_index);
- const execution_model = switch (decl.typeOf(mod).fnCallingConvention(mod)) {
+ const nav_ty = ip.getNav(nav_index).typeOf(ip);
+ if (ip.isFunctionType(nav_ty)) {
+ const target = zcu.getTarget();
+ const spv_decl_index = try self.object.resolveNav(zcu, nav_index);
+ const execution_model = switch (Type.fromInterned(nav_ty).fnCallingConvention(zcu)) {
.Vertex => spec.ExecutionModel.Vertex,
.Fragment => spec.ExecutionModel.Fragment,
.Kernel => spec.ExecutionModel.Kernel,
@@ -177,10 +177,10 @@ pub fn updateExports(
(is_vulkan and (execution_model == .Fragment or execution_model == .Vertex)))
{
for (export_indices) |export_idx| {
- const exp = mod.all_exports.items[export_idx];
+ const exp = zcu.all_exports.items[export_idx];
try self.object.spv.declareEntryPoint(
spv_decl_index,
- exp.opts.name.toSlice(&mod.intern_pool),
+ exp.opts.name.toSlice(ip),
execution_model,
);
}