[hlsl-writer] Add support for import statements.

This Cl adds support for imported methods to the HLSL backend.

Bug: tint:7
Change-Id: Ib906542915670dcc916d48d9e5d64d7032ba829a
Reviewed-on: https://dawn-review.googlesource.com/c/tint/+/26928
Commit-Queue: dan sinclair <dsinclair@chromium.org>
Reviewed-by: David Neto <dneto@google.com>
This commit is contained in:
dan sinclair 2020-08-19 19:19:04 +00:00 committed by Commit Bot service account
parent a1087ee513
commit d38df397e3
6 changed files with 511 additions and 3 deletions

View File

@ -1073,6 +1073,7 @@ source_set("tint_unittests_hlsl_writer_src") {
"src/writer/hlsl/generator_impl_function_test.cc",
"src/writer/hlsl/generator_impl_identifier_test.cc",
"src/writer/hlsl/generator_impl_if_test.cc",
"src/writer/hlsl/generator_impl_import_test.cc",
"src/writer/hlsl/generator_impl_intrinsic_test.cc",
"src/writer/hlsl/generator_impl_loop_test.cc",
"src/writer/hlsl/generator_impl_member_accessor_test.cc",

View File

@ -585,6 +585,7 @@ if (${TINT_BUILD_HLSL_WRITER})
writer/hlsl/generator_impl_identifier_test.cc
writer/hlsl/generator_impl_if_test.cc
writer/hlsl/generator_impl_intrinsic_test.cc
writer/hlsl/generator_impl_import_test.cc
writer/hlsl/generator_impl_loop_test.cc
writer/hlsl/generator_impl_member_accessor_test.cc
writer/hlsl/generator_impl_module_constant_test.cc

View File

@ -14,6 +14,7 @@
#include "src/writer/hlsl/generator_impl.h"
#include "spirv/unified1/GLSL.std.450.h"
#include "src/ast/array_accessor_expression.h"
#include "src/ast/as_expression.h"
#include "src/ast/assignment_statement.h"
@ -525,12 +526,128 @@ bool GeneratorImpl::EmitCall(ast::CallExpression* expr) {
out_ << ")";
} else {
error_ = "Imported functions not supported in HLSL backend.";
return false;
return EmitImportFunction(expr);
}
return true;
}
bool GeneratorImpl::EmitImportFunction(ast::CallExpression* expr) {
auto* ident = expr->func()->AsIdentifier();
auto* imp = module_->FindImportByName(ident->path());
if (imp == nullptr) {
error_ = "unable to find import for " + ident->path();
return 0;
}
auto id = imp->GetIdForMethod(ident->name());
if (id == 0) {
error_ = "unable to lookup: " + ident->name() + " in " + ident->path();
}
switch (id) {
case GLSLstd450Acos:
case GLSLstd450Asin:
case GLSLstd450Atan:
case GLSLstd450Atan2:
case GLSLstd450Ceil:
case GLSLstd450Cos:
case GLSLstd450Cosh:
case GLSLstd450Cross:
case GLSLstd450Degrees:
case GLSLstd450Determinant:
case GLSLstd450Distance:
case GLSLstd450Exp:
case GLSLstd450Exp2:
case GLSLstd450FaceForward:
case GLSLstd450Floor:
case GLSLstd450Fma:
case GLSLstd450Length:
case GLSLstd450Log:
case GLSLstd450Log2:
case GLSLstd450Normalize:
case GLSLstd450Pow:
case GLSLstd450Radians:
case GLSLstd450Reflect:
case GLSLstd450Round:
case GLSLstd450Sin:
case GLSLstd450Sinh:
case GLSLstd450SmoothStep:
case GLSLstd450Sqrt:
case GLSLstd450Step:
case GLSLstd450Tan:
case GLSLstd450Tanh:
case GLSLstd450Trunc:
out_ << ident->name();
break;
case GLSLstd450Fract:
out_ << "frac";
break;
case GLSLstd450InterpolateAtCentroid:
out_ << "EvaluateAttributeAtCentroid";
break;
case GLSLstd450InverseSqrt:
out_ << "rsqrt";
break;
case GLSLstd450FMix:
out_ << "mix";
break;
case GLSLstd450SSign:
case GLSLstd450FSign:
out_ << "sign";
break;
case GLSLstd450FAbs:
case GLSLstd450SAbs:
out_ << "abs";
break;
case GLSLstd450FMax:
case GLSLstd450NMax:
case GLSLstd450SMax:
case GLSLstd450UMax:
out_ << "max";
break;
case GLSLstd450FMin:
case GLSLstd450NMin:
case GLSLstd450SMin:
case GLSLstd450UMin:
out_ << "min";
break;
case GLSLstd450FClamp:
case GLSLstd450SClamp:
case GLSLstd450NClamp:
case GLSLstd450UClamp:
out_ << "clamp";
break;
// TODO(dsinclair): Determine mappings for the following
case GLSLstd450Atanh:
case GLSLstd450Asinh:
case GLSLstd450Acosh:
case GLSLstd450FindILsb:
case GLSLstd450FindUMsb:
case GLSLstd450FindSMsb:
case GLSLstd450MatrixInverse:
case GLSLstd450RoundEven:
error_ = "Unknown import method: " + ident->name();
return false;
}
out_ << "(";
bool first = true;
const auto& params = expr->params();
for (const auto& param : params) {
if (!first) {
out_ << ", ";
}
first = false;
if (!EmitExpression(param.get())) {
return false;
}
}
out_ << ")";
return true;
}
bool GeneratorImpl::EmitCast(ast::CastExpression* expr) {
if (!EmitType(expr->type(), "")) {
return false;

View File

@ -139,6 +139,10 @@ class GeneratorImpl : public TextGenerator {
/// @param stmt the statement to emit
/// @returns true if the statement was successfully emitted
bool EmitIf(ast::IfStatement* stmt);
/// Handles genreating an import expression
/// @param expr the expression
/// @returns true if the expression was successfully emitted.
bool EmitImportFunction(ast::CallExpression* expr);
/// Handles a literal
/// @param lit the literal to emit
/// @returns true if the literal was successfully emitted

View File

@ -0,0 +1,386 @@
// Copyright 2020 The Tint Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include <memory>
#include <string>
#include <vector>
#include "gtest/gtest.h"
#include "src/ast/call_expression.h"
#include "src/ast/float_literal.h"
#include "src/ast/identifier_expression.h"
#include "src/ast/module.h"
#include "src/ast/scalar_constructor_expression.h"
#include "src/ast/sint_literal.h"
#include "src/ast/type/f32_type.h"
#include "src/ast/type/i32_type.h"
#include "src/ast/type/matrix_type.h"
#include "src/ast/type/vector_type.h"
#include "src/ast/type_constructor_expression.h"
#include "src/context.h"
#include "src/type_determiner.h"
#include "src/writer/hlsl/generator_impl.h"
namespace tint {
namespace writer {
namespace hlsl {
namespace {
using HlslGeneratorImplTest = testing::Test;
struct HlslImportData {
const char* name;
const char* hlsl_name;
};
inline std::ostream& operator<<(std::ostream& out, HlslImportData data) {
out << data.name;
return out;
}
using HlslImportData_SingleParamTest = testing::TestWithParam<HlslImportData>;
TEST_P(HlslImportData_SingleParamTest, FloatScalar) {
auto param = GetParam();
ast::type::F32Type f32;
ast::ExpressionList params;
params.push_back(std::make_unique<ast::ScalarConstructorExpression>(
std::make_unique<ast::FloatLiteral>(&f32, 1.f)));
ast::CallExpression expr(std::make_unique<ast::IdentifierExpression>(
std::vector<std::string>{"std", param.name}),
std::move(params));
Context ctx;
ast::Module mod;
TypeDeterminer td(&ctx, &mod);
mod.AddImport(std::make_unique<ast::Import>("GLSL.std.450", "std"));
ASSERT_TRUE(td.DetermineResultType(&expr)) << td.error();
GeneratorImpl g(&mod);
ASSERT_TRUE(g.EmitImportFunction(&expr)) << g.error();
EXPECT_EQ(g.result(), std::string(param.hlsl_name) + "(1.00000000f)");
}
INSTANTIATE_TEST_SUITE_P(
HlslGeneratorImplTest,
HlslImportData_SingleParamTest,
testing::Values(HlslImportData{"acos", "acos"},
HlslImportData{"asin", "asin"},
HlslImportData{"atan", "atan"},
HlslImportData{"cos", "cos"},
HlslImportData{"cosh", "cosh"},
HlslImportData{"ceil", "ceil"},
HlslImportData{"degrees", "degrees"},
HlslImportData{"exp", "exp"},
HlslImportData{"exp2", "exp2"},
HlslImportData{"fabs", "abs"},
HlslImportData{"floor", "floor"},
HlslImportData{"fract", "frac"},
HlslImportData{"interpolateatcentroid",
"EvaluateAttributeAtCentroid"},
HlslImportData{"inversesqrt", "rsqrt"},
HlslImportData{"length", "length"},
HlslImportData{"log", "log"},
HlslImportData{"log2", "log2"},
HlslImportData{"normalize", "normalize"},
HlslImportData{"radians", "radians"},
HlslImportData{"round", "round"},
HlslImportData{"fsign", "sign"},
HlslImportData{"sin", "sin"},
HlslImportData{"sinh", "sinh"},
HlslImportData{"sqrt", "sqrt"},
HlslImportData{"tan", "tan"},
HlslImportData{"tanh", "tanh"},
HlslImportData{"trunc", "trunc"}));
TEST_F(HlslGeneratorImplTest, DISABLED_HlslImportData_Acosh) {
FAIL();
}
TEST_F(HlslGeneratorImplTest, DISABLED_HlslImportData_ASinh) {
FAIL();
}
TEST_F(HlslGeneratorImplTest, DISABLED_HlslImportData_ATanh) {
FAIL();
}
using HlslImportData_SingleIntParamTest =
testing::TestWithParam<HlslImportData>;
TEST_P(HlslImportData_SingleIntParamTest, IntScalar) {
auto param = GetParam();
ast::type::I32Type i32;
ast::ExpressionList params;
params.push_back(std::make_unique<ast::ScalarConstructorExpression>(
std::make_unique<ast::SintLiteral>(&i32, 1)));
ast::CallExpression expr(std::make_unique<ast::IdentifierExpression>(
std::vector<std::string>{"std", param.name}),
std::move(params));
Context ctx;
ast::Module mod;
TypeDeterminer td(&ctx, &mod);
mod.AddImport(std::make_unique<ast::Import>("GLSL.std.450", "std"));
ASSERT_TRUE(td.DetermineResultType(&expr)) << td.error();
GeneratorImpl g(&mod);
ASSERT_TRUE(g.EmitImportFunction(&expr)) << g.error();
EXPECT_EQ(g.result(), std::string(param.hlsl_name) + "(1)");
}
INSTANTIATE_TEST_SUITE_P(HlslGeneratorImplTest,
HlslImportData_SingleIntParamTest,
testing::Values(HlslImportData{"sabs", "abs"},
HlslImportData{"ssign", "sign"}));
using HlslImportData_DualParamTest = testing::TestWithParam<HlslImportData>;
TEST_P(HlslImportData_DualParamTest, FloatScalar) {
auto param = GetParam();
ast::type::F32Type f32;
ast::ExpressionList params;
params.push_back(std::make_unique<ast::ScalarConstructorExpression>(
std::make_unique<ast::FloatLiteral>(&f32, 1.f)));
params.push_back(std::make_unique<ast::ScalarConstructorExpression>(
std::make_unique<ast::FloatLiteral>(&f32, 2.f)));
ast::CallExpression expr(std::make_unique<ast::IdentifierExpression>(
std::vector<std::string>{"std", param.name}),
std::move(params));
Context ctx;
ast::Module mod;
TypeDeterminer td(&ctx, &mod);
mod.AddImport(std::make_unique<ast::Import>("GLSL.std.450", "std"));
ASSERT_TRUE(td.DetermineResultType(&expr)) << td.error();
GeneratorImpl g(&mod);
ASSERT_TRUE(g.EmitImportFunction(&expr)) << g.error();
EXPECT_EQ(g.result(),
std::string(param.hlsl_name) + "(1.00000000f, 2.00000000f)");
}
INSTANTIATE_TEST_SUITE_P(HlslGeneratorImplTest,
HlslImportData_DualParamTest,
testing::Values(HlslImportData{"atan2", "atan2"},
HlslImportData{"distance", "distance"},
HlslImportData{"fmax", "max"},
HlslImportData{"fmin", "min"},
HlslImportData{"nmax", "max"},
HlslImportData{"nmin", "min"},
HlslImportData{"pow", "pow"},
HlslImportData{"reflect", "reflect"},
HlslImportData{"step", "step"}));
using HlslImportData_DualParam_VectorTest =
testing::TestWithParam<HlslImportData>;
TEST_P(HlslImportData_DualParam_VectorTest, FloatVector) {
auto param = GetParam();
ast::type::F32Type f32;
ast::type::VectorType vec(&f32, 3);
ast::ExpressionList type_params;
type_params.push_back(std::make_unique<ast::ScalarConstructorExpression>(
std::make_unique<ast::FloatLiteral>(&f32, 1.f)));
type_params.push_back(std::make_unique<ast::ScalarConstructorExpression>(
std::make_unique<ast::FloatLiteral>(&f32, 2.f)));
type_params.push_back(std::make_unique<ast::ScalarConstructorExpression>(
std::make_unique<ast::FloatLiteral>(&f32, 3.f)));
ast::ExpressionList params;
params.push_back(std::make_unique<ast::TypeConstructorExpression>(
&vec, std::move(type_params)));
type_params.push_back(std::make_unique<ast::ScalarConstructorExpression>(
std::make_unique<ast::FloatLiteral>(&f32, 4.f)));
type_params.push_back(std::make_unique<ast::ScalarConstructorExpression>(
std::make_unique<ast::FloatLiteral>(&f32, 5.f)));
type_params.push_back(std::make_unique<ast::ScalarConstructorExpression>(
std::make_unique<ast::FloatLiteral>(&f32, 6.f)));
params.push_back(std::make_unique<ast::TypeConstructorExpression>(
&vec, std::move(type_params)));
ast::CallExpression expr(std::make_unique<ast::IdentifierExpression>(
std::vector<std::string>{"std", param.name}),
std::move(params));
Context ctx;
ast::Module mod;
TypeDeterminer td(&ctx, &mod);
mod.AddImport(std::make_unique<ast::Import>("GLSL.std.450", "std"));
ASSERT_TRUE(td.DetermineResultType(&expr)) << td.error();
GeneratorImpl g(&mod);
ASSERT_TRUE(g.EmitImportFunction(&expr)) << g.error();
EXPECT_EQ(g.result(),
std::string(param.hlsl_name) +
"(vector<float, 3>(1.00000000f, 2.00000000f, 3.00000000f), "
"vector<float, 3>(4.00000000f, 5.00000000f, 6.00000000f))");
}
INSTANTIATE_TEST_SUITE_P(HlslGeneratorImplTest,
HlslImportData_DualParam_VectorTest,
testing::Values(HlslImportData{"cross", "cross"}));
using HlslImportData_DualParam_Int_Test =
testing::TestWithParam<HlslImportData>;
TEST_P(HlslImportData_DualParam_Int_Test, IntScalar) {
auto param = GetParam();
ast::type::I32Type i32;
ast::ExpressionList params;
params.push_back(std::make_unique<ast::ScalarConstructorExpression>(
std::make_unique<ast::SintLiteral>(&i32, 1)));
params.push_back(std::make_unique<ast::ScalarConstructorExpression>(
std::make_unique<ast::SintLiteral>(&i32, 2)));
ast::CallExpression expr(std::make_unique<ast::IdentifierExpression>(
std::vector<std::string>{"std", param.name}),
std::move(params));
Context ctx;
ast::Module mod;
TypeDeterminer td(&ctx, &mod);
mod.AddImport(std::make_unique<ast::Import>("GLSL.std.450", "std"));
ASSERT_TRUE(td.DetermineResultType(&expr)) << td.error();
GeneratorImpl g(&mod);
ASSERT_TRUE(g.EmitImportFunction(&expr)) << g.error();
EXPECT_EQ(g.result(), std::string(param.hlsl_name) + "(1, 2)");
}
INSTANTIATE_TEST_SUITE_P(HlslGeneratorImplTest,
HlslImportData_DualParam_Int_Test,
testing::Values(HlslImportData{"smax", "max"},
HlslImportData{"smin", "min"},
HlslImportData{"umax", "max"},
HlslImportData{"umin", "min"}));
using HlslImportData_TripleParamTest = testing::TestWithParam<HlslImportData>;
TEST_P(HlslImportData_TripleParamTest, FloatScalar) {
auto param = GetParam();
ast::type::F32Type f32;
ast::ExpressionList params;
params.push_back(std::make_unique<ast::ScalarConstructorExpression>(
std::make_unique<ast::FloatLiteral>(&f32, 1.f)));
params.push_back(std::make_unique<ast::ScalarConstructorExpression>(
std::make_unique<ast::FloatLiteral>(&f32, 2.f)));
params.push_back(std::make_unique<ast::ScalarConstructorExpression>(
std::make_unique<ast::FloatLiteral>(&f32, 3.f)));
ast::CallExpression expr(std::make_unique<ast::IdentifierExpression>(
std::vector<std::string>{"std", param.name}),
std::move(params));
Context ctx;
ast::Module mod;
TypeDeterminer td(&ctx, &mod);
mod.AddImport(std::make_unique<ast::Import>("GLSL.std.450", "std"));
ASSERT_TRUE(td.DetermineResultType(&expr)) << td.error();
GeneratorImpl g(&mod);
ASSERT_TRUE(g.EmitImportFunction(&expr)) << g.error();
EXPECT_EQ(g.result(), std::string(param.hlsl_name) +
"(1.00000000f, 2.00000000f, 3.00000000f)");
}
INSTANTIATE_TEST_SUITE_P(
HlslGeneratorImplTest,
HlslImportData_TripleParamTest,
testing::Values(HlslImportData{"faceforward", "faceforward"},
HlslImportData{"fma", "fma"},
HlslImportData{"fclamp", "clamp"},
HlslImportData{"nclamp", "clamp"},
HlslImportData{"smoothstep", "smoothstep"}));
TEST_F(HlslGeneratorImplTest, DISABLED_HlslImportData_FMix) {
FAIL();
}
using HlslImportData_TripleParam_Int_Test =
testing::TestWithParam<HlslImportData>;
TEST_P(HlslImportData_TripleParam_Int_Test, IntScalar) {
auto param = GetParam();
ast::type::I32Type i32;
ast::ExpressionList params;
params.push_back(std::make_unique<ast::ScalarConstructorExpression>(
std::make_unique<ast::SintLiteral>(&i32, 1)));
params.push_back(std::make_unique<ast::ScalarConstructorExpression>(
std::make_unique<ast::SintLiteral>(&i32, 2)));
params.push_back(std::make_unique<ast::ScalarConstructorExpression>(
std::make_unique<ast::SintLiteral>(&i32, 3)));
ast::CallExpression expr(std::make_unique<ast::IdentifierExpression>(
std::vector<std::string>{"std", param.name}),
std::move(params));
Context ctx;
ast::Module mod;
TypeDeterminer td(&ctx, &mod);
mod.AddImport(std::make_unique<ast::Import>("GLSL.std.450", "std"));
ASSERT_TRUE(td.DetermineResultType(&expr)) << td.error();
GeneratorImpl g(&mod);
ASSERT_TRUE(g.EmitImportFunction(&expr)) << g.error();
EXPECT_EQ(g.result(), std::string(param.hlsl_name) + "(1, 2, 3)");
}
INSTANTIATE_TEST_SUITE_P(HlslGeneratorImplTest,
HlslImportData_TripleParam_Int_Test,
testing::Values(HlslImportData{"sclamp", "clamp"},
HlslImportData{"uclamp", "clamp"}));
TEST_F(HlslGeneratorImplTest, HlslImportData_Determinant) {
ast::type::F32Type f32;
ast::type::MatrixType mat(&f32, 3, 3);
auto var = std::make_unique<ast::Variable>(
"var", ast::StorageClass::kFunction, &mat);
ast::ExpressionList params;
params.push_back(std::make_unique<ast::IdentifierExpression>("var"));
ast::CallExpression expr(std::make_unique<ast::IdentifierExpression>(
std::vector<std::string>{"std", "determinant"}),
std::move(params));
Context ctx;
ast::Module mod;
mod.AddGlobalVariable(std::move(var));
mod.AddImport(std::make_unique<ast::Import>("GLSL.std.450", "std"));
TypeDeterminer td(&ctx, &mod);
// Register the global
ASSERT_TRUE(td.Determine()) << td.error();
ASSERT_TRUE(td.DetermineResultType(&expr)) << td.error();
GeneratorImpl g(&mod);
ASSERT_TRUE(g.EmitImportFunction(&expr)) << g.error();
EXPECT_EQ(g.result(), std::string("determinant(var)"));
}
} // namespace
} // namespace hlsl
} // namespace writer
} // namespace tint

View File

@ -706,7 +706,6 @@ bool GeneratorImpl::EmitImportFunction(ast::CallExpression* expr) {
case GLSLstd450Radians:
case GLSLstd450RoundEven:
case GLSLstd450SSign:
default:
error_ = "Unknown import method: " + ident->name();
return false;
}