Skip to content

Commit a220400

Browse files
committed
LLVMCodeBuilder: Initialize variable stack copies outside loops
1 parent 0844b1c commit a220400

File tree

1 file changed

+12
-1
lines changed

1 file changed

+12
-1
lines changed

src/engine/internal/llvm/llvmcodebuilder.cpp

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,18 @@ std::shared_ptr<ExecutableCode> LLVMCodeBuilder::finalize()
106106
// All variables are currently created on the stack and synced later (seems to be faster)
107107
// NOTE: Strings are NOT copied, only the pointer and string size are copied
108108
varPtr.stackPtr = m_builder.CreateAlloca(m_valueDataType);
109-
varPtr.onStack = false; // use heap before the first assignment
109+
110+
// If there are no write operations outside loops, initialize the stack variable now
111+
Variable *variable = var;
112+
auto it = std::find_if(m_variableInstructions.begin(), m_variableInstructions.end(), [variable](const std::shared_ptr<LLVMInstruction> &ins) {
113+
return ins->type == LLVMInstruction::Type::WriteVariable && ins->workVariable == variable && !ins->loopScope;
114+
});
115+
116+
if (it == m_variableInstructions.end()) {
117+
createValueCopy(ptr, varPtr.stackPtr);
118+
varPtr.onStack = true;
119+
} else
120+
varPtr.onStack = false; // use heap before the first assignment
110121
}
111122

112123
// Create list pointers

0 commit comments

Comments
 (0)