Skip to content
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

C backend is amenable for the swap problem #32

Closed
leissa opened this issue Oct 13, 2015 · 4 comments
Closed

C backend is amenable for the swap problem #32

leissa opened this issue Oct 13, 2015 · 4 comments
Assignees

Comments

@leissa
Copy link
Member

leissa commented Oct 13, 2015

When translating the following program

fn main(c: bool, mut a: int, mut b: int) -> int {
    while c {
        let tmp = a;
        a = b;
        b = tmp;
    }

    a + b
}

to C:

int main_impala(bool c_29, int a_30, int b_31) {
    int tmp_55;
    int b_57;
    tmp_55 = a_30;
    b_57 = b_31;
    goto l44;
    l44: ;
        if (c_29) {
            goto l47;
        } else {
            goto l50;
        }
    l47: ;
        tmp_55 = b_57;
        b_57 = tmp_55;
        goto l44;
    l50: ;
        int _64;
        _64 = tmp_55 + b_57;
        return _64;
}

The code is incorrect. The swap is lost.

@richardmembarth
Copy link
Member

As far I see, this already broken in the thorin IR:

impala -emit-thorin test.impala -Othorin
module 'test'


main_impala_27(mem mem_28, bool c_29, qs32 a_30, qs32 b_31, fn(mem, qs32) return_32) extern 
    fn(mem) _53 = @ c_29 select while_body_47, next_50
    while_head_44 mem_28, a_30, b_31

    while_head_44(mem mem_46, qs32 tmp_55, qs32 b_57)
        _53 mem_46

    while_body_47(mem mem_49)
        while_head_44 mem_49, b_57, tmp_55

    next_50(mem mem_52)
        qs32 _64 = add tmp_55, b_57
        return_32 mem_52, _64

@leissa
Copy link
Member Author

leissa commented Oct 13, 2015

nope. it's a problem of the C output. In Thorin the swap is in the call/args/params.

@richardmembarth
Copy link
Member

I see, I'll have a look into this as discussed.

@richardmembarth
Copy link
Member

07de4d7 should finally fix this:

int main_impala(bool c_29, int a_30, int b_31) {
    int  tmp_55;
    int ptmp_55;
    int  b_57;
    int pb_57;
    ptmp_55 = a_30;
    pb_57 = b_31;
    goto l44;
    l44: ;
        tmp_55 = ptmp_55;
        b_57 = pb_57;
        if (c_29) goto l47; else goto l50;
    l47: ;
        ptmp_55 = b_57;
        pb_57 = tmp_55;
        goto l44;
    l50: ;
        int _64;
        _64 = tmp_55 + b_57;
        return _64;
}

NeuralCoder3 pushed a commit to NeuralCoder3/thorin-2-autodiff-legacy that referenced this issue Apr 12, 2022
PearCoding pushed a commit to PearCoding/thorin that referenced this issue Dec 25, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants