From 150547b560c1259225712369df330a5e3266a2d7 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?S=C3=A9bastien=20Villemot?= <sebastien@dynare.org>
Date: Fri, 12 May 2023 17:22:02 +0200
Subject: [PATCH] =?UTF-8?q?=F0=9F=90=9B=20Bytecode=20+=20block=20decomposi?=
 =?UTF-8?q?tion:=20temporary=20terms=20were=20not=20correctly=20output?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

Temporary terms computed in previous blocks were not used in the bytecode
output of a given block. This was inefficient (because this means that
expressions already computed and store in the temporary terms vector would be
recomputed), and incidentally it would break the external functions
output (because it would trigger a lookup in the “TEF terms”, which would thus
fail).

Closes: #115
---
 src/DynamicModel.cc | 4 +++-
 src/ModelTree.hh    | 5 ++---
 src/StaticModel.cc  | 4 +++-
 3 files changed, 8 insertions(+), 5 deletions(-)

diff --git a/src/DynamicModel.cc b/src/DynamicModel.cc
index e225d48d..ebaa5152 100644
--- a/src/DynamicModel.cc
+++ b/src/DynamicModel.cc
@@ -209,6 +209,8 @@ DynamicModel::writeDynamicBlockBytecode(const string &basename) const
   // Temporary variables declaration
   code_file << FDIMT_{static_cast<int>(blocks_temporary_terms_idxs.size())};
 
+  temporary_terms_t temporary_terms_written;
+
   for (int block {0}; block < static_cast<int>(blocks.size()); block++)
     {
       const BlockSimulationType simulation_type {blocks[block].simulation_type};
@@ -231,7 +233,7 @@ DynamicModel::writeDynamicBlockBytecode(const string &basename) const
                                 u_count,
                                 static_cast<int>(blocks_jacob_cols_endo[block].size())};
 
-      writeBlockBytecodeHelper<true>(code_file, block);
+      writeBlockBytecodeHelper<true>(code_file, block, temporary_terms_written);
     }
   code_file << FEND_{};
 }
diff --git a/src/ModelTree.hh b/src/ModelTree.hh
index 2cc2be0c..0c0044a4 100644
--- a/src/ModelTree.hh
+++ b/src/ModelTree.hh
@@ -333,7 +333,7 @@ protected:
 
   // Helper for writing blocks in bytecode
   template<bool dynamic>
-  void writeBlockBytecodeHelper(BytecodeWriter &code_file, int block) const;
+  void writeBlockBytecodeHelper(BytecodeWriter &code_file, int block, temporary_terms_t &temporary_terms_union) const;
 
   // Helper for writing sparse derivatives indices in MATLAB/Octave driver file
   template<bool dynamic>
@@ -1642,7 +1642,7 @@ ModelTree::writeBytecodeHelper(BytecodeWriter &code_file) const
 
 template<bool dynamic>
 void
-ModelTree::writeBlockBytecodeHelper(BytecodeWriter &code_file, int block) const
+ModelTree::writeBlockBytecodeHelper(BytecodeWriter &code_file, int block, temporary_terms_t &temporary_terms_union) const
 {
   constexpr ExprNodeBytecodeOutputType output_type
     { dynamic ? ExprNodeBytecodeOutputType::dynamicModel : ExprNodeBytecodeOutputType::staticModel };
@@ -1654,7 +1654,6 @@ ModelTree::writeBlockBytecodeHelper(BytecodeWriter &code_file, int block) const
   const int block_mfs {blocks[block].mfs_size};
   const int block_recursive {blocks[block].getRecursiveSize()};
 
-  temporary_terms_t temporary_terms_union;
   deriv_node_temp_terms_t tef_terms;
 
   auto write_eq_tt = [&](int eq)
diff --git a/src/StaticModel.cc b/src/StaticModel.cc
index 3282d4e9..7b59a287 100644
--- a/src/StaticModel.cc
+++ b/src/StaticModel.cc
@@ -143,6 +143,8 @@ StaticModel::writeStaticBlockBytecode(const string &basename) const
   // Temporary variables declaration
   code_file << FDIMST_{static_cast<int>(blocks_temporary_terms_idxs.size())};
 
+  temporary_terms_t temporary_terms_written;
+
   for (int block {0}; block < static_cast<int>(blocks.size()); block++)
     {
       const BlockSimulationType simulation_type {blocks[block].simulation_type};
@@ -163,7 +165,7 @@ StaticModel::writeStaticBlockBytecode(const string &basename) const
                                 u_count,
                                 block_size};
 
-      writeBlockBytecodeHelper<false>(code_file, block);
+      writeBlockBytecodeHelper<false>(code_file, block, temporary_terms_written);
     }
   code_file << FEND_{};
 }
-- 
GitLab