Skip to content

Commit 3859e30

Browse files
committed
WIP: Add support for function attributes
1 parent b70e0e1 commit 3859e30

File tree

7 files changed

+118
-4
lines changed

7 files changed

+118
-4
lines changed

gcc/jit/jit-playback.cc

+50-1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ along with GCC; see the file COPYING3. If not see
2020

2121
#include "config.h"
2222
#define INCLUDE_MUTEX
23+
#include "libgccjit.h"
2324
#include "system.h"
2425
#include "coretypes.h"
2526
#include "target.h"
@@ -507,6 +508,20 @@ new_param (location *loc,
507508
return new param (this, inner);
508509
}
509510

511+
const char* fn_attribute_to_string(gcc_jit_fn_attribute attr)
512+
{
513+
switch (attr)
514+
{
515+
case GCC_JIT_FN_ATTRIBUTE_TARGET:
516+
return "target";
517+
case GCC_JIT_FN_ATTRIBUTE_USED:
518+
return "used";
519+
case GCC_JIT_FN_ATTRIBUTE_VISIBILITY:
520+
return "visibility";
521+
}
522+
return NULL;
523+
}
524+
510525
/* Construct a playback::function instance. */
511526

512527
playback::function *
@@ -518,7 +533,9 @@ new_function (location *loc,
518533
const auto_vec<param *> *params,
519534
int is_variadic,
520535
enum built_in_function builtin_id,
521-
int is_target_builtin)
536+
int is_target_builtin,
537+
const std::vector<gcc_jit_fn_attribute> &attributes,
538+
const std::vector<std::pair<gcc_jit_fn_attribute, std::string>> &string_attributes)
522539
{
523540
int i;
524541
param *param;
@@ -611,6 +628,38 @@ new_function (location *loc,
611628
DECL_ATTRIBUTES (fndecl));
612629
}
613630

631+
for (auto attr: attributes)
632+
{
633+
tree ident = get_identifier (fn_attribute_to_string (attr));
634+
635+
/* See handle_used_attribute in gcc/c-family/c-attribs.cc. */
636+
if (attr == GCC_JIT_FN_ATTRIBUTE_USED)
637+
{
638+
TREE_USED (fndecl) = 1;
639+
DECL_PRESERVE_P (fndecl) = 1;
640+
}
641+
642+
DECL_ATTRIBUTES (fndecl) =
643+
tree_cons (ident, NULL_TREE, DECL_ATTRIBUTES (fndecl));
644+
}
645+
646+
for (auto attr: string_attributes)
647+
{
648+
gcc_jit_fn_attribute& name = std::get<0>(attr);
649+
std::string& value = std::get<1>(attr);
650+
tree attribute_value = build_tree_list (NULL_TREE, ::build_string (value.length () + 1, value.c_str ()));
651+
tree ident = get_identifier (fn_attribute_to_string (name));
652+
653+
/* See handle_target_attribute in gcc/c-family/c-attribs.cc. */
654+
if (name == GCC_JIT_FN_ATTRIBUTE_TARGET)
655+
/* We need to call valid_attribute_p so that the hook set-up some internal options. */
656+
if (!targetm.target_option.valid_attribute_p (fndecl, ident, attribute_value, 0))
657+
continue;
658+
659+
DECL_ATTRIBUTES (fndecl) =
660+
tree_cons (ident, attribute_value, DECL_ATTRIBUTES (fndecl));
661+
}
662+
614663
function *func = new function (this, fndecl, kind);
615664
m_functions.safe_push (func);
616665
return func;

gcc/jit/jit-playback.h

+5-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,9 @@ 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<gcc_jit_fn_attribute> &attributes,
113+
const std::vector<std::pair<gcc_jit_fn_attribute, std::string>> &string_attributes);
110114

111115
lvalue *
112116
new_global (location *loc,

gcc/jit/jit-recording.cc

+18-2
Original file line numberDiff line numberDiff line change
@@ -4172,7 +4172,9 @@ recording::function::function (context *ctxt,
41724172
m_locals (),
41734173
m_blocks (),
41744174
m_fn_ptr_type (NULL),
4175-
m_is_target_builtin (is_target_builtin)
4175+
m_is_target_builtin (is_target_builtin),
4176+
m_attributes(),
4177+
m_string_attributes()
41764178
{
41774179
for (int i = 0; i< num_params; i++)
41784180
{
@@ -4232,7 +4234,9 @@ recording::function::replay_into (replayer *r)
42324234
&params,
42334235
m_is_variadic,
42344236
m_builtin_id,
4235-
m_is_target_builtin));
4237+
m_is_target_builtin,
4238+
m_attributes,
4239+
m_string_attributes));
42364240
}
42374241

42384242
/* Create a recording::local instance and add it to
@@ -4476,6 +4480,18 @@ recording::function::get_address (recording::location *loc)
44764480
return result;
44774481
}
44784482

4483+
void
4484+
recording::function::add_attribute (gcc_jit_fn_attribute attribute)
4485+
{
4486+
m_attributes.push_back (attribute);
4487+
}
4488+
4489+
void
4490+
recording::function::add_string_attribute (gcc_jit_fn_attribute attribute, const char* value)
4491+
{
4492+
m_string_attributes.push_back (std::make_pair (attribute, std::string (value)));
4493+
}
4494+
44794495
/* Implementation of recording::memento::make_debug_string for
44804496
functions. */
44814497

gcc/jit/jit-recording.h

+7
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

@@ -1428,6 +1430,9 @@ class function : public memento
14281430

14291431
rvalue *get_address (location *loc);
14301432

1433+
void add_attribute (gcc_jit_fn_attribute attribute);
1434+
void add_string_attribute (gcc_jit_fn_attribute attribute, const char* value);
1435+
14311436
private:
14321437
string * make_debug_string () final override;
14331438
void write_reproducer (reproducer &r) final override;
@@ -1444,6 +1449,8 @@ class function : public memento
14441449
auto_vec<block *> m_blocks;
14451450
type *m_fn_ptr_type;
14461451
int m_is_target_builtin;
1452+
std::vector<gcc_jit_fn_attribute> m_attributes;
1453+
std::vector<std::pair<gcc_jit_fn_attribute, std::string>> m_string_attributes;
14471454
};
14481455

14491456
class block : public memento

gcc/jit/libgccjit.cc

+16
Original file line numberDiff line numberDiff line change
@@ -4094,6 +4094,22 @@ gcc_jit_type_set_packed (gcc_jit_type *type)
40944094
type->set_packed ();
40954095
}
40964096

4097+
void
4098+
gcc_jit_function_add_attribute (gcc_jit_function *func, gcc_jit_fn_attribute attribute)
4099+
{
4100+
RETURN_IF_FAIL (func, NULL, NULL, "NULL func");
4101+
4102+
func->add_attribute (attribute);
4103+
}
4104+
4105+
void
4106+
gcc_jit_function_add_string_attribute (gcc_jit_function *func, gcc_jit_fn_attribute attribute, const char* value)
4107+
{
4108+
RETURN_IF_FAIL (func, NULL, NULL, "NULL func");
4109+
4110+
func->add_string_attribute (attribute, value);
4111+
}
4112+
40974113
/* Public entrypoint. See description in libgccjit.h.
40984114
40994115
After error-checking, the real work is done by the

gcc/jit/libgccjit.h

+16
Original file line numberDiff line numberDiff line change
@@ -2045,6 +2045,22 @@ gcc_jit_type_unqualified (gcc_jit_type *type);
20452045
extern void
20462046
gcc_jit_type_set_packed (gcc_jit_type *type);
20472047

2048+
/* Function attributes. */
2049+
enum gcc_jit_fn_attribute
2050+
{
2051+
GCC_JIT_FN_ATTRIBUTE_TARGET,
2052+
GCC_JIT_FN_ATTRIBUTE_USED,
2053+
GCC_JIT_FN_ATTRIBUTE_VISIBILITY,
2054+
};
2055+
2056+
/* Add an attribute to a function. */
2057+
// TODO: also support integer values.
2058+
extern void
2059+
gcc_jit_function_add_attribute (gcc_jit_function *func, enum gcc_jit_fn_attribute attribute);
2060+
2061+
extern void
2062+
gcc_jit_function_add_string_attribute (gcc_jit_function *func, enum gcc_jit_fn_attribute attribute, const char* value);
2063+
20482064
#ifdef __cplusplus
20492065
}
20502066
#endif /* __cplusplus */

gcc/jit/libgccjit.map

+6
Original file line numberDiff line numberDiff line change
@@ -299,3 +299,9 @@ LIBGCCJIT_ABI_29 {
299299
global:
300300
gcc_jit_global_set_readonly;
301301
} LIBGCCJIT_ABI_28;
302+
303+
LIBGCCJIT_ABI_30 {
304+
global:
305+
gcc_jit_function_add_attribute;
306+
gcc_jit_function_add_string_attribute;
307+
} LIBGCCJIT_ABI_29;

0 commit comments

Comments
 (0)