Skip to content

Commit bfe97e4

Browse files
authored
Make native code generation through ocloc more friendly for Windows (#4656)
The main idea is to get rid of the creation of temporary files where possible, and where not - use a temporary folder that will be automatically deleted when exit the context. --------- Signed-off-by: Anatoly Myachev <[email protected]>
1 parent 5a42f93 commit bfe97e4

File tree

1 file changed

+15
-34
lines changed

1 file changed

+15
-34
lines changed

third_party/intel/backend/compiler.py

Lines changed: 15 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -408,10 +408,9 @@ def make_spv(src, metadata, options, device_arch):
408408
metadata["generate_native_code"] = options.generate_native_code
409409

410410
if options.generate_native_code:
411-
with tempfile.NamedTemporaryFile(delete=False, mode='wb', suffix='.spv') as fsrc, \
412-
tempfile.NamedTemporaryFile(delete=False, mode='r', suffix='.log') as flog:
413-
fsrc.write(spirv)
414-
fsrc.flush()
411+
with tempfile.TemporaryDirectory() as temp_dir:
412+
with tempfile.NamedTemporaryFile(mode='wb', suffix='.spv', dir=temp_dir, delete=False) as fsrc:
413+
fsrc.write(spirv)
415414
fbin = fsrc.name + '.o'
416415

417416
ocloc_cmd = [
@@ -420,34 +419,18 @@ def make_spv(src, metadata, options, device_arch):
420419
]
421420

422421
try:
423-
subprocess.run(ocloc_cmd, check=True, close_fds=False, stdout=flog, stderr=subprocess.STDOUT)
424-
if os.path.exists(flog.name):
425-
with open(flog.name) as log_file:
426-
log = log_file.read().strip()
427-
if 'spilled' in log and metadata["build_flags"].find("-cl-intel-256-GRF-per-thread") == -1:
428-
"""
429-
The exact message is something like:
430-
warning: kernel matmul_kernel compiled SIMD16 allocated 128 regs and spilled around 217
431-
is "spilled" enough for now?
432-
"""
433-
metadata["build_flags"] += " -cl-intel-256-GRF-per-thread"
434-
# re-run with new build flags
435-
ocloc_cmd[-1] = metadata["build_flags"] + shader_dump_opt
436-
subprocess.run(ocloc_cmd, check=True, close_fds=False, stdout=flog,
437-
stderr=subprocess.STDOUT)
438-
if os.name != "nt":
439-
# Skip deleting on Windows to avoid
440-
# PermissionError: [WinError 32] The process cannot access the file because
441-
# it is being used by another process
442-
os.remove(flog.name)
443-
if os.path.exists(fsrc.name) and os.name != "nt":
444-
os.remove(fsrc.name)
422+
output = subprocess.check_output(ocloc_cmd, stderr=subprocess.STDOUT, text=True)
423+
if 'spilled' in output and metadata["build_flags"].find("-cl-intel-256-GRF-per-thread") == -1:
424+
"""
425+
The exact message is something like:
426+
warning: kernel matmul_kernel compiled SIMD16 allocated 128 regs and spilled around 217
427+
is "spilled" enough for now?
428+
"""
429+
metadata["build_flags"] += " -cl-intel-256-GRF-per-thread"
430+
# re-run with new build flags
431+
ocloc_cmd[-1] = metadata["build_flags"] + shader_dump_opt
432+
subprocess.check_output(ocloc_cmd, stderr=subprocess.STDOUT, text=True)
445433
except subprocess.CalledProcessError as e:
446-
with open(flog.name) as log_file:
447-
log = log_file.read()
448-
if os.path.exists(flog.name) and os.name != "nt":
449-
os.remove(flog.name)
450-
451434
if e.returncode == 255:
452435
error = 'Internal Triton ZEBIN codegen error'
453436
elif e.returncode == 128 + signal.SIGSEGV:
@@ -456,13 +439,11 @@ def make_spv(src, metadata, options, device_arch):
456439
error = f'`ocloc` failed with error code {e.returncode}'
457440

458441
raise RuntimeError(f'{error}\n'
459-
f'`ocloc` stderr:\n{log}\n'
442+
f'`ocloc` stderr:\n{e.output}\n'
460443
f'Repro command: {ocloc_cmd}\n') from e
461444

462445
with open(fbin, 'rb') as f:
463446
zebin = f.read()
464-
if os.path.exists(fbin) and os.name != "nt":
465-
os.remove(fbin)
466447
return zebin
467448
return spirv
468449

0 commit comments

Comments
 (0)