Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

GCC Plugin, add new optimizing pragma

I'm creating a GCC plugin.

I'm trying to create a plugin for a specific loop transformation - unroll loop exactly N (parameter given) times. I have installed plugins correctly and I can successfully register my pragma in compilation process. When I register pragma with function c_register_pragma, I can handle it in lexical analysis (with function handle_my_pragma), but how can I find it then?

I can also define my own pass and traverse GIMPLE, but there is no trace of any pragma. So my question is: Where is my pragma and how can I influence my code with it? Or what would you suggest to reach my goal? It doesn't have to be with pragma, but it seemed to be a good idea. Also, I know about MELT, but within the study of GCC, I would prefer pure plugin in C.

My code

static bool looplugin_gate(void)
{
    return true;
}

static unsigned looplugin_exec(void)
{
    printf( "===looplugin_exec===\n" );

    basic_block bb;
    gimple stmt;
    gimple_stmt_iterator gsi;

    FOR_EACH_BB(bb)
    {
        for (gsi=gsi_start_bb(bb); !gsi_end_p(gsi); gsi_next(&gsi), j++)
        {
            stmt = gsi_stmt(gsi);
            print_gimple_stmt (stdout, stmt, 0, TDF_SLIM);
        }
    }
    return 0;
}


void handle_my_pragma(cpp_reader *ARG_UNUSED(dummy))
{
    printf ("=======Handling loopragma=======\n" );
    enum cpp_ttype token;
    tree x;
    int num = -1;

    token = pragma_lex (&x);
    if (TREE_CODE (x) != INTEGER_CST)
        warning (0, "invalid constant in %<#pragma looppragma%> - ignored"); 
    num = TREE_INT_CST_LOW (x);
    printf( "Detected #pragma loopragma %d\n", num );
}

static void register_my_pragma (void *event_data, void *data)
{
    warning (0, G_("Callback to register pragmas"));
    c_register_pragma (NULL, "loopragma", handle_my_pragma);
}


static struct opt_pass myopt_pass = 
{
    .type = GIMPLE_PASS,
    .name = "LoopPlugin",
    .gate = looplugin_gate,
    .execute = looplugin_exec
};

int plugin_init(struct plugin_name_args   *info,  /* Argument infor */
struct plugin_gcc_version *ver)   /* Version of GCC */
{
const char * plugin_name = info->base_name;
struct register_pass_info pass;

pass.pass = &myopt_pass;
pass.reference_pass_name = "ssa";
pass.ref_pass_instance_number = 1;
pass.pos_op = PASS_POS_INSERT_BEFORE;

register_callback( plugin_name, PLUGIN_PRAGMAS, register_my_pragma, NULL );
register_callback( plugin_name, PLUGIN_PASS_MANAGER_SETUP, NULL, &pass );

return 0;
}

PS: If there was someone familiar with GCC plugins development and had a good heart :), please contact me (mbukovy gmail com). I'm doing this because of my final thesis (own choice) and I welcome any soulmate.

like image 263
Michal Bukovy Avatar asked Mar 02 '14 11:03

Michal Bukovy


1 Answers

When I register pragma with function c_register_pragma, I can handle it in lexical analysis (with function handle_my_pragma), but how can I find it then?

There is an option (actually, a hack) to create fictive helper function call at the place of pragma, when parsing. Then you can detect this function by name in intermediate representation.

Aslo, several days ago there was a question in GCC ML from felix.yang (huawei) "How to deliver loop-related pragma information from TREE to RTL?" - http://comments.gmane.org/gmane.comp.gcc.devel/135243 - check the thread

Some recommendations from the list:

Look at how we implement #pragma ivdep (see replace_loop_annotate () and fortran/trans-stmt.c where it builds ANNOTATE_EXPR).

Patch with replace_loop_annotate() function addition and ivdep pragma implementation: "Re: Patch: Add #pragma ivdep support to the ME and C FE" by Tobias Burnus (2013-08-24).

like image 66
osgx Avatar answered Oct 29 '22 15:10

osgx