AlignmentSizeCalculator.cpp 14 KB

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