Skip to content

Commit 05cd43f

Browse files
committed
LLVMCodeBuilder: Fix list type change before last write in loop
1 parent d85fb86 commit 05cd43f

File tree

2 files changed

+24
-8
lines changed

2 files changed

+24
-8
lines changed

src/engine/internal/llvm/llvmcodebuilder.cpp

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2356,7 +2356,7 @@ bool LLVMCodeBuilder::isVarOrListTypeSafe(std::shared_ptr<LLVMInstruction> ins,
23562356
// Find previous write instruction in this, parent or child loop scope
23572357
size_t index = it - instructions.begin();
23582358

2359-
if (index > 0) {
2359+
if (varPtr && index > 0) { // this is only needed for variables
23602360
bool found = false;
23612361

23622362
do {
@@ -2401,7 +2401,7 @@ bool LLVMCodeBuilder::isVarOrListTypeSafe(std::shared_ptr<LLVMInstruction> ins,
24012401
checkScope = checkScope->parentScope;
24022402
}
24032403

2404-
// Find n-th last write operation based on counter (may be in a parent or child scope)
2404+
// Get all write operations in all loop scopes (from the root loop scope)
24052405
std::vector<std::shared_ptr<LLVMInstruction>> lastWrites;
24062406

24072407
while (checkScope) {
@@ -2425,15 +2425,26 @@ bool LLVMCodeBuilder::isVarOrListTypeSafe(std::shared_ptr<LLVMInstruction> ins,
24252425
if (c >= lastWrites.size())
24262426
return true;
24272427

2428-
write = lastWrites[lastWrites.size() - c - 1]; // Ignore last c writes
2428+
if (varPtr)
2429+
write = lastWrites[lastWrites.size() - c - 1]; // Ignore last c writes
2430+
else {
2431+
// If this is a list instruction, check last write operations except current
2432+
for (long i = lastWrites.size() - c - 1; i >= 0; i--) { // Ignore last c writes
2433+
if (lastWrites[i] == ins)
2434+
continue;
2435+
2436+
if (!isVarOrListWriteResultTypeSafe(lastWrites[i], expectedType, false, log, c))
2437+
return false;
2438+
}
2439+
}
24292440

24302441
bool safe = true;
24312442

24322443
if (VAR_LIST_READ_INSTRUCTIONS.find(ins->type) == VAR_LIST_READ_INSTRUCTIONS.cend()) // write
24332444
safe = isVarOrListWriteResultTypeSafe(ins, expectedType, false, log, c);
24342445

24352446
if (safe)
2436-
return isVarOrListWriteResultTypeSafe(write, expectedType, false, log, c);
2447+
return write ? isVarOrListWriteResultTypeSafe(write, expectedType, false, log, c) : true;
24372448
else
24382449
return false;
24392450
}

test/llvm/llvmcodebuilder_test.cpp

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5149,15 +5149,19 @@ TEST_F(LLVMCodeBuilderTest, ListLoopTypeAnalysis6)
51495149

51505150
createBuilder(&sprite, true);
51515151

5152+
m_builder->createListClear(globalList.get());
51525153
m_builder->createListClear(localList.get());
51535154

5154-
CompilerValue *v = m_builder->addConstValue(5.25);
5155+
CompilerValue *v = m_builder->addConstValue(-156.07);
5156+
m_builder->createListAppend(globalList.get(), v);
5157+
5158+
v = m_builder->addConstValue(5.25);
51555159
m_builder->createListAppend(localList.get(), v);
51565160

51575161
v = m_builder->addConstValue(2);
51585162
m_builder->beginRepeatLoop(v);
51595163
{
5160-
// Type is unknown here because a list item with unknown type is added later (even though the list item has the original type assigned, it cannot be determined at compile time)
5164+
// Type is unknown here because a list item with unknown type is added later
51615165
v = m_builder->createSub(m_builder->addListSize(localList.get()), m_builder->addConstValue(1));
51625166
v = m_builder->addListItem(localList.get(), v);
51635167
m_builder->addFunctionCall("test_print_number", Compiler::StaticType::Void, { Compiler::StaticType::Number }, { v });
@@ -5174,7 +5178,8 @@ TEST_F(LLVMCodeBuilderTest, ListLoopTypeAnalysis6)
51745178
v = m_builder->addConstValue(10);
51755179
m_builder->createListReplace(globalList.get(), m_builder->addConstValue(0), v);
51765180

5177-
v = m_builder->addListItem(globalList.get(), m_builder->addConstValue(0));
5181+
v = m_builder->createSub(m_builder->addListSize(globalList.get()), m_builder->addConstValue(1));
5182+
v = m_builder->addListItem(globalList.get(), v);
51785183
m_builder->createListAppend(localList.get(), v);
51795184

51805185
v = m_builder->addConstValue("test");
@@ -5188,7 +5193,7 @@ TEST_F(LLVMCodeBuilderTest, ListLoopTypeAnalysis6)
51885193
"5.25\n"
51895194
"0\n"
51905195
"1\n"
5191-
"10\n"
5196+
"0\n"
51925197
"0\n"
51935198
"1\n";
51945199

0 commit comments

Comments
 (0)