src/transform: Replace std::make_unique<T> -> create<T>

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 <bclayton@google.com>
Reviewed-by: dan sinclair <dsinclair@chromium.org>
This commit is contained in:
Ben Clayton 2020-11-16 16:13:49 +00:00 committed by Commit Bot service account
parent f65799e7da
commit 0575449b82
3 changed files with 61 additions and 59 deletions

View File

@ -244,13 +244,13 @@ bool BoundArrayAccessorsTransform::ProcessAccessExpression(
cast_expr.push_back(expr->take_idx_expr()); cast_expr.push_back(expr->take_idx_expr());
ast::ExpressionList params; ast::ExpressionList params;
params.push_back(std::make_unique<ast::TypeConstructorExpression>( params.push_back(
u32, std::move(cast_expr))); create<ast::TypeConstructorExpression>(u32, std::move(cast_expr)));
params.push_back(std::make_unique<ast::ScalarConstructorExpression>( params.push_back(create<ast::ScalarConstructorExpression>(
std::make_unique<ast::UintLiteral>(u32, size - 1))); create<ast::UintLiteral>(u32, size - 1)));
auto call_expr = std::make_unique<ast::CallExpression>( auto call_expr = create<ast::CallExpression>(
std::make_unique<ast::IdentifierExpression>("min"), std::move(params)); create<ast::IdentifierExpression>("min"), std::move(params));
call_expr->set_result_type(u32); call_expr->set_result_type(u32);
expr->set_idx_expr(std::move(call_expr)); expr->set_idx_expr(std::move(call_expr));

View File

@ -15,7 +15,9 @@
#ifndef SRC_TRANSFORM_TRANSFORMER_H_ #ifndef SRC_TRANSFORM_TRANSFORMER_H_
#define SRC_TRANSFORM_TRANSFORMER_H_ #define SRC_TRANSFORM_TRANSFORMER_H_
#include <memory>
#include <string> #include <string>
#include <utility>
#include "src/ast/module.h" #include "src/ast/module.h"
#include "src/context.h" #include "src/context.h"
@ -39,6 +41,13 @@ class Transformer {
const std::string& error() { return error_; } const std::string& error() { return error_; }
protected: 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 <typename T, typename... ARGS>
std::unique_ptr<T> create(ARGS&&... args) const {
return std::make_unique<T>(std::forward<ARGS>(args)...);
}
/// The context /// The context
Context* ctx_ = nullptr; Context* ctx_ = nullptr;
/// The module /// The module

View File

@ -143,13 +143,12 @@ void VertexPullingTransform::FindOrInsertVertexIndexIfUsed() {
// We didn't find a vertex index builtin, so create one // We didn't find a vertex index builtin, so create one
vertex_index_name_ = kDefaultVertexIndexName; vertex_index_name_ = kDefaultVertexIndexName;
auto var = auto var = create<ast::DecoratedVariable>(create<ast::Variable>(
std::make_unique<ast::DecoratedVariable>(std::make_unique<ast::Variable>(
vertex_index_name_, ast::StorageClass::kInput, GetI32Type())); vertex_index_name_, ast::StorageClass::kInput, GetI32Type()));
ast::VariableDecorationList decorations; ast::VariableDecorationList decorations;
decorations.push_back(std::make_unique<ast::BuiltinDecoration>( decorations.push_back(
ast::Builtin::kVertexIdx, Source{})); create<ast::BuiltinDecoration>(ast::Builtin::kVertexIdx, Source{}));
var->set_decorations(std::move(decorations)); var->set_decorations(std::move(decorations));
mod_->AddGlobalVariable(std::move(var)); mod_->AddGlobalVariable(std::move(var));
@ -186,13 +185,12 @@ void VertexPullingTransform::FindOrInsertInstanceIndexIfUsed() {
// We didn't find an instance index builtin, so create one // We didn't find an instance index builtin, so create one
instance_index_name_ = kDefaultInstanceIndexName; instance_index_name_ = kDefaultInstanceIndexName;
auto var = auto var = create<ast::DecoratedVariable>(create<ast::Variable>(
std::make_unique<ast::DecoratedVariable>(std::make_unique<ast::Variable>(
instance_index_name_, ast::StorageClass::kInput, GetI32Type())); instance_index_name_, ast::StorageClass::kInput, GetI32Type()));
ast::VariableDecorationList decorations; ast::VariableDecorationList decorations;
decorations.push_back(std::make_unique<ast::BuiltinDecoration>( decorations.push_back(
ast::Builtin::kInstanceIdx, Source{})); create<ast::BuiltinDecoration>(ast::Builtin::kInstanceIdx, Source{}));
var->set_decorations(std::move(decorations)); var->set_decorations(std::move(decorations));
mod_->AddGlobalVariable(std::move(var)); mod_->AddGlobalVariable(std::move(var));
@ -213,8 +211,8 @@ void VertexPullingTransform::ConvertVertexInputVariablesToPrivate() {
// This is where the replacement happens. Expressions use identifier // This is where the replacement happens. Expressions use identifier
// strings instead of pointers, so we don't need to update any other place // strings instead of pointers, so we don't need to update any other place
// in the AST. // in the AST.
v = std::make_unique<ast::Variable>( v = create<ast::Variable>(v->name(), ast::StorageClass::kPrivate,
v->name(), ast::StorageClass::kPrivate, v->type()); v->type());
location_to_var_[location] = v.get(); location_to_var_[location] = v.get();
break; break;
} }
@ -226,7 +224,7 @@ void VertexPullingTransform::AddVertexStorageBuffers() {
// The array inside the struct definition // The array inside the struct definition
auto internal_array = std::make_unique<ast::type::ArrayType>(GetU32Type()); auto internal_array = std::make_unique<ast::type::ArrayType>(GetU32Type());
ast::ArrayDecorationList ary_decos; ast::ArrayDecorationList ary_decos;
ary_decos.push_back(std::make_unique<ast::StrideDecoration>(4u, Source{})); ary_decos.push_back(create<ast::StrideDecoration>(4u, Source{}));
internal_array->set_decorations(std::move(ary_decos)); internal_array->set_decorations(std::move(ary_decos));
auto* internal_array_type = ctx_->type_mgr().Get(std::move(internal_array)); auto* internal_array_type = ctx_->type_mgr().Get(std::move(internal_array));
@ -234,33 +232,29 @@ void VertexPullingTransform::AddVertexStorageBuffers() {
// Creating the struct type // Creating the struct type
ast::StructMemberList members; ast::StructMemberList members;
ast::StructMemberDecorationList member_dec; ast::StructMemberDecorationList member_dec;
member_dec.push_back( member_dec.push_back(create<ast::StructMemberOffsetDecoration>(0u, Source{}));
std::make_unique<ast::StructMemberOffsetDecoration>(0u, Source{}));
members.push_back(std::make_unique<ast::StructMember>( members.push_back(create<ast::StructMember>(
kStructBufferName, internal_array_type, std::move(member_dec))); kStructBufferName, internal_array_type, std::move(member_dec)));
ast::StructDecorationList decos; ast::StructDecorationList decos;
decos.push_back(std::make_unique<ast::StructBlockDecoration>(Source{})); decos.push_back(create<ast::StructBlockDecoration>(Source{}));
auto* struct_type = auto* struct_type =
ctx_->type_mgr().Get(std::make_unique<ast::type::StructType>( ctx_->type_mgr().Get(std::make_unique<ast::type::StructType>(
kStructName, kStructName,
std::make_unique<ast::Struct>(std::move(decos), std::move(members)))); create<ast::Struct>(std::move(decos), std::move(members))));
for (uint32_t i = 0; i < vertex_state_->vertex_buffers.size(); ++i) { for (uint32_t i = 0; i < vertex_state_->vertex_buffers.size(); ++i) {
// The decorated variable with struct type // The decorated variable with struct type
auto var = std::make_unique<ast::DecoratedVariable>( auto var = create<ast::DecoratedVariable>(
std::make_unique<ast::Variable>(GetVertexBufferName(i), create<ast::Variable>(GetVertexBufferName(i),
ast::StorageClass::kStorageBuffer, ast::StorageClass::kStorageBuffer, struct_type));
struct_type));
// Add decorations // Add decorations
ast::VariableDecorationList decorations; ast::VariableDecorationList decorations;
decorations.push_back( decorations.push_back(create<ast::BindingDecoration>(i, Source{}));
std::make_unique<ast::BindingDecoration>(i, Source{})); decorations.push_back(create<ast::SetDecoration>(pulling_set_, Source{}));
decorations.push_back(
std::make_unique<ast::SetDecoration>(pulling_set_, Source{}));
var->set_decorations(std::move(decorations)); var->set_decorations(std::move(decorations));
mod_->AddGlobalVariable(std::move(var)); mod_->AddGlobalVariable(std::move(var));
@ -274,11 +268,11 @@ void VertexPullingTransform::AddVertexPullingPreamble(
// location. // location.
// A block statement allowing us to use append instead of insert // A block statement allowing us to use append instead of insert
auto block = std::make_unique<ast::BlockStatement>(); auto block = create<ast::BlockStatement>();
// Declare the |kPullingPosVarName| variable in the shader // Declare the |kPullingPosVarName| variable in the shader
auto pos_declaration = std::make_unique<ast::VariableDeclStatement>( auto pos_declaration =
std::make_unique<ast::Variable>( create<ast::VariableDeclStatement>(create<ast::Variable>(
kPullingPosVarName, ast::StorageClass::kFunction, GetI32Type())); kPullingPosVarName, ast::StorageClass::kFunction, GetI32Type()));
// |kPullingPosVarName| refers to the byte location of the current read. We // |kPullingPosVarName| refers to the byte location of the current read. We
@ -299,26 +293,26 @@ void VertexPullingTransform::AddVertexPullingPreamble(
auto* v = it->second; auto* v = it->second;
// Identifier to index by // Identifier to index by
auto index_identifier = std::make_unique<ast::IdentifierExpression>( auto index_identifier = create<ast::IdentifierExpression>(
buffer_layout.step_mode == InputStepMode::kVertex buffer_layout.step_mode == InputStepMode::kVertex
? vertex_index_name_ ? vertex_index_name_
: instance_index_name_); : instance_index_name_);
// An expression for the start of the read in the buffer in bytes // An expression for the start of the read in the buffer in bytes
auto pos_value = std::make_unique<ast::BinaryExpression>( auto pos_value = create<ast::BinaryExpression>(
ast::BinaryOp::kAdd, ast::BinaryOp::kAdd,
std::make_unique<ast::BinaryExpression>( create<ast::BinaryExpression>(
ast::BinaryOp::kMultiply, std::move(index_identifier), ast::BinaryOp::kMultiply, std::move(index_identifier),
GenUint(static_cast<uint32_t>(buffer_layout.array_stride))), GenUint(static_cast<uint32_t>(buffer_layout.array_stride))),
GenUint(static_cast<uint32_t>(attribute_desc.offset))); GenUint(static_cast<uint32_t>(attribute_desc.offset)));
// Update position of the read // Update position of the read
auto set_pos_expr = std::make_unique<ast::AssignmentStatement>( auto set_pos_expr = create<ast::AssignmentStatement>(
CreatePullingPositionIdent(), std::move(pos_value)); CreatePullingPositionIdent(), std::move(pos_value));
block->append(std::move(set_pos_expr)); block->append(std::move(set_pos_expr));
block->append(std::make_unique<ast::AssignmentStatement>( block->append(create<ast::AssignmentStatement>(
std::make_unique<ast::IdentifierExpression>(v->name()), create<ast::IdentifierExpression>(v->name()),
AccessByFormat(i, attribute_desc.format))); AccessByFormat(i, attribute_desc.format)));
} }
} }
@ -328,13 +322,13 @@ void VertexPullingTransform::AddVertexPullingPreamble(
std::unique_ptr<ast::Expression> VertexPullingTransform::GenUint( std::unique_ptr<ast::Expression> VertexPullingTransform::GenUint(
uint32_t value) { uint32_t value) {
return std::make_unique<ast::ScalarConstructorExpression>( return create<ast::ScalarConstructorExpression>(
std::make_unique<ast::UintLiteral>(GetU32Type(), value)); create<ast::UintLiteral>(GetU32Type(), value));
} }
std::unique_ptr<ast::Expression> std::unique_ptr<ast::Expression>
VertexPullingTransform::CreatePullingPositionIdent() { VertexPullingTransform::CreatePullingPositionIdent() {
return std::make_unique<ast::IdentifierExpression>(kPullingPosVarName); return create<ast::IdentifierExpression>(kPullingPosVarName);
} }
std::unique_ptr<ast::Expression> VertexPullingTransform::AccessByFormat( std::unique_ptr<ast::Expression> VertexPullingTransform::AccessByFormat(
@ -373,29 +367,28 @@ std::unique_ptr<ast::Expression> VertexPullingTransform::AccessU32(
// by dividing. Then, that element is going to be read, and if needed, // 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 // unpacked into an appropriate variable. All reads should end up here as a
// base case. // base case.
return std::make_unique<ast::ArrayAccessorExpression>( return create<ast::ArrayAccessorExpression>(
std::make_unique<ast::MemberAccessorExpression>( create<ast::MemberAccessorExpression>(
std::make_unique<ast::IdentifierExpression>( create<ast::IdentifierExpression>(GetVertexBufferName(buffer)),
GetVertexBufferName(buffer)), create<ast::IdentifierExpression>(kStructBufferName)),
std::make_unique<ast::IdentifierExpression>(kStructBufferName)), create<ast::BinaryExpression>(ast::BinaryOp::kDivide, std::move(pos),
std::make_unique<ast::BinaryExpression>(ast::BinaryOp::kDivide, GenUint(4)));
std::move(pos), GenUint(4)));
} }
std::unique_ptr<ast::Expression> VertexPullingTransform::AccessI32( std::unique_ptr<ast::Expression> VertexPullingTransform::AccessI32(
uint32_t buffer, uint32_t buffer,
std::unique_ptr<ast::Expression> pos) { std::unique_ptr<ast::Expression> pos) {
// as<T> reinterprets bits // as<T> reinterprets bits
return std::make_unique<ast::BitcastExpression>( return create<ast::BitcastExpression>(GetI32Type(),
GetI32Type(), AccessU32(buffer, std::move(pos))); AccessU32(buffer, std::move(pos)));
} }
std::unique_ptr<ast::Expression> VertexPullingTransform::AccessF32( std::unique_ptr<ast::Expression> VertexPullingTransform::AccessF32(
uint32_t buffer, uint32_t buffer,
std::unique_ptr<ast::Expression> pos) { std::unique_ptr<ast::Expression> pos) {
// as<T> reinterprets bits // as<T> reinterprets bits
return std::make_unique<ast::BitcastExpression>( return create<ast::BitcastExpression>(GetF32Type(),
GetF32Type(), AccessU32(buffer, std::move(pos))); AccessU32(buffer, std::move(pos)));
} }
std::unique_ptr<ast::Expression> VertexPullingTransform::AccessPrimitive( std::unique_ptr<ast::Expression> VertexPullingTransform::AccessPrimitive(
@ -427,14 +420,14 @@ std::unique_ptr<ast::Expression> VertexPullingTransform::AccessVec(
ast::ExpressionList expr_list; ast::ExpressionList expr_list;
for (uint32_t i = 0; i < count; ++i) { for (uint32_t i = 0; i < count; ++i) {
// Offset read position by element_stride for each component // Offset read position by element_stride for each component
auto cur_pos = std::make_unique<ast::BinaryExpression>( auto cur_pos = create<ast::BinaryExpression>(ast::BinaryOp::kAdd,
ast::BinaryOp::kAdd, CreatePullingPositionIdent(), CreatePullingPositionIdent(),
GenUint(element_stride * i)); GenUint(element_stride * i));
expr_list.push_back( expr_list.push_back(
AccessPrimitive(buffer, std::move(cur_pos), base_format)); AccessPrimitive(buffer, std::move(cur_pos), base_format));
} }
return std::make_unique<ast::TypeConstructorExpression>( return create<ast::TypeConstructorExpression>(
ctx_->type_mgr().Get( ctx_->type_mgr().Get(
std::make_unique<ast::type::VectorType>(base_type, count)), std::make_unique<ast::type::VectorType>(base_type, count)),
std::move(expr_list)); std::move(expr_list));