RawBufferMethods.h 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. //===------ RawBufferMethods.h ---- Raw Buffer Methods ----------*- 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. #ifndef LLVM_CLANG_SPIRV_RAWBUFFERMETHODS_H
  9. #define LLVM_CLANG_SPIRV_RAWBUFFERMETHODS_H
  10. class ASTContext;
  11. class SpirvBuilder;
  12. class SpirvInstruction;
  13. #include "SpirvEmitter.h"
  14. namespace clang {
  15. namespace spirv {
  16. class RawBufferHandler {
  17. public:
  18. RawBufferHandler(SpirvEmitter &emitter)
  19. : theEmitter(emitter), astContext(emitter.getASTContext()),
  20. spvBuilder(emitter.getSpirvBuilder()) {}
  21. /// \brief Performs (RW)ByteAddressBuffer.Load<T>(address).
  22. /// (RW)ByteAddressBuffers are represented as structs with only one member
  23. /// which is a runtime array in SPIR-V. This method works by loading one or
  24. /// more uints, and performing necessary casts and composite constructions
  25. /// to build the 'targetType'. The 'offset' parameter can be used for finer
  26. /// grained load of bitwidths smaller than 32-bits.
  27. ///
  28. /// Example:
  29. /// targetType = uint16_t, address=0, offset=0
  30. /// --> Load the first 16-bit uint starting at address 0.
  31. /// targetType = uint16_t, address=0, offset=16
  32. /// --> Load the second 16-bit uint starting at address 0.
  33. SpirvInstruction *processTemplatedLoadFromBuffer(SpirvInstruction *buffer,
  34. SpirvInstruction *&index,
  35. const QualType targetType,
  36. uint32_t &bitOffset);
  37. /// \brief Performs RWByteAddressBuffer.Store<T>(address, value).
  38. /// RWByteAddressBuffers are represented in SPIR-V as structs with only one
  39. /// member which is a runtime array of uints. This method works by decomposing
  40. /// the given |value| to reach numeric/bool types. Then performs necessary
  41. /// casts to uints and stores them in the underlying runtime array.
  42. /// The |bitOffset| parameter can be used for finer-grained bit-offset
  43. /// control.
  44. ///
  45. /// Example:
  46. /// targetType = uint16_t, address=0, offset=0
  47. /// --> Store to the first 16-bit uint starting at address 0.
  48. /// targetType = uint16_t, address=0, offset=16
  49. /// --> Store to the second 16-bit uint starting at address 0.
  50. void processTemplatedStoreToBuffer(SpirvInstruction *value,
  51. SpirvInstruction *buffer,
  52. SpirvInstruction *&index,
  53. const QualType valueType,
  54. uint32_t &bitOffset);
  55. private:
  56. SpirvInstruction *load16BitsAtBitOffset0(SpirvInstruction *buffer,
  57. SpirvInstruction *&index,
  58. QualType target16BitType,
  59. uint32_t &bitOffset);
  60. SpirvInstruction *load32BitsAtBitOffset0(SpirvInstruction *buffer,
  61. SpirvInstruction *&index,
  62. QualType target32BitType,
  63. uint32_t &bitOffset);
  64. SpirvInstruction *load64BitsAtBitOffset0(SpirvInstruction *buffer,
  65. SpirvInstruction *&index,
  66. QualType target64BitType,
  67. uint32_t &bitOffset);
  68. SpirvInstruction *load16BitsAtBitOffset16(SpirvInstruction *buffer,
  69. SpirvInstruction *&index,
  70. QualType target16BitType,
  71. uint32_t &bitOffset);
  72. private:
  73. void store16BitsAtBitOffset0(SpirvInstruction *value,
  74. SpirvInstruction *buffer,
  75. SpirvInstruction *&index,
  76. const QualType valueType);
  77. void store32BitsAtBitOffset0(SpirvInstruction *value,
  78. SpirvInstruction *buffer,
  79. SpirvInstruction *&index,
  80. const QualType valueType);
  81. void store64BitsAtBitOffset0(SpirvInstruction *value,
  82. SpirvInstruction *buffer,
  83. SpirvInstruction *&index,
  84. const QualType valueType);
  85. void store16BitsAtBitOffset16(SpirvInstruction *value,
  86. SpirvInstruction *buffer,
  87. SpirvInstruction *&index,
  88. const QualType valueType);
  89. void storeArrayOfScalars(std::deque<SpirvInstruction *> values,
  90. SpirvInstruction *buffer, SpirvInstruction *&index,
  91. const QualType valueType, uint32_t &bitOffset,
  92. SourceLocation);
  93. /// \brief Serializes the given values into their components until a scalar or
  94. /// a struct has been reached. Returns the most basic type it reaches.
  95. QualType serializeToScalarsOrStruct(std::deque<SpirvInstruction *> *values,
  96. QualType valueType, SourceLocation);
  97. private:
  98. /// \brief Performs an OpBitCast from |fromType| to |toType| on the given
  99. /// instruction.
  100. ///
  101. /// If the |toType| is a boolean type, it performs a regular type cast.
  102. ///
  103. /// If the |fromType| and |toType| are the same, does not thing and returns
  104. /// the given instruction
  105. SpirvInstruction *bitCastToNumericalOrBool(SpirvInstruction *instr,
  106. QualType fromType, QualType toType,
  107. SourceLocation loc);
  108. private:
  109. SpirvEmitter &theEmitter;
  110. ASTContext &astContext;
  111. SpirvBuilder &spvBuilder;
  112. };
  113. } // namespace spirv
  114. } // namespace clang
  115. #endif // LLVM_CLANG_SPIRV_RAWBUFFERMETHODS_H