File: fir_code_container.cpp

package info (click to toggle)
faust 2.79.3%2Bds-2
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 397,496 kB
  • sloc: cpp: 278,433; ansic: 116,164; javascript: 18,529; vhdl: 14,052; sh: 13,884; java: 5,900; objc: 3,852; python: 3,222; makefile: 2,655; cs: 1,672; lisp: 1,146; ruby: 954; yacc: 586; xml: 471; lex: 247; awk: 110; tcl: 26
file content (433 lines) | stat: -rw-r--r-- 17,457 bytes parent folder | download | duplicates (2)
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
/************************************************************************
 ************************************************************************
    FAUST compiler
    Copyright (C) 2003-2018 GRAME, Centre National de Creation Musicale
    ---------------------------------------------------------------------
    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU Lesser General Public License as published by
    the Free Software Foundation; either version 2.1 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU Lesser General Public License for more details.

    You should have received a copy of the GNU Lesser General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 ************************************************************************
 ************************************************************************/

#include "fir_code_container.hh"
#include "fir_to_fir.hh"
#include "global.hh"
#include "instructions_complexity.hh"
#include "struct_manager.hh"

using namespace std;

dsp_factory_base* FIRCodeContainer::produceFactory()
{
    return new text_dsp_factory_aux(
        fKlassName, "", "",
        ((static_cast<ostringstream*>(fOut)) ? static_cast<ostringstream*>(fOut)->str() : ""), "");
}

CodeContainer* FIRCodeContainer::createScalarContainer(const string& name, int sub_container_type)
{
    return new FIRScalarCodeContainer(name, 0, 1, sub_container_type, fOut, false);
}

CodeContainer* FIRCodeContainer::createContainer(const string& name, int numInputs, int numOutputs,
                                                 ostream* dst, bool top_level)
{
    CodeContainer* container;

    if (gGlobal->gOpenMPSwitch) {
        container = new FIROpenMPCodeContainer(name, numInputs, numOutputs, dst, top_level);
    } else if (gGlobal->gSchedulerSwitch) {
        container = new FIRWorkStealingCodeContainer(name, numInputs, numOutputs, dst, top_level);
    } else if (gGlobal->gVectorSwitch) {
        container = new FIRVectorCodeContainer(name, numInputs, numOutputs, dst, top_level);
    } else {
        container = new FIRScalarCodeContainer(name, numInputs, numOutputs, kInt, dst, top_level);
    }

    return container;
}

void FIRCodeContainer::dumpUserInterface(FIRInstVisitor& firvisitor, ostream* dst)
{
    // User Interface
    if (fUserInterfaceInstructions->fCode.size() > 0) {
        *dst << "======= User Interface begin ==========" << endl << endl;
        fUserInterfaceInstructions->accept(&firvisitor);
        *dst << endl << "======= User Interface end ==========" << endl << endl;
    }
}

void FIRCodeContainer::dumpSubContainers(FIRInstVisitor& firvisitor, ostream* dst)
{
    *dst << "======= Sub container begin ==========" << endl << endl;
    for (const auto& it : fSubContainers) {
        it->produceInternal();
        it->dump(dst);
    }
    *dst << "======= Sub container end ==========" << endl << endl;
}

void FIRCodeContainer::dumpGlobalsAndInit(FIRInstVisitor& firvisitor, ostream* dst)
{
    if (fExtGlobalDeclarationInstructions->fCode.size() > 0) {
        *dst << "======= Global external declarations begin ==========" << endl << endl;
        fExtGlobalDeclarationInstructions->accept(&firvisitor);
        *dst << endl << "======= Global external declarations end ==========" << endl << endl;
    }

    if (fGlobalDeclarationInstructions->fCode.size() > 0) {
        *dst << "======= Global declarations begin ==========" << endl << endl;
        fGlobalDeclarationInstructions->accept(&firvisitor);
        *dst << endl << "======= Global declarations end ==========" << endl << endl;
    }

    if (fDeclarationInstructions->fCode.size() > 0) {
        *dst << "======= DSP struct begin ==========" << endl << endl;
        StructInstVisitor visitor;
        fDeclarationInstructions->accept(&visitor);
        visitor.getStructType(fKlassName)->accept(&firvisitor);
        *dst << endl << "======= DSP struct end ==========" << endl << endl;
    }

    generateGetInputs(subst("$0::getNumInputs", fKlassName), "dsp", true, FunTyped::kDefault)
        ->accept(&firvisitor);
    *dst << endl;
    generateGetOutputs(subst("$0::getNumOutputs", fKlassName), "dsp", true, FunTyped::kDefault)
        ->accept(&firvisitor);
    *dst << endl;

    if (fStaticInitInstructions->fCode.size() > 0) {
        *dst << "======= Static Init begin ==========" << endl << endl;
        fStaticInitInstructions->accept(&firvisitor);
        if (fPostStaticInitInstructions->fCode.size() > 0) {
            fPostStaticInitInstructions->accept(&firvisitor);
        }
        *dst << endl << "======= Static Init end ==========" << endl << endl;
    }

    if (fInitInstructions->fCode.size() > 0) {
        *dst << "======= Init begin ==========" << endl << endl;
        fInitInstructions->accept(&firvisitor);
        *dst << endl << "======= Init end ==========" << endl << endl;
    }

    if (fResetUserInterfaceInstructions->fCode.size() > 0) {
        *dst << "======= ResetUI begin ==========" << endl << endl;
        fResetUserInterfaceInstructions->accept(&firvisitor);
        *dst << endl << "======= ResetUI end ==========" << endl << endl;
    }

    if (fClearInstructions->fCode.size() > 0) {
        *dst << "======= Clear begin ==========" << endl << endl;
        fClearInstructions->accept(&firvisitor);
        *dst << endl << "======= Clear end ==========" << endl << endl;
    }

    if (fDestroyInstructions->fCode.size() > 0) {
        *dst << "======= Destroy begin ==========" << endl << endl;
        fDestroyInstructions->accept(&firvisitor);
        *dst << endl << "======= Destroy end ==========" << endl << endl;
    }

    if (fAllocateInstructions->fCode.size() > 0) {
        *dst << "======= Allocate begin ==========" << endl << endl;
        fAllocateInstructions->accept(&firvisitor);
        *dst << endl << "======= Allocate end ==========" << endl << endl;
    }
}

static void dumpCost(StatementInst* inst, ostream* dst)
{
    InstComplexityVisitor complexity;
    inst->accept(&complexity);
    complexity.dump(dst);
    *dst << endl;
}

void FIRCodeContainer::dumpComputeBlock(FIRInstVisitor& firvisitor, ostream* dst)
{
    if (fComputeBlockInstructions->fCode.size() > 0) {
        *dst << "======= Compute control begin ==========" << endl << endl;
        // Complexity estimation
        dumpCost(fComputeBlockInstructions, dst);
        fComputeBlockInstructions->accept(&firvisitor);
        *dst << endl << "======= Compute control end ==========" << endl << endl;
    }
}

void FIRCodeContainer::dumpControlBlock(FIRInstVisitor& firvisitor, ostream* dst)
{
    if (fControlDeclarationInstructions->fCode.size() > 0) {
        *dst << "======= Control begin ==========" << endl << endl;
        // Complexity estimation
        dumpCost(fControlDeclarationInstructions, dst);
        fControlDeclarationInstructions->accept(&firvisitor);
        *dst << endl << "======= Control end ==========" << endl << endl;
    }
}

void FIRCodeContainer::dumpFlatten(ostream* dst)
{
    *dst << "======= Flatten FIR begin ==========" << endl << endl;
    FIRInstVisitor firvisitor(dst);
    flattenFIR()->accept(&firvisitor);
    *dst << endl << "======= Flatten FIR end ==========" << endl << endl;
}

void FIRCodeContainer::dumpMemory(ostream* dst)
{
    // Compute memory footprint
    if (fTopLevel) {
        int total_heap_size = 0;
        for (const auto& it : fSubContainers) {
            VariableSizeCounter heap_counter(
                Address::AccessType(Address::kStruct | Address::kStaticStruct));
            it->generateDeclarations(&heap_counter);
            total_heap_size += heap_counter.fSizeBytes;
        }

        VariableSizeCounter heap_counter1(
            Address::AccessType(Address::kStruct | Address::kStaticStruct), Typed::kInt32);
        generateDeclarations(&heap_counter1);

        VariableSizeCounter heap_counter2(
            Address::AccessType(Address::kStruct | Address::kStaticStruct), Typed::kInt32_ptr);
        generateDeclarations(&heap_counter2);

        VariableSizeCounter heap_counter3(
            Address::AccessType(Address::kStruct | Address::kStaticStruct));
        generateDeclarations(&heap_counter3);

        VariableSizeCounter stack_counter(Address::kStack);
        generateComputeBlock(&stack_counter);

        *dst << "======= Object memory footprint ==========" << endl << endl;
        *dst << "Heap size int = " << heap_counter1.fSizeBytes << " bytes" << endl;
        *dst << "Heap size int* = " << heap_counter2.fSizeBytes << " bytes" << endl;
        *dst << "Heap size real = "
             << heap_counter3.fSizeBytes - (heap_counter1.fSizeBytes + heap_counter2.fSizeBytes)
             << " bytes" << endl;
        *dst << "Total heap size = " << heap_counter3.fSizeBytes + total_heap_size << " bytes"
             << endl;
        *dst << "Stack size in compute = " << stack_counter.fSizeBytes << " bytes" << endl;

        *dst << endl << "======= Variable access in Control ==========" << endl << endl;
        {
            StructInstVisitor struct_visitor;
            fDeclarationInstructions->accept(&struct_visitor);
            fControlDeclarationInstructions->accept(&struct_visitor);

            for (const auto& it : struct_visitor.getFieldTable()) {
                *dst << "Field = " << it.first;
                *dst << " size = " << it.second.fSize;
                *dst << " size_bytes = " << it.second.fSizeBytes;
                *dst << " read = " << it.second.fRAccessCount;
                *dst << " write = " << it.second.fWAccessCount;
                *dst << " ratio = "
                     << float(it.second.fRAccessCount + it.second.fWAccessCount) /
                            float(it.second.fSize)
                     << endl;
            }
        }

        *dst << endl << "======= Variable access in compute control ==========" << endl << endl;
        {
            StructInstVisitor struct_visitor;
            fDeclarationInstructions->accept(&struct_visitor);
            fComputeBlockInstructions->accept(&struct_visitor);

            for (const auto& it : struct_visitor.getFieldTable()) {
                *dst << "Field = " << it.first;
                *dst << " size = " << it.second.fSize;
                *dst << " size_bytes = " << it.second.fSizeBytes;
                *dst << " read = " << it.second.fRAccessCount;
                *dst << " write = " << it.second.fWAccessCount;
                *dst << " ratio = "
                     << float(it.second.fRAccessCount + it.second.fWAccessCount) /
                            float(it.second.fSize)
                     << endl;
            }
        }

        *dst << endl << "======= Variable access in compute DSP ==========" << endl << endl;
        {
            StructInstVisitor struct_visitor;
            fDeclarationInstructions->accept(&struct_visitor);

            ForLoopInst* loop = fCurLoop->generateScalarLoop("count");
            loop->accept(&struct_visitor);

            for (const auto& it : struct_visitor.getFieldTable()) {
                *dst << "Field = " << it.first;
                *dst << " size = " << it.second.fSize;
                *dst << " size_bytes = " << it.second.fSizeBytes;
                *dst << " read = " << it.second.fRAccessCount;
                *dst << " write = " << it.second.fWAccessCount;
                *dst << " ratio = "
                     << float(it.second.fRAccessCount + it.second.fWAccessCount) /
                            float(it.second.fSize)
                     << endl;
            }
        }
    }
}

void FIRCodeContainer::produceInternal()
{
    FIRInstVisitor firvisitor(fOut);
    *fOut << "======= Sub container \"" << fKlassName << "\" ==========" << endl;
    *fOut << endl;

    dumpGlobalsAndInit(firvisitor, fOut);
    dumpComputeBlock(firvisitor, fOut);
    dumpCompute(firvisitor, fOut);
}

void FIRCodeContainer::produceClass()
{
    FIRInstVisitor firvisitor(fOut);
    *fOut << "======= Container \"" << fKlassName << "\" ==========" << endl;
    *fOut << endl;

    *fOut << "======= External types declaration ==========" << endl;
    *fOut << endl;
    for (const auto& it : gGlobal->gExternalStructTypes) {
        (it.second)->accept(&firvisitor);
        *fOut << endl;
    }

    dumpSubContainers(firvisitor, fOut);
    dumpUserInterface(firvisitor, fOut);
    dumpGlobalsAndInit(firvisitor, fOut);
    dumpThread(firvisitor, fOut);
    dumpComputeBlock(firvisitor, fOut);
    dumpControlBlock(firvisitor, fOut);
    dumpCompute(firvisitor, fOut);
    dumpPostCompute(firvisitor, fOut);
    dumpFlatten(fOut);
    dumpMemory(fOut);
}

void FIRCodeContainer::dumpPostCompute(FIRInstVisitor& firvisitor, ostream* dst)
{
    *dst << "======= Post compute DSP begin ==========" << endl << endl;
    fPostComputeBlockInstructions->accept(&firvisitor);
    *dst << endl << "======= Post compute DSP end ==========" << endl << endl;
}

void FIRScalarCodeContainer::dumpCompute(FIRInstVisitor& firvisitor, ostream* dst)
{
    *dst << "======= Compute DSP begin ==========" << endl << endl;
    ForLoopInst* loop = fCurLoop->generateScalarLoop("count");
    // Complexity estimation
    dumpCost(loop, dst);
    loop->accept(&firvisitor);
    // Currently for soundfile management
    generatePostComputeBlock(&firvisitor);
    *dst << endl << "======= Compute DSP end ==========" << endl << endl;
}

void FIRVectorCodeContainer::dumpCompute(FIRInstVisitor& firvisitor, ostream* dst)
{
    *dst << "======= Compute DSP begin ==========" << endl << endl;
    // Complexity estimation
    dumpCost(fDAGBlock, dst);
    // Generates the DSP loop
    fDAGBlock->accept(&firvisitor);
    *dst << endl << "======= Compute DSP end ==========" << endl << endl;

    // Possibly generate separated functions
    if (fComputeFunctions->fCode.size() > 0) {
        *dst << "======= Separated functions begin ==========" << endl;
        *dst << endl;
        // Complexity estimation
        dumpCost(fComputeFunctions, dst);
        fComputeFunctions->accept(&firvisitor);
        *dst << endl << "======= Separated functions end ==========" << endl << endl;
    }
}

void FIROpenMPCodeContainer::dumpCompute(FIRInstVisitor& firvisitor, ostream* dst)
{
    *dst << "======= Compute DSP begin ==========" << endl << endl;
    // Complexity estimation
    dumpCost(fGlobalLoopBlock, dst);
    // Generate it
    fGlobalLoopBlock->accept(&firvisitor);
    *dst << endl << "======= Compute DSP end ==========" << endl << endl;

    // Possibly generate separated functions
    if (fComputeFunctions->fCode.size() > 0) {
        *dst << "======= Separated functions begin ==========" << endl;
        *dst << endl;
        // Complexity estimation
        dumpCost(fComputeFunctions, dst);
        fComputeFunctions->accept(&firvisitor);
        *dst << endl << "======= Separated functions end ==========" << endl << endl;
    }
}

void FIRWorkStealingCodeContainer::dumpCompute(FIRInstVisitor& firvisitor, ostream* dst)
{
    // Possibly generate separated functions
    if (fComputeFunctions->fCode.size() > 0) {
        *dst << "======= Separated functions begin ==========" << endl;
        *dst << endl;
        // Complexity estimation
        dumpCost(fComputeFunctions, dst);
        fComputeFunctions->accept(&firvisitor);
        *dst << endl << "======= Separated functions end ==========" << endl << endl;
    }
}

void FIRWorkStealingCodeContainer::dumpMemory(ostream* dst)
{
    // Compute memory footprint
    if (fTopLevel) {
        int total_heap_size = 0;
        for (const auto& it : fSubContainers) {
            VariableSizeCounter heap_counter(
                Address::AccessType(Address::kStruct | Address::kStaticStruct));
            it->generateDeclarations(&heap_counter);
            total_heap_size += heap_counter.fSizeBytes;
        }

        VariableSizeCounter heap_counter(
            Address::AccessType(Address::kStruct | Address::kStaticStruct));
        generateDeclarations(&heap_counter);

        VariableSizeCounter stack_counter_compute(Address::kStack);
        generateComputeBlock(&stack_counter_compute);

        VariableSizeCounter stack_counter_compute_thread(Address::kStack);
        fComputeThreadBlockInstructions->accept(&stack_counter_compute_thread);

        *dst << "======= Object memory footprint ==========\n\n";
        *dst << "Total heap size = " << heap_counter.fSizeBytes + total_heap_size << " bytes"
             << endl;
        *dst << "Stack size in compute = " << stack_counter_compute.fSizeBytes << " bytes" << endl;
        *dst << "Stack size in computeThread = " << stack_counter_compute_thread.fSizeBytes
             << " bytes"
             << "\n\n";
    }
}

void FIRWorkStealingCodeContainer::dumpThread(FIRInstVisitor& firvisitor, ostream* dst)
{
    // Generate it
    *dst << "======= Compute Thread begin ==========" << endl << endl;
    // Complexity estimation
    dumpCost(fThreadLoopBlock, dst);
    fThreadLoopBlock->accept(&firvisitor);
    *dst << endl << "======= Compute Thread end ==========" << endl << endl;
}