Skip to content

Commit aee66d2

Browse files
amaioranoDawn LUCI CQ
authored andcommitted
HLSL-IR: Fix ArrayLengthFromUniform not applying
to arrayLength introduced by Robustness. As with AST, this transform must run after Robustness, as the latter may add arrayLength calls. Fixes dawn_end2end_tests: ClampedOOBDynamicBufferOffsetTests.CheckOOBAccess/* Bug: 375287154 Change-Id: I47d0ea5999d8d97c72365b4b993e571d6c4b5040 Reviewed-on: https://dawn-review.googlesource.com/c/dawn/+/212318 Auto-Submit: Antonio Maiorano <[email protected]> Reviewed-by: James Price <[email protected]> Commit-Queue: James Price <[email protected]>
1 parent 34a2d65 commit aee66d2

File tree

2 files changed

+65
-12
lines changed

2 files changed

+65
-12
lines changed

src/tint/lang/hlsl/writer/arraylength_test.cc

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -228,5 +228,58 @@ void foo() {
228228
)");
229229
}
230230

231+
TEST_F(HlslWriterTest, ArrayLength_Robustness) {
232+
auto* dst = b.Var("dst", ty.ptr(storage, ty.array<u32>()));
233+
dst->SetBindingPoint(0, 1);
234+
b.ir.root_block->Append(dst);
235+
auto* func = b.Function("foo", ty.void_(), core::ir::Function::PipelineStage::kFragment);
236+
b.Append(func->Block(), [&] {
237+
auto* access = b.Access(ty.ptr(storage, ty.u32()), dst, 0_u);
238+
b.Store(access, 123_u);
239+
b.Return(func);
240+
});
241+
242+
Options options;
243+
options.disable_robustness = false;
244+
ASSERT_TRUE(Generate(options)) << err_ << output_.hlsl;
245+
EXPECT_EQ(output_.hlsl, R"(
246+
RWByteAddressBuffer dst : register(u1);
247+
void foo() {
248+
uint v = 0u;
249+
dst.GetDimensions(v);
250+
dst.Store((0u + (uint(min(0u, ((v / 4u) - 1u))) * 4u)), 123u);
251+
}
252+
253+
)");
254+
}
255+
256+
TEST_F(HlslWriterTest, ArrayLength_RobustnessAndArrayLengthFromUniform) {
257+
auto* dst = b.Var("dst", ty.ptr(storage, ty.array<u32>()));
258+
dst->SetBindingPoint(0, 1);
259+
b.ir.root_block->Append(dst);
260+
auto* func = b.Function("foo", ty.void_(), core::ir::Function::PipelineStage::kFragment);
261+
b.Append(func->Block(), [&] {
262+
auto* access = b.Access(ty.ptr(storage, ty.u32()), dst, 0_u);
263+
b.Store(access, 123_u);
264+
b.Return(func);
265+
});
266+
267+
Options options;
268+
options.disable_robustness = false;
269+
options.array_length_from_uniform.ubo_binding = {30, 0};
270+
options.array_length_from_uniform.bindpoint_to_size_index[{0, 1}] = 0;
271+
ASSERT_TRUE(Generate(options)) << err_ << output_.hlsl;
272+
EXPECT_EQ(output_.hlsl, R"(
273+
RWByteAddressBuffer dst : register(u1);
274+
cbuffer cbuffer_tint_storage_buffer_sizes : register(b0, space30) {
275+
uint4 tint_storage_buffer_sizes[1];
276+
};
277+
void foo() {
278+
dst.Store((0u + (uint(min(0u, ((tint_storage_buffer_sizes[0u].x / 4u) - 1u))) * 4u)), 123u);
279+
}
280+
281+
)");
282+
}
283+
231284
} // namespace
232285
} // namespace tint::hlsl::writer

src/tint/lang/hlsl/writer/raise/raise.cc

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -76,17 +76,6 @@ Result<SuccessType> Raise(core::ir::Module& module, const Options& options) {
7676
PopulateBindingRelatedOptions(options, remapper_data, multiplanar_map,
7777
array_length_from_uniform_options);
7878

79-
{
80-
auto result = core::ir::transform::ArrayLengthFromUniform(
81-
module,
82-
BindingPoint{array_length_from_uniform_options.ubo_binding.group,
83-
array_length_from_uniform_options.ubo_binding.binding},
84-
array_length_from_uniform_options.bindpoint_to_size_index);
85-
if (result != Success) {
86-
return result.Failure();
87-
}
88-
}
89-
9079
RUN_TRANSFORM(core::ir::transform::BindingRemapper, module, remapper_data);
9180
RUN_TRANSFORM(core::ir::transform::MultiplanarExternalTexture, module, multiplanar_map);
9281

@@ -159,6 +148,18 @@ Result<SuccessType> Raise(core::ir::Module& module, const Options& options) {
159148
RUN_TRANSFORM(core::ir::transform::Robustness, module, config);
160149
}
161150

151+
// ArrayLengthFromUniform must run after Robustness, which introduces arrayLength calls.
152+
{
153+
auto result = core::ir::transform::ArrayLengthFromUniform(
154+
module,
155+
BindingPoint{array_length_from_uniform_options.ubo_binding.group,
156+
array_length_from_uniform_options.ubo_binding.binding},
157+
array_length_from_uniform_options.bindpoint_to_size_index);
158+
if (result != Success) {
159+
return result.Failure();
160+
}
161+
}
162+
162163
if (!options.disable_workgroup_init) {
163164
// Must run before ShaderIO as it may introduce a builtin parameter (local_invocation_index)
164165
RUN_TRANSFORM(core::ir::transform::ZeroInitWorkgroupMemory, module);
@@ -190,7 +191,6 @@ Result<SuccessType> Raise(core::ir::Module& module, const Options& options) {
190191
}
191192

192193
// TODO(dsinclair): TruncateInterstageVariables
193-
// TODO(dsinclair): CalculateArrayLength
194194

195195
// DemoteToHelper must come before any transform that introduces non-core instructions.
196196
// Run after ShaderIO to ensure the discards are added to the entry point it introduces.

0 commit comments

Comments
 (0)