Richard and Joseph:

> On May 28, 2024, at 17:09, Qing Zhao <qing.z...@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

Reply via email to