{"id":2230360,"url":"http://patchwork.ozlabs.org/api/1.1/patches/2230360/?format=json","web_url":"http://patchwork.ozlabs.org/project/gcc/patch/bmm.hi2b0wo6rq.gcc.gcc-TEST.chris.bazley.150.1.1@forge-stage.sourceware.org/","project":{"id":17,"url":"http://patchwork.ozlabs.org/api/1.1/projects/17/?format=json","name":"GNU Compiler Collection","link_name":"gcc","list_id":"gcc-patches.gcc.gnu.org","list_email":"gcc-patches@gcc.gnu.org","web_url":null,"scm_url":null,"webscm_url":null},"msgid":"<bmm.hi2b0wo6rq.gcc.gcc-TEST.chris.bazley.150.1.1@forge-stage.sourceware.org>","date":"2026-04-29T16:26:07","name":"[v1,1/1] Prevent optimize attribute from undoing target attribute","commit_ref":null,"pull_url":null,"state":"new","archived":false,"hash":"f7f4e0181d248b2327fa320914de53f4db0bd290","submitter":{"id":92535,"url":"http://patchwork.ozlabs.org/api/1.1/people/92535/?format=json","name":"Christopher Bazley via Sourceware Forge","email":"forge-bot+chris.bazley@forge-stage.sourceware.org"},"delegate":null,"mbox":"http://patchwork.ozlabs.org/project/gcc/patch/bmm.hi2b0wo6rq.gcc.gcc-TEST.chris.bazley.150.1.1@forge-stage.sourceware.org/mbox/","series":[{"id":502107,"url":"http://patchwork.ozlabs.org/api/1.1/series/502107/?format=json","web_url":"http://patchwork.ozlabs.org/project/gcc/list/?series=502107","date":"2026-04-29T16:26:06","name":"Prevent optimize attribute from undoing target attribute","version":1,"mbox":"http://patchwork.ozlabs.org/series/502107/mbox/"}],"comments":"http://patchwork.ozlabs.org/api/patches/2230360/comments/","check":"pending","checks":"http://patchwork.ozlabs.org/api/patches/2230360/checks/","tags":{},"headers":{"Return-Path":"<gcc-patches-bounces~incoming=patchwork.ozlabs.org@gcc.gnu.org>","X-Original-To":["incoming@patchwork.ozlabs.org","gcc-patches@gcc.gnu.org"],"Delivered-To":["patchwork-incoming@legolas.ozlabs.org","gcc-patches@gcc.gnu.org"],"Authentication-Results":["legolas.ozlabs.org;\n spf=pass (sender SPF authorized) smtp.mailfrom=gcc.gnu.org\n (client-ip=2620:52:6:3111::32; helo=vm01.sourceware.org;\n envelope-from=gcc-patches-bounces~incoming=patchwork.ozlabs.org@gcc.gnu.org;\n receiver=patchwork.ozlabs.org)","sourceware.org; dmarc=none (p=none dis=none)\n header.from=forge-stage.sourceware.org","sourceware.org;\n spf=pass smtp.mailfrom=forge-stage.sourceware.org","server2.sourceware.org;\n arc=none smtp.remote-ip=38.145.34.39"],"Received":["from vm01.sourceware.org (vm01.sourceware.org\n [IPv6:2620:52:6:3111::32])\n\t(using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)\n\t key-exchange x25519 server-signature ECDSA (secp384r1) server-digest SHA384)\n\t(No client certificate requested)\n\tby legolas.ozlabs.org (Postfix) with ESMTPS id 4g5N4b5Rvkz1yHZ\n\tfor <incoming@patchwork.ozlabs.org>; Thu, 30 Apr 2026 02:27:27 +1000 (AEST)","from vm01.sourceware.org (localhost [127.0.0.1])\n\tby sourceware.org (Postfix) with ESMTP id BA3CE4BB58BE\n\tfor <incoming@patchwork.ozlabs.org>; Wed, 29 Apr 2026 16:27:25 +0000 (GMT)","from forge-stage.sourceware.org (vm08.sourceware.org [38.145.34.39])\n by sourceware.org (Postfix) with ESMTPS id F1B2F4B99F60\n for <gcc-patches@gcc.gnu.org>; Wed, 29 Apr 2026 16:26:58 +0000 (GMT)","from forge-stage.sourceware.org (localhost [IPv6:::1])\n (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)\n key-exchange x25519 server-signature ECDSA (prime256v1) server-digest SHA256)\n (No client certificate requested)\n by forge-stage.sourceware.org (Postfix) with ESMTPS id C7FA54363D\n for <gcc-patches@gcc.gnu.org>; Wed, 29 Apr 2026 16:26:58 +0000 (UTC)"],"DKIM-Filter":["OpenDKIM Filter v2.11.0 sourceware.org BA3CE4BB58BE","OpenDKIM Filter v2.11.0 sourceware.org F1B2F4B99F60"],"DMARC-Filter":"OpenDMARC Filter v1.4.2 sourceware.org F1B2F4B99F60","ARC-Filter":"OpenARC Filter v1.0.0 sourceware.org F1B2F4B99F60","ARC-Seal":"i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1777480019; cv=none;\n b=OexvPs3RrQZv78KO3KhNirV08eLLm/pB8MtT8zAUnoJ10h4701vYhDIdcyilwrpKQLyKIk6ww8seiLnka2jeN8Wgta4r++2oZYhpu3FQdFcINM/CQtMPLbkPSSpRT7FhEmzQDBzbf877K9jIL9Jku5VmT9h28Oz9PXjqBhIwFV4=","ARC-Message-Signature":"i=1; a=rsa-sha256; d=sourceware.org; s=key;\n t=1777480019; c=relaxed/simple;\n bh=vWx/yJn2lqwi4S4KNUf/iXCeXYRKcSZlmciW439b4oY=;\n h=From:Date:Subject:To:Message-ID;\n b=xj5dT70pVNC9MSaVutNdRFWH+NBjskIAyEpU6a+0ydS6uKqcK2bSYS5a5hquUqEEUX9jtCXXgpsVQcvaRajvm02Ey0HZgj9wU5Kv85ZXv22evWubFU7V/qXDiXTdrQwm/+6IzUL+BAuTNMDNXhAMuEmODMLU3Fz+6vEOVTZchT4=","ARC-Authentication-Results":"i=1; server2.sourceware.org","From":"Christopher Bazley via Sourceware Forge\n <forge-bot+chris.bazley@forge-stage.sourceware.org>","Date":"Wed, 29 Apr 2026 16:26:07 +0000","Subject":"[PATCH v1 1/1] Prevent optimize attribute from undoing target\n attribute","To":"gcc-patches mailing list <gcc-patches@gcc.gnu.org>","Message-ID":"\n <bmm.hi2b0wo6rq.gcc.gcc-TEST.chris.bazley.150.1.1@forge-stage.sourceware.org>","X-Mailer":"batrachomyomachia","X-Pull-Request-Organization":"gcc","X-Pull-Request-Repository":"gcc-TEST","X-Pull-Request":"https://forge.sourceware.org/gcc/gcc-TEST/pulls/150","References":"\n <bmm.hi2b0wo6rq.gcc.gcc-TEST.chris.bazley.150.1.0@forge-stage.sourceware.org>","In-Reply-To":"\n <bmm.hi2b0wo6rq.gcc.gcc-TEST.chris.bazley.150.1.0@forge-stage.sourceware.org>","X-Patch-URL":"\n https://forge.sourceware.org/chris.bazley/gcc/commit/2cf351ca3decc56a05ee2d12b1f27a16096a47d2","X-BeenThere":"gcc-patches@gcc.gnu.org","X-Mailman-Version":"2.1.30","Precedence":"list","List-Id":"Gcc-patches mailing list <gcc-patches.gcc.gnu.org>","List-Unsubscribe":"<https://gcc.gnu.org/mailman/options/gcc-patches>,\n <mailto:gcc-patches-request@gcc.gnu.org?subject=unsubscribe>","List-Archive":"<https://gcc.gnu.org/pipermail/gcc-patches/>","List-Post":"<mailto:gcc-patches@gcc.gnu.org>","List-Help":"<mailto:gcc-patches-request@gcc.gnu.org?subject=help>","List-Subscribe":"<https://gcc.gnu.org/mailman/listinfo/gcc-patches>,\n <mailto:gcc-patches-request@gcc.gnu.org?subject=subscribe>","Reply-To":"gcc-patches mailing list <gcc-patches@gcc.gnu.org>,\n chris.bazley@arm.com","Errors-To":"gcc-patches-bounces~incoming=patchwork.ozlabs.org@gcc.gnu.org"},"content":"From: Christopher Bazley <chris.bazley@arm.com>\n\nPreviously, function attributes such as\n__attribute__ ((target (\"+sve\"), optimize (\"O2\"))) could\nbe applied wrongly because correct processing of the\noptimize attribute relied on one of the following to be\ntrue:\n\n1. The function had no function-specific target options, or\n2. Changing optimization options did not have the side-effect\nof modifying target options.\n\nAssumption 2 is not generally guaranteed to be true, and\nthe implementation of the handle_optimize_attribute function\nalready implicitly acknowledged that by rebuilding the target\noptions after parsing optimization options, and replacing the\ncurrent target option node if that rebuilding resulted in\na fresh target option node. However, any target options\nalready associated with the function being modified were not\napplied before parsing optimization options, therefore they\nwere lost if that function's target option node was replaced.\n\ngcc/c-family/ChangeLog:\n\n\t* c-attribs.cc (handle_optimize_attribute):\n\tSave the global target options before modifying\n\tthem. If the function with the optimize attribute\n\talready has a target option node then restore those\n\ttarget options as the global options, so that they\n\tare used as the basis for any new optimization and\n\ttarget options attached to the function.\n\ngcc/d/ChangeLog:\n\n\t* d-attribs.cc (d_handle_optimize_attribute):\n\tSave the global target options before modifying\n\tthem. If the function with the optimize attribute\n\talready has a target option node then restore those\n\ttarget options as the global options, so that they\n\tare used as the basis for any new optimization and\n\ttarget options attached to the function.\n---\n gcc/c-family/c-attribs.cc | 16 +++++++++++++---\n gcc/d/d-attribs.cc        | 16 +++++++++++++---\n 2 files changed, 26 insertions(+), 6 deletions(-)","diff":"diff --git a/gcc/c-family/c-attribs.cc b/gcc/c-family/c-attribs.cc\nindex d437c55285e3..99ce04abff56 100644\n--- a/gcc/c-family/c-attribs.cc\n+++ b/gcc/c-family/c-attribs.cc\n@@ -6468,12 +6468,18 @@ handle_optimize_attribute (tree *node, tree name, tree args,\n   else\n     {\n       struct cl_optimization cur_opts;\n+      struct cl_target_option cur_target;\n       tree old_opts = DECL_FUNCTION_SPECIFIC_OPTIMIZATION (*node);\n+      tree old_target = DECL_FUNCTION_SPECIFIC_TARGET (*node);\n \n       /* Save current options.  */\n       cl_optimization_save (&cur_opts, &global_options, &global_options_set);\n-      tree prev_target_node = build_target_option_node (&global_options,\n-\t\t\t\t\t\t\t&global_options_set);\n+      cl_target_option_save (&cur_target, &global_options, &global_options_set);\n+\n+      tree prev_target_node\n+\t= old_target\n+\t    ? old_target\n+\t    : build_target_option_node (&global_options, &global_options_set);\n \n       /* If we previously had some optimization options, use them as the\n \t default.  */\n@@ -6492,6 +6498,10 @@ handle_optimize_attribute (tree *node, tree name, tree args,\n \tcl_optimization_restore (&global_options, &global_options_set,\n \t\t\t\t TREE_OPTIMIZATION (old_opts));\n \n+      if (old_target)\n+\tcl_target_option_restore (&global_options, &global_options_set,\n+\t\t\t\t  TREE_TARGET_OPTION (old_target));\n+\n       /* Parse options, and update the vector.  */\n       parse_optimize_options (args, true);\n       DECL_FUNCTION_SPECIFIC_OPTIMIZATION (*node)\n@@ -6509,7 +6519,7 @@ handle_optimize_attribute (tree *node, tree name, tree args,\n       cl_optimization_restore (&global_options, &global_options_set,\n \t\t\t       &cur_opts);\n       cl_target_option_restore (&global_options, &global_options_set,\n-\t\t\t\tTREE_TARGET_OPTION (prev_target_node));\n+\t\t\t\t&cur_target);\n \n       if (saved_global_options != NULL)\n \t{\ndiff --git a/gcc/d/d-attribs.cc b/gcc/d/d-attribs.cc\nindex 3f748b7bef3e..30c1cb320ec2 100644\n--- a/gcc/d/d-attribs.cc\n+++ b/gcc/d/d-attribs.cc\n@@ -923,12 +923,18 @@ d_handle_optimize_attribute (tree *node, tree name, tree args, int,\n   else\n     {\n       struct cl_optimization cur_opts;\n+      struct cl_target_option cur_target;\n       tree old_opts = DECL_FUNCTION_SPECIFIC_OPTIMIZATION (*node);\n+      tree old_target = DECL_FUNCTION_SPECIFIC_TARGET (*node);\n \n       /* Save current options.  */\n       cl_optimization_save (&cur_opts, &global_options, &global_options_set);\n-      tree prev_target_node = build_target_option_node (&global_options,\n-\t\t\t\t\t\t\t&global_options_set);\n+      cl_target_option_save (&cur_target, &global_options, &global_options_set);\n+\n+      tree prev_target_node\n+\t= old_target\n+\t    ? old_target\n+\t    : build_target_option_node (&global_options, &global_options_set);\n \n       /* If we previously had some optimization options, use them as the\n \t default.  */\n@@ -943,6 +949,10 @@ d_handle_optimize_attribute (tree *node, tree name, tree args, int,\n \tcl_optimization_restore (&global_options, &global_options_set,\n \t\t\t\t TREE_OPTIMIZATION (old_opts));\n \n+      if (old_target)\n+\tcl_target_option_restore (&global_options, &global_options_set,\n+\t\t\t\t  TREE_TARGET_OPTION (old_target));\n+\n       /* Parse options, and update the vector.  */\n       parse_optimize_options (args);\n       DECL_FUNCTION_SPECIFIC_OPTIMIZATION (*node)\n@@ -956,7 +966,7 @@ d_handle_optimize_attribute (tree *node, tree name, tree args, int,\n       cl_optimization_restore (&global_options, &global_options_set,\n \t\t\t       &cur_opts);\n       cl_target_option_restore (&global_options, &global_options_set,\n-\t\t\t\tTREE_TARGET_OPTION (prev_target_node));\n+\t\t\t\t&cur_target);\n       if (saved_global_options != NULL)\n \t{\n \t  cl_optimization_compare (saved_global_options, &global_options);\n","prefixes":["v1","1/1"]}