public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
From: "beschindler at gmail dot com" <gcc-bugzilla@gcc.gnu.org>
To: gcc-bugs@gcc.gnu.org
Subject: [Bug c++/62080] New: Suboptimal code generation with eigen library
Date: Sun, 10 Aug 2014 10:20:00 -0000	[thread overview]
Message-ID: <bug-62080-4@http.gcc.gnu.org/bugzilla/> (raw)

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=62080

            Bug ID: 62080
           Summary: Suboptimal code generation with eigen library
           Product: gcc
           Version: 4.8.3
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: beschindler at gmail dot com

Created attachment 33281
  --> https://gcc.gnu.org/bugzilla/attachment.cgi?id=33281&action=edit
Source code used to get the provided assembly

I'm currently optimizing some code using the eigen library and I'm stumbling
over an interesting problem. 
I have a function, which I wrote in two different ways (the attributes are
there to provide some optimization barriers, dimEigen is a member variable of
the containing class): 


void eigenClamp(Eigen::Vector4i& vec) __attribute__((noinline, noclone))
{
    vec = vec.array().min(dimEigen.array()).max(Eigen::Array4i::Zero());
}

void eigenClamp2(Eigen::Vector4i& vec) __attribute__((noinline, noclone))
{
    vec = vec.array().min(dimEigen.array());
    vec = vec.array().max(Eigen::Array4i::Zero());
}

I'm compiling this on a core i7 920 using -O2 -fno-exceptions -fno-rtti
-std=c++11 -march=native

The first function generates this assembly, which looks great: 

movdqu    (%rsi), %xmm1
movdqu    (%rdi), %xmm0
pminsd    %xmm1, %xmm0
pxor    %xmm1, %xmm1
pmaxsd    %xmm1, %xmm0
movdqa    %xmm0, (%rsi)

The second version does this: 

movdqa    (%rsi), %xmm0
pminsd    (%rdi), %xmm0
movdqa    %xmm0, (%rsi) <-- 
pxor    %xmm0, %xmm0
movdqu    (%rsi), %xmm1 <-- 
pmaxsd    %xmm1, %xmm0
movdqa    %xmm0, (%rsi)

It seems, because there are two lines in the original source code, the result
of the first expression is written to memory and then two instructions later,
read back from memory. This makes this function almost 50% slower in what I can
measure. As I find the latter code much easier to read as the former, it would
be great if the same assembly would be generated. 

Also, I note that in the second version, the pminsd is executed directly from
the memory source, while in the first version, it is read to a register and
then pminsd is called. Thus, I'd love to see this code: 

movdqu    (%rsi), %xmm1
pminsd    (%rdi), %xmm1
pxor    %xmm1, %xmm1
pmaxsd    %xmm1, %xmm0
movdqa    %xmm0, (%rsi)

As a reference, I'm attaching the complete source code and the generated
assembly


             reply	other threads:[~2014-08-10 10:20 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2014-08-10 10:20 beschindler at gmail dot com [this message]
2014-08-10 10:21 ` [Bug c++/62080] " beschindler at gmail dot com
2014-08-10 11:30 ` glisse at gcc dot gnu.org
2014-08-10 12:05 ` beschindler at gmail dot com
2014-08-11 19:16 ` beschindler at gmail dot com
2014-08-11 20:40 ` glisse at gcc dot gnu.org
2014-08-27  9:58 ` rguenth at gcc dot gnu.org
2020-04-06  9:43 ` [Bug middle-end/62080] " pinskia at gcc dot gnu.org
2020-04-06 10:34 ` glisse at gcc dot gnu.org
2021-07-18 21:18 ` pinskia at gcc dot gnu.org

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=bug-62080-4@http.gcc.gnu.org/bugzilla/ \
    --to=gcc-bugzilla@gcc.gnu.org \
    --cc=gcc-bugs@gcc.gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).