Skip to content

Commit

Permalink
More readonly and more fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
kainino0x committed Oct 25, 2023
1 parent ae43985 commit 70c3b03
Show file tree
Hide file tree
Showing 31 changed files with 368 additions and 296 deletions.
13 changes: 7 additions & 6 deletions src/common/framework/params_builder.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Merged, mergeParams, mergeParamsChecked } from '../internal/params_utils.js';
import { comparePublicParamsPaths, Ordering } from '../internal/query/compare.js';
import { stringifyPublicParams } from '../internal/query/stringify_params.js';
import { DeepReadonly } from '../util/types.js';
import { assert, mapLazy, objectEquals } from '../util/util.js';

import { TestParams } from './fixture.js';
Expand Down Expand Up @@ -98,7 +99,7 @@ export type ParamTypeOf<
* - `[case params, undefined]` if not.
*/
export type CaseSubcaseIterable<CaseP, SubcaseP> = Iterable<
readonly [CaseP, Iterable<SubcaseP> | undefined]
readonly [DeepReadonly<CaseP>, Iterable<DeepReadonly<SubcaseP>> | undefined]
>;

/**
Expand Down Expand Up @@ -143,7 +144,7 @@ export function builderIterateCasesWithSubcases(
*/
export class CaseParamsBuilder<CaseP extends {}>
extends ParamsBuilderBase<CaseP, {}>
implements Iterable<Readonly<CaseP>>, ParamsBuilder {
implements Iterable<DeepReadonly<CaseP>>, ParamsBuilder {
*iterateCasesWithSubcases(caseFilter: TestParams | null): CaseSubcaseIterable<CaseP, {}> {
for (const caseP of this.cases(caseFilter)) {
if (caseFilter) {
Expand All @@ -155,12 +156,12 @@ export class CaseParamsBuilder<CaseP extends {}>
}
}

yield [caseP, undefined];
yield [caseP as DeepReadonly<CaseP>, undefined];
}
}

[Symbol.iterator](): Iterator<Readonly<CaseP>> {
return this.cases(null);
[Symbol.iterator](): Iterator<DeepReadonly<CaseP>> {
return this.cases(null) as Iterator<DeepReadonly<CaseP>>;
}

/** @inheritDoc */
Expand Down Expand Up @@ -302,7 +303,7 @@ export class SubcaseParamsBuilder<CaseP extends {}, SubcaseP extends {}>

const subcases = Array.from(this.subcases(caseP));
if (subcases.length) {
yield [caseP, subcases];
yield [caseP as DeepReadonly<CaseP>, subcases as DeepReadonly<SubcaseP>[]];
}
}
}
Expand Down
36 changes: 26 additions & 10 deletions src/common/util/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,35 @@ export type TypeEqual<X, Y> = (<T>() => T extends X ? 1 : 2) extends <T>() => T
/* eslint-disable-next-line @typescript-eslint/no-unused-vars */
export function assertTypeTrue<T extends true>() {}

export type DeepReadonly<T> = T extends (infer R)[]
? DeepReadonlyArray<R>
: T extends Function
export type Primitive = string | number | boolean | undefined | null | Function | symbol;
export type DeepReadonly<T> = T extends [infer A]
? DeepReadonlyObject<[A]>
: T extends [infer A, infer B]
? DeepReadonlyObject<[A, B]>
: T extends [infer A, infer B, infer C]
? DeepReadonlyObject<[A, B, C]>
: T extends [infer A, infer B, infer C, infer D]
? DeepReadonlyObject<[A, B, C, D]>
: T extends [infer A, infer B, infer C, infer D, infer E]
? DeepReadonlyObject<[A, B, C, D, E]>
: T extends [infer A, infer B, infer C, infer D, infer E, infer F]
? DeepReadonlyObject<[A, B, C, D, E, F]>
: T extends [infer A, infer B, infer C, infer D, infer E, infer F, infer G]
? DeepReadonlyObject<[A, B, C, D, E, F, G]>
: T extends Map<infer U, infer V>
? ReadonlyMap<DeepReadonlyObject<U>, DeepReadonlyObject<V>>
: T extends Set<infer U>
? ReadonlySet<DeepReadonlyObject<U>>
: T extends Promise<infer U>
? Promise<DeepReadonlyObject<U>>
: T extends Primitive
? T
: T extends object
? DeepReadonlyObject<T>
: T;
: T extends (infer A)[]
? DeepReadonlyArray<A>
: DeepReadonlyObject<T>;

type DeepReadonlyArray<T> = ReadonlyArray<DeepReadonly<T>>;

type DeepReadonlyObject<T> = {
readonly [P in keyof T]: DeepReadonly<T[P]>;
};
type DeepReadonlyObject<T> = { readonly [P in keyof T]: DeepReadonly<T[P]> };

/**
* Computes the intersection of a set of types, given the union of those types.
Expand Down
2 changes: 1 addition & 1 deletion src/common/util/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -346,7 +346,7 @@ interface TypedArrayMap {

type TypedArrayParam<K extends keyof TypedArrayMap> = {
type: K;
data: number[];
data: readonly number[];
};

/**
Expand Down
4 changes: 2 additions & 2 deletions src/unittests/maths.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,8 @@ function withinOneULPF32(got: number, expected: number, mode: FlushMode): boolea
* FTZ occur during comparison
**/
function compareArrayOfNumbersF32(
got: Array<number>,
expect: Array<number>,
got: readonly number[],
expect: readonly number[],
mode: FlushMode = 'flush'
): boolean {
return (
Expand Down
4 changes: 2 additions & 2 deletions src/webgpu/api/validation/render_pipeline/inter_stage.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ function getVarName(i: number) {
}

class InterStageMatchingValidationTest extends CreateRenderPipelineValidationTest {
getVertexStateWithOutputs(outputs: string[]): GPUVertexState {
getVertexStateWithOutputs(outputs: readonly string[]): GPUVertexState {
return {
module: this.device.createShaderModule({
code: `
Expand All @@ -32,7 +32,7 @@ class InterStageMatchingValidationTest extends CreateRenderPipelineValidationTes
}

getFragmentStateWithInputs(
inputs: string[],
inputs: readonly string[],
hasBuiltinPosition: boolean = false
): GPUFragmentState {
return {
Expand Down
2 changes: 1 addition & 1 deletion src/webgpu/format_info.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1255,7 +1255,7 @@ export const kFeaturesForFormats = getFeaturesForFormats(kTextureFormats);
/**
* Given an array of texture formats return the number of bytes per sample.
*/
export function computeBytesPerSampleFromFormats(formats: GPUTextureFormat[]) {
export function computeBytesPerSampleFromFormats(formats: readonly GPUTextureFormat[]) {
let bytesPerSample = 0;
for (const format of formats) {
const info = kTextureFormatInfo[format];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@ import { onlyConstInputSource, run } from '../expression.js';

import { abstractBinary } from './binary.js';

const additionVectorScalarInterval = (v: number[], s: number): FPVector => {
const additionVectorScalarInterval = (v: readonly number[], s: number): FPVector => {
return FP.abstract.toVector(v.map(e => FP.abstract.additionInterval(e, s)));
};

const additionScalarVectorInterval = (s: number, v: number[]): FPVector => {
const additionScalarVectorInterval = (s: number, v: readonly number[]): FPVector => {
return FP.abstract.toVector(v.map(e => FP.abstract.additionInterval(s, e)));
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@ import { onlyConstInputSource, run } from '../expression.js';

import { abstractBinary } from './binary.js';

const divisionVectorScalarInterval = (v: number[], s: number): FPVector => {
const divisionVectorScalarInterval = (v: readonly number[], s: number): FPVector => {
return FP.abstract.toVector(v.map(e => FP.abstract.divisionInterval(e, s)));
};

const divisionScalarVectorInterval = (s: number, v: number[]): FPVector => {
const divisionScalarVectorInterval = (s: number, v: readonly number[]): FPVector => {
return FP.abstract.toVector(v.map(e => FP.abstract.divisionInterval(s, e)));
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@ import { onlyConstInputSource, run } from '../expression.js';

import { abstractBinary } from './binary.js';

const multiplicationVectorScalarInterval = (v: number[], s: number): FPVector => {
const multiplicationVectorScalarInterval = (v: readonly number[], s: number): FPVector => {
return FP.abstract.toVector(v.map(e => FP.abstract.multiplicationInterval(e, s)));
};

const multiplicationScalarVectorInterval = (s: number, v: number[]): FPVector => {
const multiplicationScalarVectorInterval = (s: number, v: readonly number[]): FPVector => {
return FP.abstract.toVector(v.map(e => FP.abstract.multiplicationInterval(s, e)));
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@ import { onlyConstInputSource, run } from '../expression.js';

import { abstractBinary } from './binary.js';

const remainderVectorScalarInterval = (v: number[], s: number): FPVector => {
const remainderVectorScalarInterval = (v: readonly number[], s: number): FPVector => {
return FP.abstract.toVector(v.map(e => FP.abstract.remainderInterval(e, s)));
};

const remainderScalarVectorInterval = (s: number, v: number[]): FPVector => {
const remainderScalarVectorInterval = (s: number, v: readonly number[]): FPVector => {
return FP.abstract.toVector(v.map(e => FP.abstract.remainderInterval(s, e)));
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@ import { onlyConstInputSource, run } from '../expression.js';

import { abstractBinary } from './binary.js';

const subtractionVectorScalarInterval = (v: number[], s: number): FPVector => {
const subtractionVectorScalarInterval = (v: readonly number[], s: number): FPVector => {
return FP.abstract.toVector(v.map(e => FP.abstract.subtractionInterval(e, s)));
};

const subtractionScalarVectorInterval = (s: number, v: number[]): FPVector => {
const subtractionScalarVectorInterval = (s: number, v: readonly number[]): FPVector => {
return FP.abstract.toVector(v.map(e => FP.abstract.subtractionInterval(s, e)));
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@ import { allInputSources, run } from '../expression.js';

import { binary, compoundBinary } from './binary.js';

const additionVectorScalarInterval = (v: number[], s: number): FPVector => {
const additionVectorScalarInterval = (v: readonly number[], s: number): FPVector => {
return FP.f16.toVector(v.map(e => FP.f16.additionInterval(e, s)));
};

const additionScalarVectorInterval = (s: number, v: number[]): FPVector => {
const additionScalarVectorInterval = (s: number, v: readonly number[]): FPVector => {
return FP.f16.toVector(v.map(e => FP.f16.additionInterval(s, e)));
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@ import { allInputSources, run } from '../expression.js';

import { binary, compoundBinary } from './binary.js';

const divisionVectorScalarInterval = (v: number[], s: number): FPVector => {
const divisionVectorScalarInterval = (v: readonly number[], s: number): FPVector => {
return FP.f16.toVector(v.map(e => FP.f16.divisionInterval(e, s)));
};

const divisionScalarVectorInterval = (s: number, v: number[]): FPVector => {
const divisionScalarVectorInterval = (s: number, v: readonly number[]): FPVector => {
return FP.f16.toVector(v.map(e => FP.f16.divisionInterval(s, e)));
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@ import { allInputSources, run } from '../expression.js';

import { binary, compoundBinary } from './binary.js';

const multiplicationVectorScalarInterval = (v: number[], s: number): FPVector => {
const multiplicationVectorScalarInterval = (v: readonly number[], s: number): FPVector => {
return FP.f16.toVector(v.map(e => FP.f16.multiplicationInterval(e, s)));
};

const multiplicationScalarVectorInterval = (s: number, v: number[]): FPVector => {
const multiplicationScalarVectorInterval = (s: number, v: readonly number[]): FPVector => {
return FP.f16.toVector(v.map(e => FP.f16.multiplicationInterval(s, e)));
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@ import { allInputSources, run } from '../expression.js';

import { binary, compoundBinary } from './binary.js';

const remainderVectorScalarInterval = (v: number[], s: number): FPVector => {
const remainderVectorScalarInterval = (v: readonly number[], s: number): FPVector => {
return FP.f16.toVector(v.map(e => FP.f16.remainderInterval(e, s)));
};

const remainderScalarVectorInterval = (s: number, v: number[]): FPVector => {
const remainderScalarVectorInterval = (s: number, v: readonly number[]): FPVector => {
return FP.f16.toVector(v.map(e => FP.f16.remainderInterval(s, e)));
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@ import { allInputSources, run } from '../expression.js';

import { binary, compoundBinary } from './binary.js';

const subtractionVectorScalarInterval = (v: number[], s: number): FPVector => {
const subtractionVectorScalarInterval = (v: readonly number[], s: number): FPVector => {
return FP.f16.toVector(v.map(e => FP.f16.subtractionInterval(e, s)));
};

const subtractionScalarVectorInterval = (s: number, v: number[]): FPVector => {
const subtractionScalarVectorInterval = (s: number, v: readonly number[]): FPVector => {
return FP.f16.toVector(v.map(e => FP.f16.subtractionInterval(s, e)));
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@ import { allInputSources, run } from '../expression.js';

import { binary, compoundBinary } from './binary.js';

const additionVectorScalarInterval = (v: number[], s: number): FPVector => {
const additionVectorScalarInterval = (v: readonly number[], s: number): FPVector => {
return FP.f32.toVector(v.map(e => FP.f32.additionInterval(e, s)));
};

const additionScalarVectorInterval = (s: number, v: number[]): FPVector => {
const additionScalarVectorInterval = (s: number, v: readonly number[]): FPVector => {
return FP.f32.toVector(v.map(e => FP.f32.additionInterval(s, e)));
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@ import { allInputSources, run } from '../expression.js';

import { binary, compoundBinary } from './binary.js';

const divisionVectorScalarInterval = (v: number[], s: number): FPVector => {
const divisionVectorScalarInterval = (v: readonly number[], s: number): FPVector => {
return FP.f32.toVector(v.map(e => FP.f32.divisionInterval(e, s)));
};

const divisionScalarVectorInterval = (s: number, v: number[]): FPVector => {
const divisionScalarVectorInterval = (s: number, v: readonly number[]): FPVector => {
return FP.f32.toVector(v.map(e => FP.f32.divisionInterval(s, e)));
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@ import { allInputSources, run } from '../expression.js';

import { binary, compoundBinary } from './binary.js';

const multiplicationVectorScalarInterval = (v: number[], s: number): FPVector => {
const multiplicationVectorScalarInterval = (v: readonly number[], s: number): FPVector => {
return FP.f32.toVector(v.map(e => FP.f32.multiplicationInterval(e, s)));
};

const multiplicationScalarVectorInterval = (s: number, v: number[]): FPVector => {
const multiplicationScalarVectorInterval = (s: number, v: readonly number[]): FPVector => {
return FP.f32.toVector(v.map(e => FP.f32.multiplicationInterval(s, e)));
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@ import { allInputSources, run } from '../expression.js';

import { binary, compoundBinary } from './binary.js';

const remainderVectorScalarInterval = (v: number[], s: number): FPVector => {
const remainderVectorScalarInterval = (v: readonly number[], s: number): FPVector => {
return FP.f32.toVector(v.map(e => FP.f32.remainderInterval(e, s)));
};

const remainderScalarVectorInterval = (s: number, v: number[]): FPVector => {
const remainderScalarVectorInterval = (s: number, v: readonly number[]): FPVector => {
return FP.f32.toVector(v.map(e => FP.f32.remainderInterval(s, e)));
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@ import { allInputSources, run } from '../expression.js';

import { binary, compoundBinary } from './binary.js';

const subtractionVectorScalarInterval = (v: number[], s: number): FPVector => {
const subtractionVectorScalarInterval = (v: readonly number[], s: number): FPVector => {
return FP.f32.toVector(v.map(e => FP.f32.subtractionInterval(e, s)));
};

const subtractionScalarVectorInterval = (s: number, v: number[]): FPVector => {
const subtractionScalarVectorInterval = (s: number, v: readonly number[]): FPVector => {
return FP.f32.toVector(v.map(e => FP.f32.subtractionInterval(s, e)));
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ const f16ZerosInterval: FPInterval = new FPInterval('f16', -0.0, 0.0);
* @returns an u32 whose lower and higher 16bits are the two elements of the
* given array of two u16 respectively, in little-endian.
*/
function u16x2ToU32(u16x2: number[]): number {
function u16x2ToU32(u16x2: readonly number[]): number {
assert(u16x2.length === 2);
// Create a DataView with 4 bytes buffer.
const buffer = new ArrayBuffer(4);
Expand Down Expand Up @@ -531,7 +531,7 @@ function possible32BitScalarIntervalsFromF16x2(
}
const possibleU16Bits = f16x2InU16x2.map(possibleBitsInU16FromFiniteF16InU16);
const possibleExpectations = cartesianProduct(...possibleU16Bits).flatMap<Scalar | FPInterval>(
(possibleBitsU16x2: number[]) => {
(possibleBitsU16x2: readonly number[]) => {
assert(possibleBitsU16x2.length === 2);
return expectationsForValue(reinterpretFromU32(u16x2ToU32(possibleBitsU16x2)));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ function generateIntegerTestCases(
}

function generateFloatTestCases(
test_values: Array<number>,
test_values: readonly number[],
trait: 'f32' | 'f16' | 'abstract',
stage: 'const' | 'non-const'
): Array<Case> {
Expand Down
Loading

0 comments on commit 70c3b03

Please sign in to comment.