AlignmentSizeCalculator.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376
  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. if (isEnumType(type))
  132. type = astContext.IntTy;
  133. { // Rule 1
  134. QualType ty = {};
  135. if (isScalarType(type, &ty))
  136. if (const auto *builtinType = ty->getAs<BuiltinType>())
  137. switch (builtinType->getKind()) {
  138. case BuiltinType::Bool:
  139. case BuiltinType::Int:
  140. case BuiltinType::UInt:
  141. case BuiltinType::Float:
  142. return {4, 4};
  143. case BuiltinType::Double:
  144. case BuiltinType::LongLong:
  145. case BuiltinType::ULongLong:
  146. return {8, 8};
  147. case BuiltinType::Min12Int:
  148. case BuiltinType::Min16Int:
  149. case BuiltinType::Min16UInt:
  150. case BuiltinType::Min16Float:
  151. case BuiltinType::Min10Float: {
  152. if (spvOptions.enable16BitTypes)
  153. return {2, 2};
  154. else
  155. return {4, 4};
  156. }
  157. // the 'Half' enum always represents 16-bit floats.
  158. // int16_t and uint16_t map to Short and UShort.
  159. case BuiltinType::Short:
  160. case BuiltinType::UShort:
  161. case BuiltinType::Half:
  162. return {2, 2};
  163. // 'HalfFloat' always represents 32-bit floats.
  164. case BuiltinType::HalfFloat:
  165. return {4, 4};
  166. default:
  167. emitError("alignment and size calculation for type %0 unimplemented")
  168. << type;
  169. return {0, 0};
  170. }
  171. }
  172. { // Rule 2 and 3
  173. QualType elemType = {};
  174. uint32_t elemCount = {};
  175. if (isVectorType(type, &elemType, &elemCount)) {
  176. uint32_t alignment = 0, size = 0;
  177. std::tie(alignment, size) =
  178. getAlignmentAndSize(elemType, rule, isRowMajor, stride);
  179. // Use element alignment for fxc rules and VK_EXT_scalar_block_layout
  180. if (rule != SpirvLayoutRule::FxcCTBuffer &&
  181. rule != SpirvLayoutRule::FxcSBuffer &&
  182. rule != SpirvLayoutRule::Scalar)
  183. alignment = (elemCount == 3 ? 4 : elemCount) * size;
  184. return {alignment, elemCount * size};
  185. }
  186. }
  187. { // Rule 5 and 7
  188. QualType elemType = {};
  189. uint32_t rowCount = 0, colCount = 0;
  190. if (isMxNMatrix(type, &elemType, &rowCount, &colCount)) {
  191. uint32_t alignment = 0, size = 0;
  192. std::tie(alignment, size) =
  193. getAlignmentAndSize(elemType, rule, isRowMajor, stride);
  194. // Matrices are treated as arrays of vectors:
  195. // The base alignment and array stride are set to match the base alignment
  196. // of a single array element, according to rules 1, 2, and 3, and rounded
  197. // up to the base alignment of a vec4.
  198. bool rowMajor = isRowMajor.hasValue()
  199. ? isRowMajor.getValue()
  200. : isRowMajorMatrix(spvOptions, type);
  201. const uint32_t vecStorageSize = rowMajor ? rowCount : colCount;
  202. if (rule == SpirvLayoutRule::FxcSBuffer ||
  203. rule == SpirvLayoutRule::Scalar) {
  204. *stride = vecStorageSize * size;
  205. // Use element alignment for fxc structured buffers and
  206. // VK_EXT_scalar_block_layout
  207. return {alignment, rowCount * colCount * size};
  208. }
  209. alignment *= (vecStorageSize == 3 ? 4 : vecStorageSize);
  210. if (rule == SpirvLayoutRule::GLSLStd140 ||
  211. rule == SpirvLayoutRule::RelaxedGLSLStd140 ||
  212. rule == SpirvLayoutRule::FxcCTBuffer) {
  213. alignment = roundToPow2(alignment, kStd140Vec4Alignment);
  214. }
  215. *stride = alignment;
  216. size = (rowMajor ? colCount : rowCount) * alignment;
  217. return {alignment, size};
  218. }
  219. }
  220. // Rule 9
  221. if (const auto *structType = type->getAs<RecordType>()) {
  222. // Special case for handling empty structs, whose size is 0 and has no
  223. // requirement over alignment (thus 1).
  224. if (structType->getDecl()->field_empty())
  225. return {1, 0};
  226. uint32_t maxAlignment = 0;
  227. uint32_t structSize = 0;
  228. // If this struct is derived from some other structs, place an implicit
  229. // field at the very beginning for the base struct.
  230. if (const auto *cxxDecl = dyn_cast<CXXRecordDecl>(structType->getDecl())) {
  231. for (const auto base : cxxDecl->bases()) {
  232. uint32_t memberAlignment = 0, memberSize = 0;
  233. std::tie(memberAlignment, memberSize) =
  234. getAlignmentAndSize(base.getType(), rule, isRowMajor, stride);
  235. if (rule == SpirvLayoutRule::RelaxedGLSLStd140 ||
  236. rule == SpirvLayoutRule::RelaxedGLSLStd430 ||
  237. rule == SpirvLayoutRule::FxcCTBuffer) {
  238. alignUsingHLSLRelaxedLayout(base.getType(), memberSize,
  239. memberAlignment, &structSize);
  240. } else {
  241. structSize = roundToPow2(structSize, memberAlignment);
  242. }
  243. // The base alignment of the structure is N, where N is the largest
  244. // base alignment value of any of its members...
  245. maxAlignment = std::max(maxAlignment, memberAlignment);
  246. structSize += memberSize;
  247. }
  248. }
  249. for (const auto *field : structType->getDecl()->fields()) {
  250. uint32_t memberAlignment = 0, memberSize = 0;
  251. std::tie(memberAlignment, memberSize) =
  252. getAlignmentAndSize(field->getType(), rule, isRowMajor, stride);
  253. if (rule == SpirvLayoutRule::RelaxedGLSLStd140 ||
  254. rule == SpirvLayoutRule::RelaxedGLSLStd430 ||
  255. rule == SpirvLayoutRule::FxcCTBuffer) {
  256. alignUsingHLSLRelaxedLayout(field->getType(), memberSize,
  257. memberAlignment, &structSize);
  258. } else {
  259. structSize = roundToPow2(structSize, memberAlignment);
  260. }
  261. // Reset the current offset to the one specified in the source code
  262. // if exists. It's debatable whether we should do sanity check here.
  263. // If the developers want manually control the layout, we leave
  264. // everything to them.
  265. if (const auto *offsetAttr = field->getAttr<VKOffsetAttr>()) {
  266. structSize = offsetAttr->getOffset();
  267. }
  268. // The base alignment of the structure is N, where N is the largest
  269. // base alignment value of any of its members...
  270. maxAlignment = std::max(maxAlignment, memberAlignment);
  271. structSize += memberSize;
  272. }
  273. if (rule == SpirvLayoutRule::Scalar) {
  274. // A structure has a scalar alignment equal to the largest scalar
  275. // alignment of any of its members in VK_EXT_scalar_block_layout.
  276. return {maxAlignment, structSize};
  277. }
  278. if (rule == SpirvLayoutRule::GLSLStd140 ||
  279. rule == SpirvLayoutRule::RelaxedGLSLStd140 ||
  280. rule == SpirvLayoutRule::FxcCTBuffer) {
  281. // ... and rounded up to the base alignment of a vec4.
  282. maxAlignment = roundToPow2(maxAlignment, kStd140Vec4Alignment);
  283. }
  284. if (rule != SpirvLayoutRule::FxcCTBuffer &&
  285. rule != SpirvLayoutRule::FxcSBuffer) {
  286. // The base offset of the member following the sub-structure is rounded up
  287. // to the next multiple of the base alignment of the structure.
  288. structSize = roundToPow2(structSize, maxAlignment);
  289. }
  290. return {maxAlignment, structSize};
  291. }
  292. // Rule 4, 6, 8, and 10
  293. if (const auto *arrayType = astContext.getAsConstantArrayType(type)) {
  294. const auto elemCount = arrayType->getSize().getZExtValue();
  295. uint32_t alignment = 0, size = 0;
  296. std::tie(alignment, size) = getAlignmentAndSize(arrayType->getElementType(),
  297. rule, isRowMajor, stride);
  298. if (rule == SpirvLayoutRule::FxcSBuffer ||
  299. rule == SpirvLayoutRule::Scalar) {
  300. *stride = size;
  301. // Use element alignment for fxc structured buffers and
  302. // VK_EXT_scalar_block_layout
  303. return {alignment, size * elemCount};
  304. }
  305. if (rule == SpirvLayoutRule::GLSLStd140 ||
  306. rule == SpirvLayoutRule::RelaxedGLSLStd140 ||
  307. rule == SpirvLayoutRule::FxcCTBuffer) {
  308. // The base alignment and array stride are set to match the base alignment
  309. // of a single array element, according to rules 1, 2, and 3, and rounded
  310. // up to the base alignment of a vec4.
  311. alignment = roundToPow2(alignment, kStd140Vec4Alignment);
  312. }
  313. if (rule == SpirvLayoutRule::FxcCTBuffer) {
  314. // In fxc cbuffer/tbuffer packing rules, arrays does not affect the data
  315. // packing after it. But we still need to make sure paddings are inserted
  316. // internally if necessary.
  317. *stride = roundToPow2(size, alignment);
  318. size += *stride * (elemCount - 1);
  319. } else {
  320. // Need to round size up considering stride for scalar types
  321. size = roundToPow2(size, alignment);
  322. *stride = size; // Use size instead of alignment here for Rule 10
  323. size *= elemCount;
  324. // The base offset of the member following the array is rounded up to the
  325. // next multiple of the base alignment.
  326. size = roundToPow2(size, alignment);
  327. }
  328. return {alignment, size};
  329. }
  330. emitError("alignment and size calculation for type %0 unimplemented") << type;
  331. return {0, 0};
  332. }
  333. } // namespace spirv
  334. } // namespace clang