-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
Copy pathTarget.cpp
1796 lines (1628 loc) · 58.2 KB
/
Target.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include <array>
#include <iostream>
#include <string>
#include "Target.h"
#include "Debug.h"
#include "DeviceInterface.h"
#include "Error.h"
#include "Util.h"
#include "WasmExecutor.h"
#if defined(__powerpc__) && (defined(__FreeBSD__) || defined(__linux__))
#if defined(__FreeBSD__)
#include <machine/cpu.h>
#include <sys/elf_common.h>
#endif
// This uses elf.h and must be included after "LLVM_Headers.h", which
// uses llvm/support/Elf.h.
#include <sys/auxv.h>
#endif
#ifdef _MSC_VER
#define NOMINMAX
#define WIN32_LEAN_AND_MEAN
#include <intrin.h>
#include <windows.h>
#endif // _MSC_VER
#ifdef __APPLE__
#include <mach/machine.h>
#include <sys/sysctl.h>
#include <sys/types.h>
#endif
#if defined(__linux__) && (defined(__arm__) || defined(__aarch64__))
#include <asm/hwcap.h>
#include <sys/auxv.h>
#ifndef HWCAP_ASIMDHP
#define HWCAP_ASIMDHP 0
#endif
#ifndef HWCAP_ASIMDDP
#define HWCAP_ASIMDDP 0
#endif
#ifndef HWCAP_SVE
#define HWCAP_SVE 0
#endif
#ifndef HWCAP2_SVE2
#define HWCAP2_SVE2 0
#endif
#endif
namespace Halide {
using std::string;
using std::vector;
namespace {
#if defined(_M_IX86) || defined(_M_AMD64)
void cpuid(int info[4], int infoType, int extra) {
__cpuidex(info, infoType, extra);
}
#elif defined(__x86_64__) || defined(__i386__)
// CPU feature detection code taken from ispc
// (https://github.com/ispc/ispc/blob/master/builtins/dispatch.ll)
void cpuid(int info[4], int infoType, int extra) {
__asm__ __volatile__(
"cpuid \n\t"
: "=a"(info[0]), "=b"(info[1]), "=c"(info[2]), "=d"(info[3])
: "0"(infoType), "2"(extra));
}
#endif
#if defined(__x86_64__) || defined(__i386__) || defined(_M_IX86) || defined(_M_AMD64)
enum class VendorSignatures {
Unknown,
GenuineIntel,
AuthenticAMD,
};
VendorSignatures get_vendor_signature() {
int info[4];
cpuid(info, 0, 0);
if (info[0] < 1) {
return VendorSignatures::Unknown;
}
// "Genu ineI ntel"
if (info[1] == 0x756e6547 && info[3] == 0x49656e69 && info[2] == 0x6c65746e) {
return VendorSignatures::GenuineIntel;
}
// "Auth enti cAMD"
if (info[1] == 0x68747541 && info[3] == 0x69746e65 && info[2] == 0x444d4163) {
return VendorSignatures::AuthenticAMD;
}
return VendorSignatures::Unknown;
}
void detect_family_and_model(int info0, unsigned &family, unsigned &model) {
family = (info0 >> 8) & 0xF; // Bits 8..11
model = (info0 >> 4) & 0xF; // Bits 4..7
if (family == 0x6 || family == 0xF) {
if (family == 0xF) {
// Examine extended family ID if family ID is 0xF.
family += (info0 >> 20) & 0xFf; // Bits 20..27
}
// Examine extended model ID if family ID is 0x6 or 0xF.
model += ((info0 >> 16) & 0xF) << 4; // Bits 16..19
}
}
Target::Processor get_amd_processor(unsigned family, unsigned model, bool have_sse3) {
switch (family) {
case 0xF: // AMD Family 0Fh
if (have_sse3) {
return Target::Processor::K8_SSE3; // Hammer (modern, with SSE3)
}
return Target::Processor::K8; // Hammer (original, without SSE3)
case 0x10: // AMD Family 10h
return Target::Processor::AMDFam10; // Barcelona
case 0x14: // AMD Family 14h
return Target::Processor::BtVer1; // Bobcat
case 0x15: // AMD Family 15h
if (model >= 0x60 && model <= 0x7f) {
return Target::Processor::BdVer4; // 60h-7Fh: Excavator
}
if (model >= 0x30 && model <= 0x3f) {
return Target::Processor::BdVer3; // 30h-3Fh: Steamroller
}
if ((model >= 0x10 && model <= 0x1f) || model == 0x02) {
return Target::Processor::BdVer2; // 02h, 10h-1Fh: Piledriver
}
if (model <= 0x0f) {
return Target::Processor::BdVer1; // 00h-0Fh: Bulldozer
}
break;
case 0x16: // AMD Family 16h
return Target::Processor::BtVer2; // Jaguar
case 0x17: // AMD Family 17h
if ((model >= 0x30 && model <= 0x3f) || model == 0x71) {
return Target::Processor::ZnVer2; // 30h-3Fh, 71h: Zen2
}
if (model <= 0x0f) {
return Target::Processor::ZnVer1; // 00h-0Fh: Zen1
}
break;
case 0x19: // AMD Family 19h
if ((model & 0xf0) == 0 || model == 0x21) {
return Target::Processor::ZnVer3; // 00h-0Fh, 21h: Zen3
} else if (model == 0x61) {
return Target::Processor::ZnVer4; // 61h: Zen4
}
break;
default:
break; // Unknown AMD CPU.
}
return Target::Processor::ProcessorGeneric;
}
#endif // defined(__x86_64__) || defined(__i386__) || defined(_MSC_VER)
#ifdef __APPLE__
template<typename T>
std::optional<T> getsysctl(const char *name) {
T value;
size_t size = sizeof(value);
if (sysctlbyname(name, &value, &size, nullptr, 0)) {
return std::nullopt;
}
return std::make_optional(value);
}
bool sysctl_is_set(const char *name) {
return getsysctl<int>(name).value_or(0);
}
bool is_armv7s() {
return getsysctl<cpu_type_t>("hw.cputype") == CPU_TYPE_ARM &&
getsysctl<cpu_subtype_t>("hw.cpusubtype") == CPU_SUBTYPE_ARM_V7S;
}
#endif // __APPLE__
Target calculate_host_target() {
Target::OS os = Target::OSUnknown;
#ifdef __linux__
os = Target::Linux;
#endif
#ifdef _WIN32
os = Target::Windows;
#endif
#ifdef __APPLE__
os = Target::OSX;
#endif
bool use_64_bits = (sizeof(size_t) == 8);
int bits = use_64_bits ? 64 : 32;
int vector_bits = 0;
Target::Processor processor = Target::Processor::ProcessorGeneric;
std::vector<Target::Feature> initial_features;
#if __riscv
Target::Arch arch = Target::RISCV;
#else
#if defined(__arm__) || defined(__aarch64__) || defined(_M_ARM64) || defined(_M_ARM64EC)
Target::Arch arch = Target::ARM;
#ifdef __APPLE__
if (is_armv7s()) {
initial_features.push_back(Target::ARMv7s);
}
if (sysctl_is_set("hw.optional.arm.FEAT_DotProd")) {
initial_features.push_back(Target::ARMDotProd);
}
if (sysctl_is_set("hw.optional.arm.FEAT_FP16")) {
initial_features.push_back(Target::ARMFp16);
}
#endif
#ifdef __linux__
unsigned long hwcaps = getauxval(AT_HWCAP);
unsigned long hwcaps2 = getauxval(AT_HWCAP2);
if (hwcaps & HWCAP_ASIMDDP) {
initial_features.push_back(Target::ARMDotProd);
}
if (hwcaps & HWCAP_ASIMDHP) {
initial_features.push_back(Target::ARMFp16);
}
if (hwcaps & HWCAP_SVE) {
initial_features.push_back(Target::SVE);
}
if (hwcaps2 & HWCAP2_SVE2) {
initial_features.push_back(Target::SVE2);
}
#endif
#ifdef _MSC_VER
// Magic value from: https://github.com/dotnet/runtime/blob/7e977dcbe5efaeec2c75ed0c3e200c85b2e55522/src/native/minipal/cpufeatures.c#L19
#define PF_ARM_SVE_INSTRUCTIONS_AVAILABLE (46)
// This is the strategy used by Google's cpuinfo library for
// detecting fp16 arithmetic support on Windows.
if (!IsProcessorFeaturePresent(PF_FLOATING_POINT_EMULATED) &&
IsProcessorFeaturePresent(PF_ARM_FMAC_INSTRUCTIONS_AVAILABLE)) {
initial_features.push_back(Target::ARMFp16);
}
if (IsProcessorFeaturePresent(PF_ARM_V82_DP_INSTRUCTIONS_AVAILABLE)) {
initial_features.push_back(Target::ARMDotProd);
}
if (IsProcessorFeaturePresent(PF_ARM_SVE_INSTRUCTIONS_AVAILABLE)) {
initial_features.push_back(Target::SVE);
}
#endif
#else
#if defined(__powerpc__) && (defined(__FreeBSD__) || defined(__linux__))
Target::Arch arch = Target::POWERPC;
#if defined(__linux__)
unsigned long hwcap = getauxval(AT_HWCAP);
unsigned long hwcap2 = getauxval(AT_HWCAP2);
#elif defined(__FreeBSD__)
unsigned long hwcap, hwcap2;
elf_aux_info(AT_HWCAP, &hwcap, sizeof(hwcap));
elf_aux_info(AT_HWCAP2, &hwcap2, sizeof(hwcap2));
#endif
bool have_altivec = (hwcap & PPC_FEATURE_HAS_ALTIVEC) != 0;
bool have_vsx = (hwcap & PPC_FEATURE_HAS_VSX) != 0;
bool arch_2_07 = (hwcap2 & PPC_FEATURE2_ARCH_2_07) != 0;
user_assert(have_altivec)
<< "The POWERPC backend assumes at least AltiVec support. This machine does not appear to have AltiVec.\n";
if (have_vsx) initial_features.push_back(Target::VSX);
if (arch_2_07) initial_features.push_back(Target::POWER_ARCH_2_07);
#else
Target::Arch arch = Target::X86;
VendorSignatures vendor_signature = get_vendor_signature();
int info[4];
cpuid(info, 1, 0);
unsigned family = 0, model = 0;
detect_family_and_model(info[0], family, model);
bool have_sse41 = (info[2] & (1 << 19)) != 0; // ECX[19]
bool have_sse2 = (info[3] & (1 << 26)) != 0; // EDX[26]
bool have_sse3 = (info[2] & (1 << 0)) != 0; // ECX[0]
bool have_avx = (info[2] & (1 << 28)) != 0; // ECX[28]
bool have_f16c = (info[2] & (1 << 29)) != 0; // ECX[29]
bool have_rdrand = (info[2] & (1 << 30)) != 0; // ECX[30]
bool have_fma = (info[2] & (1 << 12)) != 0; // ECX[12]
user_assert(have_sse2)
<< "The x86 backend assumes at least sse2 support. This machine does not appear to have sse2.\n"
<< "cpuid returned: "
<< std::hex << info[0]
<< ", " << info[1]
<< ", " << info[2]
<< ", " << info[3]
<< std::dec << "\n";
if (vendor_signature == VendorSignatures::AuthenticAMD) {
processor = get_amd_processor(family, model, have_sse3);
if (processor == Target::Processor::ZnVer4) {
Target t{os, arch, bits, processor, initial_features, vector_bits};
t.set_features({Target::SSE41, Target::AVX,
Target::F16C, Target::FMA,
Target::AVX2, Target::AVX512,
Target::AVX512_Skylake, Target::AVX512_Cannonlake,
Target::AVX512_Zen4});
return t;
}
}
// Processors not specifically detected by model number above use the cpuid
// feature bits to determine what flags are supported. For future models,
// detect them explicitly above rather than extending the code below.
if (have_sse41) {
initial_features.push_back(Target::SSE41);
}
if (have_avx) {
initial_features.push_back(Target::AVX);
}
if (have_f16c) {
initial_features.push_back(Target::F16C);
}
if (have_fma) {
initial_features.push_back(Target::FMA);
}
if (use_64_bits && have_avx && have_f16c && have_rdrand) {
// So far, so good. AVX2/512?
// Call cpuid with eax=7, ecx=0
int info2[4];
cpuid(info2, 7, 0);
int info3[4];
cpuid(info3, 7, 1);
const uint32_t avx2 = 1U << 5;
const uint32_t avx512f = 1U << 16;
const uint32_t avx512dq = 1U << 17;
const uint32_t avx512pf = 1U << 26;
const uint32_t avx512er = 1U << 27;
const uint32_t avx512cd = 1U << 28;
const uint32_t avx512bw = 1U << 30;
const uint32_t avx512vl = 1U << 31;
const uint32_t avx512ifma = 1U << 21;
const uint32_t avx512 = avx512f | avx512cd;
const uint32_t avx512_knl = avx512 | avx512pf | avx512er;
const uint32_t avx512_skylake = avx512 | avx512vl | avx512bw | avx512dq;
const uint32_t avx512_cannonlake = avx512_skylake | avx512ifma; // Assume ifma => vbmi
if ((info2[1] & avx2) == avx2) {
initial_features.push_back(Target::AVX2);
}
if ((info2[1] & avx512) == avx512) {
initial_features.push_back(Target::AVX512);
// TODO: port to family/model -based detection.
if ((info2[1] & avx512_knl) == avx512_knl) {
initial_features.push_back(Target::AVX512_KNL);
}
// TODO: port to family/model -based detection.
if ((info2[1] & avx512_skylake) == avx512_skylake) {
initial_features.push_back(Target::AVX512_Skylake);
}
// TODO: port to family/model -based detection.
if ((info2[1] & avx512_cannonlake) == avx512_cannonlake) {
initial_features.push_back(Target::AVX512_Cannonlake);
const uint32_t avxvnni = 1U << 4; // avxvnni (note, not avx512vnni) result in eax
const uint32_t avx512bf16 = 1U << 5; // bf16 result in eax, with cpuid(eax=7, ecx=1)
// TODO: port to family/model -based detection.
if ((info3[0] & avxvnni) == avxvnni &&
(info3[0] & avx512bf16) == avx512bf16) {
initial_features.push_back(Target::AVX512_SapphireRapids);
}
}
}
// AVX10 converged vector instructions.
const uint32_t avx10 = 1U << 19;
if (info2[3] & avx10) {
int info_avx10[4];
cpuid(info_avx10, 0x24, 0x0);
// This checks that the AVX10 version is greater than zero.
// It isn't really needed as for now only one version exists, but
// the docs indicate bits 0:7 of EBX should be >= 0 so...
if ((info[1] & 0xff) >= 1) {
initial_features.push_back(Target::AVX10_1);
const uint32_t avx10_128 = 1U << 16;
const uint32_t avx10_256 = 1U << 17;
const uint32_t avx10_512 = 1U << 18;
// Choose the maximum one that is available.
if (info[1] & avx10_512) {
vector_bits = 512;
} else if (info[1] & avx10_256) {
vector_bits = 256;
} else if (info[1] & avx10_128) { // Not clear it is worth turning on AVX10 for this case.
vector_bits = 128;
}
}
}
// APX register extensions, etc.
const uint32_t apx = 1U << 21;
if (info3[3] & apx) {
initial_features.push_back(Target::X86APX);
}
}
#endif
#endif
#endif
return {os, arch, bits, processor, initial_features, vector_bits};
}
bool is_using_hexagon(const Target &t) {
return (t.has_feature(Target::HVX) ||
t.has_feature(Target::HVX_v62) ||
t.has_feature(Target::HVX_v65) ||
t.has_feature(Target::HVX_v66) ||
t.has_feature(Target::HVX_v68) ||
t.has_feature(Target::HexagonDma) ||
t.arch == Target::Hexagon);
}
int get_hvx_lower_bound(const Target &t) {
if (!is_using_hexagon(t)) {
return -1;
}
if (t.has_feature(Target::HVX_v62)) {
return 62;
}
if (t.has_feature(Target::HVX_v65)) {
return 65;
}
if (t.has_feature(Target::HVX_v66)) {
return 66;
}
if (t.has_feature(Target::HVX_v68)) {
return 68;
}
return 60;
}
} // namespace
Target get_host_target() {
// Calculating the host target isn't slow but it isn't free,
// and it's pointless to recalculate it every time we (e.g.) parse
// an arbitrary Target string. It won't ever change, so cache on first
// use.
static Target host_target = calculate_host_target();
return host_target;
}
namespace {
Target::Feature calculate_host_cuda_capability(Target t) {
const auto *interface = get_device_interface_for_device_api(DeviceAPI::CUDA, t);
internal_assert(interface->compute_capability);
int major, minor;
int err = interface->compute_capability(nullptr, &major, &minor);
internal_assert(err == 0) << "Failed to query cuda compute capability\n";
int ver = major * 10 + minor;
if (ver < 30) {
return Target::FeatureEnd;
} else if (ver < 32) {
return Target::CUDACapability30;
} else if (ver < 35) {
return Target::CUDACapability32;
} else if (ver < 50) {
return Target::CUDACapability35;
} else if (ver < 61) {
return Target::CUDACapability50;
} else if (ver < 70) {
return Target::CUDACapability61;
} else if (ver < 75) {
return Target::CUDACapability70;
} else if (ver < 80) {
return Target::CUDACapability75;
} else if (ver < 86) {
return Target::CUDACapability80;
} else {
return Target::CUDACapability86;
}
}
Target::Feature get_host_cuda_capability(Target t) {
static Target::Feature cap = calculate_host_cuda_capability(t);
return cap;
}
Target::Feature calculate_host_vulkan_capability(Target t) {
const auto *interface = get_device_interface_for_device_api(DeviceAPI::Vulkan, t);
internal_assert(interface->compute_capability);
int major, minor;
int err = interface->compute_capability(nullptr, &major, &minor);
internal_assert(err == 0) << "Failed to query vulkan compute capability\n";
int ver = major * 10 + minor;
if (ver < 10) {
return Target::FeatureEnd;
} else if (ver < 12) {
return Target::VulkanV10;
} else if (ver < 13) {
return Target::VulkanV12;
} else {
return Target::VulkanV13;
}
}
Target::Feature get_host_vulkan_capability(Target t) {
static Target::Feature cap = calculate_host_vulkan_capability(t);
return cap;
}
// Keep this list in sync in HalideGeneratorHelpers.cmake
const std::map<std::string, Target::OS> os_name_map = {
{"os_unknown", Target::OSUnknown},
{"linux", Target::Linux},
{"windows", Target::Windows},
{"osx", Target::OSX},
{"android", Target::Android},
{"ios", Target::IOS},
{"qurt", Target::QuRT},
{"noos", Target::NoOS},
{"fuchsia", Target::Fuchsia},
{"wasmrt", Target::WebAssemblyRuntime}};
bool lookup_os(const std::string &tok, Target::OS &result) {
auto os_iter = os_name_map.find(tok);
if (os_iter != os_name_map.end()) {
result = os_iter->second;
return true;
}
return false;
}
// Keep this list in sync in HalideGeneratorHelpers.cmake
const std::map<std::string, Target::Arch> arch_name_map = {
{"arch_unknown", Target::ArchUnknown},
{"x86", Target::X86},
{"arm", Target::ARM},
{"powerpc", Target::POWERPC},
{"hexagon", Target::Hexagon},
{"wasm", Target::WebAssembly},
{"riscv", Target::RISCV},
};
bool lookup_arch(const std::string &tok, Target::Arch &result) {
auto arch_iter = arch_name_map.find(tok);
if (arch_iter != arch_name_map.end()) {
result = arch_iter->second;
return true;
}
return false;
}
/// Important design consideration: currently, the string key is
/// effectively identical to the LLVM CPU string, and it would be really really
/// good to keep it that way, so the proper tune_* can be autogenerated easily
/// from the LLVM CPU string (currently, by replacing "-" with "_",
/// and prepending "tune_" prefix)
///
/// Please keep sorted.
const std::map<std::string, Target::Processor> processor_name_map = {
{"tune_amdfam10", Target::Processor::AMDFam10},
{"tune_bdver1", Target::Processor::BdVer1},
{"tune_bdver2", Target::Processor::BdVer2},
{"tune_bdver3", Target::Processor::BdVer3},
{"tune_bdver4", Target::Processor::BdVer4},
{"tune_btver1", Target::Processor::BtVer1},
{"tune_btver2", Target::Processor::BtVer2},
{"tune_generic", Target::Processor::ProcessorGeneric},
{"tune_k8", Target::Processor::K8},
{"tune_k8_sse3", Target::Processor::K8_SSE3},
{"tune_znver1", Target::Processor::ZnVer1},
{"tune_znver2", Target::Processor::ZnVer2},
{"tune_znver3", Target::Processor::ZnVer3},
{"tune_znver4", Target::Processor::ZnVer4},
};
bool lookup_processor(const std::string &tok, Target::Processor &result) {
auto processor_iter = processor_name_map.find(tok);
if (processor_iter != processor_name_map.end()) {
result = processor_iter->second;
return true;
}
return false;
}
const std::map<std::string, Target::Feature> feature_name_map = {
{"jit", Target::JIT},
{"debug", Target::Debug},
{"no_asserts", Target::NoAsserts},
{"no_bounds_query", Target::NoBoundsQuery},
{"sse41", Target::SSE41},
{"avx", Target::AVX},
{"avx2", Target::AVX2},
{"fma", Target::FMA},
{"fma4", Target::FMA4},
{"f16c", Target::F16C},
{"armv7s", Target::ARMv7s},
{"no_neon", Target::NoNEON},
{"vsx", Target::VSX},
{"power_arch_2_07", Target::POWER_ARCH_2_07},
{"cuda", Target::CUDA},
{"cuda_capability_30", Target::CUDACapability30},
{"cuda_capability_32", Target::CUDACapability32},
{"cuda_capability_35", Target::CUDACapability35},
{"cuda_capability_50", Target::CUDACapability50},
{"cuda_capability_61", Target::CUDACapability61},
{"cuda_capability_70", Target::CUDACapability70},
{"cuda_capability_75", Target::CUDACapability75},
{"cuda_capability_80", Target::CUDACapability80},
{"cuda_capability_86", Target::CUDACapability86},
{"opencl", Target::OpenCL},
{"cl_doubles", Target::CLDoubles},
{"cl_half", Target::CLHalf},
{"cl_atomics64", Target::CLAtomics64},
{"egl", Target::EGL},
{"user_context", Target::UserContext},
{"profile", Target::Profile},
{"no_runtime", Target::NoRuntime},
{"metal", Target::Metal},
{"c_plus_plus_name_mangling", Target::CPlusPlusMangling},
{"large_buffers", Target::LargeBuffers},
{"hvx", Target::HVX_128},
{"hvx_128", Target::HVX_128},
{"hvx_v62", Target::HVX_v62},
{"hvx_v65", Target::HVX_v65},
{"hvx_v66", Target::HVX_v66},
{"hvx_v68", Target::HVX_v68},
{"fuzz_float_stores", Target::FuzzFloatStores},
{"soft_float_abi", Target::SoftFloatABI},
{"msan", Target::MSAN},
{"avx512", Target::AVX512},
{"avx512_knl", Target::AVX512_KNL},
{"avx512_skylake", Target::AVX512_Skylake},
{"avx512_cannonlake", Target::AVX512_Cannonlake},
{"avx512_sapphirerapids", Target::AVX512_SapphireRapids},
{"avx512_zen4", Target::AVX512_Zen4},
{"trace_loads", Target::TraceLoads},
{"trace_stores", Target::TraceStores},
{"trace_realizations", Target::TraceRealizations},
{"trace_pipeline", Target::TracePipeline},
{"d3d12compute", Target::D3D12Compute},
{"strict_float", Target::StrictFloat},
{"tsan", Target::TSAN},
{"asan", Target::ASAN},
{"check_unsafe_promises", Target::CheckUnsafePromises},
{"hexagon_dma", Target::HexagonDma},
{"embed_bitcode", Target::EmbedBitcode},
{"enable_llvm_loop_opt", Target::EnableLLVMLoopOpt},
{"wasm_simd128", Target::WasmSimd128},
{"wasm_mvponly", Target::WasmMvpOnly},
{"wasm_threads", Target::WasmThreads},
{"wasm_bulk_memory", Target::WasmBulkMemory},
{"webgpu", Target::WebGPU},
{"sve", Target::SVE},
{"sve2", Target::SVE2},
{"arm_dot_prod", Target::ARMDotProd},
{"arm_fp16", Target::ARMFp16},
{"llvm_large_code_model", Target::LLVMLargeCodeModel},
{"rvv", Target::RVV},
{"armv8a", Target::ARMv8a},
{"armv81a", Target::ARMv81a},
{"armv82a", Target::ARMv82a},
{"armv83a", Target::ARMv83a},
{"armv84a", Target::ARMv84a},
{"armv85a", Target::ARMv85a},
{"armv86a", Target::ARMv86a},
{"armv87a", Target::ARMv87a},
{"armv88a", Target::ARMv88a},
{"armv89a", Target::ARMv89a},
{"sanitizer_coverage", Target::SanitizerCoverage},
{"profile_by_timer", Target::ProfileByTimer},
{"spirv", Target::SPIRV},
{"vulkan", Target::Vulkan},
{"vk_int8", Target::VulkanInt8},
{"vk_int16", Target::VulkanInt16},
{"vk_int64", Target::VulkanInt64},
{"vk_float16", Target::VulkanFloat16},
{"vk_float64", Target::VulkanFloat64},
{"vk_v10", Target::VulkanV10},
{"vk_v12", Target::VulkanV12},
{"vk_v13", Target::VulkanV13},
{"semihosting", Target::Semihosting},
{"avx10_1", Target::AVX10_1},
{"x86apx", Target::X86APX},
// NOTE: When adding features to this map, be sure to update PyEnums.cpp as well.
};
bool lookup_feature(const std::string &tok, Target::Feature &result) {
auto feature_iter = feature_name_map.find(tok);
if (feature_iter != feature_name_map.end()) {
result = feature_iter->second;
return true;
}
return false;
}
int parse_vector_bits(const std::string &tok) {
if (tok.find("vector_bits_") == 0) {
std::string num = tok.substr(sizeof("vector_bits_") - 1, std::string::npos);
size_t end_index;
int parsed = std::stoi(num, &end_index);
if (end_index == num.size()) {
return parsed;
}
}
return -1;
}
void set_sanitizer_bits(Target &t) {
// Note, we must include Util.h for these to be defined properly (or not)
#ifdef HALIDE_INTERNAL_USING_ASAN
t.set_feature(Target::ASAN);
#endif
#ifdef HALIDE_INTERNAL_USING_MSAN
t.set_feature(Target::MSAN);
#endif
#ifdef HALIDE_INTERNAL_USING_TSAN
t.set_feature(Target::TSAN);
#endif
#ifdef HALIDE_INTERNAL_USING_COVSAN
t.set_feature(Target::SanitizerCoverage);
#endif
}
} // End anonymous namespace
Target get_target_from_environment() {
string target = Internal::get_env_variable("HL_TARGET");
if (target.empty()) {
return get_host_target();
} else {
return Target(target);
}
}
Target get_jit_target_from_environment() {
Target host = get_host_target();
host.set_feature(Target::JIT);
string target = Internal::get_env_variable("HL_JIT_TARGET");
if (target.empty()) {
set_sanitizer_bits(host);
return host;
} else {
Target t(target);
t.set_feature(Target::JIT);
user_assert((t.os == host.os && t.arch == host.arch && t.bits == host.bits) || Internal::WasmModule::can_jit_target(t))
<< "HL_JIT_TARGET must match the host OS, architecture, and bit width.\n"
<< "HL_JIT_TARGET was " << target << ". "
<< "Host is " << host.to_string() << ".\n";
user_assert(!t.has_feature(Target::NoBoundsQuery))
<< "The Halide JIT requires the use of bounds query, but HL_JIT_TARGET was specified with no_bounds_query: " << target;
set_sanitizer_bits(t);
return t;
}
}
namespace {
bool merge_string(Target &t, const std::string &target) {
string rest = target;
vector<string> tokens;
size_t first_dash;
while ((first_dash = rest.find('-')) != string::npos) {
// Internal::debug(0) << first_dash << ", " << rest << "\n";
tokens.push_back(rest.substr(0, first_dash));
rest = rest.substr(first_dash + 1);
}
tokens.push_back(rest);
bool os_specified = false, arch_specified = false, bits_specified = false, processor_specified = false, features_specified = false;
bool is_host = false;
for (size_t i = 0; i < tokens.size(); i++) {
const string &tok = tokens[i];
Target::Feature feature;
int vector_bits;
if (tok == "host") {
if (i > 0) {
// "host" is now only allowed as the first token.
return false;
}
is_host = true;
t = get_host_target();
} else if (tok == "32" || tok == "64" || tok == "0") {
if (bits_specified) {
return false;
}
bits_specified = true;
t.bits = std::stoi(tok);
} else if (lookup_arch(tok, t.arch)) {
if (arch_specified) {
return false;
}
arch_specified = true;
} else if (lookup_os(tok, t.os)) {
if (os_specified) {
return false;
}
os_specified = true;
} else if (lookup_processor(tok, t.processor_tune)) {
if (processor_specified) {
return false;
}
processor_specified = true;
} else if (lookup_feature(tok, feature)) {
t.set_feature(feature);
features_specified = true;
} else if (tok == "trace_all") {
t.set_features({Target::TraceLoads, Target::TraceStores, Target::TraceRealizations});
features_specified = true;
} else if ((vector_bits = parse_vector_bits(tok)) >= 0) {
t.vector_bits = vector_bits;
} else {
return false;
}
}
if (is_host &&
t.has_feature(Target::CUDA) &&
!t.has_feature(Target::CUDACapability30) &&
!t.has_feature(Target::CUDACapability32) &&
!t.has_feature(Target::CUDACapability35) &&
!t.has_feature(Target::CUDACapability50) &&
!t.has_feature(Target::CUDACapability61) &&
!t.has_feature(Target::CUDACapability70) &&
!t.has_feature(Target::CUDACapability75) &&
!t.has_feature(Target::CUDACapability80) &&
!t.has_feature(Target::CUDACapability86)) {
// Detect host cuda capability
t.set_feature(get_host_cuda_capability(t));
}
if (is_host &&
t.has_feature(Target::Vulkan) &&
!t.has_feature(Target::VulkanV10) &&
!t.has_feature(Target::VulkanV12) &&
!t.has_feature(Target::VulkanV13)) {
// Detect host vulkan capability
t.set_feature(get_host_vulkan_capability(t));
}
if (arch_specified && !bits_specified) {
return false;
}
if (bits_specified && t.bits == 0) {
// bits == 0 is allowed iff arch and os are "unknown" and no features are set,
// to allow for roundtripping the string for default Target() ctor.
if (!(arch_specified && t.arch == Target::ArchUnknown) ||
!(os_specified && t.os == Target::OSUnknown) ||
features_specified) {
return false;
}
}
return true;
}
void bad_target_string(const std::string &target) {
const char *separator = "";
std::string architectures;
for (const auto &arch_entry : arch_name_map) {
architectures += separator + arch_entry.first;
separator = ", ";
}
separator = "";
std::string oses;
for (const auto &os_entry : os_name_map) {
oses += separator + os_entry.first;
separator = ", ";
}
separator = "";
std::string processors;
for (const auto &processor_entry : processor_name_map) {
processors += separator + processor_entry.first;
separator = ", ";
}
separator = "";
// Format the features to go one feature over 70 characters per line,
// assume the first line starts with "Features are ".
int line_char_start = -(int)sizeof("Features are");
std::string features;
for (const auto &feature_entry : feature_name_map) {
features += separator + feature_entry.first;
if (features.length() - line_char_start > 70) {
separator = "\n";
line_char_start = features.length();
} else {
separator = ", ";
}
}
user_error << "Did not understand Halide target " << target << "\n"
<< "Expected format is arch-bits-os-processor-feature1-feature2-...\n"
<< "Where arch is: " << architectures << ".\n"
<< "bits is either 32 or 64.\n"
<< "os is: " << oses << ".\n"
<< "processor is: " << processors << ".\n"
<< "\n"
<< "If arch, bits, or os are omitted, they default to the host.\n"
<< "\n"
<< "If processor is omitted, it defaults to tune_generic.\n"
<< "\n"
<< "Features are: " << features << ".\n"
<< "\n"
<< "The target can also begin with \"host\", which sets the "
<< "host's architecture, os, and feature set, with the "
<< "exception of the GPU runtimes, which default to off.\n"
<< "\n"
<< "On this platform, the host target is: " << get_host_target().to_string() << "\n";
}
void do_check_bad(const Target &t, const std::initializer_list<Target::Feature> &v) {
for (Target::Feature f : v) {
user_assert(!t.has_feature(f))
<< "Target feature " << Target::feature_to_name(f)
<< " is incompatible with the Target's architecture. (" << t << ")\n";
}
}
} // namespace
void Target::validate_features() const {
// Note that the features don't have to be exhaustive, but enough to avoid obvious mistakes is good.
if (arch == X86) {
do_check_bad(*this, {
ARMDotProd,
ARMFp16,
ARMv7s,
ARMv81a,
NoNEON,
POWER_ARCH_2_07,
RVV,
SVE,
SVE2,
VSX,
WasmBulkMemory,
WasmMvpOnly,
WasmSimd128,
WasmThreads,
});
} else if (arch == ARM) {
do_check_bad(*this, {
AVX,
AVX2,
AVX512,
AVX512_Cannonlake,
AVX512_KNL,
AVX512_SapphireRapids,
AVX512_Skylake,
AVX512_Zen4,
F16C,
FMA,
FMA4,
POWER_ARCH_2_07,
RVV,
SSE41,
VSX,
WasmBulkMemory,
WasmMvpOnly,
WasmSimd128,
WasmThreads,
});
} else if (arch == WebAssembly) {
do_check_bad(*this, {
ARMDotProd,
ARMFp16,