def rewrite_cl(src, id='anon'): # Rewriter can't read from stdin. with NamedTemporaryFile('w', suffix='.cl') as tmp: tmp.write(src) tmp.flush() cmd = ([cfg.rewriter(), tmp.name] + ['-extra-arg=' + x for x in clang_cl_args()] + ['--']) process = Popen(cmd, stdin=PIPE, stdout=PIPE, stderr=PIPE, env=cfg.toolchain_env()) stdout, stderr = process.communicate() # If there was nothing to rewrite, rewriter exits with error code: EUGLY_CODE = 204 if process.returncode == EUGLY_CODE: # Propagate the error: raise RewriterException(src) # NOTE: the rewriter process can still fail because of some other # compilation problem, e.g. for some reason the 'enable 64bit # support' pragma which should be included in the shim isn't being # propogated correctly to the rewriter. However, the rewriter will # still correctly process the input, so we ignore all error codes # except the one we care about (EUGLY_CODE). rewritten = stdout.decode('utf-8') # Remove __attribute__ qualifiers stripped = clutil.strip_attributes(rewritten) return stripped
def compiler_preprocess_cl(src, id='anon'): cmd = [cfg.clang()] + clang_cl_args() + [ '-E', '-c', '-', '-o', '-' ] process = Popen(cmd, stdin=PIPE, stdout=PIPE, stderr=PIPE, env=cfg.toolchain_env()) stdout, stderr = process.communicate(src.encode('utf-8')) if process.returncode != 0: raise ClangException(stderr.decode('utf-8')) src = stdout.decode('utf-8') lines = src.split('\n') # Strip all the includes: for i, line in enumerate(lines): if line == '# 1 "<stdin>" 2': break src = '\n'.join(lines[i + 1:]).strip() # Strip lines beginning with '#' (that's preprocessor # stuff): src = '\n'.join([line for line in src.split('\n') if not line.startswith('#')]) return src
def compile_cl_bytecode(src, id='anon'): cmd = [cfg.clang()] + clang_cl_args() + [ '-emit-llvm', '-S', '-c', '-', '-o', '-' ] process = Popen(cmd, stdin=PIPE, stdout=PIPE, stderr=PIPE, env=cfg.toolchain_env()) stdout, stderr = process.communicate(src.encode('utf-8')) if process.returncode != 0: raise ClangException(stderr.decode('utf-8')) return stdout
def clangformat_ocl(src, id='anon'): clangformat = fs.path(cfg.llvm_path(), "build", "bin", "clang-format") cmd = [clangformat, '-style={}'.format(json.dumps(clangformat_config))] process = Popen(cmd, stdin=PIPE, stdout=PIPE, stderr=PIPE, env=cfg.toolchain_env()) stdout, stderr = process.communicate(src.encode('utf-8')) if stderr: print(stderr.decode('utf-8')) if process.returncode != 0: raise ClangFormatException(stderr.decode('utf-8')) return stdout.decode('utf-8')
def bytecode_features(bc, id='anon'): cmd = [cfg.opt(), '-analyze', '-stats', '-instcount', '-'] # LLVM pass output pritns to stderr, so we'll pipe stderr to # stdout. process = Popen(cmd, stdin=PIPE, stdout=PIPE, stderr=STDOUT, env=cfg.toolchain_env()) stdout, _ = process.communicate(bc) if process.returncode != 0: raise OptException(stdout.decode('utf-8')) instcounts = parse_instcounts(stdout.decode('utf-8')) instratios = instcounts2ratios(instcounts) return instratios