NO

Author Topic: Optimization gets too enthusiastic when it sees memcpy (or CopyMemory)  (Read 2987 times)

Wurstgeist

  • Guest
When I think I see a bug in pocc it's usually me misunderstanding something. But this one has enough weirdness that I'll risk submitting it.

Code: [Select]
// This uses a function to put some values into a local array, via a global array.
// Then it prints out the same cell from both. They should contain the same values.

// When compiled with the default "optimizations: maximize speed",
// the global array is bypassed and retains its initial values.

#include <stdio.h>
#include <string.h>

float global_values[5] = {1, 2, 3, 4, 5}; // Effect doesn't happen if array is size 4 or less.

// Not sure if it has to be floats, haven't tried with ints.

static void optim_bug_test(float *);

int main(void)
{
float local_values[5];
optim_bug_test(local_values);
printf("%f\n", global_values[0]);
printf("%f\n", local_values[0]);
return 0;
}

static void optim_bug_test(float *o)
{
float whut;

//whut = global_values[0]; // uncomment this to make things work properly.

// Compiling with "optimizations: none" also makes things work properly.

global_values[0] = 999;
global_values[1] = 998;
global_values[2] = 997;
global_values[3] = 996;
global_values[4] = 995;

// Remove the following line, and global_values gets values 999 and 998, etc., as above,
// presumably because without memcpy there's no optimization,
// but with memcpy, the step where the values get assigned is optimized away.

memcpy(o, global_values, 5 * sizeof(float));
}

// Note: I don't think this is anything specific to do with globals,
// because I first observed it with two local arrays.

Offline frankie

  • Global Moderator
  • Member
  • *****
  • Posts: 2113
Re: Optimization gets too enthusiastic when it sees memcpy (or CopyMemory)
« Reply #1 on: February 10, 2016, 12:39:35 PM »
It's ok  :)
This is a well known bug due to after inlining optimizations (see also).
As a workaround use the switch /Ob0 to disable inlining.
« Last Edit: February 10, 2016, 12:42:19 PM by frankie »
"It is better to be hated for what you are than to be loved for what you are not." - Andre Gide

Snowman

  • Guest
Re: Optimization gets too enthusiastic when it sees memcpy (or CopyMemory)
« Reply #2 on: February 26, 2016, 04:30:46 PM »
When can we expect the next version of Pelles C to be released?

Edit: I realize my question above is off-topic but then again it isn't. This bug won't go away on its own.