AlignmentSizeCalculator.h 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. //===--- AlignmentSizeCalculator.h - Alignment 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. #ifndef LLVM_CLANG_LIB_SPIRV_ALIGNMENTSIZECALCULATOR_H
  10. #define LLVM_CLANG_LIB_SPIRV_ALIGNMENTSIZECALCULATOR_H
  11. #include "dxc/Support/SPIRVOptions.h"
  12. #include "clang/AST/ASTContext.h"
  13. namespace clang {
  14. namespace spirv {
  15. /// The class responsible to translate Clang frontend types into SPIR-V types.
  16. class AlignmentSizeCalculator {
  17. public:
  18. AlignmentSizeCalculator(ASTContext &astCtx, const SpirvCodeGenOptions &opts)
  19. : astContext(astCtx), spvOptions(opts) {}
  20. /// \brief Returns the alignment and size in bytes for the given type
  21. /// according to the given LayoutRule. If the caller has information about
  22. /// whether the type is a row-major matrix, that should also be passed in. If
  23. /// this information is not provided, the function tries to find any majorness
  24. /// attributes on the given type and use it.
  25. ///
  26. /// If the type is an array/matrix type, writes the array/matrix stride to
  27. /// stride.
  28. ///
  29. /// Note that the size returned is not exactly how many bytes the type
  30. /// will occupy in memory; rather it is used in conjunction with alignment
  31. /// to get the next available location (alignment + size), which means
  32. /// size contains post-paddings required by the given type.
  33. std::pair<uint32_t, uint32_t>
  34. getAlignmentAndSize(QualType type, SpirvLayoutRule rule,
  35. llvm::Optional<bool> isRowMajor, uint32_t *stride);
  36. /// \brief Aligns currentOffset properly to allow packing vectors in the HLSL
  37. /// way: using the element type's alignment as the vector alignment, as long
  38. /// as there is no improper straddle.
  39. /// fieldSize and fieldAlignment are the original size and alignment
  40. /// calculated without considering the HLSL vector relaxed rule.
  41. void alignUsingHLSLRelaxedLayout(QualType fieldType, uint32_t fieldSize,
  42. uint32_t fieldAlignment,
  43. uint32_t *currentOffset);
  44. private:
  45. /// Emits error to the diagnostic engine associated with this visitor.
  46. template <unsigned N>
  47. DiagnosticBuilder emitError(const char (&message)[N],
  48. SourceLocation srcLoc = {}) {
  49. const auto diagId = astContext.getDiagnostics().getCustomDiagID(
  50. clang::DiagnosticsEngine::Error, message);
  51. return astContext.getDiagnostics().Report(srcLoc, diagId);
  52. }
  53. private:
  54. ASTContext &astContext; /// AST context
  55. const SpirvCodeGenOptions &spvOptions; /// SPIR-V options
  56. };
  57. } // end namespace spirv
  58. } // end namespace clang
  59. #endif // LLVM_CLANG_LIB_SPIRV_ALIGNMENTSIZECALCULATOR_H