Files
third_party_spirv-tools/test/opt/constant_manager_test.cpp
T
Steven Perron ba38d349a6 Fix finding constant with particular type. (#1724)
With current implementation, the constant manager does not keep around
two constant with the same value but different types when the types
hash to the same value. So when you start looking for that constant you
will get a constant with the wrong type back.

I've made a few changes to the constant manager to fix this.  First off,
I have changed the map from constant to ids to be an std::multimap.
This way a single constant can be mapped to mutiple ids each
representing a different type.

Then when asking for an id of a constant, we can search all of the ids
associated with that constant in order to find the one with the correct
type.
2018-07-16 12:36:53 -04:00

87 lines
2.8 KiB
C++

// Copyright (c) 2018 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include <gmock/gmock.h>
#include <gtest/gtest.h>
#include "opt/build_module.h"
#include "opt/constants.h"
#include "opt/ir_context.h"
namespace spvtools {
namespace opt {
namespace analysis {
namespace {
using ConstantManagerTest = ::testing::Test;
TEST_F(ConstantManagerTest, GetDefiningInstruction) {
const std::string text = R"(
%int = OpTypeInt 32 0
%1 = OpTypeStruct %int
%2 = OpTypeStruct %int
)";
std::unique_ptr<IRContext> context =
BuildModule(SPV_ENV_UNIVERSAL_1_2, nullptr, text,
SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS);
ASSERT_NE(context, nullptr);
Type* struct_type_1 = context->get_type_mgr()->GetType(1);
StructConstant struct_const_1(struct_type_1->AsStruct());
Instruction* const_inst_1 =
context->get_constant_mgr()->GetDefiningInstruction(&struct_const_1, 1);
EXPECT_EQ(const_inst_1->type_id(), 1);
Type* struct_type_2 = context->get_type_mgr()->GetType(2);
StructConstant struct_const_2(struct_type_2->AsStruct());
Instruction* const_inst_2 =
context->get_constant_mgr()->GetDefiningInstruction(&struct_const_2, 2);
EXPECT_EQ(const_inst_2->type_id(), 2);
}
TEST_F(ConstantManagerTest, GetDefiningInstruction2) {
const std::string text = R"(
%int = OpTypeInt 32 0
%1 = OpTypeStruct %int
%2 = OpTypeStruct %int
%3 = OpConstantNull %1
%4 = OpConstantNull %2
)";
std::unique_ptr<IRContext> context =
BuildModule(SPV_ENV_UNIVERSAL_1_2, nullptr, text,
SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS);
ASSERT_NE(context, nullptr);
Type* struct_type_1 = context->get_type_mgr()->GetType(1);
NullConstant struct_const_1(struct_type_1->AsStruct());
Instruction* const_inst_1 =
context->get_constant_mgr()->GetDefiningInstruction(&struct_const_1, 1);
EXPECT_EQ(const_inst_1->type_id(), 1);
EXPECT_EQ(const_inst_1->result_id(), 3);
Type* struct_type_2 = context->get_type_mgr()->GetType(2);
NullConstant struct_const_2(struct_type_2->AsStruct());
Instruction* const_inst_2 =
context->get_constant_mgr()->GetDefiningInstruction(&struct_const_2, 2);
EXPECT_EQ(const_inst_2->type_id(), 2);
EXPECT_EQ(const_inst_2->result_id(), 4);
}
} // namespace
} // namespace analysis
} // namespace opt
} // namespace spvtools