Skip to content

Add support for "cold" function attribute #18

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jul 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions gcc/jit/jit-playback.cc
Original file line number Diff line number Diff line change
Expand Up @@ -526,6 +526,8 @@ const char* fn_attribute_to_string(gcc_jit_fn_attribute attr)
return "used";
case GCC_JIT_FN_ATTRIBUTE_VISIBILITY:
return "visibility";
case GCC_JIT_FN_ATTRIBUTE_COLD:
return "cold";
}
return NULL;
}
Expand Down
1 change: 1 addition & 0 deletions gcc/jit/libgccjit.h
Original file line number Diff line number Diff line change
Expand Up @@ -2084,6 +2084,7 @@ enum gcc_jit_fn_attribute
GCC_JIT_FN_ATTRIBUTE_TARGET,
GCC_JIT_FN_ATTRIBUTE_USED,
GCC_JIT_FN_ATTRIBUTE_VISIBILITY,
GCC_JIT_FN_ATTRIBUTE_COLD,
};

/* Add an attribute to a function. */
Expand Down
54 changes: 54 additions & 0 deletions gcc/testsuite/jit.dg/test-cold-attribute.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/* { dg-do compile { target x86_64-*-* } } */

#include <stdlib.h>
#include <stdio.h>

#include "libgccjit.h"

/* We don't want set_options() in harness.h to set -O2 to see that the cold
attribute affects the optimizations. */
#define TEST_ESCHEWS_SET_OPTIONS
static void set_options (gcc_jit_context *ctxt, const char *argv0)
{
// Set "-O2".
gcc_jit_context_set_int_option(ctxt, GCC_JIT_INT_OPTION_OPTIMIZATION_LEVEL, 2);
}

#define TEST_COMPILING_TO_FILE
#define OUTPUT_KIND GCC_JIT_OUTPUT_KIND_ASSEMBLER
#define OUTPUT_FILENAME "output-of-test-cold-attribute.c.s"
#include "harness.h"

void
create_code (gcc_jit_context *ctxt, void *user_data)
{
/* Let's try to inject the equivalent of:
int
__attribute__ ((cold))
t()
{
return -1;
}

*/
gcc_jit_type *int_type =
gcc_jit_context_get_type (ctxt, GCC_JIT_TYPE_INT);

gcc_jit_function *func_t =
gcc_jit_context_new_function (ctxt, NULL,
GCC_JIT_FUNCTION_EXPORTED,
int_type,
"t",
0, NULL,
0);
gcc_jit_function_add_attribute(func_t, GCC_JIT_FN_ATTRIBUTE_COLD);
gcc_jit_block *block = gcc_jit_function_new_block (func_t, NULL);
gcc_jit_rvalue *ret = gcc_jit_context_new_rvalue_from_int (ctxt,
int_type,
-1);

gcc_jit_block_end_with_return (block, NULL, ret);
}

/* { dg-final { jit-verify-output-file-was-created "" } } */
/* { dg-final { jit-verify-assembler-output "orl" } } */