Skip to content

Convert op

convert_op

CastInstrWithFlags

Bases: CastInstr

Source code in xdsl/backend/llvm/convert_op.py
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
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
106
107
108
109
110
111
112
113
114
115
116
117
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
119
120
121
122
123
124
125
126
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
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
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 llvm.FAbsOp():
            _convert_fabs(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.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 _:
            raise NotImplementedError(f"Conversion not implemented for op: {op.name}")