def _PreprocessKernel(src: str) -> str: """Format a kernel for use and check that it meets requirements.""" src = opencl.StripDoubleUnderscorePrefixes(src.strip()) if not src.startswith("kernel void "): raise ValueError("Invalid kernel") # Strip trailing whitespace, and exclude blank lines. return "\n".join(ln.rstrip() for ln in src.split("\n") if ln.rstrip())
def preprocessor_worker(contentfile_batch): kernel_batch = [] p, cf = contentfile_batch ks = opencl.ExtractSingleKernelsHeaders( opencl.InvertKernelSpecifier( opencl.StripDoubleUnderscorePrefixes( opencl.ClangPreprocessWithShim(c.StripIncludes(cf))))) for k, h in ks: kernel_batch.append((p, k, h)) return kernel_batch
def test_StripDoubleUnderscorePrefixes_simple_kernel(): assert ( opencl.StripDoubleUnderscorePrefixes( """ __kernel void A(__global int* a) { __private int b; } """ ) == """ kernel void A(global int* a) { private int b; } """ )
def test_StripDoubleUnderscorePrefixes_empty_input(): assert opencl.StripDoubleUnderscorePrefixes('') == ''
def execute_clsmith(idx: int, tokenizer, timeout_seconds: int = 15) -> typing.List[CLSmithSample]: """ Execute clsmith and return sample. """ try: tdir = pathlib.Path(FLAGS.local_filesystem).resolve() except Exception: tdir = None extra_args = ["-include{}".format(pathlib.Path(CLSMITH_INCLUDE) / "CLSmith.h")] with tempfile.NamedTemporaryFile("w", prefix = "clsmith_", suffix = ".cl", dir = tdir) as f: cmd =[ "timeout", "-s9", str(timeout_seconds), CLSMITH, "-o", str(f.name) ] process = subprocess.Popen( cmd, stdout = subprocess.PIPE, stderr = subprocess.PIPE, universal_newlines = True, ) try: stdout, stderr = process.communicate() except TimeoutError: return None contentfile = open(str(f.name), 'r').read() try: ks = opencl.ExtractSingleKernelsHeaders( opencl.StripDoubleUnderscorePrefixes( c.StripIncludes(contentfile), ) ) except ValueError as e: l.logger().error(contentfile) raise e samples = [] for kernel, include in ks: encoded_sample = tokenizer.AtomizeString(kernel) try: stdout = opencl.Compile(kernel, header_file = include, extra_args = extra_args) compile_status = True except ValueError as e: stdout = str(e) compile_status = False samples.append( CLSmithSample.FromArgs( id = idx, sample = stdout, include = include, encoded_sample = ','.join(encoded_sample), compile_status = compile_status, feature_vector = extractor.ExtractRawFeatures(kernel, header_file = include, extra_args = extra_args), num_tokens = len(encoded_sample) ) ) return samples