Skip to content

Commit fabd52a

Browse files
committed
Support Function multiversion
Define the two syntax for function multiversion on RISC-V. 1. target_clones 2. target_version Both of them are kind of function attribute
1 parent d7941a5 commit fabd52a

File tree

1 file changed

+104
-0
lines changed

1 file changed

+104
-0
lines changed

riscv-c-api.md

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -290,6 +290,104 @@ __attribute__((target("arch=+v"))) int foo(void) { return 0; }
290290
__attribute__((target("arch=+zbb"))) int foo(void) { return 1; }
291291
```
292292
293+
### `__attribute__((target_clones("<TARGET-CLONES-ATTR-STRING>", ...)))
294+
295+
The `target_clones` attribute is used to create multiple versions of a function. The compiler will emit multiple versions based on the provided arguments.
296+
297+
Each `TARGET-CLONES-ATTR-STRING` defines a distinguished version of the function. The `TARGET-CLONES-ATTR-STRING` list must include `default` indicating the translation unit scope build attributes.
298+
299+
The syntax of `<TARGET-CLONES-ATTR-STRING>` describes below:
300+
301+
```
302+
TARGET-CLONES-ATTR-STRING := 'arch=' EXTENSIONS
303+
| 'default'
304+
305+
EXTENSIONS := <EXTENSION> ',' <EXTENSIONS>
306+
| <EXTENSION>
307+
308+
EXTENSION := <OP> <EXTENSION-NAME> <VERSION>
309+
310+
OP := '+'
311+
312+
VERSION := [0-9]+ 'p' [0-9]+
313+
| [1-9][0-9]*
314+
|
315+
316+
EXTENSION-NAME := Naming rule is defined in RISC-V ISA manual
317+
```
318+
319+
For example, the following `foo` function will have three versions but share the same function signature.
320+
321+
```c
322+
__attribute__((target_clones("arch=+v", "default", "arch=rv64gc_zbb")))
323+
int foo(int a)
324+
{
325+
return a + 5;
326+
}
327+
328+
int bar() {
329+
// foo will be resolved by ifunc
330+
return foo(1);
331+
}
332+
```
333+
334+
It makes the compiler trigger the [function multi-version](#function-multi-version) when there exist more than one version for the same function signature.
335+
336+
### `__attribute__((target_version("<TARGET-VERSION-ATTR-STRING>")))
337+
338+
The `target_version` attribute is used to create one version of a function. Functions with the same signature may exist with multiple versions in the same translation unit.
339+
340+
Each `TARGET-VERSION-ATTR-STRING` defines a distinguished version of the function. If there is more than one version for the same function, it must have `default` one that indicating the translation unit scope build attributes.
341+
342+
The syntax of `<TARGET-VERSION-ATTR-STRING>` describes below:
343+
344+
```
345+
TARGET-VERSION-ATTR-STRING := 'arch=' EXTENSIONS
346+
| 'default'
347+
348+
EXTENSIONS := <EXTENSION> ',' <EXTENSIONS>
349+
| <EXTENSION>
350+
351+
EXTENSION := <OP> <EXTENSION-NAME> <VERSION>
352+
353+
OP := '+'
354+
355+
VERSION := [0-9]+ 'p' [0-9]+
356+
| [1-9][0-9]*
357+
|
358+
359+
EXTENSION-NAME := Naming rule is defined in RISC-V ISA manual
360+
```
361+
362+
For example, the following foo function has three versions.
363+
364+
```c
365+
__attribute__((target_version("arch=+v")))
366+
int foo(int a)
367+
{
368+
return a + 5;
369+
}
370+
371+
__attribute__((target_version("arch=+zbb")))
372+
int foo(int a)
373+
{
374+
return a + 5;
375+
}
376+
377+
__attribute__((target_version("default")))
378+
int foo(int a)
379+
{
380+
return a + 5;
381+
}
382+
383+
int bar() {
384+
// foo will be resolved by ifunc
385+
return foo(1);
386+
}
387+
```
388+
389+
It makes the compiler trigger the [function multi-version](#function-multi-version) when there exist more than one version for the same function signature.
390+
293391
### riscv_vector_cc
294392
295393
Supported Syntaxes:
@@ -604,3 +702,9 @@ statements, including both RISC-V specific and common operand modifiers.
604702
| ------------ | --------------------------------------------------------------------------------- | ----------- |
605703
| z | Print `zero` (`x0`) register for immediate 0, typically used with constraints `J` | |
606704
| i | Print `i` if corresponding operand is immediate. | |
705+
706+
## Function Multi-version
707+
708+
Function multi-versioning(FMV) provides an approach to selecting the appropriate function according to the runtime environment. The final binary may contain all versions of the function, with the compiler generating all supported versions and the runtime selecting the appropriate one.
709+
710+
This feature is triggered by `target_version/target_clones` function attribute.

0 commit comments

Comments
 (0)