Skip to content

Convert op

convert_op

CastInstrWithFlags

Bases: CastInstr

Source code in xdsl/backend/llvm/convert_op.py
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
class CastInstrWithFlags(instructions.CastInstr):
    # workaround: llvmlite's CastInstr doesn't support flags like 'trunc nsw' or 'zext nneg'
    def __init__(
        self,
        parent: LLVMBlock,
        op: str,
        val: Value,
        typ: Type,
        name: str = "",
        flags: tuple[str, ...] | list[str] = (),
    ):
        instructions.Instruction.__init__(
            self, parent, typ, op, [val], name=name, flags=flags
        )

    def descr(self, buf: list[str]) -> None:
        opname = (
            " ".join([self.opname] + list(self.flags)) if self.flags else self.opname
        )
        op = self.operands[0]
        buf.append(
            f"{opname} {op.type} {op.get_reference()} to {self.type}{self._stringify_metadata(leading_comma=True)}\n"
        )

__init__(parent: LLVMBlock, op: str, val: Value, typ: Type, name: str = '', flags: tuple[str, ...] | list[str] = ())

Source code in xdsl/backend/llvm/convert_op.py
114
115
116
117
118
119
120
121
122
123
124
125
def __init__(
    self,
    parent: LLVMBlock,
    op: str,
    val: Value,
    typ: Type,
    name: str = "",
    flags: tuple[str, ...] | list[str] = (),
):
    instructions.Instruction.__init__(
        self, parent, typ, op, [val], name=name, flags=flags
    )

descr(buf: list[str]) -> None

Source code in xdsl/backend/llvm/convert_op.py
127
128
129
130
131
132
133
134
def descr(self, buf: list[str]) -> None:
    opname = (
        " ".join([self.opname] + list(self.flags)) if self.flags else self.opname
    )
    op = self.operands[0]
    buf.append(
        f"{opname} {op.type} {op.get_reference()} to {self.type}{self._stringify_metadata(leading_comma=True)}\n"
    )

convert_op(op: Operation, builder: ir.IRBuilder, val_map: dict[SSAValue, ir.Value], block_map: dict[Block, LLVMBlock] | None = None)

Convert an xDSL operation to an llvmlite LLVM IR.

Side effects

Mutates val_map by adding entries for the operation's results.

Parameters:

Name Type Description Default
op Operation

The xDSL operation to convert

required
builder IRBuilder

The LLVM IR builder for constructing instructions

required
val_map dict[SSAValue, Value]

The Mapping from xDSL SSA values to LLVM IR values. This dictionary is mutated to store the LLVM IR value produced by this operation for use by subsequent operations.

required

Raises:

Type Description
NotImplementedError

If the operation is not supported.

Source code in xdsl/backend/llvm/convert_op.py
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
def convert_op(
    op: Operation,
    builder: ir.IRBuilder,
    val_map: dict[SSAValue, ir.Value],
    block_map: dict[Block, LLVMBlock] | None = None,
):
    """
    Convert an xDSL operation to an llvmlite LLVM IR.

    Side effects:
        Mutates val_map by adding entries for the operation's results.

    Args:
        op: The xDSL operation to convert
        builder: The LLVM IR builder for constructing instructions
        val_map: The Mapping from xDSL SSA values to LLVM IR values.
                 This dictionary is mutated to store the LLVM IR value produced by this operation for
                 use by subsequent operations.

    Raises:
        NotImplementedError: If the operation is not supported.
    """
    match op:
        case op if type(op) in _BINARY_OP_MAP:
            _convert_binop(op, builder, val_map)
        case llvm.ICmpOp():
            _convert_icmp(op, builder, val_map)
        case llvm.FCmpOp():
            _convert_fcmp(op, builder, val_map)
        case op if type(op) in _CAST_OP_NAMES:
            _convert_cast(op, builder, val_map)
        case op if type(op) in _UNARY_INTRINSIC_MAP:
            _convert_unary_intrinsic(op, builder, val_map)
        case op if type(op) in _BINARY_INTRINSIC_MAP:
            _convert_binary_intrinsic(op, builder, val_map)
        case llvm.FNegOp():
            _convert_fneg(op, builder, val_map)
        case llvm.CallOp():
            _convert_call(op, builder, val_map)
        case llvm.AllocaOp():
            _convert_alloca(op, builder, val_map)
        case llvm.LoadOp():
            _convert_load(op, builder, val_map)
        case llvm.StoreOp():
            _convert_store(op, builder, val_map)
        case llvm.ExtractValueOp():
            val_map[op.results[0]] = builder.extract_value(
                val_map[op.container], list(op.position.iter_values())
            )
        case llvm.InsertValueOp():
            val_map[op.results[0]] = builder.insert_value(
                val_map[op.container],
                val_map[op.value],
                list(op.position.iter_values()),
            )
        case llvm.GEPOp():
            _convert_getelementptr(op, builder, val_map)
        case llvm.InlineAsmOp():
            _convert_inline_asm(op, builder, val_map)
        case llvm.BrOp() if block_map is not None:
            _convert_br(op, builder, val_map, block_map)
        case llvm.CondBrOp() if block_map is not None:
            _convert_condbr(op, builder, val_map, block_map)
        case llvm.UnreachableOp():
            builder.unreachable()
        case llvm.SelectOp():
            _convert_select(op, builder, val_map)
        case llvm.MaskedStoreOp():
            _convert_masked_store(op, builder, val_map)
        case llvm.ReturnOp():
            _convert_return(op, builder, val_map)
        case llvm.ZeroOp():
            val_map[op.res] = ir.Constant(convert_type(op.res.type), None)
        case llvm.UndefOp():
            val_map[op.res] = ir.Constant(convert_type(op.res.type), ir.Undefined)
        case llvm.AddressOfOp():
            _convert_addressof(op, builder, val_map)
        case llvm.CallIntrinsicOp():
            _convert_call_intrinsic(op, builder, val_map)
        case llvm.InsertElementOp():
            val_map[op.res] = builder.insert_element(
                val_map[op.vector],
                val_map[op.value],
                val_map[op.index],
            )
        case llvm.ShuffleVectorOp():
            _convert_shuffle_vector(op, builder, val_map)
        case llvm.FMAOp():
            _convert_fma(op, builder, val_map)
        case llvm.ConstantOp():
            _convert_constant(op, builder, val_map)
        case _:
            raise NotImplementedError(f"Conversion not implemented for op: {op.name}")