Add unary derivative type determination.

This Cl adds the type determination for unary derivative expressions.

Bug: tint:5
Change-Id: I44ad22733ce6b94e4b6d77b432024bacc110e8af
Reviewed-on: https://dawn-review.googlesource.com/c/tint/+/18848
Reviewed-by: David Neto <dneto@google.com>
This commit is contained in:
dan sinclair 2020-04-07 19:26:49 +00:00
parent 9b97802d99
commit b173056fca
3 changed files with 53 additions and 0 deletions

View File

@ -40,6 +40,7 @@
#include "src/ast/type/struct_type.h"
#include "src/ast/type/vector_type.h"
#include "src/ast/type_constructor_expression.h"
#include "src/ast/unary_derivative_expression.h"
#include "src/ast/unless_statement.h"
#include "src/ast/variable_decl_statement.h"
@ -211,6 +212,9 @@ bool TypeDeterminer::DetermineResultType(ast::Expression* expr) {
if (expr->IsRelational()) {
return DetermineRelational(expr->AsRelational());
}
if (expr->IsUnaryDerivative()) {
return DetermineUnaryDerivative(expr->AsUnaryDerivative());
}
error_ = "unknown expression for type determination";
return false;
@ -401,4 +405,14 @@ bool TypeDeterminer::DetermineRelational(ast::RelationalExpression* expr) {
return false;
}
bool TypeDeterminer::DetermineUnaryDerivative(
ast::UnaryDerivativeExpression* expr) {
// The result type must be the same as the type of the parameter.
if (!DetermineResultType(expr->param())) {
return false;
}
expr->set_result_type(expr->param()->result_type());
return true;
}
} // namespace tint

View File

@ -34,6 +34,7 @@ class Function;
class IdentifierExpression;
class MemberAccessorExpression;
class RelationalExpression;
class UnaryDerivativeExpression;
class Variable;
} // namespace ast
@ -83,6 +84,7 @@ class TypeDeterminer {
bool DetermineIdentifier(ast::IdentifierExpression* expr);
bool DetermineMemberAccessor(ast::MemberAccessorExpression* expr);
bool DetermineRelational(ast::RelationalExpression* expr);
bool DetermineUnaryDerivative(ast::UnaryDerivativeExpression* expr);
Context& ctx_;
std::string error_;

View File

@ -49,6 +49,7 @@
#include "src/ast/type/struct_type.h"
#include "src/ast/type/vector_type.h"
#include "src/ast/type_constructor_expression.h"
#include "src/ast/unary_derivative_expression.h"
#include "src/ast/unless_statement.h"
#include "src/ast/variable_decl_statement.h"
@ -1231,5 +1232,41 @@ TEST_F(TypeDeterminerTest, Expr_Relational_Multiply_Matrix_Matrix) {
EXPECT_EQ(mat->columns(), 4);
}
using UnaryDerivativeExpressionTest =
testing::TestWithParam<ast::UnaryDerivative>;
TEST_P(UnaryDerivativeExpressionTest, Expr_UnaryDerivative) {
auto derivative = GetParam();
ast::type::F32Type f32;
ast::type::VectorType vec4(&f32, 4);
auto var =
std::make_unique<ast::Variable>("ident", ast::StorageClass::kNone, &vec4);
ast::Module m;
m.AddGlobalVariable(std::move(var));
Context ctx;
TypeDeterminer td(&ctx);
// Register the global
EXPECT_TRUE(td.Determine(&m));
ast::UnaryDerivativeExpression der(
derivative, ast::DerivativeModifier::kNone,
std::make_unique<ast::IdentifierExpression>("ident"));
EXPECT_TRUE(td.DetermineResultType(&der));
ASSERT_NE(der.result_type(), nullptr);
ASSERT_TRUE(der.result_type()->IsVector());
EXPECT_TRUE(der.result_type()->AsVector()->type()->IsF32());
EXPECT_EQ(der.result_type()->AsVector()->size(), 4);
}
INSTANTIATE_TEST_SUITE_P(TypeDeterminerTest,
UnaryDerivativeExpressionTest,
testing::Values(ast::UnaryDerivative::kDpdx,
ast::UnaryDerivative::kDpdy,
ast::UnaryDerivative::kFwidth));
} // namespace
} // namespace tint