pyxis-roc / xlatir

The Translatable Intermediate Representation (aka XIR)
0 stars 0 forks source link

Untyped constants should be type inferred based on their usage #21

Open bavalpey opened 3 years ago

bavalpey commented 3 years ago

Translation of xir to c has an issue resolving the type of an untyped constant. A simple case where this is an issue is the translation of the following xir file to c

def SASS_ISET_GT_AND(a: s32, b: s32, p: bool) -> s32:
    return (4294967295 if AND(p, GT(sext_32(a), sext_32(b))) else RZ)

Assume there is a definition for RZ in one of the included libraries that looks like the following

# snip
RZ = 0

Currently, this results in an Assertion Error.

Expand for full trace FunctionDef(name='SASS_ISET_GT_AND', args=arguments(posonlyargs=[], args=[arg(arg='a', annotation=Name(id='s32', ctx=Load()), type_comment=None), arg(arg='b', annotation=Name(id='s32', ctx=Load()), type_comment=None), arg(arg='p', annotation=Name(id='bool', ctx=Load()), type_comment=None)], vararg=None, kwonlyargs=[], kw_defaults=[], kwarg=None, defaults=[]), body=[Return(value=IfExp(test=Call(func=Name(id='AND', ctx=Load()), args=[Name(id='p', ctx=Load()), Call(func=Name(id='GT', ctx=Load()), args=[Call(func=Name(id='sext_32', ctx=Load()), args=[Name(id='a', ctx=Load())], keywords=[]), Call(func=Name(id='sext_32', ctx=Load()), args=[Name(id='b', ctx=Load())], keywords=[])], keywords=[])], keywords=[]), body=Constant(value=4294967295, kind=None), orelse=Name(id='RZ', ctx=Load())))], decorator_list=[], returns=Name(id='s32', ctx=Load()), type_comment=None) WARNING:xlatir.xir.xir:Missing declaration: sext_32 WARNING:xlatir.xir.xir:Missing declaration: sext_32 TyVar(TY:a) = TyConstant(s32) # TyVar(TY:b) = TyConstant(s32) # TyVar(TY:p) = TyConstant(bool) # TyVar(TY:fn_SASS_ISET_GT_AND_retval) = TyConstant(s32) # TyVar(TY:GT:2) = TyApp(TyVar(TY:ret2), [TyVar(TY:unknown_fn_sext_323), TyVar(TY:unknown_fn_sext_324)]) # TyApp(TyVar(TY:ret2), [TyVar(TY:unknown_fn_sext_323), TyVar(TY:unknown_fn_sext_324)]) = TyApp(TyConstant(bool), [TyVar(TY:T:2), TyVar(TY:T:2)]) # TyVar(TY:AND:1) = TyApp(TyVar(TY:ret1), [TyVar(TY:p), TyVar(TY:ret2)]) # TyApp(TyVar(TY:ret1), [TyVar(TY:p), TyVar(TY:ret2)]) = TyApp(TyVar(TY:T:1), [TyVar(TY:T:1), TyVar(TY:T:1)]) # TyVar(TY:if_exp:0) = TyApp(TyVar(TY:ret0), [TyVar(TY:ret1), TyVarLiteral(TY:4294967295_0, 4294967295), TyVar(TY:RZ)]) # TyApp(TyVar(TY:ret0), [TyVar(TY:ret1), TyVarLiteral(TY:4294967295_0, 4294967295), TyVar(TY:RZ)]) = TyApp(TyVar(TY:if_gamma:0), [TyConstant(bool), TyVar(TY:if_gamma:0), TyVar(TY:if_gamma:0)]) # TyVar(TY:ret0) = TyVar(TY:fn_SASS_ISET_GT_AND_retval) # **** TyVar(TY:a) TyConstant(s32) TyConstant(s32) TyConstant(s32) TyVar(TY:b) TyConstant(s32) TyVar(TY:p) TyConstant(bool) TyConstant(bool) TyConstant(bool) TyVar(TY:fn_SASS_ISET_GT_AND_retval) TyConstant(s32) TyVar(TY:GT:2) TyApp(TyVar(TY:ret2), [TyVar(TY:unknown_fn_sext_323), TyVar(TY:unknown_fn_sext_324)]) TyApp(TyVar(TY:ret2), [TyVar(TY:unknown_fn_sext_323), TyVar(TY:unknown_fn_sext_324)]) TyApp(TyVar(TY:ret2), [TyVar(TY:unknown_fn_sext_323), TyVar(TY:unknown_fn_sext_324)]) TyApp(TyConstant(bool), [TyVar(TY:T:2), TyVar(TY:T:2)]) TyApp(TyVar(TY:ret2), [TyVar(TY:unknown_fn_sext_323), TyVar(TY:unknown_fn_sext_324)]) TyVar(TY:ret2) TyConstant(bool) TyVar(TY:unknown_fn_sext_323) TyVar(TY:T:2) TyVar(TY:T:2) TyVar(TY:T:2) TyVar(TY:unknown_fn_sext_324) TyVar(TY:T:2) TyVar(TY:AND:1) TyApp(TyVar(TY:ret1), [TyVar(TY:p), TyVar(TY:ret2)]) TyApp(TyVar(TY:ret1), [TyVar(TY:p), TyVar(TY:ret2)]) TyApp(TyVar(TY:ret1), [TyVar(TY:p), TyVar(TY:ret2)]) TyApp(TyVar(TY:T:1), [TyVar(TY:T:1), TyVar(TY:T:1)]) TyApp(TyVar(TY:ret1), [TyVar(TY:p), TyVar(TY:ret2)]) TyVar(TY:ret1) TyConstant(bool) TyVar(TY:T:1) TyConstant(bool) TyVar(TY:if_exp:0) TyApp(TyVar(TY:ret0), [TyVar(TY:ret1), TyVarLiteral(TY:4294967295_0, 4294967295), TyVar(TY:RZ)]) TyApp(TyVar(TY:ret0), [TyVar(TY:ret1), TyVarLiteral(TY:4294967295_0, 4294967295), TyVar(TY:RZ)]) TyApp(TyVar(TY:ret0), [TyVar(TY:ret1), TyVarLiteral(TY:4294967295_0, 4294967295), TyVar(TY:RZ)]) TyApp(TyVar(TY:if_gamma:0), [TyConstant(bool), TyVar(TY:if_gamma:0), TyVar(TY:if_gamma:0)]) TyApp(TyVar(TY:ret0), [TyVar(TY:ret1), TyVarLiteral(TY:4294967295_0, 4294967295), TyVar(TY:RZ)]) TyVar(TY:ret0) TyVarLiteral(TY:4294967295_0, 4294967295) TyVar(TY:if_gamma:0) TyVarLiteral(TY:4294967295_0, 4294967295) TyVarLiteral(TY:4294967295_0, 4294967295) TyConstant(s32) TyVar(TY:RZ) TyVarLiteral(TY:4294967295_0, 4294967295) Traceback (most recent call last): File "/localdisk/bvalpey/repos/ROCetta/.venv/bin/xir2x.py", line 6, in exec(compile(open(__file__).read(), __file__, 'exec')) File "/localdisk/bvalpey/repos/ROCetta/xlatir/bin/xir2x.py", line 334, in translator.polyinst.get_instantiations(sem, ty, gltyenv) File "/localdisk/bvalpey/repos/ROCetta/xlatir/xlatir/xir/xirxlat.py", line 570, in get_instantiations self.visit(node) File "/usr/lib/python3.8/ast.py", line 360, in visit return visitor(node) File "/usr/lib/python3.8/ast.py", line 368, in generic_visit self.visit(item) File "/usr/lib/python3.8/ast.py", line 360, in visit return visitor(node) File "/usr/lib/python3.8/ast.py", line 370, in generic_visit self.visit(value) File "/usr/lib/python3.8/ast.py", line 360, in visit return visitor(node) File "/usr/lib/python3.8/ast.py", line 370, in generic_visit self.visit(value) File "/usr/lib/python3.8/ast.py", line 360, in visit return visitor(node) File "/localdisk/bvalpey/repos/ROCetta/xlatir/xlatir/xir/xirxlat.py", line 538, in visit_Call self.generic_visit(node) File "/usr/lib/python3.8/ast.py", line 368, in generic_visit self.visit(item) File "/usr/lib/python3.8/ast.py", line 360, in visit return visitor(node) File "/localdisk/bvalpey/repos/ROCetta/xlatir/xlatir/xir/xirxlat.py", line 538, in visit_Call self.generic_visit(node) File "/usr/lib/python3.8/ast.py", line 368, in generic_visit self.visit(item) File "/usr/lib/python3.8/ast.py", line 360, in visit return visitor(node) File "/localdisk/bvalpey/repos/ROCetta/xlatir/xlatir/xir/xirxlat.py", line 526, in visit_Call assert hasattr(node, '_xir_type') AssertionError
sree314 commented 3 years ago

The assertion error has nothing to do with RZ. If a node does not have an '_xir_type', it means there was definition found for it. Look for an "unknown_fn" in the debug logs. Here, sext_32 does not have a definition associated with it (see the WARNINGs). Let me know if you want a flag that turns these warnings into errors.