[COMMITTED,27/30] ada: Get rid of secondary stack for indefinite record types with size clause

Message ID 20240520074858.222435-27-poulhies@adacore.com
State Committed
Commit efc7ba5f8f113565b47214d0ffdb27a6638ab8b6
Headers
Series [COMMITTED,01/30] ada: Rework and augment documentation on strict aliasing |

Checks

Context Check Description
linaro-tcwg-bot/tcwg_gcc_build--master-arm fail Patch failed to apply
linaro-tcwg-bot/tcwg_gcc_build--master-aarch64 fail Patch failed to apply

Commit Message

Marc Poulhiès May 20, 2024, 7:48 a.m. UTC
  From: Eric Botcazou <ebotcazou@adacore.com>

This change eliminates the use of the secondary stack for indefinite record
types for which a valid (object) size clause is specified.  In accordance
with the RM, the compiler accepts (object) size clauses on such types only
if all the components, including those of the variants of the variant part
if any, have a size known at compile time, and only if the clauses specify
a value that is at least as large as the largest possible size of objects
of the types when all the variants are considered.  However, it would still
have used the secondary stack, despite valid (object) size clauses, before
the change, as soon as a variant part was present in the types.

gcc/ada/

	* freeze.ads (Check_Compile_Time_Size): Remove obsolete description
	of usage for the Size_Known_At_Compile_Time flag.
	* freeze.adb (Check_Compile_Time_Size.Size_Known): In the case where
	a variant part is present, do not return False if Esize is known.
	* sem_util.adb (Needs_Secondary_Stack.Caller_Known_Size_Record): Add
	missing "Start of processing" comment.  Return true if either a size
	clause or an object size clause has been given for the first subtype
	of the type.

Tested on x86_64-pc-linux-gnu, committed on master.

---
 gcc/ada/freeze.adb   |  1 +
 gcc/ada/freeze.ads   | 11 +++++------
 gcc/ada/sem_util.adb | 12 ++++++++++++
 3 files changed, 18 insertions(+), 6 deletions(-)
  

Patch

diff --git a/gcc/ada/freeze.adb b/gcc/ada/freeze.adb
index 26e9d01d8b2..ea6106e6455 100644
--- a/gcc/ada/freeze.adb
+++ b/gcc/ada/freeze.adb
@@ -1077,6 +1077,7 @@  package body Freeze is
                     and then
                       No (Discriminant_Default_Value (First_Discriminant (T)))
                     and then not Known_RM_Size (T)
+                    and then not Known_Esize (T)
                   then
                      return False;
                   end if;
diff --git a/gcc/ada/freeze.ads b/gcc/ada/freeze.ads
index fc0b7678fdc..066d8f054f6 100644
--- a/gcc/ada/freeze.ads
+++ b/gcc/ada/freeze.ads
@@ -156,17 +156,16 @@  package Freeze is
    --    RM_Size field is set to the required size, allowing for possible front
    --    end packing of an array using this type as a component type.
    --
-   --  Note: the flag Size_Known_At_Compile_Time is used to determine if the
-   --  secondary stack must be used to return a value of the type, and also
-   --  to determine whether a component clause is allowed for a component
-   --  of the given type.
-   --
-   --  Note: this is public because of one dubious use in Sem_Res???
+   --  Note: the flag Size_Known_At_Compile_Time is used to determine whether a
+   --  size clause is allowed for the type, and also whether a component clause
+   --  is allowed for a component of the type.
    --
    --  Note: Check_Compile_Time_Size does not test the case of the size being
    --  known because a size clause is specifically given. That is because we
    --  do not allow a size clause if the size would not otherwise be known at
    --  compile time in any case.
+   --
+   --  ??? This is public because of dubious uses in Sem_Ch3 and Sem_Res
 
    procedure Check_Inherited_Conditions
     (R               : Entity_Id;
diff --git a/gcc/ada/sem_util.adb b/gcc/ada/sem_util.adb
index 09358278210..15994b4d1e9 100644
--- a/gcc/ada/sem_util.adb
+++ b/gcc/ada/sem_util.adb
@@ -22409,6 +22409,8 @@  package body Sem_Util is
             return False;
          end Depends_On_Discriminant;
 
+      --  Start of processing for Caller_Known_Size_Record
+
       begin
          --  This is a protected type without Corresponding_Record_Type set,
          --  typically because expansion is disabled. The safe thing to do is
@@ -22418,6 +22420,16 @@  package body Sem_Util is
             return True;
          end if;
 
+         --  If either size is specified for the type, then it's known in the
+         --  caller in particular. Note that, even if the clause is confirming,
+         --  this does not change the outcome since the size was already known.
+
+         if Has_Size_Clause (First_Subtype (Typ))
+           or else Has_Object_Size_Clause (First_Subtype (Typ))
+         then
+            return True;
+         end if;
+
          --  First see if we have a variant part and return False if it depends
          --  on discriminants.