Skip to content

Commit a90765b

Browse files
committed
LLVMCodeBuilder: Use unsigned integer in repeat loop
1 parent f62c2e0 commit a90765b

File tree

3 files changed

+15
-13
lines changed

3 files changed

+15
-13
lines changed

src/dev/engine/internal/llvm/llvmcodebuilder.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -796,7 +796,7 @@ std::shared_ptr<ExecutableCode> LLVMCodeBuilder::finalize()
796796
m_builder.SetInsertPoint(roundBranch);
797797
llvm::Function *roundFunc = llvm::Intrinsic::getDeclaration(m_module.get(), llvm::Intrinsic::round, { count->getType() });
798798
count = m_builder.CreateCall(roundFunc, { count });
799-
count = m_builder.CreateFPToSI(count, m_builder.getInt64Ty()); // cast to signed integer
799+
count = m_builder.CreateFPToUI(count, m_builder.getInt64Ty()); // cast to unsigned integer
800800
count = m_builder.CreateSelect(isInf, zero, count);
801801

802802
// Jump to condition branch
@@ -884,7 +884,7 @@ std::shared_ptr<ExecutableCode> LLVMCodeBuilder::finalize()
884884
if (loop.isRepeatLoop) {
885885
// Increment index
886886
llvm::Value *currentIndex = m_builder.CreateLoad(m_builder.getInt64Ty(), loop.index);
887-
llvm::Value *incremented = m_builder.CreateAdd(currentIndex, llvm::ConstantInt::get(m_builder.getInt64Ty(), 1, true));
887+
llvm::Value *incremented = m_builder.CreateAdd(currentIndex, llvm::ConstantInt::get(m_builder.getInt64Ty(), 1, false));
888888
m_builder.CreateStore(incremented, loop.index);
889889
}
890890

test/dev/llvm/llvmcodebuilder_test.cpp

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2907,7 +2907,8 @@ TEST_F(LLVMCodeBuilderTest, RepeatLoop)
29072907
v = m_builder->addConstValue(2);
29082908
v = callConstFuncForType(ValueType::Number, v);
29092909
m_builder->beginRepeatLoop(v);
2910-
m_builder->addTargetFunctionCall("test_function_no_args", Compiler::StaticType::Void, {}, {});
2910+
CompilerValue *index = m_builder->addLoopIndex();
2911+
m_builder->addTargetFunctionCall("test_print_number", Compiler::StaticType::Void, { Compiler::StaticType::Number }, { index });
29112912
m_builder->endLoop();
29122913

29132914
// Nested
@@ -2928,8 +2929,8 @@ TEST_F(LLVMCodeBuilderTest, RepeatLoop)
29282929
v = m_builder->addConstValue(3);
29292930
m_builder->beginRepeatLoop(v);
29302931
{
2931-
v = m_builder->addConstValue(3);
2932-
m_builder->addTargetFunctionCall("test_function_1_arg", Compiler::StaticType::Void, { Compiler::StaticType::String }, { v });
2932+
index = m_builder->addLoopIndex();
2933+
m_builder->addTargetFunctionCall("test_print_number", Compiler::StaticType::Void, { Compiler::StaticType::Number }, { index });
29332934
}
29342935
m_builder->endLoop();
29352936
}
@@ -2950,20 +2951,20 @@ TEST_F(LLVMCodeBuilderTest, RepeatLoop)
29502951
"1_arg 1\n"
29512952
"1_arg 1\n"
29522953
"1_arg 1\n"
2953-
"no_args\n"
2954-
"no_args\n"
2954+
"0\n"
2955+
"1\n"
29552956
"1_arg 1\n"
29562957
"1_arg 1\n"
29572958
"1_arg 2\n"
2958-
"1_arg 3\n"
2959-
"1_arg 3\n"
2960-
"1_arg 3\n"
2959+
"0\n"
2960+
"1\n"
2961+
"2\n"
29612962
"1_arg 1\n"
29622963
"1_arg 1\n"
29632964
"1_arg 2\n"
2964-
"1_arg 3\n"
2965-
"1_arg 3\n"
2966-
"1_arg 3\n";
2965+
"0\n"
2966+
"1\n"
2967+
"2\n";
29672968

29682969
EXPECT_CALL(m_target, isStage).WillRepeatedly(Return(false));
29692970
testing::internal::CaptureStdout();

test/mocks/codebuildermock.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ class CodeBuilderMock : public ICodeBuilder
1313
MOCK_METHOD(CompilerValue *, addTargetFunctionCall, (const std::string &, Compiler::StaticType, const Compiler::ArgTypes &, const Compiler::Args &), (override));
1414
MOCK_METHOD(CompilerValue *, addFunctionCallWithCtx, (const std::string &, Compiler::StaticType, const Compiler::ArgTypes &, const Compiler::Args &), (override));
1515
MOCK_METHOD(CompilerConstant *, addConstValue, (const Value &), (override));
16+
MOCK_METHOD(CompilerValue *, addLoopIndex, (), (override));
1617
MOCK_METHOD(CompilerValue *, addVariableValue, (Variable *), (override));
1718
MOCK_METHOD(CompilerValue *, addListContents, (List *), (override));
1819
MOCK_METHOD(CompilerValue *, addListItem, (List *, CompilerValue *), (override));

0 commit comments

Comments
 (0)