-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathGenerateCodePass.cpp
More file actions
1749 lines (1560 loc) · 71 KB
/
GenerateCodePass.cpp
File metadata and controls
1749 lines (1560 loc) · 71 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 "Common/AcceleratorAttrs.h"
#include "mlir/Pass/Pass.h"
#include "mlir/IR/BuiltinOps.h"
#include "mlir/IR/Operation.h"
#include "mlir/IR/Value.h"
#include "mlir/IR/Attributes.h"
#include "mlir/Dialect/Func/IR/FuncOps.h"
#include "llvm/ADT/ArrayRef.h"
#include "llvm/ADT/DenseMap.h"
#include "llvm/ADT/Hashing.h"
#include "llvm/ADT/SmallVector.h"
#include "llvm/ADT/STLExtras.h"
#include "llvm/Support/raw_ostream.h"
#include <algorithm>
#include <cassert>
#include <map>
#include <optional>
#include <string>
#include <vector>
#include <unordered_set>
#include <tuple>
#include <sstream>
#include "NeuraDialect/Architecture/Architecture.h"
#include "NeuraDialect/NeuraOps.h"
#include "NeuraDialect/NeuraAttributes.h"
using namespace mlir;
using namespace neura;
namespace {
struct Operand {
std::string operand;
std::string color;
Operand(const std::string &op, const std::string &c = "RED")
: operand(op), color(c) {}
};
struct Instruction {
std::string opcode;
std::vector<Operand> src_operands;
std::vector<Operand> dst_operands;
int id = -1; // Unique instruction ID for debug/DFG.
int time_step = -1; // original scheduling timestep.
int index_per_ii = -1; // grouping key (typically time_step % compiled_ii).
int invalid_iterations = 0; // prologue length before the op becomes valid.
Instruction(const std::string &op) : opcode(op) {}
};
// A single-entry model per tile for now. Can be extended later.
struct Entry {
std::string entry_id;
std::string type;
std::vector<Instruction> instructions;
Entry(const std::string &id, const std::string &t = "loop")
: entry_id(id), type(t) {}
};
struct Tile {
int col_idx, row_idx;
int core_id;
Entry entry; // single entry per tile.
Tile(int c, int r, int id) : col_idx(c), row_idx(r), core_id(id), entry("entry0", "loop") {}
};
struct ArrayConfig {
int columns;
int rows;
int compiled_ii = -1;
std::vector<Tile> cores;
};
struct TileLocation {
int col_idx = -1, row_idx = -1, time_step = -1;
int index_per_ii = -1;
int invalid_iterations = 0;
bool has_tile = false;
};
// ---- Operation kind helpers ----.
static bool isDataMov(Operation *op) { return dyn_cast<DataMovOp>(op) != nullptr; }
static bool isCtrlMov(Operation *op) { return dyn_cast<CtrlMovOp>(op) != nullptr; }
static bool isPhiStart(Operation *op) { return dyn_cast<PhiStartOp>(op) != nullptr; }
static bool isReserve(Operation *op) { return dyn_cast<ReserveOp>(op) != nullptr; }
static bool isConstant(Operation *op) { return dyn_cast<ConstantOp>(op) != nullptr; }
static bool isFusedOp(Operation *op) { return dyn_cast<FusedOp>(op) != nullptr; }
// ---- Constant for phi_start operation ----.
static constexpr unsigned kReserveOpIndex = 1;
// Returns the reserve operand for phi_start (operand #1). Guards to ReserveOp.
static Value getReserveOperand(Operation *op) {
if (auto phi_start = dyn_cast<PhiStartOp>(op)) {
assert(op->getNumOperands() > kReserveOpIndex &&
"phi_start must have a reserve at operand #1");
Value candidate = phi_start->getOperand(kReserveOpIndex);
assert((!candidate || isa<ReserveOp>(candidate.getDefiningOp())) &&
"phi_start operand #1 must be a ReserveOp");
return candidate;
}
return Value();
}
namespace mapping_utils {
// ----- placement helpers -----.
static TileLocation getTileLocation(Operation *op) {
TileLocation tile_location;
if (auto arr = op->getAttrOfType<ArrayAttr>("mapping_locs")) {
for (Attribute a : arr) {
auto d = dyn_cast<DictionaryAttr>(a);
if (!d) continue;
auto resource = dyn_cast_or_null<StringAttr>(d.get("resource"));
if (auto timestep = dyn_cast_or_null<IntegerAttr>(d.get("time_step")))
tile_location.time_step = timestep.getInt();
if (auto index_attr = dyn_cast_or_null<IntegerAttr>(d.get("index_per_ii")))
tile_location.index_per_ii = index_attr.getInt();
if (auto invalid_attr = dyn_cast_or_null<IntegerAttr>(d.get("invalid_iterations")))
tile_location.invalid_iterations = invalid_attr.getInt();
if (resource && resource.getValue() == "tile") {
if (auto x_coord = dyn_cast_or_null<IntegerAttr>(d.get("x"))) tile_location.col_idx = x_coord.getInt();
if (auto y_coord = dyn_cast_or_null<IntegerAttr>(d.get("y"))) tile_location.row_idx = y_coord.getInt();
tile_location.has_tile = true;
}
}
}
// If tile mappings exist, x/y must be valid.
assert(!tile_location.has_tile || (tile_location.col_idx >= 0 && tile_location.row_idx >= 0));
return tile_location;
}
// Helper to get mapping_locations arrays.
static ArrayAttr getMappingLocations(Operation *op) {
return op->getAttrOfType<ArrayAttr>("mapping_locs");
}
static std::optional<int> getMappedRegId(Operation *op) {
if (auto mapping_locations = getMappingLocations(op)) {
for (Attribute location_attr : mapping_locations) {
auto location_dict = dyn_cast<DictionaryAttr>(location_attr);
if (!location_dict) continue;
auto resource_attr = dyn_cast_or_null<StringAttr>(location_dict.get("resource"));
if (!resource_attr) continue;
if (resource_attr.getValue() == "register" || resource_attr.getValue() == "reg") {
if (auto per_tile_register_id =
dyn_cast_or_null<IntegerAttr>(location_dict.get("per_tile_register_id"))) {
return per_tile_register_id.getInt();
}
}
}
}
return std::nullopt;
}
} // namespace mapping_utils
static std::string getOpcode(Operation *op) {
std::string opcode = op->getName().getStringRef().str();
if (opcode.rfind("neura.", 0) == 0) opcode = opcode.substr(6);
if (isConstant(op)) return "CONSTANT";
std::transform(opcode.begin(), opcode.end(), opcode.begin(), ::toupper);
// For comparison operations, appends the comparison type to the opcode.
if (auto icmp_op = dyn_cast<ICmpOp>(op)) {
std::string cmp_type = icmp_op.getCmpType().str();
std::transform(cmp_type.begin(), cmp_type.end(), cmp_type.begin(), ::toupper);
return opcode + "_" + cmp_type;
}
if (auto fcmp_op = dyn_cast<FCmpOp>(op)) {
std::string cmp_type = fcmp_op.getCmpType().str();
std::transform(cmp_type.begin(), cmp_type.end(), cmp_type.begin(), ::toupper);
return opcode + "_" + cmp_type;
}
// For cast operations, appends the cast type to the opcode.
if (auto cast_op = dyn_cast<CastOp>(op)) {
std::string cast_type = cast_op.getCastType().str();
std::transform(cast_type.begin(), cast_type.end(), cast_type.begin(), ::toupper);
return opcode + "_" + cast_type;
}
return opcode;
}
// Extracts constant literal from an attribute.
// Returns formatted string like "#10" or "#3.0", or "arg0" for "%arg0", or empty string if not found.
static std::string extractConstantLiteralFromAttr(Attribute attr) {
if (!attr) return "";
if (auto integer_attr = dyn_cast<IntegerAttr>(attr))
return "#" + std::to_string(integer_attr.getInt());
if (auto float_attr = dyn_cast<FloatAttr>(attr))
return "#" + std::to_string(float_attr.getValueAsDouble());
// Handles string attributes like "%arg0" -> "arg0".
if (auto string_attr = dyn_cast<StringAttr>(attr)) {
std::string value = string_attr.getValue().str();
// Checks if the string starts with "%arg" followed by digits.
if (value.size() > 4 && value.substr(0, 4) == "%arg") {
return value.substr(1);
}
}
return "";
}
// Literals for CONSTANT operations, e.g. "#10" / "#0" / "#3.0".
static std::string getConstantLiteral(Operation *op) {
if (isConstant(op)) {
if (auto value_attr = op->getAttr(attr::kValue)) {
std::string result = extractConstantLiteralFromAttr(value_attr);
if (!result.empty()) return result;
}
return "#0";
}
// Checks for constant_value attribute in non-CONSTANT operations.
if (auto constant_value_attr = op->getAttr(attr::kConstantValue)) {
std::string result = extractConstantLiteralFromAttr(constant_value_attr);
if (!result.empty()) return result;
}
// Checks for rhs_value attribute (for binary operations with constant RHS).
if (auto rhs_value_attr = op->getAttr(attr::kRhsValue)) {
std::string result = extractConstantLiteralFromAttr(rhs_value_attr);
if (!result.empty()) return result;
}
return "";
}
namespace mapping_utils {
// ----- Topology from Architecture -----.
struct Topology {
DenseMap<int, std::pair<int,int>> link_ends; // link_id -> (srcTileId, dstTileId).
DenseMap<int, std::pair<int,int>> tile_location; // tileId -> (x,y).
DenseMap<std::pair<int,int>, int> coord_to_tile; // (x,y) -> tileId.
StringRef getDirBetween(int src_tile_id, int dst_tile_id) const {
auto [src_x, src_y] = tile_location.lookup(src_tile_id);
auto [dst_x, dst_y] = tile_location.lookup(dst_tile_id);
int dc = dst_x - src_x, dr = dst_y - src_y;
if (dc == 1 && dr == 0) return "EAST";
if (dc == -1 && dr == 0) return "WEST";
if (dc == 0 && dr == 1) return "NORTH";
if (dc == 0 && dr == -1) return "SOUTH";
if (dc == 1 && dr == 1) return "NORTHEAST";
if (dc == -1 && dr == 1) return "NORTHWEST";
if (dc == 1 && dr == -1) return "SOUTHEAST";
if (dc == -1 && dr == -1) return "SOUTHWEST";
return "LOCAL";
}
StringRef dirFromLink(int link_id) const {
auto it = link_ends.find(link_id);
if (it == link_ends.end()) return "LOCAL";
return getDirBetween(it->second.first, it->second.second);
}
StringRef invertDir(StringRef d) const {
if (d == "EAST") return "WEST";
if (d == "WEST") return "EAST";
if (d == "NORTH") return "SOUTH";
if (d == "SOUTH") return "NORTH";
if (d == "NORTHEAST") return "SOUTHWEST";
if (d == "NORTHWEST") return "SOUTHEAST";
if (d == "SOUTHEAST") return "NORTHWEST";
if (d == "SOUTHWEST") return "NORTHEAST";
return "LOCAL";
}
int srcTileOfLink(int link_id) const { return link_ends.lookup(link_id).first; }
int dstTileOfLink(int link_id) const { return link_ends.lookup(link_id).second; }
int tileIdAt(int x, int y) const {
auto it = coord_to_tile.find({x,y});
return (it == coord_to_tile.end()) ? -1 : it->second;
}
};
static Topology getTopologyFromArchitecture(int per_cgra_rows, int per_cgra_columns) {
Topology topo;
const Architecture &architecture = mlir::neura::getArchitecture();
for (auto *tile : architecture.getAllTiles()) {
topo.tile_location[tile->getId()] = {tile->getX(), tile->getY()};
topo.coord_to_tile[{tile->getX(), tile->getY()}] = tile->getId();
}
for (auto *link : architecture.getAllLinks()) {
auto *src_tile = link->getSrcTile();
auto *dst_tile = link->getDstTile();
topo.link_ends[link->getId()] = {src_tile->getId(), dst_tile->getId()};
}
return topo;
}
// ----- Extract mapping steps (sorted by time) -----.
struct LinkStep { int link_id; int time_step; };
struct RegStep { int regId; int time_step; };
static SmallVector<LinkStep, 8> collectLinkSteps(Operation *op) {
SmallVector<LinkStep, 8> steps;
if (auto mapping_locations = getMappingLocations(op)) {
for (Attribute location_attr : mapping_locations) {
auto location_dict = dyn_cast<DictionaryAttr>(location_attr);
if (!location_dict) continue;
auto resource_attr = dyn_cast_or_null<StringAttr>(location_dict.get("resource"));
if (!resource_attr || resource_attr.getValue() != "link") continue;
auto link_id = dyn_cast_or_null<IntegerAttr>(location_dict.get("id"));
auto time_step = dyn_cast_or_null<IntegerAttr>(location_dict.get("time_step"));
if (!link_id || !time_step) continue;
steps.push_back({(int)link_id.getInt(), (int)time_step.getInt()});
}
}
llvm::sort(steps, [](const LinkStep &a, const LinkStep &b){ return a.time_step < b.time_step; });
return steps;
}
static SmallVector<RegStep, 4> collectRegSteps(Operation *op) {
SmallVector<RegStep, 4> steps;
if (auto mapping_locations = getMappingLocations(op)) {
for (Attribute location_attr : mapping_locations) {
auto location_dict = dyn_cast<DictionaryAttr>(location_attr);
if (!location_dict) continue;
auto resource_attr = dyn_cast_or_null<StringAttr>(location_dict.get("resource"));
if (!resource_attr) continue;
if (resource_attr.getValue() == "register" || resource_attr.getValue() == "reg") {
auto per_tile_register_id = dyn_cast_or_null<IntegerAttr>(location_dict.get("per_tile_register_id"));
auto time_step = dyn_cast_or_null<IntegerAttr>(location_dict.get("time_step"));
if (!per_tile_register_id || !time_step) continue;
steps.push_back({(int)per_tile_register_id.getInt(), (int)time_step.getInt()});
}
}
}
llvm::sort(steps, [](const RegStep &a, const RegStep &b){ return a.time_step < b.time_step; });
return steps;
}
} // namespace mapping_utils
// Keep existing call sites stable (byte-identical behavior) by re-exporting the names.
using mapping_utils::Topology;
using mapping_utils::LinkStep;
using mapping_utils::RegStep;
using mapping_utils::collectLinkSteps;
using mapping_utils::collectRegSteps;
using mapping_utils::getMappedRegId;
using mapping_utils::getMappingLocations;
using mapping_utils::getTileLocation;
using mapping_utils::getTopologyFromArchitecture;
// ----- Pass -----.
struct InstructionReference { int col_idx, row_idx, t, idx; };
struct GenerateCodePass
: public PassWrapper<GenerateCodePass, OperationPass<ModuleOp>> {
MLIR_DEFINE_EXPLICIT_INTERNAL_INLINE_TYPE_ID(GenerateCodePass)
StringRef getArgument() const override { return "generate-code"; }
StringRef getDescription() const override {
return "CGRA YAML/ASM gen (multi-hop routers + endpoint register deposit + timing-aware rewiring, with CTRL_MOV kept).";
}
void getDependentDialects(DialectRegistry ®istry) const override {
registry.insert<mlir::func::FuncDialect>();
}
DenseMap<Operation*, TileLocation> operation_placements;
// Maps of tile coordinates (x,y) -> time_step -> vector of Instructions.
std::map<std::pair<int,int>, std::map<int, std::vector<Instruction>>> tile_time_instructions;
// Back references from IR operations to emitted instructions.
DenseMap<Operation*, InstructionReference> operation_to_instruction_reference;
DenseMap<Operation*, SmallVector<Value>> operation_to_operands;
// Map dfg_id -> op for later adjustments.
DenseMap<int, Operation*> dfg_id_to_op;
// Map (col,row,index_per_ii,local_idx_in_bucket) -> global instruction id.
std::map<std::tuple<int,int,int,int>, int> instruction_id_map;
int next_instruction_id = 0;
int current_compiled_ii = -1;
bool timing_field_error = false;
// De-dup sets.
std::unordered_set<uint64_t> hop_signatures; // (midTileId, time_step, link_id).
std::unordered_set<uint64_t> deposit_signatures; // (dstTileId, time_step, regId).
std::unordered_set<uint64_t> egress_signatures; // (srcTileId, time_step, regId, out_dir).
// ---------- helpers to place materialized instructions ----------.
void placeRouterHop(const Topology &topology, int tile_id, int time_step,
StringRef input_direction, StringRef output_direction,
bool asCtrlMov = false, int assigned_id = -1) {
auto [tile_x, tile_y] = topology.tile_location.lookup(tile_id);
Instruction instruction(asCtrlMov ? "CTRL_MOV" : "DATA_MOV");
instruction.id = assigned_id;
instruction.time_step = time_step;
instruction.index_per_ii = indexPerIiFromTimeStep(time_step);
instruction.invalid_iterations = syntheticInvalidIterations(time_step);
instruction.src_operands.emplace_back(input_direction.str(), "RED");
instruction.dst_operands.emplace_back(output_direction.str(), "RED");
tile_time_instructions[{tile_x, tile_y}][instruction.index_per_ii].push_back(std::move(instruction));
}
// ---------- initialization helpers ----------.
void clearState() {
operation_placements.clear();
tile_time_instructions.clear();
operation_to_instruction_reference.clear();
operation_to_operands.clear();
dfg_id_to_op.clear();
hop_signatures.clear();
deposit_signatures.clear();
egress_signatures.clear();
instruction_id_map.clear();
next_instruction_id = 0;
timing_field_error = false;
}
std::pair<int, int> getArrayDimensions(func::FuncOp function) {
const Architecture &architecture = mlir::neura::getArchitecture();
int columns = architecture.getPerCgraColumns();
int rows = architecture.getPerCgraRows();
if (auto mapping_info = function->getAttrOfType<DictionaryAttr>(attr::kMappingInfo)) {
if (auto x_tiles = dyn_cast_or_null<IntegerAttr>(mapping_info.get(attr::kXTiles))) columns = x_tiles.getInt();
if (auto y_tiles = dyn_cast_or_null<IntegerAttr>(mapping_info.get(attr::kYTiles))) rows = y_tiles.getInt();
}
return {columns, rows};
}
int getCompiledII(func::FuncOp function) {
if (auto mapping_info = function->getAttrOfType<DictionaryAttr>(attr::kMappingInfo)) {
if (auto compiled_ii = dyn_cast_or_null<IntegerAttr>(mapping_info.get(attr::kCompiledII))) {
return compiled_ii.getInt();
}
}
return -1;
}
// Derives index_per_ii from an absolute time_step; falls back to time_step when II is unknown.
int indexPerIiFromTimeStep(int time_step) const {
return current_compiled_ii > 0 ? time_step % current_compiled_ii : time_step;
}
int syntheticInvalidIterations(int time_step) const {
return current_compiled_ii > 0 ? time_step / current_compiled_ii : 0;
}
// Extracts index_per_ii and invalid_iterations from mapping_locs.
bool getIndexAndInvalid(Operation *op, int &index_per_ii, int &invalid_iterations) {
index_per_ii = -1;
invalid_iterations = 0;
bool has_index = false, has_invalid = false;
if (auto arr = op->getAttrOfType<ArrayAttr>("mapping_locs")) {
for (Attribute a : arr) {
auto dict = dyn_cast<DictionaryAttr>(a);
if (!dict) continue;
if (auto idx_attr = dyn_cast_or_null<IntegerAttr>(dict.get("index_per_ii"))) {
index_per_ii = idx_attr.getInt();
has_index = true;
}
if (auto inv_attr = dyn_cast_or_null<IntegerAttr>(dict.get("invalid_iterations"))) {
invalid_iterations = inv_attr.getInt();
has_invalid = true;
}
}
}
if (!has_index || !has_invalid) {
std::string loc_str;
llvm::raw_string_ostream rso(loc_str);
rso << op->getLoc();
rso.flush();
std::string op_name = op->getName().getStringRef().str();
std::stringstream errMsg;
errMsg << "Operation '" << op_name << "' at " << loc_str
<< " missing index_per_ii or invalid_iterations in mapping_locs";
op->emitError(errMsg.str());
timing_field_error = true;
return false;
}
return true;
}
// ---------- Single-walk indexing ----------.
// Do everything that needs walks in a single pass:.
// - record operation_placements.
// - materialize compute/phi/const instructions.
// - collect DATA_MOV and CTRL_MOV ops.
// - collect reserve_to_phi_maps (PHI's operand#0 is the reserve).
void indexIR(func::FuncOp function,
SmallVector<Operation*> &data_movs,
SmallVector<Operation*> &ctrl_movs,
DenseMap<Value, Operation*> &reserve_to_phi_map) {
function.walk([&](Operation *op) {
// Skips operations inside fused_op regions.
if (op->getParentOp() && isFusedOp(op->getParentOp())) {
return;
}
// Records Records placement for every op (even for mov/reserve).
operation_placements[op] = getTileLocation(op);
// Builds reserve -> phi mapping for loop-carried dependencies.
if (isPhiStart(op)) {
if (Value reserve = getReserveOperand(op)) {
reserve_to_phi_map[reserve] = op;
}
}
// Collects forwarders for later expansion.
if (isDataMov(op)) { data_movs.push_back(op); return; }
if (isCtrlMov(op)) { ctrl_movs.push_back(op); return; }
// Skips Reserve from materialization.
if (isReserve(op)) return;
// Materializes all other ops placed on tiles (compute/phi/const/fused_op/etc.).
TileLocation placement = operation_placements[op];
if (!placement.has_tile) return;
std::string opcode = getOpcode(op);
Instruction inst(opcode);
inst.id = getDfgId(op);
inst.time_step = placement.time_step;
if (isConstant(op)) {
inst.src_operands.emplace_back(getConstantLiteral(op), "RED");
} else if (op->getAttr(attr::kConstantValue)) {
// Checks if operation has constant_value attribute (for non-CONSTANT operations).
inst.src_operands.emplace_back(getConstantLiteral(op), "RED");
} else {
// Handles normal operands, including operations with rhs_value attribute.
SmallVector<Value> operands; operands.reserve(op->getNumOperands());
// Processes actual Value operands (if any).
for (Value v : op->getOperands()) {
operands.push_back(v);
inst.src_operands.emplace_back("UNRESOLVED", "RED");
}
// Handles cases where binary operations have the RHS constant stored as an attribute.
if (auto rhs_value_attr = op->getAttr(attr::kRhsValue)) {
std::string rhs_literal = extractConstantLiteralFromAttr(rhs_value_attr);
if (!rhs_literal.empty()) {
inst.src_operands.emplace_back(rhs_literal, "RED");
}
}
operation_to_operands[op] = std::move(operands);
}
if (auto mapped_register_id = getMappedRegId(op))
inst.dst_operands.emplace_back("$" + std::to_string(*mapped_register_id), "RED");
int index_per_ii = -1, invalid_iterations = 0;
if (!getIndexAndInvalid(op, index_per_ii, invalid_iterations)) return;
inst.index_per_ii = index_per_ii;
inst.invalid_iterations = invalid_iterations;
auto &bucket = getInstructionBucket(placement.col_idx, placement.row_idx, index_per_ii);
bucket.push_back(std::move(inst));
operation_to_instruction_reference[op] =
InstructionReference{placement.col_idx, placement.row_idx, index_per_ii,
(int)bucket.size() - 1};
});
}
// ---------- unified forwarder expansion helpers ----------.
static SmallVector<LinkStep, 8> getLinkChain(Operation *forwarder) { return collectLinkSteps(forwarder); }
static SmallVector<RegStep, 4> getRegisterSteps(Operation *forwarder) { return collectRegSteps(forwarder); }
// Validates forwarder op arities: DATA_MOV: at least 1 in/1 out; CTRL_MOV: at least 2 inputs (src,reserve).
template<bool IsCtrl>
bool validateForwarderShape(Operation *forwarder) {
if constexpr (!IsCtrl) {
return forwarder->getNumOperands() >= 1 && forwarder->getNumResults() >= 1;
} else {
return forwarder->getNumOperands() >= 2;
}
}
// Computes producer first-hop directions and consumer last-hop directions (or LOCAL if link-less).
std::pair<StringRef, StringRef> computeDirections(const SmallVector<LinkStep, 8> &links, const Topology &topo) {
StringRef producer_direction("LOCAL");
StringRef consumer_direction("LOCAL");
if (!links.empty()) {
producer_direction = topo.dirFromLink(links.front().link_id);
consumer_direction = topo.invertDir(topo.dirFromLink(links.back().link_id));
}
return {producer_direction, consumer_direction};
}
// Adds producer endpoints:
// - If src_reg_step exists: producer writes ONLY to $src_reg (directional port is emitted by egress at first_link_ts).
// - Else: producer writes to producer_direction if link-based, or $reg for reg-only paths.
void setProducerDestination(Operation *producer, StringRef producer_direction,
const SmallVector<RegStep, 4> ®s,
std::optional<RegStep> src_reg_step) {
if (auto *pi = getInstructionPointer(producer)) {
if (src_reg_step) {
// This mov uses src_reg + egress to send on link.time_step. We still must NOT
// delete existing directional outputs, because the same producer may
// fan-out to other consumers via other mov paths.
setUniqueDestination(pi, "$" + std::to_string(src_reg_step->regId));
return;
}
if (!producer_direction.empty() && producer_direction != "LOCAL")
setUniqueDestination(pi, producer_direction.str());
else if (!regs.empty())
setUniqueDestination(pi, "$" + std::to_string(regs.back().regId));
}
}
// Egress: on source tile at first_link_ts, move [$src_reg] -> [out_dir].
void placeSrcEgress(const Topology &topology, int src_tile_id, int time_step,
StringRef out_dir, int reg_id,
bool asCtrlMov = false, int assigned_id = -1) {
// Signature must be stable and support arbitrary direction strings (arch-spec dependent).
uint64_t signature =
static_cast<uint64_t>(llvm::hash_combine(src_tile_id, time_step, reg_id, out_dir));
if (!egress_signatures.insert(signature).second) return;
auto [tile_x, tile_y] = topology.tile_location.lookup(src_tile_id);
Instruction inst(asCtrlMov ? "CTRL_MOV" : "DATA_MOV");
inst.id = assigned_id;
inst.time_step = time_step;
inst.index_per_ii = indexPerIiFromTimeStep(time_step);
inst.invalid_iterations = syntheticInvalidIterations(time_step);
inst.src_operands.emplace_back("$" + std::to_string(reg_id), "RED");
inst.dst_operands.emplace_back(out_dir.str(), "RED");
tile_time_instructions[{tile_x, tile_y}][inst.index_per_ii].push_back(std::move(inst));
}
struct MovRegSplit {
std::optional<RegStep> src_reg_step; // last reg with time_step < first_link_time_step
std::optional<RegStep> dst_reg_step; // first reg with time_step > last_link_time_step
};
MovRegSplit splitMovRegs(const SmallVector<RegStep, 4> ®s,
const SmallVector<LinkStep, 8> &links) const {
MovRegSplit out;
if (regs.empty() || links.empty()) return out;
int first_link_time_step = links.front().time_step;
int last_link_time_step = links.back().time_step;
for (const RegStep &r : regs) {
if (r.time_step < first_link_time_step) out.src_reg_step = r;
if (!out.dst_reg_step && r.time_step > last_link_time_step) out.dst_reg_step = r;
}
return out;
}
// Emits router hops for multi-hop paths (from the second hop onwards). CTRL_MOV emits CTRL_MOV hops.
template<bool IsCtrl>
void generateIntermediateHops(const SmallVector<LinkStep, 8> &links, const Topology &topo,
int base_mov_id, size_t &hop_counter) {
for (size_t i = 1; i < links.size(); ++i) {
int prev_link = links[i - 1].link_id;
int cur_link = links[i].link_id;
int time_step = links[i].time_step;
int mid_tile = topo.srcTileOfLink(cur_link);
StringRef in = topo.invertDir(topo.dirFromLink(prev_link));
StringRef out = topo.dirFromLink(cur_link);
uint64_t sig = static_cast<uint64_t>(llvm::hash_combine(mid_tile, time_step, cur_link));
if (hop_signatures.insert(sig).second) {
int hop_id = base_mov_id >= 0 ? base_mov_id * 10000 + static_cast<int>(hop_counter) : -1;
++hop_counter;
placeRouterHop(topo, mid_tile, time_step, in, out, /*asCtrlMov=*/IsCtrl, hop_id);
}
}
}
// Consumers for DATA_MOV: all users of forwarder results(0).
SmallVector<std::pair<Operation*, Value>, 2> collectDataMovConsumers(Operation *forwarder) {
SmallVector<std::pair<Operation*, Value>, 2> consumers;
Value out = forwarder->getResult(0);
for (OpOperand &use : out.getUses())
consumers.push_back({use.getOwner(), use.get()});
return consumers;
}
// Consumers for CTRL_MOV: find PHI via reserve->phi maps; wire the PHI's *data* inputs (sources).
SmallVector<std::pair<Operation*, Value>, 2> collectCtrlMovConsumers(Operation *forwarder,
const DenseMap<Value, Operation*> &reserve2phi) {
SmallVector<std::pair<Operation*, Value>, 2> consumers;
Value reserve = forwarder->getOperand(1);
if (Operation *phi = reserve2phi.lookup(reserve))
consumers.push_back({phi, reserve});
else
forwarder->emitWarning("ctrl_mov dest is not consumed by a PHI operand#0; skipping.");
return consumers;
}
// Try register-based rewiring. If cross-tile, emit deposits [incoming_dir]->[$reg] at earliest reg time_step.
// Returns true if rewiring to $reg was applied to consumers.
template<bool IsCtrl>
bool handleRegisterRewiring(Operation *consumer_operation, Value value_at_consumer,
const SmallVector<RegStep, 4> ®s,
std::optional<RegStep> dst_reg_step,
const SmallVector<LinkStep, 8> &links, const Topology &topo, int mov_dfg_id) {
if (!links.empty()) {
// Cross-tile: deposit on destination tile at dst_reg_step (post-link register).
if (!dst_reg_step) return false;
int deposit_time_step = dst_reg_step->time_step;
int register_id = dst_reg_step->regId;
int dst_tile = topo.dstTileOfLink(links.back().link_id);
// Computes incoming direction from destination tile's perspective.
StringRef incoming_dir = topo.invertDir(topo.dirFromLink(links.back().link_id));
placeDstDeposit(topo, dst_tile, deposit_time_step, incoming_dir, register_id,
/*asCtrlMov=*/IsCtrl, mov_dfg_id);
TileLocation consumer_placement = operation_placements.lookup(consumer_operation);
if (consumer_placement.has_tile) {
// For CTRL_MOV, the destination register often represents a stateful value (reserve/control)
// that must be consumed via a local register even if the consumer's time_step is earlier
// (e.g., prologue reads default, later iterations read updated).
const bool should_rewire_to_register =
IsCtrl || (consumer_placement.time_step > deposit_time_step);
if (should_rewire_to_register) {
setConsumerSourceExact(consumer_operation, value_at_consumer,
"$" + std::to_string(register_id));
return true;
}
}
} else {
// Same-tile: must go via register.
if (regs.empty()) return false;
int register_id = regs.back().regId;
setConsumerSourceExact(consumer_operation, value_at_consumer, "$" + std::to_string(register_id));
return true;
}
return false;
}
template<bool IsCtrl>
void handleDirectionRewiring(Operation *consumer_operation, Value value_at_consumer, StringRef consumer_direction,
const SmallVector<LinkStep, 8> &links, const Topology &topo,
Operation *forwarder) {
if (!links.empty()) {
// Computes the direction from the link destination tile to the consumer tile.
TileLocation consumer_placement = operation_placements.lookup(consumer_operation);
if (consumer_placement.has_tile) {
int dst_tile_id = topo.dstTileOfLink(links.back().link_id);
int consumer_tile_id = topo.tileIdAt(consumer_placement.col_idx, consumer_placement.row_idx);
// If consumer is on the link destination tile, use the incoming direction.
if (consumer_tile_id == dst_tile_id) {
setConsumerSourceExact(consumer_operation, value_at_consumer, consumer_direction.str());
} else {
// Computes direction from link destination tile to consumer tile.
StringRef actual_dir = topo.invertDir(topo.getDirBetween(dst_tile_id, consumer_tile_id));
setConsumerSourceExact(consumer_operation, value_at_consumer, actual_dir.str());
}
} else {
// Falls back to consumer_direction if consumer placement is unknown.
setConsumerSourceExact(consumer_operation, value_at_consumer, consumer_direction.str());
}
} else {
forwarder->emitError(IsCtrl
? "same-tile ctrl_mov without register mapping is illegal. Provide a register in mapping_locs."
: "same-tile data_mov without register mapping is illegal. Provide a register in mapping_locs.");
assert(false && "same-tile mov without register mapping");
}
}
template<bool IsCtrl>
struct MovBasics {
int mov_dfg_id = -1;
Operation *producer = nullptr;
SmallVector<LinkStep, 8> links;
SmallVector<RegStep, 4> regs;
MovRegSplit reg_split;
StringRef producer_direction;
StringRef consumer_direction;
};
template<bool IsCtrl>
MovBasics<IsCtrl> buildMovBasics(Operation *forwarder, const Topology &topo) {
MovBasics<IsCtrl> basics;
basics.mov_dfg_id = getDfgId(forwarder);
// Basic info from forwarders.
Value source = forwarder->getOperand(0);
basics.producer = source.getDefiningOp();
basics.links = getLinkChain(forwarder);
basics.regs = getRegisterSteps(forwarder);
basics.reg_split = splitMovRegs(basics.regs, basics.links);
std::pair<StringRef, StringRef> directions = computeDirections(basics.links, topo);
basics.producer_direction = directions.first;
basics.consumer_direction = directions.second;
return basics;
}
template<bool IsCtrl>
void emitMovRoutingInstructions(Operation *forwarder, const MovBasics<IsCtrl> &basics,
const Topology &topo) {
// Producer endpoints & intermediate hops.
setProducerDestination(basics.producer, basics.producer_direction, basics.regs, basics.reg_split.src_reg_step);
if (!basics.links.empty() && basics.reg_split.src_reg_step) {
int src_tile = topo.srcTileOfLink(basics.links.front().link_id);
int first_link_time_step = basics.links.front().time_step;
int egress_instruction_id = basics.mov_dfg_id >= 0 ? basics.mov_dfg_id * 10000 : -1;
placeSrcEgress(topo, src_tile, first_link_time_step, basics.producer_direction,
basics.reg_split.src_reg_step->regId,
/*asCtrlMov=*/IsCtrl, egress_instruction_id);
}
size_t hop_counter = 1;
generateIntermediateHops<IsCtrl>(basics.links, topo, basics.mov_dfg_id, hop_counter);
}
template<bool IsCtrl>
SmallVector<std::pair<Operation*, Value>, 2>
collectMovConsumers(Operation *forwarder, const DenseMap<Value, Operation*> &reserve2phi) {
if constexpr (IsCtrl) {
return collectCtrlMovConsumers(forwarder, reserve2phi);
} else {
return collectDataMovConsumers(forwarder);
}
}
template<bool IsCtrl>
void rewriteMovConsumers(Operation *forwarder,
const MovBasics<IsCtrl> &basics,
const SmallVector<std::pair<Operation*, Value>, 2> &consumers,
const Topology &topo) {
// Wires each consumer: prefer register rewiring; fallback to direction rewiring.
for (const std::pair<Operation*, Value> &consumer_pair : consumers) {
Operation *consumer_operation = consumer_pair.first;
Value value_at_consumer = consumer_pair.second;
if (!handleRegisterRewiring<IsCtrl>(consumer_operation, value_at_consumer, basics.regs,
basics.reg_split.dst_reg_step,
basics.links, topo, basics.mov_dfg_id))
handleDirectionRewiring<IsCtrl>(consumer_operation, value_at_consumer, basics.consumer_direction,
basics.links, topo, forwarder);
}
}
template<bool IsCtrl>
void expandMovImpl(Operation *forwarder, const Topology &topo,
const DenseMap<Value, Operation*> &reserve2phi) {
if (!validateForwarderShape<IsCtrl>(forwarder)) return;
// Checks if this data_mov/ctrl_mov has mapping_locs assigned by MapToAcceleratorPass.
auto mapping_locs = getMappingLocations(forwarder);
if (!mapping_locs || mapping_locs.empty()) {
// Skips this mov operation - it will be handled by its consumer or does not need routing.
// This is expected for data_mov that only feeds into ctrl_mov.
if constexpr (!IsCtrl) {
// For data_mov without mapping, verifies if it is only used by ctrl_mov.
bool only_ctrl_mov_users = true;
for (OpOperand &use : forwarder->getResult(0).getUses()) {
if (!isa<CtrlMovOp>(use.getOwner())) {
only_ctrl_mov_users = false;
break;
}
}
if (only_ctrl_mov_users) {
// This is expected - ctrl_mov handles this data transfer implicitly.
return;
} else {
// This data_mov has non-ctrl_mov users but no mapping - this is an error.
forwarder->emitWarning("data_mov without mapping_locs has non-ctrl_mov users");
}
}
return;
}
MovBasics<IsCtrl> basics = buildMovBasics<IsCtrl>(forwarder, topo);
emitMovRoutingInstructions<IsCtrl>(forwarder, basics, topo);
SmallVector<std::pair<Operation*, Value>, 2> consumers =
collectMovConsumers<IsCtrl>(forwarder, reserve2phi);
if constexpr (IsCtrl) {
if (consumers.empty()) return;
}
rewriteMovConsumers<IsCtrl>(forwarder, basics, consumers, topo);
}
// ---------- output generation ----------.
void logUnresolvedOperands() {
unsigned unsrc = 0, undst = 0;
for (auto &tile_entry : tile_time_instructions) {
std::pair<int,int> tile_key = tile_entry.first;
int column = tile_key.first, row = tile_key.second;
for (auto ×tep_entry : tile_entry.second) {
int time_step = timestep_entry.first;
std::vector<Instruction> &vec = timestep_entry.second;
for (size_t i = 0; i < vec.size(); ++i) {
Instruction &inst = vec[i];
for (size_t si = 0; si < inst.src_operands.size(); ++si) {
Operand &s = inst.src_operands[si];
if (s.operand == "UNRESOLVED") {
s.color = "ERROR"; ++unsrc;
llvm::errs() << "[UNRESOLVED SRC] tile("<<column<<","<<row<<") t="<<time_step
<< " inst#" << i << " op=" << inst.opcode
<< " src_idx=" << si << "\n";
}
}
inst.dst_operands.erase(
std::remove_if(inst.dst_operands.begin(), inst.dst_operands.end(),
[](const Operand &o){ return o.operand.empty() || o.operand=="UNKNOWN"; }),
inst.dst_operands.end());
for (size_t di = 0; di < inst.dst_operands.size(); ++di) {
Operand &d = inst.dst_operands[di];
if (d.operand == "UNRESOLVED") {
d.color = "ERROR"; ++undst;
llvm::errs() << "[UNRESOLVED DST] tile("<<column<<","<<row<<") t="<< time_step
<< " inst#" << i << " op=" << inst.opcode
<< " dst_idx=" << di << "\n";
}
}
}
}
}
if (unsrc + undst) {
ModuleOp module = getOperation();
auto diag = module.emitWarning("GenerateCodePass: UNRESOLVED operands kept for debugging");
diag << " (src=" << unsrc << ", dst=" << undst << "); they are highlighted with color=ERROR in YAML.";
}
}
// Assigns unique IDs to all materialized instructions (including data/ctrl mov hops).
void assignInstructionIds(std::unordered_set<int> &materialized_ids) {
instruction_id_map.clear();
int max_assigned = -1;
for (auto &[tile_key, timestep_map] : tile_time_instructions) {
for (auto &[time_step, inst_vec] : timestep_map) {
for (Instruction &inst : inst_vec) {
if (inst.id >= 0) max_assigned = std::max(max_assigned, inst.id);
}
}
}
next_instruction_id = max_assigned + 1;
for (auto &[tile_key, timestep_map] : tile_time_instructions) {
int col = tile_key.first;
int row = tile_key.second;
for (auto &[time_step, inst_vec] : timestep_map) {
for (size_t idx = 0; idx < inst_vec.size(); ++idx) {
Instruction &inst = inst_vec[idx];
if (inst.id < 0) inst.id = next_instruction_id++;
instruction_id_map[{col, row, time_step, (int)idx}] = inst.id;
materialized_ids.insert(inst.id);
}
}
}
}
// Looks up instruction ID by InstructionReference (col,row,time_step,idx in bucket).
int lookupInstructionId(const InstructionReference &ref) const {
auto it = instruction_id_map.find({ref.col_idx, ref.row_idx, ref.t, ref.idx});
if (it == instruction_id_map.end()) return -1;
return it->second;
}
// Helper to escape strings for DOT/JSON.
static std::string escape(const std::string &s) {
std::string out;
out.reserve(s.size());
for (char c : s) {
if (c == '"') out += "\\\"";
else if (c == '\\') out += "\\\\";
else if (c == '\n') out += "\\n";
else out += c;
}
return out;
}
// Helper to extract dfg_id from operation.
static int getDfgId(Operation *op) {
if (auto id_attr = op->getAttrOfType<IntegerAttr>(attr::kDfgId)) {
return id_attr.getInt();
}
return -1;
}
// Helper to extract tile coordinates and time_step from mapping_locs.
struct LocationInfo {
int tile_x = -1;
int tile_y = -1;
int time_step = -1;
int index_per_ii = -1;
int invalid_iterations = 0;
bool has_tile = false;
};
static LocationInfo getLocationInfo(Operation *op) {
LocationInfo info;
if (auto arr = op->getAttrOfType<ArrayAttr>("mapping_locs")) {
for (Attribute a : arr) {
auto d = dyn_cast<DictionaryAttr>(a);
if (!d) continue;
auto resource = dyn_cast_or_null<StringAttr>(d.get("resource"));
// Extracts time_step from any resource type.
if (auto ts_attr = dyn_cast_or_null<IntegerAttr>(d.get("time_step"))) {
info.time_step = ts_attr.getInt();
}
if (auto index_attr = dyn_cast_or_null<IntegerAttr>(d.get("index_per_ii"))) {
info.index_per_ii = index_attr.getInt();