Skip to content

Commit 5885e87

Browse files
committed
[FMV] Add Priority syntax for version selection order
1 parent fbf5f4e commit 5885e87

File tree

1 file changed

+39
-25
lines changed

1 file changed

+39
-25
lines changed

src/c-api.adoc

Lines changed: 39 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -328,8 +328,21 @@ The syntax of `<TARGET-CLONES-ATTR-STRING>` describes below:
328328

329329
[source, C]
330330
----
331-
TARGET-CLONES-ATTR-STRING := 'arch=' EXTENSIONS
332-
| 'default'
331+
TARGET-CLONES-ATTR-STRING := 'default'
332+
| ATTR-STRINGS
333+
334+
ATTR-STRINGS := ATTR-STRING
335+
| ';' ATTR-STRINGS
336+
337+
ATTR-STRING := ARCH-ATTR ';' PRIORITY-ATTR
338+
| PRIORITY-ATTR ';' ARCH-ATTR
339+
| ARCH-ATTR
340+
341+
ARCH-ATTR := 'arch=' EXTENSIONS
342+
343+
PRIORITY-ATTR := 'priority=' DIGITS
344+
345+
DIGITS := [0-9]+
333346
334347
EXTENSIONS := <EXTENSION> ',' <EXTENSIONS>
335348
| <EXTENSION>
@@ -350,7 +363,7 @@ same function signature.
350363

351364
[source, C]
352365
----
353-
__attribute__((target_clones("arch=+v", "default", "arch=+zbb")))
366+
__attribute__((target_clones("arch=+v;priority=2", "default", "arch=+zbb;priority=1")))
354367
int foo(int a)
355368
{
356369
return a + 5;
@@ -362,6 +375,8 @@ int bar() {
362375
}
363376
----
364377

378+
The `priority` accepts a digit as the version priority during [Version Selection](#version-selection). If `priority` isn't specified, then the priority of version defaults to zero.
379+
365380
It makes the compiler trigger the <<function-multi-version, function multi-version>>,
366381
when there exist more than one version for the same function signature.
367382

@@ -376,38 +391,19 @@ function. If there is more than one version for the same function, it
376391
must have `default` one that indicating the translation unit scope build
377392
attributes.
378393

379-
The syntax of `<TARGET-VERSION-ATTR-STRING>` describes below:
380-
381-
[source, C]
382-
----
383-
TARGET-VERSION-ATTR-STRING := 'arch=' EXTENSIONS
384-
| 'default'
385-
386-
EXTENSIONS := <EXTENSION> ',' <EXTENSIONS>
387-
| <EXTENSION>
388-
389-
EXTENSION := <OP> <EXTENSION-NAME> <VERSION>
390-
391-
OP := '+'
392-
393-
VERSION := [0-9]+ 'p' [0-9]+
394-
| [1-9][0-9]*
395-
|
396-
397-
EXTENSION-NAME := Naming rule is defined in RISC-V ISA manual
398-
----
394+
The syntax of `<TARGET-VERSION-ATTR-STRING>` is the same as described above for `<TARGET-CLONES-ATTR-STRING>`.
399395

400396
For example, the following foo function has three versions.
401397

402398
[source, C]
403399
----
404-
__attribute__((target_version("arch=+v")))
400+
__attribute__((target_version("arch=+v;priority=1")))
405401
int foo(int a)
406402
{
407403
return a + 5;
408404
}
409405
410-
__attribute__((target_version("arch=+zbb")))
406+
__attribute__((target_version("arch=+zbb;priority=2")))
411407
int foo(int a)
412408
{
413409
return a + 5;
@@ -425,6 +421,10 @@ int bar() {
425421
}
426422
----
427423

424+
The `priority` accepts a digit as the version priority during [Version Selection](#version-selection). If `priority` isn't specified, then the priority of version defaults to zero.
425+
426+
The `default` version does not accept the priority.
427+
428428
It makes the compiler trigger the <<function-multi-version, function multi-version>>
429429
when there exist more than one version for the same function signature.
430430

@@ -946,3 +946,17 @@ Each queryable extension must have an associated `groupid` and `bitmask` that in
946946
| zcmop | 1 | 6
947947
| zawrs | 1 | 7
948948
|====
949+
950+
=== Version Selection
951+
952+
The process of selecting the appropriate function version during function multi-versioning follows these guidelines:
953+
954+
1. The implementation of the selection algorithm is implementation-specific.
955+
2. Once a version is selected, it remains in use for the entire duration of the process.
956+
3. Only versions whose required features are all available in the runtime environment are eligible for selection.
957+
958+
The version selection process applies the following rules in order:
959+
960+
1. Among the eligible versions, select the one with the highest priority.
961+
2. If multiple versions have equal priority, select one based on an implementation-defined heuristic.
962+
3. If no other suitable versions are found, fall back to the "default" version.

0 commit comments

Comments
 (0)