public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
From: Qing Zhao <qing.zhao@oracle.com>
To: Richard Biener <richard.guenther@gmail.com>,
	Joseph Myers <josmyers@redhat.com>
Cc: Siddhesh Poyarekar <siddhesh@gotplt.org>,
	"uecker@tugraz.at" <uecker@tugraz.at>,
	Kees Cook <keescook@chromium.org>,
	"isanbard@gmail.com" <isanbard@gmail.com>,
	GCC Patches <gcc-patches@gcc.gnu.org>
Subject: Re: [PATCH v9 2/5] Convert references with "counted_by" attributes to/from .ACCESS_WITH_SIZE.
Date: Wed, 29 May 2024 19:11:03 +0000	[thread overview]
Message-ID: <51215440-F891-4EC1-B1CD-8844BFBCEBD5@oracle.com> (raw)
In-Reply-To: <D50E7F57-0791-47B0-86EB-08CC5646B753@oracle.com>

Richard and Joseph:


> On May 28, 2024, at 17:09, Qing Zhao <qing.zhao@oracle.com> wrote:
> 
>>> 
>>> diff --git a/gcc/varasm.cc b/gcc/varasm.cc
>>> index fa17eff551e8..d75b23668925 100644
>>> --- a/gcc/varasm.cc
>>> +++ b/gcc/varasm.cc
>>> @@ -5082,6 +5082,11 @@ initializer_constant_valid_p_1 (tree value, tree endtype, tree *cache)
>>>       }
>>>      return ret;
>>> 
>>> +    case CALL_EXPR:
>>> +      /* For a call to .ACCESS_WITH_SIZE, check the first argument.  */
>>> +      if (tree ref = get_ref_from_access_with_size (value))
>>> +       return initializer_constant_valid_p_1 (ref, endtype, cache);
>> 
>> I think we should fold/strip .ACCESS_WITH_SIZE from initializers
>> instead.  That would be
>> the frontends job I guess, most probably not even generate those in
>> the first place?
> 
> Sounds reasonable, I will see how to do this in C FE. 
> Joseph, do you have any suggestion where in C FE I should do this folding? 
> 
> thanks.
> 
> Qing

In order to address this above comment from Richard, I studied a little bit more on the C FE code, and then come up with the following patch:

diff --git a/gcc/c/c-typeck.cc b/gcc/c/c-typeck.cc
index ac306749e8d7..efd111305b5a 100644
--- a/gcc/c/c-typeck.cc
+++ b/gcc/c/c-typeck.cc
@@ -8650,6 +8650,20 @@ digest_init (location_t init_loc, tree type, tree init, tree origtype,
     STRIP_TYPE_NOPS (inside_init);
+  /* If require_constant is TRUE,  when the initializer is a call to
+     .ACCESS_WITH_SIZE, use the first argument as the initializer.
+     For example:
+     y = (char *) .ACCESS_WITH_SIZE ((char *) &static_annotated.c,...)
+     will be converted to
+     y = &static_annotated.c.  */
+
+  if (require_constant
+      && TREE_CODE (inside_init) == NOP_EXPR
+      && TREE_CODE (TREE_OPERAND (inside_init, 0)) == CALL_EXPR
+      && is_access_with_size_p (TREE_OPERAND (inside_init, 0)))
+    inside_init
+      = get_ref_from_access_with_size (TREE_OPERAND (inside_init, 0));
+
   if (!c_in_omp_for)
     {
       if (TREE_CODE (inside_init) == EXCESS_PRECISION_EXPR)
diff --git a/gcc/varasm.cc b/gcc/varasm.cc
index 2e8fa5e30a80..e1a8458f8749 100644
--- a/gcc/varasm.cc
+++ b/gcc/varasm.cc
@@ -5078,10 +5078,6 @@ initializer_constant_valid_p_1 (tree value, tree endtype, tree *cache)
  }
       return ret;
-    case CALL_EXPR:
-      /* For a call to .ACCESS_WITH_SIZE, check the first argument.  */
-      if (tree ref = get_ref_from_access_with_size (value))
- return initializer_constant_valid_p_1 (ref, endtype, cache);
       /* FALLTHROUGH.  */
     default:
       break;
@@ -5277,11 +5273,6 @@ output_constant (tree exp, unsigned HOST_WIDE_INT size, unsigned int align,
  exp = TREE_OPERAND (exp, 0);
     }
-  /* For a call to .ACCESS_WITH_SIZE, check the first argument.  */
-  if (TREE_CODE (exp) == CALL_EXPR)
-    if (tree ref = get_ref_from_access_with_size (exp))
-      exp = ref;
-
   code = TREE_CODE (TREE_TYPE (exp));

This resolved the issue well. 

Let me know if you have any comment or suggestion. 

I have fixed all the issues Richard raised in my private workspace, testing is ongoing.
Will post the modified patch set soon.

Thanks a lot!

Qing

  parent reply	other threads:[~2024-05-29 19:11 UTC|newest]

Thread overview: 22+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-04-12 13:54 [PATCH v9 0/5] New attribute "counted_by" to annotate bounds for C99 FAM(PR108896) Qing Zhao
2024-04-12 13:54 ` [PATCH v9 1/5] Provide counted_by attribute to flexible array member field (PR108896) Qing Zhao
2024-04-22 20:38   ` Joseph Myers
2024-04-22 21:52     ` Qing Zhao
2024-04-12 13:54 ` [PATCH v9 2/5] Convert references with "counted_by" attributes to/from .ACCESS_WITH_SIZE Qing Zhao
2024-05-28  7:38   ` Richard Biener
2024-05-28 21:09     ` Qing Zhao
2024-05-29  6:57       ` Richard Biener
2024-05-29 13:18         ` Qing Zhao
2024-05-29 19:11       ` Qing Zhao [this message]
2024-04-12 13:54 ` [PATCH v9 3/5] Use the .ACCESS_WITH_SIZE in builtin object size Qing Zhao
2024-05-28  7:39   ` Richard Biener
2024-05-28 21:09     ` Qing Zhao
2024-04-12 13:54 ` [PATCH v9 4/5] Use the .ACCESS_WITH_SIZE in bound sanitizer Qing Zhao
2024-04-12 13:54 ` [PATCH v9 5/5] Add the 6th argument to .ACCESS_WITH_SIZE Qing Zhao
2024-05-28  7:43   ` Richard Biener
2024-05-28 21:10     ` Qing Zhao
2024-05-29  6:58       ` Richard Biener
2024-04-23 19:56 ` [PATCH v9 0/5] New attribute "counted_by" to annotate bounds for C99 FAM(PR108896) Qing Zhao
2024-05-07 14:02   ` Ping * 2 " Qing Zhao
2024-05-07 14:14     ` Qing Zhao
2024-05-20 22:31       ` 3rd Ping " Qing Zhao

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=51215440-F891-4EC1-B1CD-8844BFBCEBD5@oracle.com \
    --to=qing.zhao@oracle.com \
    --cc=gcc-patches@gcc.gnu.org \
    --cc=isanbard@gmail.com \
    --cc=josmyers@redhat.com \
    --cc=keescook@chromium.org \
    --cc=richard.guenther@gmail.com \
    --cc=siddhesh@gotplt.org \
    --cc=uecker@tugraz.at \
    /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).