AlignmentSizeCalculator.cpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401
  1. //===--- AlignmentSizeCalculator.cpp -- Alignemnt And Size Calc --*- C++ -*-==//
  2. //
  3. // The LLVM Compiler Infrastructure
  4. //
  5. // This file is distributed under the University of Illinois Open Source
  6. // License. See LICENSE.TXT for details.
  7. //
  8. //===----------------------------------------------------------------------===//
  9. #include "AlignmentSizeCalculator.h"
  10. #include "clang/AST/Attr.h"
  11. namespace {
  12. /// The alignment for 4-component float vectors.
  13. constexpr uint32_t kStd140Vec4Alignment = 16u;
  14. /// Rounds the given value up to the given power of 2.
  15. inline uint32_t roundToPow2(uint32_t val, uint32_t pow2) {
  16. assert(pow2 != 0);
  17. return (val + pow2 - 1) & ~(pow2 - 1);
  18. }
  19. /// Returns true if the given vector type (of the given size) crosses the
  20. /// 4-component vector boundary if placed at the given offset.
  21. bool improperStraddle(clang::QualType type, int size, int offset) {
  22. assert(clang::spirv::isVectorType(type));
  23. return size <= 16 ? offset / 16 != (offset + size - 1) / 16
  24. : offset % 16 != 0;
  25. }
  26. } // end anonymous namespace
  27. namespace clang {
  28. namespace spirv {
  29. void AlignmentSizeCalculator::alignUsingHLSLRelaxedLayout(
  30. QualType fieldType, uint32_t fieldSize, uint32_t fieldAlignment,
  31. uint32_t *currentOffset) {
  32. QualType vecElemType = {};
  33. const bool fieldIsVecType = isVectorType(fieldType, &vecElemType);
  34. // Adjust according to HLSL relaxed layout rules.
  35. // Aligning vectors as their element types so that we can pack a float
  36. // and a float3 tightly together.
  37. if (fieldIsVecType) {
  38. uint32_t scalarAlignment = 0;
  39. std::tie(scalarAlignment, std::ignore) = getAlignmentAndSize(
  40. vecElemType, SpirvLayoutRule::Void, /*isRowMajor*/ llvm::None, nullptr);
  41. if (scalarAlignment <= 4)
  42. fieldAlignment = scalarAlignment;
  43. }
  44. *currentOffset = roundToPow2(*currentOffset, fieldAlignment);
  45. // Adjust according to HLSL relaxed layout rules.
  46. // Bump to 4-component vector alignment if there is a bad straddle
  47. if (fieldIsVecType &&
  48. improperStraddle(fieldType, fieldSize, *currentOffset)) {
  49. fieldAlignment = kStd140Vec4Alignment;
  50. *currentOffset = roundToPow2(*currentOffset, fieldAlignment);
  51. }
  52. }
  53. std::pair<uint32_t, uint32_t> AlignmentSizeCalculator::getAlignmentAndSize(
  54. QualType type, SpirvLayoutRule rule, llvm::Optional<bool> isRowMajor,
  55. uint32_t *stride) {
  56. // std140 layout rules:
  57. // 1. If the member is a scalar consuming N basic machine units, the base
  58. // alignment is N.
  59. //
  60. // 2. If the member is a two- or four-component vector with components
  61. // consuming N basic machine units, the base alignment is 2N or 4N,
  62. // respectively.
  63. //
  64. // 3. If the member is a three-component vector with components consuming N
  65. // basic machine units, the base alignment is 4N.
  66. //
  67. // 4. If the member is an array of scalars or vectors, the base alignment and
  68. // array stride are set to match the base alignment of a single array
  69. // element, according to rules (1), (2), and (3), and rounded up to the
  70. // base alignment of a vec4. The array may have padding at the end; the
  71. // base offset of the member following the array is rounded up to the next
  72. // multiple of the base alignment.
  73. //
  74. // 5. If the member is a column-major matrix with C columns and R rows, the
  75. // matrix is stored identically to an array of C column vectors with R
  76. // components each, according to rule (4).
  77. //
  78. // 6. If the member is an array of S column-major matrices with C columns and
  79. // R rows, the matrix is stored identically to a row of S X C column
  80. // vectors with R components each, according to rule (4).
  81. //
  82. // 7. If the member is a row-major matrix with C columns and R rows, the
  83. // matrix is stored identically to an array of R row vectors with C
  84. // components each, according to rule (4).
  85. //
  86. // 8. If the member is an array of S row-major matrices with C columns and R
  87. // rows, the matrix is stored identically to a row of S X R row vectors
  88. // with C components each, according to rule (4).
  89. //
  90. // 9. If the member is a structure, the base alignment of the structure is N,
  91. // where N is the largest base alignment value of any of its members, and
  92. // rounded up to the base alignment of a vec4. The individual members of
  93. // this substructure are then assigned offsets by applying this set of
  94. // rules recursively, where the base offset of the first member of the
  95. // sub-structure is equal to the aligned offset of the structure. The
  96. // structure may have padding at the end; the base offset of the member
  97. // following the sub-structure is rounded up to the next multiple of the
  98. // base alignment of the structure.
  99. //
  100. // 10. If the member is an array of S structures, the S elements of the array
  101. // are laid out in order, according to rule (9).
  102. //
  103. // This method supports multiple layout rules, all of them modifying the
  104. // std140 rules listed above:
  105. //
  106. // std430:
  107. // - Array base alignment and stride does not need to be rounded up to a
  108. // multiple of 16.
  109. // - Struct base alignment does not need to be rounded up to a multiple of 16.
  110. //
  111. // Relaxed std140/std430:
  112. // - Vector base alignment is set as its element type's base alignment.
  113. //
  114. // FxcCTBuffer:
  115. // - Vector base alignment is set as its element type's base alignment.
  116. // - Arrays/structs do not need to have padding at the end; arrays/structs do
  117. // not affect the base offset of the member following them.
  118. // - For typeNxM matrix, if M > 1,
  119. // - It must be alinged to 16 bytes.
  120. // - Its size must be (16 * (M - 1)) + N * sizeof(type).
  121. // - We have the same rule for column_major typeNxM and row_major typeMxN.
  122. //
  123. // FxcSBuffer:
  124. // - Vector/matrix/array base alignment is set as its element type's base
  125. // alignment.
  126. // - Arrays/structs do not need to have padding at the end; arrays/structs do
  127. // not affect the base offset of the member following them.
  128. // - Struct base alignment does not need to be rounded up to a multiple of 16.
  129. const auto desugaredType = desugarType(type, &isRowMajor);
  130. if (desugaredType != type) {
  131. auto result = getAlignmentAndSize(desugaredType, rule, isRowMajor, stride);
  132. return result;
  133. }
  134. if (isEnumType(type))
  135. type = astContext.IntTy;
  136. { // Rule 1
  137. QualType ty = {};
  138. if (isScalarType(type, &ty))
  139. if (const auto *builtinType = ty->getAs<BuiltinType>())
  140. switch (builtinType->getKind()) {
  141. case BuiltinType::Bool:
  142. case BuiltinType::Int:
  143. case BuiltinType::UInt:
  144. case BuiltinType::Float:
  145. return {4, 4};
  146. case BuiltinType::Double:
  147. case BuiltinType::LongLong:
  148. case BuiltinType::ULongLong:
  149. return {8, 8};
  150. case BuiltinType::Min12Int:
  151. case BuiltinType::Min16Int:
  152. case BuiltinType::Min16UInt:
  153. case BuiltinType::Min16Float:
  154. case BuiltinType::Min10Float: {
  155. if (spvOptions.enable16BitTypes)
  156. return {2, 2};
  157. else
  158. return {4, 4};
  159. }
  160. // the 'Half' enum always represents 16-bit floats.
  161. // int16_t and uint16_t map to Short and UShort.
  162. case BuiltinType::Short:
  163. case BuiltinType::UShort:
  164. case BuiltinType::Half:
  165. return {2, 2};
  166. // 'HalfFloat' always represents 32-bit floats.
  167. case BuiltinType::HalfFloat:
  168. return {4, 4};
  169. default:
  170. emitError("alignment and size calculation for type %0 unimplemented")
  171. << type;
  172. return {0, 0};
  173. }
  174. }
  175. // FxcCTBuffer for typeNxM matrix where M > 1,
  176. // - It must be alinged to 16 bytes.
  177. // - Its size must be (16 * (M - 1)) + N * sizeof(type).
  178. // - We have the same rule for column_major typeNxM and row_major typeMxN.
  179. if (rule == SpirvLayoutRule::FxcCTBuffer && hlsl::IsHLSLMatType(type)) {
  180. uint32_t rowCount = 0, colCount = 0;
  181. hlsl::GetHLSLMatRowColCount(type, rowCount, colCount);
  182. if (!useRowMajor(isRowMajor, type))
  183. std::swap(rowCount, colCount);
  184. if (colCount > 1) {
  185. auto elemType = hlsl::GetHLSLMatElementType(type);
  186. uint32_t alignment = 0, size = 0;
  187. std::tie(alignment, size) =
  188. getAlignmentAndSize(elemType, rule, isRowMajor, stride);
  189. alignment = roundToPow2(alignment * (rowCount == 3 ? 4 : rowCount),
  190. kStd140Vec4Alignment);
  191. *stride = alignment;
  192. return {alignment, 16 * (colCount - 1) + rowCount * size};
  193. }
  194. }
  195. { // Rule 2 and 3
  196. QualType elemType = {};
  197. uint32_t elemCount = {};
  198. if (isVectorType(type, &elemType, &elemCount)) {
  199. uint32_t alignment = 0, size = 0;
  200. std::tie(alignment, size) =
  201. getAlignmentAndSize(elemType, rule, isRowMajor, stride);
  202. // Use element alignment for fxc rules and VK_EXT_scalar_block_layout
  203. if (rule != SpirvLayoutRule::FxcCTBuffer &&
  204. rule != SpirvLayoutRule::FxcSBuffer &&
  205. rule != SpirvLayoutRule::Scalar)
  206. alignment = (elemCount == 3 ? 4 : elemCount) * size;
  207. return {alignment, elemCount * size};
  208. }
  209. }
  210. { // Rule 5 and 7
  211. QualType elemType = {};
  212. uint32_t rowCount = 0, colCount = 0;
  213. if (isMxNMatrix(type, &elemType, &rowCount, &colCount)) {
  214. uint32_t alignment = 0, size = 0;
  215. std::tie(alignment, size) =
  216. getAlignmentAndSize(elemType, rule, isRowMajor, stride);
  217. // Matrices are treated as arrays of vectors:
  218. // The base alignment and array stride are set to match the base alignment
  219. // of a single array element, according to rules 1, 2, and 3, and rounded
  220. // up to the base alignment of a vec4.
  221. bool rowMajor = useRowMajor(isRowMajor, type);
  222. const uint32_t vecStorageSize = rowMajor ? rowCount : colCount;
  223. if (rule == SpirvLayoutRule::FxcSBuffer ||
  224. rule == SpirvLayoutRule::Scalar) {
  225. *stride = vecStorageSize * size;
  226. // Use element alignment for fxc structured buffers and
  227. // VK_EXT_scalar_block_layout
  228. return {alignment, rowCount * colCount * size};
  229. }
  230. alignment *= (vecStorageSize == 3 ? 4 : vecStorageSize);
  231. if (rule == SpirvLayoutRule::GLSLStd140 ||
  232. rule == SpirvLayoutRule::RelaxedGLSLStd140 ||
  233. rule == SpirvLayoutRule::FxcCTBuffer) {
  234. alignment = roundToPow2(alignment, kStd140Vec4Alignment);
  235. }
  236. *stride = alignment;
  237. size = (rowMajor ? colCount : rowCount) * alignment;
  238. return {alignment, size};
  239. }
  240. }
  241. // Rule 9
  242. if (const auto *structType = type->getAs<RecordType>()) {
  243. bool hasBaseStructs = type->getAsCXXRecordDecl() &&
  244. type->getAsCXXRecordDecl()->getNumBases() > 0;
  245. // Special case for handling empty structs, whose size is 0 and has no
  246. // requirement over alignment (thus 1).
  247. if (structType->getDecl()->field_empty() && !hasBaseStructs)
  248. return {1, 0};
  249. uint32_t maxAlignment = 0;
  250. uint32_t structSize = 0;
  251. // If this struct is derived from some other structs, place an implicit
  252. // field at the very beginning for the base struct.
  253. if (const auto *cxxDecl = dyn_cast<CXXRecordDecl>(structType->getDecl())) {
  254. for (const auto base : cxxDecl->bases()) {
  255. uint32_t memberAlignment = 0, memberSize = 0;
  256. std::tie(memberAlignment, memberSize) =
  257. getAlignmentAndSize(base.getType(), rule, isRowMajor, stride);
  258. if (rule == SpirvLayoutRule::RelaxedGLSLStd140 ||
  259. rule == SpirvLayoutRule::RelaxedGLSLStd430 ||
  260. rule == SpirvLayoutRule::FxcCTBuffer) {
  261. alignUsingHLSLRelaxedLayout(base.getType(), memberSize,
  262. memberAlignment, &structSize);
  263. } else {
  264. structSize = roundToPow2(structSize, memberAlignment);
  265. }
  266. // The base alignment of the structure is N, where N is the largest
  267. // base alignment value of any of its members...
  268. maxAlignment = std::max(maxAlignment, memberAlignment);
  269. structSize += memberSize;
  270. }
  271. }
  272. for (const auto *field : structType->getDecl()->fields()) {
  273. uint32_t memberAlignment = 0, memberSize = 0;
  274. std::tie(memberAlignment, memberSize) =
  275. getAlignmentAndSize(field->getType(), rule, isRowMajor, stride);
  276. if (rule == SpirvLayoutRule::RelaxedGLSLStd140 ||
  277. rule == SpirvLayoutRule::RelaxedGLSLStd430 ||
  278. rule == SpirvLayoutRule::FxcCTBuffer) {
  279. alignUsingHLSLRelaxedLayout(field->getType(), memberSize,
  280. memberAlignment, &structSize);
  281. } else {
  282. structSize = roundToPow2(structSize, memberAlignment);
  283. }
  284. // Reset the current offset to the one specified in the source code
  285. // if exists. It's debatable whether we should do sanity check here.
  286. // If the developers want manually control the layout, we leave
  287. // everything to them.
  288. if (const auto *offsetAttr = field->getAttr<VKOffsetAttr>()) {
  289. structSize = offsetAttr->getOffset();
  290. }
  291. // The base alignment of the structure is N, where N is the largest
  292. // base alignment value of any of its members...
  293. maxAlignment = std::max(maxAlignment, memberAlignment);
  294. structSize += memberSize;
  295. }
  296. if (rule == SpirvLayoutRule::Scalar) {
  297. // A structure has a scalar alignment equal to the largest scalar
  298. // alignment of any of its members in VK_EXT_scalar_block_layout.
  299. return {maxAlignment, structSize};
  300. }
  301. if (rule == SpirvLayoutRule::GLSLStd140 ||
  302. rule == SpirvLayoutRule::RelaxedGLSLStd140 ||
  303. rule == SpirvLayoutRule::FxcCTBuffer) {
  304. // ... and rounded up to the base alignment of a vec4.
  305. maxAlignment = roundToPow2(maxAlignment, kStd140Vec4Alignment);
  306. }
  307. if (rule != SpirvLayoutRule::FxcCTBuffer &&
  308. rule != SpirvLayoutRule::FxcSBuffer) {
  309. // The base offset of the member following the sub-structure is rounded up
  310. // to the next multiple of the base alignment of the structure.
  311. structSize = roundToPow2(structSize, maxAlignment);
  312. }
  313. return {maxAlignment, structSize};
  314. }
  315. // Rule 4, 6, 8, and 10
  316. if (const auto *arrayType = astContext.getAsConstantArrayType(type)) {
  317. const auto elemCount = arrayType->getSize().getZExtValue();
  318. uint32_t alignment = 0, size = 0;
  319. std::tie(alignment, size) = getAlignmentAndSize(arrayType->getElementType(),
  320. rule, isRowMajor, stride);
  321. if (rule == SpirvLayoutRule::FxcSBuffer ||
  322. rule == SpirvLayoutRule::Scalar) {
  323. *stride = size;
  324. // Use element alignment for fxc structured buffers and
  325. // VK_EXT_scalar_block_layout
  326. return {alignment, size * elemCount};
  327. }
  328. if (rule == SpirvLayoutRule::GLSLStd140 ||
  329. rule == SpirvLayoutRule::RelaxedGLSLStd140 ||
  330. rule == SpirvLayoutRule::FxcCTBuffer) {
  331. // The base alignment and array stride are set to match the base alignment
  332. // of a single array element, according to rules 1, 2, and 3, and rounded
  333. // up to the base alignment of a vec4.
  334. alignment = roundToPow2(alignment, kStd140Vec4Alignment);
  335. }
  336. if (rule == SpirvLayoutRule::FxcCTBuffer) {
  337. // In fxc cbuffer/tbuffer packing rules, arrays does not affect the data
  338. // packing after it. But we still need to make sure paddings are inserted
  339. // internally if necessary.
  340. *stride = roundToPow2(size, alignment);
  341. size += *stride * (elemCount - 1);
  342. } else {
  343. // Need to round size up considering stride for scalar types
  344. size = roundToPow2(size, alignment);
  345. *stride = size; // Use size instead of alignment here for Rule 10
  346. size *= elemCount;
  347. // The base offset of the member following the array is rounded up to the
  348. // next multiple of the base alignment.
  349. size = roundToPow2(size, alignment);
  350. }
  351. return {alignment, size};
  352. }
  353. emitError("alignment and size calculation for type %0 unimplemented") << type;
  354. return {0, 0};
  355. }
  356. } // namespace spirv
  357. } // namespace clang