• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
Aressera

Template function won't inline

5 posts in this topic

I'm writing a wrapper for various SSE and Altivec intrinsics and I've run into a problem when dealing with _mm_shuffle_ps(). This intrinsic requires an immediate argument to determine which elements from each vector are chosen.

Since this value is needed at compile time, I decided to use a template function:

[source]
template < int i1, int i2, int i3, int i4 >
FORCE_INLINE SIMDFloat4 shuffle( const SIMDFloat4& a, const SIMDFloat4& b )
{
return SIMDFloat4( _mm_shuffle_ps( a, b, _MM_SHUFFLE( i4, i3, i2, i1 ) ) );
}
[/source]

This compiles and works just fine, but when I examine assembly code produced by using this function, it doesn't get inlined. This is obviously unacceptable for SSE code whose whole purpose is to be as fast as possible, especially considering that this method should reduce to only 1 or 2 instructions at most.

Does anyone have an idea why GCC would not inline this method, even though I've specified for it to always be inlined using compiler attributes? I don't want to have to use any macros if I don't have to...
0

Share this post


Link to post
Share on other sites
OK, well I figured out what is causing it to not be inlined. The method in question is declared as a friend in my SIMDFloat4 class. When I remove the friend declaration, the method is then inlined correctly.

Any clues why this is? The other methods that are declared as friends are inlined properly, though they are not template methods.
0

Share this post


Link to post
Share on other sites
Which version of GCC are you using? What flags are you passing? Can you create a minimal example?

Is this representative?
[code]

#include <iostream>

class Foo
{
public:
Foo(int n) : n(n)
{
}

private:
int n;

template<int i>
friend void frobnicate(const Foo & foo) {
std::cout << (foo.n * i) << '\n';
}

};

int main() {
Foo foo(42);
frobnicate<13>(foo);
}

[/code]
My gcc version 4.6.1 inlines this without any special keywords at optimisation level 3. If I use __attribute__((force_inline)), it inlines the call even with no optimisations specified.
0

Share this post


Link to post
Share on other sites
[quote name='Aressera' timestamp='1327551363' post='4906321']
This is obviously unacceptable for SSE code whose whole purpose is to be as fast as possible, especially considering that this method should reduce to only 1 or 2 instructions at most.

[/quote]

Incorrect. The aim is to get your code decoded into microcode as quickly as possible, and then to keep it instruction cache for as long as possible. Possible guesstimations:

* Remove the explicit construction of SIMDFloat within the method. This is just dumb....

return SIMDFloat4( _mm_shuffle_ps( a, b, _MM_SHUFFLE( i4, i3, i2, i1 ) ) );

It implies SIMDFloat has a ctor that takes an __m128, so why on earth are you not just using that directly? (rather than inserting an additional copy ctor in there!?!?!). It can simply be:


return _mm_shuffle_ps( a, b, _MM_SHUFFLE( i4, i3, i2, i1 ) );

* Is [color=#000000][font=Consolas,]SIMDFloat4 declared as a DLL exported class? If it is, there's nothing you can do to inline any of it because you have told the compiler to ALWAYS extract the methods from a DLL at runtime, and so it will never inline. DLL export always takes priority. (the same well be true for shared objects).

* is [/font][/color]shuffle declared as part of a DLL exported class? (or a part of a shared object?)

* Are you storing a function pointer to the inlined method somewhere?
0

Share this post


Link to post
Share on other sites
[quote]especially considering that this method should reduce to only 1 or 2 instructions at most.[/quote]

It might, in ideal case.

But number of registers is limited. Instruction cache is small. Passing by const reference instead of by value (remember that SSE types fit into single register, so usual heuristics don't apply) might require compile to keep variables in memory rather than in registers. There might be scheduling or pipelining conflicts. Returned value might not be aligned properly, so it requires more than a single assignment. Aliasing of input parameters might matter (not sure how compiler approaches this for SSE). And if SIMDFloat4 is anything but a typedef for __m128, then a whole lot of other issues might arise.

Experiments were done on optimal instruction sequences. The most they came up with was about 10 instructions or so, being an NP class problem, more simply took too long to compute. So unless you have a cluster and centuries of time, your compiler needs to use certain heuristics and emit a good guess on what the code should look like.
0

Share this post


Link to post
Share on other sites
Well it turns out that all I had to do was add the force-inline directive to the friend declaration and things were inlined without a hitch. I guess that GCC looks at the first declaration of a method when determining its attributes, rather than all of them.

[quote name='RobTheBloke' timestamp='1327586820' post='4906414']
* Remove the explicit construction of SIMDFloat within the method. This is just dumb....

return SIMDFloat4( _mm_shuffle_ps( a, b, _MM_SHUFFLE( i4, i3, i2, i1 ) ) );

It implies SIMDFloat has a ctor that takes an __m128, so why on earth are you not just using that directly? (rather than inserting an additional copy ctor in there!?!?!). It can simply be:

return _mm_shuffle_ps( a, b, _MM_SHUFFLE( i4, i3, i2, i1 ) );

* Is [color=#000000][font=Consolas,]SIMDFloat4 declared as a DLL exported class? If it is, there's nothing you can do to inline any of it because you have told the compiler to ALWAYS extract the methods from a DLL at runtime, and so it will never inline. DLL export always takes priority. (the same well be true for shared objects).

* is [/font][/color]shuffle declared as part of a DLL exported class? (or a part of a shared object?)

* Are you storing a function pointer to the inlined method somewhere?
[/quote]

None of these should be (and aren't) an issue. The class is a header-only template class and all of the methods/constructors are force-inlined. It is very advantageous to wrap the __m128 type in a class, it allows for much nicer syntax and automatic construction from various sources (a single float, a pointer to floats, 4 floats, etc.). I also get operator overloading, plus other methods for indexing vector components, etc. I also don't have to remember all of the intrinsic names for general use. Intel has its own set of classes that do this that come with its compiler. Working with raw __m128 is probably more trouble than it's worth. A properly written wrapper can be just as fast.
0

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0