Skip to content

Commit cf1730c

Browse files
committed
LLVMCodeBuilder: Fix infinite count in repeat loop
1 parent d932257 commit cf1730c

File tree

2 files changed

+21
-1
lines changed

2 files changed

+21
-1
lines changed

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -784,6 +784,7 @@ std::shared_ptr<ExecutableCode> LLVMCodeBuilder::finalize()
784784
const auto &reg = step.args[0];
785785
assert(reg.first == Compiler::StaticType::Number);
786786
llvm::Value *count = castValue(reg.second, reg.first);
787+
llvm::Value *isInf = m_builder.CreateFCmpOEQ(count, llvm::ConstantFP::getInfinity(m_builder.getDoubleTy(), false));
787788

788789
// Clamp count if <= 0 (we can skip the loop if count is not positive)
789790
llvm::Value *comparison = m_builder.CreateFCmpULE(count, llvm::ConstantFP::get(m_ctx, llvm::APFloat(0.0)));
@@ -795,6 +796,7 @@ std::shared_ptr<ExecutableCode> LLVMCodeBuilder::finalize()
795796
llvm::Function *roundFunc = llvm::Intrinsic::getDeclaration(m_module.get(), llvm::Intrinsic::round, { count->getType() });
796797
count = m_builder.CreateCall(roundFunc, { count });
797798
count = m_builder.CreateFPToSI(count, m_builder.getInt64Ty()); // cast to signed integer
799+
count = m_builder.CreateSelect(isInf, zero, count);
798800

799801
// Jump to condition branch
800802
m_builder.CreateBr(loop.conditionBranch);
@@ -808,7 +810,7 @@ std::shared_ptr<ExecutableCode> LLVMCodeBuilder::finalize()
808810
loop.afterLoop = llvm::BasicBlock::Create(m_ctx, "", func);
809811

810812
llvm::Value *currentIndex = m_builder.CreateLoad(m_builder.getInt64Ty(), loop.index);
811-
comparison = m_builder.CreateICmpULT(currentIndex, count);
813+
comparison = m_builder.CreateOr(isInf, m_builder.CreateICmpULT(currentIndex, count));
812814
m_builder.CreateCondBr(comparison, body, loop.afterLoop);
813815

814816
// Switch to body branch

test/dev/llvm/llvmcodebuilder_test.cpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3008,6 +3008,24 @@ TEST_F(LLVMCodeBuilderTest, RepeatLoop)
30083008
ctx = code->createExecutionContext(&thread);
30093009
code->run(ctx.get());
30103010
ASSERT_TRUE(code->isFinished(ctx.get()));
3011+
3012+
// Infinite no warp loop
3013+
createBuilder(false);
3014+
3015+
v = m_builder->addConstValue("Infinity");
3016+
m_builder->beginRepeatLoop(v);
3017+
m_builder->addTargetFunctionCall("test_function_no_args", Compiler::StaticType::Void, {}, {});
3018+
m_builder->endLoop();
3019+
3020+
code = m_builder->finalize();
3021+
ctx = code->createExecutionContext(&thread);
3022+
3023+
for (int i = 0; i < 10; i++) {
3024+
testing::internal::CaptureStdout();
3025+
code->run(ctx.get());
3026+
ASSERT_EQ(testing::internal::GetCapturedStdout(), "no_args\n");
3027+
ASSERT_FALSE(code->isFinished(ctx.get()));
3028+
}
30113029
}
30123030

30133031
TEST_F(LLVMCodeBuilderTest, WhileLoop)

0 commit comments

Comments
 (0)