Revert "Have the constant manager take ownership of constants."

This reverts commit b938b74bac.
This commit is contained in:
Steven Perron 2018-08-24 15:12:49 -04:00
parent b938b74bac
commit 47ee776a2c
3 changed files with 20 additions and 26 deletions

View File

@ -196,19 +196,19 @@ Instruction* ConstantManager::GetDefiningInstruction(
}
}
std::unique_ptr<Constant> ConstantManager::CreateConstant(
const Constant* ConstantManager::CreateConstant(
const Type* type, const std::vector<uint32_t>& literal_words_or_ids) const {
if (literal_words_or_ids.size() == 0) {
// Constant declared with OpConstantNull
return MakeUnique<NullConstant>(type);
return new NullConstant(type);
} else if (auto* bt = type->AsBool()) {
assert(literal_words_or_ids.size() == 1 &&
"Bool constant should be declared with one operand");
return MakeUnique<BoolConstant>(bt, literal_words_or_ids.front());
return new BoolConstant(bt, literal_words_or_ids.front());
} else if (auto* it = type->AsInteger()) {
return MakeUnique<IntConstant>(it, literal_words_or_ids);
return new IntConstant(it, literal_words_or_ids);
} else if (auto* ft = type->AsFloat()) {
return MakeUnique<FloatConstant>(ft, literal_words_or_ids);
return new FloatConstant(ft, literal_words_or_ids);
} else if (auto* vt = type->AsVector()) {
auto components = GetConstantsFromIds(literal_words_or_ids);
if (components.empty()) return nullptr;
@ -231,19 +231,19 @@ std::unique_ptr<Constant> ConstantManager::CreateConstant(
return false;
}))
return nullptr;
return MakeUnique<VectorConstant>(vt, components);
return new VectorConstant(vt, components);
} else if (auto* mt = type->AsMatrix()) {
auto components = GetConstantsFromIds(literal_words_or_ids);
if (components.empty()) return nullptr;
return MakeUnique<MatrixConstant>(mt, components);
return new MatrixConstant(mt, components);
} else if (auto* st = type->AsStruct()) {
auto components = GetConstantsFromIds(literal_words_or_ids);
if (components.empty()) return nullptr;
return MakeUnique<StructConstant>(st, components);
return new StructConstant(st, components);
} else if (auto* at = type->AsArray()) {
auto components = GetConstantsFromIds(literal_words_or_ids);
if (components.empty()) return nullptr;
return MakeUnique<ArrayConstant>(at, components);
return new ArrayConstant(at, components);
} else {
return nullptr;
}
@ -344,7 +344,7 @@ std::unique_ptr<Instruction> ConstantManager::CreateCompositeInstruction(
const Constant* ConstantManager::GetConstant(
const Type* type, const std::vector<uint32_t>& literal_words_or_ids) {
auto cst = CreateConstant(type, literal_words_or_ids);
return cst ? RegisterConstant(std::move(cst)) : nullptr;
return cst ? RegisterConstant(cst) : nullptr;
}
std::vector<const analysis::Constant*> Constant::GetVectorComponents(

View File

@ -578,18 +578,14 @@ class ConstantManager {
// Registers a new constant |cst| in the constant pool. If the constant
// existed already, it returns a pointer to the previously existing Constant
// in the pool. Otherwise, it returns |cst|.
const Constant* RegisterConstant(std::unique_ptr<Constant> cst) {
auto ret = const_pool_.insert(cst.get());
if (ret.second) {
owned_constants_.emplace_back(std::move(cst));
} // else delete |cst|.
const Constant* RegisterConstant(const Constant* cst) {
auto ret = const_pool_.insert(cst);
return *ret.first;
}
// A helper function to get a vector of Constant instances with the specified
// ids. If it can not find the Constant instance for any one of the ids,
// it returns an empty vector.
std::vector<const Constant*> GetConstantsFromIds(
const std::vector<uint32_t>& ids) const;
@ -637,7 +633,7 @@ class ConstantManager {
// type, either Bool, Integer or Float. If any of the rules above failed, the
// creation will fail and nullptr will be returned. If the vector is empty,
// a NullConstant instance will be created with the given type.
std::unique_ptr<Constant> CreateConstant(
const Constant* CreateConstant(
const Type* type,
const std::vector<uint32_t>& literal_words_or_ids) const;
@ -684,10 +680,6 @@ class ConstantManager {
// The constant pool. All created constants are registered here.
std::unordered_set<const Constant*, ConstantHash, ConstantEqual> const_pool_;
// The constant that are owned by the constant manager. Every constant in
// |const_pool_| should be in |owned_constants_| as well.
std::vector<std::unique_ptr<Constant>> owned_constants_;
};
} // namespace analysis

View File

@ -279,10 +279,11 @@ Instruction* FoldSpecConstantOpAndCompositePass::DoVectorShuffle(
"Literal index out of bound of the concatenated vector");
selected_components.push_back(concatenated_components[literal]);
}
auto new_vec_const = MakeUnique<analysis::VectorConstant>(
result_vec_type, selected_components);
auto new_vec_const =
new analysis::VectorConstant(result_vec_type, selected_components);
auto reg_vec_const =
context()->get_constant_mgr()->RegisterConstant(std::move(new_vec_const));
context()->get_constant_mgr()->RegisterConstant(new_vec_const);
if (reg_vec_const != new_vec_const) delete new_vec_const;
return context()->get_constant_mgr()->BuildInstructionAndAddToModule(
reg_vec_const, pos);
}
@ -367,10 +368,11 @@ Instruction* FoldSpecConstantOpAndCompositePass::DoComponentWiseOperation(
assert(false && "Failed to create constants with 32-bit word");
}
}
auto new_vec_const = MakeUnique<analysis::VectorConstant>(result_type->AsVector(),
auto new_vec_const = new analysis::VectorConstant(result_type->AsVector(),
result_vector_components);
auto reg_vec_const =
context()->get_constant_mgr()->RegisterConstant(std::move(new_vec_const));
context()->get_constant_mgr()->RegisterConstant(new_vec_const);
if (reg_vec_const != new_vec_const) delete new_vec_const;
return context()->get_constant_mgr()->BuildInstructionAndAddToModule(
reg_vec_const, pos);
} else {