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 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095
|
// Copyright (c) 2018 Google LLC.
// Modifications Copyright (C) 2024 Advanced Micro Devices, Inc. All rights
// reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
#include <algorithm>
#include "source/opcode.h"
#include "source/spirv_target_env.h"
#include "source/table2.h"
#include "source/val/instruction.h"
#include "source/val/validate.h"
#include "source/val/validation_state.h"
namespace spvtools {
namespace val {
namespace {
spv_result_t ValidateEntryPoint(ValidationState_t& _, const Instruction* inst) {
const auto entry_point_id = inst->GetOperandAs<uint32_t>(1);
auto entry_point = _.FindDef(entry_point_id);
if (!entry_point || spv::Op::OpFunction != entry_point->opcode()) {
return _.diag(SPV_ERROR_INVALID_ID, inst)
<< "OpEntryPoint Entry Point <id> " << _.getIdName(entry_point_id)
<< " is not a function.";
}
// Only check the shader execution models
const spv::ExecutionModel execution_model =
inst->GetOperandAs<spv::ExecutionModel>(0);
if (execution_model != spv::ExecutionModel::Kernel) {
const auto entry_point_type_id = entry_point->GetOperandAs<uint32_t>(3);
const auto entry_point_type = _.FindDef(entry_point_type_id);
if (!entry_point_type || 3 != entry_point_type->words().size()) {
return _.diag(SPV_ERROR_INVALID_ID, inst)
<< _.VkErrorID(4633) << "OpEntryPoint Entry Point <id> "
<< _.getIdName(entry_point_id)
<< "s function parameter count is not zero.";
}
}
auto return_type = _.FindDef(entry_point->type_id());
if (!return_type || spv::Op::OpTypeVoid != return_type->opcode()) {
return _.diag(SPV_ERROR_INVALID_ID, inst)
<< _.VkErrorID(4633) << "OpEntryPoint Entry Point <id> "
<< _.getIdName(entry_point_id)
<< "s function return type is not void.";
}
const auto* execution_modes = _.GetExecutionModes(entry_point_id);
auto has_mode = [&execution_modes](spv::ExecutionMode mode) {
return execution_modes && execution_modes->count(mode);
};
if (_.HasCapability(spv::Capability::Shader)) {
switch (execution_model) {
case spv::ExecutionModel::Fragment:
if (has_mode(spv::ExecutionMode::OriginUpperLeft) &&
has_mode(spv::ExecutionMode::OriginLowerLeft)) {
return _.diag(SPV_ERROR_INVALID_DATA, inst)
<< "Fragment execution model entry points can only specify "
"one of OriginUpperLeft or OriginLowerLeft execution "
"modes.";
}
if (!has_mode(spv::ExecutionMode::OriginUpperLeft) &&
!has_mode(spv::ExecutionMode::OriginLowerLeft)) {
return _.diag(SPV_ERROR_INVALID_DATA, inst)
<< "Fragment execution model entry points require either an "
"OriginUpperLeft or OriginLowerLeft execution mode.";
}
if (execution_modes &&
1 < std::count_if(execution_modes->begin(), execution_modes->end(),
[](const spv::ExecutionMode& mode) {
switch (mode) {
case spv::ExecutionMode::DepthGreater:
case spv::ExecutionMode::DepthLess:
case spv::ExecutionMode::DepthUnchanged:
return true;
default:
return false;
}
})) {
return _.diag(SPV_ERROR_INVALID_DATA, inst)
<< "Fragment execution model entry points can specify at most "
"one of DepthGreater, DepthLess or DepthUnchanged "
"execution modes.";
}
if (execution_modes &&
1 < std::count_if(
execution_modes->begin(), execution_modes->end(),
[](const spv::ExecutionMode& mode) {
switch (mode) {
case spv::ExecutionMode::PixelInterlockOrderedEXT:
case spv::ExecutionMode::PixelInterlockUnorderedEXT:
case spv::ExecutionMode::SampleInterlockOrderedEXT:
case spv::ExecutionMode::SampleInterlockUnorderedEXT:
case spv::ExecutionMode::ShadingRateInterlockOrderedEXT:
case spv::ExecutionMode::
ShadingRateInterlockUnorderedEXT:
return true;
default:
return false;
}
})) {
return _.diag(SPV_ERROR_INVALID_DATA, inst)
<< "Fragment execution model entry points can specify at most "
"one fragment shader interlock execution mode.";
}
if (execution_modes &&
1 < std::count_if(
execution_modes->begin(), execution_modes->end(),
[](const spv::ExecutionMode& mode) {
switch (mode) {
case spv::ExecutionMode::StencilRefUnchangedFrontAMD:
case spv::ExecutionMode::StencilRefLessFrontAMD:
case spv::ExecutionMode::StencilRefGreaterFrontAMD:
return true;
default:
return false;
}
})) {
return _.diag(SPV_ERROR_INVALID_DATA, inst)
<< "Fragment execution model entry points can specify at most "
"one of StencilRefUnchangedFrontAMD, "
"StencilRefLessFrontAMD or StencilRefGreaterFrontAMD "
"execution modes.";
}
if (execution_modes &&
1 < std::count_if(
execution_modes->begin(), execution_modes->end(),
[](const spv::ExecutionMode& mode) {
switch (mode) {
case spv::ExecutionMode::StencilRefUnchangedBackAMD:
case spv::ExecutionMode::StencilRefLessBackAMD:
case spv::ExecutionMode::StencilRefGreaterBackAMD:
return true;
default:
return false;
}
})) {
return _.diag(SPV_ERROR_INVALID_DATA, inst)
<< "Fragment execution model entry points can specify at most "
"one of StencilRefUnchangedBackAMD, "
"StencilRefLessBackAMD or StencilRefGreaterBackAMD "
"execution modes.";
}
break;
case spv::ExecutionModel::TessellationControl:
case spv::ExecutionModel::TessellationEvaluation:
if (execution_modes &&
1 < std::count_if(
execution_modes->begin(), execution_modes->end(),
[](const spv::ExecutionMode& mode) {
switch (mode) {
case spv::ExecutionMode::SpacingEqual:
case spv::ExecutionMode::SpacingFractionalEven:
case spv::ExecutionMode::SpacingFractionalOdd:
return true;
default:
return false;
}
})) {
return _.diag(SPV_ERROR_INVALID_DATA, inst)
<< "Tessellation execution model entry points can specify at "
"most one of SpacingEqual, SpacingFractionalOdd or "
"SpacingFractionalEven execution modes.";
}
if (execution_modes &&
1 < std::count_if(execution_modes->begin(), execution_modes->end(),
[](const spv::ExecutionMode& mode) {
switch (mode) {
case spv::ExecutionMode::Triangles:
case spv::ExecutionMode::Quads:
case spv::ExecutionMode::Isolines:
return true;
default:
return false;
}
})) {
return _.diag(SPV_ERROR_INVALID_DATA, inst)
<< "Tessellation execution model entry points can specify at "
"most one of Triangles, Quads or Isolines execution modes.";
}
if (execution_modes &&
1 < std::count_if(execution_modes->begin(), execution_modes->end(),
[](const spv::ExecutionMode& mode) {
switch (mode) {
case spv::ExecutionMode::VertexOrderCw:
case spv::ExecutionMode::VertexOrderCcw:
return true;
default:
return false;
}
})) {
return _.diag(SPV_ERROR_INVALID_DATA, inst)
<< "Tessellation execution model entry points can specify at "
"most one of VertexOrderCw or VertexOrderCcw execution "
"modes.";
}
break;
case spv::ExecutionModel::Geometry:
if (!execution_modes ||
1 != std::count_if(
execution_modes->begin(), execution_modes->end(),
[](const spv::ExecutionMode& mode) {
switch (mode) {
case spv::ExecutionMode::InputPoints:
case spv::ExecutionMode::InputLines:
case spv::ExecutionMode::InputLinesAdjacency:
case spv::ExecutionMode::Triangles:
case spv::ExecutionMode::InputTrianglesAdjacency:
return true;
default:
return false;
}
})) {
return _.diag(SPV_ERROR_INVALID_DATA, inst)
<< "Geometry execution model entry points must specify "
"exactly one of InputPoints, InputLines, "
"InputLinesAdjacency, Triangles or InputTrianglesAdjacency "
"execution modes.";
}
if (!execution_modes ||
1 != std::count_if(execution_modes->begin(), execution_modes->end(),
[](const spv::ExecutionMode& mode) {
switch (mode) {
case spv::ExecutionMode::OutputPoints:
case spv::ExecutionMode::OutputLineStrip:
case spv::ExecutionMode::OutputTriangleStrip:
return true;
default:
return false;
}
})) {
return _.diag(SPV_ERROR_INVALID_DATA, inst)
<< "Geometry execution model entry points must specify "
"exactly one of OutputPoints, OutputLineStrip or "
"OutputTriangleStrip execution modes.";
}
break;
case spv::ExecutionModel::MeshEXT:
if (!execution_modes ||
1 != std::count_if(execution_modes->begin(), execution_modes->end(),
[](const spv::ExecutionMode& mode) {
switch (mode) {
case spv::ExecutionMode::OutputPoints:
case spv::ExecutionMode::OutputLinesEXT:
case spv::ExecutionMode::OutputTrianglesEXT:
return true;
default:
return false;
}
})) {
return _.diag(SPV_ERROR_INVALID_DATA, inst)
<< "MeshEXT execution model entry points must specify exactly "
"one of OutputPoints, OutputLinesEXT, or "
"OutputTrianglesEXT Execution Modes.";
} else if (2 != std::count_if(
execution_modes->begin(), execution_modes->end(),
[](const spv::ExecutionMode& mode) {
switch (mode) {
case spv::ExecutionMode::OutputPrimitivesEXT:
case spv::ExecutionMode::OutputVertices:
return true;
default:
return false;
}
})) {
return _.diag(SPV_ERROR_INVALID_DATA, inst)
<< "MeshEXT execution model entry points must specify both "
"OutputPrimitivesEXT and OutputVertices Execution Modes.";
}
break;
default:
break;
}
}
bool has_workgroup_size = false;
bool has_local_size_id = false;
for (auto& i : _.ordered_instructions()) {
if (i.opcode() == spv::Op::OpFunction) break;
if (i.opcode() == spv::Op::OpDecorate && i.operands().size() > 2) {
if (i.GetOperandAs<spv::Decoration>(1) == spv::Decoration::BuiltIn &&
i.GetOperandAs<spv::BuiltIn>(2) == spv::BuiltIn::WorkgroupSize) {
has_workgroup_size = true;
}
}
if (i.opcode() == spv::Op::OpExecutionModeId) {
if (i.GetOperandAs<spv::ExecutionMode>(1) ==
spv::ExecutionMode::LocalSizeId) {
has_local_size_id = true;
}
}
}
if (spvIsVulkanEnv(_.context()->target_env)) {
switch (execution_model) {
case spv::ExecutionModel::GLCompute:
if (!has_mode(spv::ExecutionMode::LocalSize)) {
bool ok = has_workgroup_size || has_local_size_id;
if (!ok && _.HasCapability(spv::Capability::TileShadingQCOM)) {
ok = has_mode(spv::ExecutionMode::TileShadingRateQCOM);
}
if (!ok) {
return _.diag(SPV_ERROR_INVALID_DATA, inst)
<< _.VkErrorID(10685)
<< "In the Vulkan environment, GLCompute execution model "
"entry points require either the "
<< (_.HasCapability(spv::Capability::TileShadingQCOM)
? "TileShadingRateQCOM, "
: "")
<< "LocalSize or LocalSizeId execution mode or an object "
"decorated with WorkgroupSize must be specified.";
}
}
if (_.HasCapability(spv::Capability::TileShadingQCOM)) {
if (has_mode(spv::ExecutionMode::TileShadingRateQCOM) &&
(has_mode(spv::ExecutionMode::LocalSize) ||
has_mode(spv::ExecutionMode::LocalSizeId))) {
return _.diag(SPV_ERROR_INVALID_DATA, inst)
<< "If the TileShadingRateQCOM execution mode is used, "
<< "LocalSize and LocalSizeId must not be specified.";
}
if (has_mode(spv::ExecutionMode::NonCoherentTileAttachmentReadQCOM)) {
return _.diag(SPV_ERROR_INVALID_DATA, inst)
<< "The NonCoherentTileAttachmentQCOM execution mode must "
"not be used in any stage other than fragment.";
}
} else {
if (has_mode(spv::ExecutionMode::TileShadingRateQCOM)) {
return _.diag(SPV_ERROR_INVALID_DATA, inst)
<< "If the TileShadingRateQCOM execution mode is used, the "
"TileShadingQCOM capability must be enabled.";
}
}
break;
default:
if (has_mode(spv::ExecutionMode::TileShadingRateQCOM)) {
return _.diag(SPV_ERROR_INVALID_DATA, inst)
<< "The TileShadingRateQCOM execution mode must not be used "
"in any stage other than compute.";
}
if (execution_model != spv::ExecutionModel::Fragment) {
if (has_mode(spv::ExecutionMode::NonCoherentTileAttachmentReadQCOM)) {
return _.diag(SPV_ERROR_INVALID_DATA, inst)
<< "The NonCoherentTileAttachmentQCOM execution mode must "
"not be used in any stage other than fragment.";
}
if (_.HasCapability(spv::Capability::TileShadingQCOM)) {
return _.diag(SPV_ERROR_INVALID_CAPABILITY, inst)
<< "The TileShadingQCOM capability must not be enabled in "
"any stage other than compute or fragment.";
}
} else {
if (has_mode(spv::ExecutionMode::NonCoherentTileAttachmentReadQCOM)) {
if (!_.HasCapability(spv::Capability::TileShadingQCOM)) {
return _.diag(SPV_ERROR_INVALID_DATA, inst)
<< "If the NonCoherentTileAttachmentReadQCOM execution "
"mode is used, the TileShadingQCOM capability must be "
"enabled.";
}
}
}
break;
}
}
// WorkgroupSize decoration takes precedence over any LocalSize or LocalSizeId
// execution mode, so the values can be ignored
if (_.EntryPointHasLocalSizeOrId(entry_point_id) && !has_workgroup_size) {
const Instruction* local_size_inst =
_.EntryPointLocalSizeOrId(entry_point_id);
if (local_size_inst) {
const auto mode = local_size_inst->GetOperandAs<spv::ExecutionMode>(1);
const uint32_t operand_x = local_size_inst->GetOperandAs<uint32_t>(2);
const uint32_t operand_y = local_size_inst->GetOperandAs<uint32_t>(3);
const uint32_t operand_z = local_size_inst->GetOperandAs<uint32_t>(4);
if (mode == spv::ExecutionMode::LocalSize) {
const uint64_t product_size = operand_x * operand_y * operand_z;
if (product_size == 0) {
return _.diag(SPV_ERROR_INVALID_DATA, local_size_inst)
<< "Local Size execution mode must not have a product of zero "
"(X "
"= "
<< operand_x << ", Y = " << operand_y << ", Z = " << operand_z
<< ").";
}
if (has_mode(spv::ExecutionMode::DerivativeGroupQuadsKHR)) {
if (operand_x % 2 != 0 || operand_y % 2 != 0) {
return _.diag(SPV_ERROR_INVALID_DATA, local_size_inst)
<< _.VkErrorID(10151)
<< "Local Size execution mode dimensions is "
"(X = "
<< operand_x << ", Y = " << operand_y
<< ") but Entry Point id " << entry_point_id
<< " also has an DerivativeGroupQuadsKHR execution mode, so "
"both dimensions must be a multiple of 2";
}
}
if (has_mode(spv::ExecutionMode::DerivativeGroupLinearKHR)) {
if (product_size % 4 != 0) {
return _.diag(SPV_ERROR_INVALID_DATA, local_size_inst)
<< _.VkErrorID(10152)
<< "Local Size execution mode dimensions is (X = "
<< operand_x << ", Y = " << operand_y
<< ", Z = " << operand_z << ") but Entry Point id "
<< entry_point_id
<< " also has an DerivativeGroupLinearKHR execution mode, "
"so "
"the product ("
<< product_size << ") must be a multiple of 4";
}
}
} else if (mode == spv::ExecutionMode::LocalSizeId) {
// can only validate product if static and not spec constant
// (This is done for us in EvalConstantValUint64)
uint64_t x_size, y_size, z_size;
bool static_x = _.EvalConstantValUint64(operand_x, &x_size);
bool static_y = _.EvalConstantValUint64(operand_y, &y_size);
bool static_z = _.EvalConstantValUint64(operand_z, &z_size);
if (static_x && static_y && static_z) {
const uint64_t product_size = x_size * y_size * z_size;
if (product_size == 0) {
return _.diag(SPV_ERROR_INVALID_DATA, local_size_inst)
<< "LocalSizeId execution mode must not have a product of "
"zero "
"(X = "
<< x_size << ", Y = " << y_size << ", Z = " << z_size
<< ").";
}
if (has_mode(spv::ExecutionMode::DerivativeGroupQuadsKHR)) {
if (x_size % 2 != 0 || y_size % 2 != 0) {
return _.diag(SPV_ERROR_INVALID_DATA, local_size_inst)
<< _.VkErrorID(10151)
<< "LocalSizeId execution mode dimensions is "
"(X = "
<< x_size << ", Y = " << y_size << ") but Entry Point id "
<< entry_point_id
<< " also has an DerivativeGroupQuadsKHR execution mode, "
"so "
"both dimensions must be a multiple of 2";
}
}
if (has_mode(spv::ExecutionMode::DerivativeGroupLinearKHR)) {
if (product_size % 4 != 0) {
return _.diag(SPV_ERROR_INVALID_DATA, local_size_inst)
<< _.VkErrorID(10152)
<< "LocalSizeId execution mode dimensions is (X = "
<< x_size << ", Y = " << y_size << ", Z = " << z_size
<< ") but Entry Point id " << entry_point_id
<< " also has an DerivativeGroupLinearKHR execution mode, "
"so "
"the product ("
<< product_size << ") must be a multiple of 4";
}
}
}
}
}
}
return SPV_SUCCESS;
}
spv_result_t ValidateExecutionMode(ValidationState_t& _,
const Instruction* inst) {
const auto entry_point_id = inst->GetOperandAs<uint32_t>(0);
const auto found = std::find(_.entry_points().cbegin(),
_.entry_points().cend(), entry_point_id);
if (found == _.entry_points().cend()) {
return _.diag(SPV_ERROR_INVALID_ID, inst)
<< "OpExecutionMode Entry Point <id> " << _.getIdName(entry_point_id)
<< " is not the Entry Point "
"operand of an OpEntryPoint.";
}
const auto mode = inst->GetOperandAs<spv::ExecutionMode>(1);
if (inst->opcode() == spv::Op::OpExecutionModeId) {
bool valid_mode = false;
switch (mode) {
case spv::ExecutionMode::SubgroupsPerWorkgroupId:
case spv::ExecutionMode::LocalSizeHintId:
case spv::ExecutionMode::LocalSizeId:
case spv::ExecutionMode::FPFastMathDefault:
case spv::ExecutionMode::MaximumRegistersIdINTEL:
case spv::ExecutionMode::IsApiEntryAMDX:
case spv::ExecutionMode::MaxNodeRecursionAMDX:
case spv::ExecutionMode::MaxNumWorkgroupsAMDX:
case spv::ExecutionMode::ShaderIndexAMDX:
case spv::ExecutionMode::SharesInputWithAMDX:
case spv::ExecutionMode::StaticNumWorkgroupsAMDX:
valid_mode = true;
break;
default:
valid_mode = false;
break;
}
if (!valid_mode) {
return _.diag(SPV_ERROR_INVALID_ID, inst)
<< "OpExecutionModeId is only valid when the Mode operand is an "
"execution mode that takes Extra Operands that are id "
"operands.";
}
size_t operand_count = inst->operands().size();
for (size_t i = 2; i < operand_count; ++i) {
const auto operand_id = inst->GetOperandAs<uint32_t>(i);
const auto* operand_inst = _.FindDef(operand_id);
switch (mode) {
case spv::ExecutionMode::SubgroupsPerWorkgroupId:
case spv::ExecutionMode::LocalSizeHintId:
case spv::ExecutionMode::LocalSizeId:
case spv::ExecutionMode::IsApiEntryAMDX:
case spv::ExecutionMode::MaxNodeRecursionAMDX:
case spv::ExecutionMode::MaxNumWorkgroupsAMDX:
case spv::ExecutionMode::ShaderIndexAMDX:
case spv::ExecutionMode::SharesInputWithAMDX:
case spv::ExecutionMode::StaticNumWorkgroupsAMDX:
if (!spvOpcodeIsConstant(operand_inst->opcode())) {
return _.diag(SPV_ERROR_INVALID_ID, inst)
<< "For OpExecutionModeId all Extra Operand ids must be "
"constant instructions.";
}
break;
case spv::ExecutionMode::FPFastMathDefault:
if (i == 2) {
if (!_.IsFloatScalarType(operand_id)) {
return _.diag(SPV_ERROR_INVALID_ID, inst)
<< "The Target Type operand must be a floating-point "
"scalar type";
}
} else {
bool is_int32 = false;
bool is_const = false;
uint32_t value = 0;
std::tie(is_int32, is_const, value) =
_.EvalInt32IfConst(operand_id);
if (is_int32 && is_const) {
// Valid values include up to 0x00040000 (AllowTransform).
uint32_t invalid_mask = 0xfff80000;
if ((invalid_mask & value) != 0) {
return _.diag(SPV_ERROR_INVALID_ID, inst)
<< "The Fast Math Default operand is an invalid bitmask "
"value";
}
if (value &
static_cast<uint32_t>(spv::FPFastMathModeMask::Fast)) {
return _.diag(SPV_ERROR_INVALID_ID, inst)
<< "The Fast Math Default operand must not include Fast";
}
const auto reassoc_contract =
spv::FPFastMathModeMask::AllowContract |
spv::FPFastMathModeMask::AllowReassoc;
if ((value & static_cast<uint32_t>(
spv::FPFastMathModeMask::AllowTransform)) != 0 &&
((value & static_cast<uint32_t>(reassoc_contract)) !=
static_cast<uint32_t>(reassoc_contract))) {
return _.diag(SPV_ERROR_INVALID_ID, inst)
<< "The Fast Math Default operand must include "
"AllowContract and AllowReassoc when AllowTransform "
"is specified";
}
} else {
return _.diag(SPV_ERROR_INVALID_ID, inst)
<< "The Fast Math Default operand must be a "
"non-specialization constant";
}
}
break;
default:
break;
}
}
} else if (mode == spv::ExecutionMode::SubgroupsPerWorkgroupId ||
mode == spv::ExecutionMode::LocalSizeHintId ||
mode == spv::ExecutionMode::LocalSizeId ||
mode == spv::ExecutionMode::FPFastMathDefault ||
mode == spv::ExecutionMode::IsApiEntryAMDX ||
mode == spv::ExecutionMode::MaxNodeRecursionAMDX ||
mode == spv::ExecutionMode::MaxNumWorkgroupsAMDX ||
mode == spv::ExecutionMode::ShaderIndexAMDX ||
mode == spv::ExecutionMode::SharesInputWithAMDX ||
mode == spv::ExecutionMode::StaticNumWorkgroupsAMDX) {
return _.diag(SPV_ERROR_INVALID_DATA, inst)
<< "OpExecutionMode is only valid when the Mode operand is an "
"execution mode that takes no Extra Operands, or takes Extra "
"Operands that are not id operands.";
}
const bool is_vulkan_env = (spvIsVulkanEnv(_.context()->target_env));
const auto* models = _.GetExecutionModels(entry_point_id);
switch (mode) {
case spv::ExecutionMode::Invocations:
case spv::ExecutionMode::InputPoints:
case spv::ExecutionMode::InputLines:
case spv::ExecutionMode::InputLinesAdjacency:
case spv::ExecutionMode::InputTrianglesAdjacency:
case spv::ExecutionMode::OutputLineStrip:
case spv::ExecutionMode::OutputTriangleStrip:
if (!std::all_of(models->begin(), models->end(),
[](const spv::ExecutionModel& model) {
return model == spv::ExecutionModel::Geometry;
})) {
return _.diag(SPV_ERROR_INVALID_DATA, inst)
<< "Execution mode can only be used with the Geometry execution "
"model.";
}
break;
case spv::ExecutionMode::OutputPoints:
if (!std::all_of(
models->begin(), models->end(),
[&_](const spv::ExecutionModel& model) {
switch (model) {
case spv::ExecutionModel::Geometry:
return true;
case spv::ExecutionModel::MeshNV:
return _.HasCapability(spv::Capability::MeshShadingNV);
case spv::ExecutionModel::MeshEXT:
return _.HasCapability(spv::Capability::MeshShadingEXT);
default:
return false;
}
})) {
if (_.HasCapability(spv::Capability::MeshShadingNV) ||
_.HasCapability(spv::Capability::MeshShadingEXT)) {
return _.diag(SPV_ERROR_INVALID_DATA, inst)
<< "Execution mode can only be used with the Geometry "
"MeshNV or MeshEXT execution model.";
} else {
return _.diag(SPV_ERROR_INVALID_DATA, inst)
<< "Execution mode can only be used with the Geometry "
"execution "
"model.";
}
}
break;
case spv::ExecutionMode::SpacingEqual:
case spv::ExecutionMode::SpacingFractionalEven:
case spv::ExecutionMode::SpacingFractionalOdd:
case spv::ExecutionMode::VertexOrderCw:
case spv::ExecutionMode::VertexOrderCcw:
case spv::ExecutionMode::PointMode:
case spv::ExecutionMode::Quads:
case spv::ExecutionMode::Isolines:
if (!std::all_of(
models->begin(), models->end(),
[](const spv::ExecutionModel& model) {
return (model == spv::ExecutionModel::TessellationControl) ||
(model == spv::ExecutionModel::TessellationEvaluation);
})) {
return _.diag(SPV_ERROR_INVALID_DATA, inst)
<< "Execution mode can only be used with a tessellation "
"execution model.";
}
break;
case spv::ExecutionMode::Triangles:
if (!std::all_of(models->begin(), models->end(),
[](const spv::ExecutionModel& model) {
switch (model) {
case spv::ExecutionModel::Geometry:
case spv::ExecutionModel::TessellationControl:
case spv::ExecutionModel::TessellationEvaluation:
return true;
default:
return false;
}
})) {
return _.diag(SPV_ERROR_INVALID_DATA, inst)
<< "Execution mode can only be used with a Geometry or "
"tessellation execution model.";
}
break;
case spv::ExecutionMode::OutputVertices:
if (!std::all_of(
models->begin(), models->end(),
[&_](const spv::ExecutionModel& model) {
switch (model) {
case spv::ExecutionModel::Geometry:
case spv::ExecutionModel::TessellationControl:
case spv::ExecutionModel::TessellationEvaluation:
return true;
case spv::ExecutionModel::MeshNV:
return _.HasCapability(spv::Capability::MeshShadingNV);
case spv::ExecutionModel::MeshEXT:
return _.HasCapability(spv::Capability::MeshShadingEXT);
default:
return false;
}
})) {
if (_.HasCapability(spv::Capability::MeshShadingNV) ||
_.HasCapability(spv::Capability::MeshShadingEXT)) {
return _.diag(SPV_ERROR_INVALID_DATA, inst)
<< "Execution mode can only be used with a Geometry, "
"tessellation, MeshNV or MeshEXT execution model.";
} else {
return _.diag(SPV_ERROR_INVALID_DATA, inst)
<< "Execution mode can only be used with a Geometry or "
"tessellation execution model.";
}
}
if (is_vulkan_env) {
if (_.HasCapability(spv::Capability::MeshShadingEXT) &&
inst->GetOperandAs<uint32_t>(2) == 0) {
return _.diag(SPV_ERROR_INVALID_DATA, inst)
<< _.VkErrorID(7330)
<< "In mesh shaders using the MeshEXT Execution Model the "
"OutputVertices Execution Mode must be greater than 0";
}
}
break;
case spv::ExecutionMode::OutputLinesEXT:
case spv::ExecutionMode::OutputTrianglesEXT:
case spv::ExecutionMode::OutputPrimitivesEXT:
if (!std::all_of(models->begin(), models->end(),
[](const spv::ExecutionModel& model) {
return (model == spv::ExecutionModel::MeshEXT ||
model == spv::ExecutionModel::MeshNV);
})) {
return _.diag(SPV_ERROR_INVALID_DATA, inst)
<< "Execution mode can only be used with the MeshEXT or MeshNV "
"execution "
"model.";
}
if (mode == spv::ExecutionMode::OutputPrimitivesEXT && is_vulkan_env) {
if (_.HasCapability(spv::Capability::MeshShadingEXT) &&
inst->GetOperandAs<uint32_t>(2) == 0) {
return _.diag(SPV_ERROR_INVALID_DATA, inst)
<< _.VkErrorID(7331)
<< "In mesh shaders using the MeshEXT Execution Model the "
"OutputPrimitivesEXT Execution Mode must be greater than 0";
}
}
break;
case spv::ExecutionMode::QuadDerivativesKHR:
if (!std::all_of(models->begin(), models->end(),
[](const spv::ExecutionModel& model) {
return (model == spv::ExecutionModel::Fragment ||
model == spv::ExecutionModel::GLCompute);
})) {
return _.diag(SPV_ERROR_INVALID_DATA, inst)
<< "Execution mode can only be used with the Fragment or "
"GLCompute execution model.";
}
break;
case spv::ExecutionMode::PixelCenterInteger:
case spv::ExecutionMode::OriginUpperLeft:
case spv::ExecutionMode::OriginLowerLeft:
case spv::ExecutionMode::EarlyFragmentTests:
case spv::ExecutionMode::DepthReplacing:
case spv::ExecutionMode::DepthGreater:
case spv::ExecutionMode::DepthLess:
case spv::ExecutionMode::DepthUnchanged:
case spv::ExecutionMode::NonCoherentColorAttachmentReadEXT:
case spv::ExecutionMode::NonCoherentDepthAttachmentReadEXT:
case spv::ExecutionMode::NonCoherentStencilAttachmentReadEXT:
case spv::ExecutionMode::PixelInterlockOrderedEXT:
case spv::ExecutionMode::PixelInterlockUnorderedEXT:
case spv::ExecutionMode::SampleInterlockOrderedEXT:
case spv::ExecutionMode::SampleInterlockUnorderedEXT:
case spv::ExecutionMode::ShadingRateInterlockOrderedEXT:
case spv::ExecutionMode::ShadingRateInterlockUnorderedEXT:
case spv::ExecutionMode::EarlyAndLateFragmentTestsAMD:
case spv::ExecutionMode::StencilRefUnchangedFrontAMD:
case spv::ExecutionMode::StencilRefGreaterFrontAMD:
case spv::ExecutionMode::StencilRefLessFrontAMD:
case spv::ExecutionMode::StencilRefUnchangedBackAMD:
case spv::ExecutionMode::StencilRefGreaterBackAMD:
case spv::ExecutionMode::StencilRefLessBackAMD:
case spv::ExecutionMode::RequireFullQuadsKHR:
if (!std::all_of(models->begin(), models->end(),
[](const spv::ExecutionModel& model) {
return model == spv::ExecutionModel::Fragment;
})) {
return _.diag(SPV_ERROR_INVALID_DATA, inst)
<< "Execution mode can only be used with the Fragment execution "
"model.";
}
break;
case spv::ExecutionMode::LocalSizeHint:
case spv::ExecutionMode::VecTypeHint:
case spv::ExecutionMode::ContractionOff:
case spv::ExecutionMode::LocalSizeHintId:
if (!std::all_of(models->begin(), models->end(),
[](const spv::ExecutionModel& model) {
return model == spv::ExecutionModel::Kernel;
})) {
return _.diag(SPV_ERROR_INVALID_DATA, inst)
<< "Execution mode can only be used with the Kernel execution "
"model.";
}
break;
case spv::ExecutionMode::LocalSize:
case spv::ExecutionMode::LocalSizeId:
if (mode == spv::ExecutionMode::LocalSizeId &&
!_.IsLocalSizeIdAllowed()) {
return _.diag(SPV_ERROR_INVALID_DATA, inst)
<< "LocalSizeId mode is not allowed by the current environment."
<< (is_vulkan_env
? _.MissingFeature("maintenance4 feature",
"--allow-localsizeid", false)
: "");
}
if (!std::all_of(
models->begin(), models->end(),
[&_](const spv::ExecutionModel& model) {
switch (model) {
case spv::ExecutionModel::Kernel:
case spv::ExecutionModel::GLCompute:
return true;
case spv::ExecutionModel::TaskNV:
case spv::ExecutionModel::MeshNV:
return _.HasCapability(spv::Capability::MeshShadingNV);
case spv::ExecutionModel::TaskEXT:
case spv::ExecutionModel::MeshEXT:
return _.HasCapability(spv::Capability::MeshShadingEXT);
default:
return false;
}
})) {
if (_.HasCapability(spv::Capability::MeshShadingNV) ||
_.HasCapability(spv::Capability::MeshShadingEXT)) {
return _.diag(SPV_ERROR_INVALID_DATA, inst)
<< "Execution mode can only be used with a Kernel, GLCompute, "
"MeshNV, MeshEXT, TaskNV or TaskEXT execution model.";
} else {
return _.diag(SPV_ERROR_INVALID_DATA, inst)
<< "Execution mode can only be used with a Kernel or "
"GLCompute "
"execution model.";
}
}
default:
break;
}
if (mode == spv::ExecutionMode::FPFastMathDefault) {
const auto* modes = _.GetExecutionModes(entry_point_id);
if (modes && modes->count(spv::ExecutionMode::ContractionOff)) {
return _.diag(SPV_ERROR_INVALID_DATA, inst)
<< "FPFastMathDefault and ContractionOff execution modes cannot "
"be applied to the same entry point";
}
if (modes && modes->count(spv::ExecutionMode::SignedZeroInfNanPreserve)) {
return _.diag(SPV_ERROR_INVALID_DATA, inst)
<< "FPFastMathDefault and SignedZeroInfNanPreserve execution "
"modes cannot be applied to the same entry point";
}
}
if (is_vulkan_env) {
if (mode == spv::ExecutionMode::OriginLowerLeft) {
return _.diag(SPV_ERROR_INVALID_DATA, inst)
<< _.VkErrorID(4653)
<< "In the Vulkan environment, the OriginLowerLeft execution mode "
"must not be used.";
}
if (mode == spv::ExecutionMode::PixelCenterInteger) {
return _.diag(SPV_ERROR_INVALID_DATA, inst)
<< _.VkErrorID(4654)
<< "In the Vulkan environment, the PixelCenterInteger execution "
"mode must not be used.";
}
if (mode == spv::ExecutionMode::TileShadingRateQCOM) {
const auto rateX = inst->GetOperandAs<int>(2);
const auto rateY = inst->GetOperandAs<int>(3);
if ((rateX & (rateX - 1)) != 0 || (rateY & (rateY - 1)) != 0)
return _.diag(SPV_ERROR_INVALID_DATA, inst)
<< "The TileShadingRateQCOM execution mode's x and y values "
"must be powers of 2.";
}
}
return SPV_SUCCESS;
}
spv_result_t ValidateMemoryModel(ValidationState_t& _,
const Instruction* inst) {
// Already produced an error if multiple memory model instructions are
// present.
if (_.memory_model() != spv::MemoryModel::VulkanKHR &&
_.HasCapability(spv::Capability::VulkanMemoryModelKHR)) {
return _.diag(SPV_ERROR_INVALID_DATA, inst)
<< "VulkanMemoryModelKHR capability must only be specified if "
"the VulkanKHR memory model is used.";
}
if (spvIsOpenCLEnv(_.context()->target_env)) {
if ((_.addressing_model() != spv::AddressingModel::Physical32) &&
(_.addressing_model() != spv::AddressingModel::Physical64)) {
return _.diag(SPV_ERROR_INVALID_DATA, inst)
<< "Addressing model must be Physical32 or Physical64 "
<< "in the OpenCL environment.";
}
if (_.memory_model() != spv::MemoryModel::OpenCL) {
return _.diag(SPV_ERROR_INVALID_DATA, inst)
<< "Memory model must be OpenCL in the OpenCL environment.";
}
}
if (spvIsVulkanEnv(_.context()->target_env)) {
if ((_.addressing_model() != spv::AddressingModel::Logical) &&
(_.addressing_model() !=
spv::AddressingModel::PhysicalStorageBuffer64)) {
return _.diag(SPV_ERROR_INVALID_DATA, inst)
<< _.VkErrorID(4635)
<< "Addressing model must be Logical or PhysicalStorageBuffer64 "
<< "in the Vulkan environment.";
}
}
return SPV_SUCCESS;
}
bool PerEntryExecutionMode(spv::ExecutionMode mode) {
switch (mode) {
// These execution modes can be specified multiple times per entry point.
case spv::ExecutionMode::DenormPreserve:
case spv::ExecutionMode::DenormFlushToZero:
case spv::ExecutionMode::SignedZeroInfNanPreserve:
case spv::ExecutionMode::RoundingModeRTE:
case spv::ExecutionMode::RoundingModeRTZ:
case spv::ExecutionMode::FPFastMathDefault:
case spv::ExecutionMode::RoundingModeRTPINTEL:
case spv::ExecutionMode::RoundingModeRTNINTEL:
case spv::ExecutionMode::FloatingPointModeALTINTEL:
case spv::ExecutionMode::FloatingPointModeIEEEINTEL:
return false;
default:
return true;
}
}
spv_result_t ValidateCapability(ValidationState_t& _, const Instruction* inst) {
auto cap = inst->GetOperandAs<spv::Capability>(0);
if (cap == spv::Capability::CooperativeMatrixKHR) {
if (_.HasCapability(spv::Capability::Shader) &&
!_.HasCapability(spv::Capability::VulkanMemoryModel)) {
return _.diag(SPV_ERROR_INVALID_CAPABILITY, inst)
<< "If the Shader and CooperativeMatrixKHR capabilities are "
"declared, the VulkanMemoryModel capability must also be "
"declared";
}
}
return SPV_SUCCESS;
}
} // namespace
spv_result_t ValidateFloatControls2(ValidationState_t& _) {
std::unordered_set<uint32_t> fp_fast_math_default_entry_points;
for (auto entry_point : _.entry_points()) {
const auto* exec_modes = _.GetExecutionModes(entry_point);
if (exec_modes &&
exec_modes->count(spv::ExecutionMode::FPFastMathDefault)) {
fp_fast_math_default_entry_points.insert(entry_point);
}
}
std::vector<std::pair<const Instruction*, spv::Decoration>> worklist;
for (const auto& inst : _.ordered_instructions()) {
if (inst.opcode() != spv::Op::OpDecorate) {
continue;
}
const auto decoration = inst.GetOperandAs<spv::Decoration>(1);
const auto target_id = inst.GetOperandAs<uint32_t>(0);
const auto target = _.FindDef(target_id);
if (decoration == spv::Decoration::NoContraction) {
worklist.push_back(std::make_pair(target, decoration));
} else if (decoration == spv::Decoration::FPFastMathMode) {
auto mask = inst.GetOperandAs<spv::FPFastMathModeMask>(2);
if ((mask & spv::FPFastMathModeMask::Fast) !=
spv::FPFastMathModeMask::MaskNone) {
worklist.push_back(std::make_pair(target, decoration));
}
}
}
std::unordered_set<const Instruction*> visited;
while (!worklist.empty()) {
const auto inst = worklist.back().first;
const auto decoration = worklist.back().second;
worklist.pop_back();
if (!visited.insert(inst).second) {
continue;
}
const auto function = inst->function();
if (function) {
const auto& entry_points = _.FunctionEntryPoints(function->id());
for (auto entry_point : entry_points) {
if (fp_fast_math_default_entry_points.count(entry_point)) {
const std::string dec = decoration == spv::Decoration::NoContraction
? "NoContraction"
: "FPFastMathMode Fast";
return _.diag(SPV_ERROR_INVALID_DATA, inst)
<< dec
<< " cannot be used by an entry point with the "
"FPFastMathDefault execution mode";
}
}
} else {
for (const auto& pair : inst->uses()) {
worklist.push_back(std::make_pair(pair.first, decoration));
}
}
}
return SPV_SUCCESS;
}
spv_result_t ModeSettingPass(ValidationState_t& _, const Instruction* inst) {
switch (inst->opcode()) {
case spv::Op::OpEntryPoint:
if (auto error = ValidateEntryPoint(_, inst)) return error;
break;
case spv::Op::OpExecutionMode:
case spv::Op::OpExecutionModeId:
if (auto error = ValidateExecutionMode(_, inst)) return error;
break;
case spv::Op::OpMemoryModel:
if (auto error = ValidateMemoryModel(_, inst)) return error;
break;
case spv::Op::OpCapability:
if (auto error = ValidateCapability(_, inst)) return error;
break;
default:
break;
}
return SPV_SUCCESS;
}
spv_result_t ValidateDuplicateExecutionModes(ValidationState_t& _) {
using PerEntryKey = std::tuple<spv::ExecutionMode, uint32_t>;
using PerOperandKey = std::tuple<spv::ExecutionMode, uint32_t, uint32_t>;
std::set<PerEntryKey> seen_per_entry;
std::set<PerOperandKey> seen_per_operand;
const auto lookupMode = [](spv::ExecutionMode mode) -> std::string {
const spvtools::OperandDesc* desc = nullptr;
if (spvtools::LookupOperand(SPV_OPERAND_TYPE_EXECUTION_MODE,
static_cast<uint32_t>(mode),
&desc) == SPV_SUCCESS) {
return std::string(desc->name().data());
}
return "Unknown";
};
for (const auto& inst : _.ordered_instructions()) {
if (inst.opcode() != spv::Op::OpExecutionMode &&
inst.opcode() != spv::Op::OpExecutionModeId) {
continue;
}
const auto entry = inst.GetOperandAs<uint32_t>(0);
const auto mode = inst.GetOperandAs<spv::ExecutionMode>(1);
if (PerEntryExecutionMode(mode)) {
if (!seen_per_entry.insert(std::make_tuple(mode, entry)).second) {
return _.diag(SPV_ERROR_INVALID_ID, &inst)
<< lookupMode(mode)
<< " execution mode must not be specified multiple times per "
"entry point";
}
} else {
// Execution modes allowed multiple times all take a single operand.
const auto operand = inst.GetOperandAs<uint32_t>(2);
if (!seen_per_operand.insert(std::make_tuple(mode, entry, operand))
.second) {
return _.diag(SPV_ERROR_INVALID_ID, &inst)
<< lookupMode(mode)
<< " execution mode must not be specified multiple times for "
"the same entry point and operands";
}
}
}
return SPV_SUCCESS;
}
} // namespace val
} // namespace spvtools
|