transform::VarForDynamicIndex: Operate on matrices

Much like arrays, the SPIR-V writer cannot cope with dynamic indexing of matrices.

Fixed: tint:825
Change-Id: Ia111f15e0cf6fbd441861a4b3455a33b82b692ab
Reviewed-on: https://dawn-review.googlesource.com/c/tint/+/51781
Kokoro: Kokoro <noreply+kokoro@google.com>
Reviewed-by: David Neto <dneto@google.com>
Reviewed-by: James Price <jrprice@google.com>
Commit-Queue: Ben Clayton <bclayton@google.com>
Auto-Submit: Ben Clayton <bclayton@google.com>
This commit is contained in:
Ben Clayton
2021-05-20 18:16:07 +00:00
committed by Tint LUCI CQ
parent 6c582778cf
commit 351ac4a009
11 changed files with 186 additions and 52 deletions

View File

@@ -37,7 +37,7 @@ Output VarForDynamicIndex::Run(const Program* in, const DataMap&) {
if (auto* access_expr = node->As<ast::ArrayAccessorExpression>()) {
// Found an array accessor expression
auto* index_expr = access_expr->idx_expr();
auto* array_expr = access_expr->array();
auto* indexed_expr = access_expr->array();
if (index_expr->Is<ast::ScalarConstructorExpression>()) {
// Index expression is a literal value. As this isn't a dynamic index,
@@ -45,20 +45,20 @@ Output VarForDynamicIndex::Run(const Program* in, const DataMap&) {
continue;
}
auto* array = ctx.src->Sem().Get(array_expr);
if (!array->Type()->Is<sem::Array>()) {
// This transform currently only cares about arrays.
auto* indexed = ctx.src->Sem().Get(indexed_expr);
if (!indexed->Type()->IsAnyOf<sem::Array, sem::Matrix>()) {
// This transform currently only cares about array and matrices.
continue;
}
auto* stmt = array->Stmt(); // Statement that owns the expression
auto* block = stmt->Block(); // Block that owns the statement
auto* stmt = indexed->Stmt(); // Statement that owns the expression
auto* block = stmt->Block(); // Block that owns the statement
// Construct a `var` declaration to hold the value in memory.
auto* ty = CreateASTTypeFor(&ctx, array->Type());
auto var_name = ctx.dst->Symbols().New("var_for_array");
auto* ty = CreateASTTypeFor(&ctx, indexed->Type());
auto var_name = ctx.dst->Symbols().New("var_for_index");
auto* var_decl = ctx.dst->Decl(ctx.dst->Var(
var_name, ty, ast::StorageClass::kNone, ctx.Clone(array_expr)));
var_name, ty, ast::StorageClass::kNone, ctx.Clone(indexed_expr)));
// Insert the `var` declaration before the statement that performs the
// indexing. Note that for indexing chains, AST node ordering guarantees
@@ -67,7 +67,7 @@ Output VarForDynamicIndex::Run(const Program* in, const DataMap&) {
var_decl);
// Replace the original index expression with the new `var`.
ctx.Replace(array_expr, ctx.dst->Expr(var_name));
ctx.Replace(indexed_expr, ctx.dst->Expr(var_name));
}
}

View File

@@ -23,10 +23,10 @@
namespace tint {
namespace transform {
/// A transform that extracts array values that are dynamically indexed to a
/// temporary `var` local before performing the index. This transform is used by
/// the SPIR-V writer for dynamically indexing arrays, as there is no SPIR-V
/// instruction that can dynamically index a non-pointer composite.
/// A transform that extracts array and matrix values that are dynamically
/// indexed to a temporary `var` local before performing the index. This
/// transform is used by the SPIR-V writer as there is no SPIR-V instruction
/// that can dynamically index a non-pointer composite.
class VarForDynamicIndex : public Transform {
public:
/// Constructor

View File

@@ -44,8 +44,8 @@ fn f() {
fn f() {
var i : i32;
let p : array<i32, 4> = array<i32, 4>(1, 2, 3, 4);
var var_for_array : array<i32, 4> = p;
let x : i32 = var_for_array[i];
var var_for_index : array<i32, 4> = p;
let x : i32 = var_for_index[i];
}
)";
@@ -67,7 +67,7 @@ fn f() {
// TODO(bclayton): Optimize this case:
// This output is not as efficient as it could be.
// We only actually need to hoist the inner-most array to a `var`
// (`var_for_array`), as later indexing operations will be working with
// (`var_for_index`), as later indexing operations will be working with
// references, not values.
auto* expect = R"(
@@ -75,9 +75,9 @@ fn f() {
var i : i32;
var j : i32;
let p : array<array<i32, 2>, 2> = array<array<i32, 2>, 2>(array<i32, 2>(1, 2), array<i32, 2>(3, 4));
var var_for_array : array<array<i32, 2>, 2> = p;
var var_for_array_1 : array<i32, 2> = var_for_array[i];
let x : i32 = var_for_array_1[j];
var var_for_index : array<array<i32, 2>, 2> = p;
var var_for_index_1 : array<i32, 2> = var_for_index[i];
let x : i32 = var_for_index_1[j];
}
)";