-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Expand file tree
/
Copy pathCodeGen_Hexagon.cpp
More file actions
2300 lines (2040 loc) · 97.6 KB
/
Copy pathCodeGen_Hexagon.cpp
File metadata and controls
2300 lines (2040 loc) · 97.6 KB
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 <sstream>
#include <utility>
#include "AlignLoads.h"
#include "CSE.h"
#include "CodeGen_Internal.h"
#include "CodeGen_Posix.h"
#include "Debug.h"
#include "HexagonOptimize.h"
#include "IREquality.h"
#include "IRMutator.h"
#include "IROperator.h"
#include "IRPrinter.h"
#include "LLVM_Headers.h"
#include "LoopCarry.h"
#include "Simplify.h"
#include "Substitute.h"
#include "Target.h"
#include "Util.h"
namespace Halide {
namespace Internal {
using std::string;
using std::vector;
using namespace llvm;
#ifdef WITH_HEXAGON
namespace {
/** A code generator that emits Hexagon code from a given Halide stmt. */
class CodeGen_Hexagon : public CodeGen_Posix {
public:
/** Create a Hexagon code generator for the given Hexagon target. */
CodeGen_Hexagon(const Target &);
protected:
void compile_func(const LoweredFunc &f,
const std::string &simple_name, const std::string &extern_name) override;
void init_module() override;
std::string mcpu_target() const override;
std::string mcpu_tune() const override;
std::string mattrs() const override;
int isa_version;
bool use_soft_float_abi() const override;
int native_vector_bits() const override;
llvm::Function *define_hvx_intrinsic(llvm::Function *intrin, Type ret_ty,
const std::string &name,
std::vector<Type> arg_types,
int flags);
int is_hvx_v65_or_later() const {
return (isa_version >= 65);
}
using CodeGen_Posix::visit;
/** Nodes for which we want to emit specific hexagon intrinsics */
///@{
void visit(const Max *) override;
void visit(const Min *) override;
void visit(const Call *) override;
void visit(const Mul *) override;
void visit(const Select *) override;
void visit(const Allocate *) override;
///@}
/** Call an LLVM intrinsic, potentially casting the operands to
* match the type of the function. */
///@{
llvm::Value *call_intrin_cast(llvm::Type *ret_ty, llvm::Function *F,
std::vector<llvm::Value *> Ops);
llvm::Value *call_intrin_cast(llvm::Type *ret_ty, int id,
std::vector<llvm::Value *> Ops);
///@}
/** Define overloads of CodeGen_LLVM::call_intrin that determine
* the intrin_lanes from the type, and allows the function to
* return null if the maybe option is true and the intrinsic is
* not found. */
///@{
llvm::Value *call_intrin(Type t, const std::string &name,
std::vector<Expr>, bool maybe = false);
llvm::Value *call_intrin(llvm::Type *t, const std::string &name,
std::vector<llvm::Value *>, bool maybe = false);
///@}
/** Override CodeGen_LLVM to use hexagon intrinics when possible. */
///@{
llvm::Value *interleave_vectors(const std::vector<llvm::Value *> &v) override;
llvm::Value *shuffle_vectors(llvm::Value *a, llvm::Value *b,
const std::vector<int> &indices) override;
using CodeGen_Posix::shuffle_vectors;
///@}
/** Generate a LUT lookup using vlut instructions. */
///@{
llvm::Value *vlut(llvm::Value *lut, llvm::Value *indices, int min_index = 0, int max_index = 1 << 30);
llvm::Value *vlut(llvm::Value *lut, const std::vector<int> &indices);
///@}
llvm::Value *vdelta(llvm::Value *lut, const std::vector<int> &indices);
/** Because HVX intrinsics operate on vectors of i32, using them
* requires a lot of extraneous bitcasts, which make it difficult
* to manipulate the IR. This function avoids generating redundant
* bitcasts. */
llvm::Value *create_bitcast(llvm::Value *v, llvm::Type *ty);
private:
/** Generates code for computing the size of an allocation from a
* list of its extents and its size. Fires a runtime assert
* (halide_error) if the size overflows 2^31 -1, the maximum
* positive number an int32_t can hold. */
llvm::Value *codegen_cache_allocation_size(const std::string &name, Type type, const std::vector<Expr> &extents, int padding);
/** Generate a LUT (8/16 bit, max_index < 256) lookup using vlut instructions. */
llvm::Value *vlut256(llvm::Value *lut, llvm::Value *indices, int min_index = 0, int max_index = 255);
/** Wrapper to create a vector populated with a constant value in each lane. */
Value *create_vector(llvm::Type *ty, int val);
};
CodeGen_Hexagon::CodeGen_Hexagon(const Target &t)
: CodeGen_Posix(t) {
if (target.has_feature(Halide::Target::HVX_v68)) {
isa_version = 68;
} else if (target.has_feature(Halide::Target::HVX_v66)) {
isa_version = 66;
} else if (target.has_feature(Halide::Target::HVX_v65)) {
isa_version = 65;
} else {
isa_version = 62;
}
user_assert(target.has_feature(Target::HVX))
<< "Creating a Codegen target for Hexagon without the hvx target feature.\n";
}
Stmt call_halide_qurt_hvx_lock(const Target &target) {
Expr hvx_lock =
Call::make(Int(32), "halide_qurt_hvx_lock", {}, Call::Extern);
string hvx_lock_result_name = unique_name("hvx_lock_result");
Expr hvx_lock_result_var = Variable::make(Int(32), hvx_lock_result_name);
Stmt check_hvx_lock = LetStmt::make(
hvx_lock_result_name, hvx_lock,
AssertStmt::make(EQ::make(hvx_lock_result_var, 0), hvx_lock_result_var));
return check_hvx_lock;
}
Stmt call_halide_qurt_hvx_unlock() {
Expr hvx_unlock =
Call::make(Int(32), "halide_qurt_hvx_unlock", {}, Call::Extern);
string hvx_unlock_result_name = unique_name("hvx_unlock_result");
Expr hvx_unlock_result_var = Variable::make(Int(32), hvx_unlock_result_name);
Stmt check_hvx_unlock =
LetStmt::make(hvx_unlock_result_name, hvx_unlock,
AssertStmt::make(EQ::make(hvx_unlock_result_var, 0),
hvx_unlock_result_var));
return check_hvx_unlock;
}
// Wrap the stmt in a call to qurt_hvx_lock, calling qurt_hvx_unlock
// as a destructor if successful.
Stmt acquire_hvx_context(Stmt stmt, const Target &target) {
// Modify the stmt to add a call to halide_qurt_hvx_lock, and
// register a destructor to call halide_qurt_hvx_unlock.
Stmt check_hvx_lock = call_halide_qurt_hvx_lock(target);
Expr dummy_obj = reinterpret(Handle(), cast<uint64_t>(1));
Expr hvx_unlock =
Call::make(Handle(), Call::register_destructor,
{Expr("halide_qurt_hvx_unlock_as_destructor"), dummy_obj},
Call::Intrinsic);
stmt = Block::make(Evaluate::make(hvx_unlock), stmt);
stmt = Block::make(check_hvx_lock, stmt);
return stmt;
}
bool is_dense_ramp(const Expr &x) {
const Ramp *r = x.as<Ramp>();
if (!r) {
return false;
}
return is_const_one(r->stride);
}
// In Hexagon, we assume that we can read one vector past the end of
// buffers. Using this assumption, this mutator replaces vector
// predicated dense loads with scalar predicated dense loads.
class SloppyUnpredicateLoadsAndStores : public IRMutator {
using IRMutator::visit;
// The first and last lanes of all monotonic vectors in scope
Scope<std::pair<Expr, Expr>> monotonic_vectors;
// If a vector monotonically increases or decreases across the
// lanes, return the first and last lane.
std::pair<Expr, Expr> get_extreme_lanes(const Expr &e) {
if (const Ramp *r = e.as<Ramp>()) {
return {r->base, r->base + r->stride * (r->lanes - 1)};
} else if (const Broadcast *b = e.as<Broadcast>()) {
return {b->value, b->value};
} else if (const LT *op = e.as<LT>()) {
if (!op->a.type().is_bool()) {
auto a = get_extreme_lanes(op->a);
auto b = get_extreme_lanes(op->b);
if (a.first.defined() && b.first.defined()) {
return {a.first < b.first, a.second < b.second};
}
}
} else if (const LE *op = e.as<LE>()) {
if (!op->a.type().is_bool()) {
auto a = get_extreme_lanes(op->a);
auto b = get_extreme_lanes(op->b);
if (a.first.defined() && b.first.defined()) {
return {a.first <= b.first, a.second <= b.second};
}
}
} else if (const Variable *op = e.as<Variable>()) {
if (const auto *p = monotonic_vectors.find(op->name)) {
return *p;
}
} else if (const Let *op = e.as<Let>()) {
auto v = get_extreme_lanes(op->value);
ScopedBinding<std::pair<Expr, Expr>> bind(v.first.defined(), monotonic_vectors, op->name, v);
return get_extreme_lanes(op->body);
}
return {Expr(), Expr()};
}
Expr visit(const Let *op) override {
auto v = get_extreme_lanes(op->value);
ScopedBinding<std::pair<Expr, Expr>> bind(op->value.type().is_vector() && v.first.defined(),
monotonic_vectors, op->name, v);
return IRMutator::visit(op);
}
Expr visit(const Load *op) override {
if (is_const_one(op->predicate)) {
// These are handled fine
return IRMutator::visit(op);
}
Expr predicate = mutate(op->predicate);
Expr index = mutate(op->index);
if (is_dense_ramp(index) || index.as<Broadcast>()) {
// Make the predicate into a scalar that is true if any of the lanes are
// true.
Expr condition;
// If the predicate is monotonic increasing or decreasing
// over the vector lanes, we can just check the last or
// first lane, respectively. We won't bother to
// distinguish between the two cases though, so we just or
// them both together.
auto v = get_extreme_lanes(predicate);
if (v.first.defined()) {
internal_assert(v.first.type() == Bool() &&
v.second.type() == Bool())
<< "The extreme lanes of a bool vector should be scalar bools\n";
condition = simplify(v.first || v.second);
} else {
// Take an OR over all lanes.
condition = VectorReduce::make(VectorReduce::Or, predicate, 1);
condition = simplify(condition);
}
Expr load = Load::make(op->type, op->name, index, op->image, op->param,
const_true(op->type.lanes()), op->alignment);
return Call::make(op->type, Call::if_then_else,
{condition, load}, Call::PureIntrinsic);
} else {
// It's a predicated vector gather. Just scalarize. We'd
// prefer to keep it in a loop, but that would require
// some sort of loop Expr. Another option would be
// introducing a set of runtime functions to do predicated
// loads.
Expr load = Load::make(op->type, op->name, index, op->image, op->param,
const_true(op->type.lanes()), op->alignment);
return Call::make(op->type, Call::if_then_else,
{predicate, load}, Call::PureIntrinsic);
}
}
Stmt visit(const Store *op) override {
if (is_const_one(op->predicate)) {
return IRMutator::visit(op);
}
Expr predicate = mutate(op->predicate);
Expr value = mutate(op->value);
Expr index = mutate(op->index);
int lanes = value.type().lanes();
if (const Broadcast *scalar_pred = predicate.as<Broadcast>()) {
Stmt unpredicated_store = Store::make(op->name, value, index, op->param, const_true(lanes), op->alignment);
return IfThenElse::make(scalar_pred->value, unpredicated_store);
}
if (predicate.same_as(op->predicate) && value.same_as(op->value) && index.same_as(op->index)) {
return op;
} else {
return Store::make(op->name, value, index, op->param, predicate, op->alignment);
}
}
};
Stmt sloppy_unpredicate_loads_and_stores(const Stmt &s) {
return SloppyUnpredicateLoadsAndStores().mutate(s);
}
class InjectHVXLocks : public IRMutator {
public:
InjectHVXLocks(const Target &t)
: target(t) {
uses_hvx_var = Variable::make(Bool(), "uses_hvx");
}
bool uses_hvx = false;
private:
Expr uses_hvx_var;
using IRMutator::visit;
// Primarily, we do two things when we encounter a parallel for loop.
// First, we check if the paralell for loop uses_hvx and accordingly
// acqure_hvx_context i.e. acquire and release HVX locks.
// Then we insert a conditional unlock before the for loop, let's call
// this the prolog, and a conditional lock after the for loop which
// we shall call the epilog. So the code for a parallel loop that uses
// hvx should look like so.
//
// if (uses_hvx_var) {
// halide_qurt_hvx_unlock();
// }
// parallel_for {
// halide_qurt_hvx_lock();
// ...
// ...
// halide_qurt_hvx_unlock();
// }
// if (uses_hvx_var) {
// halide_qurt_hvx_lock();
// }
//
// When we move up to the enclosing scope we substitute the value of uses_hvx
// into the IR that should convert the conditionals to constants.
Stmt visit(const For *op) override {
if (op->for_type == ForType::Parallel) {
bool old_uses_hvx = uses_hvx;
uses_hvx = false;
Stmt body = mutate(op->body);
Stmt s;
if (uses_hvx) {
body = acquire_hvx_context(body, target);
body = substitute("uses_hvx", true, body);
Stmt new_for = For::make(op->name, op->min, op->max, op->for_type,
op->partition_policy, op->device_api, body);
Stmt prolog =
IfThenElse::make(uses_hvx_var, call_halide_qurt_hvx_unlock());
Stmt epilog =
IfThenElse::make(uses_hvx_var, call_halide_qurt_hvx_lock(target));
s = Block::make({prolog, new_for, epilog});
debug(4) << "Wrapping prolog & epilog around par loop\n"
<< s << "\n";
} else {
// We do not substitute false for "uses_hvx" into the body as we do in
// the true case because we want to defer that to an enclosing scope.
// The logic is that in case this scope doesn't use_hvx (we are here in
// the else because of that) then an enclosing scope might. However,
// substituting false for "uses_hvx" at this stage will remove the
// prolog and epilog checks that will be needed as the enclosing scope
// uses hvx. This is exhibited by the following code structure
//
// for_par(z..) {//uses hvx
// for_par(y..) { // doesn't use hvx
// for_par(x..) { // uses hvx
// vector code
// }
// }
// vector code
// }
// If we substitute false in the else here, we'll get
// for_par(z.) {
// halide_qurt_hvx_lock();
// for_par(y..) {
// if (false) {
// halide_qurt_hvx_unlock(); // will get optimized away.
// }
// for_par(x..) {
// halide_qurt_hvx_lock(); // double lock. Not good.
// vector code
// halide_qurt_hvx_unlock();
// }
// if (false) {
// halide_qurt_hvx_lock();
// }
// }
// vector code
// halide_qurt_unlock
// }
s = For::make(op->name, op->min, op->max, op->for_type,
op->partition_policy, op->device_api, body);
}
uses_hvx = old_uses_hvx;
return s;
}
return IRMutator::visit(op);
}
Expr visit(const Variable *op) override {
uses_hvx = uses_hvx || op->type.is_vector();
return op;
}
Expr visit(const Ramp *op) override {
uses_hvx = uses_hvx || op->type.is_vector();
return op;
}
Expr visit(const Broadcast *op) override {
uses_hvx = uses_hvx || op->lanes > 1;
return op;
}
Expr visit(const Call *op) override {
uses_hvx = uses_hvx || op->type.is_vector();
if (op->name == "halide_do_par_for") {
// If we see a call to halide_do_par_for() at this point, it should mean that
// this statement was produced via HexagonOffload calling lower_parallel_tasks()
// explicitly; in this case, we won't see any parallel For statements, since they've
// all been transformed into closures already. To mirror the pattern above,
// we need to wrap the halide_do_par_for() call with an unlock/lock pair, but
// that's hard to do in Halide IR (we'd need to produce a Stmt to enforce the ordering,
// and the resulting Stmt can't easily be substituted for the Expr here). Rather than
// make fragile assumptions about the structure of the IR produced by lower_parallel_tasks(),
// we'll use a trick: we'll define a WEAK_INLINE function, _halide_hexagon_do_par_for,
// which simply encapsulates the unlock()/do_par_for()/lock() sequences, and swap out
// the call here. Since it is inlined, and since uses_hvx_var gets substituted at the end,
// we end up with LLVM IR that properly includes (or omits) the unlock/lock pair depending
// on the final value of uses_hvx_var in this scope.
internal_assert(op->call_type == Call::Extern);
internal_assert(op->args.size() == 4);
std::vector<Expr> args = op->args;
args.push_back(cast<int>(uses_hvx_var));
return Call::make(Int(32), "_halide_hexagon_do_par_for", args, Call::Extern);
}
return op;
}
Target target;
};
Stmt inject_hvx_lock_unlock(Stmt body, const Target &target) {
InjectHVXLocks i(target);
body = i.mutate(body);
if (i.uses_hvx) {
body = acquire_hvx_context(body, target);
}
body = substitute("uses_hvx", i.uses_hvx, body);
body = simplify(body);
return body;
}
void CodeGen_Hexagon::compile_func(const LoweredFunc &f,
const string &simple_name,
const string &extern_name) {
CodeGen_Posix::begin_func(f.linkage, simple_name, extern_name, f.args);
Stmt body = f.body;
debug(1) << "Hexagon: Unpredicating loads and stores...\n";
// Replace dense vector predicated loads with sloppy scalarized
// predicates, and scalarize predicated stores
body = sloppy_unpredicate_loads_and_stores(body);
debug(2) << "Hexagon: Lowering after unpredicating loads/stores:\n"
<< body << "\n\n";
if (is_hvx_v65_or_later()) {
// Generate vscatter-vgathers before optimize_hexagon_shuffles.
debug(1) << "Hexagon: Looking for vscatter-vgather...\n";
body = scatter_gather_generator(body);
debug(2) << "Hexagon: Lowering after vscatter-vgather:\n"
<< body << "\n\n";
}
debug(1) << "Hexagon: Optimizing shuffles...\n";
// vlut always indexes 64 bytes of the LUT at a time, even in 128 byte mode.
const int lut_alignment = 64;
body = optimize_hexagon_shuffles(body, lut_alignment);
debug(2) << "Hexagon: Lowering after optimizing shuffles:\n"
<< body << "\n\n";
debug(1) << "Hexagon: Aligning loads for HVX....\n";
body = align_loads(body, target.natural_vector_size(Int(8)), 8);
body = common_subexpression_elimination(body);
// Don't simplify here, otherwise it will re-collapse the loads we
// want to carry across loop iterations.
debug(2) << "Hexagon: Lowering after aligning loads:\n"
<< body << "\n\n";
debug(1) << "Hexagon: Carrying values across loop iterations...\n";
// Use at most 16 vector registers for carrying values.
body = loop_carry(body, 16);
body = simplify(body);
debug(2) << "Hexagon: Lowering after forwarding stores:\n"
<< body << "\n\n";
// Optimize the IR for Hexagon.
debug(1) << "Hexagon: Optimizing Hexagon instructions...\n";
body = optimize_hexagon_instructions(body, target);
debug(2) << "Hexagon: Lowering after optimizing Hexagon instructions:\n"
<< body << "\n\n";
debug(1) << "Hexagon: Adding calls to qurt_hvx_lock, if necessary...\n";
body = inject_hvx_lock_unlock(body, target);
debug(2) << "Hexagon: Lowering after adding calls to qurt_hvx_lock:\n"
<< body << "\n\n";
debug(1) << "Hexagon: function body for " << simple_name << " :\n";
debug(1) << body << "\n";
body.accept(this);
CodeGen_Posix::end_func(f.args);
}
struct HvxIntrinsic {
enum {
BroadcastScalarsToWords = 1 << 0, // Some intrinsics need scalar arguments
// broadcasted up to 32 bits.
v65OrLater = 1 << 1,
};
llvm::Intrinsic::ID id;
halide_type_t ret_type;
const char *name;
halide_type_t arg_types[4];
int flags;
};
// TODO: these should probably be declared constexpr, but that would
// require marking various halide_type_t methods as constexpr, and an
// obscure bug in MSVC2017 can cause compilation failures for them.
// The bug appears to be fixed in MSVC2019, so when we move to that
// as a baseline for Windows, this should be revisited.
halide_type_t i8 = halide_type_t(halide_type_int, 8);
halide_type_t i16 = halide_type_t(halide_type_int, 16);
halide_type_t i32 = halide_type_t(halide_type_int, 32);
halide_type_t u8 = halide_type_t(halide_type_uint, 8);
halide_type_t u16 = halide_type_t(halide_type_uint, 16);
halide_type_t u32 = halide_type_t(halide_type_uint, 32);
// Define vectors that are 1x and 2x the Hexagon HVX width --
// Note that we use placeholders here (which we fix up when processing
// the table) as we don't know the HVX width until we know the target
// we're using; this approach lets us make a compact table with static
// data, rather than having to assemble it at runtime.
constexpr int kOneX = 64 * 8;
halide_type_t i8v1 = i8.with_lanes(kOneX / 8);
halide_type_t i16v1 = i16.with_lanes(kOneX / 16);
halide_type_t i32v1 = i32.with_lanes(kOneX / 32);
halide_type_t u8v1 = u8.with_lanes(kOneX / 8);
halide_type_t u16v1 = u16.with_lanes(kOneX / 16);
halide_type_t u32v1 = u32.with_lanes(kOneX / 32);
halide_type_t i8v2 = i8v1.with_lanes(i8v1.lanes * 2);
halide_type_t i16v2 = i16v1.with_lanes(i16v1.lanes * 2);
halide_type_t i32v2 = i32v1.with_lanes(i32v1.lanes * 2);
halide_type_t u8v2 = u8v1.with_lanes(u8v1.lanes * 2);
halide_type_t u16v2 = u16v1.with_lanes(u16v1.lanes * 2);
halide_type_t u32v2 = u32v1.with_lanes(u32v1.lanes * 2);
#define INTRINSIC_128B(id) llvm::Intrinsic::hexagon_V6_##id##_128B
const HvxIntrinsic intrinsic_wrappers[] = {
// Zero/sign extension:
{INTRINSIC_128B(vzb), u16v2, "zxt.vub", {u8v1}},
{INTRINSIC_128B(vzh), u32v2, "zxt.vuh", {u16v1}},
{INTRINSIC_128B(vsb), i16v2, "sxt.vb", {i8v1}},
{INTRINSIC_128B(vsh), i32v2, "sxt.vh", {i16v1}},
// Similar to zxt/sxt, but without deinterleaving the result.
{INTRINSIC_128B(vunpackub), u16v2, "unpack.vub", {u8v1}},
{INTRINSIC_128B(vunpackuh), u32v2, "unpack.vuh", {u16v1}},
{INTRINSIC_128B(vunpackb), i16v2, "unpack.vb", {i8v1}},
{INTRINSIC_128B(vunpackh), i32v2, "unpack.vh", {i16v1}},
// Truncation:
// (Yes, there really are two fs in the b versions, and 1 f in
// the h versions.)
{INTRINSIC_128B(vshuffeb), i8v1, "trunc.vh", {i16v2}},
{INTRINSIC_128B(vshufeh), i16v1, "trunc.vw", {i32v2}},
{INTRINSIC_128B(vshuffob), i8v1, "trunclo.vh", {i16v2}},
{INTRINSIC_128B(vshufoh), i16v1, "trunclo.vw", {i32v2}},
// Downcast with saturation:
{INTRINSIC_128B(vsathub), u8v1, "trunc_satub.vh", {i16v2}},
{INTRINSIC_128B(vsatwh), i16v1, "trunc_sath.vw", {i32v2}},
{INTRINSIC_128B(vsatuwuh), u16v1, "trunc_satuh.vuw", {u32v2}},
{INTRINSIC_128B(vroundhub), u8v1, "trunc_satub_rnd.vh", {i16v2}},
{INTRINSIC_128B(vroundhb), i8v1, "trunc_satb_rnd.vh", {i16v2}},
{INTRINSIC_128B(vrounduhub), u8v1, "trunc_satub_rnd.vuh", {u16v2}},
{INTRINSIC_128B(vroundwuh), u16v1, "trunc_satuh_rnd.vw", {i32v2}},
{INTRINSIC_128B(vroundwh), i16v1, "trunc_sath_rnd.vw", {i32v2}},
{INTRINSIC_128B(vrounduwuh), u16v1, "trunc_satuh_rnd.vuw", {u32v2}},
// vpack does not interleave its input.
{INTRINSIC_128B(vpackhub_sat), u8v1, "pack_satub.vh", {i16v2}},
{INTRINSIC_128B(vpackwuh_sat), u16v1, "pack_satuh.vw", {i32v2}},
{INTRINSIC_128B(vpackhb_sat), i8v1, "pack_satb.vh", {i16v2}},
{INTRINSIC_128B(vpackwh_sat), i16v1, "pack_sath.vw", {i32v2}},
{INTRINSIC_128B(vpackeb), i8v1, "pack.vh", {i16v2}},
{INTRINSIC_128B(vpackeh), i16v1, "pack.vw", {i32v2}},
{INTRINSIC_128B(vpackob), i8v1, "packhi.vh", {i16v2}},
{INTRINSIC_128B(vpackoh), i16v1, "packhi.vw", {i32v2}},
// Widening adds. There are other instructions that add two vub and two vuh
// but do not widen.
// To differentiate those from the widening ones, we encode the return type
// in the name here.
{INTRINSIC_128B(vaddubh), u16v2, "add_vuh.vub.vub", {u8v1, u8v1}},
{INTRINSIC_128B(vaddhw), i32v2, "add_vw.vh.vh", {i16v1, i16v1}},
{INTRINSIC_128B(vadduhw), u32v2, "add_vuw.vuh.vuh", {u16v1, u16v1}},
// Widening subtracts. There are other instructions that subtact two vub and
// two vuh but do not widen.
// To differentiate those from the widening ones, we encode the return type
// in the name here.
{INTRINSIC_128B(vsububh), i16v2, "sub_vh.vub.vub", {u8v1, u8v1}},
{INTRINSIC_128B(vsubhw), i32v2, "sub_vw.vh.vh", {i16v1, i16v1}},
{INTRINSIC_128B(vsubuhw), i32v2, "sub_vw.vuh.vuh", {u16v1, u16v1}},
// Adds/subtract of unsigned values with saturation.
{INTRINSIC_128B(vaddubsat), u8v1, "sat_add.vub.vub", {u8v1, u8v1}},
{INTRINSIC_128B(vadduhsat), u16v1, "sat_add.vuh.vuh", {u16v1, u16v1}},
{INTRINSIC_128B(vadduwsat), u32v1, "sat_add.vuw.vuw", {u32v1, u32v1}},
{INTRINSIC_128B(vaddhsat), i16v1, "sat_add.vh.vh", {i16v1, i16v1}},
{INTRINSIC_128B(vaddwsat), i32v1, "sat_add.vw.vw", {i32v1, i32v1}},
{INTRINSIC_128B(vaddubsat_dv), u8v2, "sat_add.vub.vub.dv", {u8v2, u8v2}},
{INTRINSIC_128B(vadduhsat_dv), u16v2, "sat_add.vuh.vuh.dv", {u16v2, u16v2}},
{INTRINSIC_128B(vadduwsat_dv), u32v2, "sat_add.vuw.vuw.dv", {u32v2, u32v2}},
{INTRINSIC_128B(vaddhsat_dv), i16v2, "sat_add.vh.vh.dv", {i16v2, i16v2}},
{INTRINSIC_128B(vaddwsat_dv), i32v2, "sat_add.vw.vw.dv", {i32v2, i32v2}},
{INTRINSIC_128B(vsububsat), i8v1, "sat_sub.vub.vub", {u8v1, u8v1}},
{INTRINSIC_128B(vsubuhsat), i16v1, "sat_sub.vuh.vuh", {u16v1, u16v1}},
{INTRINSIC_128B(vsubhsat), i16v1, "sat_sub.vh.vh", {i16v1, i16v1}},
{INTRINSIC_128B(vsubwsat), i32v1, "sat_sub.vw.vw", {i32v1, i32v1}},
{INTRINSIC_128B(vsububsat_dv), i8v2, "sat_sub.vub.vub.dv", {u8v2, u8v2}},
{INTRINSIC_128B(vsubuhsat_dv), i16v2, "sat_sub.vuh.vuh.dv", {u16v2, u16v2}},
{INTRINSIC_128B(vsubhsat_dv), i16v2, "sat_sub.vh.vh.dv", {i16v2, i16v2}},
{INTRINSIC_128B(vsubwsat_dv), i32v2, "sat_sub.vw.vw.dv", {i32v2, i32v2}},
// Absolute value:
{INTRINSIC_128B(vabsh), u16v1, "abs.vh", {i16v1}},
{INTRINSIC_128B(vabsw), u32v1, "abs.vw", {i32v1}},
{INTRINSIC_128B(vabsb), u8v1, "abs.vb", {i8v1}, HvxIntrinsic::v65OrLater},
// Absolute difference:
{INTRINSIC_128B(vabsdiffub), u8v1, "absd.vub.vub", {u8v1, u8v1}},
{INTRINSIC_128B(vabsdiffuh), u16v1, "absd.vuh.vuh", {u16v1, u16v1}},
{INTRINSIC_128B(vabsdiffh), u16v1, "absd.vh.vh", {i16v1, i16v1}},
{INTRINSIC_128B(vabsdiffw), u32v1, "absd.vw.vw", {i32v1, i32v1}},
// Averaging:
{INTRINSIC_128B(vavgub), u8v1, "avg.vub.vub", {u8v1, u8v1}},
{INTRINSIC_128B(vavguh), u16v1, "avg.vuh.vuh", {u16v1, u16v1}},
{INTRINSIC_128B(vavguw), u32v1, "avg.vuw.vuw", {u32v1, u32v1}, HvxIntrinsic::v65OrLater},
{INTRINSIC_128B(vavgb), i8v1, "avg.vb.vb", {i8v1, i8v1}, HvxIntrinsic::v65OrLater},
{INTRINSIC_128B(vavgh), i16v1, "avg.vh.vh", {i16v1, i16v1}},
{INTRINSIC_128B(vavgw), i32v1, "avg.vw.vw", {i32v1, i32v1}},
{INTRINSIC_128B(vavgubrnd), u8v1, "avg_rnd.vub.vub", {u8v1, u8v1}},
{INTRINSIC_128B(vavguhrnd), u16v1, "avg_rnd.vuh.vuh", {u16v1, u16v1}},
{INTRINSIC_128B(vavguwrnd), u32v1, "avg_rnd.vuw.vuw", {u32v1, u32v1}, HvxIntrinsic::v65OrLater},
{INTRINSIC_128B(vavgbrnd), i8v1, "avg_rnd.vb.vb", {i8v1, i8v1}, HvxIntrinsic::v65OrLater},
{INTRINSIC_128B(vavghrnd), i16v1, "avg_rnd.vh.vh", {i16v1, i16v1}},
{INTRINSIC_128B(vavgwrnd), i32v1, "avg_rnd.vw.vw", {i32v1, i32v1}},
// This one is weird: i8_sat((u8 - u8)/2). It both saturates and averages.
{INTRINSIC_128B(vnavgub), i8v1, "navg.vub.vub", {u8v1, u8v1}},
{INTRINSIC_128B(vnavgb), i8v1, "navg.vb.vb", {i8v1, i8v1}, HvxIntrinsic::v65OrLater},
{INTRINSIC_128B(vnavgh), i16v1, "navg.vh.vh", {i16v1, i16v1}},
{INTRINSIC_128B(vnavgw), i32v1, "navg.vw.vw", {i32v1, i32v1}},
// Non-widening multiplication:
{INTRINSIC_128B(vmpyih), i16v1, "mul.vh.vh", {i16v1, i16v1}},
{INTRINSIC_128B(vmpyihb), i16v1, "mul.vh.b", {i16v1, i8}, HvxIntrinsic::BroadcastScalarsToWords},
{INTRINSIC_128B(vmpyiwh), i32v1, "mul.vw.h", {i32v1, i16}, HvxIntrinsic::BroadcastScalarsToWords},
{INTRINSIC_128B(vmpyiwb), i32v1, "mul.vw.b", {i32v1, i8}, HvxIntrinsic::BroadcastScalarsToWords},
{INTRINSIC_128B(vmpyih_acc), i16v1, "add_mul.vh.vh.vh", {i16v1, i16v1, i16v1}},
{INTRINSIC_128B(vmpyihb_acc), i16v1, "add_mul.vh.vh.b", {i16v1, i16v1, i8}, HvxIntrinsic::BroadcastScalarsToWords},
{INTRINSIC_128B(vmpyiwh_acc), i32v1, "add_mul.vw.vw.h", {i32v1, i32v1, i16}, HvxIntrinsic::BroadcastScalarsToWords},
{INTRINSIC_128B(vmpyiwb_acc), i32v1, "add_mul.vw.vw.b", {i32v1, i32v1, i8}, HvxIntrinsic::BroadcastScalarsToWords},
// Widening vector multiplication:
{INTRINSIC_128B(vmpyubv), u16v2, "mpy.vub.vub", {u8v1, u8v1}},
{INTRINSIC_128B(vmpyuhv), u32v2, "mpy.vuh.vuh", {u16v1, u16v1}},
{INTRINSIC_128B(vmpybv), i16v2, "mpy.vb.vb", {i8v1, i8v1}},
{INTRINSIC_128B(vmpyhv), i32v2, "mpy.vh.vh", {i16v1, i16v1}},
{INTRINSIC_128B(vmpyubv_acc), u16v2, "add_mpy.vuh.vub.vub", {u16v2, u8v1, u8v1}},
{INTRINSIC_128B(vmpyuhv_acc), u32v2, "add_mpy.vuw.vuh.vuh", {u32v2, u16v1, u16v1}},
{INTRINSIC_128B(vmpybv_acc), i16v2, "add_mpy.vh.vb.vb", {i16v2, i8v1, i8v1}},
{INTRINSIC_128B(vmpyhv_acc), i32v2, "add_mpy.vw.vh.vh", {i32v2, i16v1, i16v1}},
// Inconsistencies: both are vector instructions despite the
// missing 'v', and the signedness is indeed swapped.
{INTRINSIC_128B(vmpybusv), i16v2, "mpy.vub.vb", {u8v1, i8v1}},
{INTRINSIC_128B(vmpyhus), i32v2, "mpy.vh.vuh", {i16v1, u16v1}},
{INTRINSIC_128B(vmpybusv_acc), i16v2, "add_mpy.vh.vub.vb", {i16v2, u8v1, i8v1}},
{INTRINSIC_128B(vmpyhus_acc), i32v2, "add_mpy.vw.vh.vuh", {i32v2, i16v1, u16v1}},
// Widening scalar multiplication:
{INTRINSIC_128B(vmpyub), u16v2, "mpy.vub.ub", {u8v1, u8}, HvxIntrinsic::BroadcastScalarsToWords},
{INTRINSIC_128B(vmpyuh), u32v2, "mpy.vuh.uh", {u16v1, u16}, HvxIntrinsic::BroadcastScalarsToWords},
{INTRINSIC_128B(vmpyh), i32v2, "mpy.vh.h", {i16v1, i16}, HvxIntrinsic::BroadcastScalarsToWords},
{INTRINSIC_128B(vmpybus), i16v2, "mpy.vub.b", {u8v1, i8}, HvxIntrinsic::BroadcastScalarsToWords},
{INTRINSIC_128B(vmpyub_acc), u16v2, "add_mpy.vuh.vub.ub", {u16v2, u8v1, u8}, HvxIntrinsic::BroadcastScalarsToWords},
{INTRINSIC_128B(vmpyuh_acc), u32v2, "add_mpy.vuw.vuh.uh", {u32v2, u16v1, u16}, HvxIntrinsic::BroadcastScalarsToWords},
{INTRINSIC_128B(vmpybus_acc), i16v2, "add_mpy.vh.vub.b", {i16v2, u8v1, i8}, HvxIntrinsic::BroadcastScalarsToWords},
{INTRINSIC_128B(vmpyhsat_acc), i32v2, "satw_add_mpy.vw.vh.h", {i32v2, i16v1, i16}, HvxIntrinsic::BroadcastScalarsToWords},
// Widening vector multiplication, with horizontal reduction.
{INTRINSIC_128B(vrmpyubv), u32v1, "add_4mpy.vub.vub", {u8v1, u8v1}},
{INTRINSIC_128B(vrmpybv), i32v1, "add_4mpy.vb.vb", {i8v1, i8v1}},
{INTRINSIC_128B(vrmpybusv), i32v1, "add_4mpy.vub.vb", {i8v1, i8v1}},
{INTRINSIC_128B(vrmpyubv_acc), u32v1, "acc_add_4mpy.vuw.vub.vub", {u32v1, u8v1, u8v1}},
{INTRINSIC_128B(vrmpybv_acc), i32v1, "acc_add_4mpy.vw.vb.vb", {i32v1, i8v1, i8v1}},
{INTRINSIC_128B(vrmpybusv_acc), i32v1, "acc_add_4mpy.vw.vub.vb", {i32v1, i8v1, i8v1}},
// Widening scalar multiplication, with horizontal reduction.
{INTRINSIC_128B(vdmpybus), i16v1, "add_2mpy.vub.b", {u8v1, i32}},
{INTRINSIC_128B(vdmpyhb), i32v1, "add_2mpy.vh.b", {i16v1, i32}},
{INTRINSIC_128B(vdmpybus_acc), i16v1, "acc_add_2mpy.vh.vub.b", {i16v1, u8v1, i32}},
{INTRINSIC_128B(vdmpyhb_acc), i32v1, "acc_add_2mpy.vw.vh.b", {i32v1, i16v1, i32}},
// Saturating versions of vdmpy.
{INTRINSIC_128B(vdmpyhsat), i32v1, "add_2mpy.vh.h", {i16v1, i32}},
{INTRINSIC_128B(vdmpyhsusat), i32v1, "add_2mpy.vh.uh", {i16v1, u32}},
{INTRINSIC_128B(vdmpyhvsat), i32v1, "add_2mpy.vh.vh", {i16v1, i16v1}},
{INTRINSIC_128B(vmpabus), i16v2, "add_2mpy.vub.vub.b.b", {i8v2, i32}},
{INTRINSIC_128B(vmpabus_acc), i16v2, "acc_add_2mpy.vh.vub.vub.b.b", {i16v2, i8v2, i32}},
{INTRINSIC_128B(vmpahb), i32v2, "add_2mpy.vh.vh.b.b", {i16v2, i32}},
{INTRINSIC_128B(vmpahb_acc), i32v2, "acc_add_2mpy.vw.vh.vh.b.b", {i32v2, i16v2, i32}},
// TODO: These don't generate correctly because the vectors
// aren't interleaved correctly.
//{ vdmpybus_dv, i16v2, "add_2mpy.vub.b.dv", {u8v2, i32} },
//{ vdmpyhb_dv, i32v2, "add_2mpy.vh.b.dv", {i16v2, i32} },
//{ vdmpybus_dv_acc, i16v2, "acc_add_2mpy.vh.vub.b.dv", {i16v2, u8v2, i32} },
//{ vdmpyhb_dv_acc, i32v2, "acc_add_2mpy.vw.vh.b.dv", {i32v2, i16v2, i32} },
// vtmpy
// TODO: These (and many vdmpy variants) should have 16-bit scalars with BroadcastScalarsToWords, so
// we don't need to replicate the arguments in HexagonOptimize.cpp. However, this triggers opaque
// failures in LLVM.
{INTRINSIC_128B(vtmpybus), i16v2, "add_3mpy.vub.b", {u8v2, i32}},
{INTRINSIC_128B(vtmpyb), i16v2, "add_3mpy.vb.b", {i8v2, i32}},
{INTRINSIC_128B(vtmpyhb), i32v2, "add_3mpy.vh.b", {u16v2, i32}},
{INTRINSIC_128B(vtmpybus_acc), i16v2, "acc_add_3mpy.vh.vub.b", {i16v2, u8v2, i32}},
{INTRINSIC_128B(vtmpyb_acc), i16v2, "acc_add_3mpy.vh.vb.b", {i16v2, i8v2, i32}},
{INTRINSIC_128B(vtmpyhb_acc), i32v2, "acc_add_3mpy.vw.vh.b", {i32v2, u16v2, i32}},
{INTRINSIC_128B(vrmpybus), i32v1, "add_4mpy.vub.b", {u8v1, i32}},
{INTRINSIC_128B(vrmpyub), u32v1, "add_4mpy.vub.ub", {u8v1, u32}},
{INTRINSIC_128B(vrmpybus_acc), i32v1, "acc_add_4mpy.vw.vub.b", {i32v1, u8v1, i32}},
{INTRINSIC_128B(vrmpyub_acc), u32v1, "acc_add_4mpy.vuw.vub.ub", {u32v1, u8v1, u32}},
// Multiply keep high half, with multiplication by 2.
{INTRINSIC_128B(vmpyhvsrs), i16v1, "trunc_satw_mpy2_rnd.vh.vh", {i16v1, i16v1}},
{INTRINSIC_128B(vmpyhss), i16v1, "trunc_satw_mpy2.vh.h", {i16v1, i16}, HvxIntrinsic::BroadcastScalarsToWords},
{INTRINSIC_128B(vmpyhsrs), i16v1, "trunc_satw_mpy2_rnd.vh.h", {i16v1, i16}, HvxIntrinsic::BroadcastScalarsToWords},
// Min/max:
{INTRINSIC_128B(vmaxub), u8v1, "max.vub.vub", {u8v1, u8v1}},
{INTRINSIC_128B(vmaxuh), u16v1, "max.vuh.vuh", {u16v1, u16v1}},
{INTRINSIC_128B(vmaxh), i16v1, "max.vh.vh", {i16v1, i16v1}},
{INTRINSIC_128B(vmaxw), i32v1, "max.vw.vw", {i32v1, i32v1}},
{INTRINSIC_128B(vminub), u8v1, "min.vub.vub", {u8v1, u8v1}},
{INTRINSIC_128B(vminuh), u16v1, "min.vuh.vuh", {u16v1, u16v1}},
{INTRINSIC_128B(vminh), i16v1, "min.vh.vh", {i16v1, i16v1}},
{INTRINSIC_128B(vminw), i32v1, "min.vw.vw", {i32v1, i32v1}},
// Shifts
// We map arithmetic and logical shifts to just "shr", depending on type.
{INTRINSIC_128B(vlsrhv), u16v1, "shr.vuh.vh", {u16v1, u16v1}},
{INTRINSIC_128B(vlsrwv), u32v1, "shr.vuw.vw", {u32v1, u32v1}},
{INTRINSIC_128B(vasrhv), i16v1, "shr.vh.vh", {i16v1, u16v1}},
{INTRINSIC_128B(vasrwv), i32v1, "shr.vw.vw", {i32v1, u32v1}},
// Rounding shift right
{INTRINSIC_128B(vasrhubrndsat), u8v1, "trunc_satub_shr_rnd.vh", {i16v2, u16}},
{INTRINSIC_128B(vasrhbrndsat), i8v1, "trunc_satb_shr_rnd.vh", {i16v2, u16}},
{INTRINSIC_128B(vasruhubrndsat), u8v1, "trunc_satub_shr_rnd.vuh", {u16v2, u16}, HvxIntrinsic::v65OrLater},
{INTRINSIC_128B(vasrwuhrndsat), u16v1, "trunc_satuh_shr_rnd.vw", {i32v2, u32}},
{INTRINSIC_128B(vasrwhrndsat), i16v1, "trunc_sath_shr_rnd.vw", {i32v2, u32}},
{INTRINSIC_128B(vasruwuhrndsat), u16v1, "trunc_satuh_shr_rnd.vuw", {u32v2, u32}},
{INTRINSIC_128B(vaslhv), u16v1, "shl.vuh.vh", {u16v1, u16v1}},
{INTRINSIC_128B(vaslwv), u32v1, "shl.vuw.vw", {u32v1, u32v1}},
{INTRINSIC_128B(vaslhv), i16v1, "shl.vh.vh", {i16v1, u16v1}},
{INTRINSIC_128B(vaslwv), i32v1, "shl.vw.vw", {i32v1, u32v1}},
{INTRINSIC_128B(vlsrh), u16v1, "shr.vuh.h", {u16v1, u16}},
{INTRINSIC_128B(vlsrw), u32v1, "shr.vuw.w", {u32v1, u32}},
{INTRINSIC_128B(vasrh), i16v1, "shr.vh.h", {i16v1, u16}},
{INTRINSIC_128B(vasrw), i32v1, "shr.vw.w", {i32v1, u32}},
{INTRINSIC_128B(vaslh), u16v1, "shl.vuh.h", {u16v1, u16}},
{INTRINSIC_128B(vaslw), u32v1, "shl.vuw.w", {u32v1, u32}},
{INTRINSIC_128B(vaslh), i16v1, "shl.vh.h", {i16v1, u16}},
{INTRINSIC_128B(vaslw), i32v1, "shl.vw.w", {i32v1, u32}},
{INTRINSIC_128B(vasrh_acc), i16v1, "add_shr.vh.vh.uh", {i16v1, i16v1, i16}, HvxIntrinsic::BroadcastScalarsToWords | HvxIntrinsic::v65OrLater},
{INTRINSIC_128B(vaslh_acc), i16v1, "add_shl.vh.vh.uh", {i16v1, i16v1, i16}, HvxIntrinsic::BroadcastScalarsToWords | HvxIntrinsic::v65OrLater},
{INTRINSIC_128B(vasrw_acc), i32v1, "add_shr.vw.vw.uw", {i32v1, i32v1, i32}},
{INTRINSIC_128B(vaslw_acc), i32v1, "add_shl.vw.vw.uw", {i32v1, i32v1, i32}},
{INTRINSIC_128B(vasrwh), i16v1, "trunc_shr.vw.uw", {i32v2, u32}},
{INTRINSIC_128B(vasrhubsat), u8v1, "trunc_satub_shr.vh.uh", {i16v2, u16}},
{INTRINSIC_128B(vasrwuhsat), u16v1, "trunc_satuh_shr.vw.uw", {i32v2, u32}},
{INTRINSIC_128B(vasrwhsat), i16v1, "trunc_sath_shr.vw.uw", {i32v2, u32}},
{INTRINSIC_128B(vror), u8v1, "vror", {u8v1, i32}},
// Bit counting
{INTRINSIC_128B(vnormamth), u16v1, "cls.vh", {u16v1}},
{INTRINSIC_128B(vnormamtw), u32v1, "cls.vw", {u32v1}},
};
// TODO: Many variants of the above functions are missing. They
// need to be implemented in the runtime module, or via
// fall-through to CodeGen_LLVM.
void CodeGen_Hexagon::init_module() {
CodeGen_Posix::init_module();
// LLVM's HVX vector intrinsics don't include the type of the
// operands, they all operate on vectors of 32 bit integers. To make
// it easier to generate code, we define wrapper intrinsics with
// the correct type (plus the necessary bitcasts).
const auto fix_lanes = [&](const halide_type_t &t) -> halide_type_t {
if (t.lanes == 1) {
return t;
}
const int lanes_actual = ((int)t.lanes * native_vector_bits()) / kOneX;
return t.with_lanes(lanes_actual);
};
vector<Type> arg_types;
for (const HvxIntrinsic &i : intrinsic_wrappers) {
llvm::Intrinsic::ID id = i.id;
internal_assert(id != llvm::Intrinsic::not_intrinsic);
// Get the real intrinsic.
llvm::Function *intrin = llvm::Intrinsic::getOrInsertDeclaration(module.get(), id);
halide_type_t ret_type = fix_lanes(i.ret_type);
arg_types.clear();
for (const auto &a : i.arg_types) {
if (a.bits == 0) {
break;
}
arg_types.emplace_back(fix_lanes(a));
}
define_hvx_intrinsic(intrin, ret_type, i.name, arg_types, i.flags);
}
}
llvm::Function *CodeGen_Hexagon::define_hvx_intrinsic(llvm::Function *intrin,
Type ret_ty,
const string &name,
vector<Type> arg_types,
int flags) {
internal_assert(intrin) << "Null definition for intrinsic '" << name << "'\n";
llvm::FunctionType *intrin_ty = intrin->getFunctionType();
bool broadcast_scalar_word = flags & HvxIntrinsic::BroadcastScalarsToWords;
bool v65OrLater = flags & HvxIntrinsic::v65OrLater;
if (v65OrLater && !is_hvx_v65_or_later()) {
return nullptr;
}
// Get the types of the arguments we want to pass.
vector<llvm::Type *> llvm_arg_types;
llvm_arg_types.reserve(arg_types.size());
for (Type i : arg_types) {
llvm_arg_types.push_back(llvm_type_of(i));
}
// Make a wrapper intrinsic.
llvm::FunctionType *wrapper_ty =
llvm::FunctionType::get(llvm_type_of(ret_ty), llvm_arg_types, false);
llvm::Function *wrapper =
llvm::Function::Create(wrapper_ty, llvm::GlobalValue::InternalLinkage,
"halide.hexagon." + name, module.get());
llvm::BasicBlock *block =
llvm::BasicBlock::Create(module->getContext(), "entry", wrapper);
IRBuilderBase::InsertPoint here = builder->saveIP();
builder->SetInsertPoint(block);
vector<Value *> args;
for (Value &arg : wrapper->args()) {
args.push_back(&arg);
}
if (args.size() + 1 == intrin_ty->getNumParams()) {
// This intrinsic needs the first argument split into the high and low
// vectors.
Value *dv = args[0];
int vec_lanes = native_vector_bits() / arg_types[0].bits();
Value *low = slice_vector(dv, 0, vec_lanes);
Value *high = slice_vector(dv, vec_lanes, vec_lanes);
args[0] = high;
args.insert(args.begin() + 1, low);
Type split_type =
arg_types.front().with_lanes(arg_types.front().lanes() / 2);
arg_types[0] = split_type;
arg_types.insert(arg_types.begin() + 1, split_type);
}
// Replace args with bitcasts if necessary.
internal_assert(args.size() == intrin_ty->getNumParams());
for (size_t i = 0; i < args.size(); i++) {
llvm::Type *arg_ty = intrin_ty->getParamType(i);
if (args[i]->getType() != arg_ty) {
if (arg_ty->isVectorTy()) {
args[i] = builder->CreateBitCast(args[i], arg_ty);
} else {
if (broadcast_scalar_word) {
llvm::Function *fn = nullptr;
// We know it is a scalar type. We can have 8 bit, 16 bit or 32 bit
// types only.
unsigned bits = arg_types[i].bits();
const char *fn_name = "";
switch (bits) {
case 8:
fn_name = "halide.hexagon.dup4.b";
break;
case 16:
fn_name = "halide.hexagon.dup2.h";
break;
default:
internal_error
<< "unhandled broadcast_scalar_word in define_hvx_intrinsic";
}
fn = module->getFunction(fn_name);
internal_assert(fn) << "Unable to find function " << fn_name << " in define_hvx_intrinsic.";
args[i] = builder->CreateCall(fn, {args[i]});
} else if (args[i]->getType()->isIntegerTy()) {
args[i] =
builder->CreateIntCast(args[i], arg_ty, arg_types[i].is_int());
} else {
args[i] = builder->CreateBitCast(args[i], arg_ty);
}
}
}
}
// Call the real intrinsic.
Value *ret = builder->CreateCall(intrin, args);
// Cast the result, if necessary.
if (ret->getType() != wrapper_ty->getReturnType()) {
ret = builder->CreateBitCast(ret, wrapper_ty->getReturnType());
}
builder->CreateRet(ret);
// Always inline these wrappers.
wrapper->addFnAttr(llvm::Attribute::AlwaysInline);
builder->restoreIP(here);
llvm::verifyFunction(*wrapper);
return wrapper;
}
Value *CodeGen_Hexagon::create_bitcast(Value *v, llvm::Type *ty) {
if (BitCastInst *c = dyn_cast<BitCastInst>(v)) {
return create_bitcast(c->getOperand(0), ty);
} else if (isa<PoisonValue>(v)) {
return PoisonValue::get(ty);
} else if (v->getType() != ty) {
v = builder->CreateBitCast(v, ty);
}
return v;