ModuleBuilderTest.cpp 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. //===- unittests/SPIRV/ModuleBuilderTest.cpp ------ ModuleBuilder 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 "clang/SPIRV/ModuleBuilder.h"
  10. #include "spirv/unified1/spirv.hpp11"
  11. #include "SPIRVTestUtils.h"
  12. namespace {
  13. using namespace clang::spirv;
  14. using ::testing::ContainerEq;
  15. using ::testing::ElementsAre;
  16. TEST(ModuleBuilder, TakeModuleDirectlyCreatesHeader) {
  17. SPIRVContext context;
  18. ModuleBuilder builder(&context, nullptr, {0});
  19. EXPECT_THAT(builder.takeModule(),
  20. ElementsAre(spv::MagicNumber, 0x00010000, 14u << 16, 1u, 0u));
  21. }
  22. TEST(ModuleBuilder, CreateFunction) {
  23. SPIRVContext context;
  24. ModuleBuilder builder(&context, nullptr, {0});
  25. const auto rType = context.takeNextId();
  26. const auto fType = context.takeNextId();
  27. const auto fId = context.getNextId();
  28. const auto resultId = builder.beginFunction(fType, rType);
  29. EXPECT_EQ(fId, resultId);
  30. EXPECT_TRUE(builder.endFunction());
  31. const auto result = builder.takeModule();
  32. SimpleInstBuilder sib(context.getNextId());
  33. sib.inst(spv::Op::OpFunction, {rType, fId, 0, fType});
  34. sib.inst(spv::Op::OpFunctionEnd, {});
  35. EXPECT_THAT(result, ContainerEq(sib.get()));
  36. }
  37. TEST(ModuleBuilder, CreateBasicBlock) {
  38. SPIRVContext context;
  39. ModuleBuilder builder(&context, nullptr, {0});
  40. const auto rType = context.takeNextId();
  41. const auto fType = context.takeNextId();
  42. const auto fId = context.getNextId();
  43. EXPECT_NE(0U, builder.beginFunction(fType, rType));
  44. const auto labelId = context.getNextId();
  45. const auto resultId = builder.createBasicBlock();
  46. EXPECT_EQ(labelId, resultId);
  47. builder.setInsertPoint(resultId);
  48. builder.createReturn();
  49. EXPECT_TRUE(builder.endFunction());
  50. const auto result = builder.takeModule();
  51. SimpleInstBuilder sib(context.getNextId());
  52. sib.inst(spv::Op::OpFunction, {rType, fId, 0, fType});
  53. sib.inst(spv::Op::OpLabel, {labelId});
  54. sib.inst(spv::Op::OpReturn, {});
  55. sib.inst(spv::Op::OpFunctionEnd, {});
  56. EXPECT_THAT(result, ContainerEq(sib.get()));
  57. }
  58. } // anonymous namespace