Intel® oneAPI Data Parallel C++
Support for Intel® oneAPI DPC++ Compiler, Intel® oneAPI DPC++ Library, Intel ICX Compiler , Intel® DPC++ Compatibility Tool, and GDB*
561 Discussions

assert failes if struct tag-name is given

Shukla__Gagandeep
1,124 Views

Hi,

I noticed a strange problem. If I give structs and unions without tag-names, I get no issues but if I specify tag-names, I get static_assert.

Here is the code:

typedef struct KernelLightDistribution {
  float totarea;
  int prim;
  union dpct_type_be6907 {
    struct dpct_type_12c54e {
      int shader_flag;
      int object_id;
    } mesh_light;
    struct dpct_type_ff924c {
      float pad;
      float size;
    } lamp;
  };
} KernelLightDistribution;
static_assert_align(KernelLightDistribution, 16);

where static_assert_align is:

#define static_assert_align(st, align) \
  static_assert((sizeof(st) % (align) == 0), "Structure must be strictly aligned")  // NOLINT

When I compile using dpcpp (with -std=c++17), I get static_assert

../kernel/kernel_types.h:1553:1: error: static_assert failed due to requirement 'sizeof(ccl::KernelLightDistribution) % (16) == 0' "Structure must be strictly aligned"
static_assert_align(KernelLightDistribution, 16);
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
../util/util_static_assert.h:32:3: note: expanded from macro 'static_assert_align'
  static_assert((sizeof(st) % (align) == 0), "Structure must be strictly aligned")  // NOLINT

But If I comment out the tag-names and use anonymous union and structs, static_assert disappears and code compiles without error.

Code with tag-names commented out:

typedef struct KernelLightDistribution {
  float totarea;
  int prim;
  union /* dpct_type_be6907 */ {
    struct /* dpct_type_12c54e */ {
      int shader_flag;
      int object_id;
    } mesh_light;
    struct /* dpct_type_ff924c */ {
      float pad;
      float size;
    } lamp;
  };
} KernelLightDistribution;
static_assert_align(KernelLightDistribution, 16);

Now the file compiles without any error.

Should struct or union tag-names affect its size or alignment?

static_assert comes back if I uncomment union tag name or struct tag-names ie if I uncomment any of the three tag-names, problem comes back.

Regards,
Gagan

 

0 Kudos
1 Solution
RahulV_intel
Moderator
1,110 Views

Hi,

 

There is a small bug in your code. You have not initialized the union dpct_type_be6907. As a result, memory is not getting created for that particular data structure. Anonymous structs/unions are a bit lenient when it comes to initialization since they can be accessed right way and they are guaranteed to be present within a struct/union. (Memory always gets created for anonymous structs/unions)

 

Refer to the modified code snippet below.

 

#include <CL/sycl.hpp>
#include <iostream>

#define static_assert_align(st, align) \
  static_assert((sizeof(st) % (align) == 0), "Structure must be strictly aligned")  // NOLINT


typedef struct KernelLightDistribution {
  float totarea;
  int prim;
  union dpct_type_be6907 {
    struct dpct_type_12c54e {
      int shader_flag;
      int object_id;
    } mesh_light;
    struct  dpct_type_ff924c {
      float pad;
      float size;
    } lamp;
  } union_name; //Note the inialization here
} KernelLightDistribution;

int main() {

    std::cout << sizeof(KernelLightDistribution) << "\n";
    static_assert_align(KernelLightDistribution, 16);

    return 0;
}

 

 

Regards,

Rahul

View solution in original post

0 Kudos
4 Replies
RahulV_intel
Moderator
1,111 Views

Hi,

 

There is a small bug in your code. You have not initialized the union dpct_type_be6907. As a result, memory is not getting created for that particular data structure. Anonymous structs/unions are a bit lenient when it comes to initialization since they can be accessed right way and they are guaranteed to be present within a struct/union. (Memory always gets created for anonymous structs/unions)

 

Refer to the modified code snippet below.

 

#include <CL/sycl.hpp>
#include <iostream>

#define static_assert_align(st, align) \
  static_assert((sizeof(st) % (align) == 0), "Structure must be strictly aligned")  // NOLINT


typedef struct KernelLightDistribution {
  float totarea;
  int prim;
  union dpct_type_be6907 {
    struct dpct_type_12c54e {
      int shader_flag;
      int object_id;
    } mesh_light;
    struct  dpct_type_ff924c {
      float pad;
      float size;
    } lamp;
  } union_name; //Note the inialization here
} KernelLightDistribution;

int main() {

    std::cout << sizeof(KernelLightDistribution) << "\n";
    static_assert_align(KernelLightDistribution, 16);

    return 0;
}

 

 

Regards,

Rahul

0 Kudos
RahulV_intel
Moderator
1,099 Views

Hi Gagan,


Could you let me know if the solution provided helped?


Thanks,

Rahul


0 Kudos
Shukla__Gagandeep
1,095 Views

Hi Rahul,

Yes, that issue got solved. And yeah, once you pointed out, the mistake became quite clear.

But to match ported code with actual code, I went ahead with using anonymous structs/unions. Thanks for your help.

Regards,
Gagan

0 Kudos
RahulV_intel
Moderator
1,086 Views

Thanks for the confirmation.


Intel will no longer monitor this thread. However, it will remain open for community discussion.


0 Kudos
Reply