def test_ir_to_json(self): json_str = ir_to_json.ir_to_json( """package test fn main(x: bits[32], y: bits[32]) -> bits[32] { ret add.1: bits[32] = add(x, y) }""", 'unit') json_dict = json.loads(json_str) self.assertIn('edges', json_dict) self.assertLen(json_dict['edges'], 2) self.assertIn('nodes', json_dict) self.assertLen(json_dict['nodes'], 3)
def graph_handler(): """Parses the posted text and returns a parse status.""" text = flask.request.form['text'] try: json_text = ir_to_json.ir_to_json(text, FLAGS.delay_model, FLAGS.pipeline_stages, FLAGS.entry) except Exception as e: # pylint: disable=broad-except # TODO(meheff): Switch to new pybind11 more-specific exception. return flask.jsonify({'error_code': 'error', 'message': str(e)}) graph = json.loads(json_text) jsonified = flask.jsonify({'error_code': 'ok', 'graph': graph}) return jsonified
def test_ir_to_json_with_scheduling(self): json_str = ir_to_json.ir_to_json( """package test fn main(x: bits[32], y: bits[32]) -> bits[32] { add.1: bits[32] = add(x, y) ret neg.2: bits[32] = neg(add.1) }""", 'unit', 2) json_dict = json.loads(json_str) self.assertIn('edges', json_dict) self.assertLen(json_dict['edges'], 3) self.assertIn('nodes', json_dict) self.assertLen(json_dict['nodes'], 4) for node in json_dict['nodes']: if node['id'] == 'x' or node['id'] == 'y': self.assertEqual(node['attributes']['cycle'], 0) elif node['id'] == 'add_1': self.assertEqual(node['attributes']['cycle'], 0) elif node['id'] == 'neg_2': self.assertEqual(node['attributes']['cycle'], 1)