constant_manager_test.cpp 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. // Copyright (c) 2018 Google LLC
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. #include <memory>
  15. #include <string>
  16. #include "gmock/gmock.h"
  17. #include "gtest/gtest.h"
  18. #include "source/opt/build_module.h"
  19. #include "source/opt/constants.h"
  20. #include "source/opt/ir_context.h"
  21. namespace spvtools {
  22. namespace opt {
  23. namespace analysis {
  24. namespace {
  25. using ConstantManagerTest = ::testing::Test;
  26. TEST_F(ConstantManagerTest, GetDefiningInstruction) {
  27. const std::string text = R"(
  28. %int = OpTypeInt 32 0
  29. %1 = OpTypeStruct %int
  30. %2 = OpTypeStruct %int
  31. )";
  32. std::unique_ptr<IRContext> context =
  33. BuildModule(SPV_ENV_UNIVERSAL_1_2, nullptr, text,
  34. SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS);
  35. ASSERT_NE(context, nullptr);
  36. Type* struct_type_1 = context->get_type_mgr()->GetType(1);
  37. StructConstant struct_const_1(struct_type_1->AsStruct());
  38. Instruction* const_inst_1 =
  39. context->get_constant_mgr()->GetDefiningInstruction(&struct_const_1, 1);
  40. EXPECT_EQ(const_inst_1->type_id(), 1);
  41. Type* struct_type_2 = context->get_type_mgr()->GetType(2);
  42. StructConstant struct_const_2(struct_type_2->AsStruct());
  43. Instruction* const_inst_2 =
  44. context->get_constant_mgr()->GetDefiningInstruction(&struct_const_2, 2);
  45. EXPECT_EQ(const_inst_2->type_id(), 2);
  46. }
  47. TEST_F(ConstantManagerTest, GetDefiningInstruction2) {
  48. const std::string text = R"(
  49. %int = OpTypeInt 32 0
  50. %1 = OpTypeStruct %int
  51. %2 = OpTypeStruct %int
  52. %3 = OpConstantNull %1
  53. %4 = OpConstantNull %2
  54. )";
  55. std::unique_ptr<IRContext> context =
  56. BuildModule(SPV_ENV_UNIVERSAL_1_2, nullptr, text,
  57. SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS);
  58. ASSERT_NE(context, nullptr);
  59. Type* struct_type_1 = context->get_type_mgr()->GetType(1);
  60. NullConstant struct_const_1(struct_type_1->AsStruct());
  61. Instruction* const_inst_1 =
  62. context->get_constant_mgr()->GetDefiningInstruction(&struct_const_1, 1);
  63. EXPECT_EQ(const_inst_1->type_id(), 1);
  64. EXPECT_EQ(const_inst_1->result_id(), 3);
  65. Type* struct_type_2 = context->get_type_mgr()->GetType(2);
  66. NullConstant struct_const_2(struct_type_2->AsStruct());
  67. Instruction* const_inst_2 =
  68. context->get_constant_mgr()->GetDefiningInstruction(&struct_const_2, 2);
  69. EXPECT_EQ(const_inst_2->type_id(), 2);
  70. EXPECT_EQ(const_inst_2->result_id(), 4);
  71. }
  72. } // namespace
  73. } // namespace analysis
  74. } // namespace opt
  75. } // namespace spvtools