Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 20 additions & 3 deletions src/irx/builders/llvmliteir/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -577,9 +577,26 @@ def _unify_numeric_operands(
f"{lhs.type.count} vs {rhs.type.count}"
)
if lhs.type.element != rhs.type.element:
raise Exception(
"Vector element type mismatch: "
f"{lhs.type.element} vs {rhs.type.element}"
lhs_elem = lhs.type.element
rhs_elem = rhs.type.element
lhs_is_fp = is_fp_type(lhs_elem)
rhs_is_fp = is_fp_type(rhs_elem)

if lhs_is_fp or rhs_is_fp:
fp_candidates = [
t for t in (lhs_elem, rhs_elem) if is_fp_type(t)
]
target_scalar_ty = self._select_float_type(fp_candidates)
else:
lhs_w = getattr(lhs_elem, "width", 0)
rhs_w = getattr(rhs_elem, "width", 0)
target_scalar_ty = ir.IntType(max(lhs_w, rhs_w, 1))

lhs = self._cast_value_to_type(
lhs, target_scalar_ty, unsigned=unsigned
)
rhs = self._cast_value_to_type(
rhs, target_scalar_ty, unsigned=unsigned
)
return lhs, rhs

Expand Down
46 changes: 28 additions & 18 deletions src/irx/builders/llvmliteir/visitors/binary_ops.py
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,8 @@ def _emit_vector_mul(
node: MulBinOp,
llvm_lhs: ir.Value,
llvm_rhs: ir.Value,
*,
unsigned: bool = False,
) -> ir.Value | None:
"""
title: Emit vector mul.
Expand All @@ -192,6 +194,8 @@ def _emit_vector_mul(
type: ir.Value
llvm_rhs:
type: ir.Value
unsigned:
type: bool
returns:
type: ir.Value | None
"""
Expand All @@ -208,11 +212,12 @@ def _emit_vector_mul(
llvm_fma_rhs = safe_pop(self.result_stack)
if llvm_fma_rhs is None:
raise Exception("FMA requires a valid third operand")
if llvm_fma_rhs.type != llvm_lhs.type:
raise Exception(
f"FMA operand type mismatch: "
f"{llvm_lhs.type} vs {llvm_fma_rhs.type}"
)
llvm_lhs, llvm_fma_rhs = self._unify_numeric_operands(
llvm_lhs, llvm_fma_rhs, unsigned=unsigned
)
llvm_rhs, llvm_fma_rhs = self._unify_numeric_operands(
llvm_rhs, llvm_fma_rhs, unsigned=unsigned
)
prev_fast_math = self._fast_math_enabled
if set_fast:
self.set_fast_math(True)
Expand Down Expand Up @@ -306,7 +311,10 @@ def _emit_ordered_compare(
returns:
type: ir.Value
"""
if is_fp_type(llvm_lhs.type):
scalar_ty = (
llvm_lhs.type.element if is_vector(llvm_lhs) else llvm_lhs.type
)
if is_fp_type(scalar_ty):
return self._llvm.ir_builder.fcmp_ordered(
op_code,
llvm_lhs,
Expand Down Expand Up @@ -432,9 +440,11 @@ def visit(self, node: MulBinOp) -> None:
node:
type: MulBinOp
"""
llvm_lhs, llvm_rhs, _unsigned = self._load_binary_operands(node)
llvm_lhs, llvm_rhs, unsigned = self._load_binary_operands(node)

vector_result = self._emit_vector_mul(node, llvm_lhs, llvm_rhs)
vector_result = self._emit_vector_mul(
node, llvm_lhs, llvm_rhs, unsigned=unsigned
)
if vector_result is not None:
self.result_stack.append(vector_result)
return
Expand Down Expand Up @@ -529,8 +539,6 @@ def visit(self, node: LtBinOp) -> None:
type: LtBinOp
"""
llvm_lhs, llvm_rhs, unsigned = self._load_binary_operands(node)
if is_vector(llvm_lhs) and is_vector(llvm_rhs):
raise Exception(f"Vector binop {node.op_code} not implemented.")
result = self._emit_ordered_compare(
"<",
llvm_lhs,
Expand All @@ -549,8 +557,6 @@ def visit(self, node: GtBinOp) -> None:
type: GtBinOp
"""
llvm_lhs, llvm_rhs, unsigned = self._load_binary_operands(node)
if is_vector(llvm_lhs) and is_vector(llvm_rhs):
raise Exception(f"Vector binop {node.op_code} not implemented.")
result = self._emit_ordered_compare(
">",
llvm_lhs,
Expand All @@ -569,8 +575,6 @@ def visit(self, node: LeBinOp) -> None:
type: LeBinOp
"""
llvm_lhs, llvm_rhs, unsigned = self._load_binary_operands(node)
if is_vector(llvm_lhs) and is_vector(llvm_rhs):
raise Exception(f"Vector binop {node.op_code} not implemented.")
result = self._emit_ordered_compare(
"<=",
llvm_lhs,
Expand All @@ -589,8 +593,6 @@ def visit(self, node: GeBinOp) -> None:
type: GeBinOp
"""
llvm_lhs, llvm_rhs, unsigned = self._load_binary_operands(node)
if is_vector(llvm_lhs) and is_vector(llvm_rhs):
raise Exception(f"Vector binop {node.op_code} not implemented.")
result = self._emit_ordered_compare(
">=",
llvm_lhs,
Expand All @@ -611,7 +613,11 @@ def visit(self, node: EqBinOp) -> None:
llvm_lhs, llvm_rhs, unsigned = self._load_binary_operands(node)

if is_vector(llvm_lhs) and is_vector(llvm_rhs):
raise Exception(f"Vector binop {node.op_code} not implemented.")
result = self._emit_ordered_compare(
"==", llvm_lhs, llvm_rhs, unsigned=unsigned, name="vcmptmp"
)
self.result_stack.append(result)
return

if (
isinstance(llvm_lhs.type, ir.PointerType)
Expand Down Expand Up @@ -654,7 +660,11 @@ def visit(self, node: NeBinOp) -> None:
llvm_lhs, llvm_rhs, unsigned = self._load_binary_operands(node)

if is_vector(llvm_lhs) and is_vector(llvm_rhs):
raise Exception(f"Vector binop {node.op_code} not implemented.")
result = self._emit_ordered_compare(
"!=", llvm_lhs, llvm_rhs, unsigned=unsigned, name="vcmptmp"
)
self.result_stack.append(result)
return

if (
isinstance(llvm_lhs.type, ir.PointerType)
Expand Down
62 changes: 61 additions & 1 deletion tests/test_binary_op.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
import pytest

from irx import astx
from irx.astx import PrintExpr
from irx.builders.base import Builder
from irx.builders.llvmliteir import Builder as LLVMBuilder
from irx.system import PrintExpr

from .conftest import check_result

Expand Down Expand Up @@ -255,3 +255,63 @@ def test_binary_op_logical_and_or(
module.block.append(main_fn)

check_result("build", builder, module, expected_output=expect)


@pytest.mark.parametrize(
"a_val, b_val, op, true_label, false_label",
[
(2.0, 3.0, "<=", "le_true", "le_false"),
(3.0, 2.0, ">=", "ge_true", "ge_false"),
(2.0, 2.0, "==", "eq_true", "eq_false"),
(1.0, 2.0, "!=", "ne_true", "ne_false"),
],
)
@pytest.mark.parametrize("builder_class", [LLVMBuilder])
def test_binary_op_float_comparison(
builder_class: type[Builder],
a_val: float,
b_val: float,
op: str,
true_label: str,
false_label: str,
) -> None:
"""
title: Test Float32 comparison operators cover fcmp_ordered paths.
parameters:
builder_class:
type: type[Builder]
a_val:
type: float
b_val:
type: float
op:
type: str
true_label:
type: str
false_label:
type: str
"""
builder = builder_class()
module = builder.module()

cond = astx.BinaryOp(
op_code=op,
lhs=astx.LiteralFloat32(a_val),
rhs=astx.LiteralFloat32(b_val),
)
then_blk = astx.Block()
then_blk.append(PrintExpr(astx.LiteralUTF8String(true_label)))
else_blk = astx.Block()
else_blk.append(PrintExpr(astx.LiteralUTF8String(false_label)))
if_stmt = astx.IfStmt(condition=cond, then=then_blk, else_=else_blk)

proto = astx.FunctionPrototype(
name="main", args=astx.Arguments(), return_type=astx.Int32()
)
block = astx.Block()
block.append(if_stmt)
block.append(astx.FunctionReturn(astx.LiteralInt32(0)))
fn = astx.FunctionDef(prototype=proto, body=block)
module.block.append(fn)

check_result("build", builder, module, expected_output=true_label)
Loading
Loading