Skip to content

Commit 297ae3a

Browse files
committed
LLVMCodeBuilder: Add support for unknown type in select
1 parent 08b233f commit 297ae3a

File tree

3 files changed

+37
-11
lines changed

3 files changed

+37
-11
lines changed

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

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -505,9 +505,19 @@ std::shared_ptr<ExecutableCode> LLVMCodeBuilder::finalize()
505505
const auto &arg1 = step.args[0];
506506
const auto &arg2 = step.args[1];
507507
const auto &arg3 = step.args[2];
508+
auto type = arg2.first;
508509
llvm::Value *cond = castValue(arg1.second, arg1.first);
509-
llvm::Value *trueValue = castValue(arg2.second, arg2.first);
510-
llvm::Value *falseValue = castValue(arg3.second, arg3.first);
510+
llvm::Value *trueValue;
511+
llvm::Value *falseValue;
512+
513+
if (type == Compiler::StaticType::Unknown) {
514+
trueValue = createValue(arg2.second);
515+
falseValue = createValue(arg3.second);
516+
} else {
517+
trueValue = castValue(arg2.second, type);
518+
falseValue = castValue(arg3.second, type);
519+
}
520+
511521
step.functionReturnReg->value = m_builder.CreateSelect(cond, trueValue, falseValue);
512522
break;
513523
}
@@ -1100,7 +1110,7 @@ CompilerConstant *LLVMCodeBuilder::addConstValue(const Value &value)
11001110
{
11011111
auto constReg = std::make_shared<LLVMConstantRegister>(TYPE_MAP[value.type()], value);
11021112
auto reg = std::reinterpret_pointer_cast<LLVMRegister>(constReg);
1103-
return static_cast<CompilerConstant *>(addReg(reg));
1113+
return static_cast<CompilerConstant *>(static_cast<CompilerValue *>(addReg(reg)));
11041114
}
11051115

11061116
CompilerValue *LLVMCodeBuilder::addLoopIndex()
@@ -1317,7 +1327,12 @@ CompilerValue *LLVMCodeBuilder::createExp10(CompilerValue *num)
13171327

13181328
CompilerValue *LLVMCodeBuilder::createSelect(CompilerValue *cond, CompilerValue *trueValue, CompilerValue *falseValue, Compiler::StaticType valueType)
13191329
{
1320-
return createOp(LLVMInstruction::Type::Select, valueType, { Compiler::StaticType::Bool, valueType, valueType }, { cond, trueValue, falseValue });
1330+
LLVMRegister *ret = createOp(LLVMInstruction::Type::Select, valueType, { Compiler::StaticType::Bool, valueType, valueType }, { cond, trueValue, falseValue });
1331+
1332+
if (valueType == Compiler::StaticType::Unknown)
1333+
ret->isRawValue = false;
1334+
1335+
return ret;
13211336
}
13221337

13231338
CompilerLocalVariable *LLVMCodeBuilder::createLocalVariable(Compiler::StaticType type)
@@ -1569,7 +1584,7 @@ void LLVMCodeBuilder::optimize()
15691584
modulePassManager.run(*m_module, moduleAnalysisManager);
15701585
}
15711586

1572-
CompilerValue *LLVMCodeBuilder::addReg(std::shared_ptr<LLVMRegister> reg)
1587+
LLVMRegister *LLVMCodeBuilder::addReg(std::shared_ptr<LLVMRegister> reg)
15731588
{
15741589
m_regs.push_back(reg);
15751590
return reg.get();
@@ -1875,7 +1890,7 @@ void LLVMCodeBuilder::updateListDataPtr(const LLVMListPtr &listPtr, llvm::Functi
18751890
m_builder.CreateStore(m_builder.getInt1(false), listPtr.dataPtrDirty);
18761891
}
18771892

1878-
CompilerValue *LLVMCodeBuilder::createOp(const LLVMInstruction &ins, Compiler::StaticType retType, Compiler::StaticType argType, const Compiler::Args &args)
1893+
LLVMRegister *LLVMCodeBuilder::createOp(const LLVMInstruction &ins, Compiler::StaticType retType, Compiler::StaticType argType, const Compiler::Args &args)
18791894
{
18801895
std::vector<Compiler::StaticType> types;
18811896
types.reserve(args.size());
@@ -1886,7 +1901,7 @@ CompilerValue *LLVMCodeBuilder::createOp(const LLVMInstruction &ins, Compiler::S
18861901
return createOp(ins, retType, types, args);
18871902
}
18881903

1889-
CompilerValue *LLVMCodeBuilder::createOp(const LLVMInstruction &ins, Compiler::StaticType retType, const Compiler::ArgTypes &argTypes, const Compiler::Args &args)
1904+
LLVMRegister *LLVMCodeBuilder::createOp(const LLVMInstruction &ins, Compiler::StaticType retType, const Compiler::ArgTypes &argTypes, const Compiler::Args &args)
18901905
{
18911906
m_instructions.push_back(ins);
18921907
LLVMInstruction &createdIns = m_instructions.back();

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ class LLVMCodeBuilder : public ICodeBuilder
116116
void verifyFunction(llvm::Function *func);
117117
void optimize();
118118

119-
CompilerValue *addReg(std::shared_ptr<LLVMRegister> reg);
119+
LLVMRegister *addReg(std::shared_ptr<LLVMRegister> reg);
120120

121121
void freeHeap();
122122
llvm::Value *castValue(LLVMRegister *reg, Compiler::StaticType targetType);
@@ -134,8 +134,8 @@ class LLVMCodeBuilder : public ICodeBuilder
134134
void reloadLists();
135135
void updateListDataPtr(const LLVMListPtr &listPtr, llvm::Function *func);
136136

137-
CompilerValue *createOp(const LLVMInstruction &ins, Compiler::StaticType retType, Compiler::StaticType argType, const Compiler::Args &args);
138-
CompilerValue *createOp(const LLVMInstruction &ins, Compiler::StaticType retType, const Compiler::ArgTypes &argTypes = {}, const Compiler::Args &args = {});
137+
LLVMRegister *createOp(const LLVMInstruction &ins, Compiler::StaticType retType, Compiler::StaticType argType, const Compiler::Args &args);
138+
LLVMRegister *createOp(const LLVMInstruction &ins, Compiler::StaticType retType, const Compiler::ArgTypes &argTypes = {}, const Compiler::Args &args = {});
139139

140140
void createValueStore(LLVMRegister *reg, llvm::Value *targetPtr, Compiler::StaticType sourceType, Compiler::StaticType targetType);
141141
void createReusedValueStore(LLVMRegister *reg, llvm::Value *targetPtr, Compiler::StaticType sourceType);

test/dev/llvm/llvmcodebuilder_test.cpp

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1694,6 +1694,15 @@ TEST_F(LLVMCodeBuilderTest, Select)
16941694
v = m_builder->createSelect(v, m_builder->addConstValue(1), m_builder->addConstValue("false"), Compiler::StaticType::Bool);
16951695
m_builder->addFunctionCall("test_print_bool", Compiler::StaticType::Void, { Compiler::StaticType::Bool }, { v });
16961696

1697+
// Unknown types
1698+
v = m_builder->addConstValue(true);
1699+
v = m_builder->createSelect(v, m_builder->addConstValue("test"), m_builder->addConstValue(-456.2), Compiler::StaticType::Unknown);
1700+
m_builder->addFunctionCall("test_print_string", Compiler::StaticType::Void, { Compiler::StaticType::String }, { v });
1701+
1702+
v = m_builder->addConstValue(false);
1703+
v = m_builder->createSelect(v, m_builder->addConstValue("abc"), m_builder->addConstValue(true), Compiler::StaticType::Unknown);
1704+
m_builder->addFunctionCall("test_print_string", Compiler::StaticType::Void, { Compiler::StaticType::String }, { v });
1705+
16971706
static const std::string expected =
16981707
"5.8\n"
16991708
"-17.42\n"
@@ -1704,7 +1713,9 @@ TEST_F(LLVMCodeBuilderTest, Select)
17041713
"543\n"
17051714
"0\n"
17061715
"1\n"
1707-
"0\n";
1716+
"0\n"
1717+
"test\n"
1718+
"true\n";
17081719

17091720
auto code = m_builder->finalize();
17101721
testing::internal::CaptureStdout();

0 commit comments

Comments
 (0)