vendredi 10 avril 2020

Is there a way to check whether C++ lambda functions are inlined by the compiler?

I'm programming with C++ lambdas. For performance reason, I want to make sure that calling to a lambda is inlined by the compiler. For example I have this simplified piece of code:

template <typename T>
auto gen_fn1(T x1, T x2) {
    auto fn1 = [x1, x2]() {
        return x1 + x2;
    };
    return fn1;
}

template <typename T>
auto gen_fn2(T x1, T x2) {
    auto fn2 = [x1, x2]() {
        auto fn1 = gen_fn1(x1, x2);
        return fn1() * fn1();
    };
    return fn2;
}

int test_1() {
    auto fn2 = gen_fn2(1, 2);
    return fn2();
}

I want to make sure there is no extra cost introduced by the lambda generation and invocation in test_1(). I can manually check the assembly code generated by the compile. With '-O2' optimization of clang++8, I can see the desired result: pretty much just a 'return 9' in generated code. So my question is: is there a way to automatically check that I can always get the desired result? In particular, I want to check:

  1. No method invocation for generating the lambdas in 'test_1()', including 'gen_fn2()' and 'gen_fn1()'.
  2. No lambda invocation cost in 'test_1()' or 'gen_fn2()', like 'fn1()' and 'fn2()'. I expect they can be inlined. So how to identify them and check they are inlined?

Question 2 is more interesting to me.   Be able to check it in a programmable way is most appreciated, like 'assert(gen_fn2(1, 2) == ()[]{ return 9; }'. If not possible, check the intermediate file of the compiler is also helpful, or the assembly file. But how?

Aucun commentaire:

Enregistrer un commentaire