6.35.25 RISC-V Function Attributes

These function attributes are supported by the RISC-V back end:

naked

This attribute allows the compiler to construct the requisite function declaration, while allowing the body of the function to be assembly code. The specified function will not have prologue/epilogue sequences generated by the compiler. Only basic asm statements can safely be included in naked functions (see Basic Asm — Assembler Instructions Without Operands). While using extended asm or a mixture of basic asm and C code may appear to work, they cannot be depended upon to work reliably and are not supported.

interrupt

Use this attribute to indicate that the specified function is an interrupt handler. The compiler generates function entry and exit sequences suitable for use in an interrupt handler when this attribute is present.

You can specify the kind of interrupt to be handled by adding an optional parameter to the interrupt attribute like this:

void f (void) __attribute__ ((interrupt ("user")));

Permissible values for this parameter are user, supervisor, and machine. If there is no parameter, then it defaults to machine.

riscv_vector_cc

Use this attribute to force the function to use the vector calling convention variant.

void foo() __attribute__((riscv_vector_cc));
[[riscv::vector_cc]] void foo(); // For C++11 and C23

The following target-specific function attributes are available for the RISC-V target. For the most part, these options mirror the behavior of similar command-line options (see RISC-V Options), but on a per-function basis.

arch=

Specifies the architecture version and architectural extensions to use for this function. The behavior and permissible arguments are the same as for the -march= command-line option, in addtion, it also support extension enablement list, a list of extension name and prefixed with +, like arch=+zba means enable zba extension. Multiple extension can be enabled by separating them with a comma. For example: arch=+zba,+zbb.

tune=

Specifies the core for which to tune the performance of this function. The behavior and permissible arguments are the same as for the -mtune= command-line option.

cpu=

Specifies the core for which to tune the performance of this function and also whose architectural features to use. The behavior and valid arguments are the same as for the -mcpu= command-line option.

The above target attributes can be specified as follows:

__attribute__((target("attr-string")))
int
f (int a)
{
  return a + 5;
}

where attr-string is one of the attribute strings specified above.

Multiple target function attributes can be specified by separating them with a semicolon. For example:

__attribute__((target("arch=+zba,+zbb;tune=rocket")))
int
foo (int a)
{
  return a + 5;
}

is valid and compiles function foo with zba and zbb extensions and tunes it for rocket.