Skip to content

Commit 03a7071

Browse files
authored
Merge pull request #21 from GuillaumeGomez/pure-attr
Add `pure` attribute support in libgccjit
2 parents b4e6fe3 + 6dae9ee commit 03a7071

File tree

5 files changed

+175
-4
lines changed

5 files changed

+175
-4
lines changed

gcc/jit/jit-playback.cc

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -672,6 +672,9 @@ new_function (location *loc,
672672
/* See handle_returns_twice_attribute in gcc/c-family/c-attribs.cc. */
673673
else if (attr == GCC_JIT_FN_ATTRIBUTE_RETURNS_TWICE)
674674
DECL_IS_RETURNS_TWICE (fndecl) = 1;
675+
/* See handle_pure_attribute in gcc/c-family/c-attribs.cc. */
676+
else if (attr == GCC_JIT_FN_ATTRIBUTE_PURE)
677+
DECL_PURE_P (fndecl) = 1;
675678

676679
const char* attribute = fn_attribute_to_string (attr);
677680
if (attribute)

gcc/jit/libgccjit.cc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3874,7 +3874,7 @@ gcc_jit_target_info_release (gcc_jit_target_info *info)
38743874
delete info;
38753875
}
38763876

3877-
bool
3877+
int
38783878
gcc_jit_target_info_cpu_supports (gcc_jit_target_info *info,
38793879
const char *feature)
38803880
{
@@ -3887,7 +3887,7 @@ gcc_jit_target_info_arch (gcc_jit_target_info *info)
38873887
return info->m_arch;
38883888
}
38893889

3890-
bool
3890+
int
38913891
gcc_jit_target_info_supports_128bit_int (gcc_jit_target_info *info)
38923892
{
38933893
return info->m_supports_128bit_int;

gcc/jit/libgccjit.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2080,14 +2080,14 @@ gcc_jit_context_get_target_info (gcc_jit_context *ctxt);
20802080
extern void
20812081
gcc_jit_target_info_release (gcc_jit_target_info *info);
20822082

2083-
extern bool
2083+
extern int
20842084
gcc_jit_target_info_cpu_supports (gcc_jit_target_info *info,
20852085
const char *feature);
20862086

20872087
extern const char *
20882088
gcc_jit_target_info_arch (gcc_jit_target_info *info);
20892089

2090-
extern bool
2090+
extern int
20912091
gcc_jit_target_info_supports_128bit_int (gcc_jit_target_info *info);
20922092

20932093
/* Given type "T", get type "T __attribute__ ((packed))". */
@@ -2105,6 +2105,7 @@ enum gcc_jit_fn_attribute
21052105
GCC_JIT_FN_ATTRIBUTE_VISIBILITY,
21062106
GCC_JIT_FN_ATTRIBUTE_COLD,
21072107
GCC_JIT_FN_ATTRIBUTE_RETURNS_TWICE,
2108+
GCC_JIT_FN_ATTRIBUTE_PURE,
21082109
};
21092110

21102111
/* Add an attribute to a function. */

gcc/testsuite/jit.dg/jit.exp

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -895,8 +895,41 @@ proc jit-verify-assembler-output { args } {
895895
pass "${asm_filename} output pattern test, ${dg-output-text}"
896896
verbose "Passed test for output pattern ${dg-output-text}" 3
897897
}
898+
}
899+
900+
# Assuming that a .s file has been written out named
901+
# OUTPUT_FILENAME, check that the argument doesn't match
902+
# the output file.
903+
proc jit-verify-assembler-output-not { args } {
904+
verbose "jit-verify-assembler: $args"
905+
906+
set dg-output-text [lindex $args 0]
907+
verbose "dg-output-text: ${dg-output-text}"
898908

909+
upvar 2 name name
910+
verbose "name: $name"
911+
912+
upvar 2 prog prog
913+
verbose "prog: $prog"
914+
set asm_filename [jit-get-output-filename $prog]
915+
verbose " asm_filename: ${asm_filename}"
916+
917+
# Read the assembly file.
918+
set f [open $asm_filename r]
919+
set content [read $f]
920+
close $f
921+
922+
# Verify that the assembly matches the regex.
923+
if { [regexp ${dg-output-text} $content] } {
924+
fail "${asm_filename} output pattern test, is ${content}, should match ${dg-output-text}"
925+
verbose "Failed test for output pattern ${dg-output-text}" 3
926+
} else {
927+
pass "${asm_filename} output pattern test, ${dg-output-text}"
928+
verbose "Passed test for output pattern ${dg-output-text}" 3
929+
}
899930
}
931+
932+
900933
# Assuming that a .o file has been written out named
901934
# OUTPUT_FILENAME, invoke the driver to try to turn it into
902935
# an executable, and try to run the result.
Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
1+
/* { dg-do compile { target x86_64-*-* } } */
2+
3+
#include <stdlib.h>
4+
#include <stdio.h>
5+
6+
#include "libgccjit.h"
7+
8+
/* We don't want set_options() in harness.h to set -O3 to see that the cold
9+
attribute affects the optimizations. */
10+
#define TEST_ESCHEWS_SET_OPTIONS
11+
static void set_options (gcc_jit_context *ctxt, const char *argv0)
12+
{
13+
// Set "-O3".
14+
gcc_jit_context_set_int_option(ctxt, GCC_JIT_INT_OPTION_OPTIMIZATION_LEVEL, 3);
15+
}
16+
17+
#define TEST_COMPILING_TO_FILE
18+
#define OUTPUT_KIND GCC_JIT_OUTPUT_KIND_ASSEMBLER
19+
#define OUTPUT_FILENAME "output-of-test-pure-attribute.c.s"
20+
#include "harness.h"
21+
22+
void
23+
create_code (gcc_jit_context *ctxt, void *user_data)
24+
{
25+
/* Let's try to inject the equivalent of:
26+
__attribute__ ((pure))
27+
int foo (int x);
28+
int xxx(void)
29+
{
30+
int x = 45;
31+
int sum = 0;
32+
33+
while (x >>= 1)
34+
sum += foo (x) * 2;
35+
return sum;
36+
}
37+
*/
38+
gcc_jit_type *int_type =
39+
gcc_jit_context_get_type (ctxt, GCC_JIT_TYPE_INT);
40+
41+
/* Creating the `foo` function. */
42+
gcc_jit_param *n =
43+
gcc_jit_context_new_param (ctxt, NULL, int_type, "x");
44+
gcc_jit_param *params[1] = {n};
45+
gcc_jit_function *foo_func =
46+
gcc_jit_context_new_function (ctxt, NULL,
47+
GCC_JIT_FUNCTION_IMPORTED,
48+
int_type,
49+
"foo",
50+
1, params,
51+
0);
52+
gcc_jit_function_add_attribute(foo_func, GCC_JIT_FN_ATTRIBUTE_PURE);
53+
54+
/* Creating the `xxx` function. */
55+
gcc_jit_function *xxx_func =
56+
gcc_jit_context_new_function (ctxt, NULL,
57+
GCC_JIT_FUNCTION_EXPORTED,
58+
int_type,
59+
"xxx",
60+
0, NULL,
61+
0);
62+
63+
gcc_jit_block *block = gcc_jit_function_new_block (xxx_func, NULL);
64+
65+
/* Build locals: */
66+
gcc_jit_lvalue *x =
67+
gcc_jit_function_new_local (xxx_func, NULL, int_type, "x");
68+
gcc_jit_lvalue *sum =
69+
gcc_jit_function_new_local (xxx_func, NULL, int_type, "sum");
70+
71+
/* int x = 45 */
72+
gcc_jit_block_add_assignment (
73+
block, NULL,
74+
x,
75+
gcc_jit_context_new_rvalue_from_int (ctxt, int_type, 45));
76+
/* int sum = 0 */
77+
gcc_jit_block_add_assignment (
78+
block, NULL,
79+
sum,
80+
gcc_jit_context_new_rvalue_from_int (ctxt, int_type, 0));
81+
82+
/* while (x >>= 1) { sum += foo (x) * 2; } */
83+
gcc_jit_block *loop_cond =
84+
gcc_jit_function_new_block (xxx_func, "loop_cond");
85+
gcc_jit_block *loop_body =
86+
gcc_jit_function_new_block (xxx_func, "loop_body");
87+
gcc_jit_block *after_loop =
88+
gcc_jit_function_new_block (xxx_func, "after_loop");
89+
90+
gcc_jit_block_end_with_jump (block, NULL, loop_cond);
91+
92+
93+
/* if (x >>= 1) */
94+
/* Since gccjit doesn't (yet?) have support for `>>=` operator, we will decompose it into:
95+
`if (x = x >> 1)` */
96+
gcc_jit_block_add_assignment_op (
97+
loop_cond, NULL,
98+
x,
99+
GCC_JIT_BINARY_OP_RSHIFT,
100+
gcc_jit_context_new_rvalue_from_int (ctxt, int_type, 1));
101+
/* The condition itself */
102+
gcc_jit_block_end_with_conditional (
103+
loop_cond, NULL,
104+
gcc_jit_context_new_comparison (
105+
ctxt, NULL,
106+
GCC_JIT_COMPARISON_NE,
107+
gcc_jit_lvalue_as_rvalue (x),
108+
gcc_jit_context_new_rvalue_from_int (ctxt, int_type, 0)),
109+
after_loop,
110+
loop_body);
111+
112+
/* sum += foo (x) * 2; */
113+
gcc_jit_rvalue *arg = gcc_jit_lvalue_as_rvalue(x);
114+
gcc_jit_block_add_assignment_op (
115+
loop_body, NULL,
116+
x,
117+
GCC_JIT_BINARY_OP_PLUS,
118+
gcc_jit_context_new_binary_op (
119+
ctxt, NULL,
120+
GCC_JIT_BINARY_OP_MULT, int_type,
121+
gcc_jit_context_new_call (ctxt, NULL, foo_func, 1, &arg),
122+
gcc_jit_context_new_rvalue_from_int (
123+
ctxt,
124+
int_type,
125+
2)));
126+
gcc_jit_block_end_with_jump (loop_body, NULL, loop_cond);
127+
128+
/* return sum; */
129+
gcc_jit_block_end_with_return (after_loop, NULL, gcc_jit_lvalue_as_rvalue(sum));
130+
}
131+
132+
/* { dg-final { jit-verify-output-file-was-created "" } } */
133+
/* Check that the loop was optimized away */
134+
/* { dg-final { jit-verify-assembler-output-not "jne" } } */

0 commit comments

Comments
 (0)