Skip to content

Commit aefba9f

Browse files
committed
LLVMCodeBuilder: Fix loop scope memory leaks
1 parent 481be27 commit aefba9f

File tree

6 files changed

+15
-15
lines changed

6 files changed

+15
-15
lines changed

src/engine/internal/llvm/llvmcodebuilder.cpp

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1590,7 +1590,7 @@ void LLVMCodeBuilder::createVariableWrite(Variable *variable, CompilerValue *val
15901590

15911591
if (m_loopScope >= 0) {
15921592
auto scope = m_loopScopes[m_loopScope];
1593-
m_variablePtrs[variable].loopVariableWrites[scope].push_back(m_instructions.back());
1593+
m_variablePtrs[variable].loopVariableWrites[scope.get()].push_back(m_instructions.back());
15941594
}
15951595

15961596
m_variableInstructions.push_back(m_instructions.back());
@@ -1627,7 +1627,7 @@ void LLVMCodeBuilder::createListAppend(List *list, CompilerValue *item)
16271627

16281628
if (m_loopScope >= 0) {
16291629
auto scope = m_loopScopes[m_loopScope];
1630-
m_listPtrs[list].loopListWrites[scope].push_back(m_instructions.back());
1630+
m_listPtrs[list].loopListWrites[scope.get()].push_back(m_instructions.back());
16311631
}
16321632

16331633
m_listInstructions.push_back(m_instructions.back());
@@ -1644,7 +1644,7 @@ void LLVMCodeBuilder::createListInsert(List *list, CompilerValue *index, Compile
16441644

16451645
if (m_loopScope >= 0) {
16461646
auto scope = m_loopScopes[m_loopScope];
1647-
m_listPtrs[list].loopListWrites[scope].push_back(m_instructions.back());
1647+
m_listPtrs[list].loopListWrites[scope.get()].push_back(m_instructions.back());
16481648
}
16491649

16501650
m_listInstructions.push_back(m_instructions.back());
@@ -1661,7 +1661,7 @@ void LLVMCodeBuilder::createListReplace(List *list, CompilerValue *index, Compil
16611661

16621662
if (m_loopScope >= 0) {
16631663
auto scope = m_loopScopes[m_loopScope];
1664-
m_listPtrs[list].loopListWrites[scope].push_back(m_instructions.back());
1664+
m_listPtrs[list].loopListWrites[scope.get()].push_back(m_instructions.back());
16651665
}
16661666

16671667
m_listInstructions.push_back(m_instructions.back());
@@ -1878,8 +1878,8 @@ void LLVMCodeBuilder::pushLoopScope(bool buildPhase)
18781878

18791879
if (m_loopScope >= 0) {
18801880
auto currentScope = m_loopScopes[m_loopScope];
1881-
currentScope->childScopes.push_back(scope);
1882-
scope->parentScope = currentScope;
1881+
currentScope->childScopes.push_back(scope.get());
1882+
scope->parentScope = currentScope.get();
18831883
}
18841884

18851885
m_loopScope = m_loopScopes.size() - 1;
@@ -2511,9 +2511,9 @@ LLVMRegister *LLVMCodeBuilder::createOp(const LLVMInstruction &ins, Compiler::St
25112511
return nullptr;
25122512
}
25132513

2514-
std::shared_ptr<LLVMLoopScope> LLVMCodeBuilder::currentLoopScope() const
2514+
LLVMLoopScope *LLVMCodeBuilder::currentLoopScope() const
25152515
{
2516-
return m_loopScope >= 0 ? m_loopScopes[m_loopScope] : nullptr;
2516+
return m_loopScope >= 0 ? m_loopScopes[m_loopScope].get() : nullptr;
25172517
}
25182518

25192519
void LLVMCodeBuilder::createValueStore(LLVMRegister *reg, llvm::Value *targetPtr, Compiler::StaticType sourceType, Compiler::StaticType targetType)

src/engine/internal/llvm/llvmcodebuilder.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ class LLVMCodeBuilder : public ICodeBuilder
157157
LLVMRegister *createOp(LLVMInstruction::Type type, Compiler::StaticType retType, const Compiler::ArgTypes &argTypes = {}, const Compiler::Args &args = {});
158158
LLVMRegister *createOp(const LLVMInstruction &ins, Compiler::StaticType retType, Compiler::StaticType argType, const Compiler::Args &args);
159159
LLVMRegister *createOp(const LLVMInstruction &ins, Compiler::StaticType retType, const Compiler::ArgTypes &argTypes = {}, const Compiler::Args &args = {});
160-
std::shared_ptr<LLVMLoopScope> currentLoopScope() const;
160+
LLVMLoopScope *currentLoopScope() const;
161161

162162
void createValueStore(LLVMRegister *reg, llvm::Value *targetPtr, Compiler::StaticType sourceType, Compiler::StaticType targetType);
163163
void createReusedValueStore(LLVMRegister *reg, llvm::Value *targetPtr, Compiler::StaticType sourceType, Compiler::StaticType targetType);

src/engine/internal/llvm/llvminstruction.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ struct LLVMInstruction
7878
ProcedureArg
7979
};
8080

81-
LLVMInstruction(Type type, std::shared_ptr<LLVMLoopScope> loopScope, bool loopCondition) :
81+
LLVMInstruction(Type type, LLVMLoopScope *loopScope, bool loopCondition) :
8282
type(type),
8383
loopScope(loopScope),
8484
loopCondition(loopCondition)
@@ -95,7 +95,7 @@ struct LLVMInstruction
9595
List *workList = nullptr; // for lists
9696
BlockPrototype *procedurePrototype = nullptr;
9797
size_t procedureArgIndex = 0;
98-
std::shared_ptr<LLVMLoopScope> loopScope;
98+
LLVMLoopScope *loopScope = nullptr;
9999
bool loopCondition = false; // whether the instruction is part of a loop condition
100100
};
101101

src/engine/internal/llvm/llvmlistptr.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ struct LLVMListPtr
2828
Compiler::StaticType type = Compiler::StaticType::Unknown;
2929

3030
// Used in build phase to check the type safety of lists in loops
31-
std::unordered_map<std::shared_ptr<LLVMLoopScope>, std::vector<std::shared_ptr<LLVMInstruction>>> loopListWrites; // loop scope, write instructions
31+
std::unordered_map<LLVMLoopScope *, std::vector<std::shared_ptr<LLVMInstruction>>> loopListWrites; // loop scope, write instructions
3232
};
3333

3434
} // namespace libscratchcpp

src/engine/internal/llvm/llvmloopscope.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ namespace libscratchcpp
1111
struct LLVMLoopScope
1212
{
1313
bool containsYield = false;
14-
std::shared_ptr<LLVMLoopScope> parentScope;
15-
std::vector<std::shared_ptr<LLVMLoopScope>> childScopes;
14+
LLVMLoopScope *parentScope = nullptr;
15+
std::vector<LLVMLoopScope *> childScopes;
1616
};
1717

1818
} // namespace libscratchcpp

src/engine/internal/llvm/llvmvariableptr.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ struct LLVMVariablePtr
2727
bool changed = false;
2828

2929
// Used in build phase to check the type safety of variables in loops
30-
std::unordered_map<std::shared_ptr<LLVMLoopScope>, std::vector<std::shared_ptr<LLVMInstruction>>> loopVariableWrites; // loop scope, write instructions
30+
std::unordered_map<LLVMLoopScope *, std::vector<std::shared_ptr<LLVMInstruction>>> loopVariableWrites; // loop scope, write instructions
3131
};
3232

3333
} // namespace libscratchcpp

0 commit comments

Comments
 (0)