[spirv-reader][ir] Convert local invocation index when needed.
In SPIR-V the local invocation index can be i32 or u32. In WGSL it must
be u32. Make sure we do any required conversions to match types.
Bug: 42250952
Change-Id: I5ea4e0ab5ea49b03af467da76db4cd502fdfeda7
Reviewed-on: https://dawn-review.googlesource.com/c/dawn/+/245655
Reviewed-by: James Price <jrprice@google.com>
Commit-Queue: dan sinclair <dsinclair@chromium.org>
diff --git a/src/tint/lang/spirv/reader/lower/shader_io.cc b/src/tint/lang/spirv/reader/lower/shader_io.cc
index ef6961f..6c0cde0 100644
--- a/src/tint/lang/spirv/reader/lower/shader_io.cc
+++ b/src/tint/lang/spirv/reader/lower/shader_io.cc
@@ -416,8 +416,9 @@
var_type = ty.u32();
break;
}
- case core::BuiltinValue::kInstanceIndex: {
- // Use a scalar u32 for instance_index builtin
+ case core::BuiltinValue::kInstanceIndex:
+ case core::BuiltinValue::kLocalInvocationIndex: {
+ // Use a scalar u32
var_type = ty.u32();
break;
}
@@ -477,7 +478,8 @@
}
break;
}
- case core::BuiltinValue::kInstanceIndex: {
+ case core::BuiltinValue::kInstanceIndex:
+ case core::BuiltinValue::kLocalInvocationIndex: {
auto* idx_ty = var->Result()->Type()->UnwrapPtr();
if (idx_ty->IsSignedIntegerScalar()) {
auto* conv = b.Convert(ty.i32(), result);
diff --git a/src/tint/lang/spirv/reader/lower/shader_io_test.cc b/src/tint/lang/spirv/reader/lower/shader_io_test.cc
index 79ca87d..c7d7672 100644
--- a/src/tint/lang/spirv/reader/lower/shader_io_test.cc
+++ b/src/tint/lang/spirv/reader/lower/shader_io_test.cc
@@ -2171,6 +2171,96 @@
EXPECT_EQ(expect, str());
}
+TEST_F(SpirvReader_ShaderIOTest, LocalInvocationIndex_i32) {
+ auto* idx = b.Var("idx", ty.ptr(core::AddressSpace::kIn, ty.i32()));
+ idx->SetBuiltin(core::BuiltinValue::kLocalInvocationIndex);
+ mod.root_block->Append(idx);
+
+ auto* ep = b.Function("foo", ty.void_(), core::ir::Function::PipelineStage::kCompute);
+ ep->SetWorkgroupSize(b.Constant(1_u), b.Constant(1_u), b.Constant(1_u));
+ b.Append(ep->Block(), [&] {
+ auto* idx_value = b.Load(idx);
+ b.Let("a", b.Multiply(ty.i32(), idx_value, 2_i));
+ b.Return(ep);
+ });
+
+ auto* src = R"(
+$B1: { # root
+ %idx:ptr<__in, i32, read> = var undef @builtin(local_invocation_index)
+}
+
+%foo = @compute @workgroup_size(1u, 1u, 1u) func():void {
+ $B2: {
+ %3:i32 = load %idx
+ %4:i32 = mul %3, 2i
+ %a:i32 = let %4
+ ret
+ }
+}
+)";
+ EXPECT_EQ(src, str());
+
+ auto* expect = R"(
+%foo = @compute @workgroup_size(1u, 1u, 1u) func(%idx:u32 [@local_invocation_index]):void {
+ $B1: {
+ %3:i32 = convert %idx
+ %4:i32 = mul %3, 2i
+ %a:i32 = let %4
+ ret
+ }
+}
+)";
+
+ Run(ShaderIO);
+
+ EXPECT_EQ(expect, str());
+}
+
+TEST_F(SpirvReader_ShaderIOTest, LocalInvocationIndex_u32) {
+ auto* idx = b.Var("idx", ty.ptr(core::AddressSpace::kIn, ty.u32()));
+ idx->SetBuiltin(core::BuiltinValue::kLocalInvocationIndex);
+ mod.root_block->Append(idx);
+
+ auto* ep = b.Function("foo", ty.void_(), core::ir::Function::PipelineStage::kCompute);
+ ep->SetWorkgroupSize(b.Constant(1_u), b.Constant(1_u), b.Constant(1_u));
+ b.Append(ep->Block(), [&] {
+ auto* idx_value = b.Load(idx);
+ b.Let("a", b.Multiply(ty.u32(), idx_value, 2_u));
+
+ b.Return(ep);
+ });
+
+ auto* src = R"(
+$B1: { # root
+ %idx:ptr<__in, u32, read> = var undef @builtin(local_invocation_index)
+}
+
+%foo = @compute @workgroup_size(1u, 1u, 1u) func():void {
+ $B2: {
+ %3:u32 = load %idx
+ %4:u32 = mul %3, 2u
+ %a:u32 = let %4
+ ret
+ }
+}
+)";
+ EXPECT_EQ(src, str());
+
+ auto* expect = R"(
+%foo = @compute @workgroup_size(1u, 1u, 1u) func(%idx:u32 [@local_invocation_index]):void {
+ $B1: {
+ %3:u32 = mul %idx, 2u
+ %a:u32 = let %3
+ ret
+ }
+}
+)";
+
+ Run(ShaderIO);
+
+ EXPECT_EQ(expect, str());
+}
+
// Test that a sample mask array is converted to a scalar u32 for the entry point.
TEST_F(SpirvReader_ShaderIOTest, SampleMask) {
auto* arr = ty.array<u32, 1>();