brintos

brintos / llvm-project-archived public Read only

0
0
Text · 1.8 KiB · b03a4fc Raw
55 lines · plain
1.. title:: clang-tidy - altera-struct-pack-align2 3altera-struct-pack-align4========================5 6Finds structs that are inefficiently packed or aligned, and recommends7packing and/or aligning of said structs as needed.8 9Structs that are not packed take up more space than they should, and accessing10structs that are not well aligned is inefficient.11 12Fix-its are provided to fix both of these issues by inserting and/or amending13relevant struct attributes.14 15Based on the `Altera SDK for OpenCL: Best Practices Guide16<https://www.altera.com/en_US/pdfs/literature/hb/opencl-sdk/aocl_optimization_guide.pdf>`_.17 18.. code-block:: c++19 20  // The following struct is originally aligned to 4 bytes, and thus takes up21  // 12 bytes of memory instead of 10. Packing the struct will make it use22  // only 10 bytes of memory, and aligning it to 16 bytes will make it23  // efficient to access.24  struct example {25    char a;    // 1 byte26    double b;  // 8 bytes27    char c;    // 1 byte28  };29 30  // The following struct is arranged in such a way that packing is not needed.31  // However, it is aligned to 4 bytes instead of 8, and thus needs to be32  // explicitly aligned.33  struct implicitly_packed_example {34    char a;  // 1 byte35    char b;  // 1 byte36    char c;  // 1 byte37    char d;  // 1 byte38    int e;   // 4 bytes39  };40 41  // The following struct is explicitly aligned and packed.42  struct good_example {43    char a;    // 1 byte44    double b;  // 8 bytes45    char c;    // 1 byte46  } __attribute__((packed)) __attribute__((aligned(16));47 48  // Explicitly aligning a struct to the wrong value will result in a warning.49  // The following example should be aligned to 16 bytes, not 32.50  struct badly_aligned_example {51    char a;    // 1 byte52    double b;  // 8 bytes53    char c;    // 1 byte54  } __attribute__((packed)) __attribute__((aligned(32)));55