From 0575449b82c02bf5ca7e555768a5193afcdc8780 Mon Sep 17 00:00:00 2001 From: Ben Clayton Date: Mon, 16 Nov 2020 16:13:49 +0000 Subject: [PATCH] src/transform: Replace std::make_unique -> create create() is currently just a simple forwarder to std::make_unique<>, but will be later replaced with a function that returns a raw pointer, and owned by the context. Bug: tint:322 Change-Id: I4d0c3a6b471c559617538bda90a5a991c71045a9 Reviewed-on: https://dawn-review.googlesource.com/c/tint/+/32862 Commit-Queue: Ben Clayton Reviewed-by: dan sinclair --- .../bound_array_accessors_transform.cc | 12 +-- src/transform/transformer.h | 9 ++ src/transform/vertex_pulling_transform.cc | 99 +++++++++---------- 3 files changed, 61 insertions(+), 59 deletions(-) diff --git a/src/transform/bound_array_accessors_transform.cc b/src/transform/bound_array_accessors_transform.cc index 78f139e232..2f1e32ab28 100644 --- a/src/transform/bound_array_accessors_transform.cc +++ b/src/transform/bound_array_accessors_transform.cc @@ -244,13 +244,13 @@ bool BoundArrayAccessorsTransform::ProcessAccessExpression( cast_expr.push_back(expr->take_idx_expr()); ast::ExpressionList params; - params.push_back(std::make_unique( - u32, std::move(cast_expr))); - params.push_back(std::make_unique( - std::make_unique(u32, size - 1))); + params.push_back( + create(u32, std::move(cast_expr))); + params.push_back(create( + create(u32, size - 1))); - auto call_expr = std::make_unique( - std::make_unique("min"), std::move(params)); + auto call_expr = create( + create("min"), std::move(params)); call_expr->set_result_type(u32); expr->set_idx_expr(std::move(call_expr)); diff --git a/src/transform/transformer.h b/src/transform/transformer.h index 004133edd7..417e73240e 100644 --- a/src/transform/transformer.h +++ b/src/transform/transformer.h @@ -15,7 +15,9 @@ #ifndef SRC_TRANSFORM_TRANSFORMER_H_ #define SRC_TRANSFORM_TRANSFORMER_H_ +#include #include +#include #include "src/ast/module.h" #include "src/context.h" @@ -39,6 +41,13 @@ class Transformer { const std::string& error() { return error_; } protected: + /// @return a `std::unique_ptr` to a new `T` constructed with `args` + /// @param args the arguments to forward to the constructor for `T` + template + std::unique_ptr create(ARGS&&... args) const { + return std::make_unique(std::forward(args)...); + } + /// The context Context* ctx_ = nullptr; /// The module diff --git a/src/transform/vertex_pulling_transform.cc b/src/transform/vertex_pulling_transform.cc index addfee55f2..4b38195120 100644 --- a/src/transform/vertex_pulling_transform.cc +++ b/src/transform/vertex_pulling_transform.cc @@ -143,13 +143,12 @@ void VertexPullingTransform::FindOrInsertVertexIndexIfUsed() { // We didn't find a vertex index builtin, so create one vertex_index_name_ = kDefaultVertexIndexName; - auto var = - std::make_unique(std::make_unique( - vertex_index_name_, ast::StorageClass::kInput, GetI32Type())); + auto var = create(create( + vertex_index_name_, ast::StorageClass::kInput, GetI32Type())); ast::VariableDecorationList decorations; - decorations.push_back(std::make_unique( - ast::Builtin::kVertexIdx, Source{})); + decorations.push_back( + create(ast::Builtin::kVertexIdx, Source{})); var->set_decorations(std::move(decorations)); mod_->AddGlobalVariable(std::move(var)); @@ -186,13 +185,12 @@ void VertexPullingTransform::FindOrInsertInstanceIndexIfUsed() { // We didn't find an instance index builtin, so create one instance_index_name_ = kDefaultInstanceIndexName; - auto var = - std::make_unique(std::make_unique( - instance_index_name_, ast::StorageClass::kInput, GetI32Type())); + auto var = create(create( + instance_index_name_, ast::StorageClass::kInput, GetI32Type())); ast::VariableDecorationList decorations; - decorations.push_back(std::make_unique( - ast::Builtin::kInstanceIdx, Source{})); + decorations.push_back( + create(ast::Builtin::kInstanceIdx, Source{})); var->set_decorations(std::move(decorations)); mod_->AddGlobalVariable(std::move(var)); @@ -213,8 +211,8 @@ void VertexPullingTransform::ConvertVertexInputVariablesToPrivate() { // This is where the replacement happens. Expressions use identifier // strings instead of pointers, so we don't need to update any other place // in the AST. - v = std::make_unique( - v->name(), ast::StorageClass::kPrivate, v->type()); + v = create(v->name(), ast::StorageClass::kPrivate, + v->type()); location_to_var_[location] = v.get(); break; } @@ -226,7 +224,7 @@ void VertexPullingTransform::AddVertexStorageBuffers() { // The array inside the struct definition auto internal_array = std::make_unique(GetU32Type()); ast::ArrayDecorationList ary_decos; - ary_decos.push_back(std::make_unique(4u, Source{})); + ary_decos.push_back(create(4u, Source{})); internal_array->set_decorations(std::move(ary_decos)); auto* internal_array_type = ctx_->type_mgr().Get(std::move(internal_array)); @@ -234,33 +232,29 @@ void VertexPullingTransform::AddVertexStorageBuffers() { // Creating the struct type ast::StructMemberList members; ast::StructMemberDecorationList member_dec; - member_dec.push_back( - std::make_unique(0u, Source{})); + member_dec.push_back(create(0u, Source{})); - members.push_back(std::make_unique( + members.push_back(create( kStructBufferName, internal_array_type, std::move(member_dec))); ast::StructDecorationList decos; - decos.push_back(std::make_unique(Source{})); + decos.push_back(create(Source{})); auto* struct_type = ctx_->type_mgr().Get(std::make_unique( kStructName, - std::make_unique(std::move(decos), std::move(members)))); + create(std::move(decos), std::move(members)))); for (uint32_t i = 0; i < vertex_state_->vertex_buffers.size(); ++i) { // The decorated variable with struct type - auto var = std::make_unique( - std::make_unique(GetVertexBufferName(i), - ast::StorageClass::kStorageBuffer, - struct_type)); + auto var = create( + create(GetVertexBufferName(i), + ast::StorageClass::kStorageBuffer, struct_type)); // Add decorations ast::VariableDecorationList decorations; - decorations.push_back( - std::make_unique(i, Source{})); - decorations.push_back( - std::make_unique(pulling_set_, Source{})); + decorations.push_back(create(i, Source{})); + decorations.push_back(create(pulling_set_, Source{})); var->set_decorations(std::move(decorations)); mod_->AddGlobalVariable(std::move(var)); @@ -274,11 +268,11 @@ void VertexPullingTransform::AddVertexPullingPreamble( // location. // A block statement allowing us to use append instead of insert - auto block = std::make_unique(); + auto block = create(); // Declare the |kPullingPosVarName| variable in the shader - auto pos_declaration = std::make_unique( - std::make_unique( + auto pos_declaration = + create(create( kPullingPosVarName, ast::StorageClass::kFunction, GetI32Type())); // |kPullingPosVarName| refers to the byte location of the current read. We @@ -299,26 +293,26 @@ void VertexPullingTransform::AddVertexPullingPreamble( auto* v = it->second; // Identifier to index by - auto index_identifier = std::make_unique( + auto index_identifier = create( buffer_layout.step_mode == InputStepMode::kVertex ? vertex_index_name_ : instance_index_name_); // An expression for the start of the read in the buffer in bytes - auto pos_value = std::make_unique( + auto pos_value = create( ast::BinaryOp::kAdd, - std::make_unique( + create( ast::BinaryOp::kMultiply, std::move(index_identifier), GenUint(static_cast(buffer_layout.array_stride))), GenUint(static_cast(attribute_desc.offset))); // Update position of the read - auto set_pos_expr = std::make_unique( + auto set_pos_expr = create( CreatePullingPositionIdent(), std::move(pos_value)); block->append(std::move(set_pos_expr)); - block->append(std::make_unique( - std::make_unique(v->name()), + block->append(create( + create(v->name()), AccessByFormat(i, attribute_desc.format))); } } @@ -328,13 +322,13 @@ void VertexPullingTransform::AddVertexPullingPreamble( std::unique_ptr VertexPullingTransform::GenUint( uint32_t value) { - return std::make_unique( - std::make_unique(GetU32Type(), value)); + return create( + create(GetU32Type(), value)); } std::unique_ptr VertexPullingTransform::CreatePullingPositionIdent() { - return std::make_unique(kPullingPosVarName); + return create(kPullingPosVarName); } std::unique_ptr VertexPullingTransform::AccessByFormat( @@ -373,29 +367,28 @@ std::unique_ptr VertexPullingTransform::AccessU32( // by dividing. Then, that element is going to be read, and if needed, // unpacked into an appropriate variable. All reads should end up here as a // base case. - return std::make_unique( - std::make_unique( - std::make_unique( - GetVertexBufferName(buffer)), - std::make_unique(kStructBufferName)), - std::make_unique(ast::BinaryOp::kDivide, - std::move(pos), GenUint(4))); + return create( + create( + create(GetVertexBufferName(buffer)), + create(kStructBufferName)), + create(ast::BinaryOp::kDivide, std::move(pos), + GenUint(4))); } std::unique_ptr VertexPullingTransform::AccessI32( uint32_t buffer, std::unique_ptr pos) { // as reinterprets bits - return std::make_unique( - GetI32Type(), AccessU32(buffer, std::move(pos))); + return create(GetI32Type(), + AccessU32(buffer, std::move(pos))); } std::unique_ptr VertexPullingTransform::AccessF32( uint32_t buffer, std::unique_ptr pos) { // as reinterprets bits - return std::make_unique( - GetF32Type(), AccessU32(buffer, std::move(pos))); + return create(GetF32Type(), + AccessU32(buffer, std::move(pos))); } std::unique_ptr VertexPullingTransform::AccessPrimitive( @@ -427,14 +420,14 @@ std::unique_ptr VertexPullingTransform::AccessVec( ast::ExpressionList expr_list; for (uint32_t i = 0; i < count; ++i) { // Offset read position by element_stride for each component - auto cur_pos = std::make_unique( - ast::BinaryOp::kAdd, CreatePullingPositionIdent(), - GenUint(element_stride * i)); + auto cur_pos = create(ast::BinaryOp::kAdd, + CreatePullingPositionIdent(), + GenUint(element_stride * i)); expr_list.push_back( AccessPrimitive(buffer, std::move(cur_pos), base_format)); } - return std::make_unique( + return create( ctx_->type_mgr().Get( std::make_unique(base_type, count)), std::move(expr_list));