File: interpreter_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 (381 lines) | stat: -rw-r--r-- 17,582 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
/************************************************************************
 ************************************************************************
    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 <cstdlib>

#include "Text.hh"
#include "exception.hh"
#include "floats.hh"
#include "global.hh"
#include "interpreter_code_container.hh"
#include "interpreter_instructions.hh"

using namespace std;

/*
Interpreter backend description:

 - FIR code is transpiled in Faust Byte Code (FBC), to be executed in an interpreter or an hybrid
interpreter/compiler
 - a single global visitor for main and subcontainers
 - 'fSampleRate' and 'count' variable manually added in the IntHeap to be setup in 'instanceInit'
and 'compute'
 - DSP struct and stack variables are actually allocated in the Int32 and REAL heaps
 - 'faustpower' function fallbacks to regular 'pow' (see powprim.h)
 - subcontainers code is 'inlined': fields declarations (using the global visitor) and code
'classInit', and 'instanceInit' of the main container
 - 'clone' method is implemented in the 'interpreter_dsp' wrapping code
 - the backend exits in 3 versions:
    - pure Interpreter model: the slowest (FBCInterpreter class)
    - hybrid interpreter/MIR compiler (FBCLLVMCompiler class): the 'init' functions done once are
interpreted, the hot 'compute' function is compiled to native using MIR machinery, faster
    - hybrid interpreter/LLVM compiler (FBCMIRCompiler class): the 'init' functions done once are
interpreted, the hot 'compute' function is compiled to native using LLVM machinery, even faster, but
slower that the pure LLVM backend
 - soundfile support:
    - Soundfile* pointers are kept in FBCExecutor::fSoundTable map
    - this fSoundTable is filled in FBCInterpreter::executeBuildUserInterface when executing
FBCInstruction::kAddSoundfile, triggered by 'buildUserInterface', so has to be done at least once
before calling DSP 'init'.
    - the FBCInstruction::kLoadSoundFieldInt and FBCInstruction::kLoadSoundFieldReal FBC
instructions directly access the prepared fSoundTable in Interp mode. In Interp/[LLVM|MIR] they are
compiled as access in a module global soundfile table built at construction time (see
FBCLLVMCompiler/FBCMIRCompiler constructors).

TODO: in -mem mode, classInit and classDestroy will have to be called once at factory init and
destroy time (after global memory allocation is implemented)
*/

template <class REAL>
map<string, FBCInstruction::Opcode> InterpreterInstVisitor<REAL>::gMathLibTable;

template <class REAL>
static FBCBlockInstruction<REAL>* getCurrentBlock()
{
    FBCBlockInstruction<REAL>* block =
        static_cast<InterpreterInstVisitor<REAL>*>(gGlobal->gInterpreterVisitor)->fCurrentBlock;
    // Add kReturn in generated block
    block->push(new FBCBasicInstruction<REAL>(FBCInstruction::kReturn));
    return block;
}

template <class REAL>
static InterpreterInstVisitor<REAL>* getInterpreterVisitor()
{
    return static_cast<InterpreterInstVisitor<REAL>*>(gGlobal->gInterpreterVisitor);
}

template <class REAL>
static void setCurrentBlock(FBCBlockInstruction<REAL>* block)
{
    static_cast<InterpreterInstVisitor<REAL>*>(gGlobal->gInterpreterVisitor)->fCurrentBlock = block;
}

template <class REAL>
InterpreterCodeContainer<REAL>::InterpreterCodeContainer(const string& name, int numInputs,
                                                         int numOutputs)
{
    initialize(numInputs, numOutputs);
    fKlassName = name;

    // Allocate one static visitor to be shared by main module and sub containers
    if (!gGlobal->gInterpreterVisitor) {
        gGlobal->gInterpreterVisitor = new InterpreterInstVisitor<REAL>();
    }
}

template <class REAL>
CodeContainer* InterpreterCodeContainer<REAL>::createScalarContainer(const string& name,
                                                                     int sub_container_type)
{
    return new InterpreterScalarCodeContainer<REAL>(name, 0, 1, sub_container_type);
}

template <class REAL>
CodeContainer* InterpreterCodeContainer<REAL>::createContainer(const string& name, int numInputs,
                                                               int numOutputs)
{
    CodeContainer* container;

    if (gGlobal->gOpenCLSwitch) {
        throw faustexception("ERROR : OpenCL not supported for Interpreter\n");
    }
    if (gGlobal->gCUDASwitch) {
        throw faustexception("ERROR : CUDA not supported for Interpreter\n");
    }

    if (gGlobal->gOpenMPSwitch) {
        throw faustexception("ERROR : OpenMP not supported for Interpreter\n");
    } else if (gGlobal->gSchedulerSwitch) {
        throw faustexception("ERROR : Scheduler mode not supported for Interpreter\n");
    } else if (gGlobal->gVectorSwitch) {
        // throw faustexception("ERROR : Vector mode not supported for Interpreter\n");
        if (gGlobal->gVectorLoopVariant == 0) {
            throw faustexception("ERROR : Vector mode with -lv 0 not supported for Interpreter\n");
        }
        container = new InterpreterVectorCodeContainer<REAL>(name, numInputs, numOutputs);
    } else {
        container = new InterpreterScalarCodeContainer<REAL>(name, numInputs, numOutputs, kInt);
    }

    return container;
}

// Scalar
template <class REAL>
InterpreterScalarCodeContainer<REAL>::InterpreterScalarCodeContainer(const string& name,
                                                                     int numInputs, int numOutputs,
                                                                     int sub_container_type)
    : InterpreterCodeContainer<REAL>(name, numInputs, numOutputs)
{
    this->fSubContainerType = sub_container_type;
}

template <class REAL>
InterpreterScalarCodeContainer<REAL>::~InterpreterScalarCodeContainer()
{
}

template <class REAL>
dsp_factory_base* InterpreterCodeContainer<REAL>::produceFactory()
{
    // "count" variable added to be setup later by 'compute'
    pushDeclare(IB::genDecStructVar("count", IB::genInt32Typed()));

    // Sub containers are merged
    mergeSubContainers();

    generateGlobalDeclarations(gGlobal->gInterpreterVisitor);
    generateDeclarations(gGlobal->gInterpreterVisitor);

    // Rename 'sig' in 'dsp', remove 'dsp' allocation, inline subcontainers 'instanceInit' and
    // 'fill' function call
    inlineSubcontainersFunCalls(fStaticInitInstructions)->accept(gGlobal->gInterpreterVisitor);

    // Keep "init_static_block"
    FBCBlockInstruction<REAL>* init_static_block = getCurrentBlock<REAL>();
    setCurrentBlock<REAL>(new FBCBlockInstruction<REAL>());

    // Rename 'sig' in 'dsp', remove 'dsp' allocation, inline subcontainers 'instanceInit' and
    // 'fill' function call
    inlineSubcontainersFunCalls(fInitInstructions)->accept(gGlobal->gInterpreterVisitor);

    // Keep "init_block"
    FBCBlockInstruction<REAL>* init_block = getCurrentBlock<REAL>();
    setCurrentBlock<REAL>(new FBCBlockInstruction<REAL>);

    // Keep "resetui_block"
    generateResetUserInterface(gGlobal->gInterpreterVisitor);
    FBCBlockInstruction<REAL>* resetui_block = getCurrentBlock<REAL>();
    setCurrentBlock<REAL>(new FBCBlockInstruction<REAL>);

    // Keep "clear_block"
    generateClear(gGlobal->gInterpreterVisitor);
    FBCBlockInstruction<REAL>* clear_block = getCurrentBlock<REAL>();
    setCurrentBlock<REAL>(new FBCBlockInstruction<REAL>);

    // Generate UI
    generateUserInterface(gGlobal->gInterpreterVisitor);

    // Generate local variables declaration and setup
    generateComputeBlock(gGlobal->gInterpreterVisitor);

    // Keep "compute_control_block"
    FBCBlockInstruction<REAL>* compute_control_block = getCurrentBlock<REAL>();
    setCurrentBlock<REAL>(new FBCBlockInstruction<REAL>);

    // Keep "compute_dsp_block"
    FBCBlockInstruction<REAL>* compute_dsp_block = generateCompute();

    // Generate metadata block and name
    string                   name;
    FIRMetaBlockInstruction* metadata_block = produceMetadata(name);

    // Then create factory depending of the trace mode
    const char* trace = getenv("FAUST_INTERP_TRACE");
    int         mode  = (trace) ? std::atoi(trace) : 0;

    // Prepare compilation options
    stringstream compile_options;
    gGlobal->printCompilationOptions(compile_options);

    switch (mode) {
#if defined(INTERP_BUILD)
        case 1:
            return new interpreter_dsp_factory_aux<REAL, 1>(
                name, compile_options.str(), "", INTERP_FILE_VERSION, fNumInputs, fNumOutputs,
                getInterpreterVisitor<REAL>()->fIntHeapOffset,
                getInterpreterVisitor<REAL>()->fRealHeapOffset,
                getInterpreterVisitor<REAL>()->getFieldOffset("fSampleRate"),
                getInterpreterVisitor<REAL>()->getFieldOffset("count"),
                getInterpreterVisitor<REAL>()->getFieldOffset("IOTA"), INTER_MAX_OPT_LEVEL,
                metadata_block, getInterpreterVisitor<REAL>()->fUserInterfaceBlock,
                init_static_block, init_block, resetui_block, clear_block, compute_control_block,
                compute_dsp_block);

        case 2:
            return new interpreter_dsp_factory_aux<REAL, 2>(
                name, compile_options.str(), "", INTERP_FILE_VERSION, fNumInputs, fNumOutputs,
                getInterpreterVisitor<REAL>()->fIntHeapOffset,
                getInterpreterVisitor<REAL>()->fRealHeapOffset,
                getInterpreterVisitor<REAL>()->getFieldOffset("fSampleRate"),
                getInterpreterVisitor<REAL>()->getFieldOffset("count"),
                getInterpreterVisitor<REAL>()->getFieldOffset("IOTA"), INTER_MAX_OPT_LEVEL,
                metadata_block, getInterpreterVisitor<REAL>()->fUserInterfaceBlock,
                init_static_block, init_block, resetui_block, clear_block, compute_control_block,
                compute_dsp_block);

        case 3:
            return new interpreter_dsp_factory_aux<REAL, 3>(
                name, compile_options.str(), "", INTERP_FILE_VERSION, fNumInputs, fNumOutputs,
                getInterpreterVisitor<REAL>()->fIntHeapOffset,
                getInterpreterVisitor<REAL>()->fRealHeapOffset,
                getInterpreterVisitor<REAL>()->getFieldOffset("fSampleRate"),
                getInterpreterVisitor<REAL>()->getFieldOffset("count"),
                getInterpreterVisitor<REAL>()->getFieldOffset("IOTA"), INTER_MAX_OPT_LEVEL,
                metadata_block, getInterpreterVisitor<REAL>()->fUserInterfaceBlock,
                init_static_block, init_block, resetui_block, clear_block, compute_control_block,
                compute_dsp_block);

        case 4:
            return new interpreter_dsp_factory_aux<REAL, 4>(
                name, compile_options.str(), "", INTERP_FILE_VERSION, fNumInputs, fNumOutputs,
                getInterpreterVisitor<REAL>()->fIntHeapOffset,
                getInterpreterVisitor<REAL>()->fRealHeapOffset,
                getInterpreterVisitor<REAL>()->getFieldOffset("fSampleRate"),
                getInterpreterVisitor<REAL>()->getFieldOffset("count"),
                getInterpreterVisitor<REAL>()->getFieldOffset("IOTA"), INTER_MAX_OPT_LEVEL,
                metadata_block, getInterpreterVisitor<REAL>()->fUserInterfaceBlock,
                init_static_block, init_block, resetui_block, clear_block, compute_control_block,
                compute_dsp_block);

        case 5:
            return new interpreter_dsp_factory_aux<REAL, 5>(
                name, compile_options.str(), "", INTERP_FILE_VERSION, fNumInputs, fNumOutputs,
                getInterpreterVisitor<REAL>()->fIntHeapOffset,
                getInterpreterVisitor<REAL>()->fRealHeapOffset,
                getInterpreterVisitor<REAL>()->getFieldOffset("fSampleRate"),
                getInterpreterVisitor<REAL>()->getFieldOffset("count"),
                getInterpreterVisitor<REAL>()->getFieldOffset("IOTA"), INTER_MAX_OPT_LEVEL,
                metadata_block, getInterpreterVisitor<REAL>()->fUserInterfaceBlock,
                init_static_block, init_block, resetui_block, clear_block, compute_control_block,
                compute_dsp_block);

        case 6:
            return new interpreter_dsp_factory_aux<REAL, 6>(
                name, compile_options.str(), "", INTERP_FILE_VERSION, fNumInputs, fNumOutputs,
                getInterpreterVisitor<REAL>()->fIntHeapOffset,
                getInterpreterVisitor<REAL>()->fRealHeapOffset,
                getInterpreterVisitor<REAL>()->getFieldOffset("fSampleRate"),
                getInterpreterVisitor<REAL>()->getFieldOffset("count"),
                getInterpreterVisitor<REAL>()->getFieldOffset("IOTA"), INTER_MAX_OPT_LEVEL,
                metadata_block, getInterpreterVisitor<REAL>()->fUserInterfaceBlock,
                init_static_block, init_block, resetui_block, clear_block, compute_control_block,
                compute_dsp_block);

        default:
            // Default case, no trace...
            return new interpreter_dsp_factory_aux<REAL, 0>(
                name, compile_options.str(), "", INTERP_FILE_VERSION, fNumInputs, fNumOutputs,
                getInterpreterVisitor<REAL>()->fIntHeapOffset,
                getInterpreterVisitor<REAL>()->fRealHeapOffset,
                getInterpreterVisitor<REAL>()->getFieldOffset("fSampleRate"),
                getInterpreterVisitor<REAL>()->getFieldOffset("count"),
                getInterpreterVisitor<REAL>()->getFieldOffset("IOTA"), INTER_MAX_OPT_LEVEL,
                metadata_block, getInterpreterVisitor<REAL>()->fUserInterfaceBlock,
                init_static_block, init_block, resetui_block, clear_block, compute_control_block,
                compute_dsp_block);
#elif defined(INTERP_COMP_BUILD)
        default:
            // Default case, no trace...
            return new interpreter_comp_dsp_factory_aux<REAL, 0>(
                name, compile_options.str(), "", INTERP_FILE_VERSION, fNumInputs, fNumOutputs,
                getInterpreterVisitor<REAL>()->fIntHeapOffset,
                getInterpreterVisitor<REAL>()->fRealHeapOffset,
                getInterpreterVisitor<REAL>()->getFieldOffset("fSampleRate"),
                getInterpreterVisitor<REAL>()->getFieldOffset("count"),
                getInterpreterVisitor<REAL>()->getFieldOffset("IOTA"), INTER_MAX_OPT_LEVEL,
                metadata_block, getInterpreterVisitor<REAL>()->fUserInterfaceBlock,
                init_static_block, init_block, resetui_block, clear_block, compute_control_block,
                compute_dsp_block);
#endif
    }
}

template <class REAL>
FBCBlockInstruction<REAL>* InterpreterScalarCodeContainer<REAL>::generateCompute()
{
    BlockInst* compute_block = IB::genBlockInst();
    compute_block->pushBackInst(this->fCurLoop->generateScalarLoop(fFullCount));

    // Generates post DSP loop code
    compute_block->pushBackInst(this->fPostComputeBlockInstructions);

    compute_block->accept(gGlobal->gInterpreterVisitor);
    return getCurrentBlock<REAL>();
}

template <class REAL>
FBCBlockInstruction<REAL>* InterpreterVectorCodeContainer<REAL>::generateCompute()
{
    // Rename all loop variables name to avoid name clash
    LoopVariableRenamer loop_renamer;
    loop_renamer.getCode(this->fDAGBlock)->accept(gGlobal->gInterpreterVisitor);

    return getCurrentBlock<REAL>();
}

template <class REAL>
FIRMetaBlockInstruction* InterpreterCodeContainer<REAL>::produceMetadata(string& name)
{
    FIRMetaBlockInstruction* block = new FIRMetaBlockInstruction();

    // Add global metadata
    for (const auto& it : gGlobal->gMetaDataSet) {
        if (it.first != tree("author")) {
            stringstream str1, str2;
            str1 << *(it.first);
            str2 << **(it.second.begin());
            if (str1.str() == "name") {
                name = unquote(str2.str());
            }
            block->push(new FIRMetaInstruction(str1.str(), unquote(str2.str())));
        } else {
            for (set<Tree>::iterator j = it.second.begin(); j != it.second.end(); j++) {
                if (j == it.second.begin()) {
                    stringstream str1, str2;
                    str1 << *(it.first);
                    str2 << **j;
                    if (str1.str() == "name") {
                        name = unquote(str2.str());
                    }
                    block->push(new FIRMetaInstruction(str1.str(), unquote(str2.str())));
                } else {
                    stringstream str2;
                    str2 << **j;
                    block->push(new FIRMetaInstruction("contributor", unquote(str2.str())));
                }
            }
        }
    }

    return block;
}