Problem with functions not inlining

Discussion of chess software programming and technical issues.

Moderators: bob, hgm, Harvey Williamson

Forum rules
This textbox is used to restore diagrams posted with the [d] tag before the upgrade.
User avatar
Greg Strong
Posts: 388
Joined: Sun Dec 21, 2008 5:57 pm
Location: Washington, DC

Problem with functions not inlining

Post by Greg Strong » Wed Nov 04, 2009 12:26 am

I'm using SSE2 functions for what was supposed to be very fast pawn structure analysis, and it has turned out to be quite slow. Now I think I've tracked down the problem, but I don't know what to do about it. I think the problem is that even the smallest trivial functions are not being inlined, despite my best efforts (like making sure that agressive inlining is turned on in the project settings, using the __inline keyword, etc.)

I've encapsulated the __128i data type for clarity. Here's a fragment of the class, with a very simple function that seems to me like it should inline...

Code: Select all

class xmm128i
{
  private:
	__m128i m_data;

  public:
	xmm128i( __m128i *source )
	{ m_data = _mm_load_si128( source ); }

	xmm128i( __m128i source ): m_data(source) { }

	xmm128i( xmm const &source )
	{ _mm_load_si128( &source.As128i ); }

	~xmm128i() { }

	operator __m128i()
	{ return m_data; }


	// very simple operator ...

	xmm128i operator &( __m128i const &other )
	{ return xmm128i( _mm_and_si128( m_data, other ) ); }

	// many more operators ...
}
The operator & is (intented to be) a totally trivial wrapper around the _mm_and_si128 compiler intrinsic. I just can't imagine why it refuses to inline, but with me using these operators frequently and every one adding the overhead of a function call, the code that should be fast is winding up being very, very slow...

Does anyone have any idea what's going on?!? I'm using Visual C++ 2008.

Thanks for any help you can provide. I'm rather frustrated :)

User avatar
michiguel
Posts: 6388
Joined: Thu Mar 09, 2006 7:30 pm
Location: Chicago, Illinois, USA
Contact:

Re: Problem with functions not inlining

Post by michiguel » Wed Nov 04, 2009 12:36 am

Greg Strong wrote:I'm using SSE2 functions for what was supposed to be very fast pawn structure analysis, and it has turned out to be quite slow. Now I think I've tracked down the problem, but I don't know what to do about it. I think the problem is that even the smallest trivial functions are not being inlined, despite my best efforts (like making sure that agressive inlining is turned on in the project settings, using the __inline keyword, etc.)

I've encapsulated the __128i data type for clarity. Here's a fragment of the class, with a very simple function that seems to me like it should inline...

Code: Select all

class xmm128i
{
  private:
	__m128i m_data;

  public:
	xmm128i( __m128i *source )
	{ m_data = _mm_load_si128( source ); }

	xmm128i( __m128i source ): m_data(source) { }

	xmm128i( xmm const &source )
	{ _mm_load_si128( &source.As128i ); }

	~xmm128i() { }

	operator __m128i()
	{ return m_data; }


	// very simple operator ...

	xmm128i operator &( __m128i const &other )
	{ return xmm128i( _mm_and_si128( m_data, other ) ); }

	// many more operators ...
}
The operator & is (intented to be) a totally trivial wrapper around the _mm_and_si128 compiler intrinsic. I just can't imagine why it refuses to inline, but with me using these operators frequently and every one adding the overhead of a function call, the code that should be fast is winding up being very, very slow...

Does anyone have any idea what's going on?!? I'm using Visual C++ 2008.

Thanks for any help you can provide. I'm rather frustrated :)
Can you make a macro of the whole thing? Ugly, but...

Miguel

Dann Corbit
Posts: 10124
Joined: Wed Mar 08, 2006 7:57 pm
Location: Redmond, WA USA
Contact:

Re: Problem with functions not inlining

Post by Dann Corbit » Wed Nov 04, 2009 1:46 am

Greg Strong wrote:I'm using SSE2 functions for what was supposed to be very fast pawn structure analysis, and it has turned out to be quite slow. Now I think I've tracked down the problem, but I don't know what to do about it. I think the problem is that even the smallest trivial functions are not being inlined, despite my best efforts (like making sure that agressive inlining is turned on in the project settings, using the __inline keyword, etc.)

I've encapsulated the __128i data type for clarity. Here's a fragment of the class, with a very simple function that seems to me like it should inline...

Code: Select all

class xmm128i
{
  private:
	__m128i m_data;

  public:
	xmm128i( __m128i *source )
	{ m_data = _mm_load_si128( source ); }

	xmm128i( __m128i source ): m_data(source) { }

	xmm128i( xmm const &source )
	{ _mm_load_si128( &source.As128i ); }

	~xmm128i() { }

	operator __m128i()
	{ return m_data; }


	// very simple operator ...

	xmm128i operator &( __m128i const &other )
	{ return xmm128i( _mm_and_si128( m_data, other ) ); }

	// many more operators ...
}
The operator & is (intented to be) a totally trivial wrapper around the _mm_and_si128 compiler intrinsic. I just can't imagine why it refuses to inline, but with me using these operators frequently and every one adding the overhead of a function call, the code that should be fast is winding up being very, very slow...

Does anyone have any idea what's going on?!? I'm using Visual C++ 2008.

Thanks for any help you can provide. I'm rather frustrated :)
You can almost always make something inline with __forceinline:
http://msdn.microsoft.com/en-us/library/z8y1yy88.aspx

However, it isn't always good to inline. If the code spills the cache because if inlining, it may well run slower instead of faster.

jwes
Posts: 778
Joined: Sat Jul 01, 2006 5:11 am

Re: Problem with functions not inlining

Post by jwes » Wed Nov 04, 2009 3:37 am

You are compiling a release version.

Gerd Isenberg
Posts: 2127
Joined: Wed Mar 08, 2006 7:47 pm
Location: Hattingen, Germany

Re: Problem with functions not inlining

Post by Gerd Isenberg » Wed Nov 04, 2009 4:54 am

Greg Strong wrote:I'm using SSE2 functions for what was supposed to be very fast pawn structure analysis, and it has turned out to be quite slow. Now I think I've tracked down the problem, but I don't know what to do about it. I think the problem is that even the smallest trivial functions are not being inlined, despite my best efforts (like making sure that agressive inlining is turned on in the project settings, using the __inline keyword, etc.)

I've encapsulated the __128i data type for clarity. Here's a fragment of the class, with a very simple function that seems to me like it should inline...

Code: Select all

class xmm128i
{
  private:
	__m128i m_data;

  public:
	xmm128i( __m128i *source )
	{ m_data = _mm_load_si128( source ); }

	xmm128i( __m128i source ): m_data(source) { }

	xmm128i( xmm const &source )
	{ _mm_load_si128( &source.As128i ); }

	~xmm128i() { }

	operator __m128i()
	{ return m_data; }


	// very simple operator ...

	xmm128i operator &( __m128i const &other )
	{ return xmm128i( _mm_and_si128( m_data, other ) ); }

	// many more operators ...
}
The operator & is (intented to be) a totally trivial wrapper around the _mm_and_si128 compiler intrinsic. I just can't imagine why it refuses to inline, but with me using these operators frequently and every one adding the overhead of a function call, the code that should be fast is winding up being very, very slow...

Does anyone have any idea what's going on?!? I'm using Visual C++ 2008.

Thanks for any help you can provide. I'm rather frustrated :)
I don't have that problem, everything inlines very well with vc2008 release version. I have memory layout in a base class, binary operators as friends, and combined assignment ops returning a reference and no explicit destructor:

Code: Select all

class mm128i : public DBB {

   friend xmm128i operator& (const xmm128i &a, const xmm128i &b) {
      return xmm128i (_mm_and_si128(a.m_data, b.m_data));
   }


   xmm128i & operator &=( mm128i const &other ) {
      m_data = _mm_and_si128( m_data, other.m_data ); 
      return *this;
   }
};
Also all parameter are all const ref xmm128i rather than __m128i.

User avatar
Greg Strong
Posts: 388
Joined: Sun Dec 21, 2008 5:57 pm
Location: Washington, DC

Re: Problem with functions not inlining

Post by Greg Strong » Wed Nov 04, 2009 12:21 pm

I tried __forceinline and that didn't work either. Also, __forceinline is supposed to throw a warning if a function can't be inlined, but it doesn't do that either. I'm beginning to think my installation of Visual Studio is messed up.

Sven
Posts: 3826
Joined: Thu May 15, 2008 7:57 pm
Location: Berlin, Germany
Full name: Sven Schüle
Contact:

Re: Problem with functions not inlining

Post by Sven » Thu Nov 05, 2009 8:30 am

Greg Strong wrote:I tried __forceinline and that didn't work either. Also, __forceinline is supposed to throw a warning if a function can't be inlined, but it doesn't do that either. I'm beginning to think my installation of Visual Studio is messed up.
Maybe not your installation but your project settings? Have you set inlining to "all suitable"?

Sven

User avatar
Greg Strong
Posts: 388
Joined: Sun Dec 21, 2008 5:57 pm
Location: Washington, DC

Re: Problem with functions not inlining

Post by Greg Strong » Thu Nov 05, 2009 1:11 pm

Sven Schüle wrote:
Greg Strong wrote:I tried __forceinline and that didn't work either. Also, __forceinline is supposed to throw a warning if a function can't be inlined, but it doesn't do that either. I'm beginning to think my installation of Visual Studio is messed up.
Maybe not your installation but your project settings? Have you set inlining to "all suitable"?

Sven
Yes, it's set that way. And I did a clean install on another machine. That didn't work either. I just can't imagine what is going on.

steffan
Posts: 28
Joined: Sat Mar 11, 2006 11:08 pm
Location: Midlands, England

Re: Problem with functions not inlining

Post by steffan » Thu Nov 05, 2009 4:05 pm

Are you certain your project settings are set for "release" rather than "debug"? From what I remember of Visual Studio, debug effectively turns off optimisations, including inlining.

User avatar
Greg Strong
Posts: 388
Joined: Sun Dec 21, 2008 5:57 pm
Location: Washington, DC

Re: Problem with functions not inlining

Post by Greg Strong » Thu Nov 05, 2009 4:08 pm

Yes, definitely release build for X64 platform ...

Post Reply