mirror of
https://github.com/tinygrad/tinygrad.git
synced 2026-09-01 06:26:07 +00:00
add restrict qualifier to inputs in c backend (#593)
* add restrict qualifier for clang backend convolution inputs/ outputs see https://godbolt.org/z/Tb9jMxWfx for generated assembly * enable more checks * inline fmax to motivate the compiler to inline some more * fix if else binding power
This commit is contained in:
@@ -5,7 +5,7 @@ import ast
|
||||
|
||||
def compile_net(run, special_names):
|
||||
# c header
|
||||
cprog = ["#include <stdio.h>", "#include <math.h>","#define max(x,y) fmax(x,y)"]
|
||||
cprog = ["#include <stdio.h>", "#include <math.h>", "#define max(x,y) ((x>y)?x:y)"]
|
||||
|
||||
# functions that run the net
|
||||
bufs = {}
|
||||
|
||||
@@ -324,7 +324,7 @@ class CLASTKernel(ASTKernel):
|
||||
|
||||
# kernel function definition
|
||||
function_name = ("re_S" if self.reduceop else "ew_S") + '_'.join([str(x) for x in self.bufs[0].shape if x != 1])
|
||||
buftypes = [f"{'read_only' if i > 0 else 'write_only'} image2d_t" if isinstance(x._buf, CLImage) else (CLProgram.buffer_prefix+self.buftokens[i].decltype()) for i,x in enumerate(self.bufs)]
|
||||
buftypes = [f"{'read_only' if i > 0 else 'write_only'} image2d_t" if isinstance(x._buf, CLImage) else (CLProgram.buffer_prefix+self.buftokens[i].decltype() + ("restrict" if CLANG else "")) for i,x in enumerate(self.bufs)]
|
||||
self.kernel = list(self.prekernel) + [f"{CLProgram.kernel_prefix} void {function_name}(",] + \
|
||||
[', '.join([f'{t} data{i}' for i,t in enumerate(buftypes) if i not in self.bufs_to_delete] + CLProgram.extra_args)] + \
|
||||
[") {\n"] + self.kernel
|
||||
|
||||
@@ -26,12 +26,12 @@ class CLProgram:
|
||||
self.name = f"{name}{('_N'+str(CLProgram.kernel_cnt[name])) if CLProgram.kernel_cnt[name] else str()}" if rename else name
|
||||
CLProgram.kernel_cnt[name] += 1
|
||||
self.prg = prg.replace(f"{name}(", f"{self.name}(")
|
||||
prg = "#include <math.h>\n#define max(x,y) fmax(x,y)\n" + prg
|
||||
prg = "#include <math.h>\n#define max(x,y) ((x>y)?x:y)\n" + prg
|
||||
if DEBUG >= 4: print(prg) # TODO: outside runtime!
|
||||
# TODO: is there a way to not write this to disk?
|
||||
fn = f"/tmp/clang_{hashlib.md5(prg.encode('utf-8')).hexdigest()}.{'dylib' if OSX else 'so'}"
|
||||
if not os.path.exists(fn):
|
||||
subprocess.check_output(['clang', '-shared', '-O2', '-lm', '-fPIC', '-x', 'c', '-', '-o', fn+".tmp"], input=prg.encode('utf-8'))
|
||||
subprocess.check_output(['clang', '-shared', '-O2', '-Wall','-Werror', '-lm', '-fPIC', '-x', 'c', '-', '-o', fn+".tmp"], input=prg.encode('utf-8'))
|
||||
os.rename(fn+".tmp", fn)
|
||||
self.lib = ctypes.CDLL(fn)
|
||||
self.fxn = self.lib[name]
|
||||
|
||||
Reference in New Issue
Block a user