Skip to content

operator&& result differs for certain swizzle operands #11995

@0x12CC

Description

@0x12CC

Describe the bug
The operator&& gives a different result for testVec1.template swizzle<sycl::elem::s0>() && testVec2 and testVec1 && testVec2.

To Reproduce

#include <sycl/sycl.hpp>

template <typename T, typename ResultT> void testAndOperator() {
  constexpr int N = 4;
  std::array<ResultT, N> test_results{};

  sycl::queue q;
  sycl::buffer<ResultT, 1> buffer{test_results.data(), N};
  q.submit([&](sycl::handler &cgh) {
     auto acc = buffer.template get_access<sycl::access_mode::write>(cgh);
     cgh.parallel_for(sycl::range<1>{1}, [=](sycl::id<1> id) {
       auto testVec1 = sycl::vec<T, 1>(static_cast<T>(1));
       auto testVec2 = sycl::vec<T, 1>(static_cast<T>(2));
       sycl::vec<ResultT, 1> resVec;

       ResultT expected = static_cast<ResultT>(
           -(static_cast<ResultT>(1) && static_cast<ResultT>(2)));
       acc[0] = expected;

       // LHS swizzle
       resVec = testVec1.template swizzle<sycl::elem::s0>() && testVec2;
       acc[1] = resVec[0];

       // RHS swizzle
       resVec = testVec1 && testVec2.template swizzle<sycl::elem::s0>();
       acc[2] = resVec[0];

       // No swizzle
       resVec = testVec1 && testVec2;
       acc[3] = resVec[0];
     });
   }).wait();

  for (int i = 0; i < N; i++) {
    std::cout << "Test " << i << ": " << ((int)test_results[i]) << std::endl;
  }
  std::cout << std::endl;
}

int main() {
  std::cout << "Testing with T = bool" << std::endl;
  testAndOperator<bool, std::int8_t>();
  std::cout << "Testing with T = std::int8_t" << std::endl;
  testAndOperator<std::int8_t, std::int8_t>();
}
$ clang++ -fsycl ./reproducer.cpp
$ ./a.out
Testing with T = bool
Test 0: -1
Test 1: 1
Test 2: -1
Test 3: -1

Testing with T = std::int8_t
Test 0: -1
Test 1: -1
Test 2: -1
Test 3: -1

The Test 1 result for type bool differs from the others. This is a bug since the three test expressions should produce the same result.

Environment (please complete the following information):

  • OS: Linux
  • Target device and vendor: Intel CPU
  • DPC++ version: 6bce7f6
  • Dependencies version: OpenCL [2023.16.6.0.28_042959]

Additional context
I'm not sure if the correct test result is -1 or 1. The SYCL specification states the following:

Construct a new instance of the SYCL vec class template with the same template parameters as lhs vec with each element of the new SYCL vec instance the result of an element-wise OP logical operation between each element of lhs vec and each element of the rhs SYCL vec.

The result of the element-wise && is true. Converting the result to an std::int8_t in C++ results in 1, but SYCL states the following for other operators with a logical result:

Each element of the SYCL vec that is returned must be -1 if the operation results in true and 0 if the operation results in false.

Metadata

Metadata

Assignees

No one assigned

    Labels

    CTSImpacts Khronos SYCL CTSbugSomething isn't workingconfirmed

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions