Would C++ exception stops function from being inlined?
Asked Answered
H

3

12

Suppose I have a very simple function that I expect the compiler to inline it. But I may need to throw exception on seeing some invalid input, would that stop the compiler from inlining the function?

Haiku answered 21/9, 2011 at 3:53 Comment(7)
Short answer: No. Inlining is independent of exception handling since it's just a copy/paste of the function in the caller.Ule
If I compile with exception handling turned off, I get rid of a bunch of warnings, all looking like this: ... Inlining of specified subprogram failed due to the presence of a C++ exception handler: utills::ScopeFile<utills::MpiAbbortOnError>::~ScopeFile(). Notice that the thing that it complains about is not even an exception, but it seems the destructors have to be treated in the same manner. These warnings appear during the linking phase, when I do xlC object1.o object2.o -o my_binary (xlC is the IBM compiler).Blossomblot
I thought it is some kind of a specific IBM compiler problem, but in a brief (unfortunately) conversation with a person way more enlightened that I am I was convinced that it is a generic problem. Unfortunately since I am not as enlightened I am not able to lay out the arguments, but I believe the key words where "elimination of stack" which is not possible in case you have some kind of objects like exceptions. I'd be very-very interested to read a detailed explanation of that. And my application speed doulbes (this is not exaggeration), when I disable exception handling.Blossomblot
I have to mention, that the application is quite niche: it performs a simulation of a specific physical process (diffusion) and it spends 90% of time in a small loop no bigger, than 100 lines of code. So another short answer: yes it may prevent the inlining. I do have evidence of it at hand. And even more it may be a move fundamental problem, not related to a specific compiler.Blossomblot
@Ule I'd be really glad to hear what do you think about it.Blossomblot
@mezhaka I'm not sure myself since I never have exception handling in performance critical code. But if I had to guess, it's probably just a compiler-specific limitation. Either that, or the compiler is OCD about preserving an exception stack-trace that is true to the original code. If the function is inlined, then the stack-trace will be missing the inlined calls. (There might be a way to get them back without a performance hit. But I'm not too familiar with how exception handlers walk the stack.)Ule
Fast forward to 1:07:00 to listen about compiler optimizations and exceptions: channel9.msdn.com/Events/GoingNative/GoingNative-2012/…Blossomblot
G
10

A compiler can refuse to inline for any reason. gcc lists reasons why it might not inline a function, but exception throwing is not among them. Also, the option -Winline will cause the compiler to issue a warning if it can't inline a function that you marked as inline. You can try that and see if you are doing anything to prevent inlining.

Gusset answered 21/9, 2011 at 4:2 Comment(0)
L
6

It is perfectly reasonable and valid for a compiler to inline a function that throws.

Lyly answered 21/9, 2011 at 3:55 Comment(0)
P
1

I've just run into this situation with MSVC version 19. A function that throws an exception would not get inlined when compiling for x86. If I replace throw with exit(1) or if I compile for x86-64, it gets inlined just fine.

Provincialism answered 12/11, 2017 at 5:18 Comment(1)
The behaviour not to inline a function with a try statement seems to be documented here: learn.microsoft.com/en-us/cpp/error-messages/compiler-warnings/…Ascender

© 2022 - 2024 — McMap. All rights reserved.