예제 #1
0
    def map_if(self, expr, base_condition, base_deps, extra_deps):
        from pymbolic.primitives import LogicalNot
        from pymbolic import var

        flag = var(self.var_name_gen("<cond>ifthenelse_cond"))
        tmp_result = self.var_name_gen("ifthenelse_result")
        if_stmt_id = self.stmt_id_gen("ifthenelse_cond")
        then_stmt_id = self.stmt_id_gen("ifthenelse_then")
        else_stmt_id = self.stmt_id_gen("ifthenelse_else")

        sub_condition_deps = []
        rec_condition = self.rec(expr.condition, base_condition, base_deps,
                                 sub_condition_deps)

        sub_then_deps = []
        then_condition = flat_LogicalAnd(base_condition, flag)
        rec_then = self.rec(expr.then, then_condition,
                            base_deps | frozenset([if_stmt_id]), sub_then_deps)

        sub_else_deps = []
        else_condition = flat_LogicalAnd(base_condition, LogicalNot(flag))
        rec_else = self.rec(expr.else_, else_condition,
                            base_deps | frozenset([if_stmt_id]), sub_else_deps)

        from dagrt.language import Assign

        self.new_statements.extend([
            Assign(
                assignee=flag.name,
                assignee_subscript=(),
                expression=rec_condition,
                condition=base_condition,
                id=if_stmt_id,
                depends_on=base_deps | frozenset(sub_condition_deps)),
            Assign(
                assignee=tmp_result,
                assignee_subscript=(),
                condition=then_condition,
                expression=rec_then,
                id=then_stmt_id,
                depends_on=(
                    base_deps
                    | frozenset(sub_then_deps)
                    | frozenset([if_stmt_id]))),
            Assign(
                assignee=tmp_result,
                assignee_subscript=(),
                condition=else_condition,
                expression=rec_else,
                id=else_stmt_id,
                depends_on=(
                    base_deps
                    | frozenset(sub_else_deps)
                    | frozenset([if_stmt_id])))
                ])

        extra_deps.extend([then_stmt_id, else_stmt_id])
        return var(tmp_result)
예제 #2
0
    def construct_else_condition(self):
        context_cond, prev_cond = self.conditions_data.pop()
        if prev_cond is None:
            raise RuntimeError("else if may not follow else")

        self.conditions.pop()

        from pymbolic.primitives import LogicalNot, LogicalAnd
        else_expr = LogicalNot(prev_cond)
        if context_cond is not None:
            else_expr = LogicalAnd((else_expr, context_cond))

        return else_expr
예제 #3
0
파일: language.py 프로젝트: inducer/dagrt
    def else_(self):
        """
        Create the "else" portion of a conditionally executed block.
        """
        assert self._last_if_block_conditional_expression is not None

        # Create conditions for the context.
        from pymbolic.primitives import LogicalNot
        self._conditional_expression_stack.append(
            LogicalNot(self._last_if_block_conditional_expression))
        yield
        self._conditional_expression_stack.pop()
        self._last_if_block_conditional_expression = None
예제 #4
0
    def parse_prefix(self, pstate):
        import pymbolic.primitives as primitives
        pstate.expect_not_end()

        if pstate.is_next(_colon):
            pstate.advance()

            expr_pstate = pstate.copy()
            from pytools.lex import ParseError
            try:
                next_expr = self.parse_expression(expr_pstate, _PREC_SLICE)
            except ParseError:
                # no expression follows, too bad.
                left_exp = primitives.Slice((None, ))
            else:
                left_exp = _join_to_slice(None, next_expr)
                pstate.assign(expr_pstate)
        elif pstate.is_next(_times):
            pstate.advance()
            left_exp = primitives.Wildcard()
        elif pstate.is_next(_plus):
            pstate.advance()
            left_exp = self.parse_expression(pstate, _PREC_UNARY)
        elif pstate.is_next(_minus):
            pstate.advance()
            left_exp = -self.parse_expression(pstate, _PREC_UNARY)
        elif pstate.is_next(_not):
            pstate.advance()
            from pymbolic.primitives import LogicalNot
            left_exp = LogicalNot(self.parse_expression(pstate, _PREC_UNARY))
        elif pstate.is_next(_bitwisenot):
            pstate.advance()
            from pymbolic.primitives import BitwiseNot
            left_exp = BitwiseNot(self.parse_expression(pstate, _PREC_UNARY))
        elif pstate.is_next(_openpar):
            pstate.advance()
            left_exp = self.parse_expression(pstate)
            pstate.expect(_closepar)
            pstate.advance()
            if isinstance(left_exp, tuple):
                left_exp = FinalizedTuple(left_exp)
        else:
            left_exp = self.parse_terminal(pstate)

        return left_exp
예제 #5
0
파일: instruction.py 프로젝트: mmmika/loopy
    def __init__(self,
                 id,
                 depends_on,
                 depends_on_is_final,
                 groups,
                 conflicts_with_groups,
                 no_sync_with,
                 within_inames_is_final,
                 within_inames,
                 priority,
                 boostable,
                 boostable_into,
                 predicates,
                 tags,
                 insn_deps=None,
                 insn_deps_is_final=None,
                 forced_iname_deps=None,
                 forced_iname_deps_is_final=None):

        # {{{ backwards compatibility goop

        if depends_on is not None and insn_deps is not None:
            raise LoopyError("may not specify both insn_deps and depends_on")
        elif insn_deps is not None:
            warn("insn_deps is deprecated, use depends_on",
                 DeprecationWarning,
                 stacklevel=2)

            depends_on = insn_deps
            depends_on_is_final = insn_deps_is_final

        if forced_iname_deps is not None and within_inames is not None:
            raise LoopyError("may not specify both forced_iname_deps "
                             "and within_inames")
        elif forced_iname_deps is not None:
            warn("forced_iname_deps is deprecated, use within_inames",
                 DeprecationWarning,
                 stacklevel=2)

            within_inames = forced_iname_deps
            within_inames_is_final = forced_iname_deps_is_final

        if predicates is None:
            predicates = frozenset()

        new_predicates = set()
        for pred in predicates:
            if isinstance(pred, str):
                from pymbolic.primitives import LogicalNot
                from loopy.symbolic import parse
                if pred.startswith("!"):
                    warn("predicates starting with '!' are deprecated. "
                         "Simply use 'not' instead")
                    pred = LogicalNot(parse(pred[1:]))
                else:
                    pred = parse(pred)

            new_predicates.add(pred)

        predicates = frozenset(new_predicates)
        del new_predicates

        # }}}

        if depends_on is None:
            depends_on = frozenset()

        if groups is None:
            groups = frozenset()

        if conflicts_with_groups is None:
            conflicts_with_groups = frozenset()

        if no_sync_with is None:
            no_sync_with = frozenset()

        if within_inames is None:
            within_inames = frozenset()

        if within_inames_is_final is None:
            within_inames_is_final = False

        if isinstance(depends_on, str):
            depends_on = frozenset(s.strip() for s in depends_on.split(",")
                                   if s.strip())

        if depends_on_is_final is None:
            depends_on_is_final = False

        if depends_on_is_final and not isinstance(depends_on, frozenset):
            raise LoopyError("Setting depends_on_is_final to True requires "
                             "actually specifying depends_on")

        if tags is None:
            tags = frozenset()

        if priority is None:
            priority = 0

        if not isinstance(tags, frozenset):
            # was previously allowed to be tuple
            tags = frozenset(tags)

        # Periodically reenable these and run the tests to ensure all
        # performance-relevant identifiers are interned.
        #
        # from loopy.tools import is_interned
        # assert is_interned(id)
        # assert all(is_interned(dep) for dep in depends_on)
        # assert all(is_interned(grp) for grp in groups)
        # assert all(is_interned(grp) for grp in conflicts_with_groups)
        # assert all(is_interned(iname) for iname in within_inames)
        # assert all(is_interned(pred) for pred in predicates)

        assert isinstance(within_inames, frozenset)
        assert isinstance(depends_on, frozenset) or depends_on is None
        assert isinstance(groups, frozenset)
        assert isinstance(conflicts_with_groups, frozenset)

        ImmutableRecord.__init__(self,
                                 id=id,
                                 depends_on=depends_on,
                                 depends_on_is_final=depends_on_is_final,
                                 no_sync_with=no_sync_with,
                                 groups=groups,
                                 conflicts_with_groups=conflicts_with_groups,
                                 within_inames_is_final=within_inames_is_final,
                                 within_inames=within_inames,
                                 priority=priority,
                                 boostable=boostable,
                                 boostable_into=boostable_into,
                                 predicates=predicates,
                                 tags=tags)
예제 #6
0
 def map_logical_not(self, expr, *args):
     from pymbolic.primitives import LogicalNot
     return LogicalNot(self.rec(expr.child, *args))
예제 #7
0
    nop = ComparableNop(condition=x, id="nop", depends_on=())
    code = create_DAGCode_with_steady_phase([nop])
    ast = create_ast_from_phase(code, "main")
    assert ast == IfThen(x, StatementWrapper(nop.copy(condition=True)))


_p = var("<cond>p")


@pytest.mark.parametrize(
    "in_ast, out_ast",
    [(Block(Block(0)), 0),
     (Block(IfThen(_p, 0), IfThen(_p, 1), IfThen(
         _p, 2)), IfThen(_p, Block(0, 1, 2))),
     (Block(IfThen(_p, 0), IfThen(LogicalNot(_p), 1)), IfThenElse(_p, 0, 1)),
     (Block(IfThen(_p, 0), IfThenElse(_p, 1, 2)), IfThenElse(
         _p, Block(0, 1), 2)),
     (Block(IfThenElse(_p, 0, 1), IfThen(LogicalNot(_p),
                                         2)), IfThenElse(_p, 0, Block(1, 2))),
     (IfThenElse(_p, IfThen(LogicalNot(_p), 1), 2), IfThen(LogicalNot(_p), 2)),
     (Block(IfThenElse(_p, 1, 2), IfThenElse(
         _p, 3, 4)), IfThenElse(_p, Block(1, 3), Block(2, 4))),
     (Block(IfThenElse(LogicalNot(_p), 1, 2), IfThenElse(
         _p, 3, 4)), IfThenElse(_p, Block(2, 3), Block(1, 4))),
     (IfThen(_p, IfThen(_p, 1)), IfThen(_p, 1)),
     (IfThen(LogicalNot(_p), IfThen(LogicalNot(_p),
                                    1)), IfThen(LogicalNot(_p), 1)),
     (IfThen(_p, IfThen(LogicalNot(_p), 1)), Block()),
     (IfThen(LogicalNot(_p), IfThen(_p, 1)), Block()),
     (IfThenElse(_p, IfThen(_p, 1), 2), IfThenElse(_p, 1, 2)),
예제 #8
0
파일: parser.py 프로젝트: redrossa/pymbolic
    def parse_prefix(self, pstate):
        import pymbolic.primitives as primitives
        pstate.expect_not_end()

        if pstate.is_next(_colon):
            pstate.advance()

            expr_pstate = pstate.copy()
            from pytools.lex import ParseError
            try:
                next_expr = self.parse_expression(expr_pstate, _PREC_SLICE)
            except ParseError:
                # no expression follows, too bad.
                left_exp = primitives.Slice((None,))
            else:
                left_exp = _join_to_slice(None, next_expr)
                pstate.assign(expr_pstate)
        elif pstate.is_next(_times):
            pstate.advance()
            left_exp = primitives.Wildcard()
        elif pstate.is_next(_plus):
            pstate.advance()
            left_exp = self.parse_expression(pstate, _PREC_UNARY)
        elif pstate.is_next(_minus):
            pstate.advance()
            left_exp = -self.parse_expression(pstate, _PREC_UNARY)  # noqa pylint:disable=invalid-unary-operand-type
        elif pstate.is_next(_not):
            pstate.advance()
            from pymbolic.primitives import LogicalNot
            left_exp = LogicalNot(
                    self.parse_expression(pstate, _PREC_UNARY))
        elif pstate.is_next(_bitwisenot):
            pstate.advance()
            from pymbolic.primitives import BitwiseNot
            left_exp = BitwiseNot(
                    self.parse_expression(pstate, _PREC_UNARY))
        elif pstate.is_next(_openpar):
            pstate.advance()

            if pstate.is_next(_closepar):
                left_exp = ()
            else:
                # This is parsing expressions separated by commas, so it
                # will return a tuple. Kind of the lazy way out.
                left_exp = self.parse_expression(pstate)

            pstate.expect(_closepar)
            pstate.advance()
            if isinstance(left_exp, tuple):
                # These could just be plain parentheses.

                # Finalization prevents things from being appended
                # to containers after their closing delimiter.
                left_exp = FinalizedTuple(left_exp)
        elif pstate.is_next(_openbracket):
            pstate.advance()

            if pstate.is_next(_closebracket):
                left_exp = ()
            else:
                # This is parsing expressions separated by commas, so it
                # will return a tuple. Kind of the lazy way out.
                left_exp = self.parse_expression(pstate)

            pstate.expect(_closebracket)
            pstate.advance()

            # Finalization prevents things from being appended
            # to containers after their closing delimiter.
            if isinstance(left_exp, tuple):
                left_exp = FinalizedList(left_exp)
            else:
                left_exp = FinalizedList([left_exp])

        else:
            left_exp = self.parse_terminal(pstate)

        return left_exp