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