1+ import os
2+ import os .path
3+ import tempfile
4+ from contextlib import contextmanager
5+ from ctypes import CDLL , byref , c_double
6+ from pathlib import Path
7+ from typing import Generator , Any
8+
9+ import pytest
10+ from mlir .runtime import (
11+ get_ranked_memref_descriptor ,
12+ make_nd_memref_descriptor ,
13+ ranked_memref_to_numpy ,
14+ )
15+
16+ import nbcc
17+ from nbcc .compiler import compile_to_mlir
18+ try :
19+ import cuda_tile
20+ except ImportError :
21+ HAS_CUDA_TILE = False
22+ else :
23+ HAS_CUDA_TILE = True
24+
25+ if HAS_CUDA_TILE :
26+ from nbcc .cutile_backend .backend import CuTileBackend
27+
28+
29+ example_dir = Path (os .path .dirname (nbcc .__file__ )) / ".." / "examples" / "cuda_tile"
30+
31+
32+ @contextmanager
33+ def make_temp_directory () -> Generator [Path , None , None ]:
34+ with tempfile .TemporaryDirectory (delete = False ) as dirpath :
35+ yield Path (dirpath )
36+
37+
38+ @contextmanager
39+ def compile_mlir (filename : str ) -> Generator [Any , None , None ]:
40+ path = example_dir / filename
41+ assert path .exists ()
42+ with make_temp_directory () as dir :
43+ mlir_mod = compile_to_mlir (str (path ), be_type = CuTileBackend )
44+ yield mlir_mod
45+
46+
47+ def test_has_examples ():
48+ assert example_dir .exists ()
49+
50+ @pytest .mark .skipif (not HAS_CUDA_TILE , reason = "no cuda_tile" )
51+ def test_cuda_tile_to_mlir ():
52+ with compile_mlir ("tile_example.spy" ) as mlir_mod :
53+ mlir_text = mlir_mod .operation .get_asm ()
54+ assert "entry @spy_tile_example$exported$export_foo" in mlir_text
55+ assert "ntry @spy_tile_example$exported$export_vecadd" in mlir_text
0 commit comments