Sign in to follow this  
deffer

xmintrin + vs2005ee + debug = visual studio BUG [reported]

Recommended Posts

Quickly to the point: why the following code is crashing? main.cpp
#include <xmmintrin.h>

#define ALGN __declspec(align(16))

//ALGN // doesn't matter
struct vec4
{
  vec4() {};
  vec4(const __m128 & x)
    : m_x(x)
  {};

  __m128 m_x;
};
inline __m128 r(const vec4 & v) { return v.m_x; }


inline
void f2(const vec4 & q1)
{
  vec4 temp = r(q1);
};

void f1(const vec4 & q1)
{
  f2(q1); // HERE it crashes!
};

int main()
{
  vec4 q1;
  f1(q1);
  return 0;
}





Compiler: vs2005ee Project: new empty project, then add the above 'main.cpp' Settings: Debug Settings changes: 1. C/C++ -> Optimization -> Optimization -> Disabled (/Od) // EDIT: could also be "Custom" 2. C/C++ -> Optimization -> Iniline Function Expansion -> Only __inline (/Ob1) Well, I know why it's crashing. At the marked point, the generated assembly does not care about 16-byte alignment of temporary variables.
00401020  movaps      xmmword ptr [ebp-18h],xmm0 // A
00401024  movaps      xmm0,xmmword ptr [ebp-18h] 
00401028  movaps      xmmword ptr [ebp-30h],xmm0 // B
0040102C  movaps      xmm0,xmmword ptr [ebp-30h] 




One of the instructions, A or B must be invalid and cause a crash. And it is. The first one. So, my question is quite obvious now: Where the heck did I do something wrong? In case nowhere, and it's compiler's fault, how about other compilers eating this - are they also producing stupid code (doubtful)? And what do I do about it... [Edited by - deffer on September 8, 2006 8:56:17 AM]

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
well i cannot reproduce the error. i'm using VC Version 14.00.50727.42, though. Using the same settings you described the app doesn't crash. also, asm output seems quite sane. the body of f1() basically becomes:

mov eax, DWORD PTR _q1$[ebx]
movaps xmm0, XMMWORD PTR [eax]
movaps XMMWORD PTR $T5819[ebp], xmm0
movaps xmm0, XMMWORD PTR $T5819[ebp]
movaps XMMWORD PTR $T5825[ebp], xmm0
movaps xmm0, XMMWORD PTR $T5825[ebp]
movaps XMMWORD PTR _temp$5824[ebp], xmm0

f2() is inlined correctly.

Share this post


Link to post
Share on other sites
I tried again and I can reproduce the error at any time.

My version info:

Microsoft Visual Studio 2005
Version 8.0.50727.42 (RTM.050727-4200)
Microsoft .NET Framework
Version 2.0.50727

Installed Edition: VC Express

Microsoft Visual C++ 2005 76542-000-0000011-00125
Microsoft Visual C++ 2005


--------
AP: "14.00.50727.42" - what verson is this, exactly? vs2003 ?

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Hi deffer,

no, 14.00.50727.42 is the RTM version of visual c++ 2005 optimizing compiler for 32 bit.

Microsoft Visual C++ 2005 77642-113-3000004-41614

i don't know if vc++ express uses the same compiler, but i very much suspect so. you can verify that by executing cl.exe from the visual c++ command prompt or from within the visual studio/vc/bin/ directory.

Share this post


Link to post
Share on other sites
I tweaked my project's code to avoid the error, so it doesn't bother my anymore (for now, at least).

Now I'm just courious:
Can this be considered a vs bug? And if it can, where can I report it?

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
hi deffer,

the right place to let the vs team know of the bug / complaint is right here: http://connect.microsoft.com/feedback/default.aspx?SiteID=210
i'll investigate your problem a bit later today. as i said, i couldn't reproduce the bug.. but i'll check again and make sure i repeat your configuration steps again.
if you post the bug at the ms connect site (please do, if you're certain it is a problem) i'll vote for it, too, if i could reproduce the problem.

also, does the app crash if /Ob1 is not set? maybe a clash of debug vs. /Ob1 ?

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
just a quick note: the bug has not been posted yet - again, if it is a bug. would be interesting of what others say about this.

Share this post


Link to post
Share on other sites
Quote:
Original post by Anonymous Poster
also, does the app crash if /Ob1 is not set? maybe a clash of debug vs. /Ob1 ?


No, it doesn't crash then. The whole point seems to be when combining two wanna-be-aligned functions.

About the posible clash: you could change
"C/C++ -> Optimization -> Optimization -> Disabled (/Od)"
into
"C/C++ -> Optimization -> Optimization -> Custom"
with the same effect, so it's not really about /Od.

<opinion>
I don't think "debug" is incompatybile with "only inline". I find it necessary to debug application with inlining turned on (at least with __forceinline). Otherwise many complicated expressions using tiny helper functions can bring one's app to a crawl.
</opinion>

EDIT:
Managed to simplify the sample code a bit:
#include <xmmintrin.h>

inline
__m128 cp(const __m128 & m) { return m; };

inline
void g2(const __m128 & m)
{
__m128 temp = cp(cp(m));
};
void g1(const __m128 & m)
{
g2(m);
};
int main()
{
__m128 m;
g1(m);
return 0;
}


[Edited by - deffer on September 8, 2006 7:11:09 AM]

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
i could reproduce the bug now. as you said, if you compile your source with debug and no inlining everything works fine. if you turn on "only __inline" with /Od1 then the app crashes with an access violation.
the assembly is exactly the one you posted, deffer.

00402C9A mov eax,dword ptr [ebx+8]
00402C9D movaps xmm0,xmmword ptr [eax]
00402CA0 movaps xmmword ptr [ebp-18h],xmm0
00402CA4 movaps xmm0,xmmword ptr [ebp-18h]
00402CA8 movaps xmmword ptr [ebp-40h],xmm0
00402CAC movaps xmm0,xmmword ptr [ebp-40h]
00402CB0 movaps xmmword ptr [ebp-50h],xmm0
00402CB4 movaps xmm0,xmmword ptr [ebp-50h]
00402CB8 movaps xmmword ptr [ebp-30h],xmm0

do you want to post that and your test source to the ms connect site, deffer? i would vote your bug report there.

still it would be interesting to have someone with vc7.1 or 7 test this thing..

if it's a compiler bug, i congratulate, deffer :-)

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