constant_manager_test.cpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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 "gtest/gtest.h"
  16. #include "source/opt/build_module.h"
  17. #include "source/opt/constants.h"
  18. #include "source/opt/ir_context.h"
  19. namespace spvtools {
  20. namespace opt {
  21. namespace analysis {
  22. namespace {
  23. using ConstantManagerTest = ::testing::Test;
  24. TEST_F(ConstantManagerTest, GetDefiningInstruction) {
  25. const std::string text = R"(
  26. %int = OpTypeInt 32 0
  27. %1 = OpTypeStruct %int
  28. %2 = OpTypeStruct %int
  29. )";
  30. std::unique_ptr<IRContext> context =
  31. BuildModule(SPV_ENV_UNIVERSAL_1_2, nullptr, text,
  32. SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS);
  33. ASSERT_NE(context, nullptr);
  34. Type* struct_type_1 = context->get_type_mgr()->GetType(1);
  35. StructConstant struct_const_1(struct_type_1->AsStruct());
  36. Instruction* const_inst_1 =
  37. context->get_constant_mgr()->GetDefiningInstruction(&struct_const_1, 1);
  38. EXPECT_EQ(const_inst_1->type_id(), 1);
  39. Type* struct_type_2 = context->get_type_mgr()->GetType(2);
  40. StructConstant struct_const_2(struct_type_2->AsStruct());
  41. Instruction* const_inst_2 =
  42. context->get_constant_mgr()->GetDefiningInstruction(&struct_const_2, 2);
  43. EXPECT_EQ(const_inst_2->type_id(), 2);
  44. }
  45. TEST_F(ConstantManagerTest, GetDefiningInstruction2) {
  46. const std::string text = R"(
  47. %int = OpTypeInt 32 0
  48. %1 = OpTypeStruct %int
  49. %2 = OpTypeStruct %int
  50. %3 = OpConstantNull %1
  51. %4 = OpConstantNull %2
  52. )";
  53. std::unique_ptr<IRContext> context =
  54. BuildModule(SPV_ENV_UNIVERSAL_1_2, nullptr, text,
  55. SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS);
  56. ASSERT_NE(context, nullptr);
  57. Type* struct_type_1 = context->get_type_mgr()->GetType(1);
  58. NullConstant struct_const_1(struct_type_1->AsStruct());
  59. Instruction* const_inst_1 =
  60. context->get_constant_mgr()->GetDefiningInstruction(&struct_const_1, 1);
  61. EXPECT_EQ(const_inst_1->type_id(), 1);
  62. EXPECT_EQ(const_inst_1->result_id(), 3);
  63. Type* struct_type_2 = context->get_type_mgr()->GetType(2);
  64. NullConstant struct_const_2(struct_type_2->AsStruct());
  65. Instruction* const_inst_2 =
  66. context->get_constant_mgr()->GetDefiningInstruction(&struct_const_2, 2);
  67. EXPECT_EQ(const_inst_2->type_id(), 2);
  68. EXPECT_EQ(const_inst_2->result_id(), 4);
  69. }
  70. TEST_F(ConstantManagerTest, GetDefiningInstructionIdOverflow) {
  71. const std::string text = R"(
  72. %1 = OpTypeInt 32 0
  73. %3 = OpConstant %1 1
  74. %4 = OpConstant %1 2
  75. )";
  76. std::unique_ptr<IRContext> context =
  77. BuildModule(SPV_ENV_UNIVERSAL_1_2, nullptr, text,
  78. SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS);
  79. ASSERT_NE(context, nullptr);
  80. // Set the id bound to the max, so the new constant cannot be generated.
  81. context->module()->SetIdBound(context->max_id_bound());
  82. Type* int_type = context->get_type_mgr()->GetType(1);
  83. IntConstant int_constant(int_type->AsInteger(), {3});
  84. Instruction* inst =
  85. context->get_constant_mgr()->GetDefiningInstruction(&int_constant, 1);
  86. EXPECT_EQ(inst, nullptr);
  87. }
  88. } // namespace
  89. } // namespace analysis
  90. } // namespace opt
  91. } // namespace spvtools