コード例 #1
0
def test_early_out_outputs():
    @family_closure
    def IR_fc(family):
        Data = family.BitVector[16]

        @family.assemble(locals(), globals())
        class IR(Peak):
            @name_outputs(out=Data)
            def __call__(self, in_: Data):
                return in_

        return IR

    @family_closure
    def Arch_fc(family):
        Data = family.BitVector[16]
        Bit = family.Bit

        @family.assemble(locals(), globals())
        class Arch(Peak):
            @name_outputs(out=Bit)
            def __call__(self, in_: Data):
                return in_[0]

        return Arch

    arch_fc = Arch_fc
    arch_bv = arch_fc(family.PyFamily())
    arch_mapper = ArchMapper(arch_fc)
    ir_fc = IR_fc
    ir_mapper = arch_mapper.process_ir_instruction(ir_fc)
    rr = ir_mapper.solve('z3')
    assert rr is None
    assert not ir_mapper.has_bindings
コード例 #2
0
def test_automapper():
    IR = gen_SmallIR(8)
    arch_fc = PE_fc
    arch_bv = arch_fc(family.PyFamily())
    arch_mapper = ArchMapper(arch_fc)
    expect_found = ('Add', 'Sub', 'And', 'Nand', 'Or', 'Nor', 'Not', 'Neg')
    expect_not_found = ('Mul', 'Shftr', 'Shftl', 'Not', 'Neg')
    for ir_name, ir_fc in IR.instructions.items():
        ir_mapper = arch_mapper.process_ir_instruction(ir_fc)
        rewrite_rule = ir_mapper.solve('z3')
        if rewrite_rule is None:
            assert ir_name in expect_not_found
            continue
        assert ir_name in expect_found
        #verify the mapping works
        counter_example = rewrite_rule.verify()
        assert counter_example is None
        ir_bv = ir_fc(family.PyFamily())
        for _ in range(num_test_vectors):
            ir_vals = {
                path: BitVector.random(8)
                for path in rewrite_rule.ir_bounded
            }
            ir_inputs = rewrite_rule.build_ir_input(ir_vals, family.PyFamily())
            arch_inputs = rewrite_rule.build_arch_input(
                ir_vals, family.PyFamily())
            assert ir_bv()(**ir_inputs) == arch_bv()(**arch_inputs)
コード例 #3
0
def test_mul():
    arch_fc = ALU_fc
    ir_fc = Mul_fc
    arch_mapper = ArchMapper(arch_fc)
    ir_mapper = arch_mapper.process_ir_instruction(ir_fc)
    assert ir_mapper.formula is not None
    rr = ir_mapper.solve('z3')
    assert rr is not None
コード例 #4
0
def run_constraint_test(ir_fc, constraints, solved):
    arch_fc = PE_fc
    arch_bv = arch_fc(family.PyFamily())
    arch_mapper = ArchMapper(arch_fc, path_constraints=constraints)
    ir_mapper = arch_mapper.process_ir_instruction(ir_fc)
    assert ir_mapper.has_bindings
    rr = ir_mapper.solve('z3')
    if rr is None:
        assert not solved
    else:
        assert solved
コード例 #5
0
def test_const():

    #Testing out something like coreir const
    @family_closure
    def IR_fc(family):
        Data = family.BitVector[16]

        @family.assemble(locals(), globals())
        class IR(Peak):
            @name_outputs(out=Data)
            def __call__(self, const_value: Const(Data)):
                return const_value

        return IR

    @family_closure
    def Arch_fc(family):
        Data = family.BitVector[16]

        class Op(Enum):
            add = 1
            const = 2

        class Inst(Product):
            op = Op
            imm = Data

        @family.assemble(locals(), globals())
        class Arch(Peak):
            @name_outputs(out=Data)
            def __call__(self, inst: Const(Inst), in0: Data, in1: Data):
                if inst.op == Op.add:
                    return in0 + in1
                else:  #inst.op == Op.const
                    return inst.imm

        return Arch

    arch_fc = Arch_fc
    arch_bv = arch_fc(family.PyFamily())
    arch_mapper = ArchMapper(arch_fc)
    ir_fc = IR_fc
    ir_mapper = arch_mapper.process_ir_instruction(ir_fc)
    rr = ir_mapper.solve('z3')
    assert rr is not None
    assert (('const_value', ), ('inst', 'imm')) in rr.ibinding
コード例 #6
0
    def generate_rewrite_rule(self, subgraph_ind, mul_op):

        if not os.path.exists("./outputs/PE.json"):
            raise ValueError("Generate and write merged graph peak arch first")

        if not os.path.exists("./outputs/peak_eqs/peak_eq_" +
                              str(subgraph_ind) + ".py"):
            raise ValueError("Generate and write peak_eq first")
        print("\nGenerating rewrite rule for:")
        print(self.short_eq)

        arch = read_arch("./outputs/PE.json")
        graph_arch(arch)
        PE_fc = wrapped_peak_class(arch)

        arch_inputs = arch.inputs

        input_constraints = {}

        for n, d in self.subgraph.nodes.data(True):
            if utils.is_node_input(d):
                if utils.is_node_bit_input(d):
                    input_constraints[(f"bitinputs{arch.bit_inputs.index(n)}",
                                       )] = (f"data{n}", )
                else:
                    input_constraints[(f"inputs{arch.inputs.index(n)}", )] = (
                        f"data{n}", )

        path_constraints = {}

        if not mul_op:
            print("Datagating multipliers")
            idx = 0
            for module in arch.modules:
                if module.type_ == "mul":
                    path_constraints[(
                        'inst', 'mul',
                        idx)] = hwtypes.smt_bit_vector.SMTBitVector[2](2)
                    print(path_constraints)
                    idx += 1

        arch_mapper = ArchMapper(PE_fc,
                                 path_constraints=path_constraints,
                                 input_constraints=input_constraints)

        peak_eq = importlib.import_module("outputs.peak_eqs.peak_eq_" +
                                          str(subgraph_ind))

        if subgraph_ind > -1:
            print("Solving...")
            ir_mapper = arch_mapper.process_ir_instruction(
                peak_eq.mapping_function_fc)
            start = time.time()
            solution = ir_mapper.solve('btor', external_loop=True, logic=QF_BV)
            end = time.time()
            print("Rewrite rule solving time:", end - start)
        else:
            print("Skipping...")
            solution = None

        if solution is None:
            utils.print_red(
                "No rewrite rule found, trying without input constraints")
            arch_mapper = ArchMapper(PE_fc)
            ir_mapper = arch_mapper.process_ir_instruction(
                peak_eq.mapping_function_fc)
            solution = ir_mapper.solve('btor', external_loop=True, logic=QF_BV)
            if solution is None:
                print("Still couldn't find solution")
                exit()
            else:
                utils.print_green("Found rewrite rule")
                self.rewrite_rule = solution
        else:
            utils.print_green("Found rewrite rule")
            self.rewrite_rule = solution
            for i in solution.ibinding:
                print(i)
コード例 #7
0
ファイル: test_mips.py プロジェクト: cdonovick/peak-examples
def test_mips_smt():
    arch_fc = sim.MIPS32_mappable_fc
    arch_mapper = ArchMapper(arch_fc, family=family)
コード例 #8
0
def test_smt():
    arch_fc = riscv_sim.R32I_mappable_fc
    arch_mapper = ArchMapper(arch_fc, family=riscv_family)