Skip to content
Draft
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
6 changes: 6 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,12 @@ check-lit-c:
check-lit-nvgpu:
[ `uname -s` = Darwin ] || env XTC_MLIR_TARGET=nvgpu lit -v tests/filecheck/backends tests/filecheck/mlir_loop tests/filecheck/evaluation

check-lit-mppa:
env XTC_MLIR_TARGET=mppa lit -v -j 1 tests/filecheck/backends/target_mppa

check-lit-aie:
env XTC_MLIR_TARGET=aie lit -v -j 1 tests/filecheck/backends/target_aie

check-pytest:
scripts/pytest/run_pytest.sh -v

Expand Down
2 changes: 1 addition & 1 deletion sdist_requirements.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
--index-url https://gitlab.inria.fr/api/v4/groups/corse/-/packages/pypi/simple
mlir-sdist==21.1.2.2026012001
mlir-sdist==21.1.2.2026021601
mlir==21.1.2.2025091603
xtc-mlir==21.1.2.2
11 changes: 9 additions & 2 deletions src/xtc/backends/mlir/MlirCompiler.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,14 @@
get_default_target,
)
from xtc.utils.ext_tools import get_shlib_extension
from xtc.itf.runtime.common import CommonRuntimeInterface


class MlirCompiler(itf.comp.Compiler):
def __init__(
self,
backend: "backend.MlirBackend",
target: str | None = None,
target: str | CommonRuntimeInterface | None = None,
**kwargs: Any,
):
self._backend = backend
Expand All @@ -44,9 +45,12 @@ def __init__(
self._config = MlirConfig(**kwargs)
if target is None:
self._target = get_default_target()(self._config)
else:
elif isinstance(target, str):
self._target = get_target_from_name(target)(self._config)
elif isinstance(target, CommonRuntimeInterface):
self._target = get_target_from_name(target.target_name())(self._config)
assert self._target is not None
self._runtime_target = target
self._compiler_kwargs = kwargs

@property
Expand Down Expand Up @@ -136,6 +140,7 @@ def mlir_insert_transform_pass(self) -> None:
concluding_passes=self._config.concluding_passes,
always_vectorize=self._config.always_vectorize,
vectors_size=self._config.vectors_size,
target=self._target,
)
insert_transform_pass.run()
if self._config.print_source_ir:
Expand All @@ -157,6 +162,8 @@ def _save_temp(self, fname: str, content: Any) -> None:
outf.write(str(content))

def _register_mlir_extensions(self) -> None:
for extension in self._config.required_extensions:
self._mlir_program.require_extension(extension, weak=False)
if self._mlir_schedule is not None:
for extension, weak in self._mlir_schedule.mlir_extensions.items():
self._mlir_program.require_extension(extension, weak=weak)
Expand Down
18 changes: 12 additions & 6 deletions src/xtc/backends/mlir/MlirCompilerPasses.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@

from .MlirProgram import RawMlirProgram
from .MlirScheduler import MlirSchedule, MlirNodeSchedule
from .MlirTarget import MlirTarget

_VECTO_SEQ_NAME = "_vecto"
_SUPER_VECTORIZE_SEQ_NAME = "_super_vectorize"
Expand Down Expand Up @@ -98,12 +99,14 @@ class MlirProgramInsertTransformPass:
def __init__(
self,
mlir_program: RawMlirProgram,
target: MlirTarget,
mlir_schedule: MlirSchedule | None = None,
concluding_passes: list[str] = [],
always_vectorize: bool = True,
vectors_size: int | None = None,
) -> None:
self._mlir_program = mlir_program
self._target = target
self._mlir_schedule = mlir_schedule
self._loc = Location.unknown(self._mlir_program.mlir_context)
self._concluding_passes = concluding_passes
Expand Down Expand Up @@ -428,12 +431,15 @@ def _vectorize(self, sched_state: SchedulingState):
if self._vectors_size is not None:
return

transform.IncludeOp(
results_=[],
target=_VECTO_SEQ_NAME,
failure_propagation_mode=2,
operands_=[sched_state.handle],
)
if self._target.custom_vectorize():
self._target.apply_custom_vectorize(sched_state.handle)
else:
transform.IncludeOp(
results_=[],
target=_VECTO_SEQ_NAME,
failure_propagation_mode=2,
operands_=[sched_state.handle],
)

def _post_vectorize(self, sched_state: SchedulingState):
if self._vectors_size is not None:
Expand Down
1 change: 1 addition & 0 deletions src/xtc/backends/mlir/MlirConfig.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ class MlirConfig:
arch: str = "native"
cpu: str = "native"
selected_device: int | None = None
required_extensions: list[str] = field(default_factory=list)

def __post_init__(self):
object.__setattr__(
Expand Down
28 changes: 24 additions & 4 deletions src/xtc/backends/mlir/MlirGraphBackend.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,14 @@

from xdsl.dialects.func import FuncOp as xdslFuncOp
from xdsl.dialects import func, memref
from xdsl.dialects.builtin import MemRefType, f32, f64
from xdsl.dialects.builtin import (
MemRefType,
f32,
f64,
ArrayAttr,
UnitAttr,
DictionaryAttr,
)
from xdsl.ir import Region, Block, Operation
from xdsl.builder import ImplicitBuilder

Expand Down Expand Up @@ -97,6 +104,14 @@ def _init_from_graph(
self._xdsl_type_from_tensortype(cast(XTCTensorType, tensor_type))
for tensor_type in [*inputs_types, *outputs_types]
]
arg_attrs = ArrayAttr(
[
DictionaryAttr(
self._xdsl_attrs_from_tensortype(cast(XTCTensorType, tensor_type))
)
for tensor_type in [*inputs_types, *outputs_types]
]
)
inlined_block = Block(arg_types=params_types)
variables = {
name: arg
Expand All @@ -109,11 +124,11 @@ def _init_from_graph(
with ImplicitBuilder(inlined_block):
func.ReturnOp()
region = Region([inlined_block]) # type: ignore # issue with mypy
payload = xdslFuncOp.from_region(
payload = xdslFuncOp(
name=graph.name,
input_types=params_types,
return_types=[],
function_type=(params_types, []),
region=region,
arg_attrs=arg_attrs,
)
nodes_dict = {}
for attrs in block_attrs:
Expand All @@ -139,6 +154,11 @@ def _xdsl_type_from_tensortype(self, type: XTCTensorType) -> Any:
elt_type, shape = self._xdsl_elt_shape_from_tensortype(type)
return MemRefType(elt_type, shape)

def _xdsl_attrs_from_tensortype(self, type: XTCTensorType):
if type.device is not None:
return {"memref.on_device": UnitAttr()}
return {}

def _np_types_spec(
self, types: list[MemRefType]
) -> list[dict[str, tuple[int, ...] | str]]:
Expand Down
35 changes: 19 additions & 16 deletions src/xtc/backends/mlir/MlirProgram.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,23 +73,26 @@ def parse_and_add_function(
function, context=self.mlir_context
)

# Insert (or not) the noalias attributes
arg_attrs = []
if no_alias:
for _ in payload_func.arguments:
dict_attr = DictAttr.get(
{
"llvm.noalias": UnitAttr.get(context=self.mlir_context),
},
context=self.mlir_context,
with self.mlir_context:
# Insert (or not) the noalias attributes
new_arg_attrs = []
if no_alias:
for arg_attrs in payload_func.arg_attrs:
new_dict = {}
for i in range(len(arg_attrs)):
new_dict[arg_attrs[i].name] = arg_attrs[i].attr
new_dict["llvm.noalias"] = UnitAttr.get(context=self.mlir_context)
new_arg_attrs.append(
DictAttr.get(new_dict, context=self.mlir_context)
)
payload_func.arg_attrs = ArrayAttr.get(
new_arg_attrs, context=self.mlir_context
)
arg_attrs.append(dict_attr)
payload_func.arg_attrs = ArrayAttr.get(arg_attrs, context=self.mlir_context)

# Insert the function in the MLIR program
ip = InsertionPoint.at_block_begin(self.mlir_module.body)
ip.insert(payload_func)
name = str(payload_func.name).replace('"', "")
self.local_functions[str(name)] = payload_func
# Insert the function in the MLIR program
ip = InsertionPoint.at_block_begin(self.mlir_module.body)
ip.insert(payload_func)
name = str(payload_func.name).replace('"', "")
self.local_functions[str(name)] = payload_func

return payload_func
Loading