[msl-writer] Emit function constant information

This CL adds the code to emit function_constant attributes to the
generated MSL.

Bug: tint:153
Change-Id: I0e4f50257fde7e8db8f53e15bc9f460ebfc809ee
Reviewed-on: https://dawn-review.googlesource.com/c/tint/+/29520
Commit-Queue: Ryan Harrison <rharrison@chromium.org>
Reviewed-by: Ryan Harrison <rharrison@chromium.org>
This commit is contained in:
dan sinclair 2020-10-06 21:05:18 +00:00 committed by Commit Bot service account
parent 90ee6c4360
commit 56acec91b1
4 changed files with 33 additions and 2 deletions

View File

@ -41,6 +41,11 @@ DecoratedVariable* Variable::AsDecorated() {
return static_cast<DecoratedVariable*>(this); return static_cast<DecoratedVariable*>(this);
} }
const DecoratedVariable* Variable::AsDecorated() const {
assert(IsDecorated());
return static_cast<const DecoratedVariable*>(this);
}
bool Variable::IsDecorated() const { bool Variable::IsDecorated() const {
return false; return false;
} }

View File

@ -141,6 +141,8 @@ class Variable : public Node {
/// @returns the expression as a decorated variable /// @returns the expression as a decorated variable
DecoratedVariable* AsDecorated(); DecoratedVariable* AsDecorated();
/// @returns the expression as a decorated variable
const DecoratedVariable* AsDecorated() const;
/// @returns true if the name and path are both present /// @returns true if the name and path are both present
bool IsValid() const override; bool IsValid() const override;

View File

@ -1829,7 +1829,7 @@ bool GeneratorImpl::EmitVariable(ast::Variable* var, bool skip_constructor) {
bool GeneratorImpl::EmitProgramConstVariable(const ast::Variable* var) { bool GeneratorImpl::EmitProgramConstVariable(const ast::Variable* var) {
make_indent(); make_indent();
if (var->IsDecorated()) { if (var->IsDecorated() && !var->AsDecorated()->HasConstantIdDecoration()) {
error_ = "Decorated const values not valid"; error_ = "Decorated const values not valid";
return false; return false;
} }
@ -1846,7 +1846,10 @@ bool GeneratorImpl::EmitProgramConstVariable(const ast::Variable* var) {
out_ << " " << var->name(); out_ << " " << var->name();
} }
if (var->constructor() != nullptr) { if (var->IsDecorated() && var->AsDecorated()->HasConstantIdDecoration()) {
out_ << " [[function_constant(" << var->AsDecorated()->constant_id()
<< ")]]";
} else if (var->constructor() != nullptr) {
out_ << " = "; out_ << " = ";
if (!EmitExpression(var->constructor())) { if (!EmitExpression(var->constructor())) {
return false; return false;

View File

@ -16,6 +16,8 @@
#include <vector> #include <vector>
#include "gtest/gtest.h" #include "gtest/gtest.h"
#include "src/ast/constant_id_decoration.h"
#include "src/ast/decorated_variable.h"
#include "src/ast/float_literal.h" #include "src/ast/float_literal.h"
#include "src/ast/module.h" #include "src/ast/module.h"
#include "src/ast/scalar_constructor_expression.h" #include "src/ast/scalar_constructor_expression.h"
@ -58,6 +60,25 @@ TEST_F(MslGeneratorImplTest, Emit_ModuleConstant) {
"constant float pos[3] = {1.00000000f, 2.00000000f, 3.00000000f};\n"); "constant float pos[3] = {1.00000000f, 2.00000000f, 3.00000000f};\n");
} }
TEST_F(MslGeneratorImplTest, Emit_SpecConstant) {
ast::type::F32Type f32;
ast::VariableDecorationList decos;
decos.push_back(std::make_unique<ast::ConstantIdDecoration>(23));
auto var = std::make_unique<ast::DecoratedVariable>(
std::make_unique<ast::Variable>("pos", ast::StorageClass::kNone, &f32));
var->set_decorations(std::move(decos));
var->set_is_const(true);
var->set_constructor(std::make_unique<ast::ScalarConstructorExpression>(
std::make_unique<ast::FloatLiteral>(&f32, 3.0f)));
ast::Module m;
GeneratorImpl g(&m);
ASSERT_TRUE(g.EmitProgramConstVariable(var.get())) << g.error();
EXPECT_EQ(g.result(), "constant float pos [[function_constant(23)]];\n");
}
} // namespace } // namespace
} // namespace msl } // namespace msl
} // namespace writer } // namespace writer