Skip to content

Commit 1c67ddc

Browse files
committed
Store variables on the stack in warp scripts
1 parent 3bcf4d9 commit 1c67ddc

File tree

3 files changed

+20
-52
lines changed

3 files changed

+20
-52
lines changed

src/engine/internal/llvm/instructions/variables.cpp

Lines changed: 3 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -111,30 +111,8 @@ LLVMInstruction *Variables::buildWriteVariable(LLVMInstruction *ins)
111111
const auto &arg = ins->args[0];
112112
Compiler::StaticType argType = m_utils.optimizeRegisterType(arg.second);
113113
LLVMVariablePtr &varPtr = m_utils.variablePtr(ins->targetVariable);
114-
varPtr.changed = true; // TODO: Handle loops and if statements
115-
116-
// Initialize stack variable on first assignment
117-
// TODO: Use stack in the top level (outside loops and if statements)
118-
/*if (!varPtr.onStack) {
119-
varPtr.onStack = true;
120-
varPtr.type = type; // don't care about unknown type on first assignment
121-
122-
ValueType mappedType;
123-
124-
if (type == Compiler::StaticType::String || type == Compiler::StaticType::Unknown) {
125-
// Value functions are used for these types, so don't break them
126-
mappedType = ValueType::Number;
127-
} else {
128-
auto it = std::find_if(TYPE_MAP.begin(), TYPE_MAP.end(), [type](const std::pair<ValueType, Compiler::StaticType> &pair) { return pair.second == type; });
129-
assert(it != TYPE_MAP.cend());
130-
mappedType = it->first;
131-
}
132-
133-
llvm::Value *typeField = m_builder.CreateStructGEP(m_valueDataType, varPtr.stackPtr, 1);
134-
m_builder.CreateStore(m_builder.getInt32(static_cast<uint32_t>(mappedType)), typeField);
135-
}*/
136-
137-
m_utils.createValueStore(arg.second, varPtr.stackPtr, ins->targetType, argType);
114+
115+
m_utils.createValueStore(arg.second, varPtr.onStack ? varPtr.stackPtr : varPtr.heapPtr, ins->targetType, argType);
138116
return ins->next;
139117
}
140118

@@ -143,6 +121,6 @@ LLVMInstruction *Variables::buildReadVariable(LLVMInstruction *ins)
143121
assert(ins->args.size() == 0);
144122
LLVMVariablePtr &varPtr = m_utils.variablePtr(ins->targetVariable);
145123

146-
ins->functionReturnReg->value = varPtr.onStack && !(ins->loopCondition && !m_utils.warp()) ? varPtr.stackPtr : varPtr.heapPtr;
124+
ins->functionReturnReg->value = varPtr.onStack ? varPtr.stackPtr : varPtr.heapPtr;
147125
return ins->next;
148126
}

src/engine/internal/llvm/llvmbuildutils.cpp

Lines changed: 17 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -63,26 +63,12 @@ void LLVMBuildUtils::init(llvm::Function *function, BlockPrototype *procedurePro
6363
// Direct access
6464
varPtr.heapPtr = ptr;
6565

66-
// All variables are currently created on the stack and synced later (seems to be faster)
66+
// In warp scripts, all variables are created on the stack and synced later (seems to be faster)
6767
// NOTE: Strings are NOT copied, only the pointer is copied
68-
// TODO: Restore this feature
69-
// varPtr.stackPtr = m_builder.CreateAlloca(m_valueDataType);
70-
varPtr.stackPtr = varPtr.heapPtr;
71-
varPtr.onStack = false;
72-
73-
// If there are no write operations outside loops, initialize the stack variable now
74-
/*Variable *variable = var;
75-
// TODO: Loop scope was used here, replace it with some "inside loop" flag if needed
76-
auto it = std::find_if(m_variableInstructions.begin(), m_variableInstructions.end(), [variable](const LLVMInstruction *ins) {
77-
return ins->type == LLVMInstruction::Type::WriteVariable && ins->targetVariable == variable && !ins->loopScope;
78-
});
79-
80-
if (it == m_variableInstructions.end()) {
81-
createValueCopy(ptr, varPtr.stackPtr);
82-
varPtr.onStack = true;
83-
} else
84-
varPtr.onStack = false; // use heap before the first assignment
85-
*/
68+
varPtr.onStack = m_warp;
69+
70+
if (varPtr.onStack)
71+
varPtr.stackPtr = m_builder.CreateAlloca(m_valueDataType);
8672
}
8773

8874
// Create list pointers
@@ -104,6 +90,9 @@ void LLVMBuildUtils::init(llvm::Function *function, BlockPrototype *procedurePro
10490
}
10591
}
10692

93+
reloadVariables(m_targetVariables);
94+
reloadLists();
95+
10796
// Create end branch
10897
m_endBranch = llvm::BasicBlock::Create(m_llvmCtx, "end", m_function);
10998
}
@@ -313,19 +302,21 @@ void LLVMBuildUtils::syncVariables(llvm::Value *targetVariables)
313302
{
314303
// Copy stack variables to the actual variables
315304
for (auto &[var, varPtr] : m_variablePtrs) {
316-
if (varPtr.onStack && varPtr.changed)
305+
if (varPtr.onStack)
317306
createValueCopy(varPtr.stackPtr, getVariablePtr(targetVariables, var));
318-
319-
varPtr.changed = false;
320307
}
321308
}
322309

323310
void LLVMBuildUtils::reloadVariables(llvm::Value *targetVariables)
324311
{
325-
// Reset variables to use heap
326-
for (auto &[var, varPtr] : m_variablePtrs) {
327-
varPtr.onStack = false;
328-
varPtr.changed = false;
312+
// Load variables to stack
313+
if (m_warp) {
314+
for (auto &[var, varPtr] : m_variablePtrs) {
315+
if (varPtr.onStack) {
316+
llvm::Value *ptr = getVariablePtr(m_targetVariables, var);
317+
createValueCopy(ptr, varPtr.stackPtr);
318+
}
319+
}
329320
}
330321
}
331322

src/engine/internal/llvm/llvmvariableptr.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ struct LLVMVariablePtr
2121
llvm::Value *stackPtr = nullptr;
2222
llvm::Value *heapPtr = nullptr;
2323
bool onStack = false;
24-
bool changed = false;
2524
};
2625

2726
} // namespace libscratchcpp

0 commit comments

Comments
 (0)