def ir_value_to_interpreter_value(value: ir_value_mod.Value) -> Value: """Converts an IR Value to an interpreter Value.""" if value.is_bits(): return Value.make_bits(Tag.UBITS, value.get_bits()) elif value.is_array(): return Value.make_array( tuple(ir_value_to_interpreter_value(e) for e in value.get_elements())) else: assert value.is_tuple() return Value.make_tuple( tuple(ir_value_to_interpreter_value(e) for e in value.get_elements()))
def ir_value_to_interpreter_value(value: ir_value_mod.Value) -> Value: """Converts an IR Value to an interpreter Value.""" if value.is_bits(): if value.get_bits().bit_count() <= 64: return Value.make_bits(Tag.UBITS, value.get_bits()) else: # For wide values which do not fit in 64 bits, parse value as as string. return value_from_string(value.to_str(FormatPreference.HEX)) elif value.is_array(): return Value.make_array( tuple(ir_value_to_interpreter_value(e) for e in value.get_elements())) else: assert value.is_tuple() return Value.make_tuple( tuple(ir_value_to_interpreter_value(e) for e in value.get_elements()))
def ir_value_to_interpreter_value(value: ir_value.Value, dslx_type: ConcreteType) -> dslx_value.Value: """Converts an IR Value to an interpreter Value.""" if value.is_bits(): assert isinstance(dslx_type, BitsType), dslx_type ir_bits_val = value.get_bits() if dslx_type.get_signedness(): return dslx_value.Value.make_sbits(ir_bits_val) return dslx_value.Value.make_ubits(ir_bits_val) elif value.is_array(): assert isinstance(dslx_type, ArrayType), dslx_type return dslx_value.Value.make_array( tuple( ir_value_to_interpreter_value(e, dslx_type.element_type) for e in value.get_elements())) else: assert value.is_tuple() assert isinstance(dslx_type, TupleType), dslx_type return dslx_value.Value.make_tuple( tuple( ir_value_to_interpreter_value(e, t) for e, t in zip( value.get_elements(), dslx_type.get_unnamed_members())))
def compare_values(interpreter_value: dslx_value.Value, jit_value: ir_value.Value) -> None: """Asserts equality between a DSLX Value and an IR Value. Recursively traverses the values (for arrays/tuples) and makes assertions about value and length properties. Args: interpreter_value: Value that resulted from DSL interpretation. jit_value: Value that resulted from JIT-compiled execution. Raises: JitMiscompareError: If the dslx_value and jit_value are not equivalent. UnsupportedJitConversionError: If there is not JIT-supported type equivalent for the interpreter value. """ if interpreter_value.is_bits() or interpreter_value.is_enum(): assert jit_value.is_bits(), f'Expected bits value: {jit_value!r}' jit_value = jit_value.get_bits() bit_count = interpreter_value.get_bit_count() if bit_count != jit_value.bit_count(): raise JitMiscompareError(f'Inconsistent bit counts for value -- ' f'interp: {bit_count}, ' f'jit: {jit_value.bit_count()}') if interpreter_value.is_ubits(): interpreter_bits_value = interpreter_value.get_bits_value() jit_bits_value = bits_to_int(jit_value, signed=False) else: interpreter_bits_value = interpreter_value.get_bits_value_signed() jit_bits_value = bits_to_int(jit_value, signed=True) if interpreter_bits_value != jit_bits_value: raise JitMiscompareError( 'Inconsistent bit values in return value -- ' 'interp: {!r}, jit: {!r}'.format(interpreter_bits_value, jit_bits_value)) elif interpreter_value.is_array(): assert jit_value.is_array(), f'Expected array value: {jit_value!r}' interpreter_values = interpreter_value.array_payload.elements jit_values = jit_value.get_elements() interp_len = len(interpreter_values) jit_len = len(jit_values) if interp_len != jit_len: raise JitMiscompareError( f'Inconsistent array lengths in return value -- ' f'interp: {interp_len}, jit: {jit_len}') for interpreter_element, jit_element in zip(interpreter_values, jit_values): compare_values(interpreter_element, jit_element) elif interpreter_value.is_tuple(): assert jit_value.is_tuple(), 'Expected tuple value: {jit_value!r}' interpreter_values = interpreter_value.tuple_members jit_values = jit_value.get_elements() interp_len = len(interpreter_values) jit_len = len(jit_values) if interp_len != jit_len: raise JitMiscompareError( f'Inconsistent tuple lengths in return value -- ' f'interp: {interp_len}, jit: {jit_len}') for interpreter_element, jit_element in zip(interpreter_values, jit_values): compare_values(interpreter_element, jit_element) else: raise UnsupportedJitConversionError( 'No JIT-supported type equivalent: {}'.format(interpreter_value))