Skip to content

Commit

Permalink
Add support for __jcvt intrinsic
Browse files Browse the repository at this point in the history
This patch implements the __jcvt ACLE intrinsic [1] that maps down to the FJCVTZS [2] instruction from Armv8.3-a.
No fancy mode iterators or nothing. Just a single builtin, UNSPEC and define_insn and the associate plumbing.
This patch also defines __ARM_FEATURE_JCVT to indicate when the intrinsic is available.

[1] https://developer.arm.com/docs/101028/latest/data-processing-intrinsics
[2] https://developer.arm.com/docs/ddi0596/latest/simd-and-floating-point-instructions-alphabetic-order/fjcvtzs-floating-point-javascript-convert-to-signed-fixed-point-rounding-toward-zero

gcc/
	PR target/71233
	* config/aarch64/aarch64.md (UNSPEC_FJCVTZS): Define.
	(aarch64_fjcvtzs): New define_insn.
	* config/aarch64/aarch64.h (TARGET_JSCVT): Define.
	* config/aarch64/aarch64-builtins.c (aarch64_builtins):
	Add AARCH64_JSCVT.
	(aarch64_init_builtins): Initialize __builtin_aarch64_jcvtzs.
	(aarch64_expand_builtin): Handle AARCH64_JSCVT.
	* config/aarch64/aarch64-c.c (aarch64_update_cpp_builtins): Define
	__ARM_FEATURE_JCVT where appropriate.
	* config/aarch64/arm_acle.h (__jcvt): Define.
	* doc/sourcebuild.texi (aarch64_fjcvtzs_hw) Document new
	target supports option.

gcc/testsuite/
	PR target/71233
	* gcc.target/aarch64/acle/jcvt_1.c: New test.
	* gcc.target/aarch64/acle/jcvt_2.c: New testcase.
	* lib/target-supports.exp
	(check_effective_target_aarch64_fjcvtzs_hw): Add new check for
	FJCVTZS hw.

Co-Authored-By: Andrea Corallo  <andrea.corallo@arm.com>

(cherry picked from commit e1d5d19)
(cherry picked from commit 2c62952)
(cherry picked from commit d2b86e1)
  • Loading branch information
Kyrylo Tkachov committed Sep 24, 2020
1 parent 6ca87f2 commit 6f06be1
Show file tree
Hide file tree
Showing 9 changed files with 119 additions and 0 deletions.
18 changes: 18 additions & 0 deletions gcc/config/aarch64/aarch64-builtins.c
Original file line number Diff line number Diff line change
Expand Up @@ -436,6 +436,8 @@ enum aarch64_builtins
/* Special cased Armv8.3-A Complex FMA by Lane quad Builtins. */
AARCH64_SIMD_FCMLA_LANEQ_BUILTIN_BASE,
AARCH64_SIMD_FCMLA_LANEQ_BUILTINS
/* Builtin for Arm8.3-a Javascript conversion instruction. */
AARCH64_JSCVT,
AARCH64_BUILTIN_MAX
};

Expand Down Expand Up @@ -1086,6 +1088,12 @@ aarch64_init_builtins (void)
aarch64_init_crc32_builtins ();
aarch64_init_builtin_rsqrt ();

tree ftype_jcvt
= build_function_type_list (intSI_type_node, double_type_node, NULL);
aarch64_builtin_decls[AARCH64_JSCVT]
= add_builtin_function ("__builtin_aarch64_jcvtzs", ftype_jcvt,
AARCH64_JSCVT, BUILT_IN_MD, NULL, NULL_TREE);

/* Initialize pointer authentication builtins which are backed by instructions
in NOP encoding space.
Expand Down Expand Up @@ -1576,6 +1584,16 @@ aarch64_expand_builtin (tree exp,

return target;

case AARCH64_JSCVT:
{
expand_operand ops[2];
create_output_operand (&ops[0], target, SImode);
op0 = expand_normal (CALL_EXPR_ARG (exp, 0));
create_input_operand (&ops[1], op0, DFmode);
expand_insn (CODE_FOR_aarch64_fjcvtzs, 2, ops);
return ops[0].value;
}

case AARCH64_SIMD_BUILTIN_FCMLA_LANEQ0_V2SF:
case AARCH64_SIMD_BUILTIN_FCMLA_LANEQ90_V2SF:
case AARCH64_SIMD_BUILTIN_FCMLA_LANEQ180_V2SF:
Expand Down
1 change: 1 addition & 0 deletions gcc/config/aarch64/aarch64-c.c
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ aarch64_update_cpp_builtins (cpp_reader *pfile)
aarch64_def_or_undef (TARGET_CRC32, "__ARM_FEATURE_CRC32", pfile);
aarch64_def_or_undef (TARGET_DOTPROD, "__ARM_FEATURE_DOTPROD", pfile);
aarch64_def_or_undef (TARGET_COMPLEX, "__ARM_FEATURE_COMPLEX", pfile);
aarch64_def_or_undef (TARGET_JSCVT, "__ARM_FEATURE_JCVT", pfile);

cpp_undef (pfile, "__AARCH64_CMODEL_TINY__");
cpp_undef (pfile, "__AARCH64_CMODEL_SMALL__");
Expand Down
3 changes: 3 additions & 0 deletions gcc/config/aarch64/aarch64.h
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,9 @@ extern unsigned aarch64_architecture_version;
/* ARMv8.3-A features. */
#define TARGET_ARMV8_3 (AARCH64_ISA_V8_3)

/* Javascript conversion instruction from Armv8.3-a. */
#define TARGET_JSCVT (TARGET_FLOAT && AARCH64_ISA_V8_3)

/* Armv8.3-a Complex number extension to AdvSIMD extensions. */
#define TARGET_COMPLEX (TARGET_SIMD && TARGET_ARMV8_3)

Expand Down
11 changes: 11 additions & 0 deletions gcc/config/aarch64/aarch64.md
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,7 @@
UNSPEC_CRC32X
UNSPEC_FCVTZS
UNSPEC_FCVTZU
UNSPEC_FJCVTZS
UNSPEC_URECPE
UNSPEC_FRECPE
UNSPEC_FRECPS
Expand Down Expand Up @@ -6836,6 +6837,16 @@
[(set_attr "length" "0")]
)

(define_insn "aarch64_fjcvtzs"
[(set (match_operand:SI 0 "register_operand" "=r")
(unspec:SI [(match_operand:DF 1 "register_operand" "w")]
UNSPEC_FJCVTZS))
(clobber (reg:CC CC_REGNUM))]
"TARGET_JSCVT"
"fjcvtzs\\t%w0, %d1"
[(set_attr "type" "f_cvtf2i")]
)

;; Pointer authentication patterns are always provided. In architecture
;; revisions prior to ARMv8.3-A these HINT instructions operate as NOPs.
;; This lets the user write portable software which authenticates pointers
Expand Down
14 changes: 14 additions & 0 deletions gcc/config/aarch64/arm_acle.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,20 @@

#include <stdint.h>

#ifdef __cplusplus
extern "C" {
#endif

#pragma GCC push_options
#pragma GCC target ("arch=armv8.3-a")
__extension__ static __inline int32_t __attribute__ ((__always_inline__))
__jcvt (double __a)
{
return __builtin_aarch64_jcvtzs (__a);
}

#pragma GCC pop_options

#pragma GCC push_options

#pragma GCC target ("+nothing+crc")
Expand Down
3 changes: 3 additions & 0 deletions gcc/doc/sourcebuild.texi
Original file line number Diff line number Diff line change
Expand Up @@ -1927,6 +1927,9 @@ AArch64 target which generates instruction sequences for big endian.
Binutils installed on test system supports relocation types required by -fpic
for AArch64 small memory model.

@item aarch64_fjcvtzs_hw
AArch64 target that is able to generate and execute armv8.3-a FJCVTZS
instruction.
@end table

@subsubsection MIPS-specific attributes
Expand Down
15 changes: 15 additions & 0 deletions gcc/testsuite/gcc.target/aarch64/acle/jcvt_1.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
/* Test the __jcvt ACLE intrinsic. */
/* { dg-do compile } */
/* { dg-options "-O2 -march=armv8.3-a" } */

#include <arm_acle.h>

#ifdef __ARM_FEATURE_JCVT
int32_t
test_jcvt (double a)
{
return __jcvt (a);
}
#endif

/* { dg-final { scan-assembler-times "fjcvtzs\tw\[0-9\]+, d\[0-9\]+\n" 1 } } */
33 changes: 33 additions & 0 deletions gcc/testsuite/gcc.target/aarch64/acle/jcvt_2.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/* Test the __jcvt ACLE intrinsic. */
/* { dg-do run } */
/* { dg-options "-O2 -march=armv8.3-a -save-temps" } */
/* { dg-require-effective-target aarch64_fjcvtzs_hw } */

#include <arm_acle.h>

extern void abort (void);

#ifdef __ARM_FEATURE_JCVT
volatile int32_t x;

int __attribute__((noinline))
foo (double a, int b, int c)
{
b = b > c;
x = __jcvt (a);
return b;
}

int
main (void)
{
int x = foo (1.1, 2, 3);
if (x)
abort ();

return 0;
}

#endif

/* { dg-final { scan-assembler-times "fjcvtzs\tw\[0-9\]+, d\[0-9\]+\n" 1 } } */
21 changes: 21 additions & 0 deletions gcc/testsuite/lib/target-supports.exp
Original file line number Diff line number Diff line change
Expand Up @@ -4414,6 +4414,27 @@ proc check_effective_target_aarch64_bti_hw { } {
} "-O2" ]
}

# Return 1 if the target supports executing the armv8.3-a FJCVTZS
# instruction.
proc check_effective_target_aarch64_fjcvtzs_hw { } {
if { ![istarget aarch64*-*-*] } {
return 0
}
return [check_runtime aarch64_fjcvtzs_hw_available {
int
main (void)
{
double in = 25.1;
int out;
asm volatile ("fjcvtzs %w0, %d1"
: "=r" (out)
: "w" (in)
: /* No clobbers. */);
return out != 25;
}
} "-march=armv8.3-a" ]
}

# Return 1 if GCC was configured with --enable-standard-branch-protection
proc check_effective_target_default_branch_protection { } {
return [check_configured_with "enable-standard-branch-protection"]
Expand Down

0 comments on commit 6f06be1

Please sign in to comment.