Skip to content

Commit e784280

Browse files
committed
Implement all delays types in C/C++ backends.
1 parent 0a4e6ac commit e784280

18 files changed

+435
-309
lines changed

compiler/generator/c/c_code_container.cpp

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -807,18 +807,8 @@ void CScalarCodeContainer::generateComputeAux(int n)
807807
tab(n + 1, *fOut);
808808
fCodeProducer->Tab(n + 1);
809809

810-
// Generates local variables declaration and setup
811-
generateComputeBlock(fCodeProducer);
812-
813-
// Generates one single scalar loop
814-
ForLoopInst* loop = fCurLoop->generateScalarLoop(fFullCount);
815-
loop->accept(fCodeProducer);
816-
817-
/*
818-
// TODO : atomic switch
819-
// Currently for soundfile management
820-
*/
821-
generatePostComputeBlock(fCodeProducer);
810+
BlockInst* block = generateComputeBlockLoop();
811+
block->accept(fCodeProducer);
822812

823813
back(1, *fOut);
824814
*fOut << "}" << endl;

compiler/generator/code_container.cpp

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,8 @@ CodeContainer::CodeContainer()
7171
fNumPassives(0),
7272
fSubContainerType(kInt),
7373
fGeneratedSR(false),
74+
fComputeByBlock(false),
75+
fComputeBlockIndex(""),
7476
fExtGlobalDeclarationInstructions(IB::genBlockInst()),
7577
fGlobalDeclarationInstructions(IB::genBlockInst()),
7678
fDeclarationInstructions(IB::genBlockInst()),
@@ -123,6 +125,80 @@ CodeContainer::CodeContainer()
123125
}
124126
}
125127

128+
void CodeContainer::setComputeByBlock(bool b)
129+
{
130+
fComputeByBlock = b;
131+
if (b && fComputeBlockIndex.empty()) {
132+
fComputeBlockIndex = gGlobal->getFreshID("index");
133+
}
134+
}
135+
136+
BlockInst* CodeContainer::generateComputeBlockLoop(bool loop_var_in_bytes)
137+
{
138+
BlockInst* block = IB::genBlockInst();
139+
140+
if (!fComputeByBlock) {
141+
block->merge(fComputeBlockInstructions);
142+
block->pushBackInst(fCurLoop->generateScalarLoop(fFullCount, loop_var_in_bytes));
143+
block->merge(fPostComputeBlockInstructions);
144+
return block;
145+
}
146+
147+
ValueInst* fullcount_value = IB::genLoadFunArgsVar(fFullCount);
148+
ValueInst* vec_size = IB::genInt32NumInst(gGlobal->gVecSize);
149+
150+
ValueInst* byte_size = nullptr;
151+
ValueInst* step = nullptr;
152+
153+
// int fullcount = count;
154+
DeclareVarInst* fullcount = nullptr;
155+
if (loop_var_in_bytes) {
156+
int byte_size_int = 1 << (gGlobal->gFloatSize + 1);
157+
byte_size = IB::genInt32NumInst(byte_size_int);
158+
fullcount = IB::genDecStackVar("fullcount", IB::genInt32Typed(),
159+
IB::genMul(byte_size, fullcount_value));
160+
step = IB::genInt32NumInst(gGlobal->gVecSize * byte_size_int);
161+
} else {
162+
fullcount = IB::genDecStackVar("fullcount", IB::genInt32Typed(), fullcount_value);
163+
step = vec_size;
164+
}
165+
block->pushBackInst(fullcount);
166+
167+
// for (int index = 0; index < fullcount; index += gVecSize)
168+
if (fComputeBlockIndex.empty()) {
169+
fComputeBlockIndex = gGlobal->getFreshID("index");
170+
}
171+
DeclareVarInst* index_decl =
172+
IB::genDecLoopVar(fComputeBlockIndex, IB::genInt32Typed(), IB::genInt32NumInst(0));
173+
174+
BlockInst* loop_code = IB::genBlockInst();
175+
176+
// int count = min(gVecSize, fullcount - index);
177+
ValueInst* remaining = IB::genSub(fullcount->load(), index_decl->load());
178+
ValueInst* count_base = remaining;
179+
if (loop_var_in_bytes) {
180+
count_base = IB::genDiv(remaining, byte_size);
181+
}
182+
Values min_fun_args;
183+
min_fun_args.push_back(vec_size);
184+
min_fun_args.push_back(count_base);
185+
ValueInst* count_val = IB::genFunCallInst("min_i", min_fun_args);
186+
DeclareVarInst* count_decl = IB::genDecStackVar("count", IB::genInt32Typed(), count_val);
187+
loop_code->pushBackInst(count_decl);
188+
189+
loop_code->merge(fComputeBlockInstructions);
190+
loop_code->pushBackInst(
191+
fCurLoop->generateScalarLoop(count_decl->load(), loop_var_in_bytes));
192+
loop_code->merge(fPostComputeBlockInstructions);
193+
194+
ValueInst* loop_end = IB::genLessThan(index_decl->load(), fullcount->load());
195+
StoreVarInst* loop_inc = index_decl->store(IB::genAdd(index_decl->load(), step));
196+
ForLoopInst* loop = IB::genForLoopInst(index_decl, loop_end, loop_inc, loop_code, false);
197+
block->pushBackInst(loop);
198+
199+
return block;
200+
}
201+
126202
int ZoneArray::gInternalMemorySize = 0;
127203

128204
CodeContainer::~CodeContainer()

compiler/generator/code_container.hh

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,8 @@ class CodeContainer : public virtual Garbageable {
7676

7777
int fSubContainerType;
7878
bool fGeneratedSR;
79+
bool fComputeByBlock{false}; ///< true if a compute method by block is needed
80+
std::string fComputeBlockIndex; ///< loop index name for block-based compute
7981

8082
MemoryLayoutType fMemoryLayout;
8183
std::string fKlassName;
@@ -338,6 +340,12 @@ class CodeContainer : public virtual Garbageable {
338340
virtual DeclareFunInst* generateComputeFun(const std::string& name, const std::string& obj,
339341
bool ismethod, bool isvirtual);
340342

343+
void setComputeByBlock(bool b);
344+
bool getComputeByBlock() const { return fComputeByBlock; }
345+
const std::string& getComputeBlockIndex() const { return fComputeBlockIndex; }
346+
347+
BlockInst* generateComputeBlockLoop(bool loop_var_in_bytes = false);
348+
341349
virtual BlockInst* generateComputeAux()
342350
{
343351
faustassert(false);

compiler/generator/cpp/cpp_code_container.cpp

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -947,18 +947,8 @@ void CPPScalarCodeContainer::generateCompute(int n)
947947
tab(n + 2, *fOut);
948948
fCodeProducer->Tab(n + 2);
949949

950-
// Generates local variables declaration and setup
951-
generateComputeBlock(fCodeProducer);
952-
953-
// Generates one single scalar loop
954-
ForLoopInst* loop = fCurLoop->generateScalarLoop(fFullCount);
955-
loop->accept(fCodeProducer);
956-
957-
/*
958-
// TODO : atomic switch
959-
// Currently for soundfile management
960-
*/
961-
generatePostComputeBlock(fCodeProducer);
950+
BlockInst* block = generateComputeBlockLoop();
951+
block->accept(fCodeProducer);
962952

963953
back(1, *fOut);
964954
*fOut << "}";

compiler/generator/fir/fir_code_container.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,9 @@ static void dumpCost(StatementInst* inst, ostream* dst)
156156

157157
void FIRCodeContainer::dumpComputeBlock(FIRInstVisitor& firvisitor, ostream* dst)
158158
{
159+
if (getComputeByBlock()) {
160+
return;
161+
}
159162
if (fComputeBlockInstructions->fCode.size() > 0) {
160163
*dst << "======= Compute control begin ==========" << endl << endl;
161164
// Complexity estimation
@@ -323,6 +326,9 @@ void FIRCodeContainer::produceClass()
323326

324327
void FIRCodeContainer::dumpPostCompute(FIRInstVisitor& firvisitor, ostream* dst)
325328
{
329+
if (getComputeByBlock()) {
330+
return;
331+
}
326332
*dst << "======= Post compute DSP begin ==========" << endl << endl;
327333
fPostComputeBlockInstructions->accept(&firvisitor);
328334
*dst << endl << "======= Post compute DSP end ==========" << endl << endl;

0 commit comments

Comments
 (0)