[writer/hlsl,msl] Correctly generate inf and nan literals

Also add missing msl macros to the renamer.

Bug: tint:951
Change-Id: I543e6eae885c979596ca63f9d6c7378dd5425e8a
Reviewed-on: https://dawn-review.googlesource.com/c/tint/+/56941
Kokoro: Kokoro <noreply+kokoro@google.com>
Commit-Queue: Ben Clayton <bclayton@chromium.org>
Auto-Submit: Ben Clayton <bclayton@google.com>
Reviewed-by: James Price <jrprice@google.com>
This commit is contained in:
Ben Clayton
2021-07-06 09:43:49 +00:00
committed by Tint LUCI CQ
parent 5d8eb4a758
commit 692fc20797
25 changed files with 2568 additions and 1068 deletions

File diff suppressed because it is too large Load Diff

View File

@@ -847,248 +847,286 @@ INSTANTIATE_TEST_SUITE_P(RenamerTestHlsl,
"volatile"));
// "while" // WGSL reserved keyword
INSTANTIATE_TEST_SUITE_P(RenamerTestMsl,
RenamerTestMsl,
testing::Values(
// c++14 spec
"alignas",
"alignof",
"and",
"and_eq",
// "asm", // Also reserved in WGSL
"auto",
"bitand",
"bitor",
// "bool", // Also used in WGSL
// "break", // Also used in WGSL
// "case", // Also used in WGSL
"catch",
"char",
"char16_t",
"char32_t",
"class",
"compl",
// "const", // Also used in WGSL
"const_cast",
"constexpr",
// "continue", // Also used in WGSL
"decltype",
// "default", // Also used in WGSL
"delete",
// "do", // Also used in WGSL
"double",
"dynamic_cast",
// "else", // Also used in WGSL
// "enum", // Also used in WGSL
"explicit",
"extern",
// "false", // Also used in WGSL
"final",
"float",
// "for", // Also used in WGSL
"friend",
"goto",
// "if", // Also used in WGSL
"inline",
"int",
"long",
"mutable",
"namespace",
"new",
"noexcept",
"not",
"not_eq",
"nullptr",
"operator",
"or",
"or_eq",
"override",
// "private", // Also used in WGSL
"protected",
"public",
"register",
"reinterpret_cast",
// "return", // Also used in WGSL
"short",
"signed",
"sizeof",
"static",
"static_assert",
"static_cast",
// "struct", // Also used in WGSL
// "switch", // Also used in WGSL
"template",
"this",
"thread_local",
"throw",
// "true", // Also used in WGSL
"try",
// "typedef", // Also used in WGSL
"typeid",
"typename",
"union",
"unsigned",
// "using", // WGSL reserved keyword
"virtual",
// "void", // Also used in WGSL
"volatile",
"wchar_t",
// "while", // WGSL reserved keyword
"xor",
"xor_eq",
INSTANTIATE_TEST_SUITE_P(
RenamerTestMsl,
RenamerTestMsl,
testing::Values(
// c++14 spec
"alignas",
"alignof",
"and",
"and_eq",
// "asm", // Also reserved in WGSL
"auto",
"bitand",
"bitor",
// "bool", // Also used in WGSL
// "break", // Also used in WGSL
// "case", // Also used in WGSL
"catch",
"char",
"char16_t",
"char32_t",
"class",
"compl",
// "const", // Also used in WGSL
"const_cast",
"constexpr",
// "continue", // Also used in WGSL
"decltype",
// "default", // Also used in WGSL
"delete",
// "do", // Also used in WGSL
"double",
"dynamic_cast",
// "else", // Also used in WGSL
// "enum", // Also used in WGSL
"explicit",
"extern",
// "false", // Also used in WGSL
"final",
"float",
// "for", // Also used in WGSL
"friend",
"goto",
// "if", // Also used in WGSL
"inline",
"int",
"long",
"mutable",
"namespace",
"new",
"noexcept",
"not",
"not_eq",
"nullptr",
"operator",
"or",
"or_eq",
"override",
// "private", // Also used in WGSL
"protected",
"public",
"register",
"reinterpret_cast",
// "return", // Also used in WGSL
"short",
"signed",
"sizeof",
"static",
"static_assert",
"static_cast",
// "struct", // Also used in WGSL
// "switch", // Also used in WGSL
"template",
"this",
"thread_local",
"throw",
// "true", // Also used in WGSL
"try",
// "typedef", // Also used in WGSL
"typeid",
"typename",
"union",
"unsigned",
// "using", // WGSL reserved keyword
"virtual",
// "void", // Also used in WGSL
"volatile",
"wchar_t",
// "while", // WGSL reserved keyword
"xor",
"xor_eq",
// Metal Spec
"access",
// "array", // Also used in WGSL
"array_ref",
"as_type",
// "atomic", // Also used in WGSL
"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", // Also used in WGSL
"reference",
"rg11b10f",
"rg16snorm",
"rg16unorm",
// "rg8snorm", // Also used in WGSL
// "rg8unorm", // Also used in WGSL
"rgb10a2",
"rgb9e5",
"rgba16snorm",
"rgba16unorm",
// "rgba8snorm", // Also used in WGSL
// "rgba8unorm", // Also used in WGSL
// "sampler", // Also used in WGSL
"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", // Also used in WGSL
"ushort",
"ushort2",
"ushort3",
"ushort4",
// "vec", // WGSL reserved keyword
"vertex"));
// Metal Spec
"access",
// "array", // Also used in WGSL
"array_ref",
"as_type",
// "atomic", // Also used in WGSL
"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", // Also used in WGSL
"reference",
"rg11b10f",
"rg16snorm",
"rg16unorm",
// "rg8snorm", // Also used in WGSL
// "rg8unorm", // Also used in WGSL
"rgb10a2",
"rgb9e5",
"rgba16snorm",
"rgba16unorm",
// "rgba8snorm", // Also used in WGSL
// "rgba8unorm", // Also used in WGSL
// "sampler", // Also used in WGSL
"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", // Also used in WGSL
"ushort",
"ushort2",
"ushort3",
"ushort4",
// "vec", // WGSL reserved keyword
"vertex",
// https://developer.apple.com/metal/Metal-Shading-Language-Specification.pdf
// Table 6.5. Constants for single-precision floating-point math
// functions
"MAXFLOAT",
"HUGE_VALF",
"INFINITY",
"infinity",
"NAN",
"M_E_F",
"M_LOG2E_F",
"M_LOG10E_F",
"M_LN2_F",
"M_LN10_F",
"M_PI_F",
"M_PI_2_F",
"M_PI_4_F",
"M_1_PI_F",
"M_2_PI_F",
"M_2_SQRTPI_F",
"M_SQRT2_F",
"M_SQRT1_2_F",
"MAXHALF",
"HUGE_VALH",
"M_E_H",
"M_LOG2E_H",
"M_LOG10E_H",
"M_LN2_H",
"M_LN10_H",
"M_PI_H",
"M_PI_2_H",
"M_PI_4_H",
"M_1_PI_H",
"M_2_PI_H",
"M_2_SQRTPI_H",
"M_SQRT2_H",
"M_SQRT1_2_H"));
} // namespace
} // namespace transform

View File

@@ -15,6 +15,7 @@
#include "src/writer/hlsl/generator_impl.h"
#include <algorithm>
#include <cmath>
#include <iomanip>
#include <set>
#include <utility>
@@ -2474,7 +2475,14 @@ bool GeneratorImpl::EmitLiteral(std::ostream& out, ast::Literal* lit) {
if (auto* l = lit->As<ast::BoolLiteral>()) {
out << (l->IsTrue() ? "true" : "false");
} else if (auto* fl = lit->As<ast::FloatLiteral>()) {
out << FloatToString(fl->value()) << "f";
if (std::isinf(fl->value())) {
out << (fl->value() >= 0 ? "asfloat(0x7f800000u)"
: "asfloat(0xff800000u)");
} else if (std::isnan(fl->value())) {
out << "asfloat(0x7fc00000u)";
} else {
out << FloatToString(fl->value()) << "f";
}
} else if (auto* sl = lit->As<ast::SintLiteral>()) {
out << sl->value();
} else if (auto* ul = lit->As<ast::UintLiteral>()) {

View File

@@ -15,6 +15,7 @@
#include "src/writer/msl/generator_impl.h"
#include <algorithm>
#include <cmath>
#include <iomanip>
#include <utility>
#include <vector>
@@ -1086,7 +1087,13 @@ bool GeneratorImpl::EmitLiteral(std::ostream& out, ast::Literal* lit) {
if (auto* l = lit->As<ast::BoolLiteral>()) {
out << (l->IsTrue() ? "true" : "false");
} else if (auto* fl = lit->As<ast::FloatLiteral>()) {
out << FloatToString(fl->value()) << "f";
if (std::isinf(fl->value())) {
out << (fl->value() >= 0 ? "INFINITY" : "-INFINITY");
} else if (std::isnan(fl->value())) {
out << "NAN";
} else {
out << FloatToString(fl->value()) << "f";
}
} else if (auto* sl = lit->As<ast::SintLiteral>()) {
out << sl->value();
} else if (auto* ul = lit->As<ast::UintLiteral>()) {