-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathrigel.lua
More file actions
3029 lines (2528 loc) · 121 KB
/
rigel.lua
File metadata and controls
3029 lines (2528 loc) · 121 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
local IR = require("ir")
local types = require("types")
--local ffi = require("ffi")
local S = require("systolic")
local Ssugar = require("systolicsugar")
local SDFRate = require "sdfrate"
local J = require "common"
local err = J.err
local SDF = require("sdf")
local P = require "params"
-- We can operate in 2 different modes: either we stream frames continuously (STREAMING==true), or we only do one frame (STREAMING==false).
-- If STREAMING==false, we artificially cap the output once the expected number of pixels is reached. This is needed for some test harnesses
STREAMING = false
local darkroom = {}
-- enable SDF checking? (true:enable, false:disable)
darkroom.SDF = true
darkroom.default_nettype_none = true
darkroom.MONITOR_FIFOS = false
darkroom.AUTO_FIFOS = false
darkroom.THROTTLE_FIFOS = false
darkroom.Z3_FIFOS = false -- use z3 to alloc fifos
-- path to root of rigel repo
darkroom.path = string.sub(debug.getinfo(1).source,2,#debug.getinfo(1).source-9)
DEFAULT_FIFO_SIZE = 2048*16*16
if os.getenv("v") then
DARKROOM_VERBOSE = true
else
DARKROOM_VERBOSE = false
end
local function getloc()
return debug.getinfo(3).source..":"..debug.getinfo(3).currentline.."\n"..debug.traceback()
end
darkroom.VTrigger = types.VTrigger
darkroom.RVTrigger = types.RVTrigger
darkroom.HandshakeTrigger = types.HandshakeTrigger
darkroom.V = types.V
darkroom.RV = types.RV
darkroom.Handshake = types.Handshake
darkroom.HandshakeArray = types.HandshakeArray
darkroom.HandshakeTriggerArray = types.HandshakeTriggerArray
darkroom.HandshakeTuple = types.HandshakeTuple
darkroom.HandshakeArrayOneHot = types.HandshakeArrayOneHot
darkroom.HandshakeTmuxed = types.HandshakeTmuxed
darkroom.isHandshakeArrayOneHot = types.isHandshakeArrayOneHot
darkroom.isHandshakeTmuxed = types.isHandshakeTmuxed
darkroom.isHandshake = types.isHandshake
darkroom.isHandshakeTrigger = types.isHandshakeTrigger
darkroom.isHandshakeArray = types.isHandshakeArray
darkroom.isHandshakeTriggerArray = types.isHandshakeTriggerArray
darkroom.isHandshakeTuple = types.isHandshakeTuple
darkroom.isHandshakeAny = types.isHandshakeAny
darkroom.isV = types.isV
darkroom.isVTrigger = types.isVTrigger
darkroom.isRV = types.isRV
darkroom.isRVTrigger = types.isRVTrigger
darkroom.expectBasic = types.expectBasic
darkroom.expectV = types.expectV
darkroom.expectRV = types.expectRV
darkroom.expectHandshake = types.expectHandshake
darkroom.lower = types.lower
darkroom.extractData = types.extractData
darkroom.hasReady = types.hasReady
darkroom.extractReady = types.extractReady
darkroom.extractValid = types.extractValid
darkroom.streamCount = types.streamCount
darkroom.isStreaming = types.isStreaming
darkroom.isBasic = types.isBasic
-- some basic classes
local SizeFunctions = {}
local SizeMT = {__index=SizeFunctions}
SizeMT.__tostring=function(tab) return "Size("..tostring(tab[1])..","..tostring(tab[2])..")" end
function darkroom.isSize(t) return getmetatable(t)==SizeMT end
local AddressMT = {}
function darkroom.Address(num)
assert(type(num)=="number")
return setmetatable({num},AddressMT)
end
-- generator flags
darkroom.Async={} -- generate async (0 cycle delay) module
darkroom.Unoptimized={} -- disable rate optimization (at the instance level, instead of globally)
darkroom.Size = J.memoize(function( w, h, X )
assert(X==nil)
local Uniform = require "uniform"
if darkroom.isSize(w) then
assert(h==nil)
return w
elseif (type(w)=="number" or Uniform.isUniform(w)) and (type(h)=="number" or Uniform.isUniform(h)) then
return setmetatable({w,h},SizeMT)
elseif type(w)=="table" and h==nil and w[1]~=nil and w[2]~=nil then
return darkroom.Size(w[1],w[2])
else
err(false, "Passed strange arguments to rigel.Size? w=",w," h=",h)
end
end)
-- sizes may contain uniforms
function SizeFunctions:eq(a,b)
local Uniform = require "uniform"
if darkroom.isSize(a) then
assert(b==nil)
local x = Uniform(self[1]):eq( Uniform(a[1]) )
local y = Uniform(self[2]):eq( Uniform(a[2]) )
return ( x:And(y) ):assertAlwaysTrue()
elseif type(a)=="number" and type(b)=="number" then
return self:eq(darkroom.Size(a,b))
end
assert(false)
end
local function discoverName(x)
local y = 1
while true do
local name, value = debug.getlocal(3,y)
if not name then break end
--print(name,value)
if value==x then
return name
end
y = y + 1
end
for k,v in pairs(_G) do
if v==x then
--print("FOUND_G")
return k
end
end
-- return {input=x}
end
darkroom.__unnamedID = 0
local function typeToKey(t)
local res = {}
local Uniform = require "uniform"
for k,v in pairs(t) do
if type(k)=="number" then
local outk
if type(v)=="number" then
outk="number"
elseif type(v)=="boolean" then
outk="bool"
elseif type(v)=="string" then
outk="string"
elseif type(v)=="function" then
outk="luaFunction"
elseif types.isType(v) then
outk="type"
elseif darkroom.isFunction(v) then
outk="rigelFunction"
elseif SDF.isSDF(v) then
outk="rate"
elseif type(v)=="table" and getmetatable(v)==AddressMT then
outk="address"
v=v[1]
elseif type(v)=="table" and J.keycount(v)==2 and #v==2 and type(v[1])=="number" and type(v[2])=="number" then
outk="size"
v = darkroom.Size(v)
elseif darkroom.isSize(v) then
outk="size"
elseif type(v)=="table" and J.keycount(v)==4 and #v==4 and type(v[1])=="number" and type(v[2])=="number"
and type(v[3])=="number" and type(v[4])=="number" then
outk="bounds"
elseif Uniform.isUniform(v) then
if v:isNumber() then
outk="number"
if v.kind=="const" then
v = v:toNumber()
else
end
else
J.err("NYI - typeToKey "..tostring(v))
end
elseif type(v)=="table" and #t==J.keycount(t) and J.foldl(J.andop,false,J.map(v,darkroom.isInstance)) then
outk="instanceList"
elseif v==darkroom.Async then
outk="async"
v = true
elseif v==darkroom.Unoptimized then
outk="unoptimized"
v = true
else
J.err(false,"unknown type to key? "..tostring(v))
end
--J.err( res[outk]==nil, "Generator key '"..outk.."' is set twice?" )
local o_outk = outk
local i=1
while res[outk]~=nil do outk=o_outk..tostring(i);i=i+1 end
res[outk] = v
else
J.err( res[k]==nil, "Generator key '"..k.."' is set twice?" )
res[k] = v
end
end
return res
end
local functionGeneratorFunctions = {}
local functionGeneratorMT={}
-- find how to lift parameterized type Tparam to match Ttarget
local function findLifts( generatorName, fn, Ttarget, Tparam, TparamOutput, X )
assert(X==nil)
assert(type(generatorName)=="string")
J.err( darkroom.isPlainFunction(fn),"findLifts: input function should return plain function, but instead is: ",fn )
--print("findLifts ",generatorName," Target(input):",Ttarget,"ParameterizedTypeInput:",Tparam,"ParameterizedTypeOutput:",TparamOutput)
local finVars = {}
if Tparam:isSupertypeOf(Ttarget,finVars,"") then
err(finVars.type==nil,"findLifts: argument 'type' already set? Don't use 'type' as a parameter name")
err(fn~=nil,"findLifts: generator function returned nil?")
J.err( fn.inputType==Ttarget,"findLifts: generator '",generatorName,"' returned an input type that doesn't conform to interface it promised? \nTarget Input:",Ttarget," \nPromised Input:",Tparam," \nReturned Input Type:",fn.inputType,"\n",fn)
J.err( TparamOutput:isSupertypeOf(fn.outputType,{},""), "findLifts: '",generatorName,"' returned an output type that doesn't conform to interface it promised? \npromised:",TparamOutput," \nreturned:",fn.outputType,"\ninputType:",fn.inputType)
return fn
else
--print("Trivial check failed")
end
local L = require "generators.lifts"
-- search through lifts
local liftsFound = false
for liftName,lift in pairs(L) do
local vars1, vars2, vars3 = {},{},{}
local check1 = lift[3]:isSupertypeOf(Ttarget,vars1,"")
--print("check1",lift[3],"isSupertypeOf",Ttarget, check1)
local check2 = lift[1]:isSupertypeOf(Tparam,vars2,"")
--print("check2",lift[1],":isSupertypeOf",Tparam, check2)
local check3 = lift[2]:isSupertypeOf(TparamOutput,vars3,"")
--print("check3",lift[2],":isSupertypeOf",TparamOutput, check3)
--print("Try lift",liftName,check1,check2,check3)
if check1 and check2 and check3 then
--print("Apply lift: "..tostring(lift[1]).."->"..tostring(lift[2]).." to "..tostring(lift[3]).."->"..tostring(lift[4]))
--for kk,vv in pairs(vars1) do print("SPEC",kk,vv) end
--local spec = lift[3]:specialize(vars1)
--print("SPECIALIZE",Ttarget,lift[3],spec)
--print("res",Ttarget,spec)
--err( Ttarget==spec, "Internal error, specialized lift doesn't match target? target:"..tostring(Ttarget).." Specialized:"..tostring(spec) )
--print("SPECIALIZE",lift[1])
local newTtarget = lift[1]:specialize(vars1)
--print("NewTarget:",newTtarget)
liftsFound = true
local res = findLifts( generatorName, fn, newTtarget, Tparam, TparamOutput )
if res==nil then
return nil
else
--print("Found lift",liftName,res)
--print("Because ",lift[3],":isSupertypeOf",Ttarget)
--print("And ",lift[1],":isSupertypeOf",Tparam)
--for k,v in pairs(vars2) do print("VARS2",k,v) end
--for k,v in pairs(vars1) do print("VARS1",k,v) end
local liftFn = lift[5](vars1)
return liftFn(res)
end
else
--print("Lift '"..liftName.."' doesn't apply")
--print(lift[3],"supertypeof?",Ttarget,lift[3]:isSupertypeOf(Ttarget,vars1))
--print(lift[1],"Supertypeof?",Tparam,lift[1]:isSupertypeOf(Tparam,vars2))
--print(lift[2],"Supertypeof?",TparamOutput,lift[2]:isSupertypeOf(TparamOutput,vars3))
end
end
--err(liftsFound,"No lifts found?")
--if liftsFound==false then print("No lifts found?") end
return nil
end
functionGeneratorMT.__call=function(tab,...)
local rawarg = {...}
if darkroom.isIR(rawarg[1]) then -- calling with a value
local arg
if #rawarg>1 and rawarg[1].type:is("HandshakeTrigger") then
-- sort of a hack: handshake trigger can only be made into arrays
arg = darkroom.concatArray2d(nil,rawarg,#rawarg)
elseif #rawarg>1 then
-- collapse multi args into tuple
arg = darkroom.concat(nil, rawarg)
else
arg = rawarg[1]
end
if arg.scheduleConstraints~=nil and arg.scheduleConstraints.RV then
-- early out hack: if schedule pass is done (RV is known, just skip the apply)
return arg
end
local arglist = {}
for k,v in pairs(tab.curriedArgs) do arglist[k] = v end
if arglist.type==nil and (tab.requiredArgs.type~=nil or tab.optArgs.type~=nil) then arglist.type = arg.type end
if arglist.rate==nil and (tab.requiredArgs.rate~=nil or tab.optArgs.rate~=nil) then arglist.rate = arg.rate end
local rfn = tab:complete(arglist,rawarg[1].loc)
if arg.scheduleConstraints==nil then -- if we're doing the schedule pass, don't show an error
err( rfn.inputType==arg.type, "Failed to find a conversion for fn '",rfn.name,"' with \ninput type:'",rfn.inputType,"'\ninput rate ",rfn.sdfInput," \noutput type '",rfn.outputType,"' \nto convert to type:'",arg.type,"' rate:",arg.rate)
end
return rfn(arg)
elseif type(rawarg[1])=="table" and #rawarg==1 then
-- calling with a list of generator params. Either complete, or curry.
local arg = rawarg[1]
err( J.keycount(arg)>0, "Calling function generator '",tab.name,"' with an empty parameter list?" )
arg = typeToKey(arg)
local arglist = {}
for k,v in pairs(tab.curriedArgs) do arglist[k] = v end
for k,v in pairs(arg) do
if k=="type" and arg.type1==nil and tab.requiredArgs.type1~=nil then
-- special case: explicitly passing a type will put it in type1. Fix behavior later
J.err( arglist.type1==nil or arglist.type1==v, "Argument 'type1' was already passed to function generator '"..tab.name.."'" )
arglist.type1=v
elseif k=="rate" and arg.rate1==nil and (tab.requiredArgs.rate1~=nil or tab.optArgs.rate1~=nil) then
-- special case: explicitly passing a type will put it in type1. Fix behavior later
J.err( arglist.rate1==nil or arglist.rate1==v, "Argument 'rate1' was already passed to function generator '"..tab.name.."'" )
arglist.rate1=v
else
J.err( arglist[k]==nil or arglist[k]==v, "Argument '"..k.."' was already passed to function generator '"..tab.name.."', prev:",arglist[k]," new:",v )
arglist[k] = v
end
end
-- Hack: if this module is parametric (inputType~=unknown), we need to have the input before we run it
if tab:checkArgs(arglist) then
return tab:complete(arglist)
else
-- not done yet, return curried generator
local res = darkroom.FunctionGenerator( tab.name, tab.requiredArgs, tab.optArgs, tab.completeFn, tab.inputType, tab.optimized )
res.curriedArgs = arglist
return res
end
elseif tab.inputType~=nil and types.Interface():isSupertypeOf(tab.inputType,{},"") then
err(#rawarg==0,"Calling a nullary function with an argument?")
local arglist = {}
for k,v in pairs(tab.curriedArgs) do arglist[k] = v end
if arglist.type==nil and (tab.requiredArgs.type~=nil or tab.optArgs.type~=nil) then arglist.type = types.Interface() end
if arglist.rate==nil and (tab.requiredArgs.rate~=nil or tab.optArgs.rate~=nil) then arglist.rate = SDF{1,1} end
return tab:complete(arglist)()
else
J.err(false, "Called function generator '"..tab.name.."' with something other than a Rigel value or table ("..tostring(rawarg[1])..")? Make sure you call function generator with curly brackets {} ")
end
end
functionGeneratorMT.__tostring=function(tab)
local res = {}
table.insert(res,"Function Generator "..tab.name)
table.insert(res," Input Type: "..tostring(tab.inputType))
table.insert(res," Output Type: "..tostring(tab.outputType))
table.insert(res," Required Args:")
for k,v in pairs(tab.requiredArgs) do table.insert(res," "..k) end
table.insert(res," Curried Args:")
for k,v in pairs(tab.curriedArgs) do table.insert(res," "..k) end
table.insert(res," Optional Args:")
for k,v in pairs(tab.optArgs) do table.insert(res," "..k) end
return table.concat(res,"\n")
end
functionGeneratorMT.__index = functionGeneratorFunctions
-- this will specialize the generator to the given type. It must return a plain function.
-- This function may fail to return you exactly what you want, but it will try, and return something.
-- convert: should we add conversion functions to get exactly what you want? default true
function functionGeneratorFunctions:specializeToType( ty, rate, convert )
err( types.isType(ty),"specializeToType: first arg should be a type, but is: ",ty)
err( ty:isInterface(),"specializeToType: first arg should be interface type, but is: ",ty)
err( SDFRate.isSDFRate(rate),"specializeToType: second arg should be a rate, but is: ",rate)
local arglist = {}
if self.requiredArgs.type~=nil then
if self.curriedArgs.type~=nil then
if self.curriedArgs.type~=ty then
print("specializeToType error: type was already set to something different")
return nil
end
else
arglist.type = ty
end
end
if self.requiredArgs.rate~=nil or self.optArgs.rate~=nil then
if self.curriedArgs.rate~=nil then
if self.curriedArgs.rate~=rate then
print("specializeToType error: rate was already set to something different")
return nil
end
else
arglist.rate = rate
end
end
if self:checkArgs(arglist)==false then
print("Could not specialize '",self.name,"' to type, missing args")
for k,v in pairs(self.requiredArgs) do
if self.curriedArgs[k]==nil and arglist[k]==nil then print("Requires argument '"..k.."'") end
end
return nil
end
for k,v in pairs(self.curriedArgs) do
assert(arglist[k]==nil)
arglist[k] = v
end
return self:complete( arglist, nil, convert )
end
-- return true if done, false if not done
function functionGeneratorFunctions:checkArgs(arglist)
local reqArgs = {}
for k,v in pairs(arglist) do
if self.requiredArgs[k]==nil and self.optArgs[k]==nil then
print("Error, arg '"..k.."' is not in list of required or optional args on function generator '"..self.name.."'!" )
print("Required Args: ")
for k,v in pairs(self.requiredArgs) do print(k..",") end
print("Curried Args: ")
for k,v in pairs(self.curriedArgs) do print(k..",") end
assert(false)
elseif self.requiredArgs[k]~=nil then
reqArgs[k]=1
end
-- if it's optional, we don't care: we're only checking if all required args are set
end
for k,v in pairs(self.curriedArgs) do
if arglist[k]~=nil and arglist[k]~=v then
print("error, '",self.name,"' overrided a curried arg with another value? k:",k," curried:",v," passed:",arglist[k])
return false
end
if self.requiredArgs[k]~=nil then
reqArgs[k] = 1
end
end
return J.keycount(reqArgs)==J.keycount(self.requiredArgs)
end
function functionGeneratorFunctions:listRequiredArgs()
local s = {}
for k,v in pairs(self.requiredArgs) do if self.curriedArgs[k]==nil then table.insert(s,k) end end
return table.concat(s,",")
end
-- does this generate require the arg 'arg'? (returns true if either required or optional)
function functionGeneratorFunctions:requiresArg(arg)
assert(type(arg)=="string")
return (self.requiredArgs[arg]~=nil or self.optArgs[arg]~=nil) and (self.curriedArgs[arg]==nil)
end
-- top level function that implements logic to convert a fn to a given type.
-- If this fails, it just returns fn
darkroom.convertInterface = J.memoize(function( fn, targetInputTypeOrig, targetInputRateOrig, loc, X )
assert( X==nil )
assert( types.isType(targetInputTypeOrig) )
err( SDF.isSDF(targetInputRateOrig), "convertInterfaces SDF should be SDF but is: ", targetInputRateOrig )
-- print("CONVERT_INTERFACE ",targetInputTypeOrig, targetInputRateOrig," TO ", fn.inputType, fn.sdfInput, fn.name )
-- nothing to do!
if fn.inputType == targetInputTypeOrig then
return fn
end
local G = require "generators.core"
local C = require "generators.examplescommon"
local RM = require "generators.modules"
local functionStack = {}
err( targetInputTypeOrig:deSchedule()==fn.inputType:deSchedule(), "convertInterface: unscheduled types don't even match! nothing we can do. Converting ",targetInputTypeOrig," to ",fn.inputType," unscheduled ",targetInputTypeOrig:deSchedule()," to ",fn.inputType:deSchedule())
-- does this require a space-time conversion?
local function reshape( fnType, targetType )
err( fnType:isSchedule(), "reshape: input should be schedule type, but is: ",fnType )
assert( targetType:isSchedule() )
local res
if fnType:isArray() then
-- if not an array, nothing we can do!
if fnType.V[1]*fnType.V[2] ~= targetType.V[1]*targetType.V[2] then
-- we need to serialize
local resStack = {}
local fnTypeShadow, targetTypeShadow = fnType, targetType
-- we're deser: need to go from inner to outer
if targetTypeShadow:arrayOver()~=fnTypeShadow:arrayOver() and fnTypeShadow.V[1]*fnTypeShadow.V[2]>targetTypeShadow.V[1]*targetTypeShadow.V[2] then
table.insert( resStack, RM.mapSeq(reshape( fnTypeShadow.over, targetTypeShadow.over ), fnType.size[1], fnType.size[2]) )
assert( darkroom.isPlainFunction(resStack[#resStack]) )
targetTypeShadow = resStack[#resStack].outputType:deInterface()
end
if fnTypeShadow:rowMajor() and targetTypeShadow:rowMajor() then
table.insert( resStack, C.ChangeRateRowMajor( targetTypeShadow:arrayOver(), targetTypeShadow.V[1], targetTypeShadow.V[2],
fnTypeShadow.V[1]*fnTypeShadow.V[2],
targetTypeShadow.size[1], targetTypeShadow.size[2] ) )
assert( darkroom.isPlainFunction(resStack[#resStack]) )
elseif fnTypeShadow:columnMajor() or targetTypeShadow:columnMajor() then
--err( false,"NYI - convertInterface to a column major array converting:",targetType," to:",fnType," for:",fn,loc)
err( targetTypeShadow.V[2]==fnTypeShadow.V[2],"NYI - column major change rate with different array height. Converting ",targetTypeShadow," to ",fnTypeShadow)
table.insert( resStack, RM.changeRate( targetTypeShadow:arrayOver(), fnTypeShadow.V[2], targetTypeShadow.V[1], fnTypeShadow.V[1], true, fnTypeShadow.size[1], fnTypeShadow.size[2] ) )
assert( darkroom.isPlainFunction(resStack[#resStack]) )
else
J.err("converting between strange vector sizes! ",targetTypeShadow," to ",fnTypeShadow)
end
-- we're ser: need to go from outer to inner
if targetTypeShadow:arrayOver()~=fnTypeShadow:arrayOver() and fnTypeShadow.V[1]*fnTypeShadow.V[2]<targetTypeShadow.V[1]*targetTypeShadow.V[2] then
table.insert( resStack, RM.mapSeq(reshape( fnTypeShadow.over, targetTypeShadow.over ), fnTypeShadow.size[1], fnTypeShadow.size[2]) )
assert( darkroom.isPlainFunction(resStack[#resStack]) )
targetTypeShadow = resStack[#resStack].outputType:deInterface()
end
if #resStack==1 then
res = resStack[1]
else
J.err( #resStack>0,"bad fn stack? converting ",fnType," to ",targetType )
res = C.linearPipeline(resStack,"ConvertInterface_Ser_stack_"..tostring(fnType).."_"..tostring(targetType))
end
elseif fnType.V==targetType.V and fnType:arrayOver()~=targetType:arrayOver() then
-- recurse
if fnType.V:eq(0,0) then
if fnType.var then
res = RM.mapVarSeq( reshape( fnType.over, targetType.over ), fnType.size[1], fnType.size[2] )
else
res = RM.mapSeq( reshape( fnType.over, targetType.over ), fnType.size[1], fnType.size[2] )
end
else
print("NYI - ", targetType, " to ", fnType )
assert(false)
end
elseif fnType.V==targetType.V and
fnType:arrayOver()==targetType:arrayOver() then
-- nothing to do!
else
J.err(false,"convertInterface NYI - convert ",fnType," to ",targetType," calling function:",fn.name,loc)
end
elseif fnType:isTuple() then
assert( targetType:isTuple() and #fnType.list==#targetType.list )
res = G.Function{"FannedConvertInterface_"..tostring(targetType).."_"..tostring(fnType), types.RV(targetType), SDF{1,1},
function(inp)
local tmp = RM.broadcastStream( targetType, #targetType.list)(inp)
local out = {}
for k,v in ipairs(targetType.list) do
local i = darkroom.selectStream( "str"..k, tmp, k-1 )
i = RM.makeHandshake(C.index( targetType, k-1 ))(i)
i = C.fifo( v, 128, nil, nil, nil, nil, "fannedconvertInterface_"..tostring(k))(i)
local rec = reshape( fnType.list[k], v )
if rec==nil then
-- this is OK: means no change
table.insert( out, i )
else
local recres = rec(i)
recres = C.fifo( recres.type:deInterface(), 128, nil, nil, nil, nil, "fannedconvertInterface_"..tostring(k))(recres)
table.insert( out, recres )
assert( out[#out].type:deInterface()==fnType.list[k] )
end
end
local rrr = darkroom.concat(out)
local ptList = {}
for k,v in ipairs( fnType.list) do table.insert(ptList,types.RV(v)) end
return RM.packTuple( ptList )(rrr)
end}
end
if res~=nil then
err( res.inputType==types.RV(targetType), "convertInterface: conversion function input type should have been: ",types.RV(targetType)," but was: ",res.inputType )
err( res.outputType==types.RV(fnType), "convertInterface: conversion function output type should have been: ",types.RV(fnType)," but was: ",res.outputType,". input type should be: ",types.RV(targetType),", and was correct." )
else
err( targetType==fnType, "convertInterface: conversion function returned noop, but should have been conversion from ",targetType," to ",fnType )
end
return res
end
local targetInputType = targetInputTypeOrig
local targetInputRate = targetInputRateOrig
-- rate optimization
if targetInputType:deInterface()~=fn.inputType:deInterface() then
if targetInputType:isArray() then
-- RV()[N] optimization
err( fn.inputType:isArray(), "ConvertInterface NYI = if input is array of interfaces, so must output be. target: ",targetInputType," to ",fn.inputType )
err( targetInputType.size==fn.inputType.size, "ConvertInterface NYI = if input is array of interfaces, size must match. target: ",targetInputType," to ",fn.inputType )
assert( targetInputType.over:is("Interface") )
local res = RM.mapOverInterfaces( reshape( fn.inputType.over.over, targetInputType.over.over ), targetInputType.size[1], targetInputType.size[2] )
table.insert( functionStack, res )
elseif targetInputType:isTuple() then
-- {RV(),RV()} optimization
-- NOTE: at this point, the fn may expect a rv, not a RV, but we don't do that conversion yet! First: make the rates match
--err( fn.inputType:isTuple(), "ConvertInterface NYI = if input is tuple of interfaces, so must output be. target: ",targetInputType," to ",fn.inputType )
err( #targetInputType:deInterface().list==#fn.inputType:deInterface().list, "ConvertInterface NYI = if input is tuple of interfaces, size of tuple must match. target: ",targetInputType," to ",fn.inputType )
assert( targetInputType.list[1]:is("Interface") )
local res = G.Function{"TupleConvertInterface_"..tostring(targetInputType).."_"..tostring(fn.inputType), targetInputType, targetInputRate,
function(inp)
local out = {}
for k,v in ipairs(targetInputType:deInterface().list) do
local i = darkroom.selectStream( "str"..k, inp, k-1 )
local rec = reshape( fn.inputType:deInterface().list[k], v )
if rec==nil then
-- this is OK: means no change
table.insert( out, i )
else
local recres = rec(i)
table.insert( out, recres )
assert( out[#out].type:deInterface()==fn.inputType:deInterface().list[k] )
end
end
local rrr = darkroom.concat(out)
return rrr
end}
-- hack: tell the FIFO check code that streams are not mixed
res.fifoStraightThrough = true
table.insert( functionStack, res )
else
-- simple case of RV(T1) to RV(T2)
err( targetInputType:is("Interface") and fn.inputType:is("Interface"),"ConvertInterface NYI - must both be single interfaces, but is conversion from ",targetInputType," to ",fn.inputType )
local res = reshape( fn.inputType.over, targetInputType.over )
if res~=nil then
table.insert( functionStack, res )
end
end
end
--local liftTargetInputType = targetInputType
--local liftTargetInputRate = targetInputRate
if #functionStack>0 then
targetInputType = functionStack[#functionStack].outputType
targetInputRate = functionStack[#functionStack].sdfOutput
end
-- convert tuples of interfaces to interfaces of tuples, AFTER RATE MATCHING
-- this has to happen after rate matching, because after this conversion, the whole bundle will have 1 rate!
-- but some of the S-T conversions may require you to mess with individual (multiple) rates
if targetInputType:isTuple() and fn.inputType:isrv() and fn.inputType.over:isTuple() then
-- this means we are converting <RV(a),RV(b)> to rv({a,b}), by doing a packtuple
table.insert( functionStack, RM.packTuple( targetInputType.list ) )
targetInputType = functionStack[#functionStack].outputType
end
J.err( targetInputType:deInterface()==fn.inputType:deInterface(),"Reshape failed: function has input type: ",fn.inputType," but reshape returned type: ", targetInputType )
--J.err( liftTargetInputRate==fn.sdfInput, "Reshape failed: function has input rate: ",fn.sdfInput," but reshaped returned rate: ", liftTargetInputRate )
local mod = findLifts( fn.name, fn, targetInputType, fn.inputType, fn.outputType )
if mod~=nil then
table.insert( functionStack, mod )
else
table.insert( functionStack, fn )
end
if #functionStack==1 then
return functionStack[1]
else
local res = C.linearPipeline( functionStack, "convertInterface_"..tostring(targetInputTypeOrig).."_to_"..tostring(fn.inputType).."_"..fn.name, targetInputRateOrig )
return res
end
end)
-- this function either returns a plain function, or fails
-- This may not return a function with exactly the type you asked for! You need to check for that!
-- convert: should we add conversion functions to get exactly the type you want? default true
function functionGeneratorFunctions:complete( arglist, loc, convert, X )
assert( X==nil )
if DARKROOM_VERBOSE then print("FunctionGenerator:complete() ",self.name) end
if self:checkArgs(arglist)==false then
print("Function generator '"..self.name.."' is missing arguments!")
for k,v in pairs(self.requiredArgs) do
if arglist[k]==nil and self.curriedArgs[k]==nil then print("Requires argument '"..k.."'") end
end
assert(false)
end
-- don't mutate input!
local a = {}
for k,v in pairs(arglist) do a[k]=v end
for k,v in pairs(self.curriedArgs) do
err( arglist[k]==nil or arglist[k]==v, ":complete() ",self.name,", value in arglist was already in curried args? k ",k," v ",v," prev ",arglist[k] )
a[k] = v
end
if self.optimized and arglist.rate~=nil and arglist.type~=nil and arglist.unoptimized~=true then
a.type, a.rate = arglist.type:optimize( arglist.rate )
assert( types.isType(a.type) )
assert( SDF.isSDF(a.rate) )
end
if self.inputType~=nil and self.inputType~=types.Unknown then
local newlist = {}
err( self.inputType:isSupertypeOf( a.type:deInterface(), newlist, "" ), "Input type to generator '",self.name,"' is incorrect!, expected: ",self.inputType," passed:", arglist.type, " which is deinterfaced/optimized into type: ", a.type:deInterface()," optimized:",self.optimized)
for k,v in pairs(newlist) do
assert(a[k]==nil)
a[k] = v
end
end
local fn = self.completeFn( a )
J.err( darkroom.isPlainFunction(fn), "function generator '",self.name,"' returned something other than a plain rigel function? returned:",fn )
if self.requiredArgs.type~=nil and self.requiredArgs.rate~=nil and convert~=false then -- if we don't know type, we can't lift
fn = darkroom.convertInterface( fn, arglist.type, arglist.rate )
--J.err( fn.inputType == arglist.type, "convertInterface Fail wanted: ",arglist.type," returned: ",fn.inputType )
end
fn.generator = self
fn.generatorArgs = arglist
if DARKROOM_VERBOSE then print("FunctionGenerator:complete() DONE,TRIVIAL",self.name) end
return fn
end
-- optimized: should input type be optimized before being passed to completeFn?
function darkroom.FunctionGenerator( name, requiredArgs, optArgs, completeFn, inputType, optimized, X )
assert(X==nil)
err( type(name)=="string", "FunctionGenerator: name must be string, but is: "..tostring(name) )
err( type(requiredArgs)=="table", "FunctionGenerator: requiredArgs must be table, but is: "..tostring(requiredArgs) )
if J.keycount(requiredArgs)==#requiredArgs then
-- convert array of names to set
requiredArgs = J.invertTable(requiredArgs)
end
for k,v in pairs(requiredArgs) do J.err( type(k)=="string", "FunctionGenerator: requiredArgs must be set of strings" ) end
err( type(optArgs)=="table", "FunctionGenerator: requiredArgs must be table" )
if J.keycount(optArgs)==#optArgs then optArgs = J.invertTable(optArgs) end -- convert array of names to set
for k,v in pairs(optArgs) do J.err( type(k)=="string", "FunctionGenerator: optArgs must be set of strings" ) end
optArgs.unoptimized = 1
err( type(completeFn)=="function", "FunctionGenerator: completeFn must be lua function" )
if optimized==nil then optimized=true end
err( type(optimized)=="boolean", "FunctionGenerator: 'optimized' should be boolean, but is: ", optimized )
if inputType==nil or inputType==types.Unknown then
inputType=types.Unknown
else
err(types.isType(inputType) or P.isParam(inputType),"FunctionGenerator, type should be type, but is: "..tostring(inputType))
requiredArgs.type=true
requiredArgs.rate=true
end
return setmetatable( {name=name, requiredArgs=requiredArgs, optArgs=optArgs, completeFn=completeFn, curriedArgs={}, inputType=inputType, optimized=optimized }, functionGeneratorMT )
end
function darkroom.isFunctionGenerator(t) return (getmetatable(t)==functionGeneratorMT) or darkroom.isModuleGeneratorInstanceCallsite(t) end
local function buildAndCheckSystolicModule( tab, isModule )
if DARKROOM_VERBOSE then print("buildAndCheckSystolicModule",tab.name) end
-- build the systolic module as needed
err( rawget(tab, "makeSystolic")~=nil, "missing makeSystolic() for "..J.sel(isModule,"module","function").." '"..tab.name.."'" )
local sm = rawget(tab,"makeSystolic")()
if Ssugar.isModuleConstructor(sm) then sm:complete(); sm=sm.module end
J.err( S.isModule(sm),"makeSystolic didn't return a systolic module? Returned '",sm,"'. Module: ",tab)
if DARKROOM_VERBOSE then print("buildAndCheckSystolicModule","CHECK",tab.name) end
local systolicFns = sm.functions
local rigelFns = tab.functions
if isModule==false then
systolicFns={["process"]=sm.functions.process}
rigelFns = {["process"]=tab}
end
err( tab.name==sm.name, "systolic module name doesn't match rigel module name! rigelName:'",tab.name,"' systolicName:'",sm.name,"'")
for fnname,rigelFn in pairs(rigelFns) do
local systolicFn = systolicFns[fnname]
err( systolicFn~=nil, "systolic module function is missing (",sm.name,")")
-- LUTS contain brams, which aren't 'pure' (whatever that means), but they also aren't really 'stateful'
-- they have no reset or carried state
--err( systolicFn:isPure()~=rigelFn.stateful,"Error, rigel module '",tab.name,"' fn '",fnname,"' was declared with stateful=",rigelFn.stateful," but systolic function isPure=",systolicFn:isPure())
if rigelFn.outputType==types.Interface() then
else
err( systolicFn.output~=nil, "module '",tab.name,"' output is not null (is ",rigelFn.outputType,", lowered to: ",rigelFn.outputType:lower(),"), but systolic output is missing")
err( darkroom.lower(rigelFn.outputType)==systolicFn.output.type, "systolic module output type wrong on module '",tab.name,"'? systolic output is '",systolicFn.output.type,"' but should be '",darkroom.lower(rigelFn.outputType),"' (because rigel type is ",rigelFn.outputType,")" )
end
local shouldHaveCE = (types.isHandshakeAny(rigelFn.inputType) or types.isHandshakeAny(rigelFn.outputType))==false and (rigelFn.stateful or (rigelFn.delay~=nil and rigelFn.delay>0)) and (rigelFn.inputType==types.Interface() and rigelFn.outputType==types.Interface())==false
err( shouldHaveCE==(systolicFn.CE~=nil), "Systolic function CE doesn't match rigel definition. Module '",tab.name,"' fn '",fnname,"'. rigelFn.stateful=",rigelFn.stateful," rigelFn.delay=",rigelFn.delay," rigelFn.inputType=",rigelFn.inputType," shouldHaveCE=",shouldHaveCE," systolicCE:",systolicFn.CE)
if types.isHandshakeAny(rigelFn.inputType) or types.isHandshakeAny(rigelFn.outputType) then
local readyName = fnname.."_ready"
if isModule==false then readyName="ready" end
err( sm.functions[readyName]~=nil, "module '"..tab.name.."' ready function '"..readyName.."' missing?")
local expectedInputReady = darkroom.extractReady(rigelFn.inputType)
local expectedOutputReady = darkroom.extractReady(rigelFn.outputType)
err( sm.functions[readyName]~=nil,"missing ready fn?")
err( rigelFn.inputType==types.Interface() or sm.functions[readyName].output~=nil,"ready fn '"..readyName.."' has no output? module: ",rigelFn)
if rigelFn.inputType~=types.Interface() then
err( expectedInputReady==sm.functions[readyName].output.type, "module '"..tab.name.."' systolic ready '",readyName,"' output type wrong. Systolic ready output type is '",sm.functions[readyName].output.type,"', but should be '",expectedInputReady,"', because Rigel function input type is '",rigelFn.inputType,"'.")
end
if rigelFn.outputType~=types.Interface() then
err( rigelFn.outputType==types.Interface() or expectedOutputReady==sm.functions[readyName].inputParameter.type, "module '",tab.name,"' systolic ready '",readyName,"' input type wrong. Systolic ready input type is '",sm.functions[readyName].inputParameter.type,"', but should be '",darkroom.extractReady(rigelFn.outputType),"', because Rigel function output type is '",rigelFn.outputType,"'.")
end
--elseif (rigelFn.inputType==types.Interface() and rigelFn.outputType==types.Interface())==false and rigelFn.outputType:isrv() then
elseif rigelFn.inputType~=types.Interface() or rigelFn.outputType~=types.Interface() then
err( type(rigelFn.delay)=="number","Error, rigel module missing delay? fnname '",fnname,"' should have been caught earlier ",tab)
err( sm:getDelay(fnname)==rigelFn.delay, "Error, rigel module '",tab.name,"' fn '",fnname,"' was declared with delay=",rigelFn.delay," but systolic function delay is:",sm:getDelay(fnname)," Module ",sm)
end
local expectedInput = types.lower(rigelFn.inputType,rigelFn.outputType)
err( expectedInput==systolicFn.inputParameter.type, "systolic module input type wrong? fnname:'",fnname,"' module:'",tab.name,"' Rigel input type:",rigelFn.inputType," Rigel Lowered:",expectedInput," module type:",systolicFn.inputParameter.type )
end
err( (sm.functions.reset~=nil)==tab.stateful, "Modules must have reset iff the module is stateful (module "..tab.name.."). stateful:"..tostring(tab.stateful).." hasReset:"..tostring(sm.functions.reset~=nil))
for inst,lst in pairs(sm.externalInstances) do
for _,fnname in ipairs(lst) do
local found = false
for ic,_ in pairs(tab.requires) do if ic.instance.name==inst.name and ic.functionName==fnname then found=true end end
for ic,_ in pairs(tab.provides) do if ic.instance.name==inst.name and ic.functionName==fnname then found=true end end
err( found, "makeSystolic(): systolic module '"..sm.name.."' refers to external instance '"..inst.name.."', but rigel module doesn't?")
end
end
for inst,fnmap in pairs(tab.requires) do
err( sm.externalInstances[inst:toSystolicInstance()]~=nil, "Rigel module '"..tab.name.."' requires instance '"..inst.name.."', but it's missing from systolic module external list? ")
for fnname,_ in pairs(fnmap) do
err( sm.externalInstances[inst:toSystolicInstance()][fnname]~=nil, "Rigel module requires instance, but it's missing from systolic module? ")
local fn = inst.module.functions[fnname]
if types.isHandshakeAny(fn.inputType) or types.isHandshakeAny(fn.outputType) then
err( sm.externalInstances[inst:toSystolicInstance()][fnname.."_ready"]~=nil, "Rigel module '"..tab.name.."' requires instance callsite '"..inst.name..":"..fnname.."()', but its ready fn is missing from systolic module external list? ")
end
end
end
rawset(tab,"systolicModule", sm )
if DARKROOM_VERBOSE then print("buildAndCheckSystolicModule","DONE",tab.name) end
return sm
end
function buildAndCheckTerraModule(tab)
err( rawget(tab, "makeTerra")~=nil, "missing terraModule, and 'makeTerra' doesn't exist on module '",tab.name,"'" )
err( type(rawget(tab, "makeTerra"))=="function", "'makeTerra' function not a lua function, but is "..tostring(rawget(tab, "makeTerra")) )
local tm = rawget(tab,"makeTerra")()
err( terralib.types.istype(tm) and tm:isstruct(), "makeTerra for module '",tab.name,"' returned something other than a terra struct? returned: ",tm)
rawset(tab,"terraModule", tm )
return tm
end
local darkroomFunctionFunctions = {}
local darkroomFunctionMT = {
__index=function(tab,key)
local v = rawget(tab, key)
if v ~= nil then return v end
v = darkroomFunctionFunctions[key]
if v~=nil then return v end
if key=="systolicModule" then
return buildAndCheckSystolicModule(tab,false)
elseif key=="terraModule" then
return buildAndCheckTerraModule(tab)
end
end,
__call=function(tab,...)
local rawarg = {...}
for _,arg in pairs(rawarg) do
J.err( arg==nil or darkroom.isIR(arg),"Input argument to rigel function '",tab.name,"' must be a rigel value, but is:'",arg,"'")
-- discover variable name from lua
if arg~=nil and arg.defaultName then
local n = discoverName(arg)
if n~=nil then
arg.name=n.."_"..darkroom.__unnamedID
darkroom.__unnamedID = darkroom.__unnamedID+1
arg.defaultname=false
end
end
end
local arg
if #rawarg>1 and rawarg[1].type:is("HandshakeTrigger") then
-- sort of a hack: handshake trigger can only be made into arrays
arg = darkroom.concatArray2d(nil,rawarg,#rawarg)
elseif #rawarg>1 then
arg = darkroom.concat(nil,rawarg)
else
arg = rawarg[1]
end
local res = darkroom.apply(J.sanitize("un"..darkroom.__unnamedID.."_"..tab.name:sub(1,20)), tab, arg)
res.defaultName=true
darkroom.__unnamedID = darkroom.__unnamedID+1
return res
end,
__tostring=function(mod,longform)
local res = {}
local mt = getmetatable(mod)
setmetatable(mod,nil)
local tabstr = tostring(mod)
setmetatable(mod,mt)
if darkroom.isPlainFunction(mod) then
table.insert(res,"* Plain Rigel Function "..mod.name.." ("..tabstr..")")
else
table.insert(res,"* Rigel Function "..mod.name.." ("..tabstr..")")
end
table.insert(res," InputType: "..tostring(mod.inputType))
table.insert(res," OutputType: "..tostring(mod.outputType))
if SDFRate.isSDFRate(mod.sdfInput) then
table.insert(res," InputSDF: "..tostring(mod.sdfInput))
else
table.insert(res," InputSDF: Not an SDF rate?")
end
if SDFRate.isSDFRate(mod.sdfOutput) then
table.insert(res," OutputSDF: "..tostring(mod.sdfOutput))
else
table.insert(res," OutputSDF: Not an SDF rate?")
end
table.insert(res," Stateful: "..tostring(mod.stateful))
table.insert(res," Delay: "..tostring(mod.delay))
table.insert(res," InputBurstiness: "..tostring(mod.inputBurstiness))
table.insert(res," OutputBurstiness: "..tostring(mod.outputBurstiness))