Skip to content

Commit 2586690

Browse files
committed
WIP: Add support for function attributes
1 parent c041233 commit 2586690

7 files changed

+73
-4
lines changed

gcc/jit/jit-playback.cc

+30-1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ along with GCC; see the file COPYING3. If not see
1919
<http://www.gnu.org/licenses/>. */
2020

2121
#include "config.h"
22+
#include "libgccjit.h"
2223
#include "system.h"
2324
#include "coretypes.h"
2425
#include "target.h"
@@ -505,6 +506,16 @@ new_param (location *loc,
505506
return new param (this, inner);
506507
}
507508

509+
const char* fn_attribute_to_string(gcc_jit_fn_attribute attr)
510+
{
511+
switch (attr)
512+
{
513+
case GCC_JIT_FN_ATTRIBUTE_TARGET:
514+
return "target";
515+
}
516+
return NULL;
517+
}
518+
508519
/* Construct a playback::function instance. */
509520

510521
playback::function *
@@ -516,7 +527,8 @@ new_function (location *loc,
516527
const auto_vec<param *> *params,
517528
int is_variadic,
518529
enum built_in_function builtin_id,
519-
int is_target_builtin)
530+
int is_target_builtin,
531+
const std::vector<std::pair<gcc_jit_fn_attribute, std::string>> &attributes)
520532
{
521533
int i;
522534
param *param;
@@ -609,6 +621,23 @@ new_function (location *loc,
609621
DECL_ATTRIBUTES (fndecl));
610622
}
611623

624+
for (auto attr: attributes)
625+
{
626+
gcc_jit_fn_attribute& name = std::get<0>(attr);
627+
std::string& value = std::get<1>(attr);
628+
tree attribute_value = build_tree_list (NULL_TREE, ::build_string (value.length () + 1, value.c_str ()));
629+
tree ident = get_identifier (fn_attribute_to_string (name));
630+
631+
/* See handle_target_attribute in gcc/c-family/c-attribs.cc. */
632+
if (name == GCC_JIT_FN_ATTRIBUTE_TARGET)
633+
/* We need to call valid_attribute_p so that the hook set-up some internal options. */
634+
if (!targetm.target_option.valid_attribute_p (fndecl, ident, attribute_value, 0))
635+
continue;
636+
637+
DECL_ATTRIBUTES (fndecl) =
638+
tree_cons (ident, attribute_value, DECL_ATTRIBUTES (fndecl));
639+
}
640+
612641
function *func = new function (this, fndecl, kind);
613642
m_functions.safe_push (func);
614643
return func;

gcc/jit/jit-playback.h

+4-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,9 @@ along with GCC; see the file COPYING3. If not see
2121
#ifndef JIT_PLAYBACK_H
2222
#define JIT_PLAYBACK_H
2323

24+
#include <string>
2425
#include <utility> // for std::pair
26+
#include <vector>
2527

2628
#include "timevar.h"
2729
#include "varasm.h"
@@ -106,7 +108,8 @@ class context : public log_user
106108
const auto_vec<param *> *params,
107109
int is_variadic,
108110
enum built_in_function builtin_id,
109-
int is_target_builtin);
111+
int is_target_builtin,
112+
const std::vector<std::pair<gcc_jit_fn_attribute, std::string>> &attributes);
110113

111114
lvalue *
112115
new_global (location *loc,

gcc/jit/jit-recording.cc

+10-2
Original file line numberDiff line numberDiff line change
@@ -4176,7 +4176,8 @@ recording::function::function (context *ctxt,
41764176
m_locals (),
41774177
m_blocks (),
41784178
m_fn_ptr_type (NULL),
4179-
m_is_target_builtin (is_target_builtin)
4179+
m_is_target_builtin (is_target_builtin),
4180+
m_attributes()
41804181
{
41814182
for (int i = 0; i< num_params; i++)
41824183
{
@@ -4236,7 +4237,8 @@ recording::function::replay_into (replayer *r)
42364237
&params,
42374238
m_is_variadic,
42384239
m_builtin_id,
4239-
m_is_target_builtin));
4240+
m_is_target_builtin,
4241+
m_attributes));
42404242
}
42414243

42424244
/* Create a recording::local instance and add it to
@@ -4480,6 +4482,12 @@ recording::function::get_address (recording::location *loc)
44804482
return result;
44814483
}
44824484

4485+
void
4486+
recording::function::add_attribute (gcc_jit_fn_attribute attribute, const char* value)
4487+
{
4488+
m_attributes.push_back (std::make_pair (attribute, std::string (value)));
4489+
}
4490+
44834491
/* Implementation of recording::memento::make_debug_string for
44844492
functions. */
44854493

gcc/jit/jit-recording.h

+5
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,11 @@ along with GCC; see the file COPYING3. If not see
2323

2424
#include "jit-common.h"
2525
#include "jit-logging.h"
26+
#include "libgccjit.h"
2627

2728
#include <string>
2829
#include <unordered_map>
30+
#include <vector>
2931

3032
class timer;
3133

@@ -1419,6 +1421,8 @@ class function : public memento
14191421

14201422
rvalue *get_address (location *loc);
14211423

1424+
void add_attribute (gcc_jit_fn_attribute attribute, const char* value);
1425+
14221426
private:
14231427
string * make_debug_string () FINAL OVERRIDE;
14241428
void write_reproducer (reproducer &r) FINAL OVERRIDE;
@@ -1435,6 +1439,7 @@ class function : public memento
14351439
auto_vec<block *> m_blocks;
14361440
type *m_fn_ptr_type;
14371441
int m_is_target_builtin;
1442+
std::vector<std::pair<gcc_jit_fn_attribute, std::string>> m_attributes;
14381443
};
14391444

14401445
class block : public memento

gcc/jit/libgccjit.cc

+8
Original file line numberDiff line numberDiff line change
@@ -4082,6 +4082,14 @@ gcc_jit_type_set_packed (gcc_jit_type *type)
40824082
type->set_packed ();
40834083
}
40844084

4085+
void
4086+
gcc_jit_function_add_attribute (gcc_jit_function *func, gcc_jit_fn_attribute attribute, const char* value)
4087+
{
4088+
RETURN_IF_FAIL (func, NULL, NULL, "NULL func");
4089+
4090+
func->add_attribute (attribute, value);
4091+
}
4092+
40854093
/* Public entrypoint. See description in libgccjit.h.
40864094
40874095
After error-checking, the real work is done by the

gcc/jit/libgccjit.h

+11
Original file line numberDiff line numberDiff line change
@@ -2037,6 +2037,17 @@ gcc_jit_type_unqualified (gcc_jit_type *type);
20372037
extern void
20382038
gcc_jit_type_set_packed (gcc_jit_type *type);
20392039

2040+
/* Function attributes. */
2041+
enum gcc_jit_fn_attribute
2042+
{
2043+
GCC_JIT_FN_ATTRIBUTE_TARGET,
2044+
};
2045+
2046+
/* Add an attribute to a function. */
2047+
// TODO: also support integer values.
2048+
extern void
2049+
gcc_jit_function_add_attribute (gcc_jit_function *func, gcc_jit_fn_attribute attribute, const char* value);
2050+
20402051
#ifdef __cplusplus
20412052
}
20422053
#endif /* __cplusplus */

gcc/jit/libgccjit.map

+5
Original file line numberDiff line numberDiff line change
@@ -298,3 +298,8 @@ LIBGCCJIT_ABI_29 {
298298
global:
299299
gcc_jit_global_set_readonly;
300300
} LIBGCCJIT_ABI_28;
301+
302+
LIBGCCJIT_ABI_30 {
303+
global:
304+
gcc_jit_function_add_attribute;
305+
} LIBGCCJIT_ABI_29;

0 commit comments

Comments
 (0)