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

[loop unroll] mishandling LCSSA phi of value defined above loop #1757

Closed
isanbard opened this issue May 5, 2007 · 7 comments
Closed

[loop unroll] mishandling LCSSA phi of value defined above loop #1757

isanbard opened this issue May 5, 2007 · 7 comments
Assignees
Labels
bugzilla Issues migrated from bugzilla loopoptim miscompilation

Comments

@isanbard
Copy link
Contributor

isanbard commented May 5, 2007

Bugzilla Link 1385
Resolution FIXED
Resolved on Feb 22, 2010 12:48
Version 1.9
OS All
Attachments The C code (reduced from GMP's t-pow.c file) that produces the Bus Error., LLVM's assembly output, GCC's assembly output
CC @asl

Extended Description

There seems to be a problem with how code is generated for a call after a loop is unrolled. Consider
this C code (on a PowerBook G4 machine):

typedef unsigned long int mp_limb_t;

typedef struct {
int _mp_alloc;
int _mp_size;
mp_limb_t *_mp_d;
} __mpz_struct;

typedef __mpz_struct mpz_t[1];
typedef const __mpz_struct *mpz_srcptr;
typedef __mpz_struct *mpz_ptr;

void Foo(mpz_srcptr base) {
unsigned i;
mpz_t want;
__gmpz_init(want);
for (i = 0; i < 2; i++) {
__gmpz_mul(want, want, base);
}
__gmpz_clear(want);
}

void Bar(mpz_srcptr base) {
mpz_t want;
__gmpz_init(want);
__gmpz_mul(want, want, base);
__gmpz_mul(want, want, base);
__gmpz_clear(want);
}

If you look at "testcase.llvm.s", you'll notice that, even though both Foo and Bar functions compute the
exact same thing, the Foo function, because it's a loop that's been unrolled, has an "implicit def" of R3
before the gmpz_clear call:

    mr r3, r29
    mr r4, r29
    mr r5, r30
    bl L___gmpz_mul$stub
    ;IMPLICIT_DEF_GPRC r3                                                                             
    bl L___gmpz_clear$stub

while the Bar case has:

    mr r3, r29
    mr r4, r29
    mr r5, r30
    bl L___gmpz_mul$stub
    mr r3, r29
    bl L___gmpz_clear$stub

The Foo case would be fine, if R3 wasn't trashed in the calls, but it appears to be. Though my guess is
that it probably should be marked as "clobbered" across calls. The "mpz_clear" function's in a library
and looks like this:

void mpz_clear (mpz_ptr m) {
(*__gmp_free_func) (m->_mp_d, m->_mp_alloc * BYTES_PER_MP_LIMB);
}

Now, gcc produces code for Foo that looks like this:

L2:
addi r3,r1,56
mr r5,r29
mr r4,r3
bl L___gmpz_mul$stub
addic. r30,r30,-1
bne- cr0,L2
addi r3,r1,56
bl L___gmpz_clear$stub

and Bar that looks like this:

    addi r3,r1,56
    mr r5,r29
    mr r4,r3
    bl L___gmpz_mul$stub
    addi r3,r1,56
    bl L___gmpz_clear$stub

So it's doing the correct thing. We should too.

BTW, this is the only test which failed in the GMP testsuite. Woo!

-bw

@isanbard
Copy link
Contributor Author

isanbard commented May 5, 2007

assigned to @lattner

@asl
Copy link
Collaborator

asl commented May 5, 2007

Bill, thanks for tracking this!

This test fails for me (x86/linux) too. Probably due to same reason.

@isanbard
Copy link
Contributor Author

isanbard commented May 5, 2007

No prob :-)

It's pretty onerous. It looks like we are marking the R3 register as being clobbered. I now think it's some
strange loop weirdness. I'm not skilled enough with bugpoint to get it to widdle this down to the pass that
could be causing the problem.

-bw

@asl
Copy link
Collaborator

asl commented May 5, 2007

Note, that I'm on x86 :) So, this seems to be some common codegen weirdness.

@lattner
Copy link
Collaborator

lattner commented May 5, 2007

This is a bug in loop unroll rewriting LCSSA phi nodes.

@lattner
Copy link
Collaborator

lattner commented May 5, 2007

Fixed, patch here:
http://lists.cs.uiuc.edu/pipermail/llvm-commits/Week-of-Mon-20070430/049154.html

testcase here: Transforms/LoopUnroll/2007-05-05-UnrollMiscomp.ll

-Chris

@isanbard
Copy link
Contributor Author

isanbard commented May 5, 2007

Quicker next time!

;-)

Thanks!
-bw

@llvmbot llvmbot transferred this issue from llvm/llvm-bugzilla-archive Dec 3, 2021
This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bugzilla Issues migrated from bugzilla loopoptim miscompilation
Projects
None yet
Development

No branches or pull requests

3 participants