From 47ee776a2c5612591f6d1edbef2c264d92f5f05c Mon Sep 17 00:00:00 2001 From: Steven Perron Date: Fri, 24 Aug 2018 15:12:49 -0400 Subject: [PATCH] Revert "Have the constant manager take ownership of constants." This reverts commit b938b74bacee59cf065d4f36bf1a02585ca7ea47. --- source/opt/constants.cpp | 20 +++++++++---------- source/opt/constants.h | 14 +++---------- ...ld_spec_constant_op_and_composite_pass.cpp | 12 ++++++----- 3 files changed, 20 insertions(+), 26 deletions(-) diff --git a/source/opt/constants.cpp b/source/opt/constants.cpp index ecb5f97c4..b1e4bee40 100644 --- a/source/opt/constants.cpp +++ b/source/opt/constants.cpp @@ -196,19 +196,19 @@ Instruction* ConstantManager::GetDefiningInstruction( } } -std::unique_ptr ConstantManager::CreateConstant( +const Constant* ConstantManager::CreateConstant( const Type* type, const std::vector& literal_words_or_ids) const { if (literal_words_or_ids.size() == 0) { // Constant declared with OpConstantNull - return MakeUnique(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(bt, literal_words_or_ids.front()); + return new BoolConstant(bt, literal_words_or_ids.front()); } else if (auto* it = type->AsInteger()) { - return MakeUnique(it, literal_words_or_ids); + return new IntConstant(it, literal_words_or_ids); } else if (auto* ft = type->AsFloat()) { - return MakeUnique(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 ConstantManager::CreateConstant( return false; })) return nullptr; - return MakeUnique(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(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(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(at, components); + return new ArrayConstant(at, components); } else { return nullptr; } @@ -344,7 +344,7 @@ std::unique_ptr ConstantManager::CreateCompositeInstruction( const Constant* ConstantManager::GetConstant( const Type* type, const std::vector& 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 Constant::GetVectorComponents( diff --git a/source/opt/constants.h b/source/opt/constants.h index 643a54244..2833b845b 100644 --- a/source/opt/constants.h +++ b/source/opt/constants.h @@ -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 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 GetConstantsFromIds( const std::vector& 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 CreateConstant( + const Constant* CreateConstant( const Type* type, const std::vector& literal_words_or_ids) const; @@ -684,10 +680,6 @@ class ConstantManager { // The constant pool. All created constants are registered here. std::unordered_set 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> owned_constants_; }; } // namespace analysis diff --git a/source/opt/fold_spec_constant_op_and_composite_pass.cpp b/source/opt/fold_spec_constant_op_and_composite_pass.cpp index 40bd1c8ca..d43053e77 100644 --- a/source/opt/fold_spec_constant_op_and_composite_pass.cpp +++ b/source/opt/fold_spec_constant_op_and_composite_pass.cpp @@ -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( - 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(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 {