def visit_BinaryOp(self, node): if isinstance(node.op, C.Op.Assign): check = [ util.contains_symbol(node.right, var) for var in list(self.unrolled_vars) + [self.target_var] ] if any(check): body = [] if hasattr(node.left, 'type') and node.left.type is not None: self.unrolled_vars.add(node.left.name) for i in range(self.factor): stmt = deepcopy(node) for var in self.unrolled_vars: stmt = util.replace_symbol( var, C.SymbolRef(var + "_" + str(i)), stmt) if self.unroll_type == 0: body.append( util.replace_symbol( self.target_var, C.Add(C.SymbolRef(self.target_var), C.Constant(i)), stmt)) elif self.unroll_type == 1: body.append( util.replace_symbol( self.target_var, C.Add( C.Mul(C.Constant(self.factor), C.SymbolRef(self.target_var)), C.Constant(i)), stmt)) else: assert (false) return body return node
def visit_If(self, node): check = [ util.contains_symbol(node, var) for var in list(self.unrolled_vars) + [self.target_var] ] if any(check): body = [] for i in range(self.factor): stmt = deepcopy(node) for var in self.unrolled_vars: stmt = util.replace_symbol(var, C.SymbolRef(var + "_" + str(i)), stmt) if self.unroll_type == 0: body.append( util.replace_symbol( self.target_var, C.Add(C.SymbolRef(self.target_var), C.Constant(i)), stmt)) elif self.unroll_type == 1: body.append( util.replace_symbol( self.target_var, C.Add( C.Mul(C.Constant(self.factor), C.SymbolRef(self.target_var)), C.Constant(i)), stmt)) else: assert (false) return body return node
def visit_BinaryOp(self, node): if isinstance(node.op, C.Op.Assign): if not (isinstance(node.op ,C.Op.Assign) and isinstance(node.left, C.SymbolRef) and (node.left.type is not None) and node.left.name == self.old)\ and not(isinstance(node.op ,C.Op.Assign) and isinstance(node.left, C.SymbolRef) and (node.left.type is not None) and node.left.name == self.new.name): #if not isinstance(node.left, C.SymbolRef): # util.print_ast(node.left) node.left = util.replace_symbol(self.old, self.new, node.left) node.right = util.replace_symbol(self.old, self.new, node.right) else: node = util.replace_symbol(self.old, self.new, node) return node
def visit_FunctionCall(self, node): if "_store" in node.func.name: node.args = [ util.replace_symbol(self.old, self.new, arg) for arg in node.args ] return node
def visit_For(self, node): node.body = util.flatten([self.visit(s) for s in node.body]) #TODO: assumption is that every loop starts with zero, not negative init = -1 incr = -1 test = -1 if isinstance(node.init, C.BinaryOp) and \ isinstance(node.init.op, C.Op.Assign) and \ isinstance(node.init.left, C.SymbolRef) and \ isinstance(node.init.right, C.Constant): init = node.init.right.value if isinstance(node.test, C.BinaryOp) and \ isinstance(node.test.op, C.Op.Lt) and \ isinstance(node.test.left, C.SymbolRef) and \ isinstance(node.test.right, C.Constant): test = node.test.right.value if isinstance(node.incr, C.AugAssign) and \ isinstance(node.incr.op, C.Op.Add) and \ isinstance(node.incr.target, C.SymbolRef) and \ isinstance(node.incr.value, C.Constant): incr = node.incr.value.value if init != -1 and test != -1 and incr != -1 and (init+incr) >= test: return [util.replace_symbol(node.init.left.name, C.Constant(init), s) for s in node.body] return node
def visit_For(self, node): node.body = [self.visit(s) for s in node.body] if node.init.left.name == self.target_var: node.incr = C.AddAssign(C.SymbolRef(self.target_var), C.Constant(self.factor)) visitor = UnrollStatements(self.target_var, self.factor) node.body = util.flatten([visitor.visit(s) for s in node.body]) if node.test.right.value == self.factor: return [ util.replace_symbol(node.init.left.name, C.Constant(0), s) for s in node.body ] return node
def visit(self, node): node = super().visit(node) if hasattr(node, 'body'): new_body = [] seen = set() for i, stmt in enumerate(node.body): if isinstance(stmt, C.BinaryOp) and isinstance(stmt.op, C.Op.Assign) and \ (isinstance(stmt.left.type, simd.types.m256) or stmt.left.name in seen): seen.add(stmt.left.name) value = stmt.left.name counter = 0 for _stmt in node.body[i + 1:]: counter += util.count_symbol_instances(_stmt, value) if counter > 1: new_body.append(stmt) else: for i in range(i + 1, len(node.body)): node.body[i] = util.replace_symbol( value, stmt.right, node.body[i]) else: new_body.append(stmt) node.body = new_body return node
def visit_For(self, node): for j in range(1, self.factor): UnrollStatementsNoJam.new_body[j] = [] # UnrollStatementsNoJam.new_body={} #for i in node.body: #new_body_cpy = deepcopy(UnrollStatementsNoJam.new_body) #node.body = [self.visit(s) for s in node.body] newbody = [] for s in node.body: temp = deepcopy(UnrollStatementsNoJam.new_body) t = self.visit(s) stmt2 = deepcopy(t) stmt = deepcopy(t) if self.unroll_type == 0: s = util.replace_symbol( self.target_var, C.Add(C.SymbolRef(self.target_var), C.Constant(0)), stmt) else: s = util.replace_symbol( self.target_var, C.Add( C.Mul(C.Constant(self.factor), C.SymbolRef(self.target_var)), C.Constant(0)), stmt) newbody.append(t) if not isinstance(t, C.For): for i in range(1, self.factor): stmt = deepcopy(stmt2) if self.unroll_type == 0: if i in UnrollStatementsNoJam.new_body: UnrollStatementsNoJam.new_body[i].append( util.replace_symbol( self.target_var, C.Add(C.SymbolRef(self.target_var), C.Constant(i)), stmt)) else: UnrollStatementsNoJam.new_body[i] = [ util.replace_symbol( self.target_var, C.Add(C.SymbolRef(self.target_var), C.Constant(i)), stmt) ] elif self.unroll_type == 1: if i in UnrollStatementsNoJam.new_body: UnrollStatementsNoJam.new_body[i].append( util.replace_symbol( self.target_var, C.Add( C.Mul(C.Constant(self.factor), C.SymbolRef(self.target_var)), C.Constant(i)), stmt)) else: UnrollStatementsNoJam.new_body[i] = [ util.replace_symbol( self.target_var, C.Add( C.Mul(C.Constant(self.factor), C.SymbolRef(self.target_var)), C.Constant(i)), stmt) ] else: assert (false) else: var = t.init.left.name #if var != self.target_var: for j in range(1, self.factor): temp[j].append( C.For( C.Assign(C.SymbolRef(var, ctypes.c_int()), C.Constant(0)), C.Lt(C.SymbolRef(var), C.Constant(t.test.right.value)), C.AddAssign(C.SymbolRef(var), C.Constant(t.incr.value.value)), UnrollStatementsNoJam.new_body[j])) UnrollStatementsNoJam.new_body = deepcopy(temp) node.body = newbody return node
def visit_AugAssign(self, node): check = [ util.contains_symbol(node.value, var) for var in list(self.unrolled_vars) + [self.target_var] ] if any(check): body = [] if isinstance(node.target, C.SymbolRef): self.unrolled_vars.add(self._get_name(node.target.name)) for i in range(self.factor): stmt = deepcopy(node) for var in self.unrolled_vars: stmt = util.replace_symbol( var, C.SymbolRef(var + "_" + str(i)), stmt) #body.append(util.replace_symbol(self.target_var, C.Add(C.SymbolRef(self.target_var), C.Constant(i)), stmt)) if self.unroll_type == 0: body.append( util.replace_symbol( self.target_var, C.Add(C.SymbolRef(self.target_var), C.Constant(i)), stmt)) elif self.unroll_type == 1: body.append( util.replace_symbol( self.target_var, C.Add( C.Mul(C.Constant(self.factor), C.SymbolRef(self.target_var)), C.Constant(i)), stmt)) else: assert (false) return body elif isinstance(node.target, C.BinaryOp) and isinstance( node.target.op, C.Op.ArrayRef): assert False for i in range(self.factor): stmt = deepcopy(node) for var in self.unrolled_vars: stmt = util.replace_symbol( var, C.SymbolRef(var + "_" + str(i)), stmt) #body.append(util.replace_symbol(self.target_var, C.Add(C.SymbolRef(self.target_var), C.Constant(i)), stmt)) if self.unroll_type == 0: body.append( util.replace_symbol( self.target_var, C.Add(C.SymbolRef(self.target_var), C.Constant(i)), stmt)) elif self.unroll_type == 1: body.append( util.replace_symbol( self.target_var, C.Add( C.Mul(C.Constant(self.factor), C.SymbolRef(self.target_var)), C.Constant(i)), stmt)) else: assert (false) return body else: raise NotImplementedError() return node