forked from apache/tvm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
llvm_instance.cc
960 lines (861 loc) · 31 KB
/
llvm_instance.cc
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
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
#ifdef TVM_LLVM_VERSION
#include "llvm_instance.h"
#include <dmlc/base.h>
#include <llvm/ADT/ArrayRef.h>
#include <llvm/ADT/StringRef.h>
#if TVM_LLVM_VERSION >= 150
#include <llvm/IR/FMF.h>
#else
#include <llvm/IR/Operator.h>
#endif
#include <llvm/IR/LLVMContext.h>
#include <llvm/IR/Metadata.h>
#include <llvm/IR/Module.h>
#include <llvm/IRReader/IRReader.h>
#include <llvm/MC/MCSubtargetInfo.h>
#if TVM_LLVM_VERSION >= 140
#include <llvm/MC/TargetRegistry.h>
#else
#include <llvm/Support/TargetRegistry.h>
#endif
#include <llvm/Support/CodeGen.h>
#include <llvm/Support/CommandLine.h>
#include <llvm/Support/ErrorOr.h>
#include <llvm/Support/Host.h>
#include <llvm/Support/MemoryBuffer.h>
#include <llvm/Support/SourceMgr.h>
#include <llvm/Support/TargetSelect.h>
#include <llvm/Support/raw_ostream.h>
#include <llvm/Target/TargetMachine.h>
#include <llvm/Target/TargetOptions.h>
#include <tvm/runtime/container/array.h>
#include <tvm/runtime/container/map.h>
#include <tvm/runtime/container/optional.h>
#include <tvm/runtime/container/string.h>
#include <tvm/runtime/logging.h>
#include <tvm/runtime/object.h>
#include <tvm/target/target.h>
#include <atomic>
#include <cctype>
#include <memory>
#include <optional>
#include <ostream>
#include <sstream>
#include <string>
#include <system_error>
#include <utility>
#if TVM_LLVM_VERSION < 180
namespace llvm {
#if TVM_LLVM_VERSION < 170
// SubtargetSubTypeKV view
template <ArrayRef<SubtargetSubTypeKV> MCSubtargetInfo::*Member>
struct ArchViewer {
friend ArrayRef<SubtargetSubTypeKV>& archViewer(MCSubtargetInfo Obj) { return Obj.*Member; }
};
template struct ArchViewer<&MCSubtargetInfo::ProcDesc>;
ArrayRef<SubtargetSubTypeKV>& archViewer(MCSubtargetInfo);
#endif
// SubtargetFeatureKV view
template <ArrayRef<SubtargetFeatureKV> MCSubtargetInfo::*Member>
struct FeatViewer {
friend ArrayRef<SubtargetFeatureKV>& featViewer(MCSubtargetInfo Obj) { return Obj.*Member; }
};
template struct FeatViewer<&MCSubtargetInfo::ProcFeatures>;
ArrayRef<SubtargetFeatureKV>& featViewer(MCSubtargetInfo);
} // namespace llvm
#endif
namespace tvm {
namespace codegen {
namespace {
namespace defaults {
static const char* cpu = "generic";
#if TVM_LLVM_VERSION <= 170
static const llvm::CodeGenOpt::Level opt_level = llvm::CodeGenOpt::Aggressive;
#else
static const llvm::CodeGenOptLevel opt_level = llvm::CodeGenOptLevel::Aggressive;
#endif
} // namespace defaults
} // namespace
namespace {
bool InitializeLLVM() {
static std::atomic_flag initialized = ATOMIC_FLAG_INIT;
if (!initialized.test_and_set()) {
llvm::InitializeAllTargetInfos();
llvm::InitializeAllTargets();
llvm::InitializeAllTargetMCs();
llvm::InitializeAllAsmParsers();
llvm::InitializeAllAsmPrinters();
}
return true;
}
std::string Join(std::string sep, llvm::ArrayRef<std::string> strings) {
std::string result;
bool is_first = true;
for (const std::string& s : strings) {
if (!is_first) {
result += sep;
}
result += s;
is_first = false;
}
return result;
}
} // namespace
// LLVMInstance
LLVMInstance::LLVMInstance() {
// Call InitializeLLVM before anything else.
static const bool DMLC_ATTRIBUTE_UNUSED init_llvm = InitializeLLVM();
ctx_ = std::make_shared<llvm::LLVMContext>();
}
LLVMInstance::~LLVMInstance() = default;
std::unique_ptr<llvm::Module> LLVMInstance::ParseIR(const std::string& llvm_ir) const {
auto buffer = llvm::MemoryBuffer::getMemBuffer(llvm_ir, /*BufferName=*/"",
/*RequiresNullTerminator=*/false);
return ParseBuffer(*buffer);
}
std::unique_ptr<llvm::Module> LLVMInstance::LoadIR(const std::string& file_name) const {
llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>> maybe_buffer =
llvm::MemoryBuffer::getFileAsStream(file_name);
if (std::error_code ec = maybe_buffer.getError()) {
LOG(FATAL) << ec.message();
}
return ParseBuffer(**maybe_buffer);
}
std::unique_ptr<llvm::Module> LLVMInstance::ParseBuffer(const llvm::MemoryBuffer& buffer) const {
llvm::SMDiagnostic error;
std::unique_ptr<llvm::Module> module = llvm::parseIR(buffer.getMemBufferRef(), error, *ctx_);
if (module == nullptr) {
std::string message;
llvm::raw_string_ostream ostream(message);
error.print(/*ProgName=*/nullptr, ostream, /*ShowColors=*/false, /*ShowKindLabel=*/true);
LOG(FATAL) << ostream.str();
}
return module;
}
// LLVMTargetInfo
std::ostream& operator<<(std::ostream& os, const LLVMTargetInfo::Option& opt) {
os << '-' << opt.name;
switch (opt.type) {
case LLVMTargetInfo::Option::OptType::Bool:
return os << ":bool=" << (opt.value.b ? "true" : "false");
case LLVMTargetInfo::Option::OptType::Int:
return os << ":int=" << opt.value.i;
case LLVMTargetInfo::Option::OptType::UInt:
return os << ":uint=" << opt.value.u;
case LLVMTargetInfo::Option::OptType::String:
return os << ":string=" << opt.value.s;
default:
os << ":?(" << static_cast<int>(opt.type) << ")";
break;
}
return os;
}
LLVMTargetInfo::LLVMTargetInfo(LLVMInstance& instance, const Target& target) {
triple_ = target->GetAttr<String>("mtriple").value_or("default");
if (triple_.empty() || triple_ == "default") {
triple_ = llvm::sys::getDefaultTargetTriple();
}
cpu_ = target->GetAttr<String>("mcpu").value_or(defaults::cpu);
if (const Optional<Array<String>>& v = target->GetAttr<Array<String>>("mattr")) {
for (const String& s : v.value()) {
attrs_.push_back(s);
}
}
// llvm module target
if (target->kind->name == "llvm") {
// legalize -mcpu with the target -mtriple
auto arches = GetAllLLVMTargetArches();
bool has_arch =
std::any_of(arches.begin(), arches.end(), [&](const auto& var) { return var == cpu_; });
if (!has_arch) {
LOG(FATAL) << "LLVM cpu architecture `-mcpu=" << cpu_
<< "` is not valid in `-mtriple=" << triple_ << "`";
}
}
if (const Optional<Array<String>>& v = target->GetAttr<Array<String>>("cl-opt")) {
llvm::StringMap<llvm::cl::Option*>& options = llvm::cl::getRegisteredOptions();
bool parse_error = false;
for (const String& s : v.value()) {
Option opt = ParseOptionString(s);
if (opt.type == Option::OptType::Invalid) {
parse_error = true;
continue;
}
if (options.count(opt.name)) {
llvm_options_.push_back(opt);
} else {
// Flag an error, but don't abort. LLVM flags may change, and this would
// give the code a chance to run even if the option no longer applies.
LOG(ERROR) << "\"" << opt.name << "\" is not an LLVM option, option ignored";
}
}
ICHECK(!parse_error) << "there were errors parsing command-line options";
}
llvm::FloatABI::ABIType float_abi = llvm::FloatABI::Default;
if (const Optional<String>& v = target->GetAttr<String>("mfloat-abi")) {
String value = v.value();
if (value == "hard") {
float_abi = llvm::FloatABI::Hard;
} else if (value == "soft") {
float_abi = llvm::FloatABI::Soft;
} else {
LOG(FATAL) << "invalid -mfloat-abi option " << value;
}
}
// Target options
#if TVM_LLVM_VERSION < 50
target_options_.LessPreciseFPMADOption = true;
#endif
// In clang, these are fed from LangOpts which describe language specific features
// TODO(AndrewZhaoLuo): figure out how these relate to fast math flags
target_options_.AllowFPOpFusion = llvm::FPOpFusion::Fast;
target_options_.UnsafeFPMath = false;
target_options_.NoInfsFPMath = false;
target_options_.NoNaNsFPMath = true;
target_options_.FloatABIType = float_abi;
if (const Optional<String>& v = target->GetAttr<String>("mabi")) {
target_options_.MCOptions.ABIName = v.value();
}
auto maybe_level = target->GetAttr<Integer>("opt-level");
#if TVM_LLVM_VERSION <= 170
if (maybe_level.defined()) {
int level = maybe_level.value()->value;
if (level <= 0) {
opt_level_ = llvm::CodeGenOpt::None;
} else if (level == 1) {
opt_level_ = llvm::CodeGenOpt::Less;
} else if (level == 2) {
opt_level_ = llvm::CodeGenOpt::Default;
} else {
// level >= 3
opt_level_ = llvm::CodeGenOpt::Aggressive;
}
} else {
opt_level_ = defaults::opt_level;
}
#else
if (maybe_level.defined()) {
int level = maybe_level.value()->value;
if (level <= 0) {
opt_level_ = llvm::CodeGenOptLevel::None;
} else if (level == 1) {
opt_level_ = llvm::CodeGenOptLevel::Less;
} else if (level == 2) {
opt_level_ = llvm::CodeGenOptLevel::Default;
} else {
// level >= 3
opt_level_ = llvm::CodeGenOptLevel::Aggressive;
}
} else {
opt_level_ = defaults::opt_level;
}
#endif
target_options_.UseInitArray = true;
// Fast math options
auto GetBoolFlag = [&target](llvm::StringRef flag) -> bool {
return target->GetAttr<Bool>(flag.str()).value_or(Bool(false));
};
if (GetBoolFlag("fast-math")) {
#if TVM_LLVM_VERSION >= 60
fast_math_flags_.setFast();
#else
fast_math_flags_.setUnsafeAlgebra();
#endif
} else {
#if TVM_LLVM_VERSION >= 50
// This option was added in 5.x, and has a boolean argument,
// unlike the rest of options at the time.
fast_math_flags_.setAllowContract(GetBoolFlag("fast-math-contract"));
#endif
#if TVM_LLVM_VERSION >= 70
fast_math_flags_.setNoNaNs(GetBoolFlag("fast-math-nnan"));
fast_math_flags_.setNoInfs(GetBoolFlag("fast-math-ninf"));
fast_math_flags_.setNoSignedZeros(GetBoolFlag("fast-math-nsz"));
fast_math_flags_.setAllowReciprocal(GetBoolFlag("fast-math-arcp"));
fast_math_flags_.setAllowContract(GetBoolFlag("fast-math-contract"));
fast_math_flags_.setAllowReassoc(GetBoolFlag("fast-math-reassoc"));
fast_math_flags_.setApproxFunc(GetBoolFlag("fast-math-afn"));
#else
// LLVM 4.x, 5.x, and 6.x
if (GetBoolFlag("fast-math-nnan")) fast_math_flags_.setNoNaNs();
if (GetBoolFlag("fast-math-ninf")) fast_math_flags_.setNoInfs();
if (GetBoolFlag("fast-math-nsz")) fast_math_flags_.setNoSignedZeros();
if (GetBoolFlag("fast-math-arcp")) fast_math_flags_.setAllowReciprocal();
#if TVM_LLVM_VERSION >= 60
if (GetBoolFlag("fast-math-reassoc")) fast_math_flags_.setAllowReassoc();
if (GetBoolFlag("fast-math-afn")) fast_math_flags_.setApproxFunc();
#endif
#endif
}
}
LLVMTargetInfo::LLVMTargetInfo(LLVMInstance& scope, const std::string& target_str)
: LLVMTargetInfo(scope, Target(target_str)) {}
LLVMTargetInfo::~LLVMTargetInfo() = default;
static const llvm::Target* CreateLLVMTargetInstance(const std::string triple,
const bool allow_missing = true) {
std::string error;
// create LLVM instance
// required mimimum: llvm::InitializeAllTargets()
const llvm::Target* llvm_instance = llvm::TargetRegistry::lookupTarget(triple, error);
if (!allow_missing && !llvm_instance) {
ICHECK(llvm_instance) << "LLVM instance error: `" << error << "`";
}
return llvm_instance;
}
static llvm::TargetMachine* CreateLLVMTargetMachine(
const llvm::Target* llvm_instance, const std::string& triple, const std::string& cpu,
const std::string& features, const llvm::TargetOptions& target_options,
const llvm::Reloc::Model& reloc_model, const llvm::CodeModel::Model& code_model,
#if TVM_LLVM_VERSION <= 170
const llvm::CodeGenOpt::Level& opt_level) {
#else
const llvm::CodeGenOptLevel& opt_level) {
#endif
llvm::TargetMachine* tm = llvm_instance->createTargetMachine(
triple, cpu, features, target_options, reloc_model, code_model, opt_level);
ICHECK(tm != nullptr);
return tm;
}
static const llvm::MCSubtargetInfo* GetLLVMSubtargetInfo(const std::string& triple,
const std::string& cpu_name,
const std::string& feats) {
// create a LLVM instance
auto llvm_instance = CreateLLVMTargetInstance(triple, true);
// create a target machine
// required minimum: llvm::InitializeAllTargetMCs()
llvm::TargetOptions target_options;
auto tm = CreateLLVMTargetMachine(llvm_instance, triple, cpu_name, feats, target_options,
llvm::Reloc::Static, llvm::CodeModel::Small,
#if TVM_LLVM_VERSION <= 170
llvm::CodeGenOpt::Level(0));
#else
llvm::CodeGenOptLevel(0));
#endif
// create subtarget info module
const llvm::MCSubtargetInfo* MCInfo = tm->getMCSubtargetInfo();
return MCInfo;
}
llvm::TargetMachine* LLVMTargetInfo::GetOrCreateTargetMachine(bool allow_missing) {
if (target_machine_) return target_machine_.get();
std::string error;
if (const llvm::Target* llvm_instance = CreateLLVMTargetInstance(triple_, allow_missing)) {
llvm::TargetMachine* tm =
CreateLLVMTargetMachine(llvm_instance, triple_, cpu_, GetTargetFeatureString(),
target_options_, reloc_model_, code_model_, opt_level_);
target_machine_ = std::unique_ptr<llvm::TargetMachine>(tm);
}
ICHECK(target_machine_ != nullptr);
return target_machine_.get();
}
std::string LLVMTargetInfo::GetTargetFeatureString() const { //
return Join(",", attrs_);
}
std::string LLVMTargetInfo::str() const {
std::ostringstream os;
os << "llvm";
if (!triple_.empty()) {
os << " -mtriple=" << triple_;
}
if (!cpu_.empty() && cpu_ != defaults::cpu) {
os << " -mcpu=" << cpu_;
}
if (!attrs_.empty()) {
os << " -mattr=" << GetTargetFeatureString();
}
switch (target_options_.FloatABIType) {
case llvm::FloatABI::Soft:
os << " -mfloat-abi=soft";
break;
case llvm::FloatABI::Hard:
os << " -mfloat-abi=hard";
break;
case llvm::FloatABI::Default:
break;
}
if (!target_options_.MCOptions.ABIName.empty()) {
os << " -mabi=" << target_options_.MCOptions.ABIName;
}
bool do_individual = true;
#if TVM_LLVM_VERSION >= 60
if (fast_math_flags_.isFast()) {
os << " -fast-math";
do_individual = false;
}
#else
if (fast_math_flags_.unsafeAlgebra()) {
os << " -fast-math";
do_individual = false;
}
#endif
if (do_individual) {
if (fast_math_flags_.noNaNs()) os << " -fast-math-nnan";
if (fast_math_flags_.noInfs()) os << " -fast-math-ninf";
if (fast_math_flags_.noSignedZeros()) os << " -fast-math-nsz";
if (fast_math_flags_.allowReciprocal()) os << " -fast-math-arcp";
#if TVM_LLVM_VERSION >= 50
if (fast_math_flags_.allowContract()) os << " -fast-math-contract";
#endif
#if TVM_LLVM_VERSION >= 60
if (fast_math_flags_.allowReassoc()) os << " -fast-math-reassoc";
if (fast_math_flags_.approxFunc()) os << " -fast-math-afn";
#endif
}
#if TVM_LLVM_VERSION <= 170
if (opt_level_ != defaults::opt_level) {
os << " -opt-level=";
switch (opt_level_) {
case llvm::CodeGenOpt::None:
os << "0";
break;
case llvm::CodeGenOpt::Less:
os << "1";
break;
case llvm::CodeGenOpt::Default:
os << "2";
break;
case llvm::CodeGenOpt::Aggressive:
os << "3";
break;
}
}
#else
if (opt_level_ != defaults::opt_level) {
os << " -opt-level=";
switch (opt_level_) {
case llvm::CodeGenOptLevel::None:
os << "0";
break;
case llvm::CodeGenOptLevel::Less:
os << "1";
break;
case llvm::CodeGenOptLevel::Default:
os << "2";
break;
case llvm::CodeGenOptLevel::Aggressive:
os << "3";
break;
}
}
#endif
if (size_t num = llvm_options_.size(); num > 0) {
os << " -cl-opt=";
std::vector<std::string> opts;
for (const Option& opt : llvm_options_) {
std::stringstream os;
os << opt;
opts.emplace_back(os.str());
}
auto* quote = num > 1 ? "'" : "";
os << quote << Join(",", opts) << quote;
}
return os.str();
}
LLVMTargetInfo::Option LLVMTargetInfo::ParseOptionString(const std::string& str) {
Option opt;
opt.type = Option::OptType::Invalid;
// Option string: "-"+ <option_name> ":" <type> "=" <value>
//
// Note: "-"+ means 1 or more dashes, but only "-" are "--" valid.
// The first step is to do "lexing" of the option string, i.e. to break
// it up into parts (like "tokens") according to the syntax above. These
// parts will be non-overlapping substrings of the option string, and
// concatenated together, they will be equal to the option string.
// The literal elements are parts on their own.
//
// Note that the option string may be malformed, so any of the literal
// elements in the syntax may be missing.
std::vector<std::string> parts;
auto find_first_of = [](const std::string& str, const std::string& chars, auto start = 0) {
auto pos = str.find_first_of(chars, start);
return pos != std::string::npos ? pos : str.size();
};
auto find_first_not_of = [](const std::string& str, const std::string& chars, auto start = 0) {
auto pos = str.find_first_not_of(chars, start);
return pos != std::string::npos ? pos : str.size();
};
// "-"+
std::string::size_type pos_start = 0, pos_end = str.size();
std::string::size_type pos_at = find_first_not_of(str, "-", pos_start);
if (pos_at > 0) {
parts.push_back(str.substr(pos_start, pos_at));
}
// <option_name>, always present, may be empty string
pos_start = pos_at;
pos_at = find_first_of(str, ":=", pos_start);
parts.push_back(str.substr(pos_start, pos_at - pos_start));
// ":" or "=", if any
pos_start = pos_at;
char c = pos_start < pos_end ? str[pos_start] : 0;
if (c != 0) {
parts.emplace_back(1, c);
pos_start++;
}
// If the character found in the previous step wasn't '=', look for '='.
if (c == ':') {
// <type>
pos_at = find_first_of(str, "=", pos_start);
if (pos_at > pos_start) { // if non-empty
parts.push_back(str.substr(pos_start, pos_at - pos_start));
}
// "="
if (pos_at < pos_end) {
parts.emplace_back(1, str[pos_at]);
pos_start = pos_at + 1;
}
}
if (pos_start < pos_end) {
// <value>
parts.push_back(str.substr(pos_start));
}
// After breaking up the option string, examine and validate the individual
// parts.
int part_this = 0, part_end = parts.size();
const std::string error_header = "while parsing option \"" + str + "\": ";
// Check for "-" or "--".
if (part_this < part_end) {
auto& p = parts[part_this++];
if ((p.size() != 1 && p.size() != 2) || p.find_first_not_of('-') != std::string::npos) {
LOG(ERROR) << error_header << "option must start with \"-\" or \"--\"";
return opt;
}
}
// Validate option name.
if (part_this < part_end) {
auto& p = parts[part_this++];
if (p.empty()) {
LOG(ERROR) << error_header << "option name must not be empty";
return opt;
}
opt.name = std::move(p);
}
// Check type, if present.
Option::OptType type = Option::OptType::Invalid;
if (part_this < part_end) {
auto& p0 = parts[part_this];
if (p0 == ":") {
part_this++; // Only advance if we saw ":".
if (part_this < part_end) {
auto& p1 = parts[part_this];
ICHECK(!p1.empty()) << "tokenizing error"; // This shouldn't happen.
if (p1 != "=") {
part_this++;
if (p1 == "bool") {
type = Option::OptType::Bool;
} else if (p1 == "int") {
type = Option::OptType::Int;
} else if (p1 == "uint") {
type = Option::OptType::UInt;
} else if (p1 == "string") {
type = Option::OptType::String;
}
}
}
// If there was ":", there must be a type.
if (type == Option::OptType::Invalid) {
LOG(ERROR) << error_header << "invalid type";
return opt;
}
}
}
// Check value, if present.
std::optional<std::string> value;
if (part_this < part_end) {
auto& p0 = parts[part_this];
if (p0 == "=") {
part_this++;
if (part_this < part_end) {
value = std::move(parts[part_this]);
} else {
value = "";
}
} else {
// If there are still any parts left to be processed, there must be "=".
LOG(ERROR) << error_header << "expecting \"=\"";
return opt;
}
}
// NOLINTNEXTLINE(runtime/int)
auto to_integer = [](const std::string& s) -> std::optional<long long> {
// std::stoll takes "long long"
long long number; // NOLINT(runtime/int)
size_t pos;
try {
number = std::stoll(s, &pos);
} catch (...) {
return std::nullopt;
}
if (pos == s.size()) {
return number;
} else {
return std::nullopt;
}
};
auto to_boolean = [&to_integer](const std::string& s) -> std::optional<bool> {
// Return 0 or 1, if string corresponds to a valid boolean value,
// otherwise return 2.
auto ti = to_integer(s);
if (ti.has_value() && (ti.value() == 0 || ti.value() == 1)) {
return static_cast<bool>(ti.value());
}
std::string lower;
std::transform(s.begin(), s.end(), std::back_inserter(lower),
[](unsigned char c) { return std::tolower(c); });
if (lower == "true") {
return true;
} else if (lower == "false") {
return false;
}
return std::nullopt;
};
if (value.has_value()) {
if (type == Option::OptType::Int || type == Option::OptType::UInt) {
auto v = to_integer(value.value());
if (!v.has_value()) {
LOG(ERROR) << error_header << "invalid integer value \"" << value.value() << "\"";
return opt;
}
if (type == Option::OptType::Int) {
opt.value.i = static_cast<int>(v.value());
if (opt.value.i != v.value()) {
LOG(WARNING) << error_header << "value exceeds int range, assuming " << opt.value.i;
}
} else {
// NOLINTNEXTLINE(runtime/int)
opt.value.u = static_cast<unsigned>(static_cast<unsigned long long>(v.value()));
if (opt.value.u != static_cast<unsigned long long>(v.value())) { // NOLINT(runtime/int)
LOG(WARNING) << error_header << "value exceeds int range, assuming " << opt.value.u;
}
}
} else if (type == Option::OptType::String) {
opt.value.s = std::move(value.value());
} else {
// "type" is either Bool (given explicitly) or Invalid (type not present in string)
auto v = to_boolean(value.value());
if (!v.has_value()) {
LOG(ERROR) << error_header << "invalid boolean value \"" << value.value() << "\"";
return opt;
}
opt.value.b = v.value();
type = Option::OptType::Bool;
}
} else {
// Value was not present in string. Assume "true" if "type" is Bool or Invalid
if (type == Option::OptType::Bool || type == Option::OptType::Invalid) {
opt.value.b = true;
type = Option::OptType::Bool;
} else {
LOG(ERROR) << error_header << "must have a value";
return opt;
}
}
ICHECK(type != Option::OptType::Invalid);
opt.type = type;
return opt;
}
bool LLVMTargetInfo::MatchesGlobalState() const {
for (const Option& opt : GetCommandLineOptions()) {
Option current_opt = opt;
GetOptionValue(¤t_opt);
ICHECK(current_opt.type != Option::OptType::Invalid);
switch (current_opt.type) {
case Option::OptType::Bool:
if (current_opt.value.b != opt.value.b) return false;
continue;
case Option::OptType::Int:
if (current_opt.value.i != opt.value.i) return false;
continue;
case Option::OptType::UInt:
if (current_opt.value.u != opt.value.u) return false;
continue;
case Option::OptType::String:
if (current_opt.value.s != opt.value.s) return false;
continue;
default:; // NOLINT(whitespace/semicolon)
}
}
return true;
}
void LLVMTargetInfo::GetOptionValue(LLVMTargetInfo::Option* opt) const {
llvm::StringMap<llvm::cl::Option*>& options = llvm::cl::getRegisteredOptions();
llvm::cl::Option* base_op = options[opt->name];
if (opt->type == Option::OptType::Bool) {
auto* bool_op = static_cast<llvm::cl::opt<bool>*>(base_op);
opt->value.b = bool_op->getValue();
} else if (opt->type == Option::OptType::Int) {
auto* int_op = static_cast<llvm::cl::opt<int>*>(base_op);
opt->value.i = int_op->getValue();
} else if (opt->type == Option::OptType::UInt) {
auto* uint_op = static_cast<llvm::cl::opt<unsigned>*>(base_op);
opt->value.u = uint_op->getValue();
} else if (opt->type == Option::OptType::String) {
auto* str_op = static_cast<llvm::cl::opt<std::string>*>(base_op);
opt->value.s = str_op->getValue();
} else {
opt->type = Option::OptType::Invalid;
}
}
const Array<String> LLVMTargetInfo::GetAllLLVMTargets() const {
Array<String> llvm_targets;
// iterate all archtypes
for (auto a = llvm::Triple::ArchType(llvm::Triple::ArchType::UnknownArch + 1);
a < llvm::Triple::ArchType::LastArchType; a = llvm::Triple::ArchType(a + 1)) {
std::string target_name = llvm::Triple::getArchTypeName(a).str();
// get valid target
if (CreateLLVMTargetInstance(target_name + "--", true)) {
llvm_targets.push_back(target_name);
}
}
return llvm_targets;
}
const Array<String> LLVMTargetInfo::GetAllLLVMTargetArches() const {
Array<String> cpu_arches;
// get the subtarget info module
const auto MCInfo = GetLLVMSubtargetInfo(triple_, "", "");
if (!MCInfo) {
return cpu_arches;
}
// get all arches
llvm::ArrayRef<llvm::SubtargetSubTypeKV> llvm_arches =
#if TVM_LLVM_VERSION < 170
llvm::archViewer(*(llvm::MCSubtargetInfo*)MCInfo);
#else
MCInfo->getAllProcessorDescriptions();
#endif
for (const auto& arch : llvm_arches) {
cpu_arches.push_back(arch.Key);
}
return cpu_arches;
}
const Array<String> LLVMTargetInfo::GetAllLLVMCpuFeatures() const {
std::string feats = "";
for (const auto& attr : attrs_) {
feats += feats.empty() ? attr : ("," + attr);
}
// get the subtarget info module
const auto MCInfo = GetLLVMSubtargetInfo(triple_, cpu_.c_str(), feats);
// get all features for CPU
llvm::ArrayRef<llvm::SubtargetFeatureKV> llvm_features =
#if TVM_LLVM_VERSION < 180
llvm::featViewer(*(llvm::MCSubtargetInfo*)MCInfo);
#else
MCInfo->getAllProcessorFeatures();
#endif
Array<String> cpu_features;
for (const auto& feat : llvm_features) {
if (MCInfo->checkFeatures("+" + std::string(feat.Key))) {
cpu_features.push_back(feat.Key);
}
}
return cpu_features;
}
const bool LLVMTargetInfo::TargetHasCPUFeature(const std::string& feature) const {
// lookup features for `-mcpu`
auto feats = GetAllLLVMCpuFeatures();
bool has_feature =
std::any_of(feats.begin(), feats.end(), [&](const auto& var) { return var == feature; });
return has_feature;
}
// LLVMTarget
bool LLVMTarget::modified_llvm_state_ = false;
LLVMTarget::LLVMTarget(LLVMInstance& instance, const LLVMTargetInfo& target_info)
: LLVMTargetInfo(target_info), instance_(instance), ctx_(instance.GetContext()) {
// Populate the list of saved options with the current values.
for (const Option& opt : GetCommandLineOptions()) {
GetOptionValue(&saved_llvm_options_.emplace_back(opt));
}
if (modified_llvm_state_) {
ICHECK(!ApplyLLVMOptions(true));
} else {
modified_llvm_state_ = ApplyLLVMOptions(true);
}
}
LLVMTarget::LLVMTarget(LLVMInstance& instance, const Target& target)
: LLVMTarget(instance, LLVMTargetInfo(instance, target)) {}
LLVMTarget::LLVMTarget(LLVMInstance& scope, const std::string& target_str)
: LLVMTarget(scope, Target(target_str)) {}
LLVMTarget::~LLVMTarget() {
// Revert all applied LLVM options.
if (ApplyLLVMOptions(false)) {
modified_llvm_state_ = false;
}
}
llvm::LLVMContext* LLVMTarget::GetContext() const {
ICHECK(!ctx_.expired()) << "LLVM scope has been deleted";
return ctx_.lock().get();
}
std::string LLVMTarget::GetTargetMetadata(const llvm::Module& module) {
if (llvm::Metadata* tvm_target = module.getModuleFlag("tvm_target")) {
auto* mdstr = llvm::cast<llvm::MDString>(tvm_target);
llvm::StringRef meta = mdstr->getString();
if (meta.startswith("llvm")) {
return meta.str();
}
}
return "llvm -mtriple " + module.getTargetTriple();
}
void LLVMTarget::SetTargetMetadata(llvm::Module* module) const {
module->addModuleFlag(llvm::Module::Warning, "tvm_target",
llvm::MDString::get(*GetContext(), str()));
}
bool LLVMTarget::ApplyLLVMOptions(bool apply_otherwise_revert, bool dry_run) {
llvm::StringMap<llvm::cl::Option*>& options = llvm::cl::getRegisteredOptions();
bool changed = false;
#define HANDLE_OPTION_VALUE(option, new_val, saved_val) \
do { \
auto current = (option)->getValue(); \
auto replacement = apply_otherwise_revert ? (new_val) : (saved_val); \
if (current != replacement) { \
changed = true; \
if (!dry_run) { \
(option)->setValue(replacement); \
} \
} \
} while (false);
const auto& new_options = GetCommandLineOptions();
for (size_t i = 0, e = saved_llvm_options_.size(); i != e; ++i) {
const Option& new_opt = new_options[i];
const Option& saved_opt = saved_llvm_options_[i];
llvm::cl::Option* base_op = options[new_opt.name];
if (new_opt.type == Option::OptType::Bool) {
auto* bool_op = static_cast<llvm::cl::opt<bool>*>(base_op);
HANDLE_OPTION_VALUE(bool_op, new_opt.value.b, saved_opt.value.b);
} else if (new_opt.type == Option::OptType::Int) {
auto* int_op = static_cast<llvm::cl::opt<int>*>(base_op);
HANDLE_OPTION_VALUE(int_op, new_opt.value.i, saved_opt.value.i);
} else if (new_opt.type == Option::OptType::UInt) {
auto* uint_op = static_cast<llvm::cl::opt<unsigned>*>(base_op);
HANDLE_OPTION_VALUE(uint_op, new_opt.value.u, saved_opt.value.u);
} else if (new_opt.type == Option::OptType::String) {
auto* str_op = static_cast<llvm::cl::opt<std::string>*>(base_op);
HANDLE_OPTION_VALUE(str_op, new_opt.value.s, saved_opt.value.s);
} else {
LOG(FATAL) << "unexpected type in option " << new_opt;
}
if (dry_run && changed) {
return true;
}
}
#undef HANDLE_OPTION_VALUE
return changed;
}
} // namespace codegen
} // namespace tvm
#endif // TVM_LLVM_VERSION