| ... | @@ -3,35 +3,30 @@ const Target = std.Target; | ... | @@ -3,35 +3,30 @@ const Target = std.Target; |
| 3 | const CrossTarget = std.zig.CrossTarget; | 3 | const CrossTarget = std.zig.CrossTarget; |
| 4 | | 4 | |
| 5 | fn setFeature(cpu: *Target.Cpu, feature: Target.x86.Feature, enabled: bool) void { | 5 | fn setFeature(cpu: *Target.Cpu, feature: Target.x86.Feature, enabled: bool) void { |
| 6 | | | |
| 7 | const idx = @as(Target.Cpu.Feature.Set.Index, @enumToInt(feature)); | 6 | const idx = @as(Target.Cpu.Feature.Set.Index, @enumToInt(feature)); |
| 8 | | 7 | |
| 9 | if(enabled) cpu.features.addFeature(idx) | 8 | if (enabled) cpu.features.addFeature(idx) else cpu.features.removeFeature(idx); |
| 10 | else cpu.features.removeFeature(idx); | | |
| 11 | } | 9 | } |
| 12 | | 10 | |
| 13 | inline fn bit(input: u32, offset: u5) bool { | 11 | inline fn bit(input: u32, offset: u5) bool { |
| 14 | return (input >> offset) & 1 != 0; | 12 | return (input >> offset) & 1 != 0; |
| 15 | } | 13 | } |
| 16 | | 14 | |
| 17 | pub fn detectNativeCpuAndFeatures(cross_target: CrossTarget) Target.Cpu { | 15 | pub fn detectNativeCpuAndFeatures(arch: Target.Cpu.Arch, os: Target.Os, cross_target: CrossTarget) Target.Cpu { |
| 18 | | 16 | var cpu = Target.Cpu{ |
| 19 | var arch = cross_target.getCpuArch(); | | |
| 20 | | | |
| 21 | var cpu = Target.Cpu { | | |
| 22 | .arch = arch, | 17 | .arch = arch, |
| 23 | .model = Target.Cpu.Model.baseline(arch), | 18 | .model = Target.Cpu.Model.generic(arch), |
| 24 | .features = Target.Cpu.Feature.Set.empty, | 19 | .features = Target.Cpu.Feature.Set.empty, |
| 25 | }; | 20 | }; |
| 26 | | 21 | |
| 27 | detectNativeFeatures(&cpu, cross_target.getOsTag()); | 22 | // First we detect features, to use as hints when detecting CPU Model. |
| | 23 | detectNativeFeatures(&cpu, os.tag); |
| 28 | | 24 | |
| 29 | var leaf = cpuid(0, 0); | 25 | var leaf = cpuid(0, 0); |
| 30 | const max_leaf = leaf.eax; | 26 | const max_leaf = leaf.eax; |
| 31 | const vendor = leaf.ebx; | 27 | const vendor = leaf.ebx; |
| 32 | | 28 | |
| 33 | if(max_leaf > 0) { | 29 | if (max_leaf > 0) { |
| 34 | | | |
| 35 | leaf = cpuid(0x1, 0); | 30 | leaf = cpuid(0x1, 0); |
| 36 | | 31 | |
| 37 | const brand_id = leaf.ebx & 0xff; | 32 | const brand_id = leaf.ebx & 0xff; |
| ... | @@ -49,7 +44,8 @@ pub fn detectNativeCpuAndFeatures(cross_target: CrossTarget) Target.Cpu { | ... | @@ -49,7 +44,8 @@ pub fn detectNativeCpuAndFeatures(cross_target: CrossTarget) Target.Cpu { |
| 49 | } | 44 | } |
| 50 | } | 45 | } |
| 51 | | 46 | |
| 52 | switch(vendor) { | 47 | // Now we detect the model. |
| | 48 | switch (vendor) { |
| 53 | 0x756e6547 => { | 49 | 0x756e6547 => { |
| 54 | detectIntelProcessor(&cpu, family, model, brand_id); | 50 | detectIntelProcessor(&cpu, family, model, brand_id); |
| 55 | }, | 51 | }, |
| ... | @@ -60,19 +56,21 @@ pub fn detectNativeCpuAndFeatures(cross_target: CrossTarget) Target.Cpu { | ... | @@ -60,19 +56,21 @@ pub fn detectNativeCpuAndFeatures(cross_target: CrossTarget) Target.Cpu { |
| 60 | } | 56 | } |
| 61 | } | 57 | } |
| 62 | | 58 | |
| 63 | var model_features = cpu.model.features; | 59 | // Add the CPU model's feature set into the working set, but then |
| 64 | model_features.populateDependencies(cpu.arch.allFeaturesList()); | 60 | // override with actual detected features again. |
| 65 | cpu.features.addFeatureSet(model_features); | 61 | cpu.features.addFeatureSet(cpu.model.features); |
| | 62 | detectNativeFeatures(&cpu, os.tag); |
| 66 | | 63 | |
| 67 | return cpu; | 64 | cpu.features.populateDependencies(cpu.arch.allFeaturesList()); |
| 68 | | 65 | |
| | 66 | return cpu; |
| 69 | } | 67 | } |
| 70 | | 68 | |
| 71 | fn detectIntelProcessor(cpu: *Target.Cpu, family: u32, model: u32, brand_id: u32) void { | 69 | fn detectIntelProcessor(cpu: *Target.Cpu, family: u32, model: u32, brand_id: u32) void { |
| 72 | if (brand_id != 0) { | 70 | if (brand_id != 0) { |
| 73 | return; | 71 | return; |
| 74 | } | 72 | } |
| 75 | switch(family) { | 73 | switch (family) { |
| 76 | 3 => { | 74 | 3 => { |
| 77 | cpu.model = &Target.x86.cpu._i386; | 75 | cpu.model = &Target.x86.cpu._i386; |
| 78 | return; | 76 | return; |
| ... | @@ -82,7 +80,7 @@ fn detectIntelProcessor(cpu: *Target.Cpu, family: u32, model: u32, brand_id: u32 | ... | @@ -82,7 +80,7 @@ fn detectIntelProcessor(cpu: *Target.Cpu, family: u32, model: u32, brand_id: u32 |
| 82 | return; | 80 | return; |
| 83 | }, | 81 | }, |
| 84 | 5 => { | 82 | 5 => { |
| 85 | if(Target.x86.featureSetHas(cpu.features, .mmx)) { | 83 | if (Target.x86.featureSetHas(cpu.features, .mmx)) { |
| 86 | cpu.model = &Target.x86.cpu.pentium_mmx; | 84 | cpu.model = &Target.x86.cpu.pentium_mmx; |
| 87 | return; | 85 | return; |
| 88 | } | 86 | } |
| ... | @@ -90,7 +88,7 @@ fn detectIntelProcessor(cpu: *Target.Cpu, family: u32, model: u32, brand_id: u32 | ... | @@ -90,7 +88,7 @@ fn detectIntelProcessor(cpu: *Target.Cpu, family: u32, model: u32, brand_id: u32 |
| 90 | return; | 88 | return; |
| 91 | }, | 89 | }, |
| 92 | 6 => { | 90 | 6 => { |
| 93 | switch(model) { | 91 | switch (model) { |
| 94 | 0x01 => { | 92 | 0x01 => { |
| 95 | cpu.model = &Target.x86.cpu.pentiumpro; | 93 | cpu.model = &Target.x86.cpu.pentiumpro; |
| 96 | return; | 94 | return; |
| ... | @@ -148,10 +146,10 @@ fn detectIntelProcessor(cpu: *Target.Cpu, family: u32, model: u32, brand_id: u32 | ... | @@ -148,10 +146,10 @@ fn detectIntelProcessor(cpu: *Target.Cpu, family: u32, model: u32, brand_id: u32 |
| 148 | return; | 146 | return; |
| 149 | }, | 147 | }, |
| 150 | 0x55 => { | 148 | 0x55 => { |
| 151 | if(Target.x86.featureSetHas(cpu.features, .avx512bf16)) { | 149 | if (Target.x86.featureSetHas(cpu.features, .avx512bf16)) { |
| 152 | cpu.model = &Target.x86.cpu.cooperlake; | 150 | cpu.model = &Target.x86.cpu.cooperlake; |
| 153 | return; | 151 | return; |
| 154 | } else if(Target.x86.featureSetHas(cpu.features, .avx512vnni)) { | 152 | } else if (Target.x86.featureSetHas(cpu.features, .avx512vnni)) { |
| 155 | cpu.model = &Target.x86.cpu.cascadelake; | 153 | cpu.model = &Target.x86.cpu.cascadelake; |
| 156 | return; | 154 | return; |
| 157 | } else { | 155 | } else { |
| ... | @@ -199,45 +197,36 @@ fn detectIntelProcessor(cpu: *Target.Cpu, family: u32, model: u32, brand_id: u32 | ... | @@ -199,45 +197,36 @@ fn detectIntelProcessor(cpu: *Target.Cpu, family: u32, model: u32, brand_id: u32 |
| 199 | cpu.model = &Target.x86.cpu.knm; | 197 | cpu.model = &Target.x86.cpu.knm; |
| 200 | return; | 198 | return; |
| 201 | }, | 199 | }, |
| 202 | else => { | 200 | else => return, // Unknown CPU Model |
| 203 | // Unknown CPU. | | |
| 204 | // Default to baseline x86_64 or i386 cpu. | | |
| 205 | return; | | |
| 206 | }, | | |
| 207 | } | 201 | } |
| 208 | }, | 202 | }, |
| 209 | 15 => { | 203 | 15 => { |
| 210 | if(Target.x86.featureSetHas(cpu.features, .@"64bit")) { | 204 | if (Target.x86.featureSetHas(cpu.features, .@"64bit")) { |
| 211 | cpu.model = &Target.x86.cpu.nocona; | 205 | cpu.model = &Target.x86.cpu.nocona; |
| 212 | return; | 206 | return; |
| 213 | } | 207 | } |
| 214 | if(Target.x86.featureSetHas(cpu.features, .sse3)) { | 208 | if (Target.x86.featureSetHas(cpu.features, .sse3)) { |
| 215 | cpu.model = &Target.x86.cpu.prescott; | 209 | cpu.model = &Target.x86.cpu.prescott; |
| 216 | return; | 210 | return; |
| 217 | } | 211 | } |
| 218 | cpu.model = &Target.x86.cpu.pentium4; | 212 | cpu.model = &Target.x86.cpu.pentium4; |
| 219 | return; | 213 | return; |
| 220 | }, | 214 | }, |
| 221 | else => { | 215 | else => return, // Unknown CPU Model |
| 222 | // Unknown CPU. | | |
| 223 | // Default to baseline x86_64 or i386 cpu. | | |
| 224 | return; | | |
| 225 | } | | |
| 226 | } | 216 | } |
| 227 | } | 217 | } |
| 228 | | 218 | |
| 229 | fn detectAMDProcessor(cpu: *Target.Cpu, family: u32, model: u32) void { | 219 | fn detectAMDProcessor(cpu: *Target.Cpu, family: u32, model: u32) void { |
| 230 | // AMD's cpuid information is less than optimal for determining a CPU model. | 220 | // AMD's cpuid information is less than optimal for determining a CPU model. |
| 231 | // This is very unscientific, and not necessarily correct. | 221 | // This is very unscientific, and not necessarily correct. |
| 232 | | 222 | switch (family) { |
| 233 | switch(family) { | | |
| 234 | 4 => { | 223 | 4 => { |
| 235 | cpu.model = &Target.x86.cpu._i486; | 224 | cpu.model = &Target.x86.cpu._i486; |
| 236 | return; | 225 | return; |
| 237 | }, | 226 | }, |
| 238 | 5 => { | 227 | 5 => { |
| 239 | cpu.model = &Target.x86.cpu.pentium; | 228 | cpu.model = &Target.x86.cpu.pentium; |
| 240 | switch(model) { | 229 | switch (model) { |
| 241 | 6, 7 => { | 230 | 6, 7 => { |
| 242 | cpu.model = &Target.x86.cpu.k6; | 231 | cpu.model = &Target.x86.cpu.k6; |
| 243 | return; | 232 | return; |
| ... | @@ -259,7 +248,7 @@ fn detectAMDProcessor(cpu: *Target.Cpu, family: u32, model: u32) void { | ... | @@ -259,7 +248,7 @@ fn detectAMDProcessor(cpu: *Target.Cpu, family: u32, model: u32) void { |
| 259 | return; | 248 | return; |
| 260 | }, | 249 | }, |
| 261 | 6 => { | 250 | 6 => { |
| 262 | if(Target.x86.featureSetHas(cpu.features, .sse)) { | 251 | if (Target.x86.featureSetHas(cpu.features, .sse)) { |
| 263 | cpu.model = &Target.x86.cpu.athlon_xp; | 252 | cpu.model = &Target.x86.cpu.athlon_xp; |
| 264 | return; | 253 | return; |
| 265 | } | 254 | } |
| ... | @@ -267,7 +256,7 @@ fn detectAMDProcessor(cpu: *Target.Cpu, family: u32, model: u32) void { | ... | @@ -267,7 +256,7 @@ fn detectAMDProcessor(cpu: *Target.Cpu, family: u32, model: u32) void { |
| 267 | return; | 256 | return; |
| 268 | }, | 257 | }, |
| 269 | 15 => { | 258 | 15 => { |
| 270 | if(Target.x86.featureSetHas(cpu.features, .sse3)) { | 259 | if (Target.x86.featureSetHas(cpu.features, .sse3)) { |
| 271 | cpu.model = &Target.x86.cpu.k8_sse3; | 260 | cpu.model = &Target.x86.cpu.k8_sse3; |
| 272 | return; | 261 | return; |
| 273 | } | 262 | } |
| ... | @@ -284,15 +273,15 @@ fn detectAMDProcessor(cpu: *Target.Cpu, family: u32, model: u32) void { | ... | @@ -284,15 +273,15 @@ fn detectAMDProcessor(cpu: *Target.Cpu, family: u32, model: u32) void { |
| 284 | }, | 273 | }, |
| 285 | 21 => { | 274 | 21 => { |
| 286 | cpu.model = &Target.x86.cpu.bdver1; | 275 | cpu.model = &Target.x86.cpu.bdver1; |
| 287 | if(model >= 0x60 and model <= 0x7f) { | 276 | if (model >= 0x60 and model <= 0x7f) { |
| 288 | cpu.model = &Target.x86.cpu.bdver4; | 277 | cpu.model = &Target.x86.cpu.bdver4; |
| 289 | return; | 278 | return; |
| 290 | } | 279 | } |
| 291 | if(model >= 0x30 and model <= 0x3f) { | 280 | if (model >= 0x30 and model <= 0x3f) { |
| 292 | cpu.model = &Target.x86.cpu.bdver3; | 281 | cpu.model = &Target.x86.cpu.bdver3; |
| 293 | return; | 282 | return; |
| 294 | } | 283 | } |
| 295 | if((model >= 0x10 and model <= 0x1f) or model == 0x02) { | 284 | if ((model >= 0x10 and model <= 0x1f) or model == 0x02) { |
| 296 | cpu.model = &Target.x86.cpu.bdver2; | 285 | cpu.model = &Target.x86.cpu.bdver2; |
| 297 | return; | 286 | return; |
| 298 | } | 287 | } |
| ... | @@ -304,7 +293,7 @@ fn detectAMDProcessor(cpu: *Target.Cpu, family: u32, model: u32) void { | ... | @@ -304,7 +293,7 @@ fn detectAMDProcessor(cpu: *Target.Cpu, family: u32, model: u32) void { |
| 304 | }, | 293 | }, |
| 305 | 23 => { | 294 | 23 => { |
| 306 | cpu.model = &Target.x86.cpu.znver1; | 295 | cpu.model = &Target.x86.cpu.znver1; |
| 307 | if((model >= 0x30 and model <= 0x3f) or model == 0x71) { | 296 | if ((model >= 0x30 and model <= 0x3f) or model == 0x71) { |
| 308 | cpu.model = &Target.x86.cpu.znver2; | 297 | cpu.model = &Target.x86.cpu.znver2; |
| 309 | return; | 298 | return; |
| 310 | } | 299 | } |
| ... | @@ -312,41 +301,40 @@ fn detectAMDProcessor(cpu: *Target.Cpu, family: u32, model: u32) void { | ... | @@ -312,41 +301,40 @@ fn detectAMDProcessor(cpu: *Target.Cpu, family: u32, model: u32) void { |
| 312 | }, | 301 | }, |
| 313 | else => { | 302 | else => { |
| 314 | return; | 303 | return; |
| 315 | } | 304 | }, |
| 316 | } | 305 | } |
| 317 | } | 306 | } |
| 318 | | 307 | |
| 319 | fn detectNativeFeatures(cpu: *Target.Cpu, os_type: Target.Os.Tag) void { | 308 | fn detectNativeFeatures(cpu: *Target.Cpu, os_tag: Target.Os.Tag) void { |
| 320 | | | |
| 321 | var leaf = cpuid(0, 0); | 309 | var leaf = cpuid(0, 0); |
| 322 | | 310 | |
| 323 | const max_level = leaf.eax; | 311 | const max_level = leaf.eax; |
| 324 | | 312 | |
| 325 | leaf = cpuid(1, 0); | 313 | leaf = cpuid(1, 0); |
| 326 | | 314 | |
| 327 | setFeature(cpu, .cx8, bit(leaf.edx, 8)); | 315 | setFeature(cpu, .cx8, bit(leaf.edx, 8)); |
| 328 | setFeature(cpu, .cx8, bit(leaf.edx, 8)); | 316 | setFeature(cpu, .cx8, bit(leaf.edx, 8)); |
| 329 | setFeature(cpu, .cmov, bit(leaf.edx, 15)); | 317 | setFeature(cpu, .cmov, bit(leaf.edx, 15)); |
| 330 | setFeature(cpu, .mmx, bit(leaf.edx, 23)); | 318 | setFeature(cpu, .mmx, bit(leaf.edx, 23)); |
| 331 | setFeature(cpu, .fxsr, bit(leaf.edx, 24)); | 319 | setFeature(cpu, .fxsr, bit(leaf.edx, 24)); |
| 332 | setFeature(cpu, .sse, bit(leaf.edx, 25)); | 320 | setFeature(cpu, .sse, bit(leaf.edx, 25)); |
| 333 | setFeature(cpu, .sse2, bit(leaf.edx, 26)); | 321 | setFeature(cpu, .sse2, bit(leaf.edx, 26)); |
| 334 | setFeature(cpu, .sse3, bit(leaf.ecx, 0)); | 322 | setFeature(cpu, .sse3, bit(leaf.ecx, 0)); |
| 335 | setFeature(cpu, .pclmul, bit(leaf.ecx, 1)); | 323 | setFeature(cpu, .pclmul, bit(leaf.ecx, 1)); |
| 336 | setFeature(cpu, .ssse3, bit(leaf.ecx, 9)); | 324 | setFeature(cpu, .ssse3, bit(leaf.ecx, 9)); |
| 337 | setFeature(cpu, .cx16, bit(leaf.ecx, 13)); | 325 | setFeature(cpu, .cx16, bit(leaf.ecx, 13)); |
| 338 | setFeature(cpu, .sse4_1, bit(leaf.ecx, 19)); | 326 | setFeature(cpu, .sse4_1, bit(leaf.ecx, 19)); |
| 339 | setFeature(cpu, .sse4_2, bit(leaf.ecx, 20)); | 327 | setFeature(cpu, .sse4_2, bit(leaf.ecx, 20)); |
| 340 | setFeature(cpu, .movbe, bit(leaf.ecx, 22)); | 328 | setFeature(cpu, .movbe, bit(leaf.ecx, 22)); |
| 341 | setFeature(cpu, .popcnt, bit(leaf.ecx, 23)); | 329 | setFeature(cpu, .popcnt, bit(leaf.ecx, 23)); |
| 342 | setFeature(cpu, .aes, bit(leaf.ecx, 25)); | 330 | setFeature(cpu, .aes, bit(leaf.ecx, 25)); |
| 343 | setFeature(cpu, .rdrnd, bit(leaf.ecx, 30)); | 331 | setFeature(cpu, .rdrnd, bit(leaf.ecx, 30)); |
| 344 | | 332 | |
| 345 | leaf.eax = getXCR0(); | 333 | leaf.eax = getXCR0(); |
| 346 | | 334 | |
| 347 | const has_avx = bit(leaf.ecx, 27) and | 335 | const has_avx = bit(leaf.ecx, 27) and |
| 348 | bit(leaf.ecx, 28) and | 336 | bit(leaf.ecx, 28) and |
| 349 | ((leaf.eax & 0x6) == 0x6); | 337 | ((leaf.eax & 0x6) == 0x6); |
| 350 | | 338 | |
| 351 | // LLVM approaches avx512_save by hardcoding it to true on Darwin, | 339 | // LLVM approaches avx512_save by hardcoding it to true on Darwin, |
| 352 | // because the kernel saves the context even if the bit is not set. | 340 | // because the kernel saves the context even if the bit is not set. |
| ... | @@ -368,96 +356,96 @@ fn detectNativeFeatures(cpu: *Target.Cpu, os_type: Target.Os.Tag) void { | ... | @@ -368,96 +356,96 @@ fn detectNativeFeatures(cpu: *Target.Cpu, os_type: Target.Os.Tag) void { |
| 368 | // Darwin lazily saves the AVX512 context on first use: trust that the OS will | 356 | // Darwin lazily saves the AVX512 context on first use: trust that the OS will |
| 369 | // save the AVX512 context if we use AVX512 instructions, even if the bit is not | 357 | // save the AVX512 context if we use AVX512 instructions, even if the bit is not |
| 370 | // set right now. | 358 | // set right now. |
| 371 | const has_avx512_save = switch(os_type.isDarwin()) { | 359 | const has_avx512_save = switch (os_tag.isDarwin()) { |
| 372 | true => true, | 360 | true => true, |
| 373 | false => has_avx and ((leaf.eax & 0xE0) == 0xE0), | 361 | false => has_avx and ((leaf.eax & 0xE0) == 0xE0), |
| 374 | }; | 362 | }; |
| 375 | | 363 | |
| 376 | setFeature(cpu, .avx, has_avx); | 364 | setFeature(cpu, .avx, has_avx); |
| 377 | setFeature(cpu, .fma, has_avx and bit(leaf.ecx, 12)); | 365 | setFeature(cpu, .fma, has_avx and bit(leaf.ecx, 12)); |
| 378 | // Only enable XSAVE if OS has enabled support for saving YMM state. | 366 | // Only enable XSAVE if OS has enabled support for saving YMM state. |
| 379 | setFeature(cpu, .xsave, has_avx and bit(leaf.ecx, 26)); | 367 | setFeature(cpu, .xsave, has_avx and bit(leaf.ecx, 26)); |
| 380 | setFeature(cpu, .f16c, has_avx and bit(leaf.ecx, 29)); | 368 | setFeature(cpu, .f16c, has_avx and bit(leaf.ecx, 29)); |
| 381 | | 369 | |
| 382 | leaf = cpuid(0x80000000, 0); | 370 | leaf = cpuid(0x80000000, 0); |
| 383 | const max_ext_level = leaf.eax; | 371 | const max_ext_level = leaf.eax; |
| 384 | | 372 | |
| 385 | if(max_ext_level >= 0x80000001) { | 373 | if (max_ext_level >= 0x80000001) { |
| 386 | leaf = cpuid(0x80000001, 0); | 374 | leaf = cpuid(0x80000001, 0); |
| 387 | setFeature(cpu, .sahf, bit(leaf.ecx, 0)); | 375 | setFeature(cpu, .sahf, bit(leaf.ecx, 0)); |
| 388 | setFeature(cpu, .lzcnt, bit(leaf.ecx, 5)); | 376 | setFeature(cpu, .lzcnt, bit(leaf.ecx, 5)); |
| 389 | setFeature(cpu, .sse4a, bit(leaf.ecx, 6)); | 377 | setFeature(cpu, .sse4a, bit(leaf.ecx, 6)); |
| 390 | setFeature(cpu, .prfchw, bit(leaf.ecx, 8)); | 378 | setFeature(cpu, .prfchw, bit(leaf.ecx, 8)); |
| 391 | setFeature(cpu, .xop, bit(leaf.ecx, 11) and has_avx); | 379 | setFeature(cpu, .xop, bit(leaf.ecx, 11) and has_avx); |
| 392 | setFeature(cpu, .lwp, bit(leaf.ecx, 15)); | 380 | setFeature(cpu, .lwp, bit(leaf.ecx, 15)); |
| 393 | setFeature(cpu, .fma4, bit(leaf.ecx, 16) and has_avx); | 381 | setFeature(cpu, .fma4, bit(leaf.ecx, 16) and has_avx); |
| 394 | setFeature(cpu, .tbm, bit(leaf.ecx, 21)); | 382 | setFeature(cpu, .tbm, bit(leaf.ecx, 21)); |
| 395 | setFeature(cpu, .mwaitx, bit(leaf.ecx, 29)); | 383 | setFeature(cpu, .mwaitx, bit(leaf.ecx, 29)); |
| 396 | setFeature(cpu, .@"64bit", bit(leaf.edx, 29)); | 384 | setFeature(cpu, .@"64bit", bit(leaf.edx, 29)); |
| 397 | } else { | 385 | } else { |
| 398 | for([_]Target.x86.Feature{ | 386 | for ([_]Target.x86.Feature{ |
| 399 | .sahf, .lzcnt, .sse4a, .prfchw, .xop, | 387 | .sahf, .lzcnt, .sse4a, .prfchw, .xop, |
| 400 | .lwp, .fma4, .tbm, .mwaitx, .@"64bit" | 388 | .lwp, .fma4, .tbm, .mwaitx, .@"64bit", |
| 401 | }) |feat| { | 389 | }) |feat| { |
| 402 | setFeature(cpu, feat, false); | 390 | setFeature(cpu, feat, false); |
| 403 | } | 391 | } |
| 404 | } | 392 | } |
| 405 | | 393 | |
| 406 | // Misc. memory-related features. | 394 | // Misc. memory-related features. |
| 407 | if(max_ext_level >= 0x80000008) { | 395 | if (max_ext_level >= 0x80000008) { |
| 408 | leaf = cpuid(0x80000008, 0); | 396 | leaf = cpuid(0x80000008, 0); |
| 409 | setFeature(cpu, .clzero, bit(leaf.ebx, 0)); | 397 | setFeature(cpu, .clzero, bit(leaf.ebx, 0)); |
| 410 | setFeature(cpu, .wbnoinvd, bit(leaf.ebx, 9)); | 398 | setFeature(cpu, .wbnoinvd, bit(leaf.ebx, 9)); |
| 411 | } else { | 399 | } else { |
| 412 | for([_]Target.x86.Feature{ .clzero, .wbnoinvd }) |feat| { | 400 | for ([_]Target.x86.Feature{ .clzero, .wbnoinvd }) |feat| { |
| 413 | setFeature(cpu, feat, false); | 401 | setFeature(cpu, feat, false); |
| 414 | } | 402 | } |
| 415 | } | 403 | } |
| 416 | | 404 | |
| 417 | if(max_level >= 0x7) { | 405 | if (max_level >= 0x7) { |
| 418 | leaf = cpuid(0x7, 0); | 406 | leaf = cpuid(0x7, 0); |
| 419 | | 407 | |
| 420 | setFeature(cpu, .fsgsbase, bit(leaf.ebx, 0)); | 408 | setFeature(cpu, .fsgsbase, bit(leaf.ebx, 0)); |
| 421 | setFeature(cpu, .sgx, bit(leaf.ebx, 2)); | 409 | setFeature(cpu, .sgx, bit(leaf.ebx, 2)); |
| 422 | setFeature(cpu, .bmi, bit(leaf.ebx, 3)); | 410 | setFeature(cpu, .bmi, bit(leaf.ebx, 3)); |
| 423 | // AVX2 is only supported if we have the OS save support from AVX. | 411 | // AVX2 is only supported if we have the OS save support from AVX. |
| 424 | setFeature(cpu, .avx2, bit(leaf.ebx, 5) and has_avx); | 412 | setFeature(cpu, .avx2, bit(leaf.ebx, 5) and has_avx); |
| 425 | setFeature(cpu, .bmi2, bit(leaf.ebx, 8)); | 413 | setFeature(cpu, .bmi2, bit(leaf.ebx, 8)); |
| 426 | setFeature(cpu, .invpcid, bit(leaf.ebx, 10)); | 414 | setFeature(cpu, .invpcid, bit(leaf.ebx, 10)); |
| 427 | setFeature(cpu, .rtm, bit(leaf.ebx, 11)); | 415 | setFeature(cpu, .rtm, bit(leaf.ebx, 11)); |
| 428 | // AVX512 is only supported if the OS supports the context save for it. | 416 | // AVX512 is only supported if the OS supports the context save for it. |
| 429 | setFeature(cpu, .avx512f, bit(leaf.ebx, 16) and has_avx512_save); | 417 | setFeature(cpu, .avx512f, bit(leaf.ebx, 16) and has_avx512_save); |
| 430 | setFeature(cpu, .avx512dq, bit(leaf.ebx, 17) and has_avx512_save); | 418 | setFeature(cpu, .avx512dq, bit(leaf.ebx, 17) and has_avx512_save); |
| 431 | setFeature(cpu, .rdseed, bit(leaf.ebx, 18)); | 419 | setFeature(cpu, .rdseed, bit(leaf.ebx, 18)); |
| 432 | setFeature(cpu, .adx, bit(leaf.ebx, 19)); | 420 | setFeature(cpu, .adx, bit(leaf.ebx, 19)); |
| 433 | setFeature(cpu, .avx512ifma, bit(leaf.ebx, 21) and has_avx512_save); | 421 | setFeature(cpu, .avx512ifma, bit(leaf.ebx, 21) and has_avx512_save); |
| 434 | setFeature(cpu, .clflushopt, bit(leaf.ebx, 23)); | 422 | setFeature(cpu, .clflushopt, bit(leaf.ebx, 23)); |
| 435 | setFeature(cpu, .clwb, bit(leaf.ebx, 24)); | 423 | setFeature(cpu, .clwb, bit(leaf.ebx, 24)); |
| 436 | setFeature(cpu, .avx512pf, bit(leaf.ebx, 26) and has_avx512_save); | 424 | setFeature(cpu, .avx512pf, bit(leaf.ebx, 26) and has_avx512_save); |
| 437 | setFeature(cpu, .avx512er, bit(leaf.ebx, 27) and has_avx512_save); | 425 | setFeature(cpu, .avx512er, bit(leaf.ebx, 27) and has_avx512_save); |
| 438 | setFeature(cpu, .avx512cd, bit(leaf.ebx, 28) and has_avx512_save); | 426 | setFeature(cpu, .avx512cd, bit(leaf.ebx, 28) and has_avx512_save); |
| 439 | setFeature(cpu, .sha, bit(leaf.ebx, 29)); | 427 | setFeature(cpu, .sha, bit(leaf.ebx, 29)); |
| 440 | setFeature(cpu, .avx512bw, bit(leaf.ebx, 30) and has_avx512_save); | 428 | setFeature(cpu, .avx512bw, bit(leaf.ebx, 30) and has_avx512_save); |
| 441 | setFeature(cpu, .avx512vl, bit(leaf.ebx, 31) and has_avx512_save); | 429 | setFeature(cpu, .avx512vl, bit(leaf.ebx, 31) and has_avx512_save); |
| 442 | | 430 | |
| 443 | setFeature(cpu, .prefetchwt1, bit(leaf.ecx, 0)); | 431 | setFeature(cpu, .prefetchwt1, bit(leaf.ecx, 0)); |
| 444 | setFeature(cpu, .avx512vbmi, bit(leaf.ecx, 1) and has_avx512_save); | 432 | setFeature(cpu, .avx512vbmi, bit(leaf.ecx, 1) and has_avx512_save); |
| 445 | setFeature(cpu, .pku, bit(leaf.ecx, 4)); | 433 | setFeature(cpu, .pku, bit(leaf.ecx, 4)); |
| 446 | setFeature(cpu, .waitpkg, bit(leaf.ecx, 5)); | 434 | setFeature(cpu, .waitpkg, bit(leaf.ecx, 5)); |
| 447 | setFeature(cpu, .avx512vbmi2, bit(leaf.ecx, 6) and has_avx512_save); | 435 | setFeature(cpu, .avx512vbmi2, bit(leaf.ecx, 6) and has_avx512_save); |
| 448 | setFeature(cpu, .shstk, bit(leaf.ecx, 7)); | 436 | setFeature(cpu, .shstk, bit(leaf.ecx, 7)); |
| 449 | setFeature(cpu, .gfni, bit(leaf.ecx, 8)); | 437 | setFeature(cpu, .gfni, bit(leaf.ecx, 8)); |
| 450 | setFeature(cpu, .vaes, bit(leaf.ecx, 9) and has_avx); | 438 | setFeature(cpu, .vaes, bit(leaf.ecx, 9) and has_avx); |
| 451 | setFeature(cpu, .vpclmulqdq, bit(leaf.ecx, 10) and has_avx); | 439 | setFeature(cpu, .vpclmulqdq, bit(leaf.ecx, 10) and has_avx); |
| 452 | setFeature(cpu, .avx512vnni, bit(leaf.ecx, 11) and has_avx512_save); | 440 | setFeature(cpu, .avx512vnni, bit(leaf.ecx, 11) and has_avx512_save); |
| 453 | setFeature(cpu, .avx512bitalg, bit(leaf.ecx, 12) and has_avx512_save); | 441 | setFeature(cpu, .avx512bitalg, bit(leaf.ecx, 12) and has_avx512_save); |
| 454 | setFeature(cpu, .avx512vpopcntdq, bit(leaf.ecx, 14) and has_avx512_save); | 442 | setFeature(cpu, .avx512vpopcntdq, bit(leaf.ecx, 14) and has_avx512_save); |
| 455 | setFeature(cpu, .avx512vp2intersect, bit(leaf.edx, 8) and has_avx512_save); | 443 | setFeature(cpu, .avx512vp2intersect, bit(leaf.edx, 8) and has_avx512_save); |
| 456 | setFeature(cpu, .rdpid, bit(leaf.ecx, 22)); | 444 | setFeature(cpu, .rdpid, bit(leaf.ecx, 22)); |
| 457 | setFeature(cpu, .cldemote, bit(leaf.ecx, 25)); | 445 | setFeature(cpu, .cldemote, bit(leaf.ecx, 25)); |
| 458 | setFeature(cpu, .movdiri, bit(leaf.ecx, 27)); | 446 | setFeature(cpu, .movdiri, bit(leaf.ecx, 27)); |
| 459 | setFeature(cpu, .movdir64b, bit(leaf.ecx, 28)); | 447 | setFeature(cpu, .movdir64b, bit(leaf.ecx, 28)); |
| 460 | setFeature(cpu, .enqcmd, bit(leaf.ecx, 29)); | 448 | setFeature(cpu, .enqcmd, bit(leaf.ecx, 29)); |
| 461 | | 449 | |
| 462 | // There are two CPUID leafs which information associated with the pconfig | 450 | // There are two CPUID leafs which information associated with the pconfig |
| 463 | // instruction: | 451 | // instruction: |
| ... | @@ -469,21 +457,21 @@ fn detectNativeFeatures(cpu: *Target.Cpu, os_type: Target.Os.Tag) void { | ... | @@ -469,21 +457,21 @@ fn detectNativeFeatures(cpu: *Target.Cpu, os_type: Target.Os.Tag) void { |
| 469 | // leaves using cpuid, since that information is ignored while | 457 | // leaves using cpuid, since that information is ignored while |
| 470 | // detecting features using the "-march=native" flag. | 458 | // detecting features using the "-march=native" flag. |
| 471 | // For more info, see X86 ISA docs. | 459 | // For more info, see X86 ISA docs. |
| 472 | setFeature(cpu, .pconfig, bit(leaf.edx, 18)); | 460 | setFeature(cpu, .pconfig, bit(leaf.edx, 18)); |
| 473 | | 461 | |
| 474 | // TODO I feel unsure about this check. | 462 | // TODO I feel unsure about this check. |
| 475 | // It doesn't really seem to check for 7.1, just for 7. | 463 | // It doesn't really seem to check for 7.1, just for 7. |
| 476 | // Is this a sound assumption to make? | 464 | // Is this a sound assumption to make? |
| 477 | // Note that this is what other implementations do, so I kind of trust it. | 465 | // Note that this is what other implementations do, so I kind of trust it. |
| 478 | const has_leaf_7_1 = max_level >= 7; | 466 | const has_leaf_7_1 = max_level >= 7; |
| 479 | if(has_leaf_7_1) { | 467 | if (has_leaf_7_1) { |
| 480 | leaf = cpuid(0x7, 0x1); | 468 | leaf = cpuid(0x7, 0x1); |
| 481 | setFeature(cpu, .avx512bf16, bit(leaf.eax, 5) and has_avx512_save); | 469 | setFeature(cpu, .avx512bf16, bit(leaf.eax, 5) and has_avx512_save); |
| 482 | } else { | 470 | } else { |
| 483 | setFeature(cpu, .avx512bf16, false); | 471 | setFeature(cpu, .avx512bf16, false); |
| 484 | } | 472 | } |
| 485 | } else { | 473 | } else { |
| 486 | for([_]Target.x86.Feature{ | 474 | for ([_]Target.x86.Feature{ |
| 487 | .fsgsbase, .sgx, .bmi, .avx2, | 475 | .fsgsbase, .sgx, .bmi, .avx2, |
| 488 | .bmi2, .invpcid, .rtm, .avx512f, | 476 | .bmi2, .invpcid, .rtm, .avx512f, |
| 489 | .avx512dq, .rdseed, .adx, .avx512ifma, | 477 | .avx512dq, .rdseed, .adx, .avx512ifma, |
| ... | @@ -499,26 +487,24 @@ fn detectNativeFeatures(cpu: *Target.Cpu, os_type: Target.Os.Tag) void { | ... | @@ -499,26 +487,24 @@ fn detectNativeFeatures(cpu: *Target.Cpu, os_type: Target.Os.Tag) void { |
| 499 | } | 487 | } |
| 500 | } | 488 | } |
| 501 | | 489 | |
| 502 | if(max_level >= 0xD and has_avx) { | 490 | if (max_level >= 0xD and has_avx) { |
| 503 | leaf = cpuid(0xD, 0x1); | 491 | leaf = cpuid(0xD, 0x1); |
| 504 | // Only enable XSAVE if OS has enabled support for saving YMM state. | 492 | // Only enable XSAVE if OS has enabled support for saving YMM state. |
| 505 | setFeature(cpu, .xsaveopt, bit(leaf.eax, 0)); | 493 | setFeature(cpu, .xsaveopt, bit(leaf.eax, 0)); |
| 506 | setFeature(cpu, .xsavec, bit(leaf.eax, 1)); | 494 | setFeature(cpu, .xsavec, bit(leaf.eax, 1)); |
| 507 | setFeature(cpu, .xsaves, bit(leaf.eax, 3)); | 495 | setFeature(cpu, .xsaves, bit(leaf.eax, 3)); |
| 508 | | | |
| 509 | } else { | 496 | } else { |
| 510 | for([_]Target.x86.Feature{ .xsaveopt, .xsavec, .xsaves }) |feat| { | 497 | for ([_]Target.x86.Feature{ .xsaveopt, .xsavec, .xsaves }) |feat| { |
| 511 | setFeature(cpu, feat, false); | 498 | setFeature(cpu, feat, false); |
| 512 | } | 499 | } |
| 513 | } | 500 | } |
| 514 | | 501 | |
| 515 | if(max_level >= 0x14) { | 502 | if (max_level >= 0x14) { |
| 516 | leaf = cpuid(0x14, 0); | 503 | leaf = cpuid(0x14, 0); |
| 517 | setFeature(cpu, .ptwrite, bit(leaf.ebx, 4)); | 504 | setFeature(cpu, .ptwrite, bit(leaf.ebx, 4)); |
| 518 | } else { | 505 | } else { |
| 519 | setFeature(cpu, .ptwrite, false); | 506 | setFeature(cpu, .ptwrite, false); |
| 520 | } | 507 | } |
| 521 | | | |
| 522 | } | 508 | } |
| 523 | | 509 | |
| 524 | const CpuidLeaf = packed struct { | 510 | const CpuidLeaf = packed struct { |
| ... | @@ -532,9 +518,9 @@ fn cpuid(leaf_id: u32, subid: u32) CpuidLeaf { | ... | @@ -532,9 +518,9 @@ fn cpuid(leaf_id: u32, subid: u32) CpuidLeaf { |
| 532 | // Workaround for https://github.com/ziglang/zig/issues/215 | 518 | // Workaround for https://github.com/ziglang/zig/issues/215 |
| 533 | // Inline assembly in zig only supports one output, | 519 | // Inline assembly in zig only supports one output, |
| 534 | // so we pass a pointer to the struct. | 520 | // so we pass a pointer to the struct. |
| 535 | var cpuid_leaf = CpuidLeaf {.eax = 0, .ebx = 0, .ecx = 0, .edx = 0}; | 521 | var cpuid_leaf = CpuidLeaf{ .eax = 0, .ebx = 0, .ecx = 0, .edx = 0 }; |
| 536 | var leaf_ptr = &cpuid_leaf; | 522 | var leaf_ptr = &cpuid_leaf; |
| 537 | switch(Target.current.cpu.arch) { | 523 | switch (Target.current.cpu.arch) { |
| 538 | .i386 => { | 524 | .i386 => { |
| 539 | _ = asm volatile ( | 525 | _ = asm volatile ( |
| 540 | \\ cpuid | 526 | \\ cpuid |
| ... | @@ -542,10 +528,10 @@ fn cpuid(leaf_id: u32, subid: u32) CpuidLeaf { | ... | @@ -542,10 +528,10 @@ fn cpuid(leaf_id: u32, subid: u32) CpuidLeaf { |
| 542 | \\ movl %%ebx, 4(%%edi) | 528 | \\ movl %%ebx, 4(%%edi) |
| 543 | \\ movl %%ecx, 8(%%edi) | 529 | \\ movl %%ecx, 8(%%edi) |
| 544 | \\ movl %%edx, 12(%%edi) | 530 | \\ movl %%edx, 12(%%edi) |
| 545 | : : | 531 | : |
| 546 | [leaf_id] "{eax}" (leaf_id), | 532 | : [leaf_id] "{eax}" (leaf_id), |
| 547 | [subid] "{ecx}" (subid), | 533 | [subid] "{ecx}" (subid), |
| 548 | [leaf_ptr] "{edi}" (leaf_ptr), | 534 | [leaf_ptr] "{edi}" (leaf_ptr) |
| 549 | : "eax", "ebx", "ecx", "edx" | 535 | : "eax", "ebx", "ecx", "edx" |
| 550 | ); | 536 | ); |
| 551 | }, | 537 | }, |
| ... | @@ -556,10 +542,10 @@ fn cpuid(leaf_id: u32, subid: u32) CpuidLeaf { | ... | @@ -556,10 +542,10 @@ fn cpuid(leaf_id: u32, subid: u32) CpuidLeaf { |
| 556 | \\ movl %%ebx, 4(%%rdi) | 542 | \\ movl %%ebx, 4(%%rdi) |
| 557 | \\ movl %%ecx, 8(%%rdi) | 543 | \\ movl %%ecx, 8(%%rdi) |
| 558 | \\ movl %%edx, 12(%%rdi) | 544 | \\ movl %%edx, 12(%%rdi) |
| 559 | : : | 545 | : |
| 560 | [leaf_id] "{eax}" (leaf_id), | 546 | : [leaf_id] "{eax}" (leaf_id), |
| 561 | [subid] "{ecx}" (subid), | 547 | [subid] "{ecx}" (subid), |
| 562 | [leaf_ptr] "{rdi}" (leaf_ptr), | 548 | [leaf_ptr] "{rdi}" (leaf_ptr) |
| 563 | : "eax", "ebx", "ecx", "edx" | 549 | : "eax", "ebx", "ecx", "edx" |
| 564 | ); | 550 | ); |
| 565 | }, | 551 | }, |
| ... | @@ -570,14 +556,11 @@ fn cpuid(leaf_id: u32, subid: u32) CpuidLeaf { | ... | @@ -570,14 +556,11 @@ fn cpuid(leaf_id: u32, subid: u32) CpuidLeaf { |
| 570 | | 556 | |
| 571 | // Read control register 0 (XCR0). Used to detect features such as AVX. | 557 | // Read control register 0 (XCR0). Used to detect features such as AVX. |
| 572 | fn getXCR0() u32 { | 558 | fn getXCR0() u32 { |
| 573 | | | |
| 574 | return asm ( | 559 | return asm ( |
| 575 | \\ .byte 0x0F, 0x01, 0xD0 | 560 | \\ .byte 0x0F, 0x01, 0xD0 |
| 576 | : [ret] "={eax}" (-> u32) | 561 | : [ret] "={eax}" (-> u32) |
| 577 | : [number] "{eax}" (@as(u32, 0)), | 562 | : [number] "{eax}" (@as(u32, 0)), |
| 578 | [number] "{edx}" (@as(u32, 0)), | 563 | [number] "{edx}" (@as(u32, 0)), |
| 579 | [number] "{ecx}" (@as(u32, 0)), | 564 | [number] "{ecx}" (@as(u32, 0)) |
| 580 | : | | |
| 581 | ); | 565 | ); |
| 582 | } | 566 | } |
| 583 | | | |