Skip to content

Imports Too Many When Importing From File #1611

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Tracked by #1600
ronnuriel opened this issue Mar 23, 2023 · 9 comments
Closed
Tracked by #1600

Imports Too Many When Importing From File #1611

ronnuriel opened this issue Mar 23, 2023 · 9 comments
Labels
asr ASR related changes c Label for C language related changes

Comments

@ronnuriel
Copy link
Collaborator

Imports too many from import file

Lets create bar.py (bar.py is in the same directory as expr7)

from expr7 import test_pow

def main():
	test_pow()
 
main()

expr7:

from ltypes import i32

def test_pow():
    a: i32
    a = i32(pow(2, 2))

def test_pow_1(a: i32, b: i32) -> i32:
    res: i32
    res = i32(pow(a, b))
    return res

def main0():
    test_pow()
    c: i32
    c = test_pow_1(1, 2)

Run:
lpython --show-c -I . bar.py

C generated code output:

#define ASSERT(cond)                                                           \
    {                                                                          \
        if (!(cond)) {                                                         \
            printf("%s%s", "ASSERT failed: ", __FILE__);                       \
            printf("%s%s", "\nfunction ", __func__);                           \
            printf("%s%d%s", "(), line number ", __LINE__, " at \n");          \
            printf("%s%s", #cond, "\n");                                       \
            exit(1);                                                           \
        }                                                                      \
    }
#define ASSERT_MSG(cond, msg)                                                  \
    {                                                                          \
        if (!(cond)) {                                                         \
            printf("%s%s", "ASSERT failed: ", __FILE__);                       \
            printf("%s%s", "\nfunction ", __func__);                           \
            printf("%s%d%s", "(), line number ", __LINE__, " at \n");          \
            printf("%s%s", #cond, "\n");                                       \
            printf("%s", "ERROR MESSAGE:\n");                                  \
            printf("%s%s", msg, "\n");                                         \
            exit(1);                                                           \
        }                                                                      \
    }


struct dimension_descriptor
{
    int32_t lower_bound, length;
};

// Implementations
double __lpython_overloaded_0__pow(int32_t x, int32_t y)
{
    double _lpython_return_variable;
    _lpython_return_variable = (double)(pow(x, y));
    return _lpython_return_variable;
}

float _lfortran_caimag(float complex x);

double _lfortran_zaimag(double complex x);

void test_pow()
{
    int32_t a;
    a = (int32_t)(__lpython_overloaded_0__pow(2, 2));
}

int32_t test_pow_1(int32_t a, int32_t b)
{
    int32_t _lpython_return_variable;
    int32_t res;
    res = (int32_t)(__lpython_overloaded_0__pow(a, b));
    _lpython_return_variable = res;
    return _lpython_return_variable;
}

void main0()
{
    int32_t c;
    test_pow();
    c = test_pow_1(1, 2);
}

void _xx_lcompilers_changed_main_xx()
{
    test_pow();
}

void _lpython_main_program()
{
    _xx_lcompilers_changed_main_xx();
}

int main(int argc, char* argv[])
{
    _lpython_main_program();
    return 0;
}

As you can see it generates more then test_pow (main0, test_pow_1)
while it is wrong.

@ronnuriel ronnuriel added c Label for C language related changes asr ASR related changes labels Mar 23, 2023
@ronnuriel ronnuriel changed the title Imports too many when importing from file Imports Too Many When Importing From File Mar 23, 2023
@certik
Copy link
Contributor

certik commented Mar 23, 2023

I think this is the same issue as #1610, the unused function pass should remove main0, and then the rest should work (it will remove test_pow_1 as well).

@certik certik mentioned this issue Mar 23, 2023
38 tasks
@Smit-create
Copy link
Collaborator

This is in the TODO Roadmap of C. C doesn't support ASR passes yet.

@Shaikh-Ubaid
Copy link
Collaborator

This is in the TODO Roadmap of C. C doesn't support ASR passes yet.

It looks like C supports ASR passes. The following lines indicate the use of ASR passes in the C backend. @Smit-create which passes do you mean?

LCompilers::PassOptions pass_options;
pass_options.always_run = true;
pass_create_subroutine_from_function(al, asr, pass_options);
pass_replace_array_op(al, asr, pass_options);
pass_unused_functions(al, asr, pass_options);
pass_replace_class_constructor(al, asr, pass_options);
ASRToCVisitor v(diagnostics, co, default_lower_bound);

@ronnuriel
Copy link
Collaborator Author

ronnuriel commented Mar 24, 2023

@Shaikh-Ubaid
Like issue 1610
It is importing the functions that don't have a defined return value
(Or None)

@Smit-create
Copy link
Collaborator

The following lines indicate the use of ASR passes in the C backend

These are just a few hard-coded passes that are necessary with the current C tests. We need to add the same approach as the LLVM backend.

@Shaikh-Ubaid
Copy link
Collaborator

These are just a few hard-coded passes that are necessary with the current C tests. We need to add the same approach as the LLVM backend.

Got it. Thank you!

@Smit-create
Copy link
Collaborator

This also looks fixed now.
See the generated output of C code:

C code

#include <complex.h>
#include <inttypes.h>
#include <math.h>

#include <stdlib.h>
#include <stdbool.h>
#include <stdio.h>
#include <string.h>
#include <lfortran_intrinsics.h>

#define ASSERT(cond)                                                           \
    {                                                                          \
        if (!(cond)) {                                                         \
            printf("%s%s", "ASSERT failed: ", __FILE__);                       \
            printf("%s%s", "\nfunction ", __func__);                           \
            printf("%s%d%s", "(), line number ", __LINE__, " at \n");          \
            printf("%s%s", #cond, "\n");                                       \
            exit(1);                                                           \
        }                                                                      \
    }
#define ASSERT_MSG(cond, msg)                                                  \
    {                                                                          \
        if (!(cond)) {                                                         \
            printf("%s%s", "ASSERT failed: ", __FILE__);                       \
            printf("%s%s", "\nfunction ", __func__);                           \
            printf("%s%d%s", "(), line number ", __LINE__, " at \n");          \
            printf("%s%s", #cond, "\n");                                       \
            printf("%s", "ERROR MESSAGE:\n");                                  \
            printf("%s%s", msg, "\n");                                         \
            exit(1);                                                           \
        }                                                                      \
    }


struct dimension_descriptor
{
    int32_t lower_bound, length;
};

// Implementations
double __lpython_overloaded_0__pow(int32_t x, int32_t y)
{
    double _lpython_return_variable;
    _lpython_return_variable = (double)(pow(x, y));
    return _lpython_return_variable;
}

float _lfortran_caimag(float complex x);

double _lfortran_zaimag(double complex x);

void test_pow()
{
    int32_t a;
    a = (int32_t)(__lpython_overloaded_0__pow(2, 2));
}

void _xx_lcompilers_changed_main_xx()
{
    test_pow();
}

void _lpython_main_program()
{
    _xx_lcompilers_changed_main_xx();
}

int main(int argc, char* argv[])
{
    _lpython_main_program();
    return 0;
}

Is this the expected output @ronnuriel ?

@Shaikh-Ubaid
Copy link
Collaborator

Closing this as it seems fixed. The functions main0() and test_pow_1() are no longer present in --show-c output.

@certik
Copy link
Contributor

certik commented Apr 24, 2023

Thanks @Shaikh-Ubaid. If there is still a problem, we can open a new issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
asr ASR related changes c Label for C language related changes
Projects
None yet
Development

No branches or pull requests

4 participants