Skip to content

Commit 96eed4f

Browse files
committed
WIP: Add support for function attributes
1 parent 71c488c commit 96eed4f

File tree

7 files changed

+120
-4
lines changed

7 files changed

+120
-4
lines changed

gcc/jit/jit-playback.cc

Lines changed: 50 additions & 1 deletion
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 *
@@ -517,7 +532,9 @@ new_function (location *loc,
517532
const char *name,
518533
const auto_vec<param *> *params,
519534
int is_variadic,
520-
enum built_in_function builtin_id)
535+
enum built_in_function builtin_id,
536+
const std::vector<gcc_jit_fn_attribute> &attributes,
537+
const std::vector<std::pair<gcc_jit_fn_attribute, std::string>> &string_attributes)
521538
{
522539
int i;
523540
param *param;
@@ -602,6 +619,38 @@ new_function (location *loc,
602619
DECL_ATTRIBUTES (fndecl));
603620
}
604621

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

gcc/jit/jit-playback.h

Lines changed: 5 additions & 1 deletion
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"
@@ -105,7 +107,9 @@ class context : public log_user
105107
const char *name,
106108
const auto_vec<param *> *params,
107109
int is_variadic,
108-
enum built_in_function builtin_id);
110+
enum built_in_function builtin_id,
111+
const std::vector<gcc_jit_fn_attribute> &attributes,
112+
const std::vector<std::pair<gcc_jit_fn_attribute, std::string>> &string_attributes);
109113

110114
lvalue *
111115
new_global (location *loc,

gcc/jit/jit-recording.cc

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4080,7 +4080,9 @@ recording::function::function (context *ctxt,
40804080
m_builtin_id (builtin_id),
40814081
m_locals (),
40824082
m_blocks (),
4083-
m_fn_ptr_type (NULL)
4083+
m_fn_ptr_type (NULL),
4084+
m_attributes(),
4085+
m_string_attributes()
40844086
{
40854087
for (int i = 0; i< num_params; i++)
40864088
{
@@ -4139,7 +4141,9 @@ recording::function::replay_into (replayer *r)
41394141
m_name->c_str (),
41404142
&params,
41414143
m_is_variadic,
4142-
m_builtin_id));
4144+
m_builtin_id,
4145+
m_attributes,
4146+
m_string_attributes));
41434147
}
41444148

41454149
/* Create a recording::local instance and add it to
@@ -4382,6 +4386,18 @@ recording::function::get_address (recording::location *loc)
43824386
return result;
43834387
}
43844388

4389+
void
4390+
recording::function::add_attribute (gcc_jit_fn_attribute attribute)
4391+
{
4392+
m_attributes.push_back (attribute);
4393+
}
4394+
4395+
void
4396+
recording::function::add_string_attribute (gcc_jit_fn_attribute attribute, const char* value)
4397+
{
4398+
m_string_attributes.push_back (std::make_pair (attribute, std::string (value)));
4399+
}
4400+
43854401
/* Implementation of recording::memento::make_debug_string for
43864402
functions. */
43874403

gcc/jit/jit-recording.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,10 @@ 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"
27+
28+
#include <string>
29+
#include <vector>
2630

2731
class timer;
2832

@@ -1326,6 +1330,9 @@ class function : public memento
13261330

13271331
rvalue *get_address (location *loc);
13281332

1333+
void add_attribute (gcc_jit_fn_attribute attribute);
1334+
void add_string_attribute (gcc_jit_fn_attribute attribute, const char* value);
1335+
13291336
private:
13301337
string * make_debug_string () final override;
13311338
void write_reproducer (reproducer &r) final override;
@@ -1341,6 +1348,8 @@ class function : public memento
13411348
auto_vec<local *> m_locals;
13421349
auto_vec<block *> m_blocks;
13431350
type *m_fn_ptr_type;
1351+
std::vector<gcc_jit_fn_attribute> m_attributes;
1352+
std::vector<std::pair<gcc_jit_fn_attribute, std::string>> m_string_attributes;
13441353
};
13451354

13461355
class block : public memento

gcc/jit/libgccjit.cc

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4015,6 +4015,22 @@ gcc_jit_type_set_packed (gcc_jit_type *type)
40154015
type->set_packed ();
40164016
}
40174017

4018+
void
4019+
gcc_jit_function_add_attribute (gcc_jit_function *func, gcc_jit_fn_attribute attribute)
4020+
{
4021+
RETURN_IF_FAIL (func, NULL, NULL, "NULL func");
4022+
4023+
func->add_attribute (attribute);
4024+
}
4025+
4026+
void
4027+
gcc_jit_function_add_string_attribute (gcc_jit_function *func, gcc_jit_fn_attribute attribute, const char* value)
4028+
{
4029+
RETURN_IF_FAIL (func, NULL, NULL, "NULL func");
4030+
4031+
func->add_string_attribute (attribute, value);
4032+
}
4033+
40184034
/* Public entrypoint. See description in libgccjit.h.
40194035
40204036
After error-checking, the real work is done by the

gcc/jit/libgccjit.h

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2008,6 +2008,22 @@ gcc_jit_type_unqualified (gcc_jit_type *type);
20082008
extern void
20092009
gcc_jit_type_set_packed (gcc_jit_type *type);
20102010

2011+
/* Function attributes. */
2012+
enum gcc_jit_fn_attribute
2013+
{
2014+
GCC_JIT_FN_ATTRIBUTE_TARGET,
2015+
GCC_JIT_FN_ATTRIBUTE_USED,
2016+
GCC_JIT_FN_ATTRIBUTE_VISIBILITY,
2017+
};
2018+
2019+
/* Add an attribute to a function. */
2020+
// TODO: also support integer values.
2021+
extern void
2022+
gcc_jit_function_add_attribute (gcc_jit_function *func, enum gcc_jit_fn_attribute attribute);
2023+
2024+
extern void
2025+
gcc_jit_function_add_string_attribute (gcc_jit_function *func, enum gcc_jit_fn_attribute attribute, const char* value);
2026+
20112027
#ifdef __cplusplus
20122028
}
20132029
#endif /* __cplusplus */

gcc/jit/libgccjit.map

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -291,3 +291,9 @@ LIBGCCJIT_ABI_28 {
291291
global:
292292
gcc_jit_global_set_readonly;
293293
} LIBGCCJIT_ABI_27;
294+
295+
LIBGCCJIT_ABI_29 {
296+
global:
297+
gcc_jit_function_add_attribute;
298+
gcc_jit_function_add_string_attribute;
299+
} LIBGCCJIT_ABI_28;

0 commit comments

Comments
 (0)