ConstantTest.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  1. //===- unittests/SPIRV/ConstantTest.cpp ---------- Constant tests ---------===//
  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 "SPIRVTestUtils.h"
  10. #include "gmock/gmock.h"
  11. #include "clang/SPIRV/BitwiseCast.h"
  12. #include "clang/SPIRV/Constant.h"
  13. #include "clang/SPIRV/SPIRVContext.h"
  14. #include "gtest/gtest.h"
  15. using namespace clang::spirv;
  16. namespace {
  17. using ::testing::ElementsAre;
  18. using ::testing::ContainerEq;
  19. TEST(Constant, True) {
  20. SPIRVContext ctx;
  21. const Constant *c = Constant::getTrue(ctx, 2);
  22. const auto result = c->withResultId(3);
  23. const auto expected = constructInst(spv::Op::OpConstantTrue, {2, 3});
  24. EXPECT_THAT(result, ContainerEq(expected));
  25. }
  26. TEST(Constant, False) {
  27. SPIRVContext ctx;
  28. const Constant *c = Constant::getFalse(ctx, 2);
  29. const auto result = c->withResultId(3);
  30. const auto expected = constructInst(spv::Op::OpConstantFalse, {2, 3});
  31. EXPECT_THAT(result, ContainerEq(expected));
  32. }
  33. TEST(Constant, Uint32) {
  34. SPIRVContext ctx;
  35. const Constant *c = Constant::getUint32(ctx, 2, 7u);
  36. const auto result = c->withResultId(3);
  37. const auto expected = constructInst(spv::Op::OpConstant, {2, 3, 7u});
  38. EXPECT_THAT(result, ContainerEq(expected));
  39. }
  40. TEST(Constant, Int32) {
  41. SPIRVContext ctx;
  42. const Constant *c = Constant::getInt32(ctx, 2, -7);
  43. const auto result = c->withResultId(3);
  44. const auto expected = constructInst(spv::Op::OpConstant, {2, 3, 0xFFFFFFF9});
  45. EXPECT_THAT(result, ContainerEq(expected));
  46. }
  47. TEST(Constant, Float32) {
  48. SPIRVContext ctx;
  49. const Constant *c = Constant::getFloat32(ctx, 2, 7.0);
  50. const auto result = c->withResultId(3);
  51. const auto expected = constructInst(
  52. spv::Op::OpConstant, {2, 3, cast::BitwiseCast<uint32_t, float>(7.0)});
  53. EXPECT_THAT(result, ContainerEq(expected));
  54. }
  55. TEST(Constant, Composite) {
  56. SPIRVContext ctx;
  57. const Constant *c = Constant::getComposite(ctx, 8, {4, 5, 6, 7});
  58. const auto result = c->withResultId(9);
  59. const auto expected =
  60. constructInst(spv::Op::OpConstantComposite, {8, 9, 4, 5, 6, 7});
  61. EXPECT_THAT(result, ContainerEq(expected));
  62. }
  63. TEST(Constant, Sampler) {
  64. SPIRVContext ctx;
  65. const Constant *c =
  66. Constant::getSampler(ctx, 8, spv::SamplerAddressingMode::Repeat, 1,
  67. spv::SamplerFilterMode::Linear);
  68. const auto result = c->withResultId(9);
  69. const auto expected = constructInst(
  70. spv::Op::OpConstantSampler,
  71. {8, 9, static_cast<uint32_t>(spv::SamplerAddressingMode::Repeat), 1,
  72. static_cast<uint32_t>(spv::SamplerFilterMode::Linear)});
  73. EXPECT_THAT(result, ContainerEq(expected));
  74. }
  75. TEST(Constant, Null) {
  76. SPIRVContext ctx;
  77. const Constant *c = Constant::getNull(ctx, 8);
  78. const auto result = c->withResultId(9);
  79. const auto expected = constructInst(spv::Op::OpConstantNull, {8, 9});
  80. EXPECT_THAT(result, ContainerEq(expected));
  81. }
  82. TEST(Constant, SpecTrue) {
  83. SPIRVContext ctx;
  84. const Constant *c = Constant::getSpecTrue(ctx, 2);
  85. const auto result = c->withResultId(3);
  86. const auto expected = constructInst(spv::Op::OpSpecConstantTrue, {2, 3});
  87. EXPECT_THAT(result, ContainerEq(expected));
  88. }
  89. TEST(Constant, SpecFalse) {
  90. SPIRVContext ctx;
  91. const Constant *c = Constant::getSpecFalse(ctx, 2);
  92. const auto result = c->withResultId(3);
  93. const auto expected = constructInst(spv::Op::OpSpecConstantFalse, {2, 3});
  94. EXPECT_THAT(result, ContainerEq(expected));
  95. }
  96. TEST(Constant, SpecUint32) {
  97. SPIRVContext ctx;
  98. const Constant *c = Constant::getSpecUint32(ctx, 2, 7u);
  99. const auto result = c->withResultId(3);
  100. const auto expected = constructInst(spv::Op::OpSpecConstant, {2, 3, 7u});
  101. EXPECT_THAT(result, ContainerEq(expected));
  102. }
  103. TEST(Constant, SpecInt32) {
  104. SPIRVContext ctx;
  105. const Constant *c = Constant::getSpecInt32(ctx, 2, -7);
  106. const auto result = c->withResultId(3);
  107. const auto expected =
  108. constructInst(spv::Op::OpSpecConstant, {2, 3, 0xFFFFFFF9});
  109. EXPECT_THAT(result, ContainerEq(expected));
  110. }
  111. TEST(Constant, SpecFloat32) {
  112. SPIRVContext ctx;
  113. const Constant *c = Constant::getSpecFloat32(ctx, 2, 7.0);
  114. const auto result = c->withResultId(3);
  115. const auto expected = constructInst(
  116. spv::Op::OpSpecConstant, {2, 3, cast::BitwiseCast<uint32_t, float>(7.0)});
  117. EXPECT_THAT(result, ContainerEq(expected));
  118. }
  119. TEST(Constant, SpecComposite) {
  120. SPIRVContext ctx;
  121. const Constant *c = Constant::getSpecComposite(ctx, 8, {4, 5, 6, 7});
  122. const auto result = c->withResultId(9);
  123. const auto expected =
  124. constructInst(spv::Op::OpSpecConstantComposite, {8, 9, 4, 5, 6, 7});
  125. EXPECT_THAT(result, ContainerEq(expected));
  126. }
  127. TEST(Constant, DecoratedTrue) {
  128. SPIRVContext ctx;
  129. const Decoration *d = Decoration::getSpecId(ctx, 5);
  130. const Constant *c = Constant::getTrue(ctx, 2, {d});
  131. EXPECT_EQ(c->getOpcode(), spv::Op::OpConstantTrue);
  132. EXPECT_EQ(c->getTypeId(), 2);
  133. EXPECT_TRUE(c->getArgs().empty());
  134. EXPECT_THAT(c->getDecorations(), ElementsAre(d));
  135. }
  136. TEST(Constant, DecoratedFalse) {
  137. SPIRVContext ctx;
  138. const Decoration *d = Decoration::getSpecId(ctx, 5);
  139. const Constant *c = Constant::getFalse(ctx, 2, {d});
  140. EXPECT_EQ(c->getOpcode(), spv::Op::OpConstantFalse);
  141. EXPECT_EQ(c->getTypeId(), 2);
  142. EXPECT_TRUE(c->getArgs().empty());
  143. EXPECT_THAT(c->getDecorations(), ElementsAre(d));
  144. }
  145. TEST(Constant, DecoratedUint32) {
  146. SPIRVContext ctx;
  147. const Decoration *d = Decoration::getSpecId(ctx, 5);
  148. const Constant *c = Constant::getUint32(ctx, 2, 7u, {d});
  149. EXPECT_EQ(c->getOpcode(), spv::Op::OpConstant);
  150. EXPECT_EQ(c->getTypeId(), 2);
  151. EXPECT_THAT(c->getArgs(), ElementsAre(7u));
  152. EXPECT_THAT(c->getDecorations(), ElementsAre(d));
  153. }
  154. TEST(Constant, DecoratedInt32) {
  155. SPIRVContext ctx;
  156. const Decoration *d = Decoration::getSpecId(ctx, 5);
  157. const Constant *c = Constant::getInt32(ctx, 2, -7, {d});
  158. EXPECT_EQ(c->getOpcode(), spv::Op::OpConstant);
  159. EXPECT_EQ(c->getTypeId(), 2);
  160. EXPECT_THAT(c->getArgs(), ElementsAre(0xFFFFFFF9));
  161. EXPECT_THAT(c->getDecorations(), ElementsAre(d));
  162. }
  163. TEST(Constant, DecoratedFloat32) {
  164. SPIRVContext ctx;
  165. const Decoration *d = Decoration::getSpecId(ctx, 5);
  166. const Constant *c = Constant::getFloat32(ctx, 2, 7.0f, {d});
  167. EXPECT_EQ(c->getOpcode(), spv::Op::OpConstant);
  168. EXPECT_EQ(c->getTypeId(), 2);
  169. EXPECT_THAT(c->getArgs(),
  170. ElementsAre(cast::BitwiseCast<uint32_t, float>(7.0)));
  171. EXPECT_THAT(c->getDecorations(), ElementsAre(d));
  172. }
  173. TEST(Constant, DecoratedComposite) {
  174. SPIRVContext ctx;
  175. const Decoration *d = Decoration::getSpecId(ctx, 5);
  176. const Constant *c = Constant::getComposite(ctx, 8, {4, 5, 6, 7}, {d});
  177. EXPECT_EQ(c->getOpcode(), spv::Op::OpConstantComposite);
  178. EXPECT_EQ(c->getTypeId(), 8);
  179. EXPECT_THAT(c->getArgs(), ElementsAre(4, 5, 6, 7));
  180. EXPECT_THAT(c->getDecorations(), ElementsAre(d));
  181. }
  182. TEST(Constant, DecoratedSampler) {
  183. SPIRVContext ctx;
  184. const Decoration *d = Decoration::getSpecId(ctx, 5);
  185. const Constant *c =
  186. Constant::getSampler(ctx, 8, spv::SamplerAddressingMode::Repeat, 1,
  187. spv::SamplerFilterMode::Linear, {d});
  188. EXPECT_EQ(c->getOpcode(), spv::Op::OpConstantSampler);
  189. EXPECT_EQ(c->getTypeId(), 8);
  190. EXPECT_THAT(
  191. c->getArgs(),
  192. ElementsAre(static_cast<uint32_t>(spv::SamplerAddressingMode::Repeat), 1,
  193. static_cast<uint32_t>(spv::SamplerFilterMode::Linear)));
  194. EXPECT_THAT(c->getDecorations(), ElementsAre(d));
  195. }
  196. TEST(Constant, DecoratedNull) {
  197. SPIRVContext ctx;
  198. const Decoration *d = Decoration::getSpecId(ctx, 5);
  199. const Constant *c = Constant::getNull(ctx, 2, {d});
  200. EXPECT_EQ(c->getOpcode(), spv::Op::OpConstantNull);
  201. EXPECT_EQ(c->getTypeId(), 2);
  202. EXPECT_TRUE(c->getArgs().empty());
  203. EXPECT_THAT(c->getDecorations(), ElementsAre(d));
  204. }
  205. TEST(Constant, DecoratedSpecTrue) {
  206. SPIRVContext ctx;
  207. const Decoration *d = Decoration::getSpecId(ctx, 5);
  208. const Constant *c = Constant::getSpecTrue(ctx, 2, {d});
  209. EXPECT_EQ(c->getOpcode(), spv::Op::OpSpecConstantTrue);
  210. EXPECT_EQ(c->getTypeId(), 2);
  211. EXPECT_TRUE(c->getArgs().empty());
  212. EXPECT_THAT(c->getDecorations(), ElementsAre(d));
  213. }
  214. TEST(Constant, DecoratedSpecFalse) {
  215. SPIRVContext ctx;
  216. const Decoration *d = Decoration::getSpecId(ctx, 5);
  217. const Constant *c = Constant::getSpecFalse(ctx, 2, {d});
  218. EXPECT_EQ(c->getOpcode(), spv::Op::OpSpecConstantFalse);
  219. EXPECT_EQ(c->getTypeId(), 2);
  220. EXPECT_TRUE(c->getArgs().empty());
  221. EXPECT_THAT(c->getDecorations(), ElementsAre(d));
  222. }
  223. TEST(Constant, DecoratedSpecUint32) {
  224. SPIRVContext ctx;
  225. const Decoration *d = Decoration::getSpecId(ctx, 5);
  226. const Constant *c = Constant::getSpecUint32(ctx, 2, 7u, {d});
  227. EXPECT_EQ(c->getOpcode(), spv::Op::OpSpecConstant);
  228. EXPECT_EQ(c->getTypeId(), 2);
  229. EXPECT_THAT(c->getArgs(), ElementsAre(7u));
  230. EXPECT_THAT(c->getDecorations(), ElementsAre(d));
  231. }
  232. TEST(Constant, DecoratedSpecInt32) {
  233. SPIRVContext ctx;
  234. const Decoration *d = Decoration::getSpecId(ctx, 5);
  235. const Constant *c = Constant::getSpecInt32(ctx, 2, -7, {d});
  236. EXPECT_EQ(c->getOpcode(), spv::Op::OpSpecConstant);
  237. EXPECT_EQ(c->getTypeId(), 2);
  238. EXPECT_THAT(c->getArgs(), ElementsAre(0xFFFFFFF9));
  239. EXPECT_THAT(c->getDecorations(), ElementsAre(d));
  240. }
  241. TEST(Constant, DecoratedSpecFloat32) {
  242. SPIRVContext ctx;
  243. const Decoration *d = Decoration::getSpecId(ctx, 5);
  244. const Constant *c = Constant::getSpecFloat32(ctx, 2, 7.0f, {d});
  245. EXPECT_EQ(c->getOpcode(), spv::Op::OpSpecConstant);
  246. EXPECT_EQ(c->getTypeId(), 2);
  247. EXPECT_THAT(c->getArgs(),
  248. ElementsAre(cast::BitwiseCast<uint32_t, float>(7.0)));
  249. EXPECT_THAT(c->getDecorations(), ElementsAre(d));
  250. }
  251. TEST(Constant, DecoratedSpecComposite) {
  252. SPIRVContext ctx;
  253. const Decoration *d = Decoration::getSpecId(ctx, 5);
  254. const Constant *c = Constant::getSpecComposite(ctx, 8, {4, 5, 6, 7}, {d});
  255. EXPECT_EQ(c->getOpcode(), spv::Op::OpSpecConstantComposite);
  256. EXPECT_EQ(c->getTypeId(), 8);
  257. EXPECT_THAT(c->getArgs(), ElementsAre(4, 5, 6, 7));
  258. EXPECT_THAT(c->getDecorations(), ElementsAre(d));
  259. }
  260. TEST(Constant, ConstantsWithSameBitPatternButDifferentTypeIdAreNotEqual) {
  261. SPIRVContext ctx;
  262. const Constant *int1 = Constant::getInt32(ctx, /*type_id*/ 1, 0);
  263. const Constant *uint1 = Constant::getUint32(ctx, /*type_id*/ 2, 0);
  264. const Constant *float1 = Constant::getFloat32(ctx, /*type_id*/ 3, 0);
  265. const Constant *anotherInt1 = Constant::getInt32(ctx, /*type_id*/ 4, 0);
  266. EXPECT_FALSE(*int1 == *uint1);
  267. EXPECT_FALSE(*int1 == *float1);
  268. EXPECT_FALSE(*uint1 == *float1);
  269. EXPECT_FALSE(*int1 == *anotherInt1);
  270. }
  271. } // anonymous namespace