authorgravatar for alex@alexrp.comAlex Rønne Petersen <alex@alexrp.com> 2025-03-26 15:50:31+01:00
committergravatar for alex@alexrp.comAlex Rønne Petersen <alex@alexrp.com> 2025-10-18 11:13:28+02:00
logf3da54f53c1db1484b00657de18e3677358549cd
treee356155b2f652c7a434ada3e3600529ef1a41e4b
parent631915ad961c83b8a2e4e36f11a278dae054372e

std.Target: move Cpu.supportsAddressSpace() up to here

This allows us to rule out support for certain address spaces based on the OS. This commit is just a refactor, however, and doesn't actually make use of that opportunity yet.

3 files changed, 33 insertions(+), 33 deletions(-)

lib/std/Target.zig+29-29
...@@ -1944,35 +1944,6 @@ pub const Cpu = struct {...@@ -1944,35 +1944,6 @@ pub const Cpu = struct {
1944 return Model.baseline(arch, os).toCpu(arch);1944 return Model.baseline(arch, os).toCpu(arch);
1945 }1945 }
19461946
1947 /// Returns whether this architecture supports `address_space`. If `context` is `null`, this
1948 /// function simply answers the general question of whether the architecture has any concept
1949 /// of `address_space`; if non-`null`, the function additionally checks whether
1950 /// `address_space` is valid in that context.
1951 pub fn supportsAddressSpace(
1952 cpu: Cpu,
1953 address_space: std.builtin.AddressSpace,
1954 context: ?std.builtin.AddressSpace.Context,
1955 ) bool {
1956 const arch = cpu.arch;
1957
1958 const is_nvptx = arch.isNvptx();
1959 const is_spirv = arch.isSpirV();
1960 const is_gpu = is_nvptx or is_spirv or arch == .amdgcn;
1961
1962 return switch (address_space) {
1963 .generic => true,
1964 .fs, .gs, .ss => (arch == .x86_64 or arch == .x86) and (context == null or context == .pointer),
1965 .flash, .flash1, .flash2, .flash3, .flash4, .flash5 => arch == .avr, // TODO this should also check how many flash banks the cpu has
1966 .cog, .hub => arch == .propeller,
1967 .lut => arch == .propeller and cpu.has(.propeller, .p2),
1968
1969 .global, .local, .shared => is_gpu,
1970 .constant => is_gpu and (context == null or context == .constant),
1971 .param => is_nvptx,
1972 .input, .output, .uniform, .push_constant, .storage_buffer, .physical_storage_buffer => is_spirv,
1973 };
1974 }
1975
1976 /// Returns true if `feature` is enabled.1947 /// Returns true if `feature` is enabled.
1977 pub fn has(cpu: Cpu, comptime family: Arch.Family, feature: @field(Target, @tagName(family)).Feature) bool {1948 pub fn has(cpu: Cpu, comptime family: Arch.Family, feature: @field(Target, @tagName(family)).Feature) bool {
1978 if (family != cpu.arch.family()) return false;1949 if (family != cpu.arch.family()) return false;
...@@ -2132,6 +2103,35 @@ pub fn requiresLibC(target: *const Target) bool {...@@ -2132,6 +2103,35 @@ pub fn requiresLibC(target: *const Target) bool {
2132 };2103 };
2133}2104}
21342105
2106/// Returns whether this target supports `address_space`. If `context` is `null`, this
2107/// function simply answers the general question of whether the target has any concept
2108/// of `address_space`; if non-`null`, the function additionally checks whether
2109/// `address_space` is valid in that context.
2110pub fn supportsAddressSpace(
2111 target: Target,
2112 address_space: std.builtin.AddressSpace,
2113 context: ?std.builtin.AddressSpace.Context,
2114) bool {
2115 const arch = target.cpu.arch;
2116
2117 const is_nvptx = arch.isNvptx();
2118 const is_spirv = arch.isSpirV();
2119 const is_gpu = is_nvptx or is_spirv or arch == .amdgcn;
2120
2121 return switch (address_space) {
2122 .generic => true,
2123 .fs, .gs, .ss => (arch == .x86_64 or arch == .x86) and (context == null or context == .pointer),
2124 .flash, .flash1, .flash2, .flash3, .flash4, .flash5 => arch == .avr, // TODO this should also check how many flash banks the cpu has
2125 .cog, .hub => arch == .propeller,
2126 .lut => arch == .propeller and std.Target.propeller.featureSetHas(target.cpu.features, .p2),
2127
2128 .global, .local, .shared => is_gpu,
2129 .constant => is_gpu and (context == null or context == .constant),
2130 .param => is_nvptx,
2131 .input, .output, .uniform, .push_constant, .storage_buffer, .physical_storage_buffer => is_spirv,
2132 };
2133}
2134
2135pub const DynamicLinker = struct {2135pub const DynamicLinker = struct {
2136 /// Contains the memory used to store the dynamic linker path. This field2136 /// Contains the memory used to store the dynamic linker path. This field
2137 /// should not be used directly. See `get` and `set`. This field exists so2137 /// should not be used directly. See `get` and `set`. This field exists so
src/Sema.zig+1-1
...@@ -36508,7 +36508,7 @@ pub fn analyzeAsAddressSpace(...@@ -36508,7 +36508,7 @@ pub fn analyzeAsAddressSpace(
36508 const address_space = try sema.interpretBuiltinType(block, src, addrspace_val, std.builtin.AddressSpace);36508 const address_space = try sema.interpretBuiltinType(block, src, addrspace_val, std.builtin.AddressSpace);
36509 const target = pt.zcu.getTarget();36509 const target = pt.zcu.getTarget();
3651036510
36511 if (!target.cpu.supportsAddressSpace(address_space, ctx)) {36511 if (!target.supportsAddressSpace(address_space, ctx)) {
36512 // TODO error messages could be made more elaborate here36512 // TODO error messages could be made more elaborate here
36513 const entity = switch (ctx) {36513 const entity = switch (ctx) {
36514 .function => "functions",36514 .function => "functions",
src/target.zig+3-3
...@@ -539,10 +539,10 @@ pub fn addrSpaceCastIsValid(...@@ -539,10 +539,10 @@ pub fn addrSpaceCastIsValid(
539 to: AddressSpace,539 to: AddressSpace,
540) bool {540) bool {
541 switch (target.cpu.arch) {541 switch (target.cpu.arch) {
542 .x86_64, .x86 => return target.cpu.supportsAddressSpace(from, null) and target.cpu.supportsAddressSpace(to, null),542 .x86_64, .x86 => return target.supportsAddressSpace(from, null) and target.supportsAddressSpace(to, null),
543 .nvptx64, .nvptx, .amdgcn => {543 .nvptx64, .nvptx, .amdgcn => {
544 const to_generic = target.cpu.supportsAddressSpace(from, null) and to == .generic;544 const to_generic = target.supportsAddressSpace(from, null) and to == .generic;
545 const from_generic = target.cpu.supportsAddressSpace(to, null) and from == .generic;545 const from_generic = target.supportsAddressSpace(to, null) and from == .generic;
546 return to_generic or from_generic;546 return to_generic or from_generic;
547 },547 },
548 else => return from == .generic and to == .generic,548 else => return from == .generic and to == .generic,