Community
cancel
Showing results for 
Search instead for 
Did you mean: 
Highlighted
53 Views

assert failes if struct tag-name is given

Jump to solution

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

Accepted Solutions
Highlighted
Moderator
39 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
Highlighted
Moderator
40 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
Highlighted
Moderator
28 Views

Hi Gagan,


Could you let me know if the solution provided helped?


Thanks,

Rahul


0 Kudos
Highlighted
24 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
Highlighted
Moderator
15 Views

Thanks for the confirmation.


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


0 Kudos