55import math
66import re
77import warnings
8+ import contextlib
89from dataclasses import dataclass
910import datetime
1011import functools
2728from cuda .tile ._const_utils import get_constant_annotations
2829from cuda .tile ._context import TileContextConfig
2930from cuda .tile ._exception import (
31+ Loc ,
3032 TileCompilerError ,
3133 TileCompilerExecutionError ,
3234 TileCompilerTimeoutError , TileValueError , TileTypeError
@@ -180,6 +182,22 @@ def _compiler_crash_dump(func_ir: ir.Function,
180182 z .writestr (filename , content )
181183
182184
185+ @contextlib .contextmanager
186+ def unique_path_from_loc (base_dir : str , loc : Loc , suffix : str , mode : str = "wb" ):
187+ prefix = []
188+ if loc .function is not None :
189+ if loc .function .name is not None :
190+ prefix .append (loc .function .name )
191+ else :
192+ prefix .append ("lambda" )
193+ prefix .append (Path (loc .function .filename ).stem )
194+ prefix .append (f"ln{ loc .function .line } " )
195+ prefix = "." .join (prefix ) + "."
196+ with tempfile .NamedTemporaryFile (suffix = suffix , prefix = prefix , dir = base_dir ,
197+ delete = False , mode = mode ) as f :
198+ yield f
199+
200+
183201@global_compiler_lock
184202def compile_tile (pyfunc ,
185203 args ,
@@ -212,11 +230,8 @@ def compile_tile(pyfunc,
212230 if CUDA_TILE_DUMP_BYTECODE is not None :
213231 if not os .path .isdir (CUDA_TILE_DUMP_BYTECODE ):
214232 os .makedirs (CUDA_TILE_DUMP_BYTECODE )
215- base_filename = os .path .basename (func_ir .loc .filename .split ("." )[0 ])
216- path = os .path .join (CUDA_TILE_DUMP_BYTECODE ,
217- f"{ base_filename } .ln{ func_ir .loc .line } .cutile" )
218- print (f"Dumping TILEIR bytecode to file: { path } " , file = sys .stderr )
219- with open (path , "wb" ) as f :
233+ with unique_path_from_loc (CUDA_TILE_DUMP_BYTECODE , func_ir .loc , '.tileirbc' ) as f :
234+ print (f"Dumping TILEIR bytecode to file: { f .name } " , file = sys .stderr )
220235 f .write (bytecode_buf )
221236
222237 # Write MLIR module to file
@@ -226,13 +241,9 @@ def compile_tile(pyfunc,
226241 mlir_text = bytecode_to_mlir_text (bytecode_buf )
227242 if not os .path .isdir (CUDA_TILE_DUMP_TILEIR ):
228243 os .makedirs (CUDA_TILE_DUMP_TILEIR )
229- base_filename = os .path .basename (func_ir .loc .filename .split ("." )[0 ])
230- path = os .path .join (
231- CUDA_TILE_DUMP_TILEIR , f"{ base_filename } .ln{ func_ir .loc .line } .cuda_tile.mlir"
232- )
233- print (f"Dumping TILEIR MLIR module to file:{ path } " , file = sys .stderr )
234- with open (path , "w" ) as f :
235- print (mlir_text , file = f )
244+ with unique_path_from_loc (CUDA_TILE_DUMP_TILEIR , func_ir .loc , '.tileir' , mode = "w" ) as f :
245+ print (f"Dumping TILEIR MLIR module to file: { f .name } " , file = sys .stderr )
246+ f .write (mlir_text )
236247 except ImportError :
237248 print ("Can't print MLIR because the internal extension is missing. "
238249 "This is currently not a public feature." , file = sys .stderr )
0 commit comments