Branch: refs/heads/main Home: https://github.com/WebKit/WebKit Commit: 98c11ccb36a75ad6e0dc02922a88b8b767842272 https://github.com/WebKit/WebKit/commit/98c11ccb36a75ad6e0dc02922a88b8b767842272 Author: Tadeu Zagallo <tzaga...@apple.com> Date: 2023-06-01 (Thu, 01 Jun 2023)
Changed paths: M Source/WebGPU/WGSL/AST/ASTExpression.h M Source/WebGPU/WGSL/AST/ASTIdentityExpression.h M Source/WebGPU/WGSL/GlobalVariableRewriter.cpp M Source/WebGPU/WGSL/Metal/MetalFunctionWriter.cpp A Source/WebGPU/WGSL/tests/valid/type-promotion.wgsl Log Message: ----------- [WGSL] Fix serialization of promoted types https://bugs.webkit.org/show_bug.cgi?id=257558 rdar://110075846 Reviewed by Myles C. Maxfield. The problem happens when we have an array of vectors of abstract int, which later gets promoted to something other than i32. This can only happen through consts or calls with an immediate value. The two simplest examples are: let x : array<vec2<f32>> = array(vec2(0)) f(array(vec2(0)), where f expects array<vec2<f32>> In both cases, the issues is the same. The type inferred for the expression `array(vec2(0))` is `array<vec2<abstract-int>>`. However, later we discover this value will be used as `array<vec2<f32>>`, which is a valid conversion (and currently supported by our type checker). The problem occurs at serialization time, since we don't update the inferred type of the nested expression `vec2(0)`, so we generate a vector or ints. This gets further complicated when we have a single constant used by two functions, and the constant gets promoted to two different types. e.g.: const a = array(vec2(0)); let x: array<vec2<u32>> = a; let y: array<vec2<f32>> = a; To solve this problem, we duplicate the constants' declaration at the time of use, and make sure the new instance has the correct type. Effectively transforming the above code into: const a1 : array<vec2<u32>> = array(vec2(0)); let x: array<vec2<u32>> = a1; const a2 : array<vec2<f32>> = array(vec2(0)); let y: array<vec2<f32>> = a2; Further, at code generation time, we handle the special case of arrays: this problem can only occur when we have an abstract compound type inside an array constructor. So, in the codegen, whenever we see an array constructor, we pass the information down that it's arguments should have the same type as the array element type. * Source/WebGPU/WGSL/AST/ASTExpression.h: * Source/WebGPU/WGSL/AST/ASTIdentityExpression.h: * Source/WebGPU/WGSL/GlobalVariableRewriter.cpp: (WGSL::RewriteGlobalVariables::visit): (WGSL::RewriteGlobalVariables::insertLocalDefinitions): (WGSL::RewriteGlobalVariables::readVariable): (WGSL::RewriteGlobalVariables::insertBeforeCurrentStatement): (WGSL::RewriteGlobalVariables::read): Deleted. * Source/WebGPU/WGSL/Metal/MetalFunctionWriter.cpp: (WGSL::Metal::FunctionDefinitionWriter::serializeVariable): (WGSL::Metal::FunctionDefinitionWriter::visit): (WGSL::Metal::visitArguments): * Source/WebGPU/WGSL/tests/valid/type-promotion.wgsl: Added. Canonical link: https://commits.webkit.org/264787@main _______________________________________________ webkit-changes mailing list webkit-changes@lists.webkit.org https://lists.webkit.org/mailman/listinfo/webkit-changes