public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
* [PATCH] C++: add type checking for static local vector variable in template
@ 2021-09-06 12:10 wangpc
  2021-09-15 21:02 ` Jason Merrill
  0 siblings, 1 reply; 8+ messages in thread
From: wangpc @ 2021-09-06 12:10 UTC (permalink / raw)
  To: gcc-patches; +Cc: wangpc

This patch adds type checking for static local vector variable in
C++ template, both AArch64 SVE and RISCV RVV are of sizeless type
and thay all have this issue.

2021-08-06  wangpc  <pc.wang@linux.alibaba.com>

gcc/cp/ChangeLog

        * pt.c (tsubst_decl): Add type checking.

gcc/testsuite/ChangeLog

        * g++.target/aarch64/sve/static-var-in-template.C: New test.
---
 gcc/cp/pt.c                                    |  8 +++++++-
 .../aarch64/sve/static-var-in-template.C       | 18 ++++++++++++++++++
 2 files changed, 25 insertions(+), 1 deletion(-)
 create mode 100644 gcc/testsuite/g++.target/aarch64/sve/static-var-in-template.C

diff --git a/gcc/cp/pt.c b/gcc/cp/pt.c
index f0aa626ab723..988f4cb1e73f 100644
--- a/gcc/cp/pt.c
+++ b/gcc/cp/pt.c
@@ -14731,7 +14731,13 @@ tsubst_decl (tree t, tree args, tsubst_flags_t complain)
 		 even if its underlying type is not.  */
 	      TYPE_DEPENDENT_P_VALID (TREE_TYPE (r)) = false;
 	  }
-
+    /* We should verify static local variable's type
+    since vector type does not have a fixed size.  */
+    if (TREE_STATIC (t)
+      &&!verify_type_context (input_location, TCTX_STATIC_STORAGE, type))
+    {
+      RETURN (error_mark_node);
+    }
 	layout_decl (r, 0);
       }
       break;
diff --git a/gcc/testsuite/g++.target/aarch64/sve/static-var-in-template.C b/gcc/testsuite/g++.target/aarch64/sve/static-var-in-template.C
new file mode 100644
index 000000000000..26d397ca565d
--- /dev/null
+++ b/gcc/testsuite/g++.target/aarch64/sve/static-var-in-template.C
@@ -0,0 +1,18 @@
+/* { dg-do compile } */
+
+#include <arm_sve.h>
+
+template <int N>
+void f()
+{
+    int i = 0;
+    static svbool_t pg = svwhilelt_b64(0, N);
+}
+
+int main(int argc, char **argv)
+{
+    f<2>();
+    return 0;
+}
+
+/* { dg-error {SVE type 'svbool_t' does not have a fixed size} } */
-- 
2.33.0.windows.1


^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: [PATCH] C++: add type checking for static local vector variable in template
  2021-09-06 12:10 [PATCH] C++: add type checking for static local vector variable in template wangpc
@ 2021-09-15 21:02 ` Jason Merrill
  2021-09-16  9:06   ` pc.wang
  0 siblings, 1 reply; 8+ messages in thread
From: Jason Merrill @ 2021-09-15 21:02 UTC (permalink / raw)
  To: wangpc, gcc-patches

On 9/6/21 08:10, wangpc via Gcc-patches wrote:
> This patch adds type checking for static local vector variable in
> C++ template, both AArch64 SVE and RISCV RVV are of sizeless type
> and thay all have this issue.
> 
> 2021-08-06  wangpc  <pc.wang@linux.alibaba.com>
> 
> gcc/cp/ChangeLog
> 
>          * pt.c (tsubst_decl): Add type checking.
> 
> gcc/testsuite/ChangeLog
> 
>          * g++.target/aarch64/sve/static-var-in-template.C: New test.
> ---
>   gcc/cp/pt.c                                    |  8 +++++++-
>   .../aarch64/sve/static-var-in-template.C       | 18 ++++++++++++++++++
>   2 files changed, 25 insertions(+), 1 deletion(-)
>   create mode 100644 gcc/testsuite/g++.target/aarch64/sve/static-var-in-template.C
> 
> diff --git a/gcc/cp/pt.c b/gcc/cp/pt.c
> index f0aa626ab723..988f4cb1e73f 100644
> --- a/gcc/cp/pt.c
> +++ b/gcc/cp/pt.c
> @@ -14731,7 +14731,13 @@ tsubst_decl (tree t, tree args, tsubst_flags_t complain)
>   		 even if its underlying type is not.  */
>   	      TYPE_DEPENDENT_P_VALID (TREE_TYPE (r)) = false;
>   	  }
> -
> +    /* We should verify static local variable's type
> +    since vector type does not have a fixed size.  */
> +    if (TREE_STATIC (t)
> +      &&!verify_type_context (input_location, TCTX_STATIC_STORAGE, type))

It seems that the reason this was missed before was because we checked 
for this in start_decl, which isn't called for template instantiation. 
Would it work to move the verify_type_context code from start_decl to 
cp_finish_decl, near the other call to verify_type_context, instead of 
doing anything here?

> +    {
> +      RETURN (error_mark_node);
> +    }
>   	layout_decl (r, 0);
>         }
>         break;
> diff --git a/gcc/testsuite/g++.target/aarch64/sve/static-var-in-template.C b/gcc/testsuite/g++.target/aarch64/sve/static-var-in-template.C
> new file mode 100644
> index 000000000000..26d397ca565d
> --- /dev/null
> +++ b/gcc/testsuite/g++.target/aarch64/sve/static-var-in-template.C
> @@ -0,0 +1,18 @@
> +/* { dg-do compile } */
> +
> +#include <arm_sve.h>
> +
> +template <int N>
> +void f()
> +{
> +    int i = 0;
> +    static svbool_t pg = svwhilelt_b64(0, N);
> +}
> +
> +int main(int argc, char **argv)
> +{
> +    f<2>();
> +    return 0;
> +}
> +
> +/* { dg-error {SVE type 'svbool_t' does not have a fixed size} } */
> 


^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: [PATCH] C++: add type checking for static local vector variable in template
  2021-09-15 21:02 ` Jason Merrill
@ 2021-09-16  9:06   ` pc.wang
  0 siblings, 0 replies; 8+ messages in thread
From: pc.wang @ 2021-09-16  9:06 UTC (permalink / raw)
  To: gcc-patches, Jason Merrill

I move the verify_type_context code to cp_finish_decl and it works.
I will send the new patch later.
------------------------------------------------------------------
Sender:Jason Merrill <jason@redhat.com>
Sent At:2021 Sep. 16 (Thu.) 05:04
Recipient:wangpc <pc.wang@linux.alibaba.com>; gcc-patches <gcc-patches@gcc.gnu.org>
Subject:Re: [PATCH] C++: add type checking for static local vector variable in template

On 9/6/21 08:10, wangpc via Gcc-patches wrote:
> This patch adds type checking for static local vector variable in
> C++ template, both AArch64 SVE and RISCV RVV are of sizeless type
> and thay all have this issue.
> 
> 2021-08-06  wangpc  <pc.wang@linux.alibaba.com>
> 
> gcc/cp/ChangeLog
> 
>          * pt.c (tsubst_decl): Add type checking.
> 
> gcc/testsuite/ChangeLog
> 
>          * g++.target/aarch64/sve/static-var-in-template.C: New test.
> ---
>   gcc/cp/pt.c                                    |  8 +++++++-
>   .../aarch64/sve/static-var-in-template.C       | 18 ++++++++++++++++++
>   2 files changed, 25 insertions(+), 1 deletion(-)
>   create mode 100644 gcc/testsuite/g++.target/aarch64/sve/static-var-in-template.C
> 
> diff --git a/gcc/cp/pt.c b/gcc/cp/pt.c
> index f0aa626ab723..988f4cb1e73f 100644
> --- a/gcc/cp/pt.c
> +++ b/gcc/cp/pt.c
> @@ -14731,7 +14731,13 @@ tsubst_decl (tree t, tree args, tsubst_flags_t complain)
>      even if its underlying type is not.  */
>          TYPE_DEPENDENT_P_VALID (TREE_TYPE (r)) = false;
>      }
> -
> +    /* We should verify static local variable's type
> +    since vector type does not have a fixed size.  */
> +    if (TREE_STATIC (t)
> +      &&!verify_type_context (input_location, TCTX_STATIC_STORAGE, type))

It seems that the reason this was missed before was because we checked 
for this in start_decl, which isn't called for template instantiation. 
Would it work to move the verify_type_context code from start_decl to 
cp_finish_decl, near the other call to verify_type_context, instead of 
doing anything here?

> +    {
> +      RETURN (error_mark_node);
> +    }
>    layout_decl (r, 0);
>         }
>         break;
> diff --git a/gcc/testsuite/g++.target/aarch64/sve/static-var-in-template.C b/gcc/testsuite/g++.target/aarch64/sve/static-var-in-template.C
> new file mode 100644
> index 000000000000..26d397ca565d
> --- /dev/null
> +++ b/gcc/testsuite/g++.target/aarch64/sve/static-var-in-template.C
> @@ -0,0 +1,18 @@
> +/* { dg-do compile } */
> +
> +#include <arm_sve.h>
> +
> +template <int N>
> +void f()
> +{
> +    int i = 0;
> +    static svbool_t pg = svwhilelt_b64(0, N);
> +}
> +
> +int main(int argc, char **argv)
> +{
> +    f<2>();
> +    return 0;
> +}
> +
> +/* { dg-error {SVE type 'svbool_t' does not have a fixed size} } */
> 

^ permalink raw reply	[flat|nested] 8+ messages in thread

* [PATCH] C++: add type checking for static local vector variable in template
  2021-09-16 15:19 ` Jason Merrill
@ 2021-09-17  8:05   ` wangpc
  0 siblings, 0 replies; 8+ messages in thread
From: wangpc @ 2021-09-17  8:05 UTC (permalink / raw)
  To: Jason Merrill, gcc-patches

Thanks for your advice, I have misunderstood what you meant.

I have sent a second version patch, please review whether it is OK.

On 2021/9/16 23:19, Jason Merrill wrote:
> On 9/16/21 05:11, wangpc via Gcc-patches wrote:
>> This patch adds type checking for static local vector variable in
>> C++ template, both AArch64 SVE and RISCV RVV are of sizeless type
>> and they all have this issue.
>>
>> 2021-08-06  wangpc  <pc.wang@linux.alibaba.com>
>>
>> gcc/cp/ChangeLog
>>
>>          * decl.c (cp_finish_decl): Add type checking.
>>
>> gcc/testsuite/ChangeLog
>>
>>          * g++.target/aarch64/sve/static-var-in-template.C: New test.
>>
>> diff --git a/gcc/cp/decl.c b/gcc/cp/decl.c
>> index 90111e4c786..e3a06ea0858 100644
>> --- a/gcc/cp/decl.c
>> +++ b/gcc/cp/decl.c
>> @@ -7520,6 +7520,12 @@ cp_finish_decl (tree decl, tree init, bool 
>> init_const_expr_p,
>>         && DECL_INITIALIZED_IN_CLASS_P (decl))
>>       check_static_variable_definition (decl, type);
>>   +  if (VAR_P (decl)
>> +      && DECL_FUNCTION_SCOPE_P (decl)
>> +      && TREE_STATIC (decl))
>> +    verify_type_context (DECL_SOURCE_LOCATION (decl),
>> +                  TCTX_STATIC_STORAGE, type);
>
> I was thinking to move the verify_type_context code from start_decl, 
> which handles more cases:
>
>>   if (is_global_var (decl))
>>     {
>>       type_context_kind context = (DECL_THREAD_LOCAL_P (decl)
>>                                    ? TCTX_THREAD_STORAGE
>>                                    : TCTX_STATIC_STORAGE);
>>       verify_type_context (input_location, context, TREE_TYPE (decl));
>>     }
>
> Jason

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: [PATCH] C++: add type checking for static local vector variable in template
  2021-09-16  9:11 wangpc
@ 2021-09-16 15:19 ` Jason Merrill
  2021-09-17  8:05   ` wangpc
  0 siblings, 1 reply; 8+ messages in thread
From: Jason Merrill @ 2021-09-16 15:19 UTC (permalink / raw)
  To: wangpc, gcc-patches

On 9/16/21 05:11, wangpc via Gcc-patches wrote:
> This patch adds type checking for static local vector variable in
> C++ template, both AArch64 SVE and RISCV RVV are of sizeless type
> and they all have this issue.
> 
> 2021-08-06  wangpc  <pc.wang@linux.alibaba.com>
> 
> gcc/cp/ChangeLog
> 
>          * decl.c (cp_finish_decl): Add type checking.
> 
> gcc/testsuite/ChangeLog
> 
>          * g++.target/aarch64/sve/static-var-in-template.C: New test.
> 
> diff --git a/gcc/cp/decl.c b/gcc/cp/decl.c
> index 90111e4c786..e3a06ea0858 100644
> --- a/gcc/cp/decl.c
> +++ b/gcc/cp/decl.c
> @@ -7520,6 +7520,12 @@ cp_finish_decl (tree decl, tree init, bool init_const_expr_p,
>         && DECL_INITIALIZED_IN_CLASS_P (decl))
>       check_static_variable_definition (decl, type);
>   
> +  if (VAR_P (decl)
> +      && DECL_FUNCTION_SCOPE_P (decl)
> +      && TREE_STATIC (decl))
> +    verify_type_context (DECL_SOURCE_LOCATION (decl),
> +			      TCTX_STATIC_STORAGE, type);

I was thinking to move the verify_type_context code from start_decl, 
which handles more cases:

>   if (is_global_var (decl))
>     {
>       type_context_kind context = (DECL_THREAD_LOCAL_P (decl)
>                                    ? TCTX_THREAD_STORAGE
>                                    : TCTX_STATIC_STORAGE);
>       verify_type_context (input_location, context, TREE_TYPE (decl));
>     }

Jason


^ permalink raw reply	[flat|nested] 8+ messages in thread

* [PATCH] C++: add type checking for static local vector variable in template
@ 2021-09-16  9:11 wangpc
  2021-09-16 15:19 ` Jason Merrill
  0 siblings, 1 reply; 8+ messages in thread
From: wangpc @ 2021-09-16  9:11 UTC (permalink / raw)
  To: gcc-patches; +Cc: wangpc

This patch adds type checking for static local vector variable in
C++ template, both AArch64 SVE and RISCV RVV are of sizeless type
and they all have this issue.

2021-08-06  wangpc  <pc.wang@linux.alibaba.com>

gcc/cp/ChangeLog

        * decl.c (cp_finish_decl): Add type checking.

gcc/testsuite/ChangeLog

        * g++.target/aarch64/sve/static-var-in-template.C: New test.

diff --git a/gcc/cp/decl.c b/gcc/cp/decl.c
index 90111e4c786..e3a06ea0858 100644
--- a/gcc/cp/decl.c
+++ b/gcc/cp/decl.c
@@ -7520,6 +7520,12 @@ cp_finish_decl (tree decl, tree init, bool init_const_expr_p,
       && DECL_INITIALIZED_IN_CLASS_P (decl))
     check_static_variable_definition (decl, type);
 
+  if (VAR_P (decl)
+      && DECL_FUNCTION_SCOPE_P (decl)
+      && TREE_STATIC (decl))
+    verify_type_context (DECL_SOURCE_LOCATION (decl),
+			      TCTX_STATIC_STORAGE, type);
+
   if (init && TREE_CODE (decl) == FUNCTION_DECL)
     {
       tree clone;
diff --git a/gcc/testsuite/g++.target/aarch64/sve/static-var-in-template.C b/gcc/testsuite/g++.target/aarch64/sve/static-var-in-template.C
new file mode 100644
index 00000000000..c2395d18d50
--- /dev/null
+++ b/gcc/testsuite/g++.target/aarch64/sve/static-var-in-template.C
@@ -0,0 +1,17 @@
+/* { dg-do compile } */
+
+#include <arm_sve.h>
+
+template <int N>
+void f()
+{
+    static svbool_t pg = svwhilelt_b64(0, N);
+}
+
+int main(int argc, char **argv)
+{
+    f<2>();
+    return 0;
+}
+
+/* { dg-error {SVE type 'svbool_t' does not have a fixed size} } */
-- 
2.33.0.windows.1


^ permalink raw reply	[flat|nested] 8+ messages in thread

* [PATCH] C++: add type checking for static local vector variable in template
@ 2021-09-16  8:59 wangpc
  0 siblings, 0 replies; 8+ messages in thread
From: wangpc @ 2021-09-16  8:59 UTC (permalink / raw)
  To: gcc-patches; +Cc: wangpc

This patch adds type checking for static local vector variable in
C++ template, both AArch64 SVE and RISCV RVV are of sizeless type
and they all have this issue.

2021-08-06  wangpc  <pc.wang@linux.alibaba.com>

gcc/cp/ChangeLog

        * pt.c (tsubst_decl): Add type checking.

gcc/testsuite/ChangeLog

        * g++.target/aarch64/sve/static-var-in-template.C: New test.

diff --git a/gcc/cp/decl.c b/gcc/cp/decl.c
index 90111e4c786..e3a06ea0858 100644
--- a/gcc/cp/decl.c
+++ b/gcc/cp/decl.c
@@ -7520,6 +7520,12 @@ cp_finish_decl (tree decl, tree init, bool init_const_expr_p,
       && DECL_INITIALIZED_IN_CLASS_P (decl))
     check_static_variable_definition (decl, type);
 
+  if (VAR_P (decl)
+      && DECL_FUNCTION_SCOPE_P (decl)
+      && TREE_STATIC (decl))
+    verify_type_context (DECL_SOURCE_LOCATION (decl),
+			      TCTX_STATIC_STORAGE, type);
+
   if (init && TREE_CODE (decl) == FUNCTION_DECL)
     {
       tree clone;
diff --git a/gcc/testsuite/g++.target/aarch64/sve/static-var-in-template.C b/gcc/testsuite/g++.target/aarch64/sve/static-var-in-template.C
new file mode 100644
index 00000000000..c2395d18d50
--- /dev/null
+++ b/gcc/testsuite/g++.target/aarch64/sve/static-var-in-template.C
@@ -0,0 +1,17 @@
+/* { dg-do compile } */
+
+#include <arm_sve.h>
+
+template <int N>
+void f()
+{
+    static svbool_t pg = svwhilelt_b64(0, N);
+}
+
+int main(int argc, char **argv)
+{
+    f<2>();
+    return 0;
+}
+
+/* { dg-error {SVE type 'svbool_t' does not have a fixed size} } */
-- 
2.33.0.windows.1


^ permalink raw reply	[flat|nested] 8+ messages in thread

* [PATCH] C++: add type checking for static local vector variable in template
@ 2021-09-01  8:11 wangpc
  0 siblings, 0 replies; 8+ messages in thread
From: wangpc @ 2021-09-01  8:11 UTC (permalink / raw)
  To: gcc-patches; +Cc: wangpc

From: wangpc <wpc337719@alibaba-inc.com>

---
 gcc/cp/pt.c                                    |  8 +++++++-
 .../aarch64/sve/static-var-in-template.C       | 18 ++++++++++++++++++
 2 files changed, 25 insertions(+), 1 deletion(-)
 create mode 100644 gcc/testsuite/g++.target/aarch64/sve/static-var-in-template.C

diff --git a/gcc/cp/pt.c b/gcc/cp/pt.c
index f0aa626ab723..988f4cb1e73f 100644
--- a/gcc/cp/pt.c
+++ b/gcc/cp/pt.c
@@ -14731,7 +14731,13 @@ tsubst_decl (tree t, tree args, tsubst_flags_t complain)
 		 even if its underlying type is not.  */
 	      TYPE_DEPENDENT_P_VALID (TREE_TYPE (r)) = false;
 	  }
-
+    /* We should verify static local variable's type
+    since vector type does not have a fixed size.  */
+    if (TREE_STATIC (t)
+      &&!verify_type_context (input_location, TCTX_STATIC_STORAGE, type))
+    {
+      RETURN (error_mark_node);
+    }
 	layout_decl (r, 0);
       }
       break;
diff --git a/gcc/testsuite/g++.target/aarch64/sve/static-var-in-template.C b/gcc/testsuite/g++.target/aarch64/sve/static-var-in-template.C
new file mode 100644
index 000000000000..26d397ca565d
--- /dev/null
+++ b/gcc/testsuite/g++.target/aarch64/sve/static-var-in-template.C
@@ -0,0 +1,18 @@
+/* { dg-do compile } */
+
+#include <arm_sve.h>
+
+template <int N>
+void f()
+{
+    int i = 0;
+    static svbool_t pg = svwhilelt_b64(0, N);
+}
+
+int main(int argc, char **argv)
+{
+    f<2>();
+    return 0;
+}
+
+/* { dg-error {SVE type 'svbool_t' does not have a fixed size} } */
-- 
2.33.0.windows.1


^ permalink raw reply	[flat|nested] 8+ messages in thread

end of thread, other threads:[~2021-09-17  8:05 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-09-06 12:10 [PATCH] C++: add type checking for static local vector variable in template wangpc
2021-09-15 21:02 ` Jason Merrill
2021-09-16  9:06   ` pc.wang
  -- strict thread matches above, loose matches on Subject: below --
2021-09-16  9:11 wangpc
2021-09-16 15:19 ` Jason Merrill
2021-09-17  8:05   ` wangpc
2021-09-16  8:59 wangpc
2021-09-01  8:11 wangpc

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).