From patchwork Thu Feb 7 01:39:11 2013 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sriraman Tallam X-Patchwork-Id: 218810 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from sourceware.org (server1.sourceware.org [209.132.180.131]) by ozlabs.org (Postfix) with SMTP id 537A02C02EA for ; Thu, 7 Feb 2013 12:39:28 +1100 (EST) Comment: DKIM? See http://www.dkim.org DKIM-Signature: v=1; a=rsa-sha1; c=relaxed/relaxed; d=gcc.gnu.org; s=default; x=1360805970; h=Comment: DomainKey-Signature:Received:Received:Received:Received: MIME-Version:Received:In-Reply-To:References:Date:Message-ID: Subject:From:To:Cc:Content-Type:Mailing-List:Precedence:List-Id: List-Unsubscribe:List-Archive:List-Post:List-Help:Sender: Delivered-To; bh=q0V/ie6qzO8DS9/6KBq0yZv/Bhs=; b=cwRKBtzMGJQu9BN witRk698YUW7p1uM/pWVJxQnIvjYXlo0qy2olJZgyiBFN4jGLmkdFbIRi7XjHea4 u40DylfbNWrivA4J8ak3nHewYvWGoEQs0wKLqay9O7RvQoAgZri9f9az3wEFo0FQ KfXCjBmbGd4W1JU2N4sP+ZWgHuxI= Comment: DomainKeys? See http://antispam.yahoo.com/domainkeys DomainKey-Signature: a=rsa-sha1; q=dns; c=nofws; s=default; d=gcc.gnu.org; h=Received:Received:X-SWARE-Spam-Status:X-Spam-Check-By:Received:Received:X-Google-DKIM-Signature:MIME-Version:X-Received:Received:In-Reply-To:References:Date:Message-ID:Subject:From:To:Cc:Content-Type:X-Gm-Message-State:X-IsSubscribed:Mailing-List:Precedence:List-Id:List-Unsubscribe:List-Archive:List-Post:List-Help:Sender:Delivered-To; b=TZi0u6/YuRUfr3q37MIMxsLQeCwEvsYI+8Gm+P//ICJLEL+cmDtHDr+Pr/ATyf s7RP8KMvSuQ/EzOSxf8iCjMHehtOMUBP+4lt1IWT8R1HxImF5G0J023zPp7fOiMS 80qFWbrKI2io6YyjeEeFzHO9+Kk/GxpAjCtlk0WwNF6oI=; Received: (qmail 13085 invoked by alias); 7 Feb 2013 01:39:19 -0000 Received: (qmail 13032 invoked by uid 22791); 7 Feb 2013 01:39:19 -0000 X-SWARE-Spam-Status: No, hits=-5.6 required=5.0 tests=AWL, BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, KHOP_RCVD_TRUST, KHOP_THREADED, RCVD_IN_DNSWL_LOW, RCVD_IN_HOSTKARMA_YE, RP_MATCHES_RCVD X-Spam-Check-By: sourceware.org Received: from mail-ob0-f182.google.com (HELO mail-ob0-f182.google.com) (209.85.214.182) by sourceware.org (qpsmtpd/0.43rc1) with ESMTP; Thu, 07 Feb 2013 01:39:12 +0000 Received: by mail-ob0-f182.google.com with SMTP id va7so2196143obc.13 for ; Wed, 06 Feb 2013 17:39:11 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20120113; h=mime-version:x-received:in-reply-to:references:date:message-id :subject:from:to:cc:content-type:x-gm-message-state; bh=Tm/J+h9mj2QAE9g6VI0Eeyra1jHp4zBceuLSdQvZvBA=; b=QWz8rZu+My6IN33laiopHZ3O9OzQukyvSTYVFMgW2nD6a6e9rO0pZT8o20JMic46rR jT+yhU4HgcaywSTGji034KmgaHmXPdcovGuyqcjdUrekhG5bldVZanNbWg0TV20MWThR tyKsFfUoYxfRRF6x9FwjbMEC94STUApTQVt6ARnQNk0KZG8f23ReS/q2iV4cclQaYPpn 2PP1oavL6woQ3V1tihxUipWhwKfs38naCb3itmzh/x37S9JbKvkKXLHtp4wX6nv2yxDC xqDb/2Kh9j75J6xsd2GnlxXBRmHD5cQsFI/BNbwb2RKKjjZ2UtdYjgurGpcA6WAbreDK WCBg== MIME-Version: 1.0 X-Received: by 10.60.169.178 with SMTP id af18mr403416oec.133.1360201151326; Wed, 06 Feb 2013 17:39:11 -0800 (PST) Received: by 10.182.75.166 with HTTP; Wed, 6 Feb 2013 17:39:11 -0800 (PST) In-Reply-To: <51092641.7080005@redhat.com> References: <20130118202236.GE7269@tucnak.redhat.com> <50FAC778.9040702@redhat.com> <20130121143248.GM7269@tucnak.redhat.com> <51092641.7080005@redhat.com> Date: Wed, 6 Feb 2013 17:39:11 -0800 Message-ID: Subject: Re: [PATCH] Multiversioning fixes (PR c++/55742, take 2) From: Sriraman Tallam To: Jason Merrill Cc: Jakub Jelinek , Richard Henderson , GCC Patches X-Gm-Message-State: ALoCoQkDdcd+6xhs63mVybgdSKJ9Fb4OgaHhJd3ODLiSPTR2etuKJGgsxLI07x6ZBa4RfIbmmLP/fWZfHuguUIUOzzpzsTDtBFSMxvD9G4IlaookvZloA6v9G7bXocolHZLTkKRL+wr5EBzvdGMlwkzEa4BuwmdIvk7dyA66RGRzzIuLk030J1f9onTI0eVRaQZM6r5CoN4X X-IsSubscribed: yes Mailing-List: contact gcc-patches-help@gcc.gnu.org; run by ezmlm Precedence: bulk List-Id: List-Unsubscribe: List-Archive: List-Post: List-Help: Sender: gcc-patches-owner@gcc.gnu.org Delivered-To: mailing list gcc-patches@gcc.gnu.org Hi, I have attached a patch documenting Function Multiversioning and added a few more tests. I have also updated the wiki http://gcc.gnu.org/wiki/FunctionMultiVersioning. Please let me know if there are any more tests you specifically want. Please review. Thanks Sri On Wed, Jan 30, 2013 at 5:55 AM, Jason Merrill wrote: > OK. Sriraman, are you working on documentation and more tests? > > Jason * doc/extend.texi: Document Function Multiversioning and "default" parameter string to target attribute. * g++.dg/ext/mv12.C: New test. * g++.dg/ext/mv13.C: New test. * g++.dg/ext/mv13.h: New file. * g++.dg/ext/mv13-aux.C: New file. * config/i386/i386.c (ix86_mangle_function_version_assembler_name): Change comment. Index: gcc/doc/extend.texi =================================================================== --- gcc/doc/extend.texi (revision 195818) +++ gcc/doc/extend.texi (working copy) @@ -3655,6 +3655,11 @@ Enable/disable the generation of the advanced bit @cindex @code{target("aes")} attribute Enable/disable the generation of the AES instructions. +@item default +@cindex @code{target("default")} attribute +@xref{Function Multiversioning}, where it is used to specify the +default function version. + @item mmx @itemx no-mmx @cindex @code{target("mmx")} attribute @@ -15215,6 +15220,7 @@ Predefined Macros,cpp,The GNU C Preprocessor}). * Bound member functions:: You can extract a function pointer to the method denoted by a @samp{->*} or @samp{.*} expression. * C++ Attributes:: Variable, function, and type attributes for C++ only. +* Function Multiversioning:: Declaring multiple function versions. * Namespace Association:: Strong using-directives for namespace association. * Type Traits:: Compiler support for type traits * Java Exceptions:: Tweaking exception handling to work with Java. @@ -15744,6 +15750,64 @@ interface table mechanism, instead of regular virt See also @ref{Namespace Association}. +@node Function Multiversioning +@section Function Multiversioning +@cindex function versions + +With the GNU C++ front end, for target i386, you may specify multiple +versions of a function, where each function is specialized for a +specific target feature. At runtime, the appropriate version of the +function is automatically executed depending on the characteristics of +the execution platform. Here is an example. + +@smallexample +__attribute__ ((target ("default"))) +int foo () +@{ + // The default version of foo. + return 0; +@} + +__attribute__ ((target ("sse4.2"))) +int foo () +@{ + // foo version for SSE4.2 + return 1; +@} + +__attribute__ ((target ("arch=atom"))) +int foo () +@{ + // foo version for the Intel ATOM processor + return 2; +@} + +__attribute__ ((target ("arch=amdfam10"))) +int foo () +@{ + // foo version for the AMD Family 0x10 processors. + return 3; +@} + +int main () +@{ + int (*p)() = &foo; + assert ((*p) () == foo ()); + return 0; +@} +@end smallexample + +In the above example, four versions of function foo are created. The +first version of foo with the target attribute "default" is the default +version. This version gets executed when no other target specific +version qualifies for execution on a particular platform. A new version +of foo is created by using the same function signature but with a +different target string. Function foo is called or a pointer to it is +taken just like a regular function. GCC takes care of doing the +dispatching to call the right version at runtime. Refer to the +@uref{http://gcc.gnu.org/wiki/FunctionMultiVersioning, GCC wiki on +Function Multiversioning} for more details. + @node Namespace Association @section Namespace Association Index: gcc/testsuite/g++.dg/ext/mv13-aux.C =================================================================== --- gcc/testsuite/g++.dg/ext/mv13-aux.C (revision 0) +++ gcc/testsuite/g++.dg/ext/mv13-aux.C (revision 0) @@ -0,0 +1,11 @@ +// Test case to check if multiversioning works as expected when the versions +// are defined in different files. Auxiliary file for mv13.C. +// { dg-do compile } + +#include "mv13.h" + +__attribute__ ((target ("sse4.2"))) +int foo () +{ + return 1; +} Index: gcc/testsuite/g++.dg/ext/mv12.C =================================================================== --- gcc/testsuite/g++.dg/ext/mv12.C (revision 0) +++ gcc/testsuite/g++.dg/ext/mv12.C (revision 0) @@ -0,0 +1,21 @@ +// Test to check if an error is generated when virtual functions +// are multiversioned. + +// { dg-do compile { target i?86-*-* x86_64-*-* } } +// { dg-options "" } +class Foo +{ + public: + /* Default version of foo. */ + __attribute__ ((target("default"))) + virtual int foo () // { dg-error "Virtual function multiversioning not supported" } + { + return 0; + } + /* corei7 version of foo. */ + __attribute__ ((target("arch=corei7"))) + virtual int foo () // { dg-error "Virtual function multiversioning not supported" } + { + return 0; + } +}; Index: gcc/testsuite/g++.dg/ext/mv13.C =================================================================== --- gcc/testsuite/g++.dg/ext/mv13.C (revision 0) +++ gcc/testsuite/g++.dg/ext/mv13.C (revision 0) @@ -0,0 +1,22 @@ +// Test case to check if multiversioning works as expected when the versions +// are defined in different files. + +// { dg-do run { target i?86-*-* x86_64-*-* } } +// { dg-require-ifunc "" } +// { dg-options "-O2" } +// { dg-additional-sources "mv13-aux.C" } + +#include "mv13.h" + +int main () +{ + if (__builtin_cpu_supports ("sse4.2")) + return foo () - 1; + return foo (); +} + +__attribute__ ((target ("default"))) +int foo () +{ + return 0; +} Index: gcc/testsuite/g++.dg/ext/mv13.h =================================================================== --- gcc/testsuite/g++.dg/ext/mv13.h (revision 0) +++ gcc/testsuite/g++.dg/ext/mv13.h (revision 0) @@ -0,0 +1,6 @@ +// Header file used by mv13.C and mv13-aux.C. +// { dg-do compile { target i?86-*-* x86_64-*-* } } +// { dg-options "" } + +int foo () __attribute__ ((target ("default"))); +int foo () __attribute__ ((target ("sse4.2"))); Index: gcc/testsuite/g++.dg/ext/mv14.C =================================================================== --- gcc/testsuite/g++.dg/ext/mv14.C (revision 0) +++ gcc/testsuite/g++.dg/ext/mv14.C (revision 0) @@ -0,0 +1,18 @@ +// Test case to check if multiversioning functions that are extern "C" +// generates errors. + +// { dg-do compile { target i?86-*-* x86_64-*-* } } + +extern "C" +__attribute__ ((target ("default"))) +int foo () // { dg-error "previously defined here" } +{ + return 0; +} + +extern "C" +__attribute__ ((target ("sse4.2"))) +int foo () // { dg-error "redefinition" } +{ + return 1; +} Index: gcc/config/i386/i386.c =================================================================== --- gcc/config/i386/i386.c (revision 195818) +++ gcc/config/i386/i386.c (working copy) @@ -29065,7 +29065,7 @@ ix86_mangle_function_version_assembler_name (tree if (DECL_VIRTUAL_P (decl) || DECL_VINDEX (decl)) error_at (DECL_SOURCE_LOCATION (decl), - "Virtual function versioning not supported\n"); + "Virtual function multiversioning not supported\n"); version_attr = lookup_attribute ("target", DECL_ATTRIBUTES (decl));