Skip to content

Commit

Permalink
Rust: implementation of the os flag
Browse files Browse the repository at this point in the history
  • Loading branch information
crop2000 committed Nov 29, 2024
1 parent 69213c0 commit 5cb2513
Show file tree
Hide file tree
Showing 9 changed files with 606 additions and 27 deletions.
56 changes: 51 additions & 5 deletions compiler/generator/rust/rust_code_container.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -523,8 +523,12 @@ void RustCodeContainer::produceClass()
}

// Compute
generateCompute(n + 1);
generateComputeInterface(n + 1);
if (gGlobal->gOneSample) {
generateComputeFrame(n + 1);
} else {
generateCompute(n + 1);
generateComputeInterface(n + 1);
}

tab(n, *fOut);
*fOut << "}" << endl;
Expand Down Expand Up @@ -632,13 +636,54 @@ void RustCodeContainer::generateComputeHeader(int n, std::ostream* fOut, int fNu
void RustCodeContainer::generateComputeInterfaceHeader(int n, std::ostream* fOut, int fNumInputs,
int fNumOutputs)
{
// Compute "compute" declaration
*fOut << "pub fn compute("
<< "&mut self, " << fFullCount << ": usize, inputs: & [& [FaustFloat] ]"
<< ", outputs: & mut[& mut[FaustFloat] ]) {";
tab(n + 1, *fOut);
}

void RustCodeContainer::generateComputeFrame(int n)
{
tab(n, *fOut);
tab(n, *fOut);
*fOut << "pub fn compute_frame(&mut self, inputs: &[&FaustFloat], ";
*fOut << "outputs: &mut [&mut FaustFloat]) {";

for(int i = 0; i < fNumInputs; i++){
tab(n+1, *fOut);
*fOut << "let input"<< i <<" = inputs["<< i <<"];";
};

for(int i = 0; i < fNumOutputs; i++){
tab(n+1, *fOut);
*fOut << "let (nextoutput, outputs): (&mut [&mut f64], &mut [&mut f64]) = outputs.split_at_mut(1);";
tab(n+1, *fOut);
*fOut << "let output"<< i <<": &mut FaustFloat = nextoutput[0];";
};

fCodeProducer.Tab(n + 1);

tab(n+1, *fOut);
generateComputeBlock(&fCodeProducer);

tab(n+1, *fOut);
*fOut << "//generateOneSample";
tab(n+1, *fOut);
// Generates one sample computation
BlockInst* block = fCurLoop->generateOneSample();
block->accept(&fCodeProducer);

/*
// TODO : atomic switch
// Currently for soundfile management
// also for temp vars
*/
generatePostComputeBlock(&fCodeProducer);
tab(n, *fOut);
*fOut << "}";
tab(n, *fOut);
}

void RustCodeContainer::generateComputeInterface(int n)
{
// Generates declaration
Expand Down Expand Up @@ -672,7 +717,7 @@ void RustScalarCodeContainer::generateCompute(int n)
tab(n + 1, *fOut);
fCodeProducer.Tab(n + 1);

// Generates local variables declaration and setup

generateComputeBlock(&fCodeProducer);

// Generates one single scalar loop
Expand All @@ -687,9 +732,10 @@ void RustScalarCodeContainer::generateCompute(int n)
loop->accept(&fCodeProducer);

// Currently for soundfile management
// and for temp vars
generatePostComputeBlock(&fCodeProducer);

back(1, *fOut);
tab(n, *fOut);
*fOut << "}" << endl;
}

Expand Down
1 change: 1 addition & 0 deletions compiler/generator/rust/rust_code_container.hh
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ class RustCodeContainer : public virtual CodeContainer {
void generateComputeHeader(int n, std::ostream* fOut, int fNumInputs, int fNumOutputs);
void generateComputeInterfaceHeader(int n, std::ostream* fOut, int fNumInputs, int fNumOutputs);
void generateComputeInterface(int tab);
void generateComputeFrame(int tab);
virtual void generateCompute(int tab) = 0;
void produceInternal();
void produceFaustDspBlob();
Expand Down
15 changes: 5 additions & 10 deletions compiler/generator/rust/rust_instructions.hh
Original file line number Diff line number Diff line change
Expand Up @@ -225,17 +225,13 @@ class RustInstVisitor : public TextInstVisitor {
virtual void visit(DeclareBufferIterators* inst)
{
/* Generates an expression like:
let (outputs0, outputs1) = if let [outputs0, outputs1, ..] = outputs {
let outputs0 = outputs0[..count as usize].iter_mut();
let outputs1 = outputs1[..count as usize].iter_mut();
(outputs0, outputs1)
} else {
panic!("wrong number of outputs");
};
let [outputs0, outputs1, ..] = outputs;
let outputs0 = outputs0[..count].iter_mut();
let outputs1 = outputs1[..count].iter_mut();
*/

// Don't generate if no channels
if (inst->fChannels == 0) {
if (inst->fChannels == 0 || gGlobal->gOneSample) {
return;
}

Expand All @@ -248,10 +244,9 @@ class RustInstVisitor : public TextInstVisitor {
*fOut << "] = " << name << ";";

// Build fixed size iterator variables

for (int i = 0; i < inst->fChannels; ++i) {
tab(fTab, *fOut);
*fOut << "let " << name << i << " = " << name << i << "[..count as usize]";
*fOut << "let " << name << i << " = " << name << i << "[..count]";
if (inst->fMutable) {
if (inst->fChunk) {
*fOut << ".chunks_mut(vsize as usize);";
Expand Down
4 changes: 2 additions & 2 deletions compiler/global.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1682,9 +1682,9 @@ bool global::processCmdline(int argc, const char* argv[])
}
#endif
if (gOneSample && gOutputLang != "cpp" && gOutputLang != "c" && gOutputLang != "dlang" &&
!startWith(gOutputLang, "cmajor") && gOutputLang != "fir") {
!startWith(gOutputLang, "cmajor") && gOutputLang != "fir" && gOutputLang != "rust") {
throw faustexception(
"ERROR : '-os' option can only be used with 'cpp', 'c', 'fir' or 'cmajor' "
"ERROR : '-os' option can only be used with 'cpp', 'c', 'dlang', 'cmajor', 'fir' or 'rust'"
"backends\n");
}

Expand Down
3 changes: 3 additions & 0 deletions tests/impulse-tests/Make.gcc
Original file line number Diff line number Diff line change
Expand Up @@ -178,5 +178,8 @@ ir/$(outdir)/prefix.ir:
ir/cpp/double/mapp/constant.ir:
echo "todo fix bug #1074 to test dsp/constant.dsp"

ir/cpp2/double/os/bs.cpp
@echo "accessing foreign variable 'count' is not allowed in this compilation mode[os]"

ir/$(outdir)/%.$(ext) : dsp/%.dsp
$(FAUST) -lang $(lang) $(FAUSTOPTIONS) -i -A ../../architecture -a archs/$(arch) $< -o $@
18 changes: 13 additions & 5 deletions tests/impulse-tests/Make.rust
Original file line number Diff line number Diff line change
Expand Up @@ -41,22 +41,30 @@ filesCompare:
#########################################################################
# rules

ir/$(outdir)/sound.ir: dsp/sound.dsp
ir/$(outdir)/sound.ir: dsp/sound.dsp
$(FAUST) -lang rust $(FAUSTOPTIONS) dsp/sound.dsp -o archs/rust/src/bin/sound.rs > $@ 2>&1 || (echo "expected failure")
grep "ERROR : 'soundfile' primitive not yet supported for Rust" $@

ir/rust/vec4/osc_enable.ir: dsp/osc_enable.dsp
ir/rust/osec/bs.ir: dsp/bs.dsp
$(FAUST) -lang rust $(FAUSTOPTIONS) dsp/bs.dsp -o archs/rust/src/bin/bs.rs > $@ 2>&1 || (echo "expected failure")
grep "ERROR : accessing foreign variable 'count' is not allowed in this compilation mode" $@

ir/rust/os/bs.ir: dsp/bs.dsp
$(FAUST) -lang rust $(FAUSTOPTIONS) dsp/bs.dsp -o archs/rust/src/bin/bs.rs > $@ 2>&1 || (echo "expected failure")
grep "ERROR : accessing foreign variable 'count' is not allowed in this compilation mode" $@

ir/rust/vec4/osc_enable.ir: dsp/osc_enable.dsp
$(FAUST) -lang rust $(FAUSTOPTIONS) dsp/osc_enable.dsp -o archs/rust/src/bin/osc_enable.rs > $@ 2>&1 || (echo "expected failure")
grep "ERROR : 'control/enable' can only be used in scalar mode" $@

ir/rust/vec32/osc_enable.ir: dsp/osc_enable.dsp
ir/rust/vec32/osc_enable.ir: dsp/osc_enable.dsp
$(FAUST) -lang rust $(FAUSTOPTIONS) dsp/osc_enable.dsp -o archs/rust/src/bin/osc_enable.rs > $@ 2>&1 || (echo "expected failure")
grep "ERROR : 'control/enable' can only be used in scalar mode" $@

ir/rust/vec4/prefix.ir:
ir/rust/vec4/prefix.ir:
echo "todo fix bug #1071 to test dsp/prefix.dsp"

ir/rust/vec32/prefix.ir:
ir/rust/vec32/prefix.ir:
echo "todo fix bug #1071 to test dsp/prefix.dsp"

ir/$(outdir)/%.ir: dsp/%.dsp reference/%.ir
Expand Down
13 changes: 8 additions & 5 deletions tests/impulse-tests/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -122,10 +122,11 @@ travis:
#########################################################################
# automatic github action test
github_action:
$(MAKE) -f Make.rust outdir=rust/rnt CARGOOPTIONS="" FAUSTOPTIONS="-I ../../libraries/ -double -cm -a archs/rust/architecture_cm.rs"
$(MAKE) -f Make.rust outdir=rust/rnt CARGOOPTIONS="" FAUSTOPTIONS="-I ../../libraries/ -double -ec -rnt -a archs/rust/architecture_ecrnt.rs"
$(MAKE) -f Make.rust outdir=rust/os CARGOOPTIONS="" FAUSTOPTIONS="-I ../../libraries/ -double -os -ec -rnt -a archs/rust/architecture_osecrnt.rs"
$(MAKE) -f Make.rust outdir=rust/os CARGOOPTIONS="" FAUSTOPTIONS="-I ../../libraries/ -double -os -rnt -a archs/rust/architecture_osrnt.rs"
$(MAKE) -f Make.rust outdir=rust/no CARGOOPTIONS="" FAUSTOPTIONS="-I ../../libraries/ -double -a archs/rust/architecture_trait.rs"
$(MAKE) -f Make.gcc outdir=cpp/double lang=cpp arch=impulsearch.cpp FAUSTOPTIONS="-I ../../libraries/ -I dsp -double"
$(MAKE) -f Make.gcc outdir=cpp2/double/os lang=cpp arch=impulsearch7ter.cpp FAUSTOPTIONS="-I dsp -double -os"
# $(MAKE) -f Make.gcc outdir=cpp/double lang=cpp arch=impulsearch.cpp FAUSTOPTIONS="-I ../../libraries/ -I dsp -double"

#########################################################################
# c++ and c backends
Expand Down Expand Up @@ -370,8 +371,10 @@ interp1:
#########################################################################
# Rust backend
rust:
$(MAKE) -f Make.rust outdir=rust/ec CARGOOPTIONS="" FAUSTOPTIONS="-I ../../libraries/ -double -cm -a archs/rust/architecture_cm.rs"
$(MAKE) -f Make.rust outdir=rust/ec CARGOOPTIONS="" FAUSTOPTIONS="-I ../../libraries/ -double -ec -rnt -a archs/rust/architecture_ecrnt.rs"
$(MAKE) -f Make.rust outdir=rust/osec CARGOOPTIONS="" FAUSTOPTIONS="-I ../../libraries/ -double -os -ec -rnt -a archs/rust/architecture_osecrnt.rs"
$(MAKE) -f Make.rust outdir=rust/os CARGOOPTIONS="" FAUSTOPTIONS="-I ../../libraries/ -double -os -rnt -a archs/rust/architecture_osrnt.rs"
$(MAKE) -f Make.rust outdir=rust/cm CARGOOPTIONS="" FAUSTOPTIONS="-I ../../libraries/ -double -cm -a archs/rust/architecture_cm.rs"
$(MAKE) -f Make.rust outdir=rust/ecrnt CARGOOPTIONS="" FAUSTOPTIONS="-I ../../libraries/ -double -ec -rnt -a archs/rust/architecture_ecrnt.rs"
$(MAKE) -f Make.rust outdir=rust/no CARGOOPTIONS="" FAUSTOPTIONS="-I ../../libraries/ -double -a archs/rust/architecture_trait.rs"
$(MAKE) -f Make.rust outdir=rust/fp FAUSTOPTIONS="-I ../../libraries/ -double -fp -a archs/rust/architecture_trait.rs"
$(MAKE) -f Make.rust outdir=rust/vec4 CARGOOPTIONS="" FAUSTOPTIONS="-I ../../libraries/ -double -vec -vs 4 -a archs/rust/architecture_trait.rs"
Expand Down
Loading

0 comments on commit 5cb2513

Please sign in to comment.