Skip to content

Commit 2bac780

Browse files
committed
Use string stack in the LLVM backend
1 parent 78ac4d7 commit 2bac780

12 files changed

Lines changed: 98 additions & 100 deletions

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

Lines changed: 42 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -79,14 +79,25 @@ 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 array variable
86+
m_stringArrayVar = m_builder.CreateAlloca(m_stringPtrType->getPointerTo(), nullptr, "stringArray");
87+
88+
// Create string count block (string count is known after all instructions are processed)
89+
m_stringCountBlock = llvm::BasicBlock::Create(m_llvmCtx, "getStringCount", m_function);
90+
m_builder.CreateBr(m_stringCountBlock);
91+
// NOTE: This block is terminated later
92+
8293
// Create string allocation block
94+
m_builder.SetInsertPoint(m_stringCountBlock);
8395
m_stringAllocaBlock = llvm::BasicBlock::Create(m_llvmCtx, "stringAlloca", m_function);
84-
m_builder.CreateBr(m_stringAllocaBlock);
96+
// NOTE: This block is terminated later
8597

86-
// Get string array
98+
// Push string stack (load string array)
8799
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
100+
pushStringStack();
90101

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

147158
void LLVMBuildUtils::end(LLVMInstruction *lastInstruction, LLVMRegister *lastConstant)
148159
{
149-
// Terminate string allocation block
150-
if (m_stringCount == 0)
151-
m_stringAllocaBlock->erase(m_stringAllocaBlock->begin(), m_stringAllocaBlock->end());
152-
160+
// Terminate string count and allocation blocks
153161
llvm::BasicBlock *previousBlock = m_builder.GetInsertBlock();
162+
m_builder.SetInsertPoint(m_stringCountBlock);
163+
m_builder.CreateStore(m_builder.getInt64(m_stringCount), m_stringCountVar);
164+
165+
m_builder.CreateBr(m_stringAllocaBlock);
166+
154167
m_builder.SetInsertPoint(m_stringAllocaBlock);
155168
m_builder.CreateBr(m_stringAllocaNextBlock);
156169
m_builder.SetInsertPoint(previousBlock);
@@ -165,6 +178,7 @@ void LLVMBuildUtils::end(LLVMInstruction *lastInstruction, LLVMRegister *lastCon
165178

166179
// End branch
167180
m_builder.SetInsertPoint(m_endBranch);
181+
popStringStack();
168182

169183
llvm::PointerType *pointerType = llvm::PointerType::get(llvm::Type::getInt8Ty(m_llvmCtx), 0);
170184

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

199213
// Thread end branch (stop the entire thread, including procedure callers)
200214
m_builder.SetInsertPoint(m_endThreadBranch);
215+
popStringStack();
201216

202217
switch (m_codeType) {
203218
case Compiler::CodeType::Script:
@@ -497,15 +512,11 @@ llvm::Value *LLVMBuildUtils::addAlloca(llvm::Type *type)
497512

498513
llvm::Value *LLVMBuildUtils::addStringAlloca()
499514
{
500-
// All temporary strings are allocated before the script is called
501-
llvm::BasicBlock *block = m_builder.GetInsertBlock();
502-
m_builder.SetInsertPoint(m_stringAllocaBlock);
503-
515+
// All temporary strings are allocated when the string stack is pushed
516+
llvm::Value *stringArray = m_builder.CreateLoad(m_stringPtrType->getPointerTo(), m_stringArrayVar, "loadedStringArray");
504517
llvm::Value *index = m_builder.getInt64(m_stringCount++);
505-
llvm::Value *elementPtr = m_builder.CreateGEP(m_stringPtrType->getPointerTo(), m_stringArray, index);
518+
llvm::Value *elementPtr = m_builder.CreateGEP(m_stringPtrType->getPointerTo(), stringArray, index);
506519
llvm::Value *ret = m_builder.CreateLoad(m_stringPtrType->getPointerTo(), elementPtr, "localString");
507-
508-
m_builder.SetInsertPoint(block);
509520
return ret;
510521
}
511522

@@ -1417,10 +1428,14 @@ void LLVMBuildUtils::createSuspend()
14171428
m_builder.SetInsertPoint(suspendBranch);
14181429
}
14191430

1431+
popStringStack();
14201432
syncVariables();
1433+
14211434
m_coroutine->createSuspend();
1435+
14221436
reloadVariables();
14231437
reloadLists();
1438+
pushStringStack();
14241439

14251440
if (m_warpArg) {
14261441
m_builder.CreateBr(nextBranch);
@@ -2190,3 +2205,15 @@ llvm::Value *LLVMBuildUtils::getListDataPtr(const LLVMListPtr &listPtr)
21902205
{
21912206
return m_builder.CreateLoad(m_valueDataType->getPointerTo(), m_builder.CreateLoad(m_valueDataType->getPointerTo()->getPointerTo(), listPtr.dataPtr));
21922207
}
2208+
2209+
void LLVMBuildUtils::pushStringStack()
2210+
{
2211+
llvm::Value *stringCount = m_builder.CreateLoad(m_builder.getInt64Ty(), m_stringCountVar, "strCount");
2212+
llvm::Value *strings = m_builder.CreateCall(m_functions.resolve_llvm_push_string_stack(), { m_executionContextPtr, stringCount });
2213+
m_builder.CreateStore(strings, m_stringArrayVar);
2214+
}
2215+
2216+
void LLVMBuildUtils::popStringStack()
2217+
{
2218+
m_builder.CreateCall(m_functions.resolve_llvm_pop_string_stack(), { m_executionContextPtr });
2219+
}

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,9 @@ class LIBSCRATCHCPP_TEST_EXPORT LLVMBuildUtils
154154
llvm::Value *getListPtr(llvm::Value *targetLists, List *list);
155155
llvm::Value *getListDataPtr(const LLVMListPtr &listPtr);
156156

157+
void pushStringStack();
158+
void popStringStack();
159+
157160
LLVMCompilerContext *m_ctx = nullptr;
158161
llvm::LLVMContext &m_llvmCtx;
159162
llvm::IRBuilder<> &m_builder;
@@ -194,9 +197,11 @@ class LIBSCRATCHCPP_TEST_EXPORT LLVMBuildUtils
194197
std::vector<LLVMIfStatement> m_ifStatements;
195198
std::vector<LLVMLoop> m_loops;
196199

200+
llvm::BasicBlock *m_stringCountBlock = nullptr;
197201
llvm::BasicBlock *m_stringAllocaBlock = nullptr;
198202
llvm::BasicBlock *m_stringAllocaNextBlock = nullptr;
199-
llvm::Value *m_stringArray = nullptr;
203+
llvm::Value *m_stringCountVar = nullptr;
204+
llvm::Value *m_stringArrayVar = nullptr;
200205
size_t m_stringCount = 0;
201206
};
202207

‎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

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

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,14 @@ extern "C"
3434
return ctx->rng()->randint(from, to);
3535
}
3636

37-
LIBSCRATCHCPP_EXPORT StringPtr **llvm_get_string_array(ExecutionContext *ctx, function_id_t functionId)
37+
LIBSCRATCHCPP_EXPORT StringPtr **llvm_push_string_stack(ExecutionContext *ctx, size_t stringCount)
3838
{
39-
return static_cast<LLVMExecutionContext *>(ctx)->getStringArray(functionId);
39+
return ctx->pushStringStack(stringCount);
40+
}
41+
42+
LIBSCRATCHCPP_EXPORT void llvm_pop_string_stack(ExecutionContext *ctx)
43+
{
44+
return ctx->popStringStack();
4045
}
4146

4247
LIBSCRATCHCPP_EXPORT void llvm_mark_thread_as_finished(ExecutionContext *ctx)
@@ -290,12 +295,22 @@ llvm::FunctionCallee LLVMFunctions::resolve_llvm_random_bool()
290295
return resolveFunction("llvm_random_bool", llvm::FunctionType::get(m_builder->getDoubleTy(), { pointerType, m_builder->getInt1Ty(), m_builder->getInt1Ty() }, false));
291296
}
292297

293-
llvm::FunctionCallee LLVMFunctions::resolve_llvm_get_string_array()
298+
llvm::FunctionCallee LLVMFunctions::resolve_llvm_push_string_stack()
294299
{
295300
llvm::Type *stringPtr = m_stringPtrType->getPointerTo();
296301
llvm::Type *pointerType = llvm::PointerType::get(llvm::Type::getInt8Ty(*m_ctx->llvmCtx()), 0);
297302

298-
llvm::FunctionCallee callee = resolveFunction("llvm_get_string_array", llvm::FunctionType::get(stringPtr->getPointerTo(), { pointerType, m_functionIdType }, false));
303+
llvm::FunctionCallee callee = resolveFunction("llvm_push_string_stack", llvm::FunctionType::get(stringPtr->getPointerTo(), { pointerType, m_builder->getInt64Ty() }, false));
304+
llvm::Function *func = llvm::cast<llvm::Function>(callee.getCallee());
305+
func->addFnAttr(llvm::Attribute::ReadOnly);
306+
return callee;
307+
}
308+
309+
llvm::FunctionCallee LLVMFunctions::resolve_llvm_pop_string_stack()
310+
{
311+
llvm::Type *pointerType = llvm::PointerType::get(llvm::Type::getInt8Ty(*m_ctx->llvmCtx()), 0);
312+
313+
llvm::FunctionCallee callee = resolveFunction("llvm_pop_string_stack", llvm::FunctionType::get(m_builder->getVoidTy(), { pointerType }, false));
299314
llvm::Function *func = llvm::cast<llvm::Function>(callee.getCallee());
300315
func->addFnAttr(llvm::Attribute::ReadOnly);
301316
return callee;

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,8 @@ class LLVMFunctions
4747
llvm::FunctionCallee resolve_llvm_random_double();
4848
llvm::FunctionCallee resolve_llvm_random_int64();
4949
llvm::FunctionCallee resolve_llvm_random_bool();
50-
llvm::FunctionCallee resolve_llvm_get_string_array();
50+
llvm::FunctionCallee resolve_llvm_push_string_stack();
51+
llvm::FunctionCallee resolve_llvm_pop_string_stack();
5152
llvm::FunctionCallee resolve_llvm_mark_thread_as_finished();
5253
llvm::FunctionCallee resolve_llvm_is_thread_finished();
5354
llvm::FunctionCallee resolve_llvm_print();

0 commit comments

Comments
 (0)