forked from tenstorrent/whisper
-
Notifications
You must be signed in to change notification settings - Fork 0
/
HartConfig.cpp
2702 lines (2324 loc) · 70.7 KB
/
HartConfig.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
// Copyright 2020 Western Digital Corporation or its affiliates.
//
// Licensed 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.
#include <nlohmann/json.hpp>
#include <charconv>
#include <fstream>
#include <iostream>
#include <unordered_map>
#include <unordered_set>
#include <bit>
#include "HartConfig.hpp"
#include "System.hpp"
#include "Core.hpp"
#include "Hart.hpp"
using namespace WdRiscv;
constexpr bool
isPowerOf2(uint64_t x)
{
return x != 0 and (x & (x-1)) == 0;
}
HartConfig::HartConfig()
: config_(std::make_unique<nlohmann::json>())
{
}
HartConfig::~HartConfig() = default;
bool
HartConfig::loadConfigFile(const std::string& filePath)
{
std::ifstream ifs(filePath);
if (not ifs.good())
{
std::cerr << "Failed to open config file '" << filePath
<< "' for input.\n";
return false;
}
try
{
// Use json::parse rather than operator>> to allow comments to be ignored
*config_ = nlohmann::json::parse(ifs, nullptr /* callback */, true /* allow_exceptions */, true /* ignore_comments */);
}
catch (std::exception& e)
{
std::cerr << e.what() << "\n";
return false;
}
catch (...)
{
std::cerr << "Caught unknown exception while parsing "
<< " config file '" << filePath << "'\n";
return false;
}
return true;
}
namespace WdRiscv
{
/// Convert given json entry to an unsigned integer value honoring
/// hexadecimal prefix (0x) if any. Return true on succes and false
/// if given entry does not represent an integer.
template <typename URV>
bool
getJsonUnsigned(std::string_view tag, const nlohmann::json& js, URV& value)
{
value = 0;
if (js.is_number())
{
value = js.get<URV>();
return true;
}
if (js.is_string())
{
char* end = nullptr;
std::string_view str = js.get<std::string_view>();
uint64_t u64 = strtoull(str.data(), &end, 0);
if (end and *end)
{
std::cerr << "Invalid config file unsigned value for '" << tag << "': "
<< str << '\n';
return false;
}
value = static_cast<URV>(u64);
if (value != u64)
{
std::cerr << "Overflow in config file value for '" << tag << "': "
<< str << '\n';
return false;
}
return true;
}
std::cerr << "Config file entry '" << tag << "' must contain a number\n";
return false;
}
/// Convert given json array value to a vector of unsigned integers
/// honoring any hexadecimal prefix (0x) if any. Return true on
/// sucess an false on failure.
template <typename URV>
bool
getJsonUnsignedVec(std::string_view tag, const nlohmann::json& js,
std::vector<URV>& vec)
{
vec.clear();
if (not js.is_array())
{
std::cerr << "Invalid config file value for '" << tag << "'"
<< " -- expecting array of numbers\n";
return false;
}
unsigned errors = 0;
for (const auto& item :js)
{
if (item.is_number())
vec.push_back(item.get<unsigned>());
else if (item.is_string())
{
char* end = nullptr;
std::string_view str = item.get<std::string_view>();
uint64_t u64 = strtoull(str.data(), &end, 0);
if (end and *end)
{
std::cerr << "Invalid config file value for '" << tag << "': "
<< str << '\n';
errors++;
continue;
}
URV val = static_cast<URV>(u64);
if (val != u64)
{
std::cerr << "Overflow in config file value for '" << tag << "': "
<< str << '\n';
errors++;
continue;
}
vec.push_back(val);
}
else
{
std::cerr << "Invalid config file value for '" << tag << "'"
<< " -- expecting array of number\n";
errors++;
}
}
return errors == 0;
}
/// Convert given json entry to a boolean value. Return ture on
/// success and false on failure.
bool
getJsonBoolean(std::string_view tag, const nlohmann::json& js, bool& value)
{
value = false;
if (js.is_boolean())
{
value = js.get<bool>();
return true;
}
if (js.is_number())
{
value = js.get<unsigned>() != 0;
return true;
}
if (js.is_string())
{
std::string_view str = js.get<std::string_view>();
if (str == "0" or str == "false" or str == "False")
value = false;
else if (str == "1" or str == "true" or str == "True")
value = true;
else
{
std::cerr << "Invalid config file boolean value for '" << tag << "': "
<< str << '\n';
return false;
}
return true;
}
std::cerr << "Config file entry '" << tag << "' must contain a bool\n";
return false;
}
}
template <typename URV>
static
bool
applyCsrConfig(Hart<URV>& hart, std::string_view nm, const nlohmann::json& conf, bool verbose)
{
unsigned errors = 0;
URV reset = 0, mask = 0, pokeMask = 0;
bool exists = true, shared = false;
std::string name(nm);
if (name == "dscratch")
name += "0";
Csr<URV>* csr = hart.findCsr(name);
if (csr)
{
reset = csr->getResetValue();
mask = csr->getWriteMask();
pokeMask = csr->getPokeMask();
}
if (conf.contains("reset"))
getJsonUnsigned(name + ".reset", conf.at("reset"), reset) or errors++;
if (conf.contains("mask"))
{
if (not getJsonUnsigned(name + ".mask", conf.at("mask"), mask))
errors++;
// If defining a non-standard CSR (as popposed to configuring an
// existing CSR) then default the poke-mask to the write-mask.
if (not csr)
pokeMask = mask;
}
if (conf.contains("poke_mask"))
getJsonUnsigned(name + ".poke_mask", conf.at("poke_mask"), pokeMask) or errors++;
if (conf.contains("exists"))
getJsonBoolean(name + ".exists", conf.at("exists"), exists) or errors++;
if (conf.contains("shared"))
getJsonBoolean(name + ".shared", conf.at("shared"), shared) or errors++;
// If number present and csr is not defined, then define a new
// CSR; otherwise, configure.
if (conf.contains("number"))
{
unsigned number = 0;
if (not getJsonUnsigned<unsigned>(name + ".number", conf.at("number"), number))
errors++;
else
{
if (csr)
{
if (csr->getNumber() != CsrNumber(number))
{
std::cerr << "Invalid config file entry for CSR "
<< name << ": Number (0x" << std::hex << number
<< ") does not match that of previous definition ("
<< "0x" << unsigned(csr->getNumber())
<< ")\n" << std::dec;
return false;
}
// If number matches we configure below
}
else if (hart.defineCsr(name, CsrNumber(number), exists, reset, mask, pokeMask))
{
csr = hart.findCsr(name);
assert(csr);
}
else
{
std::cerr << "Invalid config file CSR definition with name "
<< name << " and number 0x" << std::hex << number
<< ": Number already in use\n" << std::dec;
return false;
}
}
}
if (not csr)
{
std::cerr << "A CSR number must be provided in configuration of non-standard CSR "
<< name << '\n';
return false;
}
bool exists0 = csr->isImplemented();
bool shared0 = csr->isShared();
URV reset0 = csr->getResetValue(), mask0 = csr->getWriteMask();
URV pokeMask0 = csr->getPokeMask();
if (name == "mhartid" or name == "vlenb")
{
std::cerr << "CSR " << name << " cannot be configured.\n";
return true;
}
if (name == "sstatus")
{
std::cerr << "CSR sstatus is a shadow of mstatus and cannot be configured.\n";
return true;
}
if (errors)
return false;
if (not hart.configCsrByUser(name, exists, reset, mask, pokeMask, shared))
{
std::cerr << "Invalid CSR (" << name << ") in config file.\n";
return false;
}
if ((mask & pokeMask) != mask and hart.sysHartIndex() == 0)
{
std::cerr << "Warning: For CSR " << name << " poke mask (0x" << std::hex << pokeMask
<< ") is not a superset of write\n mask (0x" << mask << std::dec << ")."
<< " Only bits set in both masks will be writable by CSR instructions.\n";
}
if (name == "misa")
{
// If an extension bit is writable, it should reset to 1.
URV extBits = (URV(1) << 26) - 1;
URV writeable = extBits & mask, writeableReset = extBits & mask & reset;
if (writeable != writeableReset and hart.sysHartIndex() == 0)
std::cerr << "Warning: Reset value of MISA should be 0x"
<< std::hex << (reset | writeable) << std::dec
<< " to be compatible with write mask.\n";
if ((writeable & (URV(1) << ('E' - 'A'))) and hart.sysHartIndex() == 0)
std::cerr << "Warning: Bit E of MISA cannot be writebale.\n";
if ((reset & (1 << ('S' - 'A'))) and not (reset & (1 << ('U' - 'A'))))
{
std::cerr << "Invalid MISA in config file: cannot have S=1 and U=0.\n";
return false;
}
}
if (verbose)
{
if (exists0 != exists or reset0 != reset or mask0 != mask or pokeMask0 != pokeMask)
{
std::cerr << "Configuration of CSR (" << name <<
") changed in config file:\n";
if (exists0 != exists)
std::cerr << " implemented: " << exists0 << " to "
<< exists << '\n';
if (shared0 != shared)
std::cerr << " shared: " << shared0 << " to "
<< shared << '\n';
if (reset0 != reset)
std::cerr << " reset: 0x" << std::hex << reset0
<< " to 0x" << reset << '\n' << std::dec;
if (mask0 != mask)
std::cerr << " mask: 0x" << std::hex << mask0
<< " to 0x" << mask << '\n' << std::dec;
if (pokeMask0 != pokeMask)
std::cerr << " poke_mask: " << std::hex << pokeMask0
<< " to 0x" << pokeMask << '\n' << std::dec;
}
}
return true;
}
template <typename URV>
static
bool
applyCsrConfig(Hart<URV>& hart, const nlohmann::json& config, bool verbose)
{
if (not config.contains("csr"))
return true; // Nothing to apply
const auto& csrs = config.at("csr");
if (not csrs.is_object())
{
std::cerr << "Invalid csr entry in config file (expecting an object)\n";
return false;
}
unsigned errors = 0;
for (auto it = csrs.begin(); it != csrs.end(); ++it)
{
std::string_view csrName = it.key();
const auto& conf = it.value();
std::string_view tag = "range";
if (not conf.contains(tag))
{
applyCsrConfig(hart, csrName, conf, verbose) or errors++;
continue;
}
std::vector<unsigned> range;
if (not getJsonUnsignedVec(util::join("", "csr.", tag, ".range"), conf.at(tag), range)
or range.size() != 2 or range.at(0) > range.at(1))
{
std::cerr << "Invalid range in CSR '" << csrName << "': " << conf.at(tag) << '\n';
errors++;
continue;
}
if (range.at(1) - range.at(0) > 256)
{
std::cerr << "Invalid range in CSR '" << csrName << "': " << conf.at(tag)
<< ": Range size greater than 256\n";
errors++;
continue;
}
for (unsigned n = range.at(0); n <= range.at(1); ++n)
{
std::string strand = util::join("", csrName, std::to_string(n));
if (not applyCsrConfig(hart, strand, conf, verbose))
{
errors++;
break;
}
}
}
return errors == 0;
}
template <typename URV>
static
bool
applyTriggerConfig(Hart<URV>& hart, const nlohmann::json& config)
{
if (not config.contains("triggers"))
return true; // Nothing to apply
const auto& triggers = config.at("triggers");
if (not triggers.is_array())
{
std::cerr << "Invalid triggers entry in config file (expecting an array)\n";
return false;
}
unsigned errors = 0;
unsigned ix = 0;
for (auto it = triggers.begin(); it != triggers.end(); ++it, ++ix)
{
const auto& trig = *it;
std::string name = std::string("trigger") + std::to_string(ix);
if (not trig.is_object())
{
std::cerr << "Invalid trigger in config file triggers array "
<< "(expecting an object at index " << ix << ")\n";
++errors;
break;
}
bool ok = true;
for (const auto& tag : {"reset", "mask", "poke_mask"})
if (not trig.contains(tag))
{
std::cerr << "Trigger " << name << " has no '" << tag
<< "' entry in config file\n";
ok = false;
}
if (not ok)
{
errors++;
continue;
}
std::vector<uint64_t> resets, masks, pokeMasks;
ok = (getJsonUnsignedVec(name + ".reset", trig.at("reset"), resets) and
getJsonUnsignedVec(name + ".mask", trig.at("mask"), masks) and
getJsonUnsignedVec(name + ".poke_mask", trig.at("poke_mask"), pokeMasks));
if (not ok)
{
errors++;
continue;
}
// Each trigger has up to 5 components: tdata1, tdata2, tdata3, tinfo, tcontrol
size_t maxSize = std::max(resets.size(), std::max(masks.size(), pokeMasks.size()));
if (maxSize > 5)
std::cerr << "Trigger " << name << ": Unreasonable item count (" << maxSize
<< ") for 'reset/mask/poke_mask' field in config file. "
<< " Expecting no more than to 5. Extra fields ignored.\n";
if (resets.size() != maxSize or masks.size() != maxSize or pokeMasks.size() != maxSize)
{
std::cerr << "Trigger " << name << ": Error: reset/mask/poke_mask fields must have "
<< " same number of entries.\n";
errors++;
continue;
}
if (not hart.configTrigger(ix, resets, masks, pokeMasks))
{
std::cerr << "Failed to configure trigger " << std::dec << ix << '\n';
++errors;
}
}
return errors == 0;
}
template <typename URV>
static
bool
applyPerfEventMap(Hart<URV>& hart, const nlohmann::json& config)
{
constexpr std::string_view tag = "mmode_perf_event_map";
if (not config.contains(tag))
return true;
const auto& perfMap = config.at(tag);
if (not perfMap.is_object())
{
std::cerr << "Invalid " << tag << " entry in config file (expecting an object)\n";
return false;
}
std::unordered_set<URV> eventNumbers;
unsigned errors = 0;
for (auto it = perfMap.begin(); it != perfMap.end(); ++it)
{
std::string_view eventName = it.key();
const auto& valObj = it.value();
std::string path = util::join(".", tag, eventName);
URV value = 0;
if (not getJsonUnsigned(path, valObj, value))
{
errors++;
continue;
}
EventNumber eventId = EventNumber::None;
if (not PerfRegs::findEvent(eventName, eventId))
{
std::cerr << "No such performance event: " << eventName << '\n';
errors++;
continue;
}
if (eventNumbers.contains(value))
{
std::cerr << "Event number " << value << " associaged with more than one event in mmode_perf_event_map in config file.\n";
errors++;
}
hart.configEventNumber(value, eventId);
eventNumbers.insert(value);
}
return errors == 0;
}
template <typename URV>
static
bool
applyPerfEvents(Hart<URV>& hart, const nlohmann::json& config,
bool userMode, bool cof, bool /*verbose*/)
{
unsigned errors = 0;
std::string_view tag = "num_mmode_perf_regs";
if (config.contains(tag))
{
unsigned count = 0;
if (not getJsonUnsigned<unsigned>(tag, config.at(tag), count))
errors++;
else
{
if (not hart.configMachineModePerfCounters(count, cof))
errors++;
if (userMode)
if (not hart.configUserModePerfCounters(count))
errors++;
}
}
unsigned maxPerfId = 0;
tag = "max_mmode_perf_event";
if (config.contains(tag))
{
if (not getJsonUnsigned<unsigned>(tag, config.at(tag), maxPerfId))
errors++;
else
{
unsigned limit = 16*1024;
if (maxPerfId > limit)
{
std::cerr << "Config file max_mmode_perf_event too large -- Using "
<< limit << '\n';
maxPerfId = limit;
}
hart.configMachineModeMaxPerfEvent(maxPerfId);
}
}
tag = "mmode_perf_events";
if (config.contains(tag))
{
std::vector<unsigned> eventsVec;
const auto& events = config.at(tag);
if (not events.is_array())
{
std::cerr << "Invalid mmode_perf_events entry in config file (expecting an array)\n";
errors++;
}
else
{
unsigned ix = 0;
for (auto it = events.begin(); it != events.end(); ++it, ++ix)
{
const auto& event = *it;
std::string elemTag = util::join("", tag, "element ", std::to_string(ix));
unsigned eventId = 0;
if (not getJsonUnsigned<unsigned>(elemTag, event, eventId))
errors++;
else
eventsVec.push_back(eventId);
}
}
hart.configPerfEvents(eventsVec);
}
if (not applyPerfEventMap(hart, config))
errors++;
return errors == 0;
}
// Min SEW per LMUL is allowed by the spec for m1, m2, m4, and m8.
bool
processMinBytesPerLmul(const nlohmann::json& jsonMap, unsigned minBytes, unsigned maxBytes,
std::unordered_map<GroupMultiplier, unsigned>& bytesPerLmul)
{
if (not jsonMap.is_object())
{
std::cerr << "Invalid min_bytes_per_lmul entry in config file (expecting an object)\n";
return false;
}
for (auto it = jsonMap.begin(); it != jsonMap.end(); it++)
{
GroupMultiplier group;
std::string_view lmul = it.key();
const unsigned mewb = it.value(); // min element width in bytes
if (not VecRegs::to_lmul(lmul, group))
{
std::cerr << "Invalid lmul setting in min_bytes_per_lmul: " << lmul << '\n';
return false;
}
if (group > GroupMultiplier::Eight)
{
std::cerr << "Invalid lmul setting in min_bytes_per_lmul: " << lmul
<< " (expecting non-fractional group)\n";
return false;
}
if (mewb < minBytes or mewb > maxBytes)
{
std::cerr << "Error: Config file min_bytes_per_lmul ("
<< mewb << ") must be in the range [" << minBytes
<< "," << maxBytes << "]\n";
return false;
}
if (not isPowerOf2(mewb))
{
std::cerr << "Error: config file min_bytes_per_lmul ("
<< mewb << ") is not a power of 2\n";
return false;
}
bytesPerLmul[group] = mewb;
}
return true;
}
// Maximum SEW per LMUL is allowed by the spec for mf8, mf4, and mf2.
bool
processMaxBytesPerLmul(const nlohmann::json& jsonMap, unsigned minBytes, unsigned maxBytes,
std::unordered_map<GroupMultiplier, unsigned>& bytesPerLmul)
{
if (not jsonMap.is_object())
{
std::cerr << "Invalid max_bytes_per_lmul tag in config file (expecting an object)\n";
return false;
}
for (auto it = jsonMap.begin(); it != jsonMap.end(); it++)
{
GroupMultiplier group;
std::string_view lmul = it.key();
const unsigned mewb = it.value(); // max element width in bytes
if (not VecRegs::to_lmul(lmul, group))
{
std::cerr << "Invalid lmul setting in max_bytes_per_lmul: " << lmul << '\n';
return false;
}
if (group < GroupMultiplier::Eighth)
{
std::cerr << "Invalid lmul setting in max_bytes_per_lmul: " << lmul
<< " (expecting fractional group)\n";
return false;
}
if (mewb < minBytes or mewb > maxBytes)
{
std::cerr << "Error: Config file max_bytes_per_lmul ("
<< mewb << ") must be in the range [" << minBytes
<< "," << maxBytes << "]\n";
return false;
}
if (not isPowerOf2(mewb))
{
std::cerr << "Error: config file max_bytes_per_lmul ("
<< mewb << ") is not a power of 2\n";
return false;
}
bytesPerLmul[group] = mewb;
}
return true;
}
template <typename URV>
static
bool
applyVectorConfig(Hart<URV>& hart, const nlohmann::json& config)
{
using namespace std::string_view_literals;
if (not config.contains("vector"))
return true; // Nothing to apply
unsigned errors = 0;
const auto& vconf = config.at("vector");
unsigned bytesPerVec = 0;
std::string_view tag = "bytes_per_vec";
if (not vconf.contains(tag))
{
std::cerr << "Error: Missing " << tag << " tag in vector section of config file\n";
errors++;
}
else
{
if (not getJsonUnsigned(tag, vconf.at(tag), bytesPerVec))
errors++;
else if (bytesPerVec == 0 or bytesPerVec > 4096)
{
std::cerr << "Error: Invalid config file bytes_per_vec number: "
<< bytesPerVec << '\n';
errors++;
}
else if (not isPowerOf2(bytesPerVec))
{
std::cerr << "Error: Config file bytes_per_vec ("
<< bytesPerVec << ") is not a power of 2\n";
errors++;
}
}
std::vector<unsigned> bytesPerElem = { 1, 1 };
static constexpr auto tags = std::array{ "min_bytes_per_elem"sv, "max_bytes_per_elem"sv };
for (size_t ix = 0; ix < tags.size(); ++ix)
{
unsigned bytes = 0;
tag = tags.at(ix);
if (not vconf.contains(tag))
{
if (ix > 0)
{
std::cerr << "Error: Missing " << tag
<< " tag in vector section of config file\n";
errors++;
}
continue;
}
if (not getJsonUnsigned(tag, vconf.at(tag), bytes))
errors++;
else if (bytes == 0 or bytes > bytesPerVec)
{
std::cerr << "Error: Invalid config file " << tag << " number: "
<< bytes << '\n';
errors++;
}
else
{
if (not isPowerOf2(bytes))
{
std::cerr << "Error: Config file " << tag << " ("
<< bytes << ") is not a power of 2\n";
errors++;
}
else
bytesPerElem.at(ix) = bytes;
}
}
std::unordered_map<GroupMultiplier, unsigned> minBytesPerLmul;
tag = "min_sew_per_lmul";
if (vconf.contains(tag))
{
std::cerr << "Tag min_sew_per_lmul is deprecated: Use min_bytes_per_lmul\n";
if (not processMinBytesPerLmul(vconf.at(tag), bytesPerElem.at(0), bytesPerElem.at(1),
minBytesPerLmul))
errors++;
}
tag = "min_bytes_per_lmul";
if (vconf.contains(tag))
{
if (not processMinBytesPerLmul(vconf.at(tag), bytesPerElem.at(0), bytesPerElem.at(1),
minBytesPerLmul))
errors++;
}
std::unordered_map<GroupMultiplier, unsigned> maxBytesPerLmul;
tag = "max_sew_per_lmul";
if (vconf.contains(tag))
{
std::cerr << "Tag max_sew_per_lmul is deprecated: Use max_bytes_per_lmul\n";
if (not processMaxBytesPerLmul(vconf.at(tag), bytesPerElem.at(0), bytesPerElem.at(1),
maxBytesPerLmul))
errors++;
}
tag = "max_bytes_per_lmul";
if (vconf.contains(tag))
{
if (not processMaxBytesPerLmul(vconf.at(tag), bytesPerElem.at(0), bytesPerElem.at(1),
maxBytesPerLmul))
errors++;
}
if (errors == 0)
hart.configVector(bytesPerVec, bytesPerElem.at(0), bytesPerElem.at(1), &minBytesPerLmul,
&maxBytesPerLmul);
tag = "mask_agnostic_policy";
if (vconf.contains(tag))
{
auto& item = vconf.at(tag);
if (not item.is_string())
{
std::cerr << "Error: Configuration file tag vector.mask_agnostic_policy must have a string value\n";
errors++;
}
else
{
std::string val = item.get<std::string>();
if (val == "ones")
hart.configMaskAgnosticAllOnes(true);
else if (val == "undisturb")
hart.configMaskAgnosticAllOnes(false);
else
{
std::cerr << "Error: Configuration file tag vector.mask_agnostic_policy must be 'ones' or 'undisturb'\n";
errors++;
}
}
}
tag = "tail_agnostic_policy";
if (vconf.contains(tag))
{
auto& item = vconf.at(tag);
if (not item.is_string())
{
std::cerr << "Error: Configuration file tag vector.tail_agnostic_policy must have a string value\n";
errors++;
}
else
{
std::string val = item.get<std::string>();
if (val == "ones")
hart.configTailAgnosticAllOnes(true);
else if (val == "undisturb")
hart.configTailAgnosticAllOnes(false);
else
{
std::cerr << "Error: Configuration file tag vector.tail_agnostic_policy must be 'ones' or 'undisturb'\n";
errors++;
}
}
}
tag = "trap_non_zero_vstart";
if (vconf.contains(tag))
{
bool flag = false;
if (not getJsonBoolean(tag, vconf.at(tag), flag))
errors++;
else
hart.enableTrapNonZeroVstart(flag);
}
if (errors == 0)
hart.configVector(bytesPerVec, bytesPerElem.at(0), bytesPerElem.at(1), &minBytesPerLmul,
&maxBytesPerLmul);
tag = "update_whole_mask";
if (vconf.contains(tag))
{
bool flag = false;
if (not getJsonBoolean(tag, vconf.at(tag), flag))
errors++;
else
hart.configVectorUpdateWholeMask(flag);
}
tag = "trap_invalid_vtype";
if (vconf.contains(tag))
{
bool flag = false;
if (not getJsonBoolean(tag, vconf.at(tag), flag))
errors++;
else
hart.configVectorTrapVtype(flag);
}
tag = "tt_fp_usum_tree_reduction";
if (vconf.contains(tag))
{
bool flag = false;
if (not getJsonBoolean(tag, vconf.at(tag), flag))
errors++;
else
hart.configVectorFpUnorderedSumRed(flag);
}
tag = "legalize_vsetvl_avl";
if (vconf.contains(tag))
{
bool flag = false;
if (not getJsonBoolean(tag, vconf.at(tag), flag))
errors++;
else
hart.configVectorLegalizeVsetvlAvl(flag);
}
tag = "legalize_vsetvli_avl";
if (vconf.contains(tag))
{
bool flag = false;
if (not getJsonBoolean(tag, vconf.at(tag), flag))
errors++;
else
hart.configVectorLegalizeVsetvliAvl(flag);
}
tag = "legalize_for_egs";
if (vconf.contains(tag))
{
bool flag = false;
if (not getJsonBoolean(tag, vconf.at(tag), flag))
errors++;
else
hart.configVectorLegalizeForEgs(flag);
}
return errors == 0;
}