[tint] Fix unshadowing of abstract const users

Unwrap sem::Materialize as well as sem::Load when looking for users of
a renamed declaration.

Fixed: tint:1934
Change-Id: I7d5ade1f902dbdb4cf3b4f7552b78359f5b3ea9a
Reviewed-on: https://dawn-review.googlesource.com/c/dawn/+/132060
Commit-Queue: James Price <jrprice@google.com>
Kokoro: Kokoro <noreply+kokoro@google.com>
Reviewed-by: Ben Clayton <bclayton@google.com>
This commit is contained in:
James Price 2023-05-09 10:59:58 +00:00 committed by Dawn LUCI CQ
parent e162a1adee
commit 81fc109822
9 changed files with 99 additions and 1 deletions

View File

@ -108,7 +108,7 @@ struct Unshadow::State {
ctx.ReplaceAll(
[&](const ast::IdentifierExpression* ident) -> const tint::ast::IdentifierExpression* {
if (auto* sem_ident = sem.GetVal(ident)) {
if (auto* user = sem_ident->UnwrapLoad()->As<sem::VariableUser>()) {
if (auto* user = sem_ident->Unwrap()->As<sem::VariableUser>()) {
if (auto renamed = renamed_to.Find(user->Variable())) {
return b.Expr(*renamed);
}

View File

@ -779,5 +779,25 @@ fn F() {
EXPECT_EQ(expect, str(got));
}
TEST_F(UnshadowTest, RenamedAbstractConstHasUsers) {
auto* src = R"(
fn v() {
const v = 1;
let x = v;
}
)";
auto* expect = R"(
fn v() {
const v_1 = 1;
let x = v_1;
}
)";
auto got = Run<Unshadow>(src);
EXPECT_EQ(expect, str(got));
}
} // namespace
} // namespace tint::transform

View File

@ -0,0 +1,5 @@
fn v() {
const v = vec2(1);
let i = 1;
var b = v[i];
}

View File

@ -0,0 +1,9 @@
[numthreads(1, 1, 1)]
void unused_entry_point() {
return;
}
void v() {
const int i = 1;
int b = (1).xx[i];
}

View File

@ -0,0 +1,9 @@
[numthreads(1, 1, 1)]
void unused_entry_point() {
return;
}
void v() {
const int i = 1;
int b = (1).xx[i];
}

View File

@ -0,0 +1,11 @@
#version 310 es
layout(local_size_x = 1, local_size_y = 1, local_size_z = 1) in;
void unused_entry_point() {
return;
}
void v() {
int i = 1;
int b = ivec2(1)[i];
}

View File

@ -0,0 +1,8 @@
#include <metal_stdlib>
using namespace metal;
void v() {
int const i = 1;
int b = int2(1)[i];
}

View File

@ -0,0 +1,31 @@
; SPIR-V
; Version: 1.3
; Generator: Google Tint Compiler; 0
; Bound: 15
; Schema: 0
OpCapability Shader
OpMemoryModel Logical GLSL450
OpEntryPoint GLCompute %unused_entry_point "unused_entry_point"
OpExecutionMode %unused_entry_point LocalSize 1 1 1
OpName %unused_entry_point "unused_entry_point"
OpName %v "v"
OpName %b "b"
%void = OpTypeVoid
%1 = OpTypeFunction %void
%int = OpTypeInt 32 1
%int_1 = OpConstant %int 1
%v2int = OpTypeVector %int 2
%10 = OpConstantComposite %v2int %int_1 %int_1
%_ptr_Function_int = OpTypePointer Function %int
%14 = OpConstantNull %int
%unused_entry_point = OpFunction %void None %1
%4 = OpLabel
OpReturn
OpFunctionEnd
%v = OpFunction %void None %1
%6 = OpLabel
%b = OpVariable %_ptr_Function_int Function %14
%11 = OpVectorExtractDynamic %int %10 %int_1
OpStore %b %11
OpReturn
OpFunctionEnd

View File

@ -0,0 +1,5 @@
fn v() {
const v = vec2(1);
let i = 1;
var b = v[i];
}