lcompilers / lpython

Python compiler
https://lpython.org/
Other
1.5k stars 158 forks source link

Symbolic Pass failing in LFortran #2553

Closed anutosh491 closed 7 months ago

anutosh491 commented 7 months ago

This issue was brought up here (https://github.com/lfortran/lfortran/pull/3432#discussion_r1496328641)

We were lucky to not have encountered this issue in Lpython itself. Consider the following

def main0():
    x: S = Symbol('x')
    y: S = Symbol('y')
    z: S = x + y
    print(z.func == Add)

main0()

So this is an example which is solved by the code in consideration https://github.com/lcompilers/lpython/blob/7796f6d91f59b38a671056cb21859c7156cd6da7/src/libasr/pass/replace_symbolic.cpp#L786-L788

anutosh491 commented 7 months ago

Now the ASR node we have is this

    (Print
        [(IntrinsicScalarFunction
            SymbolicAddQ
            [(Var 13 z)]
            0
            (Logical 4)
            ()
        )]
        ()
        ()
    )

So we are trying to print an IntrinsicScalarFunction that is of type Logical (and luckily as of now we don't have any other intrinsic function of logical type registered, all logical type intrinsic functions are for solving symbolic cases)

But let's say we introduce a logical intrinsic function say ListIsEmpty which would be of type Logical, how can we separate this from the functions (intrinsic + logical) that are serving a symbolic purpose (like SymbolicAddQ) ?

certik commented 7 months ago

@anutosh491 go ahead and fix this one as a high priority, so that we can enable the symbolic pass in all frontends.

certik commented 7 months ago

This condition } else if (ASR::is_a<ASR::Logical_t>(*ASRUtils::expr_type(val))) { must be improved to ALSO check for the node to be intrinsic symbolic.

certik commented 7 months ago

Right below the line ASR::IntrinsicScalarFunction_t* intrinsic_func = ASR::down_cast<ASR::IntrinsicScalarFunction_t>(val); you need to check that intrinsic_func.id is a symbolic function. Otherwise you ignore it.

certik commented 7 months ago

Create a function is_symbolic_intrinsic where inside you hardwire (for now) all the symbolic intrinsic IDs.