[spirv-reader] Avoid name clashes with builtins

An OpName decoration for a result ID may end up unintentionally
shadowing a WGSL builtin function.

Fixed: tint:1932
Change-Id: I1b1302629c69b7bdf7a0a0c97d3ad6ce3d0d587b
Reviewed-on: https://dawn-review.googlesource.com/c/dawn/+/132080
Kokoro: Kokoro <noreply+kokoro@google.com>
Commit-Queue: James Price <jrprice@google.com>
Reviewed-by: Ben Clayton <bclayton@google.com>
Reviewed-by: John Stiles <johnstiles@google.com>
This commit is contained in:
James Price 2023-05-09 16:41:15 +00:00 committed by Dawn LUCI CQ
parent 567a53e87c
commit 6c18123058
10 changed files with 133 additions and 0 deletions

View File

@ -1807,6 +1807,7 @@ if (tint_build_unittests) {
] ]
deps = [ deps = [
":libtint_builtins_src",
":libtint_spv_reader_src", ":libtint_spv_reader_src",
":libtint_wgsl_writer_src", ":libtint_wgsl_writer_src",
"${tint_spirv_tools_dir}/:spvtools_opt", "${tint_spirv_tools_dir}/:spvtools_opt",

View File

@ -17,6 +17,7 @@
#include <algorithm> #include <algorithm>
#include <unordered_set> #include <unordered_set>
#include "src/tint/builtin/function.h"
#include "src/tint/debug.h" #include "src/tint/debug.h"
#include "src/tint/utils/string_stream.h" #include "src/tint/utils/string_stream.h"
@ -175,6 +176,9 @@ Namer::Namer(const FailStream& fail_stream) : fail_stream_(fail_stream) {
for (const auto* reserved : kWGSLReservedWords) { for (const auto* reserved : kWGSLReservedWords) {
name_to_id_[std::string(reserved)] = 0; name_to_id_[std::string(reserved)] = 0;
} }
for (const auto* builtin_function : builtin::kFunctionStrings) {
name_to_id_[std::string(builtin_function)] = 0;
}
} }
Namer::~Namer() = default; Namer::~Namer() = default;

View File

@ -15,6 +15,7 @@
#include "src/tint/reader/spirv/namer.h" #include "src/tint/reader/spirv/namer.h"
#include "gmock/gmock.h" #include "gmock/gmock.h"
#include "src/tint/builtin/function.h"
#include "src/tint/utils/string_stream.h" #include "src/tint/utils/string_stream.h"
namespace tint::reader::spirv { namespace tint::reader::spirv {
@ -393,5 +394,21 @@ INSTANTIATE_TEST_SUITE_P(SpvParserTest_ReservedWords,
"workgroup", "workgroup",
})); }));
using SpvNamerBuiltinFunctionTest = ::testing::TestWithParam<const char*>;
TEST_P(SpvNamerBuiltinFunctionTest, BuiltinFunctionsAreUsed) {
bool success;
utils::StringStream errors;
FailStream fail_stream(&success, &errors);
Namer namer(fail_stream);
const std::string builtin_fn = GetParam();
// Since it's a builtin function, it's marked as used, and we can't register an ID.
EXPECT_THAT(namer.FindUnusedDerivedName(builtin_fn), Eq(builtin_fn + "_1"));
}
INSTANTIATE_TEST_SUITE_P(SpvParserTest_BuiltinFunctions,
SpvNamerBuiltinFunctionTest,
::testing::ValuesIn(builtin::kFunctionStrings));
} // namespace } // namespace
} // namespace tint::reader::spirv } // namespace tint::reader::spirv

View File

@ -0,0 +1,18 @@
OpCapability ClipDistance
%1 = OpExtInstImport "GLSL.std.450"
OpMemoryModel Logical GLSL450
OpEntryPoint GLCompute %main "main"
OpExecutionMode %main LocalSize 1 1 1
OpName %distance "distance"
%void = OpTypeVoid
%void_fn = OpTypeFunction %void
%float = OpTypeFloat 32
%float2 = OpConstant %float 2.0
%v2float = OpTypeVector %float 2
%22 = OpConstantComposite %v2float %float2 %float2
%main = OpFunction %void None %void_fn
%start = OpLabel
%distance = OpCopyObject %v2float %22
%result = OpExtInst %float %1 Distance %distance %22
OpUnreachable
OpFunctionEnd

View File

@ -0,0 +1,11 @@
void main_1() {
const float2 distance_1 = (2.0f).xx;
const float x_10 = distance(distance_1, (2.0f).xx);
return;
}
[numthreads(1, 1, 1)]
void main() {
main_1();
return;
}

View File

@ -0,0 +1,11 @@
void main_1() {
const float2 distance_1 = (2.0f).xx;
const float x_10 = distance(distance_1, (2.0f).xx);
return;
}
[numthreads(1, 1, 1)]
void main() {
main_1();
return;
}

View File

@ -0,0 +1,17 @@
#version 310 es
void main_1() {
vec2 distance_1 = vec2(2.0f);
float x_10 = distance(distance_1, vec2(2.0f));
return;
}
void tint_symbol() {
main_1();
}
layout(local_size_x = 1, local_size_y = 1, local_size_z = 1) in;
void main() {
tint_symbol();
return;
}

View File

@ -0,0 +1,14 @@
#include <metal_stdlib>
using namespace metal;
void main_1() {
float2 const distance_1 = float2(2.0f);
float const x_10 = distance(distance_1, float2(2.0f));
return;
}
kernel void tint_symbol() {
main_1();
return;
}

View File

@ -0,0 +1,28 @@
; SPIR-V
; Version: 1.3
; Generator: Google Tint Compiler; 0
; Bound: 14
; Schema: 0
OpCapability Shader
%10 = OpExtInstImport "GLSL.std.450"
OpMemoryModel Logical GLSL450
OpEntryPoint GLCompute %main "main"
OpExecutionMode %main LocalSize 1 1 1
OpName %main_1 "main_1"
OpName %main "main"
%void = OpTypeVoid
%1 = OpTypeFunction %void
%float = OpTypeFloat 32
%v2float = OpTypeVector %float 2
%float_2 = OpConstant %float 2
%8 = OpConstantComposite %v2float %float_2 %float_2
%main_1 = OpFunction %void None %1
%4 = OpLabel
%9 = OpExtInst %float %10 Distance %8 %8
OpReturn
OpFunctionEnd
%main = OpFunction %void None %1
%12 = OpLabel
%13 = OpFunctionCall %void %main_1
OpReturn
OpFunctionEnd

View File

@ -0,0 +1,12 @@
const x_22 = vec2<f32>(2.0f, 2.0f);
fn main_1() {
let distance_1 : vec2<f32> = x_22;
let x_10 : f32 = distance(distance_1, x_22);
return;
}
@compute @workgroup_size(1i, 1i, 1i)
fn main() {
main_1();
}