summaryrefslogtreecommitdiffstats
path: root/dom/webgpu/tests/cts/checkout/src/webgpu/shader/validation/functions/alias_analysis.spec.ts
blob: ba39485449df63ba1d454b0c312a5fd4c4b6e2fd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
export const description = `Validation tests for function alias analysis`;

import { makeTestGroup } from '../../../../common/framework/test_group.js';
import { keysOf } from '../../../../common/util/data_tables.js';
import { ShaderValidationTest } from '../shader_validation_test.js';

export const g = makeTestGroup(ShaderValidationTest);

interface Use {
  is_write: boolean;
  gen: (ref: string) => string;
}

const kUses: Record<string, Use> = {
  no_access: { is_write: false, gen: ref => `{ let p = &*&${ref}; }` },
  assign: { is_write: true, gen: ref => `${ref} = 42;` },
  compound_assign_lhs: { is_write: true, gen: ref => `${ref} += 1;` },
  compound_assign_rhs: { is_write: false, gen: ref => `{ var tmp : i32; tmp += ${ref}; }` },
  increment: { is_write: true, gen: ref => `${ref}++;` },
  binary_lhs: { is_write: false, gen: ref => `_ = ${ref} + 1;` },
  binary_rhs: { is_write: false, gen: ref => `_ = 1 + ${ref};` },
  unary_minus: { is_write: false, gen: ref => `_ = -${ref};` },
  bitcast: { is_write: false, gen: ref => `_ = bitcast<f32>(${ref});` },
  convert: { is_write: false, gen: ref => `_ = f32(${ref});` },
  builtin_arg: { is_write: false, gen: ref => `_ = abs(${ref});` },
  index_access: { is_write: false, gen: ref => `{ var arr : array<i32, 4>; _ = arr[${ref}]; }` },
  let_init: { is_write: false, gen: ref => `{ let tmp = ${ref}; }` },
  var_init: { is_write: false, gen: ref => `{ var tmp = ${ref}; }` },
  return: { is_write: false, gen: ref => `{ return ${ref}; }` },
  switch_cond: { is_write: false, gen: ref => `switch(${ref}) { default { break; } }` },
};

type UseName = keyof typeof kUses;

function shouldPass(aliased: boolean, ...uses: UseName[]): boolean {
  // Expect fail if the pointers are aliased and at least one of the accesses is a write.
  // If either of the accesses is a "no access" then expect pass.
  return !aliased || !uses.some(u => kUses[u].is_write) || uses.includes('no_access');
}

g.test('two_pointers')
  .desc(`Test aliasing of two pointers passed to a function.`)
  .params(u =>
    u
      .combine('address_space', ['private', 'function'] as const)
      .combine('a_use', keysOf(kUses))
      .combine('b_use', keysOf(kUses))
      .combine('aliased', [true, false])
      .beginSubcases()
  )
  .fn(t => {
    const code = `
${t.params.address_space === 'private' ? `var<private> x : i32; var<private> y : i32;` : ``}

fn callee(pa : ptr<${t.params.address_space}, i32>,
          pb : ptr<${t.params.address_space}, i32>) -> i32 {
  ${kUses[t.params.a_use].gen(`*pa`)}
  ${kUses[t.params.b_use].gen(`*pb`)}
  return 0;
}

fn caller() {
  ${t.params.address_space === 'function' ? `var x : i32; var y : i32;` : ``}
  callee(&x, ${t.params.aliased ? `&x` : `&y`});
}
`;
    t.expectCompileResult(shouldPass(t.params.aliased, t.params.a_use, t.params.b_use), code);
  });

g.test('one_pointer_one_module_scope')
  .desc(`Test aliasing of a pointer with a direct access to a module-scope variable.`)
  .params(u =>
    u
      .combine('a_use', keysOf(kUses))
      .combine('b_use', keysOf(kUses))
      .combine('aliased', [true, false])
      .beginSubcases()
  )
  .fn(t => {
    const code = `
var<private> x : i32;
var<private> y : i32;

fn callee(pb : ptr<private, i32>) -> i32 {
  ${kUses[t.params.a_use].gen(`x`)}
  ${kUses[t.params.b_use].gen(`*pb`)}
  return 0;
}

fn caller() {
  callee(${t.params.aliased ? `&x` : `&y`});
}
`;
    t.expectCompileResult(shouldPass(t.params.aliased, t.params.a_use, t.params.b_use), code);
  });

g.test('subcalls')
  .desc(`Test aliasing of two pointers passed to a function, and then passed to other functions.`)
  .params(u =>
    u
      .combine('a_use', ['no_access', 'assign', 'binary_lhs'] as UseName[])
      .combine('b_use', ['no_access', 'assign', 'binary_lhs'] as UseName[])
      .combine('aliased', [true, false])
      .beginSubcases()
  )
  .fn(t => {
    const code = `
var<private> x : i32;
var<private> y : i32;

fn subcall_no_access(p : ptr<private, i32>) {
  let pp = &*p;
}

fn subcall_binary_lhs(p : ptr<private, i32>) -> i32 {
  return *p + 1;
}

fn subcall_assign(p : ptr<private, i32>) {
  *p = 42;
}

fn callee(pa : ptr<private, i32>, pb : ptr<private, i32>) -> i32 {
  let new_pa = &*pa;
  let new_pb = &*pb;
  subcall_${t.params.a_use}(new_pa);
  subcall_${t.params.b_use}(new_pb);
  return 0;
}

fn caller() {
  callee(&x, ${t.params.aliased ? `&x` : `&y`});
}
`;
    t.expectCompileResult(shouldPass(t.params.aliased, t.params.a_use, t.params.b_use), code);
  });

g.test('member_accessors')
  .desc(`Test aliasing of two pointers passed to a function and used with member accessors.`)
  .params(u =>
    u
      .combine('a_use', ['no_access', 'assign', 'binary_lhs'] as UseName[])
      .combine('b_use', ['no_access', 'assign', 'binary_lhs'] as UseName[])
      .combine('aliased', [true, false])
      .beginSubcases()
  )
  .fn(t => {
    const code = `
struct S { a : i32 }

var<private> x : S;
var<private> y : S;

fn callee(pa : ptr<private, S>,
          pb : ptr<private, S>) -> i32 {
  ${kUses[t.params.a_use].gen(`(*pa).a`)}
  ${kUses[t.params.b_use].gen(`(*pb).a`)}
  return 0;
}

fn caller() {
  callee(&x, ${t.params.aliased ? `&x` : `&y`});
}
`;
    t.expectCompileResult(shouldPass(t.params.aliased, t.params.a_use, t.params.b_use), code);
  });

g.test('same_pointer_read_and_write')
  .desc(`Test that we can read from and write to the same pointer.`)
  .params(u => u.beginSubcases())
  .fn(t => {
    const code = `
var<private> v : i32;

fn callee(p : ptr<private, i32>) {
  *p = *p + 1;
}

fn caller() {
  callee(&v);
}
`;
    t.expectCompileResult(true, code);
  });

g.test('aliasing_inside_function')
  .desc(`Test that we can alias pointers inside a function.`)
  .params(u => u.beginSubcases())
  .fn(t => {
    const code = `
var<private> v : i32;

fn foo() {
  var v : i32;
  let p1 = &v;
  let p2 = &v;
  *p1 = 42;
  *p2 = 42;
}
`;
    t.expectCompileResult(true, code);
  });