Skip to content

Commit 5314e61

Browse files
authored
Merge pull request #705 from scratchcpp/refactor_stack_string_allocation
Refactor stack string allocation
2 parents 7ebdc2b + 58b922e commit 5314e61

18 files changed

Lines changed: 185 additions & 96 deletions

‎include/scratchcpp/executioncontext.h‎

Lines changed: 48 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,10 @@
22

33
#pragma once
44

5-
#include "global.h"
5+
#include <stack>
6+
#include <vector>
7+
8+
#include "string_pool.h"
69
#include "spimpl.h"
710

811
namespace libscratchcpp
@@ -13,6 +16,7 @@ class IEngine;
1316
class Promise;
1417
class IStackTimer;
1518
class IRandomGenerator;
19+
struct StringPtr;
1620
class ExecutionContextPrivate;
1721

1822
/*! \brief The ExecutionContext represents the execution context of a target (can be a clone) with variables, lists, etc. */
@@ -21,7 +25,7 @@ class LIBSCRATCHCPP_EXPORT ExecutionContext
2125
public:
2226
ExecutionContext(Thread *thread);
2327
ExecutionContext(const ExecutionContext &) = delete;
24-
virtual ~ExecutionContext() { }
28+
virtual ~ExecutionContext();
2529

2630
Thread *thread() const;
2731
IEngine *engine() const;
@@ -35,8 +39,50 @@ class LIBSCRATCHCPP_EXPORT ExecutionContext
3539
IRandomGenerator *rng() const;
3640
void setRng(IRandomGenerator *newRng);
3741

42+
inline StringPtr **pushStringStack(size_t stringCount)
43+
{
44+
m_stringStackCleared = false;
45+
m_stringStack.push({});
46+
47+
auto &strings = m_stringStack.top();
48+
49+
for (size_t i = 0; i < stringCount; i++) {
50+
StringPtr *str = string_pool_new();
51+
strings.push_back(str);
52+
}
53+
54+
return strings.data();
55+
}
56+
57+
inline void popStringStack()
58+
{
59+
if (m_stringStackCleared) // avoid UB when popping after killing a thread
60+
return;
61+
62+
assert(!m_stringStack.empty()); // check only in debug mode to maximize performance
63+
64+
const auto &strings = m_stringStack.top();
65+
66+
for (StringPtr *str : strings)
67+
string_pool_free(str);
68+
69+
m_stringStack.pop();
70+
}
71+
72+
inline void clearStringStack()
73+
{
74+
while (!m_stringStack.empty()) {
75+
popStringStack();
76+
}
77+
78+
m_stringStackCleared = true;
79+
}
80+
3881
private:
3982
spimpl::unique_impl_ptr<ExecutionContextPrivate> impl;
83+
84+
std::stack<std::vector<StringPtr *>> m_stringStack;
85+
bool m_stringStackCleared = false;
4086
};
4187

4288
} // namespace libscratchcpp

‎include/scratchcpp/thread.h‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ class LIBSCRATCHCPP_EXPORT Thread
2020
public:
2121
Thread(Target *target, IEngine *engine, Script *script);
2222
Thread(const Thread &) = delete;
23+
~Thread();
2324

2425
Target *target() const;
2526
IEngine *engine() const;

‎src/engine/executioncontext.cpp‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,13 @@ ExecutionContext::ExecutionContext(Thread *thread) :
1313
{
1414
}
1515

16+
/*! Destroys ExecutionContext. */
17+
ExecutionContext::~ExecutionContext()
18+
{
19+
// Deallocate strings
20+
clearStringStack();
21+
}
22+
1623
/*! Returns the thread of this context. */
1724
Thread *ExecutionContext::thread() const
1825
{

‎src/engine/internal/codegen/llvm/llvmbuildutils.cpp‎

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -79,14 +79,24 @@ void LLVMBuildUtils::init(llvm::Function *function, BlockPrototype *procedurePro
7979
reg->intValue = llvm::ConstantInt::get(m_builder.getInt64Ty(), reg->constValue().toDouble(), true);
8080
}
8181

82+
// Create string count variable (LLVM should optimize it into a constant later)
83+
m_stringCountVar = m_builder.CreateAlloca(m_builder.getInt64Ty(), nullptr, "stringCount");
84+
85+
// Create string count block (string count is known after all instructions are processed)
86+
m_stringCountBlock = llvm::BasicBlock::Create(m_llvmCtx, "getStringCount", m_function);
87+
m_builder.CreateBr(m_stringCountBlock);
88+
// NOTE: This block is terminated later
89+
8290
// Create string allocation block
91+
m_builder.SetInsertPoint(m_stringCountBlock);
8392
m_stringAllocaBlock = llvm::BasicBlock::Create(m_llvmCtx, "stringAlloca", m_function);
84-
m_builder.CreateBr(m_stringAllocaBlock);
93+
// NOTE: This block is terminated later
8594

86-
// Get string array
95+
// Push string stack (load string array)
8796
m_builder.SetInsertPoint(m_stringAllocaBlock);
88-
m_stringArray = m_builder.CreateCall(m_functions.resolve_llvm_get_string_array(), { m_executionContextPtr, m_functionIdValue });
89-
// NOTE: This block is terminated later
97+
98+
llvm::Value *stringCount = m_builder.CreateLoad(m_builder.getInt64Ty(), m_stringCountVar, "strCount");
99+
m_stringArray = m_builder.CreateCall(m_functions.resolve_llvm_push_string_stack(), { m_executionContextPtr, stringCount });
90100

91101
// Create next block
92102
m_stringAllocaNextBlock = llvm::BasicBlock::Create(m_llvmCtx, "entry.next", m_function);
@@ -146,11 +156,13 @@ void LLVMBuildUtils::init(llvm::Function *function, BlockPrototype *procedurePro
146156

147157
void LLVMBuildUtils::end(LLVMInstruction *lastInstruction, LLVMRegister *lastConstant)
148158
{
149-
// Terminate string allocation block
150-
if (m_stringCount == 0)
151-
m_stringAllocaBlock->erase(m_stringAllocaBlock->begin(), m_stringAllocaBlock->end());
152-
159+
// Terminate string count and allocation blocks
153160
llvm::BasicBlock *previousBlock = m_builder.GetInsertBlock();
161+
m_builder.SetInsertPoint(m_stringCountBlock);
162+
m_builder.CreateStore(m_builder.getInt64(m_stringCount), m_stringCountVar);
163+
164+
m_builder.CreateBr(m_stringAllocaBlock);
165+
154166
m_builder.SetInsertPoint(m_stringAllocaBlock);
155167
m_builder.CreateBr(m_stringAllocaNextBlock);
156168
m_builder.SetInsertPoint(previousBlock);
@@ -165,6 +177,7 @@ void LLVMBuildUtils::end(LLVMInstruction *lastInstruction, LLVMRegister *lastCon
165177

166178
// End branch
167179
m_builder.SetInsertPoint(m_endBranch);
180+
m_builder.CreateCall(m_functions.resolve_llvm_pop_string_stack(), { m_executionContextPtr });
168181

169182
llvm::PointerType *pointerType = llvm::PointerType::get(llvm::Type::getInt8Ty(m_llvmCtx), 0);
170183

@@ -198,6 +211,7 @@ void LLVMBuildUtils::end(LLVMInstruction *lastInstruction, LLVMRegister *lastCon
198211

199212
// Thread end branch (stop the entire thread, including procedure callers)
200213
m_builder.SetInsertPoint(m_endThreadBranch);
214+
m_builder.CreateCall(m_functions.resolve_llvm_pop_string_stack(), { m_executionContextPtr });
201215

202216
switch (m_codeType) {
203217
case Compiler::CodeType::Script:
@@ -497,7 +511,7 @@ llvm::Value *LLVMBuildUtils::addAlloca(llvm::Type *type)
497511

498512
llvm::Value *LLVMBuildUtils::addStringAlloca()
499513
{
500-
// All temporary strings are allocated before the script is called
514+
// All temporary strings are allocated when the string stack is pushed
501515
llvm::BasicBlock *block = m_builder.GetInsertBlock();
502516
m_builder.SetInsertPoint(m_stringAllocaBlock);
503517

@@ -1418,7 +1432,9 @@ void LLVMBuildUtils::createSuspend()
14181432
}
14191433

14201434
syncVariables();
1435+
14211436
m_coroutine->createSuspend();
1437+
14221438
reloadVariables();
14231439
reloadLists();
14241440

‎src/engine/internal/codegen/llvm/llvmbuildutils.h‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,8 +194,10 @@ class LIBSCRATCHCPP_TEST_EXPORT LLVMBuildUtils
194194
std::vector<LLVMIfStatement> m_ifStatements;
195195
std::vector<LLVMLoop> m_loops;
196196

197+
llvm::BasicBlock *m_stringCountBlock = nullptr;
197198
llvm::BasicBlock *m_stringAllocaBlock = nullptr;
198199
llvm::BasicBlock *m_stringAllocaNextBlock = nullptr;
200+
llvm::Value *m_stringCountVar = nullptr;
199201
llvm::Value *m_stringArray = nullptr;
200202
size_t m_stringCount = 0;
201203
};

‎src/engine/internal/codegen/llvm/llvmcodebuilder.cpp‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ std::shared_ptr<ExecutableCode> LLVMCodeBuilder::build()
9494
m_utils.end(m_instructions.empty() ? nullptr : m_instructions.last(), m_lastConstValue);
9595
verifyFunction(m_function);
9696

97-
return std::make_shared<LLVMExecutableCode>(m_ctx, m_utils.scriptFunctionId(), m_function->getName().str(), m_ctx->coroutineResumeFunction()->getName().str(), m_utils.stringCount(), m_codeType);
97+
return std::make_shared<LLVMExecutableCode>(m_ctx, m_utils.scriptFunctionId(), m_function->getName().str(), m_ctx->coroutineResumeFunction()->getName().str(), m_codeType);
9898
}
9999

100100
CompilerValue *LLVMCodeBuilder::addFunctionCall(const std::string &functionName, Compiler::StaticType returnType, const Compiler::ArgTypes &argTypes, const Compiler::Args &args)

‎src/engine/internal/codegen/llvm/llvmexecutablecode.cpp‎

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -18,18 +18,11 @@ using namespace libscratchcpp;
1818

1919
static const void *END_THREAD_SENTINEL = (void *)0x1;
2020

21-
LLVMExecutableCode::LLVMExecutableCode(
22-
LLVMCompilerContext *ctx,
23-
function_id_t functionId,
24-
const std::string &mainFunctionName,
25-
const std::string &resumeFunctionName,
26-
size_t stringCount,
27-
Compiler::CodeType codeType) :
21+
LLVMExecutableCode::LLVMExecutableCode(LLVMCompilerContext *ctx, function_id_t functionId, const std::string &mainFunctionName, const std::string &resumeFunctionName, Compiler::CodeType codeType) :
2822
m_ctx(ctx),
2923
m_functionId(functionId),
3024
m_mainFunctionName(mainFunctionName),
3125
m_resumeFunctionName(resumeFunctionName),
32-
m_stringCount(stringCount),
3326
m_codeType(codeType)
3427
{
3528
assert(m_ctx);
@@ -153,11 +146,6 @@ function_id_t LLVMExecutableCode::functionId() const
153146
return m_functionId;
154147
}
155148

156-
size_t LLVMExecutableCode::stringCount() const
157-
{
158-
return m_stringCount;
159-
}
160-
161149
LLVMExecutionContext *LLVMExecutableCode::getContext(ExecutionContext *context)
162150
{
163151
assert(dynamic_cast<LLVMExecutionContext *>(context));

‎src/engine/internal/codegen/llvm/llvmexecutablecode.h‎

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,7 @@ class LLVMExecutionContext;
1818
class LIBSCRATCHCPP_TEST_EXPORT LLVMExecutableCode : public ExecutableCode
1919
{
2020
public:
21-
LLVMExecutableCode(
22-
LLVMCompilerContext *ctx,
23-
function_id_t functionId,
24-
const std::string &mainFunctionName,
25-
const std::string &resumeFunctionName,
26-
size_t stringCount,
27-
Compiler::CodeType codeType);
21+
LLVMExecutableCode(LLVMCompilerContext *ctx, function_id_t functionId, const std::string &mainFunctionName, const std::string &resumeFunctionName, Compiler::CodeType codeType);
2822

2923
void run(ExecutionContext *context) override;
3024
ValueData runReporter(ExecutionContext *context) override;
@@ -37,7 +31,6 @@ class LIBSCRATCHCPP_TEST_EXPORT LLVMExecutableCode : public ExecutableCode
3731
std::shared_ptr<ExecutionContext> createExecutionContext(Thread *thread) const override;
3832

3933
function_id_t functionId() const;
40-
size_t stringCount() const;
4134

4235
private:
4336
using MainFunctionType = void *(*)(ExecutionContext *, Target *, ValueData **, List **);
@@ -52,7 +45,6 @@ class LIBSCRATCHCPP_TEST_EXPORT LLVMExecutableCode : public ExecutableCode
5245
std::string m_mainFunctionName;
5346
std::string m_predicateFunctionName;
5447
std::string m_resumeFunctionName;
55-
size_t m_stringCount = 0;
5648
Compiler::CodeType m_codeType;
5749

5850
mutable std::variant<MainFunctionType, ReporterFunctionType, PredicateFunctionType> m_mainFunction;

‎src/engine/internal/codegen/llvm/llvmexecutioncontext.cpp‎

Lines changed: 0 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -11,25 +11,6 @@ LLVMExecutionContext::LLVMExecutionContext(LLVMCompilerContext *compilerCtx, Thr
1111
ExecutionContext(thread),
1212
m_compilerCtx(compilerCtx)
1313
{
14-
// Allocate strings for all scripts in the target
15-
// TODO: Allocate strings for this script and procedures it calls
16-
const auto &codeMap = compilerCtx->codeMap();
17-
18-
for (const auto &[functionId, code] : codeMap) {
19-
size_t count = code->stringCount();
20-
21-
if (count > 0) {
22-
m_stringVectors[functionId] = {};
23-
24-
auto &strings = m_stringVectors[functionId];
25-
strings.reserve(count);
26-
27-
for (size_t i = 0; i < count; i++)
28-
strings.push_back(string_pool_new());
29-
30-
m_stringArrays[functionId] = strings.data();
31-
}
32-
}
3314
}
3415

3516
LLVMExecutionContext::~LLVMExecutionContext()
@@ -41,12 +22,6 @@ LLVMExecutionContext::~LLVMExecutionContext()
4122
} else
4223
assert(false);
4324
}
44-
45-
// Deallocate strings
46-
for (const auto &[functionId, strings] : m_stringVectors) {
47-
for (StringPtr *str : strings)
48-
string_pool_free(str);
49-
}
5025
}
5126

5227
void *LLVMExecutionContext::coroutineHandle() const

‎src/engine/internal/codegen/llvm/llvmexecutioncontext.h‎

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -24,19 +24,10 @@ class LIBSCRATCHCPP_TEST_EXPORT LLVMExecutionContext : public ExecutionContext
2424
bool finished() const;
2525
void setFinished(bool newFinished);
2626

27-
inline StringPtr **getStringArray(function_id_t functionId)
28-
{
29-
assert(m_stringArrays.find(functionId) != m_stringArrays.cend());
30-
return m_stringArrays[functionId];
31-
}
32-
3327
private:
3428
LLVMCompilerContext *m_compilerCtx = nullptr;
3529
void *m_coroutineHandle = nullptr;
3630
bool m_finished = false;
37-
38-
std::unordered_map<function_id_t, std::vector<StringPtr *>> m_stringVectors;
39-
std::unordered_map<function_id_t, StringPtr **> m_stringArrays;
4031
};
4132

4233
} // namespace libscratchcpp

0 commit comments

Comments
 (0)