B
Boon
Hello,
I'm involved in a discussion on comp.programming.threads regarding optimizing
compilers reordering statements.
Consider the following code.
struct foo { int a, b, c; };
int f1(int);
int f2(int);
int f3(int);
void set_foo(struct foo *bar)
{
bar->a = f1(bar->a);
bar->b = f2(bar->b);
bar->c = f3(bar->c);
}
As far as I understand, an optimizing compiler may determine that each statement
modifies a different field in the structure, and said compiler may schedule each
operation in any order.
It would be conceivable for a compiler to generate code that executes f1(bar->a)
on one co-processor, f2(bar->b) on another, and f3(bar->c) on yet another, then
stores the results sequentially on a "master" CPU.
temp1 = f1(bar->a); /* on copro1 */
temp2 = f2(bar->b); /* on copro2 */
temp3 = f3(bar->c); /* on copro3 */
/* the three above statements are executed in parallel */
bar->c = temp3;
bar->b = temp2;
bar->a = temp1;
/* the three above statements are executed sequentially */
AFAICT, this would be valid, right?
This comes from a discussion regarding semaphores. I don't understand how
optimizing C compilers deal with semaphores.
Consider
struct foo
{
semaphore_t sem;
int i;
};
void increment(struct foo *bar)
{
sem_lock(&bar->sem);
++bar->i;
sem_unlock(&bar->sem);
}
Someone writing this code expects their compiler to output code with no
reordering allowed.
"i" must be incremented after sem_lock() is called, and before sem_unlock() is
called. How is an optimizing compiler made aware that the increment statement
may not be reordered? Where is the magic?
Regards.
I'm involved in a discussion on comp.programming.threads regarding optimizing
compilers reordering statements.
Consider the following code.
struct foo { int a, b, c; };
int f1(int);
int f2(int);
int f3(int);
void set_foo(struct foo *bar)
{
bar->a = f1(bar->a);
bar->b = f2(bar->b);
bar->c = f3(bar->c);
}
As far as I understand, an optimizing compiler may determine that each statement
modifies a different field in the structure, and said compiler may schedule each
operation in any order.
It would be conceivable for a compiler to generate code that executes f1(bar->a)
on one co-processor, f2(bar->b) on another, and f3(bar->c) on yet another, then
stores the results sequentially on a "master" CPU.
temp1 = f1(bar->a); /* on copro1 */
temp2 = f2(bar->b); /* on copro2 */
temp3 = f3(bar->c); /* on copro3 */
/* the three above statements are executed in parallel */
bar->c = temp3;
bar->b = temp2;
bar->a = temp1;
/* the three above statements are executed sequentially */
AFAICT, this would be valid, right?
This comes from a discussion regarding semaphores. I don't understand how
optimizing C compilers deal with semaphores.
Consider
struct foo
{
semaphore_t sem;
int i;
};
void increment(struct foo *bar)
{
sem_lock(&bar->sem);
++bar->i;
sem_unlock(&bar->sem);
}
Someone writing this code expects their compiler to output code with no
reordering allowed.
"i" must be incremented after sem_lock() is called, and before sem_unlock() is
called. How is an optimizing compiler made aware that the increment statement
may not be reordered? Where is the magic?
Regards.