Clang error: vector operands do not have the same elements sizes (A and B) [-Wvec-elem-size] (warn_typecheck_vector_element_sizes_not_equal)

From emmtrix Wiki
Jump to navigation Jump to search
Text error: vector operands do not have the same elements sizes (A and B)
Type Downgradable Error
Category Semantic Issue
Internal Id warn_typecheck_vector_element_sizes_not_equal
Active by Default Yes
Flags -Wno-vec-elem-size (1 element)
Internal Message vector operands do not have the same elements sizes (%0 and %1)
Regular Expression (?:error|warning|fatal error)\: vector operands do not have the same elements sizes \((.*?) and (.*?)\) \[[^\]]*\-Wvec\-elem\-size[^\]]*\]
First Commit 2016-10-19 9941ca8af6b4 [Sema] Gcc compatibility of vector shift

Description

The downgradable error is issued by the Clang compiler when it encounters an operation involving two vector operands that do not have matching element sizes. In vectorized computing, operations on vectors (arrays of data processed in parallel) require that the vectors involved have elements of the same size to ensure the operation's correctness and consistency. When a discrepancy in element sizes is detected during such an operation, the compiler flags this situation as an error to alert the developer that the intended operation may not perform as expected due to the mismatch in operand sizes.

The specific diagnostic message is:

error: vector operands do not have the same elements sizes (A and B)

This message includes the detected element sizes of both operands, identified as 'A' and 'B' in this context, which did not match. This diagnostic is crucial for developers working with vectorized operations, as overlooking the element size mismatch could lead to incorrect computations or runtime errors.

This error is downgradable, meaning it can be demoted to a warning or ignored completely using the appropriate compilation flags if the developer wishes to bypass this check for specific reasons. However, caution is advised when downgrading or ignoring such errors, as this could potentially lead to logic errors in the application.

The compiler implements this diagnostic to aid in achieving compatibility with GCC's behavior in similar situations and to enforce type safety in vector operations, which plays a significant part in optimizing performance-critical code.

 
AI Generated

Example

In the following example, two types of vectors are defined using the GCC vector extension attribute. The first vector, identified by the type alias v4si, is declared to have four int elements, implying that each element occupies the size typically associated with an integer on the platform. The second vector, identified by the type alias v4hi, consists of four short elements, which usually occupy less space than an int. Following these declarations, an attempt is made to perform a right shift operation on the first vector by the values of the second vector. This operation expects both operands to consist of elements of the same size. However, due to the discrepancy in size between an int and a short, the compiler emits an error message as indicated in the output provided. The error message specifically highlights the discrepancy in the types of the vectors involved in the operation, emphasizing that their element sizes do not match, which triggers the diagnostic message.  
AI Generated


Flags -xc++

[Try out in Compiler Explorer]

Source
int main() {
  // Define a vector of 4 ints
  typedef int v4si __attribute__((vector_size (16)));
  // Define a vector of 4 shorts
  typedef short v4hi __attribute__((vector_size (8)));
  v4si a;
  v4hi b;
  // Attempt to shift a by b; different element sizes
  a = a >> b;
  return 0;
}
Compiler Output
<source>:9:9: error: vector operands do not have the same elements sizes ('v4si' (vector of 4 'int' values) and 'v4hi' (vector of 4 'short' values)) [-Wvec-elem-size]


Clang Internals (17.0.6)

Git Commit Message

[Sema] Gcc compatibility of vector shift

Gcc prints error if elements of left and right parts of a shift have different
sizes. This patch is provided the GCC compatibility.

Patch by Vladimir Yakovlev.

Differential Revision: https://reviews.llvm.org/D24669

llvm-svn: 284579

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/SemaExpr.cpp (line 12191)

/// Return the resulting type when a vector is shifted
///        by a scalar or vector shift amount.
static QualType checkVectorShift(Sema &S, ExprResult &LHS, ExprResult &RHS, SourceLocation Loc, bool IsCompAssign) {
  // ...
  if (!LHSVecTy) {
  // ...
  } else if (RHSVecTy) {
    // ...
    if (!S.LangOpts.OpenCL && !S.LangOpts.ZVector) {
      // ...
      if (LHSBT != RHSBT && S.Context.getTypeSize(LHSBT) != S.Context.getTypeSize(RHSBT)) {
        S.Diag(Loc, diag::warn_typecheck_vector_element_sizes_not_equal) << LHS.get()->getType() << RHS.get()->getType() << LHS.get()->getSourceRange() << RHS.get()->getSourceRange();

Triggered in Clang Tests

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

clang/test/CodeGen/vecshift.c

  • clang/test/CodeGen/vecshift.c:113:13: warning: vector operands do not have the same elements sizes ('vector_int8' (vector of 8 'int' values) and 'vector_uchar8' (vector of 8 'unsigned char' values)) [-Wvec-elem-size]
  • clang/test/CodeGen/vecshift.c:118:15: warning: vector operands do not have the same elements sizes ('vector_uchar8' (vector of 8 'unsigned char' values) and 'vector_int8' (vector of 8 'int' values)) [-Wvec-elem-size]
  • clang/test/CodeGen/vecshift.c:123:15: warning: vector operands do not have the same elements sizes ('vector_ushort8' (vector of 8 'unsigned short' values) and 'vector_uint8' (vector of 8 'unsigned int' values)) [-Wvec-elem-size]
  • clang/test/CodeGen/vecshift.c:128:15: warning: vector operands do not have the same elements sizes ('vector_uint8' (vector of 8 'unsigned int' values) and 'vector_short8' (vector of 8 'short' values)) [-Wvec-elem-size]