| ... | ... | @@ -10,70 +10,66 @@ fn setFeature(cpu: *Target.Cpu, feature: Target.x86.Feature, enabled: bool) void |
| 10 | 10 | else cpu.features.removeFeature(idx); |
| 11 | 11 | } |
| 12 | 12 | |
| 13 | | fn hasFeature(cpu: *Target.Cpu, feature: Target.x86.Feature) bool { |
| 14 | | const idx = @as(Target.Cpu.Feature.Set.Index, @enumToInt(feature)); |
| 15 | | return cpu.features.isEnabled(idx); |
| 16 | | } |
| 17 | | |
| 18 | 13 | inline fn bit(input: u32, offset: u5) bool { |
| 19 | 14 | return (input >> offset) & 1 != 0; |
| 20 | 15 | } |
| 21 | 16 | |
| 22 | | pub fn detectNativeCpuAndFeatures(cpu: *Target.Cpu) void { |
| 17 | pub fn detectNativeCpuAndFeatures(cross_target: CrossTarget) Target.Cpu { |
| 23 | 18 | |
| 24 | | defer { |
| 25 | | // Whenever we find a model, add that model's featureset. |
| 26 | | cpu.features.addFeatureSet(cpu.model.features); |
| 27 | | } |
| 19 | var arch = cross_target.getCpuArch(); |
| 28 | 20 | |
| 29 | | // When we can't identify a specific model, |
| 30 | | // we guess based on processor features. |
| 31 | | // This seems to be the accepted standard. |
| 21 | var cpu = Target.Cpu { |
| 22 | .arch = arch, |
| 23 | .model = Target.Cpu.Model.baseline(arch), |
| 24 | .features = Target.Cpu.Feature.Set.empty, |
| 25 | }; |
| 32 | 26 | |
| 33 | | detectNativeFeatures(cpu); |
| 27 | detectNativeFeatures(&cpu, cross_target.getOsTag()); |
| 34 | 28 | |
| 35 | 29 | var leaf = cpuid(0, 0); |
| 36 | 30 | const max_leaf = leaf.eax; |
| 37 | 31 | const vendor = leaf.ebx; |
| 38 | | if(max_leaf < 1) { |
| 39 | | cpu.model = &Target.x86.cpu.generic; |
| 40 | | return; |
| 41 | | } |
| 42 | 32 | |
| 43 | | leaf = cpuid(0x1, 0); |
| 33 | if(max_leaf > 0) { |
| 34 | |
| 35 | leaf = cpuid(0x1, 0); |
| 44 | 36 | |
| 45 | | const brand_id = leaf.ebx & 0xff; |
| 46 | | var family: u32 = 0; |
| 47 | | var model: u32 = 0; |
| 37 | const brand_id = leaf.ebx & 0xff; |
| 38 | var family: u32 = 0; |
| 39 | var model: u32 = 0; |
| 48 | 40 | |
| 49 | | { // Detect model and family |
| 50 | | family = (leaf.eax >> 8) & 0xf; |
| 51 | | model = (leaf.eax >> 4) & 0xf; |
| 52 | | if (family == 6 or family == 0xf) { |
| 53 | | if (family == 0xf) { |
| 54 | | family += (leaf.eax >> 20) & 0xff; |
| 41 | { // Detect model and family |
| 42 | family = (leaf.eax >> 8) & 0xf; |
| 43 | model = (leaf.eax >> 4) & 0xf; |
| 44 | if (family == 6 or family == 0xf) { |
| 45 | if (family == 0xf) { |
| 46 | family += (leaf.eax >> 20) & 0xff; |
| 47 | } |
| 48 | model += ((leaf.eax >> 16) & 0xf) << 4; |
| 55 | 49 | } |
| 56 | | model += ((leaf.eax >> 16) & 0xf) << 4; |
| 57 | 50 | } |
| 58 | | } |
| 59 | 51 | |
| 60 | | switch(vendor) { |
| 61 | | 0x756e6547 => { |
| 62 | | detectIntelProcessor(cpu, family, model, brand_id); |
| 63 | | }, |
| 64 | | 0x68747541 => { |
| 65 | | detectAMDProcessor(cpu, family, model); |
| 66 | | }, |
| 67 | | else => { |
| 68 | | cpu.model = &Target.x86.cpu.generic; |
| 69 | | }, |
| 52 | switch(vendor) { |
| 53 | 0x756e6547 => { |
| 54 | detectIntelProcessor(&cpu, family, model, brand_id); |
| 55 | }, |
| 56 | 0x68747541 => { |
| 57 | detectAMDProcessor(&cpu, family, model); |
| 58 | }, |
| 59 | else => {}, |
| 60 | } |
| 70 | 61 | } |
| 71 | 62 | |
| 63 | var model_features = cpu.model.features; |
| 64 | model_features.populateDependencies(cpu.arch.allFeaturesList()); |
| 65 | cpu.features.addFeatureSet(model_features); |
| 66 | |
| 67 | return cpu; |
| 68 | |
| 72 | 69 | } |
| 73 | 70 | |
| 74 | 71 | fn detectIntelProcessor(cpu: *Target.Cpu, family: u32, model: u32, brand_id: u32) void { |
| 75 | 72 | if (brand_id != 0) { |
| 76 | | cpu.model = &Target.x86.cpu.generic; |
| 77 | 73 | return; |
| 78 | 74 | } |
| 79 | 75 | switch(family) { |
| ... | ... | @@ -86,7 +82,7 @@ fn detectIntelProcessor(cpu: *Target.Cpu, family: u32, model: u32, brand_id: u32 |
| 86 | 82 | return; |
| 87 | 83 | }, |
| 88 | 84 | 5 => { |
| 89 | | if(hasFeature(cpu, .mmx)) { |
| 85 | if(Target.x86.featureSetHas(cpu.features, .mmx)) { |
| 90 | 86 | cpu.model = &Target.x86.cpu.pentium_mmx; |
| 91 | 87 | return; |
| 92 | 88 | } |
| ... | ... | @@ -152,10 +148,10 @@ fn detectIntelProcessor(cpu: *Target.Cpu, family: u32, model: u32, brand_id: u32 |
| 152 | 148 | return; |
| 153 | 149 | }, |
| 154 | 150 | 0x55 => { |
| 155 | | if(hasFeature(cpu, .avx512bf16)) { |
| 151 | if(Target.x86.featureSetHas(cpu.features, .avx512bf16)) { |
| 156 | 152 | cpu.model = &Target.x86.cpu.cooperlake; |
| 157 | 153 | return; |
| 158 | | } else if(hasFeature(cpu, .avx512vnni)) { |
| 154 | } else if(Target.x86.featureSetHas(cpu.features, .avx512vnni)) { |
| 159 | 155 | cpu.model = &Target.x86.cpu.cascadelake; |
| 160 | 156 | return; |
| 161 | 157 | } else { |
| ... | ... | @@ -204,114 +200,18 @@ fn detectIntelProcessor(cpu: *Target.Cpu, family: u32, model: u32, brand_id: u32 |
| 204 | 200 | return; |
| 205 | 201 | }, |
| 206 | 202 | else => { |
| 207 | | // Unknown, try to guess. |
| 208 | | // TODO detect tigerlake host |
| 209 | | if(hasFeature(cpu, .avx512vp2intersect)) { |
| 210 | | // TODO no tigerlake entry in Target.x86.cpu |
| 211 | | //cpu.model = &Target.x86.cpu.tigerlake; |
| 212 | | cpu.model = &Target.x86.cpu.nehalem; |
| 213 | | return; |
| 214 | | } |
| 215 | | if(hasFeature(cpu, .avx512vbmi2)) { |
| 216 | | cpu.model = &Target.x86.cpu.icelake_client; |
| 217 | | return; |
| 218 | | } |
| 219 | | if(hasFeature(cpu, .avx512vbmi)) { |
| 220 | | cpu.model = &Target.x86.cpu.cannonlake; |
| 221 | | return; |
| 222 | | } |
| 223 | | if(hasFeature(cpu, .avx512bf16)) { |
| 224 | | cpu.model = &Target.x86.cpu.cooperlake; |
| 225 | | return; |
| 226 | | } |
| 227 | | if(hasFeature(cpu, .avx512vnni)) { |
| 228 | | cpu.model = &Target.x86.cpu.cascadelake; |
| 229 | | return; |
| 230 | | } |
| 231 | | if(hasFeature(cpu, .avx512vl)) { |
| 232 | | cpu.model = &Target.x86.cpu.skylake_avx512; |
| 233 | | return; |
| 234 | | } |
| 235 | | if(hasFeature(cpu, .avx512er)) { |
| 236 | | cpu.model = &Target.x86.cpu.knl; |
| 237 | | return; |
| 238 | | } |
| 239 | | if(hasFeature(cpu, .clflushopt)) { |
| 240 | | if(hasFeature(cpu, .sha)) { |
| 241 | | cpu.model = &Target.x86.cpu.goldmont; |
| 242 | | return; |
| 243 | | } else { |
| 244 | | cpu.model = &Target.x86.cpu.skylake; |
| 245 | | return; |
| 246 | | } |
| 247 | | } |
| 248 | | if(hasFeature(cpu, .adx)) { |
| 249 | | cpu.model = &Target.x86.cpu.broadwell; |
| 250 | | return; |
| 251 | | } |
| 252 | | if(hasFeature(cpu, .avx2)) { |
| 253 | | cpu.model = &Target.x86.cpu.haswell; |
| 254 | | return; |
| 255 | | } |
| 256 | | if(hasFeature(cpu, .avx)) { |
| 257 | | cpu.model = &Target.x86.cpu.sandybridge; |
| 258 | | return; |
| 259 | | } |
| 260 | | if(hasFeature(cpu, .sse4_2)) { |
| 261 | | if(hasFeature(cpu, .movbe)) { |
| 262 | | cpu.model = &Target.x86.cpu.silvermont; |
| 263 | | return; |
| 264 | | } else { |
| 265 | | cpu.model = &Target.x86.cpu.nehalem; |
| 266 | | return; |
| 267 | | } |
| 268 | | } |
| 269 | | if(hasFeature(cpu, .sse4_1)) { |
| 270 | | cpu.model = &Target.x86.cpu.penryn; |
| 271 | | return; |
| 272 | | } |
| 273 | | if(hasFeature(cpu, .sse3)) { |
| 274 | | if(hasFeature(cpu, .movbe)) { |
| 275 | | cpu.model = &Target.x86.cpu.bonnell; |
| 276 | | return; |
| 277 | | } else { |
| 278 | | cpu.model = &Target.x86.cpu.core2; |
| 279 | | return; |
| 280 | | } |
| 281 | | } |
| 282 | | |
| 283 | | if(hasFeature(cpu, .@"64bit")) { |
| 284 | | cpu.model = &Target.x86.cpu.core2; |
| 285 | | return; |
| 286 | | } |
| 287 | | |
| 288 | | if(hasFeature(cpu, .sse3)) { |
| 289 | | cpu.model = &Target.x86.cpu.yonah; |
| 290 | | return; |
| 291 | | } |
| 292 | | if(hasFeature(cpu, .sse2)) { |
| 293 | | cpu.model = &Target.x86.cpu.pentium_m; |
| 294 | | return; |
| 295 | | } |
| 296 | | if(hasFeature(cpu, .sse)) { |
| 297 | | cpu.model = &Target.x86.cpu.pentium3; |
| 298 | | return; |
| 299 | | } |
| 300 | | if(hasFeature(cpu, .mmx)) { |
| 301 | | cpu.model = &Target.x86.cpu.pentium2; |
| 302 | | return; |
| 303 | | } |
| 304 | | cpu.model = &Target.x86.cpu.pentiumpro; |
| 203 | // Unknown CPU. |
| 204 | // Default to baseline x86_64 or i386 cpu. |
| 305 | 205 | return; |
| 306 | 206 | }, |
| 307 | 207 | } |
| 308 | 208 | }, |
| 309 | 209 | 15 => { |
| 310 | | if(hasFeature(cpu, .@"64bit")) { |
| 210 | if(Target.x86.featureSetHas(cpu.features, .@"64bit")) { |
| 311 | 211 | cpu.model = &Target.x86.cpu.nocona; |
| 312 | 212 | return; |
| 313 | 213 | } |
| 314 | | if(hasFeature(cpu, .sse3)) { |
| 214 | if(Target.x86.featureSetHas(cpu.features, .sse3)) { |
| 315 | 215 | cpu.model = &Target.x86.cpu.prescott; |
| 316 | 216 | return; |
| 317 | 217 | } |
| ... | ... | @@ -319,7 +219,8 @@ fn detectIntelProcessor(cpu: *Target.Cpu, family: u32, model: u32, brand_id: u32 |
| 319 | 219 | return; |
| 320 | 220 | }, |
| 321 | 221 | else => { |
| 322 | | cpu.model = &Target.x86.cpu.generic; |
| 222 | // Unknown CPU. |
| 223 | // Default to baseline x86_64 or i386 cpu. |
| 323 | 224 | return; |
| 324 | 225 | } |
| 325 | 226 | } |
| ... | ... | @@ -358,7 +259,7 @@ fn detectAMDProcessor(cpu: *Target.Cpu, family: u32, model: u32) void { |
| 358 | 259 | return; |
| 359 | 260 | }, |
| 360 | 261 | 6 => { |
| 361 | | if(hasFeature(cpu, .sse)) { |
| 262 | if(Target.x86.featureSetHas(cpu.features, .sse)) { |
| 362 | 263 | cpu.model = &Target.x86.cpu.athlon_xp; |
| 363 | 264 | return; |
| 364 | 265 | } |
| ... | ... | @@ -366,7 +267,7 @@ fn detectAMDProcessor(cpu: *Target.Cpu, family: u32, model: u32) void { |
| 366 | 267 | return; |
| 367 | 268 | }, |
| 368 | 269 | 15 => { |
| 369 | | if(hasFeature(cpu, .sse3)) { |
| 270 | if(Target.x86.featureSetHas(cpu.features, .sse3)) { |
| 370 | 271 | cpu.model = &Target.x86.cpu.k8_sse3; |
| 371 | 272 | return; |
| 372 | 273 | } |
| ... | ... | @@ -410,13 +311,12 @@ fn detectAMDProcessor(cpu: *Target.Cpu, family: u32, model: u32) void { |
| 410 | 311 | return; |
| 411 | 312 | }, |
| 412 | 313 | else => { |
| 413 | | cpu.model = &Target.x86.cpu.generic; |
| 414 | 314 | return; |
| 415 | 315 | } |
| 416 | 316 | } |
| 417 | 317 | } |
| 418 | 318 | |
| 419 | | fn detectNativeFeatures(cpu: *Target.Cpu) void { |
| 319 | fn detectNativeFeatures(cpu: *Target.Cpu, os_type: Target.Os.Tag) void { |
| 420 | 320 | |
| 421 | 321 | var leaf = cpuid(0, 0); |
| 422 | 322 | |
| ... | ... | @@ -442,23 +342,42 @@ fn detectNativeFeatures(cpu: *Target.Cpu) void { |
| 442 | 342 | setFeature(cpu, .aes, bit(leaf.ecx, 25)); |
| 443 | 343 | setFeature(cpu, .rdrnd, bit(leaf.ecx, 30)); |
| 444 | 344 | |
| 445 | | const has_avx_save = bit(leaf.ecx, 27) and |
| 446 | | bit(leaf.ecx, 28) and |
| 447 | | ((leaf.eax & 0x6) == 0x6); |
| 345 | leaf.eax = getXCR0(); |
| 346 | |
| 347 | const has_avx = bit(leaf.ecx, 27) and |
| 348 | bit(leaf.ecx, 28) and |
| 349 | ((leaf.eax & 0x6) == 0x6); |
| 350 | |
| 351 | // 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. |
| 353 | // https://github.com/llvm/llvm-project/blob/bca373f73fc82728a8335e7d6cd164e8747139ec/llvm/lib/Support/Host.cpp#L1378 |
| 354 | // |
| 355 | // Google approaches this by using a different series of checks and flags, |
| 356 | // and this may report the feature more accurately on a technically correct |
| 357 | // but ultimately less useful level. |
| 358 | // https://github.com/google/cpu_features/blob/b5c271c53759b2b15ff91df19bd0b32f2966e275/src/cpuinfo_x86.c#L113 |
| 359 | // (called from https://github.com/google/cpu_features/blob/b5c271c53759b2b15ff91df19bd0b32f2966e275/src/cpuinfo_x86.c#L1052) |
| 360 | // |
| 361 | // Right now, we use LLVM's approach, because even if the target doesn't support |
| 362 | // the feature, the kernel should provide the same functionality transparently, |
| 363 | // so the implementation details don't make a difference. |
| 364 | // That said, this flag impacts other CPU features' availability, |
| 365 | // so until we can verify that this doesn't come with side affects, |
| 366 | // we'll say TODO verify this. |
| 448 | 367 | |
| 449 | 368 | // Darwin lazily saves the AVX512 context on first use: trust that the OS will |
| 450 | | // save the AVX512 context if we use AVX512 instructions, even the bit is not |
| 369 | // save the AVX512 context if we use AVX512 instructions, even if the bit is not |
| 451 | 370 | // set right now. |
| 452 | | const has_avx512_save = switch(Target.current.isDarwin()) { |
| 371 | const has_avx512_save = switch(os_type.isDarwin()) { |
| 453 | 372 | true => true, |
| 454 | | false => has_avx_save and ((leaf.eax & 0xE0) == 0xE0), |
| 373 | false => has_avx and ((leaf.eax & 0xE0) == 0xE0), |
| 455 | 374 | }; |
| 456 | 375 | |
| 457 | | setFeature(cpu, .avx, has_avx_save); |
| 458 | | setFeature(cpu, .fma, has_avx_save and bit(leaf.ecx, 12)); |
| 376 | setFeature(cpu, .avx, has_avx); |
| 377 | setFeature(cpu, .fma, has_avx and bit(leaf.ecx, 12)); |
| 459 | 378 | // Only enable XSAVE if OS has enabled support for saving YMM state. |
| 460 | | setFeature(cpu, .xsave, has_avx_save and bit(leaf.ecx, 26)); |
| 461 | | setFeature(cpu, .f16c, has_avx_save and bit(leaf.ecx, 29)); |
| 379 | setFeature(cpu, .xsave, has_avx and bit(leaf.ecx, 26)); |
| 380 | setFeature(cpu, .f16c, has_avx and bit(leaf.ecx, 29)); |
| 462 | 381 | |
| 463 | 382 | leaf = cpuid(0x80000000, 0); |
| 464 | 383 | const max_ext_level = leaf.eax; |
| ... | ... | @@ -469,9 +388,9 @@ fn detectNativeFeatures(cpu: *Target.Cpu) void { |
| 469 | 388 | setFeature(cpu, .lzcnt, bit(leaf.ecx, 5)); |
| 470 | 389 | setFeature(cpu, .sse4a, bit(leaf.ecx, 6)); |
| 471 | 390 | setFeature(cpu, .prfchw, bit(leaf.ecx, 8)); |
| 472 | | setFeature(cpu, .xop, bit(leaf.ecx, 11) and has_avx_save); |
| 391 | setFeature(cpu, .xop, bit(leaf.ecx, 11) and has_avx); |
| 473 | 392 | setFeature(cpu, .lwp, bit(leaf.ecx, 15)); |
| 474 | | setFeature(cpu, .fma4, bit(leaf.ecx, 16) and has_avx_save); |
| 393 | setFeature(cpu, .fma4, bit(leaf.ecx, 16) and has_avx); |
| 475 | 394 | setFeature(cpu, .tbm, bit(leaf.ecx, 21)); |
| 476 | 395 | setFeature(cpu, .mwaitx, bit(leaf.ecx, 29)); |
| 477 | 396 | setFeature(cpu, .@"64bit", bit(leaf.edx, 29)); |
| ... | ... | @@ -486,7 +405,7 @@ fn detectNativeFeatures(cpu: *Target.Cpu) void { |
| 486 | 405 | |
| 487 | 406 | // Misc. memory-related features. |
| 488 | 407 | if(max_ext_level >= 0x80000008) { |
| 489 | | leaf = cpuid(80000008, 0); |
| 408 | leaf = cpuid(0x80000008, 0); |
| 490 | 409 | setFeature(cpu, .clzero, bit(leaf.ebx, 0)); |
| 491 | 410 | setFeature(cpu, .wbnoinvd, bit(leaf.ebx, 9)); |
| 492 | 411 | } else { |
| ... | ... | @@ -495,14 +414,14 @@ fn detectNativeFeatures(cpu: *Target.Cpu) void { |
| 495 | 414 | } |
| 496 | 415 | } |
| 497 | 416 | |
| 498 | | if(max_level >= 7) { |
| 417 | if(max_level >= 0x7) { |
| 499 | 418 | leaf = cpuid(0x7, 0); |
| 500 | 419 | |
| 501 | 420 | setFeature(cpu, .fsgsbase, bit(leaf.ebx, 0)); |
| 502 | 421 | setFeature(cpu, .sgx, bit(leaf.ebx, 2)); |
| 503 | 422 | setFeature(cpu, .bmi, bit(leaf.ebx, 3)); |
| 504 | 423 | // AVX2 is only supported if we have the OS save support from AVX. |
| 505 | | setFeature(cpu, .avx2, bit(leaf.ebx, 5) and has_avx_save); |
| 424 | setFeature(cpu, .avx2, bit(leaf.ebx, 5) and has_avx); |
| 506 | 425 | setFeature(cpu, .bmi2, bit(leaf.ebx, 8)); |
| 507 | 426 | setFeature(cpu, .invpcid, bit(leaf.ebx, 10)); |
| 508 | 427 | setFeature(cpu, .rtm, bit(leaf.ebx, 11)); |
| ... | ... | @@ -528,8 +447,8 @@ fn detectNativeFeatures(cpu: *Target.Cpu) void { |
| 528 | 447 | setFeature(cpu, .avx512vbmi2, bit(leaf.ecx, 6) and has_avx512_save); |
| 529 | 448 | setFeature(cpu, .shstk, bit(leaf.ecx, 7)); |
| 530 | 449 | setFeature(cpu, .gfni, bit(leaf.ecx, 8)); |
| 531 | | setFeature(cpu, .vaes, bit(leaf.ecx, 9) and has_avx_save); |
| 532 | | setFeature(cpu, .vpclmulqdq, bit(leaf.ecx, 10) and has_avx_save); |
| 450 | setFeature(cpu, .vaes, bit(leaf.ecx, 9) and has_avx); |
| 451 | setFeature(cpu, .vpclmulqdq, bit(leaf.ecx, 10) and has_avx); |
| 533 | 452 | setFeature(cpu, .avx512vnni, bit(leaf.ecx, 11) and has_avx512_save); |
| 534 | 453 | setFeature(cpu, .avx512bitalg, bit(leaf.ecx, 12) and has_avx512_save); |
| 535 | 454 | setFeature(cpu, .avx512vpopcntdq, bit(leaf.ecx, 14) and has_avx512_save); |
| ... | ... | @@ -580,7 +499,7 @@ fn detectNativeFeatures(cpu: *Target.Cpu) void { |
| 580 | 499 | } |
| 581 | 500 | } |
| 582 | 501 | |
| 583 | | if(max_level >= 0xD and has_avx_save) { |
| 502 | if(max_level >= 0xD and has_avx) { |
| 584 | 503 | leaf = cpuid(0xD, 0x1); |
| 585 | 504 | // Only enable XSAVE if OS has enabled support for saving YMM state. |
| 586 | 505 | setFeature(cpu, .xsaveopt, bit(leaf.eax, 0)); |