tint/transform: Fix array materialization when indexing with override

Fixed: tint:1697
Change-Id: I6de9ea520e8e8fcba281c8cf68ad77021eb3dd22
Reviewed-on: https://dawn-review.googlesource.com/c/dawn/+/104825
Reviewed-by: David Neto <dneto@google.com>
Commit-Queue: Ben Clayton <bclayton@google.com>
Kokoro: Kokoro <noreply+kokoro@google.com>
This commit is contained in:
Ben Clayton
2022-10-12 19:18:25 +00:00
committed by Dawn LUCI CQ
parent d6daefc379
commit ba384f0383
9 changed files with 146 additions and 0 deletions

View File

@@ -17,6 +17,8 @@
#include <functional>
#include "src/tint/program_builder.h"
#include "src/tint/sem/builtin.h"
#include "src/tint/sem/index_accessor_expression.h"
#include "src/tint/sem/variable.h"
TINT_INSTANTIATE_TYPEINFO(tint::transform::SubstituteOverride);
@@ -82,6 +84,25 @@ void SubstituteOverride::Run(CloneContext& ctx, const DataMap& config, DataMap&)
return ctx.dst->Const(src, sym, ty, ctor);
});
// Ensure that objects that are indexed with an override-expression are materialized.
// If the object is not materialized, and the 'override' variable is turned to a 'const', the
// resulting type of the index may change. See: crbug.com/tint/1697.
ctx.ReplaceAll(
[&](const ast::IndexAccessorExpression* expr) -> const ast::IndexAccessorExpression* {
if (auto* sem = ctx.src->Sem().Get(expr)) {
if (auto* access = sem->UnwrapMaterialize()->As<sem::IndexAccessorExpression>()) {
if (access->Object()->UnwrapMaterialize()->Type()->HoldsAbstract() &&
access->Index()->Stage() == sem::EvaluationStage::kOverride) {
auto& b = *ctx.dst;
auto* obj = b.Call(sem::str(sem::BuiltinType::kTintMaterialize),
ctx.Clone(expr->object));
return b.IndexAccessor(obj, ctx.Clone(expr->index));
}
}
}
return nullptr;
});
ctx.Clone();
}

View File

@@ -238,5 +238,46 @@ fn main() -> @builtin(position) vec4<f32> {
EXPECT_EQ(expect, str(got));
}
TEST_F(SubstituteOverrideTest, IndexMaterialization) {
auto* src = R"(
override O = 0; // Try switching to 'const'
fn f() {
const smaller_than_any_f32 = 1e-50;
const large_float = 1e27;
// When O is an override, the outer index value is not constant, so the
// value is not calculated at shader-creation time, and does not error.
//
// When O is a const, and 'smaller_than_any_f32' *is not* materialized, the
// outer index value will evaluate to 10000, resulting in an out-of-bounds
// error.
//
// When O is a const, and 'smaller_than_any_f32' *is* materialized, the
// materialization of 'smaller_than_any_f32' to f32 will evaluate to zero,
// and so the outer index value will be zero, and we get no error.
_ = vec2(0)[i32(vec2(smaller_than_any_f32)[O]*large_float*large_float)];
}
)";
auto* expect = R"(
const O = 0i;
fn f() {
const smaller_than_any_f32 = 1e-50;
const large_float = 1000000000000000013287555072.0;
_ = _tint_materialize(vec2(0))[i32(((_tint_materialize(vec2(smaller_than_any_f32))[O] * large_float) * large_float))];
}
)";
SubstituteOverride::Config cfg;
cfg.map.insert({OverrideId{0}, 0.0});
DataMap data;
data.Add<SubstituteOverride::Config>(cfg);
auto got = Run<SubstituteOverride>(src, data);
EXPECT_EQ(expect, str(got));
}
} // namespace
} // namespace tint::transform