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 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748
|
/*
* Copyright (c) 2023 Apple Inc. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
* OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "config.h"
#include "Overload.h"
#include "Types.h"
#include <wtf/DataLog.h>
namespace WGSL {
static constexpr bool shouldDumpOverloadDebugInformation = false;
static constexpr ASCIILiteral logPrefix = "> overload: "_s;
template<typename... Arguments>
inline void log(Arguments&&... arguments)
{
if (shouldDumpOverloadDebugInformation)
dataLog(logPrefix, std::forward<Arguments>(arguments)...);
}
template<typename... Arguments>
inline void logLn(Arguments&&... arguments)
{
if (shouldDumpOverloadDebugInformation)
dataLogLn(logPrefix, std::forward<Arguments>(arguments)...);
}
AbstractPointer::AbstractPointer(AbstractValue addressSpace, AbstractType element)
: AbstractPointer(addressSpace, WTFMove(element), WTF::enumToUnderlyingType(defaultAccessModeForAddressSpace(static_cast<AddressSpace>(std::get<unsigned>(addressSpace)))))
{
}
AbstractPointer::AbstractPointer(AbstractValue addressSpace, AbstractType element, AbstractValue accessMode)
: addressSpace(addressSpace)
, element(WTFMove(element))
, accessMode(accessMode)
{
}
struct ViableOverload {
const OverloadCandidate* candidate;
FixedVector<unsigned> ranks;
FixedVector<const Type*> parameters;
const Type* result;
};
class OverloadResolver {
public:
OverloadResolver(TypeStore&, const Vector<OverloadCandidate>&, const Vector<const Type*>& valueArguments, const Vector<const Type*>& typeArguments, unsigned numberOfTypeSubstitutions, unsigned numberOfValueSubstitutions);
std::optional<SelectedOverload> resolve();
private:
FixedVector<std::optional<ViableOverload>> considerCandidates();
std::optional<ViableOverload> considerCandidate(const OverloadCandidate&);
ConversionRank calculateRank(const AbstractType&, const Type*);
ConversionRank conversionRank(const Type*, const Type*) const;
bool unify(const TypeVariable*, const Type*);
bool unify(const AbstractType&, const Type*);
bool assign(TypeVariable, const Type*);
const Type* resolve(TypeVariable) const;
const Type* materialize(const AbstractType&) const;
bool unify(const AbstractValue&, unsigned);
void assign(ValueVariable, unsigned);
std::optional<unsigned> resolve(ValueVariable) const;
unsigned materialize(const AbstractValue&) const;
TypeStore& m_types;
const Vector<OverloadCandidate>& m_candidates;
const Vector<const Type*>& m_valueArguments;
const Vector<const Type*>& m_typeArguments;
FixedVector<const Type*> m_typeSubstitutions;
FixedVector<std::optional<unsigned>> m_valueSubstitutions;
};
OverloadResolver::OverloadResolver(TypeStore& types, const Vector<OverloadCandidate>& candidates, const Vector<const Type*>& valueArguments, const Vector<const Type*>& typeArguments, unsigned numberOfTypeSubstitutions, unsigned numberOfValueSubstitutions)
: m_types(types)
, m_candidates(candidates)
, m_valueArguments(valueArguments)
, m_typeArguments(typeArguments)
, m_typeSubstitutions(numberOfTypeSubstitutions)
, m_valueSubstitutions(numberOfValueSubstitutions)
{
}
std::optional<SelectedOverload> OverloadResolver::resolve()
{
auto candidates = considerCandidates();
std::optional<ViableOverload> selectedCandidate = std::nullopt;
for (auto& candidate : candidates) {
if (!candidate.has_value())
continue;
if (!selectedCandidate.has_value()) {
std::exchange(selectedCandidate, candidate);
continue;
}
ASSERT(selectedCandidate->ranks.size() == candidate->ranks.size());
bool isBetterOrEqual = true;
bool hasStrictlyBetter = false;
for (unsigned i = 0; i < candidate->ranks.size(); ++i) {
if (candidate->ranks[i] > selectedCandidate->ranks[i]) {
isBetterOrEqual = false;
break;
}
if (candidate->ranks[i] < selectedCandidate->ranks[i])
hasStrictlyBetter = true;
}
if (isBetterOrEqual && hasStrictlyBetter)
std::exchange(selectedCandidate, candidate);
}
if (!selectedCandidate.has_value()) {
logLn("no suitable overload found");
return std::nullopt;
}
logLn("selected overload: ", *selectedCandidate->candidate);
logLn("materialized result type: ", *selectedCandidate->result);
return { { WTFMove(selectedCandidate->parameters), selectedCandidate->result } };
}
const Type* OverloadResolver::materialize(const AbstractType& abstractType) const
{
return WTF::switchOn(*abstractType,
[&](const Type* type) -> const Type* {
return type;
},
[&](TypeVariable variable) -> const Type* {
const Type* type = resolve(variable);
if (!type)
return nullptr;
type = satisfyOrPromote(type, variable.constraints, m_types);
RELEASE_ASSERT(type);
return type;
},
[&](const AbstractVector& vector) -> const Type* {
if (auto* element = materialize(vector.element)) {
auto size = materialize(vector.size);
return m_types.vectorType(size, element);
}
return nullptr;
},
[&](const AbstractMatrix& matrix) -> const Type* {
if (auto* element = materialize(matrix.element)) {
auto columns = materialize(matrix.columns);
auto rows = materialize(matrix.rows);
return m_types.matrixType(columns, rows, element);
}
return nullptr;
},
[&](const AbstractTexture& texture) -> const Type* {
if (auto* element = materialize(texture.element))
return m_types.textureType(texture.kind, element);
return nullptr;
},
[&](const AbstractTextureStorage& texture) -> const Type* {
auto format = materialize(texture.format);
auto access = materialize(texture.access);
return m_types.textureStorageType(texture.kind, static_cast<TexelFormat>(format), static_cast<AccessMode>(access));
},
[&](const AbstractChannelFormat& channelFormat) -> const Type* {
auto format = materialize(channelFormat.format);
return shaderTypeForTexelFormat(static_cast<TexelFormat>(format), m_types);
},
[&](const AbstractReference& reference) -> const Type* {
if (auto* element = materialize(reference.element)) {
auto addressSpace = materialize(reference.addressSpace);
auto accessMode = materialize(reference.accessMode);
return m_types.referenceType(static_cast<AddressSpace>(addressSpace), element, static_cast<AccessMode>(accessMode));
}
return nullptr;
},
[&](const AbstractPointer& pointer) -> const Type* {
if (auto* element = materialize(pointer.element)) {
auto addressSpace = materialize(pointer.addressSpace);
auto accessMode = materialize(pointer.accessMode);
return m_types.pointerType(static_cast<AddressSpace>(addressSpace), element, static_cast<AccessMode>(accessMode));
}
return nullptr;
},
[&](const AbstractArray& array) -> const Type* {
if (auto* element = materialize(array.element))
return m_types.arrayType(element, std::monostate { });
return nullptr;
},
[&](const AbstractAtomic& atomic) -> const Type* {
if (auto* element = materialize(atomic.element))
return m_types.atomicType(element);
return nullptr;
});
}
unsigned OverloadResolver::materialize(const AbstractValue& abstractValue) const
{
return WTF::switchOn(abstractValue,
[&](unsigned value) -> unsigned {
return value;
},
[&](ValueVariable variable) -> unsigned {
std::optional<unsigned> resolvedValue = resolve(variable);
ASSERT(resolvedValue.has_value());
return *resolvedValue;
});
}
FixedVector<std::optional<ViableOverload>> OverloadResolver::considerCandidates()
{
FixedVector<std::optional<ViableOverload>> candidates(m_candidates.size());
for (unsigned i = 0; i < m_candidates.size(); ++i)
candidates[i] = considerCandidate(m_candidates[i]);
return candidates;
}
std::optional<ViableOverload> OverloadResolver::considerCandidate(const OverloadCandidate& candidate)
{
if (candidate.parameters.size() != m_valueArguments.size())
return std::nullopt;
if (m_typeArguments.size() > candidate.typeVariables.size())
return std::nullopt;
m_typeSubstitutions.fill(nullptr);
m_valueSubstitutions.fill(std::nullopt);
for (unsigned i = 0; i < m_typeArguments.size(); ++i) {
if (!assign(candidate.typeVariables[i], m_typeArguments[i]))
return std::nullopt;
}
logLn("Considering overload: ", candidate);
ViableOverload viableOverload {
&candidate,
FixedVector<unsigned>(m_valueArguments.size()),
FixedVector<const Type*>(m_valueArguments.size()),
nullptr
};
for (unsigned i = 0; i < m_valueArguments.size(); ++i) {
auto& parameter = candidate.parameters[i];
auto* argument = m_valueArguments[i];
logLn("matching parameter #", i, " '", parameter, "' with argument '", *argument, "'");
if (!unify(parameter, argument)) {
logLn("rejected on parameter #", i);
return std::nullopt;
}
}
for (unsigned i = 0; i < m_valueArguments.size(); ++i) {
auto& parameter = candidate.parameters[i];
auto* argument = m_valueArguments[i];
auto rank = calculateRank(parameter, argument);
ASSERT(!!rank);
viableOverload.ranks[i] = *rank;
viableOverload.parameters[i] = materialize(parameter);
}
viableOverload.result = materialize(candidate.result);
if (!viableOverload.result)
return std::nullopt;
if (shouldDumpOverloadDebugInformation) {
log("found a viable candidate '", candidate, "' materialized as '(");
bool first = true;
for (auto& parameter : candidate.parameters) {
if (!first)
dataLog(", ");
first = false;
dataLog(*materialize(parameter));
}
dataLog(") -> ", *viableOverload.result, "'");
dataLogLn();
}
return { WTFMove(viableOverload) };
}
ConversionRank OverloadResolver::calculateRank(const AbstractType& parameter, const Type* argumentType)
{
if (auto* variable = std::get_if<TypeVariable>(parameter.get())) {
auto* resolvedType = resolve(*variable);
ASSERT(resolvedType);
if (variable->constraints) {
resolvedType = satisfyOrPromote(resolvedType, variable->constraints, m_types);
RELEASE_ASSERT(resolvedType);
}
return conversionRank(argumentType, resolvedType);
}
if (auto* referenceParameter = std::get_if<AbstractReference>(parameter.get())) {
auto& referenceArgument = std::get<Types::Reference>(*argumentType);
return calculateRank(referenceParameter->element, referenceArgument.element);
}
if (auto* pointerParameter = std::get_if<AbstractPointer>(parameter.get())) {
auto& pointerArgument = std::get<Types::Pointer>(*argumentType);
return calculateRank(pointerParameter->element, pointerArgument.element);
}
if (auto* reference = std::get_if<Types::Reference>(argumentType)) {
ASSERT(reference->accessMode != AccessMode::Write);
return calculateRank(parameter, reference->element);
}
if (auto* vectorParameter = std::get_if<AbstractVector>(parameter.get())) {
auto& vectorArgument = std::get<Types::Vector>(*argumentType);
return calculateRank(vectorParameter->element, vectorArgument.element);
}
if (auto* matrixParameter = std::get_if<AbstractMatrix>(parameter.get())) {
auto& matrixArgument = std::get<Types::Matrix>(*argumentType);
return calculateRank(matrixParameter->element, matrixArgument.element);
}
if (auto* textureParameter = std::get_if<AbstractTexture>(parameter.get())) {
auto& textureArgument = std::get<Types::Texture>(*argumentType);
return calculateRank(textureParameter->element, textureArgument.element);
}
if (auto* arrayParameter = std::get_if<AbstractArray>(parameter.get())) {
auto& arrayArgument = std::get<Types::Array>(*argumentType);
return calculateRank(arrayParameter->element, arrayArgument.element);
}
if (auto* atomicParameter = std::get_if<AbstractAtomic>(parameter.get())) {
auto& atomicArgument = std::get<Types::Atomic>(*argumentType);
return calculateRank(atomicParameter->element, atomicArgument.element);
}
if (std::holds_alternative<AbstractTextureStorage>(*parameter.get()))
return 0;
if (auto* channelFormat = std::get_if<AbstractChannelFormat>(parameter.get())) {
auto format = materialize(channelFormat->format);
return conversionRank(argumentType, shaderTypeForTexelFormat(static_cast<TexelFormat>(format), m_types));
}
auto* parameterType = std::get<const Type*>(*parameter);
return conversionRank(argumentType, parameterType);
}
bool OverloadResolver::unify(const TypeVariable* variable, const Type* argumentType)
{
auto* resolvedType = resolve(*variable);
if (!resolvedType)
return assign(*variable, argumentType);
logLn("resolved '", *variable, "' to '", *resolvedType, "'");
// Consider the following:
// + :: (T, T) -> T
// 1 + 1u
//
// We first unify `Var(T)` with `Type(AbstractInt)`, and assign `T` to `AbstractInt`
// Next, we unify `Var(T) with `Type(u32)`, we look up `T => AbstractInt`,
// and promote `T` to `u32`.
//
// A few more examples to illustrate it:
//
// vec3 :: (T, T, T) -> T
// vec3(1, 1.0, 1.0f) -> vec3<f32>
// 1) unify(Var(T), Type(AbstractInt); T => AbstractInt (assign)
// 2) unify(Var(T), Type(AbstractFloat); T => AbstractFloat (promote T)
// 3) unify(Var(T), Type(f32); T => f32 (promote T again)
//
// vec3 :: (T, T, T) -> T
// vec3(1.0, 1, 1.0f) -> vec3<f32>
// 1) unify(Var(T), Type(AbstractFloat); T => AbstractFloat (assign)
// 2) unify(Var(T), Type(AbstractInt); T => AbstractFloat (convert the argument to AbstractInt)
// 3) unify(Var(T), Type(f32); T => f32 (promote T)
//
// vec3 :: (T, T, T) -> T
// vec3(1.0, 1u, 1.0f) -> vec3<f32>
// 1) unify(Var(T), Type(AbstractInt); T => AbstractFloat (assign)
// 2) unify(Var(T), Type(u32); Failed! Can't unify AbstractFloat and u32
auto variablePromotionRank = conversionRank(resolvedType, argumentType);
auto argumentConversionRank = conversionRank(argumentType, resolvedType);
logLn("variablePromotionRank: ", variablePromotionRank.unsafeValue(), ", argumentConversionRank: ", argumentConversionRank.unsafeValue());
if (variablePromotionRank.unsafeValue() < argumentConversionRank.unsafeValue())
return assign(*variable, argumentType);
return !!argumentConversionRank;
}
bool OverloadResolver::unify(const AbstractType& parameter, const Type* argumentType)
{
logLn("unify parameter type '", parameter, "' with argument '", *argumentType, "'");
if (auto* variable = std::get_if<TypeVariable>(parameter.get()))
return unify(variable, argumentType);
if (auto* referenceParameter = std::get_if<AbstractReference>(parameter.get())) {
auto* referenceArgument = std::get_if<Types::Reference>(argumentType);
if (!referenceArgument)
return false;
if (!unify(referenceParameter->addressSpace, WTF::enumToUnderlyingType(referenceArgument->addressSpace)))
return false;
if (!unify(referenceParameter->accessMode, WTF::enumToUnderlyingType(referenceArgument->accessMode)))
return false;
return unify(referenceParameter->element, referenceArgument->element);
}
if (auto* pointerParameter = std::get_if<AbstractPointer>(parameter.get())) {
auto* pointerArgument = std::get_if<Types::Pointer>(argumentType);
if (!pointerArgument)
return false;
if (!unify(pointerParameter->addressSpace, WTF::enumToUnderlyingType(pointerArgument->addressSpace)))
return false;
if (!unify(pointerParameter->accessMode, WTF::enumToUnderlyingType(pointerArgument->accessMode)))
return false;
return unify(pointerParameter->element, pointerArgument->element);
}
if (auto* reference = std::get_if<Types::Reference>(argumentType)) {
if (reference->accessMode == AccessMode::Write)
return false;
return unify(parameter, reference->element);
}
if (auto* vectorParameter = std::get_if<AbstractVector>(parameter.get())) {
auto* vectorArgument = std::get_if<Types::Vector>(argumentType);
if (!vectorArgument)
return false;
if (!unify(vectorParameter->element, vectorArgument->element))
return false;
return unify(vectorParameter->size, vectorArgument->size);
}
if (auto* matrixParameter = std::get_if<AbstractMatrix>(parameter.get())) {
auto* matrixArgument = std::get_if<Types::Matrix>(argumentType);
if (!matrixArgument)
return false;
if (!unify(matrixParameter->element, matrixArgument->element))
return false;
if (!unify(matrixParameter->columns, matrixArgument->columns))
return false;
return unify(matrixParameter->rows, matrixArgument->rows);
}
if (auto* textureParameter = std::get_if<AbstractTexture>(parameter.get())) {
auto* textureArgument = std::get_if<Types::Texture>(argumentType);
if (!textureArgument)
return false;
if (textureParameter->kind != textureArgument->kind)
return false;
return unify(textureParameter->element, textureArgument->element);
}
if (auto* textureStorageParameter = std::get_if<AbstractTextureStorage>(parameter.get())) {
auto* textureStorageArgument = std::get_if<Types::TextureStorage>(argumentType);
if (!textureStorageArgument)
return false;
if (textureStorageParameter->kind != textureStorageArgument->kind)
return false;
if (!unify(textureStorageParameter->format, WTF::enumToUnderlyingType(textureStorageArgument->format)))
return false;
return unify(textureStorageParameter->access, WTF::enumToUnderlyingType(textureStorageArgument->access));
}
if (auto* channelFormat = std::get_if<AbstractChannelFormat>(parameter.get())) {
auto format = materialize(channelFormat->format);
return !!conversionRank(argumentType, shaderTypeForTexelFormat(static_cast<TexelFormat>(format), m_types));
}
if (auto* arrayParameter = std::get_if<AbstractArray>(parameter.get())) {
auto* arrayArgument = std::get_if<Types::Array>(argumentType);
if (!arrayArgument)
return false;
// For now, we only support dynamic arrays
if (!arrayArgument->isRuntimeSized())
return false;
return unify(arrayParameter->element, arrayArgument->element);
}
if (auto* atomicParameter = std::get_if<AbstractAtomic>(parameter.get())) {
auto* atomicArgument = std::get_if<Types::Atomic>(argumentType);
if (!atomicArgument)
return false;
return unify(atomicParameter->element, atomicArgument->element);
}
auto* parameterType = std::get<const Type*>(*parameter);
return !!conversionRank(argumentType, parameterType);
}
bool OverloadResolver::unify(const AbstractValue& parameter, unsigned argumentValue)
{
if (auto* parameterValue = std::get_if<unsigned>(¶meter))
return *parameterValue == argumentValue;
auto variable = std::get<ValueVariable>(parameter);
auto resolvedValue = resolve(variable);
if (!resolvedValue.has_value()) {
assign(variable, argumentValue);
return true;
}
return *resolvedValue == argumentValue;
}
bool OverloadResolver::assign(TypeVariable variable, const Type* type)
{
logLn("assign ", variable, " => ", *type);
if (variable.constraints) {
if (!satisfies(type, variable.constraints))
return false;
}
m_typeSubstitutions[variable.id] = type;
return true;
}
void OverloadResolver::assign(ValueVariable variable, unsigned value)
{
logLn("assign ", variable, " => ", value);
m_valueSubstitutions[variable.id] = { value };
}
const Type* OverloadResolver::resolve(TypeVariable variable) const
{
return m_typeSubstitutions[variable.id];
}
std::optional<unsigned> OverloadResolver::resolve(ValueVariable variable) const
{
return m_valueSubstitutions[variable.id];
}
ConversionRank OverloadResolver::conversionRank(const Type* from, const Type* to) const
{
auto rank = ::WGSL::conversionRank(from, to);
logLn("conversionRank(from: ", *from, ", to: ", *to, ") = ", rank.unsafeValue());
return rank;
}
std::optional<SelectedOverload> resolveOverloads(TypeStore& types, const Vector<OverloadCandidate>& candidates, const Vector<const Type*>& valueArguments, const Vector<const Type*>& typeArguments)
{
unsigned numberOfTypeSubstitutions = 0;
unsigned numberOfValueSubstitutions = 0;
for (const auto& candidate : candidates) {
numberOfTypeSubstitutions = std::max(numberOfTypeSubstitutions, static_cast<unsigned>(candidate.typeVariables.size()));
numberOfValueSubstitutions = std::max(numberOfValueSubstitutions, static_cast<unsigned>(candidate.valueVariables.size()));
}
OverloadResolver resolver(types, candidates, valueArguments, typeArguments, numberOfTypeSubstitutions, numberOfValueSubstitutions);
return resolver.resolve();
}
} // namespace WGSL
namespace WTF {
void printInternal(PrintStream& out, const WGSL::ValueVariable& variable)
{
out.print("val", variable.id);
}
void printInternal(PrintStream& out, const WGSL::AbstractValue& value)
{
WTF::switchOn(value,
[&](unsigned value) {
out.print(value);
},
[&](WGSL::ValueVariable variable) {
printInternal(out, variable);
});
}
void printInternal(PrintStream& out, const WGSL::TypeVariable& variable)
{
out.print("type", variable.id);
}
void printInternal(PrintStream& out, const WGSL::AbstractType& type)
{
WTF::switchOn(*type,
[&](const WGSL::Type* type) {
printInternal(out, *type);
},
[&](WGSL::TypeVariable variable) {
printInternal(out, variable);
},
[&](const WGSL::AbstractVector& vector) {
out.print("vector<");
printInternal(out, vector.element);
out.print(", ");
printInternal(out, vector.size);
out.print(">");
},
[&](const WGSL::AbstractMatrix& matrix) {
out.print("matrix<");
printInternal(out, matrix.element);
out.print(", ");
printInternal(out, matrix.columns);
out.print(", ");
printInternal(out, matrix.rows);
out.print(">");
},
[&](const WGSL::AbstractTexture& texture) {
printInternal(out, texture.kind);
out.print("<");
printInternal(out, texture.element);
out.print(">");
},
[&](const WGSL::AbstractTextureStorage& texture) {
printInternal(out, texture.kind);
out.print("<");
printInternal(out, texture.format);
out.print(", ");
printInternal(out, texture.access);
out.print(">");
},
[&](const WGSL::AbstractChannelFormat& channelFormat) {
out.print("ChannelFormat<", channelFormat.format, ">");
},
[&](const WGSL::AbstractReference& reference) {
out.print("ref<");
printInternal(out, reference.addressSpace);
out.print(", ");
printInternal(out, reference.element);
out.print(", ");
printInternal(out, reference.accessMode);
out.print(">");
},
[&](const WGSL::AbstractPointer& pointer) {
out.print("ptr<");
printInternal(out, pointer.addressSpace);
out.print(", ");
printInternal(out, pointer.element);
out.print(", ");
printInternal(out, pointer.accessMode);
out.print(">");
},
[&](const WGSL::AbstractArray& array) {
out.print("array<");
printInternal(out, array.element);
out.print(">");
},
[&](const WGSL::AbstractAtomic& atomic) {
out.print("atomic<");
printInternal(out, atomic.element);
out.print(">");
});
}
void printInternal(PrintStream& out, const WGSL::OverloadCandidate& candidate)
{
if (candidate.typeVariables.size() || candidate.valueVariables.size()) {
bool first = true;
out.print("<");
for (auto& typeVariable : candidate.typeVariables) {
if (!first)
out.print(", ");
first = false;
printInternal(out, typeVariable);
}
for (auto& valueVariable : candidate.valueVariables) {
if (!first)
out.print(", ");
first = false;
printInternal(out, valueVariable);
}
out.print(">");
}
out.print("(");
bool first = true;
for (auto& parameter : candidate.parameters) {
if (!first)
out.print(", ");
first = false;
printInternal(out, parameter);
}
out.print(") -> ");
printInternal(out, candidate.result);
}
void printInternal(PrintStream& out, WGSL::Types::Texture::Kind textureKind)
{
switch (textureKind) {
case WGSL::Types::Texture::Kind::Texture1d:
out.print("texture_1d");
return;
case WGSL::Types::Texture::Kind::Texture2d:
out.print("texture_2d");
return;
case WGSL::Types::Texture::Kind::Texture2dArray:
out.print("texture_2d_array");
return;
case WGSL::Types::Texture::Kind::Texture3d:
out.print("texture_3d");
return;
case WGSL::Types::Texture::Kind::TextureCube:
out.print("texture_cube");
return;
case WGSL::Types::Texture::Kind::TextureCubeArray:
out.print("texture_cube_array");
return;
case WGSL::Types::Texture::Kind::TextureMultisampled2d:
out.print("texture_multisampled_2d");
return;
}
}
void printInternal(PrintStream& out, WGSL::Types::TextureStorage::Kind textureKind)
{
switch (textureKind) {
case WGSL::Types::TextureStorage::Kind::TextureStorage1d:
out.print("texture_storage_1d");
return;
case WGSL::Types::TextureStorage::Kind::TextureStorage2d:
out.print("texture_storage_2d");
return;
case WGSL::Types::TextureStorage::Kind::TextureStorage2dArray:
out.print("texture_storage_2d_array");
return;
case WGSL::Types::TextureStorage::Kind::TextureStorage3d:
out.print("texture_storage_3d");
return;
}
}
} // namespace WTF
|