mirror of
https://github.com/tinygrad/tinygrad.git
synced 2026-04-29 03:00:14 -04:00
objdump intel syntax (#7605)
* objdump intel syntax * test for objdump intel syntax * add disassemble to ClangCompiler and LLVMCompiler. Use just llvm-objdump * linter
This commit is contained in:
16
test/test_objdump.py
Normal file
16
test/test_objdump.py
Normal file
@@ -0,0 +1,16 @@
|
||||
from tinygrad import Device
|
||||
from tinygrad.runtime.ops_clang import ClangCompiler
|
||||
import io, contextlib, unittest
|
||||
|
||||
@unittest.skipUnless(Device.DEFAULT == "CLANG", "compile with clang")
|
||||
class TestCpuObjDump(unittest.TestCase):
|
||||
def test_intel_syntax(self):
|
||||
src = "int main() { return 0; }"
|
||||
lib = ClangCompiler().compile(src)
|
||||
out = io.StringIO()
|
||||
with contextlib.redirect_stdout(out):
|
||||
ClangCompiler().disassemble(lib)
|
||||
assert "%" not in out.getvalue(), "objdump disassembly should be intel syntax"
|
||||
|
||||
if __name__ == '__main__':
|
||||
unittest.main()
|
||||
@@ -259,10 +259,12 @@ def cpu_time_execution(cb, enable):
|
||||
cb()
|
||||
if enable: return time.perf_counter()-st
|
||||
|
||||
def cpu_objdump(lib, objdump_tool='objdump'):
|
||||
with tempfile.NamedTemporaryFile(delete=True) as f:
|
||||
pathlib.Path(f.name).write_bytes(lib)
|
||||
print(subprocess.check_output([objdump_tool, '-d', f.name]).decode('utf-8'))
|
||||
def cpu_objdump(lib):
|
||||
try:
|
||||
with tempfile.NamedTemporaryFile(delete=True) as f:
|
||||
pathlib.Path(f.name).write_bytes(lib)
|
||||
print(subprocess.check_output(['llvm-objdump', '-d', '-M', 'intel', f.name]).decode('utf-8'))
|
||||
except Exception as e: print(str(e))
|
||||
|
||||
# *** ctypes helpers
|
||||
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
from typing import Optional, List
|
||||
import ctypes, subprocess, pathlib, tempfile
|
||||
from tinygrad.device import Compiled, Compiler, MallocAllocator
|
||||
from tinygrad.helpers import cpu_time_execution, DEBUG, cpu_objdump
|
||||
from tinygrad.helpers import cpu_time_execution, cpu_objdump
|
||||
from tinygrad.renderer.cstyle import ClangRenderer
|
||||
|
||||
class ClangCompiler(Compiler):
|
||||
@@ -16,9 +16,10 @@ class ClangCompiler(Compiler):
|
||||
'-', '-o', str(output_file.name)], input=src.encode('utf-8'))
|
||||
return pathlib.Path(output_file.name).read_bytes()
|
||||
|
||||
def disassemble(self, lib:bytes): cpu_objdump(lib)
|
||||
|
||||
class ClangProgram:
|
||||
def __init__(self, name:str, lib:bytes):
|
||||
if DEBUG >= 6: cpu_objdump(lib)
|
||||
self.name, self.lib = name, lib
|
||||
# write to disk so we can load it
|
||||
with tempfile.NamedTemporaryFile(delete=True) as cached_file_path:
|
||||
|
||||
@@ -3,7 +3,7 @@ from typing import Tuple, Any
|
||||
import ctypes, os, mmap, tempfile, pathlib, array, functools, threading, contextlib, sys
|
||||
assert sys.platform != 'win32'
|
||||
from tinygrad.device import BufferOptions, Compiled, Allocator
|
||||
from tinygrad.helpers import from_mv, getenv, DEBUG, round_up, mv_address, to_mv, cpu_objdump
|
||||
from tinygrad.helpers import from_mv, getenv, round_up, mv_address, to_mv
|
||||
from tinygrad.runtime.ops_clang import ClangCompiler
|
||||
from tinygrad.renderer.cstyle import DSPRenderer
|
||||
from tinygrad.runtime.autogen import libc, qcom_dsp
|
||||
@@ -23,7 +23,6 @@ def rpc_prep_args(ins=None, outs=None, in_fds=None):
|
||||
class DSPProgram:
|
||||
def __init__(self, device:DSPDevice, name:str, lib:bytes):
|
||||
self.device, self.lib = device, lib
|
||||
if DEBUG >= 6: cpu_objdump(lib, objdump_tool='llvm-objdump')
|
||||
|
||||
def __call__(self, *bufs, vals:Tuple[int, ...]=(), wait=False):
|
||||
if len(bufs) >= 16: raise RuntimeError(f"Too many buffers to execute: {len(bufs)}")
|
||||
|
||||
@@ -22,10 +22,10 @@ class LLVMCompiler(Compiler):
|
||||
self.optimizer.run(mod)
|
||||
if DEBUG >= 5: print(self.device.target_machine.emit_assembly(mod))
|
||||
return self.device.target_machine.emit_object(mod)
|
||||
def disassemble(self, lib: bytes): cpu_objdump(lib)
|
||||
|
||||
class LLVMProgram:
|
||||
def __init__(self, device:LLVMDevice, name:str, lib:bytes):
|
||||
if DEBUG >= 6: cpu_objdump(lib)
|
||||
self.name, self.lib = name, lib
|
||||
device.engine.add_object_file(llvm.object_file.ObjectFileRef.from_data(lib))
|
||||
self.fxn = device.engine.get_function_address(name)
|
||||
|
||||
Reference in New Issue
Block a user