Clang error: always_inline function B requires target feature 'C', but would be inlined into function A that is compiled without support for 'C' (err_function_needs_feature)

From emmtrix Wiki
Jump to navigation Jump to search
Text error: always_inline function B requires target feature 'C', but would be inlined into function A that is compiled without support for 'C'
Type Error
Category None (since 8.0)
Semantic Issue (until 7.1)
Internal Id err_function_needs_feature
Internal Message always_inline function %1 requires target feature '%2', but would be inlined into function %0 that is compiled without support for '%2'
Regular Expression (?:error|fatal error)\: always_inline function (.*?) requires target feature '(.*?)', but would be inlined into function (.*?) that is compiled without support for '(.*?)'
First Commit 2015-11-12 2b2d56f059e7 Provide a frontend based error for always_inline functions that require

Description

The error is issued by the Clang compiler when an attempt is made to inline a function annotated with __attribute__((always_inline, target("..."))) into another function that does not support the required target features. The always_inline attribute instructs the compiler to inline the function whenever possible, overriding the usual decision-making process regarding inlining. The target attribute specifies that the function is only suitable for a particular target architecture or feature set.

When Clang encounters a situation where a function requires specific target features due to the presence of the target attribute and attempts to inline it into a function compiled without these target features, it generates this error. This ensures that functions intended for specific hardware or instruction set extensions are not inadvertently inlined into contexts where those extensions are not available, which would lead to incorrect program execution.

To resolve this error, ensure that the target features required by the always_inline function are available in the context into which it is being inlined, or reconsider the use of the always_inline and target attributes for the function in question.

 
AI Generated

Example

In the following example, a scenario is outlined where an attribute is used to enforce the inlining of a function, specifically when that function is only suitable for execution with specific hardware features. The function marked with the __attribute__((always_inline, target("aes"))) is designed to be inlined irrespective of optimization levels and is explicitly intended for use with hardware that supports the AES instruction set. However, the context in which this function is attempted to be inlined does not have the AES feature enabled, triggering the compiler to emit an error. This error message indicates that the function requiring specific target features is being inlined into another function that lacks support for those necessary features. The example demonstrates how the compiler enforces constraints related to hardware-specific optimizations and the inlining of functions, ensuring that functions are only inlined into contexts where the required hardware features are supported. This enforcement helps prevent potential runtime errors that could arise from executing hardware-specific instructions in incompatible execution environments.  
AI Generated


Flags -xc -O2 -mno-aes

[Try out in Compiler Explorer]

Source
#include <immintrin.h>

__attribute__((always_inline, target("aes"))) void f() {} // requires AES

void g() { f(); } // compiled without AES
Compiler Output
<source>:5:12: error: always_inline function 'f' requires target feature 'aes', but would be inlined into function 'g' that is compiled without support for 'aes'


Clang Internals (17.0.6)

Git Commit Message

Provide a frontend based error for always_inline functions that require
target features that the caller function doesn't provide. This matches
the existing backend failure to inline functions that don't have
matching target features - and diagnoses earlier in the case of
always_inline.

Fix up a few test cases that were, in fact, invalid if you tried
to generate code from the backend with the specified target features
and add a couple of tests to illustrate what's going on.

This should fix PR25246.

llvm-svn: 252834

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/CodeGen/CodeGenFunction.cpp (line 2635)

// Emits an error if we don't have a valid set of target features for the
// called function.
void CodeGenFunction::checkTargetFeatures(SourceLocation Loc, const FunctionDecl *TargetDecl) {
  // ...
  if (BuiltinID) {
  // ...
  } else if (!TargetDecl->isMultiVersion() && TargetDecl->hasAttr<TargetAttr>()) {
    // ...
    if (!llvm::all_of(ReqFeatures, [&](StringRef Feature) {
      // ...
      CGM.getDiags().Report(Loc, diag::err_function_needs_feature) << FD->getDeclName() << TargetDecl->getDeclName() << MissingFeature;

clang/lib/CodeGen/CodeGenFunction.cpp (line 2644)

// Emits an error if we don't have a valid set of target features for the
// called function.
void CodeGenFunction::checkTargetFeatures(SourceLocation Loc, const FunctionDecl *TargetDecl) {
  // ...
  if (BuiltinID) {
  // ...
  } else if (!TargetDecl->isMultiVersion() && TargetDecl->hasAttr<TargetAttr>()) {
  // ...
  } else if (!FD->isMultiVersion() && FD->hasAttr<TargetAttr>()) {
    // ...
    for (const auto &F : CalleeFeatureMap) {
      if (F.getValue() && (!CallerFeatureMap.lookup(F.getKey()) || !CallerFeatureMap.find(F.getKey())->getValue()))
        CGM.getDiags().Report(Loc, diag::err_function_needs_feature) << FD->getDeclName() << TargetDecl->getDeclName() << F.getKey();

Triggered in Clang Tests

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

clang/test/Sema/arm-neon-target.c

  • clang/test/Sema/arm-neon-target.c:49:3: error: always_inline function 'vdot_u32' requires target feature 'dotprod', but would be inlined into function 'undefined' that is compiled without support for 'dotprod'
  • clang/test/Sema/arm-neon-target.c:51:3: error: always_inline function 'vceqz_f16' requires target feature 'fullfp16', but would be inlined into function 'undefined' that is compiled without support for 'fullfp16'
  • clang/test/Sema/arm-neon-target.c:52:3: error: always_inline function 'vrnd_f16' requires target feature 'fullfp16', but would be inlined into function 'undefined' that is compiled without support for 'fullfp16'
  • clang/test/Sema/arm-neon-target.c:53:3: error: always_inline function 'vmaxnm_f16' requires target feature 'fullfp16', but would be inlined into function 'undefined' that is compiled without support for 'fullfp16'
  • clang/test/Sema/arm-neon-target.c:55:3: error: always_inline function 'vmmlaq_s32' requires target feature 'i8mm', but would be inlined into function 'undefined' that is compiled without support for 'i8mm'
  • clang/test/Sema/arm-neon-target.c:57:3: error: always_inline function 'vbfdot_f32' requires target feature 'bf16', but would be inlined into function 'undefined' that is compiled without support for 'bf16'
  • clang/test/Sema/arm-neon-target.c:60:3: error: always_inline function 'vdup_n_bf16' requires target feature 'bf16', but would be inlined into function 'undefined' that is compiled without support for 'bf16'
  • clang/test/Sema/arm-neon-target.c:62:3: error: always_inline function 'vcvt_f32_bf16' requires target feature 'bf16', but would be inlined into function 'undefined' that is compiled without support for 'bf16'
  • clang/test/Sema/arm-neon-target.c:63:3: error: always_inline function 'vcvt_bf16_f32' requires target feature 'bf16', but would be inlined into function 'undefined' that is compiled without support for 'bf16'
  • clang/test/Sema/arm-neon-target.c:65:3: error: always_inline function 'vqrdmlahq_s32' requires target feature 'v8.1a', but would be inlined into function 'undefined' that is compiled without support for 'v8.1a'
  • clang/test/Sema/arm-neon-target.c:67:3: error: always_inline function 'vcaddq_rot90_f32' requires target feature 'v8.3a', but would be inlined into function 'undefined' that is compiled without support for 'v8.3a'
  • clang/test/Sema/arm-neon-target.c:68:3: error: always_inline function 'vcmla_rot90_f16' requires target feature 'v8.3a', but would be inlined into function 'undefined' that is compiled without support for 'v8.3a'