Can a compiler optimize two atomic loads?

Will two loads be combined with one in such scenarios? If it depends on the architecture, what will happen in the case of modern processors from Intel? I believe that atomic loads are equivalent to normal loads in Intel processors.

void run1() {
    auto a = atomic_var.load(std::memory_order_relaxed);
    auto b = atomic_var.load(std::memory_order_relaxed);
   // Some code using a and b;
}

void run2() {
    if (atomic_var.load(std::memory_order_relaxed) == 2 && /*some conditions*/ ...) {
         if (atomic_var.load(std::memory_order_relaxed) * somevar > 3) {
               /*...*/
         }
    }
}

run1()and run2()are just two scenarios using two loads of the same atomic variable. Can the compiler collapse such two load scenarios into one load and reuse it?

+4
source share
2 answers

Can the compiler optimize remote atomic loads?

run1()

void run1() {
    auto a = atomic_var.load(std::memory_order_relaxed);
    auto b = a;
   // Some code using a and b;
}

atomic_var , run1(). load() .

, load(). , .

run2() . /*some conditions*/. -, (, ..), . .

?

. , , . , . , . N4455 No Sane Compiler Atomics .

GCC clang load() .

+3

GCC (6.3), Clang (3.9) .

- : https://godbolt.org/g/nZ3Ekm

+4

Source: https://habr.com/ru/post/1667604/


All Articles