Skip to content

Commit 64b447e

Browse files
committed
fix(bleach): cast args before calls to instruction handlers
Not casting values caused IR verifier to fail on FPU tests
1 parent a094084 commit 64b447e

2 files changed

Lines changed: 40 additions & 10 deletions

File tree

lib/lifter/lifter.cpp

Lines changed: 38 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -944,6 +944,24 @@ static bool is_branch(const MachineInstr &minst,
944944
}) != minst.operands_end();
945945
}
946946

947+
static Value *cast_value_to(IRBuilder<> &builder, Value *val, Type *ty) {
948+
auto from_id = val->getType()->getTypeID();
949+
auto to_id = ty->getTypeID();
950+
if (from_id != Type::TypeID::IntegerTyID)
951+
throw std::invalid_argument(
952+
"currently casting is only supported for integral types");
953+
if (to_id != Type::TypeID::IntegerTyID)
954+
throw std::invalid_argument(
955+
"currently casting is only supported for integral types");
956+
auto *from_int = dyn_cast<IntegerType>(val->getType());
957+
auto *to_int = dyn_cast<IntegerType>(ty);
958+
auto from_width = from_int->getBitWidth();
959+
auto to_width = to_int->getBitWidth();
960+
if (from_width == to_width)
961+
return val;
962+
return builder.CreateSExtOrTrunc(val, ty);
963+
}
964+
947965
auto generate_instruction(const MachineInstr &minst, BasicBlock &bb,
948966
IRBuilder<> &builder, reg2vals &rmap,
949967
const instr_impl &instrs,
@@ -999,15 +1017,26 @@ auto generate_instruction(const MachineInstr &minst, BasicBlock &bb,
9991017
return generate_stack_pointer_modification(
10001018
minst, builder, bb, target_machine, rmap, instrs, reg_stats);
10011019
}
1002-
auto op_to_val = [&](auto &mop) {
1003-
return operand_to_value(mop, bb, target_machine, rmap, reg_stats, instrs);
1004-
};
1005-
auto values = minst.uses() | views::filter([](auto &mi) {
1006-
return !mi.isReg() || (!mi.isDef() && !mi.isImplicit());
1007-
}) |
1008-
views::transform(op_to_val);
1009-
std::vector<Value *> args(values.begin(), values.end());
1010-
auto *call = builder.CreateCall(func->getFunctionType(), func, args);
1020+
auto *func_type = func->getFunctionType();
1021+
assert(func_type);
1022+
std::vector<Type *> param_types(func_type->param_begin(),
1023+
func_type->param_end());
1024+
auto args = minst.uses() | views::filter([](auto &mi) {
1025+
return !mi.isReg() || (!mi.isDef() && !mi.isImplicit());
1026+
}) |
1027+
views::transform([&](auto &mop) -> Value * {
1028+
return operand_to_value(mop, bb, target_machine, rmap,
1029+
reg_stats, instrs);
1030+
}) |
1031+
ranges::to<std::vector>();
1032+
auto casted_args = views::zip(args, param_types) |
1033+
views::transform([&](auto &&arg) -> Value * {
1034+
auto &&[val, ty] = arg;
1035+
return cast_value_to(builder, val, ty);
1036+
}) |
1037+
ranges::to<std::vector>();
1038+
1039+
auto *call = builder.CreateCall(func->getFunctionType(), func, casted_args);
10111040
for (auto &def : minst.defs()) {
10121041
assert(def.isDef());
10131042
if (!def.isReg())

test/tools/llvm-bleach/no-stack-loadstores.test

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@
44

55
# CHECK: %[[X4:[0-9]+]] = call i64 @LD(i64 %{{[0-9]+}}, i64 1352)
66
# CHECK-NEXT: call void @SD(i64 %{{[0-9]+}}, i64 %{{[0-9]+}}, i64 269)
7-
# CHECK-NEXT: call void @SW(i64 %{{[0-9]+}}, i64 %{{[0-9]+}}, i64 1926)
7+
# CHECK-NEXT: %[[X15:[0-9]+]] = trunc
8+
# CHECK-NEXT: call void @SW(i32 %[[X15]], i64 %{{[0-9]+}}, i64 1926)
89
# CHECK-NEXT: call void @SD(i64 %{{[0-9]+}}, i64 %{{[0-9]+}}, i64 1533)
910
# CHECK-NEXT: %[[X7:[0-9]+]] = call i64 @LD(i64 %[[X4]], i64 -246)
1011
# CHECK-NEXT: call void @SD(i64 %{{[0-9]+}}, i64 %{{[0-9]+}}, i64 384)

0 commit comments

Comments
 (0)