Exemplo n.º 1
0
Arquivo: app.py Projeto: zeta1999/xls
def load_benchmarks():
    """Returns a list of benchmarks as tuples of (name, IR text)."""
    bms = []
    for ir_path in runfiles.get_contents_as_text(
            IR_EXAMPLES_FILE_LIST).split():
        if '.opt.ir' not in ir_path:
            continue

        def strip_up_to(s, part):
            if part in s:
                return s[s.find(part) + len(part):]
            return s

        # Strip off path prefix up to 'examples/' or 'modules/' to create the
        # benchmark name.
        if 'examples/' in ir_path:
            name = strip_up_to(ir_path, 'examples/')
        elif 'modules/' in ir_path:
            name = strip_up_to(ir_path, 'modules/')
        else:
            name = ir_path
        name = name[:-len('.opt.ir')]
        bms.append((name, runfiles.get_contents_as_text(ir_path)))
    bms.sort()
    return bms
Exemplo n.º 2
0
def load_precanned_examples() -> List[Tuple[str, str]]:
    """Returns a list of examples as tuples of (name, IR text).

  Examples are loaded from the list of files in IR_EXAMPLES_FILE_LIST.

  Returns:
    List of tuples containing (name, IR text).
  """
    irs = []
    for ir_path in runfiles.get_contents_as_text(
            IR_EXAMPLES_FILE_LIST).split():
        if not ir_path.endswith('.ir') or ir_path.endswith('.opt.ir'):
            continue

        def strip_up_to(s, part):
            if part in s:
                return s[s.find(part) + len(part):]
            return s

        # Strip off path prefix up to 'examples/' or 'modules/' to create the
        # example name.
        if 'examples/' in ir_path:
            name = strip_up_to(ir_path, 'examples/')
        elif 'modules/' in ir_path:
            name = strip_up_to(ir_path, 'modules/')
        else:
            name = ir_path
        name = name[:-len('.ir')]
        irs.append((name, runfiles.get_contents_as_text(ir_path)))
    irs.sort()
    return irs
Exemplo n.º 3
0
 def testGetContentsAsText(self):
     self.assertEqual(
         runfiles.get_contents_as_text('xls/common/testdata/foo.txt'),
         'FOO\n')
     self.assertEqual(
         runfiles.get_contents_as_text('xls/common/testdata/bar.txt'),
         'BAR\n')
Exemplo n.º 4
0
Arquivo: app.py Projeto: zeta1999/xls
def splash():
    return flask.render_template_string(
        runfiles.get_contents_as_text(
            'xls/visualization/ir_viz/templates/splash.tmpl'),
        benchmarks=[name for name, _ in benchmarks],
        third_party_scripts=get_third_party_js(),
        load_default=FLAGS.ir_path is not None)
Exemplo n.º 5
0
def main(argv):
    env = jinja2.Environment(undefined=jinja2.StrictUndefined)
    template = env.from_string(runfiles.get_contents_as_text(argv[1]))
    print(
        '// DO NOT EDIT: this file is AUTOMATICALLY GENERATED and should not '
        'be changed.')
    print(template.render(spec=specification))
Exemplo n.º 6
0
    def test_simple_lec(self):
        ir_text = """package p
    fn main(a: bits[2], b: bits[2]) -> bits[2] {
      ret add.3: bits[2] = add(a, b)
    }
    """

        netlist_text = """module main(clk, a_1_, a_0_, b_1_, b_0_, out_1_, out_0_);
      input clk, a_1_, a_0_, b_1_, b_0_;
      output out_1_, out_0_;
      wire p0_a_1_, p0_a_0_, p0_b_1_, p0_b_0_, p0_add_3_comb_0_, p0_add_3_comb_1_, carry, high;

      DFF p0_a_reg_1_ ( .D(a_1_), .CLK(clk), .Q(p0_a_1_) );
      DFF p0_a_reg_0_ ( .D(a_0_), .CLK(clk), .Q(p0_a_0_) );
      DFF p0_b_reg_1_ ( .D(b_1_), .CLK(clk), .Q(p0_b_1_) );
      DFF p0_b_reg_0_ ( .D(b_0_), .CLK(clk), .Q(p0_b_0_) );

      XOR out_0_cell ( .A(p0_a_0_), .B(p0_b_0_), .Z(p0_add_3_comb_0_) );

      AND carry_cell ( .A(p0_a_0_), .B(p0_b_0_), .Z(carry) );
      XOR high_cell ( .A(p0_a_1_), .B(p0_b_1_), .Z(high) );
      XOR out_1_cell ( .A(high), .B(carry), .Z(p0_add_3_comb_1_) );

      DFF p0_add_3_reg_1_ ( .D(p0_add_3_comb_1_), .CLK(clk), .Q(out_1_) );
      DFF p0_add_3_reg_0_ ( .D(p0_add_3_comb_0_), .CLK(clk), .Q(out_0_) );
    endmodule
    """

        proto_text = runfiles.get_contents_as_text(
            'xls/netlist/fake_cell_library.textproto')
        result = z3_lec.run(ir_text, netlist_text, 'main', proto_text)
        self.assertTrue(result)
Exemplo n.º 7
0
def do_import(
    subject: import_fn.ImportTokens, cache: Dict[import_fn.ImportTokens,
                                                 import_fn.ModuleInfo]
) -> import_fn.ModuleInfo:
    """Imports the module identified (globally) by 'subject'.

  Resolves against an existing import in 'cache' if it is present.

  Args:
    subject: Tokens that globally uniquely identify the module to import; e.g.
      something built-in like ('std',) for the standard library or something
      fully qualified like ('xls', 'lib', 'math').
    cache: Cache that we resolve against so we don't waste resources
      re-importing things in the import DAG.

  Returns:
    The imported module information.
  """
    assert subject
    if subject in cache:
        return cache[subject]

    if subject in [('std', ), ('float32', ), ('bfloat16', )]:
        path = 'xls/dslx/stdlib/{}.x'.format(subject[0])
    else:
        path = os.path.join(*subject) + '.x'

    f_import = functools.partial(do_import, cache=cache)
    fully_qualified_name = '.'.join(subject)

    if os.path.exists(path):
        with open(path, mode='rb') as f:
            contents = f.read().decode('utf-8')
    elif os.path.exists(os.path.join(os.path.pardir, path)):
        # Genrules in-house execute inside a subdirectory, so we also search
        # starting from the parent directory for now.
        #
        # An alternative would be to explicitly note the DSLX_PATH when invoking the
        # tool in this special genrule context, but since we expect module paths to
        # be fully qualified at the moment, we opt for this kluge.
        path = os.path.join(os.path.pardir, path)
        with open(path, mode='rb') as f:
            contents = f.read().decode('utf-8')
    else:
        contents = runfiles.get_contents_as_text(path)
        path = runfiles.get_path(path)

    logging.vlog(3, 'Parsing and typechecking %r: start', fully_qualified_name)
    m, node_to_type = parse_and_typecheck.parse_text(contents,
                                                     fully_qualified_name,
                                                     f_import=f_import,
                                                     filename=path,
                                                     print_on_error=True)
    logging.vlog(3, 'Parsing and typechecking %r: done', fully_qualified_name)

    assert node_to_type is not None
    cache[subject] = (m, node_to_type)
    return m, node_to_type
Exemplo n.º 8
0
def get_examples() -> List[Dict[str, Union[int, str]]]:
    """Returns DSLX blocks in the reference Markdown as dictionary records."""
    contents = runfiles.get_contents_as_text('docs_src/dslx_reference.md')
    examples = []
    for i, m in enumerate(_DSLX_RE.finditer(contents)):
        dslx_block = m.group(1)
        examples.append(
            dict(testcase_name=f'dslx_block_{i}', i=i, dslx_block=dslx_block))
    return examples
Exemplo n.º 9
0
def get_examples() -> List[Dict[str, Union[int, str]]]:
    """Returns DSLX blocks in the reference Markdown as dictionary records."""
    examples = []
    for input_file in _INPUT_FILES:
        contents = runfiles.get_contents_as_text(input_file)
        f = input_file.replace('/', '_').replace('.', '_')
        for i, m in enumerate(_DSLX_RE.finditer(contents)):
            dslx_block = m.group(1)
            examples.append(
                dict(testcase_name=f'dslx_block_{f}_{i}',
                     i=i,
                     dslx_block=dslx_block))

    return examples
Exemplo n.º 10
0
def splash():
    """Returns the main HTML."""
    # If --preload_ir_path is given, load in the file contents and pass to the
    # template render. It will be filled into the IR text element.
    if FLAGS.preload_ir_path:
        if FLAGS.preload_ir_path == '-':
            preloaded_ir = sys.stdin.read()
        else:
            with open(FLAGS.preload_ir_path) as f:
                preloaded_ir = f.read()
    else:
        preloaded_ir = ''
    return flask.render_template_string(
        runfiles.get_contents_as_text(
            'xls/visualization/ir_viz/templates/splash.tmpl'),
        examples=[name for name, _ in examples],
        third_party_scripts=get_third_party_js(),
        preloaded_ir=preloaded_ir,
        use_benchmark_examples=FLAGS.example_ir_dir is None)
Exemplo n.º 11
0
 def test_first_n_seeds(self, seed):
     if FLAGS.update_golden and seed >= self.SEED_TO_CHECK_LIMIT:
         # Skip running unnecessary tests if updating golden because the test is
         # slow and runs unsharded.
         return
     rng = random.Random(seed)
     samples = run_fuzz.run_fuzz(
         rng,
         ast_generator.AstGeneratorOptions(
             disallow_divide=True, binop_allowlist=[ast.BinopKind.SHLL]),
         **self.KWARGS)
     for i in range(self.KWARGS['sample_count']):
         if seed < self.SEED_TO_CHECK_LIMIT and i < self.SAMPLE_TO_CHECK_LIMIT:
             path = self.GOLDEN_REFERENCE_FMT.format(seed=seed, sample=i)
             if FLAGS.update_golden:
                 with open(path, 'w') as f:
                     f.write(samples[i].input_text)
             else:
                 # rstrip to avoid miscompares from trailing newline at EOF.
                 expected = runfiles.get_contents_as_text(path).rstrip()
                 self.assertMultiLineEqual(expected, samples[i].input_text)
Exemplo n.º 12
0
    def test_parallel_add_verilog(self):
        add8_ir = opgen.generate_ir_package('add',
                                            output_type='bits[8]',
                                            operand_types=('bits[8]',
                                                           'bits[8]'))
        add16_ir = opgen.generate_ir_package('add',
                                             output_type='bits[16]',
                                             operand_types=('bits[16]',
                                                            'bits[16]'))
        add24_ir = opgen.generate_ir_package('add',
                                             output_type='bits[24]',
                                             operand_types=('bits[24]',
                                                            'bits[24]'))

        modules = (opgen.generate_verilog_module('add8_module', add8_ir),
                   opgen.generate_verilog_module('add16_module', add16_ir),
                   opgen.generate_verilog_module('add24_module', add24_ir))
        parallel_module = opgen.generate_parallel_module(modules, 'foo')
        self.assertEqual(
            parallel_module,
            runfiles.get_contents_as_text(
                'xls/delay_model/testdata/parallel_op_module.vtxt'))
Exemplo n.º 13
0
def main(argv):
  if len(argv) > 2:
    raise app.UsageError('Too many command-line arguments.')

  with open(argv[1], 'rb') as f:
    contents = f.read()

  dm = delay_model.DelayModel(
      text_format.Parse(contents, delay_model_pb2.DelayModel()))

  env = jinja2.Environment(undefined=jinja2.StrictUndefined)
  tmpl_text = runfiles.get_contents_as_text(
      'xls/delay_model/generate_delay_lookup.tmpl')
  template = env.from_string(tmpl_text)
  rendered = template.render(
      delay_model=dm,
      name=FLAGS.model_name,
      precedence=FLAGS.precedence,
      camel_case_name=''.join(
          s.capitalize() for s in FLAGS.model_name.split('_')))
  print('// DO NOT EDIT: this file is AUTOMATICALLY GENERATED and should not '
        'be changed.')
  print(rendered)
Exemplo n.º 14
0
def static_handler(filename):
    """Reads and returns static files.

  Args:
    filename: The name of the file. The file is loaded from the data deps under
      `xls/visualization/ir_viz`.

  Returns:
    Flask response.
  """

    try:
        content = runfiles.get_contents_as_text(
            flask.safe_join('xls/visualization/ir_viz', filename))
    except FileNotFoundError:
        flask.abort(404)
    if filename.endswith('.js'):
        ctype = 'application/javascript'
    elif filename.endswith('.css'):
        ctype = 'text/css'
    else:
        ctype = 'text/plain'
    return flask.Response(response=content, content_type=ctype)
Exemplo n.º 15
0
 def test_first_n_seeds(self, seed):
   if FLAGS.update_golden and seed >= self.SEED_TO_CHECK_LIMIT:
     # Skip running unnecessary tests if updating golden because the test is
     # slow and runs unsharded.
     return
   samples = run_fuzz.run_fuzz(
       random.Random(seed), self._get_ast_options(), **self.KWARGS)
   for i in range(self.KWARGS['sample_count']):
     if seed < self.SEED_TO_CHECK_LIMIT and i < self.SAMPLE_TO_CHECK_LIMIT:
       path = self.GOLDEN_REFERENCE_FMT.format(
           seed=seed, sample=i, codegen='' if FLAGS.codegen else '_no_codegen')
       if FLAGS.update_golden:
         abs_path = os.path.join(FLAGS.xls_source_dir, path.lstrip('xls/'))
         with open(abs_path, 'w') as f:
           f.write(samples[i].input_text)
       else:
         # rstrip to avoid miscompares from trailing newline at EOF.
         expected = runfiles.get_contents_as_text(path).rstrip()
         self.assertMultiLineEqual(
             expected,
             samples[i].input_text,
             msg=f'want: {expected!r}\n'
             f'got:  {samples[i].input_text!r}')
Exemplo n.º 16
0
def get_third_party_js():
    """Returns the URLS of the third-party JS to load."""
    urls = runfiles.get_contents_as_text(
        'xls/visualization/ir_viz/third_party_js.txt').split()
    return [u.strip() for u in urls if u.strip()]
Exemplo n.º 17
0
 def testNonexistantFile(self):
     with self.assertRaises(FileNotFoundError):
         runfiles.get_path('not/a/file.txt')
     with self.assertRaises(FileNotFoundError):
         runfiles.get_contents_as_text('not/a/file.txt')