-
Notifications
You must be signed in to change notification settings - Fork 5
/
externoptsdt.cc
579 lines (517 loc) · 14.5 KB
/
externoptsdt.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
#include "externoptsdt.h"
#include <algorithm>
#include <string.h>
/* id = expr_id for evaluating this expression */
void ExternOptSDT::_emit_expr (int *id, int tgt_width, Expr *e)
{
int width = 0;
int myid;
// a list of all the inputs
list_t *all_leaves;
listitem_t *li;
// the metadata item given back by the syntesis tool.
ExprBlockInfo *block_info;
/*-- recursively expand expression --*/
if (!e) {
fatal_error ("Emit NULL expression?!");
}
if (!_efp) {
fatal_error ("ExternOptSDT: requires block expression mode");
}
if (!mapper) {
mapper = new ExternalExprOpt(_map,
bundled_data ? bd : qdi,
bundled_data ? true : false,
_exprfile, "e", "blk");
}
Assert (mapper, "Could not create mapper!");
// make sure the maps are empty and create new ones
//Assert (!_inexprmap, "What?");
_inexprmap = ihash_new (0);
_inwidthmap = ihash_new (0);
// run through the expr data structure and collect all variables and constants
_expr_collect_vars (e, 1);
// fill up all the leaves list with all the elements in the inexprmap
all_leaves = list_new ();
{
ihash_iter_t iter;
ihash_bucket_t *ib;
ihash_iter_init (_inexprmap, &iter);
while ((ib = ihash_iter_next (_inexprmap, &iter))) {
Expr *e = (Expr *)ib->key;
list_append (all_leaves, e);
}
}
// generate block id and check if we have a expression file to write the blocks too
int xid = _gen_expr_blk_id ();
if (!_efp)
{
fatal_error("need expr file for optimisation mode");
}
/*-- emit leaves --*/
_expr_collect_vars (e, 0);
//convert std expr channel to array of expr channels
if (bundled_data != 1)
{
for (li = list_first (all_leaves); li; li = list_next (li))
{
ihash_bucket_t *bucket_id = ihash_lookup(_inexprmap, (long) list_value (li));
ihash_bucket_t *bucket_w = ihash_lookup(_inwidthmap, (long) list_value (li));
if (bucket_w->i > 1){
int new_id = _gen_expr_id();
_emit_expr_array_wrap(new_id, bucket_id->i, bucket_w->i);
bucket_id->i = new_id;
}
}
}
fclose (_efp);
/*-- emit expression --*/
block_info = mapper->run_external_opt(xid,tgt_width,e,all_leaves,_inexprmap,_inwidthmap);
_efp = fopen (_exprfile, "a");
// the current id of the output instance
*id = _gen_expr_id ();
// instanciate the expr block object in the main file
{
listitem_t *li;
list_t *ids;
ihash_iter_t iter;
ihash_bucket_t *ib;
ids = list_new ();
_emit_expr_block (*id, xid, all_leaves);
}
// do the conversion from wires/arraychannels to channels and for BD wrap the ctl arround
fprintf (output_stream," // wrap and ctl bypass\n");
int number_of_leaves = 0;
/*-- wrapping only - width conversion is done by the syntesis --*/
myid = _gen_expr_id ();
_emit_expr_width_conv (*id, width, myid, tgt_width);
*id = myid;
/*-- handshake bypass --*/
if (bundled_data == 1)
{
double delay;
if (block_info->getDelay().exists()) {
delay = block_info->getDelay().max_val;
if (delay == 0) {
delay = block_info->getDelay().typ_val;
}
if (delay == 0) {
delay = block_info->getDelay().min_val;
}
}
else {
delay = 100e-9;
}
_emit_bd_ctl_bypass (*id, all_leaves, delay);
}
fprintf (output_stream," //end expr blk%u\n",xid);
// free all temporary data structures
ihash_free (_inexprmap);
_inexprmap = NULL;
ihash_free (_inwidthmap);
_inwidthmap = NULL;
list_free (all_leaves);
// force write output file
fflush(output_stream);
}
void ExternOptSDT::_emit_bd_ctl_bypass (int id, list_t *all_leaves, double delay_max)
{
// distribute the request signal off the pull channel
// in case of BD connect all the ack signals via a c element tree
listitem_t *li;
int number_of_leaves = 0;
for (li = list_first (all_leaves); li; li = list_next (li))
{
fprintf(output_stream, " e%u.out.r = e%u.out.r;\n", ihash_lookup(_inexprmap, (long) list_value (li))->i, id);
number_of_leaves++;
}
if (number_of_leaves > 0)
{
int index = 0;
int stages = 50;
fprintf(output_stream, " std::gates::ctree<%u,false> ackmerge%u;\n", number_of_leaves, id);
fprintf (output_stream, " /* delay: %g */\n", delay_max);
if (delay_max != -1) {
double delay_units;
if (config_exists ("net.delay")) {
delay_units = config_get_real ("net.delay");
}
else {
delay_units = config_get_real ("net.lambda")*1e-3;
}
stages = delay_max / delay_units;
if (delay_max != 0) {
if (stages < 1) {
stages = 1;
}
}
}
fprintf(output_stream, " syn::delay<%d> delayblk%u (ackmerge%u.out, e%u.out.a);\n", stages, id, id, id);
for (li = list_first (all_leaves); li; li = list_next (li))
{
fprintf(output_stream, " e%u.out.a = ackmerge%u.in[%u];\n", ihash_lookup(_inexprmap, (long) list_value (li))->i, id, index);
index++;
}
}
else if (number_of_leaves == 0)
{
fprintf(output_stream, " e%u.out.a = e%u.out.r;\n", id, id);
}
}
void ExternOptSDT::_emit_expr_block (int id, int blkid, list_t *exprs)
{
// instanciate the expr block in the main file and connect all the input signals
listitem_t *li;
fprintf (output_stream, " syn::expr::blk%d e%d(", blkid, id);
// each element in this list is a input expr object, the map is uesd to look up the coresponding ID.
for (li = list_first (exprs); li; li = list_next (li)) {
if (li != list_first (exprs)) {
fprintf (output_stream, ", ");
}
// qdi and bd have different channel and wire names
if (bundled_data != 1) fprintf (output_stream, "e%d.out", ihash_lookup(_inexprmap, (long) list_value (li))->i);
else
{
fprintf (output_stream, "e%d.out.d", ihash_lookup(_inexprmap, (long) list_value (li))->i);
// if the signal is 1 bit wide provide the actuall wire not the array
if (ihash_lookup(_inwidthmap, (long) list_value (li))->i == 1) fprintf (output_stream, "[0]");
}
}
fprintf (output_stream, ");\n");
}
void ExternOptSDT::_emit_expr_array_wrap(int new_id, int old_id, int width)
{
if (width > 1 ) {
fprintf (output_stream, " syn::expr::wrap_to_array<%d> e%d(e%d.out);\n", width, new_id, old_id);
}
else
{ // nothing todo is already in correct form, should not go here should be handled idealy before, but if not this creates and alias
fprintf(output_stream, " syn::expr::null e%u(e%u.out);\n", new_id, old_id);
}
}
void ExternOptSDT::_emit_expr_width_conv (int from, int from_w, int to, int to_w)
{
//for bundled data from_w is 0, for qdi now too, width conversion is heandled by syntesis tool
if (bundled_data == 1)
{
// catch the case of the single bit wire, dont use an array in this case provide the acctual wire
if (to_w == 1)
{
fprintf(output_stream, " syn::expr::null e%u();\n", to);
fprintf(output_stream, " e%u.out.d[0]=e%u.out;\n", to, from);
}
else
{
fprintf(output_stream, " syn::expr::nullint<%u> e%u();\n", to_w, to);
fprintf(output_stream, " e%u.out.d=e%u.out;\n", to, from);
}
}
else
{
// catch width 1 because output is than not an array but a single channel that is fine
if (to_w > 1 ) {
fprintf (output_stream, " syn::expr::wrap_from_array<%d> e%d(e%d.out);\n", to_w, to, from);
}
else
{ // nothing todo is already in correct form, should not go here should be handled idealy before, but if yes this creates and alias
fprintf(output_stream, " syn::expr::null e%u(e%u.out);\n", to, from);
}
}
}
int ExternOptSDT::get_expr_width(Expr *ex) {
// recusivly run through the expression and collect its width
switch ((ex)->type)
{
// for a var read the bitwidth of that var
case E_VAR:
{
varmap_info *v;
v = _var_getinfo ((ActId *)(ex)->u.e.l);
return v->width;
}
// for true and false the bit width is one
case E_TRUE:
case E_FALSE:
return 1;
// for int look up the corresponding bitwidth
case E_INT:
{
return ihash_lookup(_inwidthmap, (long) ex)->i;
}
// step through
case E_QUERY:
ex = ex->u.e.r;
// get the max out of the right and the left expr part
case E_AND:
case E_OR:
case E_XOR:
{
int lw = get_expr_width(ex->u.e.l);
int rw = get_expr_width(ex->u.e.r);
return std::max(lw,rw);
}
// get the max out of the right and the left expr part and one for the overflow bit
case E_PLUS:
case E_MINUS:
{
int lw = get_expr_width(ex->u.e.l);
int rw = get_expr_width(ex->u.e.r);
return std::max(lw,rw);
// @TODO genus trys to tie the top bit and i dont know why
// return std::max(lw,rw)+1;
}
// comparisons result in a bool so 1, do not walk further
case E_LT:
case E_GT:
case E_LE:
case E_GE:
case E_EQ:
case E_NE:
// should be fine in ignoring the rest of the expr
return 1;
// for multiplication add both operand bitwidth
case E_MULT:
{
int lw = get_expr_width(ex->u.e.l);
int rw = get_expr_width(ex->u.e.r);
return lw+rw;
}
// step through
case E_MOD:
{
int rw = get_expr_width(ex->u.e.r);
return rw;
}
// use left bitwidth and add number of shifted right
case E_LSL:
{
int lw = get_expr_width(ex->u.e.l);
int rw = get_expr_width(ex->u.e.r);
return lw + (1 << rw);
}
// pass through
case E_DIV:
case E_LSR:
case E_ASR:
case E_UMINUS:
case E_NOT:
case E_COMPLEMENT:
{
int lw = get_expr_width(ex->u.e.l);
return lw;
}
//get the value out of the datastructure
case E_BUILTIN_INT:
if (ex->u.e.r) {
Assert (ex->u.e.r->type == E_INT, "What?");
return ex->u.e.r->u.ival.v;
}
else {
return 1;
}
case E_BUILTIN_BOOL:
return 1;
// the following ones should give you errors because not handled
case E_COLON:
case E_COMMA:
fatal_error ("Should have been handled elsewhere");
break;
/* XXX: here */
case E_CONCAT:
{
int w = 0;
Expr *tmp = ex;
while (tmp) {
w += get_expr_width (tmp->u.e.l);
tmp = tmp->u.e.r;
}
return w;
}
break;
case E_BITFIELD:
_var_getinfo ((ActId *)ex->u.e.l);
if (ex->u.e.r->u.e.l) {
return (ex->u.e.r->u.e.r->u.ival.v - ex->u.e.r->u.e.l->u.ival.v + 1);
}
else {
return 1;
}
break;
case E_REAL:
fatal_error ("No real expressions please.");
break;
case E_PROBE:
fatal_error ("fix probes please");
break;
case E_FUNCTION:
fatal_error ("function!");
case E_SELF:
default:
fatal_error ("Unknown expression type %d\n", ex->type);
break;
}
return 0;
}
void ExternOptSDT::_emit_expr_const (int id, int width, int val, bool forced)
{
// for QDI emmit constant
if (bundled_data != 1) fprintf (output_stream, " syn::expr::const<%d,%d> e%d;\n", width, val, id);
// for BD emmit only if its a guard for eg infinit loop
else if (forced) fprintf (output_stream, " syn::trueto1of2 e%d;\n", id);
// print for debugging BD constants, they should be handed to the syntesis tool
else fprintf (output_stream, " // would emit const e%d <%d> %d %d\n", id, width,val,forced);
}
void ExternOptSDT::_emit_guardlist (int isloop,
act_chp_gc_t *gc, list_t *res)
{
act_chp_gc_t *tmp;
int eid;
Assert (gc, "Why am I here?");
if (isloop && !gc->g) {
/*-- infinite loop --*/
eid = _gen_expr_id ();
_emit_expr_const (eid, 1, 1, true);
list_iappend (res, eid);
}
else {
tmp = gc;
while (tmp) {
_emit_one_guard_expr (tmp->g, res);
tmp = tmp->next;
}
}
}
/*
returns new eid for the safe bool
*/
int ExternOptSDT::_gen_safe_bool (int eid)
{
ActId *id;
Assert (_gen_fresh_var (1, &id), "What?");
/*
Sequence:
1. transfer expression to variable
2. read variable into expression
*/
int tid = _gen_stmt_id ();
_emit_transfer (tid, eid, id);
int fid = _gen_stmt_id ();
_emit_trueseq (fid, tid);
eid = _gen_expr_id ();
_emit_var_read (eid, id);
int eid2 = _gen_expr_id ();
if (bundled_data != 1)
{
fprintf (output_stream, " syn::expr::null e%d;\n", eid2);
fprintf (output_stream, " e%d.out.r = c%d.r; c%d.a = e%d.out.r; e%d.out.d=e%d.out.d;\n",
eid2, fid, fid, eid, eid2, eid);
}
else
{
fprintf (output_stream, " syn::bdto1of2 e%d;\n", eid2);
fprintf (output_stream, " e%d.out.r = c%d.r; c%d.a = e%d.out.r; e%d.out.d=e%d.out.d; e%d.out.a=e%d.out.a;\n",
eid2, fid, fid, eid, eid2, eid, eid2, eid);
}
return eid2;
}
void ExternOptSDT::_expr_collect_vars (Expr *e, int collect_phase)
{
int id;
Assert (e, "Hmm");
#define BINARY_OP \
do { \
_expr_collect_vars (e->u.e.l, collect_phase); \
_expr_collect_vars (e->u.e.r, collect_phase); \
} while (0)
#define UNARY_OP \
do { \
_expr_collect_vars (e->u.e.l, collect_phase); \
} while (0)
switch (e->type) {
/* binary */
case E_AND:
case E_OR:
case E_XOR:
case E_PLUS:
case E_MINUS:
case E_LT:
case E_GT:
case E_LE:
case E_GE:
case E_EQ:
case E_NE:
case E_MULT:
case E_DIV:
case E_MOD:
case E_LSL:
case E_LSR:
case E_ASR:
BINARY_OP;
break;
case E_UMINUS:
case E_NOT:
case E_COMPLEMENT:
case E_BUILTIN_INT:
case E_BUILTIN_BOOL:
UNARY_OP;
break;
case E_QUERY:
_expr_collect_vars (e->u.e.l, collect_phase);
_expr_collect_vars (e->u.e.r->u.e.l, collect_phase);
_expr_collect_vars (e->u.e.r->u.e.r, collect_phase);
break;
case E_COLON:
case E_COMMA:
fatal_error ("Should have been handled elsewhere");
break;
/* XXX: here */
case E_CONCAT:
{
Expr *tmp = e;
while (tmp) {
_expr_collect_vars (tmp->u.e.l, collect_phase);
tmp = tmp->u.e.r;
}
}
break;
case E_REAL:
fatal_error ("No real expressions please.");
break;
case E_TRUE:
break;
case E_FALSE:
break;
case E_INT:
break;
case E_BITFIELD:
case E_VAR:
if (collect_phase) {
varmap_info *v;
ihash_bucket_t *b;
v = _var_getinfo ((ActId *)e->u.e.l);
b = ihash_add (_inexprmap, (long)e);
b->i = _gen_expr_id ();
ihash_bucket_t *b_width;
b_width = ihash_add (_inwidthmap, (long) e);
b_width->i = v->width;
}
else {
ihash_bucket_t *b;
b = ihash_lookup (_inexprmap, (long)e);
_emit_var_read (b->i, (ActId *)e->u.e.l);
}
break;
case E_PROBE:
fatal_error ("fix probes please");
break;
case E_FUNCTION:
fatal_error ("function!");
case E_SELF:
default:
fatal_error ("Unknown expression type %d\n", e->type);
break;
}
return;
#undef BINARY_OP
#undef UNARY_OP
}