Elaborating on imports and using the dialects

from mlir.ir import (
Context,
InsertionPoint,
Location,
Module,
)
Can someone elaborate on the modules
Context,
InsertionPoint,
Location,
Module

How to use this dialects ( can one give some example code for below)
AddEintIntOp’, ‘AddEintOp’, ‘ApplyLookupTableEintOp’, ‘BoolAndOp’, ‘BoolNandOp’, ‘BoolNotOp’, ‘BoolOrOp’, ‘BoolXorOp’, . These operations are available under concrete lang dialects in the file _FHE_ops_gen.py

Hi @Laser_beam,

Here is how to use those to generate MLIR:

import numpy as np
from typing import Tuple

import concrete.lang
from concrete.lang.dialects import fhe, fhelinalg
from concrete.lang.dialects.fhe import EncryptedIntegerType, EncryptedSignedIntegerType

import mlir
from mlir.dialects import arith, func, tensor
from mlir.ir import *


class DType:
    context: Context

    def __init__(self, context: Context):
        self.context = context

    def i(self, width: int) -> Type:
        return IntegerType.get_signless(width)

    def eint(self, width: int) -> Type:
        return EncryptedIntegerType.get(self.context, width)

    def esint(self, width: int) -> Type:
        return EncryptedSignedIntegerType.get(self.context, width)

    def index(self) -> Type:
        return IndexType.parse("index")

    def tensor(self, dtype: Type, shape: Tuple[int, ...]) -> Type:
        return RankedTensorType.get(shape, dtype)


def define(dtype):

    def attribute(dtype, value) -> Attribute:
        if isinstance(value, (list, np.ndarray)):
            value = value if isinstance(value, list) else value.tolist()
            return Attribute.parse(f"dense<{value}> : {dtype}")
        return Attribute.parse(f"{value} : {dtype}")

    def add_eint_int(dtype, lhs, rhs) -> OpResult:
        return fhe.AddEintIntOp(dtype, lhs, rhs)

    def constant(dtype, value) -> OpResult:
        return arith.ConstantOp(dtype, attribute(dtype, value))

    parameters = [dtype.eint(8)]
    def main(a1):
        t1 = constant(dtype.i(9), 42)
        t2 = add_eint_int(dtype.eint(8), a1, t1)
        return t2

    return parameters, main


with Context() as context, Location.unknown():
    concrete.lang.register_dialects(context)

    module = Module.create()
    with InsertionPoint(module.body):
        dtype = DType(context)
        parameters, function = define(dtype)

        @func.FuncOp.from_py_func(*parameters)
        def main(*args):
            return function(*args)

        print(str(module))

Keep in mind that these are internal objects and they are subject to change at any moment :slightly_smiling_face:

Hope this helps!

1 Like

Thankyou @umutsahin if you or anyone could explain a bit on what is context,module,location,insertion module

Those are internal LLVM objects.

LLVM is a huge project and we can’t explain everything. Please refer to LLVM and MLIR documentation if you want to learn about these things.

1 Like