Skip to content

Commit af52d7b

Browse files
committed
LLVMCodeBuilder: Use raw pointers for variable and list instructions
1 parent 50e21bd commit af52d7b

File tree

2 files changed

+33
-34
lines changed

2 files changed

+33
-34
lines changed

src/engine/internal/llvm/llvmcodebuilder.cpp

Lines changed: 28 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ std::shared_ptr<ExecutableCode> LLVMCodeBuilder::finalize()
121121

122122
// If there are no write operations outside loops, initialize the stack variable now
123123
Variable *variable = var;
124-
auto it = std::find_if(m_variableInstructions.begin(), m_variableInstructions.end(), [variable](const std::shared_ptr<LLVMInstruction> &ins) {
124+
auto it = std::find_if(m_variableInstructions.begin(), m_variableInstructions.end(), [variable](const LLVMInstruction *ins) {
125125
return ins->type == LLVMInstruction::Type::WriteVariable && ins->workVariable == variable && !ins->loopScope;
126126
});
127127

@@ -743,7 +743,7 @@ std::shared_ptr<ExecutableCode> LLVMCodeBuilder::finalize()
743743
LLVMVariablePtr &varPtr = m_variablePtrs[step.workVariable];
744744
varPtr.changed = true;
745745

746-
const bool safe = isVarOrListTypeSafe(insPtr, varPtr.type);
746+
const bool safe = isVarOrListTypeSafe(insPtr.get(), varPtr.type);
747747

748748
// Initialize stack variable on first assignment
749749
if (!varPtr.onStack) {
@@ -778,7 +778,7 @@ std::shared_ptr<ExecutableCode> LLVMCodeBuilder::finalize()
778778
assert(step.args.size() == 0);
779779
LLVMVariablePtr &varPtr = m_variablePtrs[step.workVariable];
780780

781-
if (!isVarOrListTypeSafe(insPtr, varPtr.type))
781+
if (!isVarOrListTypeSafe(insPtr.get(), varPtr.type))
782782
varPtr.type = Compiler::StaticType::Unknown;
783783

784784
step.functionReturnReg->value = varPtr.onStack && !(step.loopCondition && !m_warp) ? varPtr.stackPtr : varPtr.heapPtr;
@@ -806,7 +806,7 @@ std::shared_ptr<ExecutableCode> LLVMCodeBuilder::finalize()
806806
const auto &arg = step.args[0];
807807
LLVMListPtr &listPtr = m_listPtrs[step.workList];
808808

809-
if (!isVarOrListTypeSafe(insPtr, listPtr.type))
809+
if (!isVarOrListTypeSafe(insPtr.get(), listPtr.type))
810810
listPtr.type = Compiler::StaticType::Unknown;
811811

812812
// Range check
@@ -846,7 +846,7 @@ std::shared_ptr<ExecutableCode> LLVMCodeBuilder::finalize()
846846
typeMap[&listPtr] = listPtr.type;
847847
}
848848

849-
if (!isVarOrListTypeSafe(insPtr, listPtr.type))
849+
if (!isVarOrListTypeSafe(insPtr.get(), listPtr.type))
850850
listPtr.type = Compiler::StaticType::Unknown;
851851

852852
// Check if enough space is allocated
@@ -894,7 +894,7 @@ std::shared_ptr<ExecutableCode> LLVMCodeBuilder::finalize()
894894
typeMap[&listPtr] = listPtr.type;
895895
}
896896

897-
if (!isVarOrListTypeSafe(insPtr, listPtr.type))
897+
if (!isVarOrListTypeSafe(insPtr.get(), listPtr.type))
898898
listPtr.type = Compiler::StaticType::Unknown;
899899

900900
llvm::Value *oldAllocatedSize = m_builder.CreateLoad(m_builder.getInt64Ty(), listPtr.allocatedSizePtr);
@@ -933,7 +933,7 @@ std::shared_ptr<ExecutableCode> LLVMCodeBuilder::finalize()
933933
Compiler::StaticType type = optimizeRegisterType(valueArg.second);
934934
LLVMListPtr &listPtr = m_listPtrs[step.workList];
935935

936-
if (!isVarOrListTypeSafe(insPtr, listPtr.type))
936+
if (!isVarOrListTypeSafe(insPtr.get(), listPtr.type))
937937
listPtr.type = Compiler::StaticType::Unknown;
938938

939939
// Range check
@@ -981,7 +981,7 @@ std::shared_ptr<ExecutableCode> LLVMCodeBuilder::finalize()
981981
const auto &arg = step.args[0];
982982
LLVMListPtr &listPtr = m_listPtrs[step.workList];
983983

984-
if (!isVarOrListTypeSafe(insPtr, listPtr.type))
984+
if (!isVarOrListTypeSafe(insPtr.get(), listPtr.type))
985985
listPtr.type = Compiler::StaticType::Unknown;
986986

987987
llvm::Value *min = llvm::ConstantFP::get(m_llvmCtx, llvm::APFloat(0.0));
@@ -1012,7 +1012,7 @@ std::shared_ptr<ExecutableCode> LLVMCodeBuilder::finalize()
10121012
const auto &arg = step.args[0];
10131013
LLVMListPtr &listPtr = m_listPtrs[step.workList];
10141014

1015-
if (!isVarOrListTypeSafe(insPtr, listPtr.type))
1015+
if (!isVarOrListTypeSafe(insPtr.get(), listPtr.type))
10161016
listPtr.type = Compiler::StaticType::Unknown;
10171017

10181018
step.functionReturnReg->value = m_builder.CreateSIToFP(getListItemIndex(listPtr, arg.second), m_builder.getDoubleTy());
@@ -1024,7 +1024,7 @@ std::shared_ptr<ExecutableCode> LLVMCodeBuilder::finalize()
10241024
const auto &arg = step.args[0];
10251025
LLVMListPtr &listPtr = m_listPtrs[step.workList];
10261026

1027-
if (!isVarOrListTypeSafe(insPtr, listPtr.type))
1027+
if (!isVarOrListTypeSafe(insPtr.get(), listPtr.type))
10281028
listPtr.type = Compiler::StaticType::Unknown;
10291029

10301030
llvm::Value *index = getListItemIndex(listPtr, arg.second);
@@ -1460,7 +1460,7 @@ CompilerValue *LLVMCodeBuilder::addVariableValue(Variable *variable)
14601460

14611461
m_instructions.addInstruction(ins);
14621462
m_instructionList.push_back(ins);
1463-
m_variableInstructions.push_back(m_instructionList.back());
1463+
m_variableInstructions.push_back(ins.get());
14641464
return addReg(ret, ins);
14651465
}
14661466

@@ -1491,7 +1491,7 @@ CompilerValue *LLVMCodeBuilder::addListItem(List *list, CompilerValue *index)
14911491

14921492
m_instructions.addInstruction(ins);
14931493
m_instructionList.push_back(ins);
1494-
m_listInstructions.push_back(m_instructionList.back());
1494+
m_listInstructions.push_back(ins.get());
14951495
return addReg(ret, ins);
14961496
}
14971497

@@ -1504,7 +1504,7 @@ CompilerValue *LLVMCodeBuilder::addListItemIndex(List *list, CompilerValue *item
15041504
m_listPtrs[list] = LLVMListPtr();
15051505

15061506
auto ret = createOp(ins, Compiler::StaticType::Number, Compiler::StaticType::Unknown, { item });
1507-
m_listInstructions.push_back(m_instructionList.back());
1507+
m_listInstructions.push_back(m_instructions.last());
15081508
return ret;
15091509
}
15101510

@@ -1517,7 +1517,7 @@ CompilerValue *LLVMCodeBuilder::addListContains(List *list, CompilerValue *item)
15171517
m_listPtrs[list] = LLVMListPtr();
15181518

15191519
auto ret = createOp(ins, Compiler::StaticType::Bool, Compiler::StaticType::Unknown, { item });
1520-
m_listInstructions.push_back(m_instructionList.back());
1520+
m_listInstructions.push_back(m_instructions.last());
15211521
return ret;
15221522
}
15231523

@@ -1745,7 +1745,7 @@ void LLVMCodeBuilder::createVariableWrite(Variable *variable, CompilerValue *val
17451745
m_variablePtrs[variable].loopVariableWrites[scope.get()].push_back(m_instructionList.back());
17461746
}
17471747

1748-
m_variableInstructions.push_back(m_instructionList.back());
1748+
m_variableInstructions.push_back(m_instructions.last());
17491749
}
17501750

17511751
void LLVMCodeBuilder::createListClear(List *list)
@@ -1782,7 +1782,7 @@ void LLVMCodeBuilder::createListAppend(List *list, CompilerValue *item)
17821782
m_listPtrs[list].loopListWrites[scope.get()].push_back(m_instructionList.back());
17831783
}
17841784

1785-
m_listInstructions.push_back(m_instructionList.back());
1785+
m_listInstructions.push_back(m_instructions.last());
17861786
}
17871787

17881788
void LLVMCodeBuilder::createListInsert(List *list, CompilerValue *index, CompilerValue *item)
@@ -1799,7 +1799,7 @@ void LLVMCodeBuilder::createListInsert(List *list, CompilerValue *index, Compile
17991799
m_listPtrs[list].loopListWrites[scope.get()].push_back(m_instructionList.back());
18001800
}
18011801

1802-
m_listInstructions.push_back(m_instructionList.back());
1802+
m_listInstructions.push_back(m_instructions.last());
18031803
}
18041804

18051805
void LLVMCodeBuilder::createListReplace(List *list, CompilerValue *index, CompilerValue *item)
@@ -1816,7 +1816,7 @@ void LLVMCodeBuilder::createListReplace(List *list, CompilerValue *index, Compil
18161816
m_listPtrs[list].loopListWrites[scope.get()].push_back(m_instructionList.back());
18171817
}
18181818

1819-
m_listInstructions.push_back(m_instructionList.back());
1819+
m_listInstructions.push_back(m_instructions.last());
18201820
}
18211821

18221822
void LLVMCodeBuilder::beginIfStatement(CompilerValue *cond)
@@ -2528,14 +2528,14 @@ void LLVMCodeBuilder::updateListDataPtr(const LLVMListPtr &listPtr)
25282528
m_builder.CreateStore(m_builder.getInt1(false), listPtr.dataPtrDirty);
25292529
}
25302530

2531-
bool LLVMCodeBuilder::isVarOrListTypeSafe(std::shared_ptr<LLVMInstruction> ins, Compiler::StaticType expectedType) const
2531+
bool LLVMCodeBuilder::isVarOrListTypeSafe(LLVMInstruction *ins, Compiler::StaticType expectedType) const
25322532
{
25332533
std::unordered_set<LLVMInstruction *> processed;
25342534
int counter = 0;
25352535
return isVarOrListTypeSafe(ins, expectedType, processed, counter);
25362536
}
25372537

2538-
bool LLVMCodeBuilder::isVarOrListTypeSafe(std::shared_ptr<LLVMInstruction> ins, Compiler::StaticType expectedType, std::unordered_set<LLVMInstruction *> &log, int &c) const
2538+
bool LLVMCodeBuilder::isVarOrListTypeSafe(LLVMInstruction *ins, Compiler::StaticType expectedType, std::unordered_set<LLVMInstruction *> &log, int &c) const
25392539
{
25402540
/*
25412541
* The main part of the loop type analyzer.
@@ -2571,12 +2571,12 @@ bool LLVMCodeBuilder::isVarOrListTypeSafe(std::shared_ptr<LLVMInstruction> ins,
25712571
*
25722572
* Increment counter to ignore last n write operations.
25732573
*/
2574-
if (log.find(ins.get()) != log.cend())
2574+
if (log.find(ins) != log.cend())
25752575
c++;
25762576
else
2577-
log.insert(ins.get());
2577+
log.insert(ins);
25782578

2579-
assert(std::find(m_instructionList.begin(), m_instructionList.end(), ins) != m_instructionList.end());
2579+
assert(m_instructions.containsInstruction(ins));
25802580
const LLVMVariablePtr *varPtr = ins->workVariable ? &m_variablePtrs.at(ins->workVariable) : nullptr;
25812581
const LLVMListPtr *listPtr = ins->workList ? &m_listPtrs.at(ins->workList) : nullptr;
25822582
assert((varPtr || listPtr) && !(varPtr && listPtr));
@@ -2590,7 +2590,7 @@ bool LLVMCodeBuilder::isVarOrListTypeSafe(std::shared_ptr<LLVMInstruction> ins,
25902590
if (scope->containsYield && !m_warp)
25912591
return false;
25922592

2593-
std::shared_ptr<LLVMInstruction> write;
2593+
LLVMInstruction *write = nullptr;
25942594
const auto &instructions = varPtr ? m_variableInstructions : m_listInstructions;
25952595

25962596
// Find this instruction
@@ -2646,7 +2646,7 @@ bool LLVMCodeBuilder::isVarOrListTypeSafe(std::shared_ptr<LLVMInstruction> ins,
26462646
}
26472647

26482648
// Get all write operations in all loop scopes (from the root loop scope)
2649-
std::vector<std::shared_ptr<LLVMInstruction>> lastWrites;
2649+
std::vector<LLVMInstruction *> lastWrites;
26502650

26512651
while (checkScope) {
26522652
auto it = loopWrites.find(checkScope);
@@ -2656,7 +2656,7 @@ bool LLVMCodeBuilder::isVarOrListTypeSafe(std::shared_ptr<LLVMInstruction> ins,
26562656
const auto &writes = it->second;
26572657

26582658
for (auto w : writes)
2659-
lastWrites.push_back(w);
2659+
lastWrites.push_back(w.get());
26602660
}
26612661

26622662
if (checkScope->childScopes.empty())
@@ -2693,8 +2693,7 @@ bool LLVMCodeBuilder::isVarOrListTypeSafe(std::shared_ptr<LLVMInstruction> ins,
26932693
return false;
26942694
}
26952695

2696-
bool LLVMCodeBuilder::isVarOrListWriteResultTypeSafe(std::shared_ptr<LLVMInstruction> ins, Compiler::StaticType expectedType, bool ignoreSavedType, std::unordered_set<LLVMInstruction *> &log, int &c)
2697-
const
2696+
bool LLVMCodeBuilder::isVarOrListWriteResultTypeSafe(LLVMInstruction *ins, Compiler::StaticType expectedType, bool ignoreSavedType, std::unordered_set<LLVMInstruction *> &log, int &c) const
26982697
{
26992698
const LLVMVariablePtr *varPtr = ins->workVariable ? &m_variablePtrs.at(ins->workVariable) : nullptr;
27002699
const LLVMListPtr *listPtr = ins->workList ? &m_listPtrs.at(ins->workList) : nullptr;
@@ -2705,7 +2704,7 @@ bool LLVMCodeBuilder::isVarOrListWriteResultTypeSafe(std::shared_ptr<LLVMInstruc
27052704
auto argIns = arg->instruction;
27062705

27072706
if (argIns && (argIns->type == LLVMInstruction::Type::ReadVariable || argIns->type == LLVMInstruction::Type::GetListItem))
2708-
return isVarOrListTypeSafe(argIns, expectedType, log, c);
2707+
return isVarOrListTypeSafe(argIns.get(), expectedType, log, c);
27092708

27102709
// Check written type
27112710
const bool typeMatches = (optimizeRegisterType(arg) == expectedType);

src/engine/internal/llvm/llvmcodebuilder.h

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -154,9 +154,9 @@ class LLVMCodeBuilder : public ICodeBuilder
154154
void reloadVariables(llvm::Value *targetVariables);
155155
void reloadLists();
156156
void updateListDataPtr(const LLVMListPtr &listPtr);
157-
bool isVarOrListTypeSafe(std::shared_ptr<LLVMInstruction> ins, Compiler::StaticType expectedType) const;
158-
bool isVarOrListTypeSafe(std::shared_ptr<LLVMInstruction> ins, Compiler::StaticType expectedType, std::unordered_set<LLVMInstruction *> &log, int &c) const;
159-
bool isVarOrListWriteResultTypeSafe(std::shared_ptr<LLVMInstruction> ins, Compiler::StaticType expectedType, bool ignoreSavedType, std::unordered_set<LLVMInstruction *> &log, int &c) const;
157+
bool isVarOrListTypeSafe(LLVMInstruction *ins, Compiler::StaticType expectedType) const;
158+
bool isVarOrListTypeSafe(LLVMInstruction *ins, Compiler::StaticType expectedType, std::unordered_set<LLVMInstruction *> &log, int &c) const;
159+
bool isVarOrListWriteResultTypeSafe(LLVMInstruction *ins, Compiler::StaticType expectedType, bool ignoreSavedType, std::unordered_set<LLVMInstruction *> &log, int &c) const;
160160

161161
LLVMRegister *createOp(LLVMInstruction::Type type, Compiler::StaticType retType, Compiler::StaticType argType, const Compiler::Args &args);
162162
LLVMRegister *createOp(LLVMInstruction::Type type, Compiler::StaticType retType, const Compiler::ArgTypes &argTypes = {}, const Compiler::Args &args = {});
@@ -251,8 +251,8 @@ class LLVMCodeBuilder : public ICodeBuilder
251251
long m_loopScopeCounter = 0; // replacement for m_loopScopes size in build phase
252252
std::vector<long> m_loopScopeTree;
253253
bool m_loopCondition = false; // whether we're currently compiling a loop condition
254-
std::vector<std::shared_ptr<LLVMInstruction>> m_variableInstructions;
255-
std::vector<std::shared_ptr<LLVMInstruction>> m_listInstructions;
254+
std::vector<LLVMInstruction *> m_variableInstructions;
255+
std::vector<LLVMInstruction *> m_listInstructions;
256256
std::vector<std::vector<llvm::Value *>> m_stringHeap; // scopes
257257

258258
std::shared_ptr<ExecutableCode> m_output;

0 commit comments

Comments
 (0)