Clang error: invalid cpu feature string for builtin (err_invalid_cpu_supports)

From emmtrix Wiki
Jump to navigation Jump to search
Text error: invalid cpu feature string for builtin
Type Error
Category Semantic Issue
Internal Id err_invalid_cpu_supports
Internal Message invalid cpu feature string for builtin
Regular Expression (?:error|fatal error)\: invalid cpu feature string for builtin
First Commit 2015-06-29 d983270976d2 Add support for the x86 builtin __builtin_cpu_supports.

Description

The error is issued by the Clang compiler when there is an attempt to use the __builtin_cpu_supports function with a string argument that does not correspond to a valid CPU feature. This function is designed to determine at runtime whether a specific CPU feature is supported, based on the features available on the current processor. The valid string arguments are typically feature names recognized by the compiler that relate to specific processor capabilities, such as "sse4.2" or "avx".

If the provided feature string is not recognized by the compiler as a valid CPU feature identifier, this error is triggered to indicate that the feature check cannot be performed due to the invalid argument. This is a mechanism to ensure that checks against CPU features are made against a known set of features to avoid runtime errors or undefined behavior resulting from attempting to query the support of an unrecognized feature.

The correct usage of __builtin_cpu_supports involves specifying a constant string literal as the function argument, which must exactly match one of the CPU features known and validated by the compiler.

 
AI Generated

Example

In the following example, the __builtin_cpu_supports function is utilized improperly by supplying a string that is not acknowledged as a valid CPU feature. This function aims to assess at runtime the availability of a specific CPU feature, necessitating a constant string literal representing the feature to be examined. The example showcases the incorrect use by demonstrating the compiler's response to an unrecognized feature string. This results in an error message indicating that the provided argument does not align with any recognized CPU feature. The instance underlines the critical nature of employing solely valid, compiler-recognized strings when engaging with __builtin_cpu_supports, to ensure the accuracy of runtime feature checks.  
AI Generated


Flags -xc -march=native -target x86_64-linux-gnu

[Try out in Compiler Explorer]

Source
int main() {
  __builtin_cpu_supports("unknown_feature"); // Invalid CPU feature string
}
Compiler Output
<source>:2:3: error: invalid cpu feature string for builtin


Clang Internals (17.0.6)

Git Commit Message

Add support for the x86 builtin __builtin_cpu_supports.

This matches the implementation of the gcc support for the same
feature, including checking the values set up by libgcc at runtime.
The structure looks like this:

  unsigned int __cpu_vendor;
  unsigned int __cpu_type;
  unsigned int __cpu_subtype;
  unsigned int __cpu_features[1];

with a set of enums to match various fields that are field out after
parsing the output of the cpuid instruction.
This also adds a set of errors checking for valid input (and cpu).

compiler-rt support for this and the other builtins in this family
(__builtin_cpu_init and __builtin_cpu_is) are forthcoming.

llvm-svn: 240994

Used in Clang Sources

This section lists all occurrences of the diagnostic within the Clang's codebase. For each occurrence, an auto-extracted snipped from the source code is listed including key elements like control structures, functions, or classes. It should illustrate the conditions under which the diagnostic is activated.

clang/lib/Sema/SemaChecking.cpp (line 5472)

/// SemaBuiltinCpuSupports - Handle __builtin_cpu_supports(char *).
/// This checks that the target supports __builtin_cpu_supports and
/// that the string argument is constant and valid.
static bool SemaBuiltinCpuSupports(Sema &S, const TargetInfo &TI, CallExpr *TheCall) {
  // ...
  if (!TI.validateCpuSupports(Feature))
    return S.Diag(TheCall->getBeginLoc(), diag::err_invalid_cpu_supports) << Arg->getSourceRange();

Triggered in Clang Tests

This section lists all internal Clang test cases that trigger the diagnostic.

clang/test/Sema/builtin-cpu-supports.c

  • clang/test/Sema/builtin-cpu-supports.c:10:7: error: invalid cpu feature string for builtin