Skip to content
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

cgen, checker if and $if: CPU instructions support #22680

Open
2 tasks
enghitalo opened this issue Oct 28, 2024 · 1 comment
Open
2 tasks

cgen, checker if and $if: CPU instructions support #22680

enghitalo opened this issue Oct 28, 2024 · 1 comment
Labels
Feature Request This issue is made to request a feature.

Comments

@enghitalo
Copy link
Contributor

enghitalo commented Oct 28, 2024

Describe the feature

__builtin_cpu_init

This function runs the CPU detection code to check the type of CPU and the features supported. This built-in function needs to be invoked along with the built-in functions to check CPU type and features, __builtin_cpu_is and __builtin_cpu_supports, only when used in a function that is executed before any constructors are called. The CPU detection code is automatically executed in a very high priority constructor.

For example, this function has to be used in ifunc resolvers that check for CPU type using the built-in functions __builtin_cpu_is and __builtin_cpu_supports, or in constructors on targets that don’t support constructor priority.

static void (*resolve_memcpy (void)) (void)
{
  // ifunc resolvers fire before constructors, explicitly call the init
  // function.
  __builtin_cpu_init ();
  if (__builtin_cpu_supports ("ssse3"))
    return ssse3_memcpy; // super fast memcpy with ssse3 instructions.
  else
    return default_memcpy;
}

void *memcpy (void *, const void *, size_t)
     __attribute__ ((ifunc ("resolve_memcpy")));

__builtin_cpu_supports

if (__builtin_cpu_supports ("popcnt"))
  {
     asm("popcnt %1,%0" : "=r"(count) : "rm"(n) : "cc");
  }
else
  {
     count = generic_countbits (n); //generic implementation.
  }

Use Case

$if sse2 {
    // some faster implementation to do something
} $else {
    // default code
}
if support_sse2 {
    // some faster implementation to do something
} else {
    // default code
}

Proposed Solution

I guess:

  1. changes at vlib/v/gen/c/cheaders.v to verify if compiler supports __builtin_cpu_supports
#define HAS_BUILTIN_CPU_SUPPORTS 1
  1. changes at vlib/v/ast/comptime_valid_idents.v -> valid_comptime_if_cpu_features to supports instructions listed in __builtin_cpu_supports

  2. changes at vlib/v/checker/comptime.v -> comptime_if_cond to supports instructions listed in __builtin_cpu_supports

  3. changes at doc/docs.md -> #### $if condition

| OS                             | Compilers        | Platforms                     | Other                                         | CPU features                  |
|--------------------------------|------------------|-------------------------------|-----------------------------------------------|-------------------------------|
| `windows`, `linux`, `macos`    | `gcc`, `tinyc`   | `amd64`, `arm64`, `aarch64`   | `debug`, `prod`, `test`                       | `sse2`, `avx`, `avx2`, `neon`	|
| `darwin`, `ios`, `bsd`         | `clang`, `mingw` | `i386`, `arm32`               | `js`, `glibc`, `prealloc`                     | `mmx`, `sha`, `aes`, `fma`, 	|
| `freebsd`, `openbsd`, `netbsd` | `msvc`           | `rv64`, `rv32`                | `no_bounds_checking`, `freestanding`          | `f16c`, `bmi1`, `bmi2`, `adx`	|
| `android`, `mach`, `dragonfly` | `cplusplus`      | `x64`, `x32`                  | `no_segfault_handler`, `no_backtrace`         | `rdseed`, `rdtscp`			|
| `gnu`, `hpux`, `haiku`, `qnx`  |                  | `little_endian`, `big_endian` | `no_main`, `fast_math`, `apk`, `threads`      |								|
| `solaris`, `termux`            |                  |                               | `js_node`, `js_browser`, `js_freestanding`    |								|
| `serenity`, `vinix`, `plan9`   |                  |                               | `interpreter`, `es5`, `profile`, `wasm32`     |								|
|                                |                  |                               | `wasm32_emscripten`, `wasm32_wasi`            |								|
|                                |                  |                               | `native`, `autofree`                          |								|

Other Information

No response

Acknowledgements

  • I may be able to implement this feature request
  • This feature might incur a breaking change

Version used

latest

Environment details (OS name and version, etc.)

all

Note

You can use the 👍 reaction to increase the issue's priority for developers.

Please note that only the 👍 reaction to the issue itself counts as a vote.
Other reactions and those to comments will not be taken into account.

@enghitalo enghitalo added the Feature Request This issue is made to request a feature. label Oct 28, 2024
@enghitalo enghitalo changed the title if: CPU instructions support cgen, checker if and $if: CPU instructions support Oct 28, 2024
@kbkpbot
Copy link
Contributor

kbkpbot commented Oct 29, 2024

Maybe cpuinfo can give you some help.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Feature Request This issue is made to request a feature.
Projects
None yet
Development

No branches or pull requests

2 participants