Implement clamping of runtime array accesses
Bug: tint:252 Change-Id: I2b32ab9d69ca39b6178fc4e94ccd090516a37c98 Reviewed-on: https://dawn-review.googlesource.com/c/tint/+/36620 Reviewed-by: dan sinclair <dsinclair@chromium.org> Commit-Queue: dan sinclair <dsinclair@chromium.org>
This commit is contained in:
parent
6653c13a75
commit
40b4928a73
|
@ -14,6 +14,7 @@
|
||||||
|
|
||||||
#include "src/transform/bound_array_accessors.h"
|
#include "src/transform/bound_array_accessors.h"
|
||||||
|
|
||||||
|
#include <algorithm>
|
||||||
#include <memory>
|
#include <memory>
|
||||||
#include <utility>
|
#include <utility>
|
||||||
|
|
||||||
|
@ -22,6 +23,7 @@
|
||||||
#include "src/ast/bitcast_expression.h"
|
#include "src/ast/bitcast_expression.h"
|
||||||
#include "src/ast/block_statement.h"
|
#include "src/ast/block_statement.h"
|
||||||
#include "src/ast/break_statement.h"
|
#include "src/ast/break_statement.h"
|
||||||
|
#include "src/ast/builder.h"
|
||||||
#include "src/ast/call_expression.h"
|
#include "src/ast/call_expression.h"
|
||||||
#include "src/ast/call_statement.h"
|
#include "src/ast/call_statement.h"
|
||||||
#include "src/ast/case_statement.h"
|
#include "src/ast/case_statement.h"
|
||||||
|
@ -74,47 +76,47 @@ ast::ArrayAccessorExpression* BoundArrayAccessors::Transform(
|
||||||
return nullptr;
|
return nullptr;
|
||||||
}
|
}
|
||||||
|
|
||||||
uint32_t size = 0;
|
ast::Builder b(ctx->mod);
|
||||||
if (ret_type->Is<ast::type::Vector>() || ret_type->Is<ast::type::Array>()) {
|
using u32 = ast::Builder::u32;
|
||||||
size = ret_type->Is<ast::type::Vector>()
|
|
||||||
? ret_type->As<ast::type::Vector>()->size()
|
|
||||||
: ret_type->As<ast::type::Array>()->size();
|
|
||||||
if (size == 0) {
|
|
||||||
diag::Diagnostic err;
|
|
||||||
err.severity = diag::Severity::Error;
|
|
||||||
err.message = "invalid 0 size for array or vector";
|
|
||||||
err.source = expr->source();
|
|
||||||
diags->add(std::move(err));
|
|
||||||
return nullptr;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
uint32_t size = 0;
|
||||||
|
bool is_vec = ret_type->Is<ast::type::Vector>();
|
||||||
|
bool is_arr = ret_type->Is<ast::type::Array>();
|
||||||
|
if (is_vec || is_arr) {
|
||||||
|
size = is_vec ? ret_type->As<ast::type::Vector>()->size()
|
||||||
|
: ret_type->As<ast::type::Array>()->size();
|
||||||
} else {
|
} else {
|
||||||
// The row accessor would have been an embedded array accessor and already
|
// The row accessor would have been an embedded array accessor and already
|
||||||
// handled, so we just need to do columns here.
|
// handled, so we just need to do columns here.
|
||||||
size = ret_type->As<ast::type::Matrix>()->columns();
|
size = ret_type->As<ast::type::Matrix>()->columns();
|
||||||
}
|
}
|
||||||
|
|
||||||
ast::Expression* idx_expr = nullptr;
|
auto* const old_idx = expr->idx_expr();
|
||||||
|
b.SetSource(ctx->Clone(old_idx->source()));
|
||||||
|
|
||||||
// Scalar constructor we can re-write the value to be within bounds.
|
ast::Expression* new_idx = nullptr;
|
||||||
if (auto* c = expr->idx_expr()->As<ast::ScalarConstructorExpression>()) {
|
|
||||||
|
if (size == 0) {
|
||||||
|
if (is_arr) {
|
||||||
|
auto* arr_len = b.Call("arrayLength", ctx->Clone(expr->array()));
|
||||||
|
auto* limit = b.Sub(arr_len, b.Expr(1u));
|
||||||
|
new_idx = b.Call("min", b.Construct<u32>(ctx->Clone(old_idx)), limit);
|
||||||
|
} else {
|
||||||
|
diag::Diagnostic err;
|
||||||
|
err.severity = diag::Severity::Error;
|
||||||
|
err.message = "invalid 0 size";
|
||||||
|
err.source = expr->source();
|
||||||
|
diags->add(std::move(err));
|
||||||
|
return nullptr;
|
||||||
|
}
|
||||||
|
} else if (auto* c = old_idx->As<ast::ScalarConstructorExpression>()) {
|
||||||
|
// Scalar constructor we can re-write the value to be within bounds.
|
||||||
auto* lit = c->literal();
|
auto* lit = c->literal();
|
||||||
if (auto* sint = lit->As<ast::SintLiteral>()) {
|
if (auto* sint = lit->As<ast::SintLiteral>()) {
|
||||||
int32_t val = sint->value();
|
int32_t max = static_cast<int32_t>(size) - 1;
|
||||||
if (val < 0) {
|
new_idx = b.Expr(std::max(std::min(sint->value(), max), 0));
|
||||||
val = 0;
|
|
||||||
} else if (val >= int32_t(size)) {
|
|
||||||
val = int32_t(size) - 1;
|
|
||||||
}
|
|
||||||
lit = ctx->mod->create<ast::SintLiteral>(ctx->Clone(sint->source()),
|
|
||||||
ctx->Clone(sint->type()), val);
|
|
||||||
} else if (auto* uint = lit->As<ast::UintLiteral>()) {
|
} else if (auto* uint = lit->As<ast::UintLiteral>()) {
|
||||||
uint32_t val = uint->value();
|
new_idx = b.Expr(std::min(uint->value(), size - 1));
|
||||||
if (val >= size - 1) {
|
|
||||||
val = size - 1;
|
|
||||||
}
|
|
||||||
lit = ctx->mod->create<ast::UintLiteral>(ctx->Clone(uint->source()),
|
|
||||||
ctx->Clone(uint->type()), val);
|
|
||||||
} else {
|
} else {
|
||||||
diag::Diagnostic err;
|
diag::Diagnostic err;
|
||||||
err.severity = diag::Severity::Error;
|
err.severity = diag::Severity::Error;
|
||||||
|
@ -123,32 +125,13 @@ ast::ArrayAccessorExpression* BoundArrayAccessors::Transform(
|
||||||
diags->add(std::move(err));
|
diags->add(std::move(err));
|
||||||
return nullptr;
|
return nullptr;
|
||||||
}
|
}
|
||||||
idx_expr =
|
|
||||||
ctx->mod->create<ast::ScalarConstructorExpression>(c->source(), lit);
|
|
||||||
} else {
|
} else {
|
||||||
auto* u32 = ctx->mod->create<ast::type::U32>();
|
new_idx =
|
||||||
|
b.Call("min", b.Construct<u32>(ctx->Clone(old_idx)), b.Expr(size - 1));
|
||||||
ast::ExpressionList cast_expr;
|
|
||||||
cast_expr.push_back(ctx->Clone(expr->idx_expr()));
|
|
||||||
|
|
||||||
ast::ExpressionList params;
|
|
||||||
params.push_back(ctx->mod->create<ast::TypeConstructorExpression>(
|
|
||||||
Source{}, u32, cast_expr));
|
|
||||||
params.push_back(ctx->mod->create<ast::ScalarConstructorExpression>(
|
|
||||||
Source{}, ctx->mod->create<ast::UintLiteral>(Source{}, u32, size - 1)));
|
|
||||||
|
|
||||||
auto* call_expr = ctx->mod->create<ast::CallExpression>(
|
|
||||||
Source{},
|
|
||||||
ctx->mod->create<ast::IdentifierExpression>(
|
|
||||||
Source{}, ctx->mod->RegisterSymbol("min"), "min"),
|
|
||||||
std::move(params));
|
|
||||||
call_expr->set_result_type(u32);
|
|
||||||
|
|
||||||
idx_expr = call_expr;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return ctx->mod->create<ast::ArrayAccessorExpression>(
|
return b.create<ast::ArrayAccessorExpression>(
|
||||||
ctx->Clone(expr->source()), ctx->Clone(expr->array()), idx_expr);
|
ctx->Clone(expr->source()), ctx->Clone(expr->array()), new_idx);
|
||||||
}
|
}
|
||||||
|
|
||||||
} // namespace transform
|
} // namespace transform
|
||||||
|
|
|
@ -446,16 +446,35 @@ TEST_F(BoundArrayAccessorsTest, DISABLED_Matrix_Row_Constant_Id_Clamps) {
|
||||||
// -> var b : f32 = a[1][min(u32(idx), 0, 1)]
|
// -> var b : f32 = a[1][min(u32(idx), 0, 1)]
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO(dsinclair): Implement when we have arrayLength for Runtime Arrays
|
TEST_F(BoundArrayAccessorsTest, RuntimeArray_Clamps) {
|
||||||
TEST_F(BoundArrayAccessorsTest, DISABLED_RuntimeArray_Clamps) {
|
auto* src = R"(
|
||||||
// struct S {
|
struct S {
|
||||||
// a : f32;
|
a : f32;
|
||||||
// b : array<f32>;
|
b : array<f32>;
|
||||||
// }
|
};
|
||||||
// S s;
|
var s : S;
|
||||||
// var b : f32 = s.b[25]
|
|
||||||
//
|
fn f() -> void {
|
||||||
// -> var b : f32 = s.b[min(u32(25), arrayLength(s.b))]
|
var d : f32 = s.b[25];
|
||||||
|
}
|
||||||
|
)";
|
||||||
|
|
||||||
|
auto* expect = R"(
|
||||||
|
struct S {
|
||||||
|
a : f32;
|
||||||
|
b : array<f32>;
|
||||||
|
};
|
||||||
|
|
||||||
|
var s : S;
|
||||||
|
|
||||||
|
fn f() -> void {
|
||||||
|
var d : f32 = s.b[min(u32(25), (arrayLength(s.b) - 1u))];
|
||||||
|
}
|
||||||
|
)";
|
||||||
|
|
||||||
|
auto got = Transform<BoundArrayAccessors>(src);
|
||||||
|
|
||||||
|
EXPECT_EQ(expect, got);
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO(dsinclair): Clamp atomics when available.
|
// TODO(dsinclair): Clamp atomics when available.
|
||||||
|
|
Loading…
Reference in New Issue