[tint] Generate ArrayLengthFromUniform indices
Instead of hard-coding indices for a couple of binding points that
appear in our E2E tests, scan the AST for global variables that have
runtime-sized arrays and add indices for all of them.
Fixed: tint:2111
Change-Id: I4944ad73993d7a855d7ffaa982779b9c27c6fb39
Reviewed-on: https://dawn-review.googlesource.com/c/dawn/+/163520
Reviewed-by: Ben Clayton <bclayton@google.com>
Commit-Queue: James Price <jrprice@google.com>
Kokoro: Kokoro <noreply+kokoro@google.com>
diff --git a/src/tint/cmd/tint/main.cc b/src/tint/cmd/tint/main.cc
index 5ba2c6e..07bf73b 100644
--- a/src/tint/cmd/tint/main.cc
+++ b/src/tint/cmd/tint/main.cc
@@ -33,6 +33,7 @@
#include <string>
#include <unordered_map>
#include <vector>
+#include "src/tint/lang/wgsl/sem/variable.h"
#if TINT_BUILD_SPV_READER || TINT_BUILD_SPV_WRITER
#include "spirv-tools/libspirv.hpp"
@@ -763,10 +764,19 @@
gen_options.pixel_local_options = options.pixel_local_options;
gen_options.bindings = tint::msl::writer::GenerateBindings(*input_program);
gen_options.array_length_from_uniform.ubo_binding = tint::BindingPoint{0, 30};
- gen_options.array_length_from_uniform.bindpoint_to_size_index.emplace(tint::BindingPoint{0, 0},
- 0);
- gen_options.array_length_from_uniform.bindpoint_to_size_index.emplace(tint::BindingPoint{0, 1},
- 1);
+
+ // Add array_length_from_uniform entries for all storage buffers with runtime sized arrays.
+ std::unordered_set<tint::BindingPoint> storage_bindings;
+ for (auto* var : program.AST().GlobalVariables()) {
+ auto* sem_var = program.Sem().Get<tint::sem::GlobalVariable>(var);
+ if (!sem_var->Type()->UnwrapRef()->HasFixedFootprint()) {
+ auto bp = sem_var->Attributes().binding_point.value();
+ if (storage_bindings.insert(bp).second) {
+ gen_options.array_length_from_uniform.bindpoint_to_size_index.emplace(
+ bp, static_cast<uint32_t>(storage_bindings.size() - 1));
+ }
+ }
+ }
tint::Result<tint::msl::writer::Output> result;
if (options.use_ir) {
diff --git a/test/tint/builtins/gen/literal/arrayLength/1588cd.wgsl.expected.msl b/test/tint/builtins/gen/literal/arrayLength/1588cd.wgsl.expected.msl
index b42f4ee..cc50654 100644
--- a/test/tint/builtins/gen/literal/arrayLength/1588cd.wgsl.expected.msl
+++ b/test/tint/builtins/gen/literal/arrayLength/1588cd.wgsl.expected.msl
@@ -23,7 +23,7 @@
};
void arrayLength_1588cd(const constant tint_symbol_1* const tint_symbol_3, device uint* const tint_symbol_4) {
- uint res = (((*(tint_symbol_3)).buffer_size[0u][1u] - 0u) / 4u);
+ uint res = (((*(tint_symbol_3)).buffer_size[0u][0u] - 0u) / 4u);
*(tint_symbol_4) = res;
}
diff --git a/test/tint/builtins/gen/literal/arrayLength/8421b9.wgsl.expected.msl b/test/tint/builtins/gen/literal/arrayLength/8421b9.wgsl.expected.msl
index b59b772..d1d5766 100644
--- a/test/tint/builtins/gen/literal/arrayLength/8421b9.wgsl.expected.msl
+++ b/test/tint/builtins/gen/literal/arrayLength/8421b9.wgsl.expected.msl
@@ -23,7 +23,7 @@
};
void arrayLength_8421b9(const constant tint_symbol_1* const tint_symbol_3, device uint* const tint_symbol_4) {
- uint res = (((*(tint_symbol_3)).buffer_size[0u][1u] - 0u) / 2u);
+ uint res = (((*(tint_symbol_3)).buffer_size[0u][0u] - 0u) / 2u);
*(tint_symbol_4) = res;
}
diff --git a/test/tint/builtins/gen/literal/arrayLength/a0f5ca.wgsl.expected.msl b/test/tint/builtins/gen/literal/arrayLength/a0f5ca.wgsl.expected.msl
index a4dab46..41edcde 100644
--- a/test/tint/builtins/gen/literal/arrayLength/a0f5ca.wgsl.expected.msl
+++ b/test/tint/builtins/gen/literal/arrayLength/a0f5ca.wgsl.expected.msl
@@ -23,7 +23,7 @@
};
void arrayLength_a0f5ca(const constant tint_symbol_1* const tint_symbol_3, device uint* const tint_symbol_4) {
- uint res = (((*(tint_symbol_3)).buffer_size[0u][1u] - 0u) / 4u);
+ uint res = (((*(tint_symbol_3)).buffer_size[0u][0u] - 0u) / 4u);
*(tint_symbol_4) = res;
}
diff --git a/test/tint/builtins/gen/literal/arrayLength/cfca0a.wgsl.expected.msl b/test/tint/builtins/gen/literal/arrayLength/cfca0a.wgsl.expected.msl
index 039995a..aa0827f 100644
--- a/test/tint/builtins/gen/literal/arrayLength/cfca0a.wgsl.expected.msl
+++ b/test/tint/builtins/gen/literal/arrayLength/cfca0a.wgsl.expected.msl
@@ -23,7 +23,7 @@
};
void arrayLength_cfca0a(const constant tint_symbol_1* const tint_symbol_3, device uint* const tint_symbol_4) {
- uint res = (((*(tint_symbol_3)).buffer_size[0u][1u] - 0u) / 4u);
+ uint res = (((*(tint_symbol_3)).buffer_size[0u][0u] - 0u) / 4u);
*(tint_symbol_4) = res;
}
diff --git a/test/tint/builtins/gen/var/arrayLength/1588cd.wgsl.expected.msl b/test/tint/builtins/gen/var/arrayLength/1588cd.wgsl.expected.msl
index b42f4ee..cc50654 100644
--- a/test/tint/builtins/gen/var/arrayLength/1588cd.wgsl.expected.msl
+++ b/test/tint/builtins/gen/var/arrayLength/1588cd.wgsl.expected.msl
@@ -23,7 +23,7 @@
};
void arrayLength_1588cd(const constant tint_symbol_1* const tint_symbol_3, device uint* const tint_symbol_4) {
- uint res = (((*(tint_symbol_3)).buffer_size[0u][1u] - 0u) / 4u);
+ uint res = (((*(tint_symbol_3)).buffer_size[0u][0u] - 0u) / 4u);
*(tint_symbol_4) = res;
}
diff --git a/test/tint/builtins/gen/var/arrayLength/8421b9.wgsl.expected.msl b/test/tint/builtins/gen/var/arrayLength/8421b9.wgsl.expected.msl
index b59b772..d1d5766 100644
--- a/test/tint/builtins/gen/var/arrayLength/8421b9.wgsl.expected.msl
+++ b/test/tint/builtins/gen/var/arrayLength/8421b9.wgsl.expected.msl
@@ -23,7 +23,7 @@
};
void arrayLength_8421b9(const constant tint_symbol_1* const tint_symbol_3, device uint* const tint_symbol_4) {
- uint res = (((*(tint_symbol_3)).buffer_size[0u][1u] - 0u) / 2u);
+ uint res = (((*(tint_symbol_3)).buffer_size[0u][0u] - 0u) / 2u);
*(tint_symbol_4) = res;
}
diff --git a/test/tint/builtins/gen/var/arrayLength/a0f5ca.wgsl.expected.msl b/test/tint/builtins/gen/var/arrayLength/a0f5ca.wgsl.expected.msl
index a4dab46..41edcde 100644
--- a/test/tint/builtins/gen/var/arrayLength/a0f5ca.wgsl.expected.msl
+++ b/test/tint/builtins/gen/var/arrayLength/a0f5ca.wgsl.expected.msl
@@ -23,7 +23,7 @@
};
void arrayLength_a0f5ca(const constant tint_symbol_1* const tint_symbol_3, device uint* const tint_symbol_4) {
- uint res = (((*(tint_symbol_3)).buffer_size[0u][1u] - 0u) / 4u);
+ uint res = (((*(tint_symbol_3)).buffer_size[0u][0u] - 0u) / 4u);
*(tint_symbol_4) = res;
}
diff --git a/test/tint/builtins/gen/var/arrayLength/cfca0a.wgsl.expected.msl b/test/tint/builtins/gen/var/arrayLength/cfca0a.wgsl.expected.msl
index 039995a..aa0827f 100644
--- a/test/tint/builtins/gen/var/arrayLength/cfca0a.wgsl.expected.msl
+++ b/test/tint/builtins/gen/var/arrayLength/cfca0a.wgsl.expected.msl
@@ -23,7 +23,7 @@
};
void arrayLength_cfca0a(const constant tint_symbol_1* const tint_symbol_3, device uint* const tint_symbol_4) {
- uint res = (((*(tint_symbol_3)).buffer_size[0u][1u] - 0u) / 4u);
+ uint res = (((*(tint_symbol_3)).buffer_size[0u][0u] - 0u) / 4u);
*(tint_symbol_4) = res;
}