| 1 | /* |
| 2 | * Copyright (C) 2019 Apple Inc. All rights reserved. |
| 3 | * |
| 4 | * Redistribution and use in source and binary forms, with or without |
| 5 | * modification, are permitted provided that the following conditions |
| 6 | * are met: |
| 7 | * 1. Redistributions of source code must retain the above copyright |
| 8 | * notice, this list of conditions and the following disclaimer. |
| 9 | * 2. Redistributions in binary form must reproduce the above copyright |
| 10 | * notice, this list of conditions and the following disclaimer in the |
| 11 | * documentation and/or other materials provided with the distribution. |
| 12 | * |
| 13 | * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS'' |
| 14 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, |
| 15 | * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
| 16 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS |
| 17 | * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 18 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 19 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 20 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
| 21 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 22 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF |
| 23 | * THE POSSIBILITY OF SUCH DAMAGE. |
| 24 | */ |
| 25 | |
| 26 | #include "config.h" |
| 27 | #include "WHLSLResolveOverloadImpl.h" |
| 28 | |
| 29 | #if ENABLE(WEBGPU) |
| 30 | |
| 31 | #include "WHLSLFunctionDeclaration.h" |
| 32 | #include "WHLSLFunctionDefinition.h" |
| 33 | #include "WHLSLInferTypes.h" |
| 34 | #include <limits> |
| 35 | |
| 36 | namespace WebCore { |
| 37 | |
| 38 | namespace WHLSL { |
| 39 | |
| 40 | static unsigned conversionCost(AST::FunctionDeclaration& candidate, const Vector<std::reference_wrapper<ResolvingType>>& argumentTypes) |
| 41 | { |
| 42 | unsigned conversionCost = 0; |
| 43 | for (size_t i = 0; i < candidate.parameters().size(); ++i) { |
| 44 | conversionCost += argumentTypes[i].get().visit(WTF::makeVisitor([&](UniqueRef<AST::UnnamedType>&) -> unsigned { |
| 45 | return 0; |
| 46 | }, [&](RefPtr<ResolvableTypeReference>& resolvableTypeReference) -> unsigned { |
| 47 | return resolvableTypeReference->resolvableType().conversionCost(*candidate.parameters()[i].type()); |
| 48 | })); |
| 49 | } |
| 50 | // The return type can never be a literal type, so its conversion cost is always 0. |
| 51 | return conversionCost; |
| 52 | } |
| 53 | |
| 54 | AST::FunctionDeclaration* resolveFunctionOverloadImpl(Vector<std::reference_wrapper<AST::FunctionDeclaration>, 1>& possibleFunctions, Vector<std::reference_wrapper<ResolvingType>>& argumentTypes, AST::NamedType* castReturnType) |
| 55 | { |
| 56 | Vector<std::reference_wrapper<AST::FunctionDeclaration>, 1> candidates; |
| 57 | for (auto& possibleFunction : possibleFunctions) { |
| 58 | if (possibleFunction.get().entryPointType()) |
| 59 | continue; |
| 60 | if (inferTypesForCall(possibleFunction.get(), argumentTypes, castReturnType)) |
| 61 | candidates.append(possibleFunction.get()); |
| 62 | } |
| 63 | |
| 64 | unsigned minimumConversionCost = std::numeric_limits<unsigned>::max(); |
| 65 | for (auto& candidate : candidates) |
| 66 | minimumConversionCost = std::min(minimumConversionCost, conversionCost(candidate.get(), argumentTypes)); |
| 67 | |
| 68 | Vector<std::reference_wrapper<AST::FunctionDeclaration>, 1> minimumCostCandidates; |
| 69 | for (auto& candidate : candidates) { |
| 70 | if (conversionCost(candidate.get(), argumentTypes) == minimumConversionCost) |
| 71 | minimumCostCandidates.append(candidate); |
| 72 | } |
| 73 | |
| 74 | if (minimumCostCandidates.size() == 1) |
| 75 | return &minimumCostCandidates[0].get(); |
| 76 | return nullptr; |
| 77 | } |
| 78 | |
| 79 | AST::NamedType* resolveTypeOverloadImpl(Vector<std::reference_wrapper<AST::NamedType>, 1>& possibleTypes, AST::TypeArguments& typeArguments) |
| 80 | { |
| 81 | AST::NamedType* result = nullptr; |
| 82 | for (auto& possibleType : possibleTypes) { |
| 83 | if (inferTypesForTypeArguments(possibleType, typeArguments)) { |
| 84 | if (result) |
| 85 | return nullptr; |
| 86 | result = &static_cast<AST::NamedType&>(possibleType); |
| 87 | } |
| 88 | } |
| 89 | |
| 90 | return result; |
| 91 | } |
| 92 | |
| 93 | } // namespace WHLSL |
| 94 | |
| 95 | } // namespace WebCore |
| 96 | |
| 97 | #endif // ENABLE(WEBGPU) |
| 98 | |