i386: Fix emissing of __builtin_cpu_supports.

Message ID aebba1a8-0b9c-ab1a-49bd-7b54186dce9d@suse.cz
State New
Headers
Series i386: Fix emissing of __builtin_cpu_supports. |

Commit Message

Martin Liška Dec. 14, 2021, 9:55 a.m. UTC
  The patch fixes __builtin_cpu_supports("avx512vbmi2") which returns a negative
value (that's not allowed in the documentation).

I also checked ppc target that does the same, and __builtin_cpu_is, which
are fine.

Patch can bootstrap on x86_64-linux-gnu and survives regression tests.

Ready to be installed?
Thanks,
Martin	

	PR target/103661

gcc/ChangeLog:

	* config/i386/i386-builtins.c (fold_builtin_cpu): Compare to 0
	as API expects that non-zero values are returned.  For
	"avx512vbmi2" argument, we return now 1 << 31, which is a
	negative integer value.
---
  gcc/config/i386/i386-builtins.c | 3 ++-
  1 file changed, 2 insertions(+), 1 deletion(-)
  

Comments

Jakub Jelinek Dec. 14, 2021, 10:28 a.m. UTC | #1
On Tue, Dec 14, 2021 at 10:55:01AM +0100, Martin Liška wrote:
> The patch fixes __builtin_cpu_supports("avx512vbmi2") which returns a negative
> value (that's not allowed in the documentation).
> 
> I also checked ppc target that does the same, and __builtin_cpu_is, which
> are fine.
> 
> Patch can bootstrap on x86_64-linux-gnu and survives regression tests.
> 
> Ready to be installed?
> Thanks,
> Martin	
> 
> 	PR target/103661
> 
> gcc/ChangeLog:
> 
> 	* config/i386/i386-builtins.c (fold_builtin_cpu): Compare to 0
> 	as API expects that non-zero values are returned.  For
> 	"avx512vbmi2" argument, we return now 1 << 31, which is a
> 	negative integer value.
> ---
>  gcc/config/i386/i386-builtins.c | 3 ++-
>  1 file changed, 2 insertions(+), 1 deletion(-)
> 
> diff --git a/gcc/config/i386/i386-builtins.c b/gcc/config/i386/i386-builtins.c
> index 0fb14b55712..7e57b665c1e 100644
> --- a/gcc/config/i386/i386-builtins.c
> +++ b/gcc/config/i386/i386-builtins.c
> @@ -2353,7 +2353,8 @@ fold_builtin_cpu (tree fndecl, tree *args)
>        /* Return __cpu_model.__cpu_features[0] & field_val  */
>        final = build2 (BIT_AND_EXPR, unsigned_type_node, array_elt,
>  		      build_int_cstu (unsigned_type_node, field_val));
> -      return build1 (CONVERT_EXPR, integer_type_node, final);
> +      return build2 (NE_EXPR, integer_type_node, final,
> +		     build_int_cst (unsigned_type_node, 0));
>      }
>    gcc_unreachable ();
>  }

Wouldn't this be better done only if field_val has the msb set
and keep the CONVERT_EXPR otherwise (why isn't it NOP_EXPR?)?

	Jakub
  
Martin Liška Dec. 14, 2021, 3:07 p.m. UTC | #2
On 12/14/21 11:28, Jakub Jelinek wrote:
> Wouldn't this be better done only if field_val has the msb set

Yes, updated in the attached patch.

> and keep the CONVERT_EXPR otherwise (why isn't it NOP_EXPR?)?

Dunno, but I can prepare a separate patch (likely stage1 material,
right)? Note that are other places that also use CONVERT_EXPR.

Patch can bootstrap on x86_64-linux-gnu and survives regression tests.

Ready to be installed?
Thanks,
Martin
  
Jakub Jelinek Dec. 14, 2021, 4:12 p.m. UTC | #3
On Tue, Dec 14, 2021 at 04:07:55PM +0100, Martin Liška wrote:
> On 12/14/21 11:28, Jakub Jelinek wrote:
> > Wouldn't this be better done only if field_val has the msb set
> 
> Yes, updated in the attached patch.
> 
> > and keep the CONVERT_EXPR otherwise (why isn't it NOP_EXPR?)?
> 
> Dunno, but I can prepare a separate patch (likely stage1 material,
> right)? Note that are other places that also use CONVERT_EXPR.
> 
> Patch can bootstrap on x86_64-linux-gnu and survives regression tests.
> 
> Ready to be installed?
> Thanks,
> Martin

> From 227450e9f3a506fdfcff67aa45135fe31f3f91f6 Mon Sep 17 00:00:00 2001
> From: Martin Liska <mliska@suse.cz>
> Date: Mon, 13 Dec 2021 15:34:30 +0100
> Subject: [PATCH] i386: Fix emissing of __builtin_cpu_supports.
> 
> 	PR target/103661
> 
> gcc/ChangeLog:
> 
> 	* config/i386/i386-builtins.c (fold_builtin_cpu): Compare to 0
> 	as API expects that non-zero values are returned (do that
> 	it mask == 31).
> 	For "avx512vbmi2" argument, we return now 1 << 31, which is a
> 	negative integer value.
> ---
>  gcc/config/i386/i386-builtins.c | 6 +++++-
>  1 file changed, 5 insertions(+), 1 deletion(-)
> 
> diff --git a/gcc/config/i386/i386-builtins.c b/gcc/config/i386/i386-builtins.c
> index 0fb14b55712..bca244fc011 100644
> --- a/gcc/config/i386/i386-builtins.c
> +++ b/gcc/config/i386/i386-builtins.c
> @@ -2353,7 +2353,11 @@ fold_builtin_cpu (tree fndecl, tree *args)
>        /* Return __cpu_model.__cpu_features[0] & field_val  */
>        final = build2 (BIT_AND_EXPR, unsigned_type_node, array_elt,
>  		      build_int_cstu (unsigned_type_node, field_val));
> -      return build1 (CONVERT_EXPR, integer_type_node, final);
> +      if (isa_names_table[i].feature == 31)
> +	return build2 (NE_EXPR, integer_type_node, final,
> +		       build_int_cst (unsigned_type_node, 0));
> +      else
> +	return build1 (CONVERT_EXPR, integer_type_node, final);
>      }
>    gcc_unreachable ();
>  }

I'd use INT_TYPE_SIZE - 1 instead of 31.  Otherwise LGTM.

	Jakub
  

Patch

diff --git a/gcc/config/i386/i386-builtins.c b/gcc/config/i386/i386-builtins.c
index 0fb14b55712..7e57b665c1e 100644
--- a/gcc/config/i386/i386-builtins.c
+++ b/gcc/config/i386/i386-builtins.c
@@ -2353,7 +2353,8 @@  fold_builtin_cpu (tree fndecl, tree *args)
        /* Return __cpu_model.__cpu_features[0] & field_val  */
        final = build2 (BIT_AND_EXPR, unsigned_type_node, array_elt,
  		      build_int_cstu (unsigned_type_node, field_val));
-      return build1 (CONVERT_EXPR, integer_type_node, final);
+      return build2 (NE_EXPR, integer_type_node, final,
+		     build_int_cst (unsigned_type_node, 0));
      }
    gcc_unreachable ();
  }