writer/msl: Remove the renaming logic

This is now entirely handled as transforms.

Bug: tint:273
Change-Id: Ib3c0db7b5ecf024b6ae2aed7788e4b582d07c4ce
Reviewed-on: https://dawn-review.googlesource.com/c/tint/+/43983
Commit-Queue: Ben Clayton <bclayton@google.com>
Reviewed-by: James Price <jrprice@google.com>
This commit is contained in:
Ben Clayton 2021-03-04 23:24:05 +00:00 committed by Commit Bot service account
parent 37d2b35d59
commit 97b92178c5
13 changed files with 140 additions and 987 deletions

View File

@ -621,8 +621,6 @@ source_set("libtint_msl_writer_src") {
"src/writer/msl/generator.h",
"src/writer/msl/generator_impl.cc",
"src/writer/msl/generator_impl.h",
"src/writer/msl/namer.cc",
"src/writer/msl/namer.h",
]
configs += [ ":tint_common_config" ]
@ -1211,7 +1209,6 @@ source_set("tint_unittests_msl_writer_src") {
"src/writer/msl/generator_impl_type_test.cc",
"src/writer/msl/generator_impl_unary_op_test.cc",
"src/writer/msl/generator_impl_variable_decl_statement_test.cc",
"src/writer/msl/namer_test.cc",
"src/writer/msl/test_helper.h",
]

View File

@ -367,8 +367,6 @@ if(${TINT_BUILD_MSL_WRITER})
writer/msl/generator.h
writer/msl/generator_impl.cc
writer/msl/generator_impl.h
writer/msl/namer.cc
writer/msl/namer.h
)
endif()
@ -749,7 +747,6 @@ if(${TINT_BUILD_TESTS})
writer/msl/generator_impl_type_test.cc
writer/msl/generator_impl_unary_op_test.cc
writer/msl/generator_impl_variable_decl_statement_test.cc
writer/msl/namer_test.cc
writer/msl/test_helper.h
)
endif()

View File

@ -82,8 +82,8 @@ namespace {
const char kInStructNameSuffix[] = "in";
const char kOutStructNameSuffix[] = "out";
const char kTintStructInVarPrefix[] = "tint_in";
const char kTintStructOutVarPrefix[] = "tint_out";
const char kTintStructInVarPrefix[] = "_tint_in";
const char kTintStructOutVarPrefix[] = "_tint_out";
bool last_is_break_or_fallthrough(const ast::BlockStatement* stmts) {
if (stmts->empty()) {
@ -109,17 +109,6 @@ GeneratorImpl::GeneratorImpl(const Program* program)
GeneratorImpl::~GeneratorImpl() = default;
std::string GeneratorImpl::generate_name(const std::string& prefix) {
std::string name = prefix;
uint32_t i = 0;
while (namer_.IsMapped(name)) {
name = prefix + "_" + std::to_string(i);
++i;
}
namer_.RegisterRemappedName(name);
return name;
}
bool GeneratorImpl::Generate() {
out_ << "#include <metal_stdlib>" << std::endl << std::endl;
out_ << "using namespace metal;" << std::endl;
@ -268,8 +257,8 @@ bool GeneratorImpl::EmitConstructedType(const type::Type* ty) {
if (!EmitType(alias->type(), "")) {
return false;
}
out_ << " " << namer_.NameFor(program_->Symbols().NameFor(alias->symbol()))
<< ";" << std::endl;
out_ << " " << program_->Symbols().NameFor(alias->symbol()) << ";"
<< std::endl;
} else if (auto* str = ty->As<type::Struct>()) {
if (!EmitStructType(str)) {
return false;
@ -1102,9 +1091,8 @@ bool GeneratorImpl::EmitEntryPointData(ast::Function* func) {
if (!in_locations.empty()) {
auto in_struct_name =
generate_name(program_->Symbols().NameFor(func->symbol()) + "_" +
kInStructNameSuffix);
auto in_var_name = generate_name(kTintStructInVarPrefix);
program_->Symbols().NameFor(func->symbol()) + "_" + kInStructNameSuffix;
auto* in_var_name = kTintStructInVarPrefix;
ep_sym_to_in_data_[func->symbol()] = {in_struct_name, in_var_name};
make_indent();
@ -1139,10 +1127,9 @@ bool GeneratorImpl::EmitEntryPointData(ast::Function* func) {
}
if (!out_variables.empty()) {
auto out_struct_name =
generate_name(program_->Symbols().NameFor(func->symbol()) + "_" +
kOutStructNameSuffix);
auto out_var_name = generate_name(kTintStructOutVarPrefix);
auto out_struct_name = program_->Symbols().NameFor(func->symbol()) + "_" +
kOutStructNameSuffix;
auto* out_var_name = kTintStructOutVarPrefix;
ep_sym_to_out_data_[func->symbol()] = {out_struct_name, out_var_name};
make_indent();
@ -1323,14 +1310,11 @@ bool GeneratorImpl::EmitFunctionInternal(ast::Function* func,
if (emit_duplicate_functions) {
auto func_name = name;
auto ep_name = ep_sym.to_str();
// TODO(dsinclair): The SymbolToName should go away and just use
// to_str() here when the conversion is complete.
name = generate_name(program_->Symbols().NameFor(func->symbol()) + "_" +
program_->Symbols().NameFor(ep_sym));
name = program_->Symbols().NameFor(func->symbol()) + "_" +
program_->Symbols().NameFor(ep_sym);
ep_func_name_remapped_[ep_name + "_" + func_name] = name;
} else {
// TODO(dsinclair): this should be updated to a remapped name
name = namer_.NameFor(program_->Symbols().NameFor(func->symbol()));
name = program_->Symbols().NameFor(func->symbol());
}
out_ << name << "(";
@ -1494,8 +1478,7 @@ bool GeneratorImpl::EmitEntryPointFunction(ast::Function* func) {
} else {
out_ << "void";
}
out_ << " " << namer_.NameFor(program_->Symbols().NameFor(func->symbol()))
<< "(";
out_ << " " << program_->Symbols().NameFor(func->symbol()) << "(";
bool first = true;
auto in_data = ep_sym_to_in_data_.find(current_ep_sym_);
@ -1653,7 +1636,7 @@ bool GeneratorImpl::EmitIdentifier(ast::IdentifierExpression* expr) {
}
}
out_ << namer_.NameFor(program_->Symbols().NameFor(ident->symbol()));
out_ << program_->Symbols().NameFor(ident->symbol());
return true;
}
@ -1661,8 +1644,8 @@ bool GeneratorImpl::EmitIdentifier(ast::IdentifierExpression* expr) {
bool GeneratorImpl::EmitLoop(ast::LoopStatement* stmt) {
loop_emission_counter_++;
std::string guard = namer_.NameFor("tint_msl_is_first_" +
std::to_string(loop_emission_counter_));
std::string guard =
"tint_msl_is_first_" + std::to_string(loop_emission_counter_);
if (stmt->has_continuing()) {
make_indent();
@ -1951,7 +1934,7 @@ bool GeneratorImpl::EmitType(type::Type* type, const std::string& name) {
}
if (auto* alias = type->As<type::Alias>()) {
out_ << namer_.NameFor(program_->Symbols().NameFor(alias->symbol()));
out_ << program_->Symbols().NameFor(alias->symbol());
} else if (auto* ary = type->As<type::Array>()) {
type::Type* base_type = ary;
std::vector<uint32_t> sizes;
@ -1967,7 +1950,7 @@ bool GeneratorImpl::EmitType(type::Type* type, const std::string& name) {
return false;
}
if (!name.empty()) {
out_ << " " << namer_.NameFor(name);
out_ << " " << name;
}
for (uint32_t size : sizes) {
out_ << "[" << size << "]";
@ -2111,7 +2094,7 @@ bool GeneratorImpl::EmitStructType(const type::Struct* str) {
// Array member name will be output with the type
if (!mem->type()->Is<type::Array>()) {
out_ << " " << namer_.NameFor(program_->Symbols().NameFor(mem->symbol()));
out_ << " " << program_->Symbols().NameFor(mem->symbol());
}
out_ << ";" << std::endl;
}

View File

@ -43,7 +43,6 @@
#include "src/scope_stack.h"
#include "src/semantic/intrinsic.h"
#include "src/type/struct_type.h"
#include "src/writer/msl/namer.h"
#include "src/writer/text_generator.h"
namespace tint {
@ -253,10 +252,6 @@ class GeneratorImpl : public TextGenerator {
/// @returns true if an input or output struct is required.
bool has_referenced_var_needing_struct(ast::Function* func);
/// Generates a name for the prefix
/// @param prefix the prefix of the name to generate
/// @returns the name
std::string generate_name(const std::string& prefix);
/// Handles generating a builtin name
/// @param intrinsic the semantic info for the intrinsic
/// @returns the name or "" if not valid
@ -272,9 +267,6 @@ class GeneratorImpl : public TextGenerator {
/// @returns the string name of the builtin or blank on error
std::string builtin_to_attribute(ast::Builtin builtin) const;
/// @returns the namer for testing purposes
Namer* namer_for_testing() { return &namer_; }
private:
enum class VarType { kIn, kOut };
@ -291,7 +283,6 @@ class GeneratorImpl : public TextGenerator {
return program_->TypeOf(expr);
}
Namer namer_;
ScopeStack<const semantic::Variable*> global_variables_;
Symbol current_ep_sym_;
bool generating_entry_point_ = false;

View File

@ -37,16 +37,6 @@ TEST_F(MslGeneratorImplTest, EmitConstructedType_F32) {
)");
}
TEST_F(MslGeneratorImplTest, EmitConstructedType_NameCollision) {
auto* alias = ty.alias("float", ty.f32());
GeneratorImpl& gen = Build();
ASSERT_TRUE(gen.EmitConstructedType(alias)) << gen.error();
EXPECT_EQ(gen.result(), R"(typedef float float_tint_0;
)");
}
TEST_F(MslGeneratorImplTest, EmitConstructedType_Struct) {
auto* str = create<ast::Struct>(
ast::StructMemberList{Member("a", ty.f32()),

View File

@ -77,28 +77,6 @@ using namespace metal;
)");
}
TEST_F(MslGeneratorImplTest, Emit_Function_Name_Collision) {
Func("main", ast::VariableList{}, ty.void_(),
ast::StatementList{
create<ast::ReturnStatement>(),
},
ast::FunctionDecorationList{});
GeneratorImpl& gen = Build();
gen.increment_indent();
ASSERT_TRUE(gen.Generate()) << gen.error();
EXPECT_EQ(gen.result(), R"(#include <metal_stdlib>
using namespace metal;
void main_tint_0() {
return;
}
)");
}
TEST_F(MslGeneratorImplTest, Emit_Function_WithParams) {
ast::VariableList params;
params.push_back(Var("a", ty.f32(), ast::StorageClass::kNone));
@ -137,7 +115,7 @@ TEST_F(MslGeneratorImplTest, Emit_FunctionDecoration_EntryPoint_NoReturn_Void) {
EXPECT_EQ(gen.result(), R"(#include <metal_stdlib>
using namespace metal;
fragment void main_tint_0() {
fragment void main() {
return;
}
@ -173,10 +151,10 @@ struct main_out {
float bar [[color(1)]];
};
fragment main_out main_tint_0(main_in tint_in [[stage_in]]) {
main_out tint_out = {};
tint_out.bar = tint_in.foo;
return tint_out;
fragment main_out main(main_in _tint_in [[stage_in]]) {
main_out _tint_out = {};
_tint_out.bar = _tint_in.foo;
return _tint_out;
}
)");
@ -211,10 +189,10 @@ struct frag_main_out {
float bar [[color(1)]];
};
fragment frag_main_out frag_main(frag_main_in tint_in [[stage_in]]) {
frag_main_out tint_out = {};
tint_out.bar = tint_in.foo;
return tint_out;
fragment frag_main_out frag_main(frag_main_in _tint_in [[stage_in]]) {
frag_main_out _tint_out = {};
_tint_out.bar = _tint_in.foo;
return _tint_out;
}
)");
@ -252,9 +230,9 @@ struct frag_main_out {
};
fragment frag_main_out frag_main(float4 coord [[position]]) {
frag_main_out tint_out = {};
tint_out.depth = coord.x;
return tint_out;
frag_main_out _tint_out = {};
_tint_out.depth = coord.x;
return _tint_out;
}
)");
@ -431,16 +409,16 @@ struct ep_1_out {
float val [[color(0)]];
};
float sub_func_ep_1(thread ep_1_in& tint_in, thread ep_1_out& tint_out, float param) {
tint_out.bar = tint_in.foo;
tint_out.val = param;
return tint_in.foo;
float sub_func_ep_1(thread ep_1_in& _tint_in, thread ep_1_out& _tint_out, float param) {
_tint_out.bar = _tint_in.foo;
_tint_out.val = param;
return _tint_in.foo;
}
fragment ep_1_out ep_1(ep_1_in tint_in [[stage_in]]) {
ep_1_out tint_out = {};
tint_out.bar = sub_func_ep_1(tint_in, tint_out, 1.0f);
return tint_out;
fragment ep_1_out ep_1(ep_1_in _tint_in [[stage_in]]) {
ep_1_out _tint_out = {};
_tint_out.bar = sub_func_ep_1(_tint_in, _tint_out, 1.0f);
return _tint_out;
}
)");
@ -486,9 +464,9 @@ float sub_func(float param) {
}
fragment ep_1_out ep_1() {
ep_1_out tint_out = {};
tint_out.depth = sub_func(1.0f);
return tint_out;
ep_1_out _tint_out = {};
_tint_out.depth = sub_func(1.0f);
return _tint_out;
}
)");
@ -536,15 +514,15 @@ struct ep_1_out {
float depth [[depth(any)]];
};
float sub_func_ep_1(thread ep_1_out& tint_out, thread float4& coord, float param) {
tint_out.depth = coord.x;
float sub_func_ep_1(thread ep_1_out& _tint_out, thread float4& coord, float param) {
_tint_out.depth = coord.x;
return param;
}
fragment ep_1_out ep_1(float4 coord [[position]]) {
ep_1_out tint_out = {};
tint_out.depth = sub_func_ep_1(tint_out, coord, 1.0f);
return tint_out;
ep_1_out _tint_out = {};
_tint_out.depth = sub_func_ep_1(_tint_out, coord, 1.0f);
return _tint_out;
}
)");
@ -750,32 +728,12 @@ struct ep_1_out {
};
fragment ep_1_out ep_1() {
ep_1_out tint_out = {};
tint_out.bar = 1.0f;
ep_1_out _tint_out = {};
_tint_out.bar = 1.0f;
if ((1 == 1)) {
return tint_out;
return _tint_out;
}
return tint_out;
}
)");
}
TEST_F(MslGeneratorImplTest,
Emit_FunctionDecoration_EntryPoint_WithNameCollision) {
Func("main", ast::VariableList{}, ty.void_(), ast::StatementList{},
ast::FunctionDecorationList{
create<ast::StageDecoration>(ast::PipelineStage::kCompute),
});
GeneratorImpl& gen = Build();
ASSERT_TRUE(gen.Generate()) << gen.error();
EXPECT_EQ(gen.result(), R"(#include <metal_stdlib>
using namespace metal;
kernel void main_tint_0() {
return;
return _tint_out;
}
)");

View File

@ -34,15 +34,6 @@ TEST_F(MslGeneratorImplTest, EmitIdentifierExpression) {
EXPECT_EQ(gen.result(), "foo");
}
TEST_F(MslGeneratorImplTest, EmitIdentifierExpression_Single_WithCollision) {
auto* i = Expr("virtual");
GeneratorImpl& gen = Build();
ASSERT_TRUE(gen.EmitExpression(i)) << gen.error();
EXPECT_EQ(gen.result(), "virtual_tint_0");
}
} // namespace
} // namespace msl
} // namespace writer

View File

@ -35,7 +35,7 @@ std::string expected_texture_overload(
case ValidTextureOverload::kDimensions1d:
case ValidTextureOverload::kDimensionsStorageRO1d:
case ValidTextureOverload::kDimensionsStorageWO1d:
return R"(int(texture_tint_0.get_width()))";
return R"(int(texture.get_width()))";
case ValidTextureOverload::kDimensions2d:
case ValidTextureOverload::kDimensions2dArray:
case ValidTextureOverload::kDimensionsMultisampled2d:
@ -46,35 +46,35 @@ std::string expected_texture_overload(
case ValidTextureOverload::kDimensionsStorageRO2dArray:
case ValidTextureOverload::kDimensionsStorageWO2d:
case ValidTextureOverload::kDimensionsStorageWO2dArray:
return R"(int2(texture_tint_0.get_width(), texture_tint_0.get_height()))";
return R"(int2(texture.get_width(), texture.get_height()))";
case ValidTextureOverload::kDimensions3d:
case ValidTextureOverload::kDimensionsStorageRO3d:
case ValidTextureOverload::kDimensionsStorageWO3d:
return R"(int3(texture_tint_0.get_width(), texture_tint_0.get_height(), texture_tint_0.get_depth()))";
return R"(int3(texture.get_width(), texture.get_height(), texture.get_depth()))";
case ValidTextureOverload::kDimensionsCube:
case ValidTextureOverload::kDimensionsCubeArray:
case ValidTextureOverload::kDimensionsDepthCube:
case ValidTextureOverload::kDimensionsDepthCubeArray:
return R"(int3(texture_tint_0.get_width(), texture_tint_0.get_height(), texture_tint_0.get_height()))";
return R"(int3(texture.get_width(), texture.get_height(), texture.get_height()))";
case ValidTextureOverload::kDimensions2dLevel:
case ValidTextureOverload::kDimensions2dArrayLevel:
case ValidTextureOverload::kDimensionsDepth2dLevel:
case ValidTextureOverload::kDimensionsDepth2dArrayLevel:
return R"(int2(texture_tint_0.get_width(1), texture_tint_0.get_height(1)))";
return R"(int2(texture.get_width(1), texture.get_height(1)))";
case ValidTextureOverload::kDimensions3dLevel:
return R"(int3(texture_tint_0.get_width(1), texture_tint_0.get_height(1), texture_tint_0.get_depth(1)))";
return R"(int3(texture.get_width(1), texture.get_height(1), texture.get_depth(1)))";
case ValidTextureOverload::kDimensionsCubeLevel:
case ValidTextureOverload::kDimensionsCubeArrayLevel:
case ValidTextureOverload::kDimensionsDepthCubeLevel:
case ValidTextureOverload::kDimensionsDepthCubeArrayLevel:
return R"(int3(texture_tint_0.get_width(1), texture_tint_0.get_height(1), texture_tint_0.get_height(1)))";
return R"(int3(texture.get_width(1), texture.get_height(1), texture.get_height(1)))";
case ValidTextureOverload::kNumLayers2dArray:
case ValidTextureOverload::kNumLayersCubeArray:
case ValidTextureOverload::kNumLayersMultisampled2dArray:
case ValidTextureOverload::kNumLayersDepth2dArray:
case ValidTextureOverload::kNumLayersDepthCubeArray:
case ValidTextureOverload::kNumLayersStorageWO2dArray:
return R"(int(texture_tint_0.get_array_size()))";
return R"(int(texture.get_array_size()))";
case ValidTextureOverload::kNumLevels2d:
case ValidTextureOverload::kNumLevels2dArray:
case ValidTextureOverload::kNumLevels3d:
@ -84,154 +84,154 @@ std::string expected_texture_overload(
case ValidTextureOverload::kNumLevelsDepth2dArray:
case ValidTextureOverload::kNumLevelsDepthCube:
case ValidTextureOverload::kNumLevelsDepthCubeArray:
return R"(int(texture_tint_0.get_num_mip_levels()))";
return R"(int(texture.get_num_mip_levels()))";
case ValidTextureOverload::kNumSamplesMultisampled2d:
case ValidTextureOverload::kNumSamplesMultisampled2dArray:
return R"(int(texture_tint_0.get_num_samples()))";
return R"(int(texture.get_num_samples()))";
case ValidTextureOverload::kSample1dF32:
return R"(texture_tint_0.sample(sampler_tint_0, 1.0f))";
return R"(texture.sample(sampler, 1.0f))";
case ValidTextureOverload::kSample2dF32:
return R"(texture_tint_0.sample(sampler_tint_0, float2(1.0f, 2.0f)))";
return R"(texture.sample(sampler, float2(1.0f, 2.0f)))";
case ValidTextureOverload::kSample2dOffsetF32:
return R"(texture_tint_0.sample(sampler_tint_0, float2(1.0f, 2.0f), int2(3, 4)))";
return R"(texture.sample(sampler, float2(1.0f, 2.0f), int2(3, 4)))";
case ValidTextureOverload::kSample2dArrayF32:
return R"(texture_tint_0.sample(sampler_tint_0, float2(1.0f, 2.0f), 3))";
return R"(texture.sample(sampler, float2(1.0f, 2.0f), 3))";
case ValidTextureOverload::kSample2dArrayOffsetF32:
return R"(texture_tint_0.sample(sampler_tint_0, float2(1.0f, 2.0f), 3, int2(4, 5)))";
return R"(texture.sample(sampler, float2(1.0f, 2.0f), 3, int2(4, 5)))";
case ValidTextureOverload::kSample3dF32:
return R"(texture_tint_0.sample(sampler_tint_0, float3(1.0f, 2.0f, 3.0f)))";
return R"(texture.sample(sampler, float3(1.0f, 2.0f, 3.0f)))";
case ValidTextureOverload::kSample3dOffsetF32:
return R"(texture_tint_0.sample(sampler_tint_0, float3(1.0f, 2.0f, 3.0f), int3(4, 5, 6)))";
return R"(texture.sample(sampler, float3(1.0f, 2.0f, 3.0f), int3(4, 5, 6)))";
case ValidTextureOverload::kSampleCubeF32:
return R"(texture_tint_0.sample(sampler_tint_0, float3(1.0f, 2.0f, 3.0f)))";
return R"(texture.sample(sampler, float3(1.0f, 2.0f, 3.0f)))";
case ValidTextureOverload::kSampleCubeArrayF32:
return R"(texture_tint_0.sample(sampler_tint_0, float3(1.0f, 2.0f, 3.0f), 4))";
return R"(texture.sample(sampler, float3(1.0f, 2.0f, 3.0f), 4))";
case ValidTextureOverload::kSampleDepth2dF32:
return R"(texture_tint_0.sample(sampler_tint_0, float2(1.0f, 2.0f)))";
return R"(texture.sample(sampler, float2(1.0f, 2.0f)))";
case ValidTextureOverload::kSampleDepth2dOffsetF32:
return R"(texture_tint_0.sample(sampler_tint_0, float2(1.0f, 2.0f), int2(3, 4)))";
return R"(texture.sample(sampler, float2(1.0f, 2.0f), int2(3, 4)))";
case ValidTextureOverload::kSampleDepth2dArrayF32:
return R"(texture_tint_0.sample(sampler_tint_0, float2(1.0f, 2.0f), 3))";
return R"(texture.sample(sampler, float2(1.0f, 2.0f), 3))";
case ValidTextureOverload::kSampleDepth2dArrayOffsetF32:
return R"(texture_tint_0.sample(sampler_tint_0, float2(1.0f, 2.0f), 3, int2(4, 5)))";
return R"(texture.sample(sampler, float2(1.0f, 2.0f), 3, int2(4, 5)))";
case ValidTextureOverload::kSampleDepthCubeF32:
return R"(texture_tint_0.sample(sampler_tint_0, float3(1.0f, 2.0f, 3.0f)))";
return R"(texture.sample(sampler, float3(1.0f, 2.0f, 3.0f)))";
case ValidTextureOverload::kSampleDepthCubeArrayF32:
return R"(texture_tint_0.sample(sampler_tint_0, float3(1.0f, 2.0f, 3.0f), 4))";
return R"(texture.sample(sampler, float3(1.0f, 2.0f, 3.0f), 4))";
case ValidTextureOverload::kSampleBias2dF32:
return R"(texture_tint_0.sample(sampler_tint_0, float2(1.0f, 2.0f), bias(3.0f)))";
return R"(texture.sample(sampler, float2(1.0f, 2.0f), bias(3.0f)))";
case ValidTextureOverload::kSampleBias2dOffsetF32:
return R"(texture_tint_0.sample(sampler_tint_0, float2(1.0f, 2.0f), bias(3.0f), int2(4, 5)))";
return R"(texture.sample(sampler, float2(1.0f, 2.0f), bias(3.0f), int2(4, 5)))";
case ValidTextureOverload::kSampleBias2dArrayF32:
return R"(texture_tint_0.sample(sampler_tint_0, float2(1.0f, 2.0f), 4, bias(3.0f)))";
return R"(texture.sample(sampler, float2(1.0f, 2.0f), 4, bias(3.0f)))";
case ValidTextureOverload::kSampleBias2dArrayOffsetF32:
return R"(texture_tint_0.sample(sampler_tint_0, float2(1.0f, 2.0f), 3, bias(4.0f), int2(5, 6)))";
return R"(texture.sample(sampler, float2(1.0f, 2.0f), 3, bias(4.0f), int2(5, 6)))";
case ValidTextureOverload::kSampleBias3dF32:
return R"(texture_tint_0.sample(sampler_tint_0, float3(1.0f, 2.0f, 3.0f), bias(4.0f)))";
return R"(texture.sample(sampler, float3(1.0f, 2.0f, 3.0f), bias(4.0f)))";
case ValidTextureOverload::kSampleBias3dOffsetF32:
return R"(texture_tint_0.sample(sampler_tint_0, float3(1.0f, 2.0f, 3.0f), bias(4.0f), int3(5, 6, 7)))";
return R"(texture.sample(sampler, float3(1.0f, 2.0f, 3.0f), bias(4.0f), int3(5, 6, 7)))";
case ValidTextureOverload::kSampleBiasCubeF32:
return R"(texture_tint_0.sample(sampler_tint_0, float3(1.0f, 2.0f, 3.0f), bias(4.0f)))";
return R"(texture.sample(sampler, float3(1.0f, 2.0f, 3.0f), bias(4.0f)))";
case ValidTextureOverload::kSampleBiasCubeArrayF32:
return R"(texture_tint_0.sample(sampler_tint_0, float3(1.0f, 2.0f, 3.0f), 3, bias(4.0f)))";
return R"(texture.sample(sampler, float3(1.0f, 2.0f, 3.0f), 3, bias(4.0f)))";
case ValidTextureOverload::kSampleLevel2dF32:
return R"(texture_tint_0.sample(sampler_tint_0, float2(1.0f, 2.0f), level(3.0f)))";
return R"(texture.sample(sampler, float2(1.0f, 2.0f), level(3.0f)))";
case ValidTextureOverload::kSampleLevel2dOffsetF32:
return R"(texture_tint_0.sample(sampler_tint_0, float2(1.0f, 2.0f), level(3.0f), int2(4, 5)))";
return R"(texture.sample(sampler, float2(1.0f, 2.0f), level(3.0f), int2(4, 5)))";
case ValidTextureOverload::kSampleLevel2dArrayF32:
return R"(texture_tint_0.sample(sampler_tint_0, float2(1.0f, 2.0f), 3, level(4.0f)))";
return R"(texture.sample(sampler, float2(1.0f, 2.0f), 3, level(4.0f)))";
case ValidTextureOverload::kSampleLevel2dArrayOffsetF32:
return R"(texture_tint_0.sample(sampler_tint_0, float2(1.0f, 2.0f), 3, level(4.0f), int2(5, 6)))";
return R"(texture.sample(sampler, float2(1.0f, 2.0f), 3, level(4.0f), int2(5, 6)))";
case ValidTextureOverload::kSampleLevel3dF32:
return R"(texture_tint_0.sample(sampler_tint_0, float3(1.0f, 2.0f, 3.0f), level(4.0f)))";
return R"(texture.sample(sampler, float3(1.0f, 2.0f, 3.0f), level(4.0f)))";
case ValidTextureOverload::kSampleLevel3dOffsetF32:
return R"(texture_tint_0.sample(sampler_tint_0, float3(1.0f, 2.0f, 3.0f), level(4.0f), int3(5, 6, 7)))";
return R"(texture.sample(sampler, float3(1.0f, 2.0f, 3.0f), level(4.0f), int3(5, 6, 7)))";
case ValidTextureOverload::kSampleLevelCubeF32:
return R"(texture_tint_0.sample(sampler_tint_0, float3(1.0f, 2.0f, 3.0f), level(4.0f)))";
return R"(texture.sample(sampler, float3(1.0f, 2.0f, 3.0f), level(4.0f)))";
case ValidTextureOverload::kSampleLevelCubeArrayF32:
return R"(texture_tint_0.sample(sampler_tint_0, float3(1.0f, 2.0f, 3.0f), 4, level(5.0f)))";
return R"(texture.sample(sampler, float3(1.0f, 2.0f, 3.0f), 4, level(5.0f)))";
case ValidTextureOverload::kSampleLevelDepth2dF32:
return R"(texture_tint_0.sample(sampler_tint_0, float2(1.0f, 2.0f), level(3)))";
return R"(texture.sample(sampler, float2(1.0f, 2.0f), level(3)))";
case ValidTextureOverload::kSampleLevelDepth2dOffsetF32:
return R"(texture_tint_0.sample(sampler_tint_0, float2(1.0f, 2.0f), level(3), int2(4, 5)))";
return R"(texture.sample(sampler, float2(1.0f, 2.0f), level(3), int2(4, 5)))";
case ValidTextureOverload::kSampleLevelDepth2dArrayF32:
return R"(texture_tint_0.sample(sampler_tint_0, float2(1.0f, 2.0f), 3, level(4)))";
return R"(texture.sample(sampler, float2(1.0f, 2.0f), 3, level(4)))";
case ValidTextureOverload::kSampleLevelDepth2dArrayOffsetF32:
return R"(texture_tint_0.sample(sampler_tint_0, float2(1.0f, 2.0f), 3, level(4), int2(5, 6)))";
return R"(texture.sample(sampler, float2(1.0f, 2.0f), 3, level(4), int2(5, 6)))";
case ValidTextureOverload::kSampleLevelDepthCubeF32:
return R"(texture_tint_0.sample(sampler_tint_0, float3(1.0f, 2.0f, 3.0f), level(4)))";
return R"(texture.sample(sampler, float3(1.0f, 2.0f, 3.0f), level(4)))";
case ValidTextureOverload::kSampleLevelDepthCubeArrayF32:
return R"(texture_tint_0.sample(sampler_tint_0, float3(1.0f, 2.0f, 3.0f), 4, level(5)))";
return R"(texture.sample(sampler, float3(1.0f, 2.0f, 3.0f), 4, level(5)))";
case ValidTextureOverload::kSampleGrad2dF32:
return R"(texture_tint_0.sample(sampler_tint_0, float2(1.0f, 2.0f), gradient2d(float2(3.0f, 4.0f), float2(5.0f, 6.0f))))";
return R"(texture.sample(sampler, float2(1.0f, 2.0f), gradient2d(float2(3.0f, 4.0f), float2(5.0f, 6.0f))))";
case ValidTextureOverload::kSampleGrad2dOffsetF32:
return R"(texture_tint_0.sample(sampler_tint_0, float2(1.0f, 2.0f), gradient2d(float2(3.0f, 4.0f), float2(5.0f, 6.0f)), int2(7, 8)))";
return R"(texture.sample(sampler, float2(1.0f, 2.0f), gradient2d(float2(3.0f, 4.0f), float2(5.0f, 6.0f)), int2(7, 8)))";
case ValidTextureOverload::kSampleGrad2dArrayF32:
return R"(texture_tint_0.sample(sampler_tint_0, float2(1.0f, 2.0f), 3, gradient2d(float2(4.0f, 5.0f), float2(6.0f, 7.0f))))";
return R"(texture.sample(sampler, float2(1.0f, 2.0f), 3, gradient2d(float2(4.0f, 5.0f), float2(6.0f, 7.0f))))";
case ValidTextureOverload::kSampleGrad2dArrayOffsetF32:
return R"(texture_tint_0.sample(sampler_tint_0, float2(1.0f, 2.0f), 3, gradient2d(float2(4.0f, 5.0f), float2(6.0f, 7.0f)), int2(8, 9)))";
return R"(texture.sample(sampler, float2(1.0f, 2.0f), 3, gradient2d(float2(4.0f, 5.0f), float2(6.0f, 7.0f)), int2(8, 9)))";
case ValidTextureOverload::kSampleGrad3dF32:
return R"(texture_tint_0.sample(sampler_tint_0, float3(1.0f, 2.0f, 3.0f), gradient3d(float3(4.0f, 5.0f, 6.0f), float3(7.0f, 8.0f, 9.0f))))";
return R"(texture.sample(sampler, float3(1.0f, 2.0f, 3.0f), gradient3d(float3(4.0f, 5.0f, 6.0f), float3(7.0f, 8.0f, 9.0f))))";
case ValidTextureOverload::kSampleGrad3dOffsetF32:
return R"(texture_tint_0.sample(sampler_tint_0, float3(1.0f, 2.0f, 3.0f), gradient3d(float3(4.0f, 5.0f, 6.0f), float3(7.0f, 8.0f, 9.0f)), int3(10, 11, 12)))";
return R"(texture.sample(sampler, float3(1.0f, 2.0f, 3.0f), gradient3d(float3(4.0f, 5.0f, 6.0f), float3(7.0f, 8.0f, 9.0f)), int3(10, 11, 12)))";
case ValidTextureOverload::kSampleGradCubeF32:
return R"(texture_tint_0.sample(sampler_tint_0, float3(1.0f, 2.0f, 3.0f), gradientcube(float3(4.0f, 5.0f, 6.0f), float3(7.0f, 8.0f, 9.0f))))";
return R"(texture.sample(sampler, float3(1.0f, 2.0f, 3.0f), gradientcube(float3(4.0f, 5.0f, 6.0f), float3(7.0f, 8.0f, 9.0f))))";
case ValidTextureOverload::kSampleGradCubeArrayF32:
return R"(texture_tint_0.sample(sampler_tint_0, float3(1.0f, 2.0f, 3.0f), 4, gradientcube(float3(5.0f, 6.0f, 7.0f), float3(8.0f, 9.0f, 10.0f))))";
return R"(texture.sample(sampler, float3(1.0f, 2.0f, 3.0f), 4, gradientcube(float3(5.0f, 6.0f, 7.0f), float3(8.0f, 9.0f, 10.0f))))";
case ValidTextureOverload::kSampleCompareDepth2dF32:
return R"(texture_tint_0.sample_compare(sampler_tint_0, float2(1.0f, 2.0f), 3.0f))";
return R"(texture.sample_compare(sampler, float2(1.0f, 2.0f), 3.0f))";
case ValidTextureOverload::kSampleCompareDepth2dOffsetF32:
return R"(texture_tint_0.sample_compare(sampler_tint_0, float2(1.0f, 2.0f), 3.0f, int2(4, 5)))";
return R"(texture.sample_compare(sampler, float2(1.0f, 2.0f), 3.0f, int2(4, 5)))";
case ValidTextureOverload::kSampleCompareDepth2dArrayF32:
return R"(texture_tint_0.sample_compare(sampler_tint_0, float2(1.0f, 2.0f), 4, 3.0f))";
return R"(texture.sample_compare(sampler, float2(1.0f, 2.0f), 4, 3.0f))";
case ValidTextureOverload::kSampleCompareDepth2dArrayOffsetF32:
return R"(texture_tint_0.sample_compare(sampler_tint_0, float2(1.0f, 2.0f), 4, 3.0f, int2(5, 6)))";
return R"(texture.sample_compare(sampler, float2(1.0f, 2.0f), 4, 3.0f, int2(5, 6)))";
case ValidTextureOverload::kSampleCompareDepthCubeF32:
return R"(texture_tint_0.sample_compare(sampler_tint_0, float3(1.0f, 2.0f, 3.0f), 4.0f))";
return R"(texture.sample_compare(sampler, float3(1.0f, 2.0f, 3.0f), 4.0f))";
case ValidTextureOverload::kSampleCompareDepthCubeArrayF32:
return R"(texture_tint_0.sample_compare(sampler_tint_0, float3(1.0f, 2.0f, 3.0f), 4, 5.0f))";
return R"(texture.sample_compare(sampler, float3(1.0f, 2.0f, 3.0f), 4, 5.0f))";
case ValidTextureOverload::kLoad1dLevelF32:
return R"(texture_tint_0.read(1, 3))";
return R"(texture.read(1, 3))";
case ValidTextureOverload::kLoad1dLevelU32:
return R"(texture_tint_0.read(1, 3))";
return R"(texture.read(1, 3))";
case ValidTextureOverload::kLoad1dLevelI32:
return R"(texture_tint_0.read(1, 3))";
return R"(texture.read(1, 3))";
case ValidTextureOverload::kLoad2dLevelF32:
return R"(texture_tint_0.read(int2(1, 2), 3))";
return R"(texture.read(int2(1, 2), 3))";
case ValidTextureOverload::kLoad2dLevelU32:
return R"(texture_tint_0.read(int2(1, 2), 3))";
return R"(texture.read(int2(1, 2), 3))";
case ValidTextureOverload::kLoad2dLevelI32:
return R"(texture_tint_0.read(int2(1, 2), 3))";
return R"(texture.read(int2(1, 2), 3))";
case ValidTextureOverload::kLoad2dArrayLevelF32:
return R"(texture_tint_0.read(int2(1, 2), 3, 4))";
return R"(texture.read(int2(1, 2), 3, 4))";
case ValidTextureOverload::kLoad2dArrayLevelU32:
return R"(texture_tint_0.read(int2(1, 2), 3, 4))";
return R"(texture.read(int2(1, 2), 3, 4))";
case ValidTextureOverload::kLoad2dArrayLevelI32:
return R"(texture_tint_0.read(int2(1, 2), 3, 4))";
return R"(texture.read(int2(1, 2), 3, 4))";
case ValidTextureOverload::kLoad3dLevelF32:
return R"(texture_tint_0.read(int3(1, 2, 3), 4))";
return R"(texture.read(int3(1, 2, 3), 4))";
case ValidTextureOverload::kLoad3dLevelU32:
return R"(texture_tint_0.read(int3(1, 2, 3), 4))";
return R"(texture.read(int3(1, 2, 3), 4))";
case ValidTextureOverload::kLoad3dLevelI32:
return R"(texture_tint_0.read(int3(1, 2, 3), 4))";
return R"(texture.read(int3(1, 2, 3), 4))";
case ValidTextureOverload::kLoadMultisampled2dF32:
return R"(texture_tint_0.read(int2(1, 2), 3))";
return R"(texture.read(int2(1, 2), 3))";
case ValidTextureOverload::kLoadMultisampled2dU32:
return R"(texture_tint_0.read(int2(1, 2), 3))";
return R"(texture.read(int2(1, 2), 3))";
case ValidTextureOverload::kLoadMultisampled2dI32:
return R"(texture_tint_0.read(int2(1, 2), 3))";
return R"(texture.read(int2(1, 2), 3))";
case ValidTextureOverload::kLoadMultisampled2dArrayF32:
return R"(texture_tint_0.read(int2(1, 2), 3, 4))";
return R"(texture.read(int2(1, 2), 3, 4))";
case ValidTextureOverload::kLoadMultisampled2dArrayU32:
return R"(texture_tint_0.read(int2(1, 2), 3, 4))";
return R"(texture.read(int2(1, 2), 3, 4))";
case ValidTextureOverload::kLoadMultisampled2dArrayI32:
return R"(texture_tint_0.read(int2(1, 2), 3, 4))";
return R"(texture.read(int2(1, 2), 3, 4))";
case ValidTextureOverload::kLoadDepth2dLevelF32:
return R"(texture_tint_0.read(int2(1, 2), 3))";
return R"(texture.read(int2(1, 2), 3))";
case ValidTextureOverload::kLoadDepth2dArrayLevelF32:
return R"(texture_tint_0.read(int2(1, 2), 3, 4))";
return R"(texture.read(int2(1, 2), 3, 4))";
case ValidTextureOverload::kLoadStorageRO1dRgba32float:
return R"(texture_tint_0.read(1))";
return R"(texture.read(1))";
case ValidTextureOverload::kLoadStorageRO2dRgba8unorm:
case ValidTextureOverload::kLoadStorageRO2dRgba8snorm:
case ValidTextureOverload::kLoadStorageRO2dRgba8uint:
@ -248,19 +248,19 @@ std::string expected_texture_overload(
case ValidTextureOverload::kLoadStorageRO2dRgba32uint:
case ValidTextureOverload::kLoadStorageRO2dRgba32sint:
case ValidTextureOverload::kLoadStorageRO2dRgba32float:
return R"(texture_tint_0.read(int2(1, 2)))";
return R"(texture.read(int2(1, 2)))";
case ValidTextureOverload::kLoadStorageRO2dArrayRgba32float:
return R"(texture_tint_0.read(int2(1, 2), 3))";
return R"(texture.read(int2(1, 2), 3))";
case ValidTextureOverload::kLoadStorageRO3dRgba32float:
return R"(texture_tint_0.read(int3(1, 2, 3)))";
return R"(texture.read(int3(1, 2, 3)))";
case ValidTextureOverload::kStoreWO1dRgba32float:
return R"(texture_tint_0.write(float4(2.0f, 3.0f, 4.0f, 5.0f), 1))";
return R"(texture.write(float4(2.0f, 3.0f, 4.0f, 5.0f), 1))";
case ValidTextureOverload::kStoreWO2dRgba32float:
return R"(texture_tint_0.write(float4(3.0f, 4.0f, 5.0f, 6.0f), int2(1, 2)))";
return R"(texture.write(float4(3.0f, 4.0f, 5.0f, 6.0f), int2(1, 2)))";
case ValidTextureOverload::kStoreWO2dArrayRgba32float:
return R"(texture_tint_0.write(float4(4.0f, 5.0f, 6.0f, 7.0f), int2(1, 2), 3))";
return R"(texture.write(float4(4.0f, 5.0f, 6.0f, 7.0f), int2(1, 2), 3))";
case ValidTextureOverload::kStoreWO3dRgba32float:
return R"(texture_tint_0.write(float4(4.0f, 5.0f, 6.0f, 7.0f), int3(1, 2, 3)))";
return R"(texture.write(float4(4.0f, 5.0f, 6.0f, 7.0f), int3(1, 2, 3)))";
}
return "<unmatched texture overload>";
} // NOLINT - Ignore the length of this function

View File

@ -37,7 +37,6 @@
#include "src/type/u32_type.h"
#include "src/type/vector_type.h"
#include "src/type/void_type.h"
#include "src/writer/msl/namer.h"
#include "src/writer/msl/test_helper.h"
namespace tint {
@ -66,29 +65,6 @@ kernel void my_func() {
)");
}
TEST_F(MslGeneratorImplTest, InputStructName) {
GeneratorImpl& gen = Build();
ASSERT_EQ(gen.generate_name("func_main_in"), "func_main_in");
}
TEST_F(MslGeneratorImplTest, InputStructName_ConflictWithExisting) {
GeneratorImpl& gen = Build();
gen.namer_for_testing()->NameFor("func_main_out");
ASSERT_EQ(gen.generate_name("func_main_out"), "func_main_out_0");
}
TEST_F(MslGeneratorImplTest, NameConflictWith_InputStructName) {
auto* ident = Expr("func_main_in");
GeneratorImpl& gen = Build();
ASSERT_EQ(gen.generate_name("func_main_in"), "func_main_in");
ASSERT_TRUE(gen.EmitIdentifier(ident));
EXPECT_EQ(gen.result(), "func_main_in_0");
}
struct MslBuiltinData {
ast::Builtin builtin;
const char* attribute_name;

View File

@ -56,15 +56,6 @@ TEST_F(MslGeneratorImplTest, EmitType_Alias) {
EXPECT_EQ(gen.result(), "alias");
}
TEST_F(MslGeneratorImplTest, EmitType_Alias_NameCollision) {
auto* alias = ty.alias("bool", ty.f32());
GeneratorImpl& gen = Build();
ASSERT_TRUE(gen.EmitType(alias, "")) << gen.error();
EXPECT_EQ(gen.result(), "bool_tint_0");
}
TEST_F(MslGeneratorImplTest, EmitType_Array) {
auto* arr = ty.array<bool, 4>();
@ -107,15 +98,6 @@ TEST_F(MslGeneratorImplTest, EmitType_ArrayOfArrayOfArray) {
EXPECT_EQ(gen.result(), "bool ary[6][5][4]");
}
TEST_F(MslGeneratorImplTest, EmitType_Array_NameCollision) {
auto* arr = ty.array<bool, 4>();
GeneratorImpl& gen = Build();
ASSERT_TRUE(gen.EmitType(arr, "bool")) << gen.error();
EXPECT_EQ(gen.result(), "bool bool_tint_0[4]");
}
TEST_F(MslGeneratorImplTest, EmitType_Array_WithoutName) {
auto* arr = ty.array<bool, 4>();
@ -134,15 +116,6 @@ TEST_F(MslGeneratorImplTest, EmitType_RuntimeArray) {
EXPECT_EQ(gen.result(), "bool ary[1]");
}
TEST_F(MslGeneratorImplTest, EmitType_RuntimeArray_NameCollision) {
auto* arr = ty.array<bool, 1>();
GeneratorImpl& gen = Build();
ASSERT_TRUE(gen.EmitType(arr, "discard_fragment")) << gen.error();
EXPECT_EQ(gen.result(), "bool discard_fragment_tint_0[1]");
}
TEST_F(MslGeneratorImplTest, EmitType_Bool) {
auto* bool_ = ty.bool_();
@ -246,24 +219,6 @@ TEST_F(MslGeneratorImplTest, EmitType_Struct_InjectPadding) {
)");
}
TEST_F(MslGeneratorImplTest, EmitType_Struct_NameCollision) {
auto* str =
create<ast::Struct>(ast::StructMemberList{Member("main", ty.i32()),
Member("float", ty.f32())},
ast::StructDecorationList{});
auto* s = ty.struct_("S", str);
GeneratorImpl& gen = Build();
ASSERT_TRUE(gen.EmitStructType(s)) << gen.error();
EXPECT_EQ(gen.result(), R"(struct S {
int main_tint_0;
float float_tint_0;
};
)");
}
// TODO(dsinclair): How to translate [[block]]
TEST_F(MslGeneratorImplTest, DISABLED_EmitType_Struct_WithDecoration) {
ast::StructDecorationList decos;

View File

@ -1,323 +0,0 @@
// 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 "src/writer/msl/namer.h"
#include <algorithm>
namespace tint {
namespace writer {
namespace msl {
namespace {
const char* kNames[] = {"access",
"alignas",
"alignof",
"and",
"and_eq",
"array",
"array_ref",
"as_type",
"asm",
"atomic",
"atomic_bool",
"atomic_int",
"atomic_uint",
"auto",
"bitand",
"bitor",
"bool",
"bool2",
"bool3",
"bool4",
"break",
"buffer",
"case",
"catch",
"char",
"char16_t",
"char2",
"char3",
"char32_t",
"char4",
"class",
"compl",
"const",
"const_cast",
"const_reference",
"constant",
"constexpr",
"continue",
"decltype",
"default",
"delete",
"depth2d",
"depth2d_array",
"depth2d_ms",
"depth2d_ms_array",
"depthcube",
"depthcube_array",
"device",
"discard_fragment",
"do",
"double",
"dynamic_cast",
"else",
"enum",
"explicit",
"extern",
"extern",
"false",
"final",
"float",
"float2",
"float2x2",
"float2x3",
"float2x4",
"float3",
"float3x2",
"float3x3",
"float3x4",
"float4",
"float4x2",
"float4x3",
"float4x4",
"for",
"fragment",
"friend",
"goto",
"half",
"half2",
"half2x2",
"half2x3",
"half2x4",
"half3",
"half3x2",
"half3x3",
"half3x4",
"half4",
"half4x2",
"half4x3",
"half4x4",
"if",
"imageblock",
"inline",
"inline",
"int",
"int16_t",
"int2",
"int3",
"int32_t",
"int4",
"int64_t",
"int8_t",
"kernel",
"long",
"long2",
"long3",
"long4",
"main",
"metal",
"mutable"
"mutable",
"namespace",
"new",
"noexcept"
"not",
"not_eq",
"nullptr",
"operator",
"or",
"or_eq",
"override",
"packed_bool2",
"packed_bool3",
"packed_bool4",
"packed_char2",
"packed_char3",
"packed_char4",
"packed_float2",
"packed_float3",
"packed_float4",
"packed_half2",
"packed_half3",
"packed_half4",
"packed_int2",
"packed_int3",
"packed_int4",
"packed_short2",
"packed_short3",
"packed_short4",
"packed_uchar2",
"packed_uchar3",
"packed_uchar4",
"packed_uint2",
"packed_uint3",
"packed_uint4",
"packed_ushort2",
"packed_ushort3",
"packed_ushort4",
"patch_control_point",
"private",
"protected",
"ptrdiff_t",
"public",
"r16snorm",
"r16unorm",
"r8unorm",
"reference",
"register",
"reinterpret_cast",
"return",
"rg11b10f",
"rg16snorm",
"rg16unorm",
"rg8snorm",
"rg8unorm",
"rgb10a2",
"rgb9e5",
"rgba16snorm",
"rgba16unorm",
"rgba8snorm",
"rgba8unorm",
"sampler",
"short",
"short2",
"short3",
"short4",
"signed",
"size_t",
"sizeof",
"srgba8unorm",
"static",
"static_assert",
"static_cast",
"struct",
"switch",
"template",
"texture",
"texture1d",
"texture1d_array",
"texture2d",
"texture2d_array",
"texture2d_ms",
"texture2d_ms_array",
"texture3d",
"texture_buffer",
"texturecube",
"texturecube_array",
"this",
"thread",
"thread_local",
"threadgroup",
"threadgroup_imageblock",
"throw",
"true",
"try",
"typedef",
"typeid",
"typename",
"uchar",
"uchar2",
"uchar3",
"uchar4",
"uint",
"uint16_t",
"uint2",
"uint3",
"uint32_t",
"uint4",
"uint64_t",
"uint8_t",
"ulong2",
"ulong3",
"ulong4",
"uniform",
"union",
"unsigned",
"ushort",
"ushort2",
"ushort3",
"ushort4",
"using",
"vec",
"vertex",
"virtual",
"virtual",
"void",
"volatile",
"wchar_t",
"while",
"xor",
"xor_eq"};
} // namespace
Namer::Namer() = default;
Namer::~Namer() = default;
std::string Namer::NameFor(const std::string& name) {
// If it's in the name map we can just return it. There are no shadow names
// in WGSL so this has to be unique in the WGSL names, and we've already
// checked the name collisions with MSL.
auto it = name_map_.find(name);
if (it != name_map_.end()) {
return it->second;
}
std::string ret_name = name;
if (std::binary_search(std::begin(kNames), std::end(kNames), ret_name)) {
uint32_t i = 0;
// Make sure there wasn't already a tint variable with the new name we've
// now created.
while (true) {
ret_name = name + "_tint_" + std::to_string(i);
it = name_map_.find(ret_name);
if (it == name_map_.end()) {
break;
}
i++;
}
RegisterRemappedName(ret_name);
} else {
uint32_t i = 0;
// Make sure the ident name wasn't assigned by a remapping.
while (true) {
auto remap_it = remapped_names_.find(ret_name);
if (remap_it == remapped_names_.end()) {
break;
}
ret_name = name + "_" + std::to_string(i);
i++;
}
RegisterRemappedName(ret_name);
}
name_map_[name] = ret_name;
return ret_name;
}
bool Namer::IsMapped(const std::string& name) {
auto it = name_map_.find(name);
return it != name_map_.end();
}
void Namer::RegisterRemappedName(const std::string& name) {
remapped_names_.insert(name);
}
} // namespace msl
} // namespace writer
} // namespace tint

View File

@ -1,58 +0,0 @@
// 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.
#ifndef SRC_WRITER_MSL_NAMER_H_
#define SRC_WRITER_MSL_NAMER_H_
#include <string>
#include <unordered_map>
#include <unordered_set>
namespace tint {
namespace writer {
namespace msl {
/// Remaps maps names to avoid reserved words and collisions for MSL.
class Namer {
public:
/// Constructor
Namer();
~Namer();
/// Returns a sanitized version of `name`
/// @param name the name to sanitize
/// @returns the sanitized version of `name`
std::string NameFor(const std::string& name);
/// Registers a remapped name.
/// @param name the name to register
void RegisterRemappedName(const std::string& name);
/// Returns if the given name has been mapped alread
/// @param name the name to check
/// @returns true if the name has been mapped
bool IsMapped(const std::string& name);
private:
/// Map of original name to new name. The two names may be the same.
std::unordered_map<std::string, std::string> name_map_;
// The list of names taken by the remapper
std::unordered_set<std::string> remapped_names_;
};
} // namespace msl
} // namespace writer
} // namespace tint
#endif // SRC_WRITER_MSL_NAMER_H_

View File

@ -1,304 +0,0 @@
// 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 "src/writer/msl/namer.h"
#include "gtest/gtest.h"
namespace tint {
namespace writer {
namespace msl {
namespace {
using MslNamerTest = testing::Test;
TEST_F(MslNamerTest, ReturnsName) {
Namer n;
EXPECT_EQ("my_name", n.NameFor("my_name"));
EXPECT_EQ("my_name", n.NameFor("my_name"));
}
TEST_F(MslNamerTest, HandlesConflictWithRenamedReservedWordAfterIdentSeen) {
Namer n;
EXPECT_EQ("float_tint_0", n.NameFor("float_tint_0"));
EXPECT_EQ("float_tint_1", n.NameFor("float"));
EXPECT_EQ("float_tint_0", n.NameFor("float_tint_0"));
}
TEST_F(MslNamerTest, HandlesConflictWithRenamedReservedWordBeforeIdentSeen) {
Namer n;
EXPECT_EQ("float_tint_0", n.NameFor("float"));
EXPECT_EQ("float_tint_0_0", n.NameFor("float_tint_0"));
EXPECT_EQ("float_tint_0_0_0", n.NameFor("float_tint_0_0"));
EXPECT_EQ("float_tint_0_0", n.NameFor("float_tint_0"));
}
using MslReservedNameTest = testing::TestWithParam<std::string>;
TEST_P(MslReservedNameTest, Emit) {
auto name = GetParam();
Namer n;
EXPECT_EQ(name + "_tint_0", n.NameFor(name));
}
INSTANTIATE_TEST_SUITE_P(MslNamerTest,
MslReservedNameTest,
testing::Values(
// c++14 spec
"alignas",
"alignof",
"and",
"and_eq",
"asm",
"auto",
"bitand",
"bitor",
"bool",
"break",
"case",
"catch",
"char",
"char16_t",
"char32_t",
"class",
"compl",
"const",
"const_cast",
"constexpr",
"continue",
"decltype",
"default",
"delete",
"do",
"double",
"dynamic_cast",
"else",
"enum",
"explicit",
"extern",
"extern",
"false",
"final",
"float",
"for",
"friend",
"goto",
"if",
"inline",
"inline",
"int",
"long",
"mutable"
"mutable",
"namespace",
"new",
"noexcept"
"not",
"not_eq",
"nullptr",
"operator",
"or",
"or_eq",
"override",
"private",
"protected",
"public",
"register",
"reinterpret_cast",
"return",
"short",
"signed",
"sizeof",
"static",
"static_assert",
"static_cast",
"struct",
"switch",
"template",
"this",
"thread_local",
"throw",
"true",
"try",
"typedef",
"typeid",
"typename",
"union",
"unsigned",
"using",
"virtual",
"virtual",
"void",
"volatile",
"wchar_t",
"while",
"xor",
"xor_eq",
// Metal Spec
"access",
"array",
"array_ref",
"as_type",
"atomic",
"atomic_bool",
"atomic_int",
"atomic_uint",
"bool2",
"bool3",
"bool4",
"buffer",
"char2",
"char3",
"char4",
"const_reference",
"constant",
"depth2d",
"depth2d_array",
"depth2d_ms",
"depth2d_ms_array",
"depthcube",
"depthcube_array",
"device",
"discard_fragment",
"float2",
"float2x2",
"float2x3",
"float2x4",
"float3",
"float3x2",
"float3x3",
"float3x4",
"float4",
"float4x2",
"float4x3",
"float4x4",
"fragment",
"half",
"half2",
"half2x2",
"half2x3",
"half2x4",
"half3",
"half3x2",
"half3x3",
"half3x4",
"half4",
"half4x2",
"half4x3",
"half4x4",
"imageblock",
"int16_t",
"int2",
"int3",
"int32_t",
"int4",
"int64_t",
"int8_t",
"kernel",
"long2",
"long3",
"long4",
"main", // No functions called main
"metal", // The namespace
"packed_bool2",
"packed_bool3",
"packed_bool4",
"packed_char2",
"packed_char3",
"packed_char4",
"packed_float2",
"packed_float3",
"packed_float4",
"packed_half2",
"packed_half3",
"packed_half4",
"packed_int2",
"packed_int3",
"packed_int4",
"packed_short2",
"packed_short3",
"packed_short4",
"packed_uchar2",
"packed_uchar3",
"packed_uchar4",
"packed_uint2",
"packed_uint3",
"packed_uint4",
"packed_ushort2",
"packed_ushort3",
"packed_ushort4",
"patch_control_point",
"ptrdiff_t",
"r16snorm",
"r16unorm",
"r8unorm",
"reference",
"rg11b10f",
"rg16snorm",
"rg16unorm",
"rg8snorm",
"rg8unorm",
"rgb10a2",
"rgb9e5",
"rgba16snorm",
"rgba16unorm",
"rgba8snorm",
"rgba8unorm",
"sampler",
"short2",
"short3",
"short4",
"size_t",
"srgba8unorm",
"texture",
"texture1d",
"texture1d_array",
"texture2d",
"texture2d_array",
"texture2d_ms",
"texture2d_ms_array",
"texture3d",
"texture_buffer",
"texturecube",
"texturecube_array",
"thread",
"threadgroup",
"threadgroup_imageblock",
"uchar",
"uchar2",
"uchar3",
"uchar4",
"uint",
"uint16_t",
"uint2",
"uint3",
"uint32_t",
"uint4",
"uint64_t",
"uint8_t",
"ulong2",
"ulong3",
"ulong4",
"uniform",
"ushort",
"ushort2",
"ushort3",
"ushort4",
"vec",
"vertex"));
} // namespace
} // namespace msl
} // namespace writer
} // namespace tint