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
|
// Copyright (c) 2017 Valve Corporation
// Copyright (c) 2017 LunarG Inc.
//
// 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 <string>
#include "source/opt/simplification_pass.h"
#include "test/opt/pass_fixture.h"
#include "test/opt/pass_utils.h"
namespace spvtools {
namespace opt {
namespace {
using InsertExtractElimTest = PassTest<::testing::Test>;
TEST_F(InsertExtractElimTest, Simple) {
// Note: The SPIR-V assembly has had store/load elimination
// performed to allow the inserts and extracts to directly
// reference each other.
//
// #version 140
//
// in vec4 BaseColor;
//
// struct S_t {
// vec4 v0;
// vec4 v1;
// };
//
// void main()
// {
// S_t s0;
// s0.v1 = BaseColor;
// gl_FragColor = s0.v1;
// }
const std::string predefs =
R"(OpCapability Shader
%1 = OpExtInstImport "GLSL.std.450"
OpMemoryModel Logical GLSL450
OpEntryPoint Fragment %main "main" %BaseColor %gl_FragColor
OpExecutionMode %main OriginUpperLeft
OpSource GLSL 140
OpName %main "main"
OpName %S_t "S_t"
OpMemberName %S_t 0 "v0"
OpMemberName %S_t 1 "v1"
OpName %s0 "s0"
OpName %BaseColor "BaseColor"
OpName %gl_FragColor "gl_FragColor"
%void = OpTypeVoid
%8 = OpTypeFunction %void
%float = OpTypeFloat 32
%v4float = OpTypeVector %float 4
%S_t = OpTypeStruct %v4float %v4float
%_ptr_Function_S_t = OpTypePointer Function %S_t
%int = OpTypeInt 32 1
%int_1 = OpConstant %int 1
%_ptr_Input_v4float = OpTypePointer Input %v4float
%BaseColor = OpVariable %_ptr_Input_v4float Input
%_ptr_Function_v4float = OpTypePointer Function %v4float
%_ptr_Output_v4float = OpTypePointer Output %v4float
%gl_FragColor = OpVariable %_ptr_Output_v4float Output
)";
const std::string before =
R"(%main = OpFunction %void None %8
%17 = OpLabel
%s0 = OpVariable %_ptr_Function_S_t Function
%18 = OpLoad %v4float %BaseColor
%19 = OpLoad %S_t %s0
%20 = OpCompositeInsert %S_t %18 %19 1
OpStore %s0 %20
%21 = OpCompositeExtract %v4float %20 1
OpStore %gl_FragColor %21
OpReturn
OpFunctionEnd
)";
const std::string after =
R"(%main = OpFunction %void None %8
%17 = OpLabel
%s0 = OpVariable %_ptr_Function_S_t Function
%18 = OpLoad %v4float %BaseColor
%19 = OpLoad %S_t %s0
%20 = OpCompositeInsert %S_t %18 %19 1
OpStore %s0 %20
OpStore %gl_FragColor %18
OpReturn
OpFunctionEnd
)";
SinglePassRunAndCheck<SimplificationPass>(predefs + before, predefs + after,
true, true);
}
TEST_F(InsertExtractElimTest, OptimizeAcrossNonConflictingInsert) {
// Note: The SPIR-V assembly has had store/load elimination
// performed to allow the inserts and extracts to directly
// reference each other.
//
// #version 140
//
// in vec4 BaseColor;
//
// struct S_t {
// vec4 v0;
// vec4 v1;
// };
//
// void main()
// {
// S_t s0;
// s0.v1 = BaseColor;
// s0.v0[2] = 0.0;
// gl_FragColor = s0.v1;
// }
const std::string predefs =
R"(OpCapability Shader
%1 = OpExtInstImport "GLSL.std.450"
OpMemoryModel Logical GLSL450
OpEntryPoint Fragment %main "main" %BaseColor %gl_FragColor
OpExecutionMode %main OriginUpperLeft
OpSource GLSL 140
OpName %main "main"
OpName %S_t "S_t"
OpMemberName %S_t 0 "v0"
OpMemberName %S_t 1 "v1"
OpName %s0 "s0"
OpName %BaseColor "BaseColor"
OpName %gl_FragColor "gl_FragColor"
%void = OpTypeVoid
%8 = OpTypeFunction %void
%float = OpTypeFloat 32
%v4float = OpTypeVector %float 4
%S_t = OpTypeStruct %v4float %v4float
%_ptr_Function_S_t = OpTypePointer Function %S_t
%int = OpTypeInt 32 1
%int_1 = OpConstant %int 1
%float_0 = OpConstant %float 0
%_ptr_Input_v4float = OpTypePointer Input %v4float
%BaseColor = OpVariable %_ptr_Input_v4float Input
%_ptr_Function_v4float = OpTypePointer Function %v4float
%_ptr_Output_v4float = OpTypePointer Output %v4float
%gl_FragColor = OpVariable %_ptr_Output_v4float Output
)";
const std::string before =
R"(%main = OpFunction %void None %8
%18 = OpLabel
%s0 = OpVariable %_ptr_Function_S_t Function
%19 = OpLoad %v4float %BaseColor
%20 = OpLoad %S_t %s0
%21 = OpCompositeInsert %S_t %19 %20 1
%22 = OpCompositeInsert %S_t %float_0 %21 0 2
OpStore %s0 %22
%23 = OpCompositeExtract %v4float %22 1
OpStore %gl_FragColor %23
OpReturn
OpFunctionEnd
)";
const std::string after =
R"(%main = OpFunction %void None %8
%18 = OpLabel
%s0 = OpVariable %_ptr_Function_S_t Function
%19 = OpLoad %v4float %BaseColor
%20 = OpLoad %S_t %s0
%21 = OpCompositeInsert %S_t %19 %20 1
%22 = OpCompositeInsert %S_t %float_0 %21 0 2
OpStore %s0 %22
OpStore %gl_FragColor %19
OpReturn
OpFunctionEnd
)";
SinglePassRunAndCheck<SimplificationPass>(predefs + before, predefs + after,
true, true);
}
TEST_F(InsertExtractElimTest, OptimizeOpaque) {
// SPIR-V not representable in GLSL; not generatable from HLSL
// for the moment.
const std::string predefs =
R"(OpCapability Shader
%1 = OpExtInstImport "GLSL.std.450"
OpMemoryModel Logical GLSL450
OpEntryPoint Fragment %main "main" %outColor %texCoords
OpExecutionMode %main OriginUpperLeft
OpSource GLSL 140
OpName %main "main"
OpName %S_t "S_t"
OpMemberName %S_t 0 "v0"
OpMemberName %S_t 1 "v1"
OpMemberName %S_t 2 "smp"
OpName %outColor "outColor"
OpName %sampler15 "sampler15"
OpName %s0 "s0"
OpName %texCoords "texCoords"
OpDecorate %sampler15 DescriptorSet 0
%void = OpTypeVoid
%9 = OpTypeFunction %void
%float = OpTypeFloat 32
%v2float = OpTypeVector %float 2
%v4float = OpTypeVector %float 4
%_ptr_Output_v4float = OpTypePointer Output %v4float
%outColor = OpVariable %_ptr_Output_v4float Output
%14 = OpTypeImage %float 2D 0 0 0 1 Unknown
%15 = OpTypeSampledImage %14
%S_t = OpTypeStruct %v2float %v2float %15
%_ptr_Function_S_t = OpTypePointer Function %S_t
%17 = OpTypeFunction %void %_ptr_Function_S_t
%_ptr_UniformConstant_15 = OpTypePointer UniformConstant %15
%_ptr_Function_15 = OpTypePointer Function %15
%sampler15 = OpVariable %_ptr_UniformConstant_15 UniformConstant
%int = OpTypeInt 32 1
%int_0 = OpConstant %int 0
%int_2 = OpConstant %int 2
%_ptr_Function_v2float = OpTypePointer Function %v2float
%_ptr_Input_v2float = OpTypePointer Input %v2float
%texCoords = OpVariable %_ptr_Input_v2float Input
)";
const std::string before =
R"(%main = OpFunction %void None %9
%25 = OpLabel
%s0 = OpVariable %_ptr_Function_S_t Function
%26 = OpLoad %v2float %texCoords
%27 = OpLoad %S_t %s0
%28 = OpCompositeInsert %S_t %26 %27 0
%29 = OpLoad %15 %sampler15
%30 = OpCompositeInsert %S_t %29 %28 2
OpStore %s0 %30
%31 = OpCompositeExtract %15 %30 2
%32 = OpCompositeExtract %v2float %30 0
%33 = OpImageSampleImplicitLod %v4float %31 %32
OpStore %outColor %33
OpReturn
OpFunctionEnd
)";
const std::string after =
R"(%main = OpFunction %void None %9
%25 = OpLabel
%s0 = OpVariable %_ptr_Function_S_t Function
%26 = OpLoad %v2float %texCoords
%27 = OpLoad %S_t %s0
%28 = OpCompositeInsert %S_t %26 %27 0
%29 = OpLoad %15 %sampler15
%30 = OpCompositeInsert %S_t %29 %28 2
OpStore %s0 %30
%33 = OpImageSampleImplicitLod %v4float %29 %26
OpStore %outColor %33
OpReturn
OpFunctionEnd
)";
SinglePassRunAndCheck<SimplificationPass>(predefs + before, predefs + after,
true, true);
}
TEST_F(InsertExtractElimTest, OptimizeNestedStruct) {
// The following HLSL has been pre-optimized to get the SPIR-V:
// struct S0
// {
// int x;
// SamplerState ss;
// };
//
// struct S1
// {
// float b;
// S0 s0;
// };
//
// struct S2
// {
// int a1;
// S1 resources;
// };
//
// SamplerState samp;
// Texture2D tex;
//
// float4 main(float4 vpos : VPOS) : COLOR0
// {
// S1 s1;
// S2 s2;
// s1.s0.ss = samp;
// s2.resources = s1;
// return tex.Sample(s2.resources.s0.ss, float2(0.5));
// }
const std::string predefs =
R"(OpCapability Shader
%1 = OpExtInstImport "GLSL.std.450"
OpMemoryModel Logical GLSL450
OpEntryPoint Fragment %main "main" %_entryPointOutput
OpExecutionMode %main OriginUpperLeft
OpSource HLSL 500
OpName %main "main"
OpName %S0 "S0"
OpMemberName %S0 0 "x"
OpMemberName %S0 1 "ss"
OpName %S1 "S1"
OpMemberName %S1 0 "b"
OpMemberName %S1 1 "s0"
OpName %samp "samp"
OpName %S2 "S2"
OpMemberName %S2 0 "a1"
OpMemberName %S2 1 "resources"
OpName %tex "tex"
OpName %_entryPointOutput "@entryPointOutput"
OpDecorate %samp DescriptorSet 0
OpDecorate %tex DescriptorSet 0
OpDecorate %_entryPointOutput Location 0
%void = OpTypeVoid
%10 = OpTypeFunction %void
%float = OpTypeFloat 32
%v4float = OpTypeVector %float 4
%_ptr_Function_v4float = OpTypePointer Function %v4float
%14 = OpTypeFunction %v4float %_ptr_Function_v4float
%int = OpTypeInt 32 1
%16 = OpTypeSampler
%S0 = OpTypeStruct %int %16
%S1 = OpTypeStruct %float %S0
%_ptr_Function_S1 = OpTypePointer Function %S1
%int_1 = OpConstant %int 1
%_ptr_UniformConstant_16 = OpTypePointer UniformConstant %16
%samp = OpVariable %_ptr_UniformConstant_16 UniformConstant
%_ptr_Function_16 = OpTypePointer Function %16
%S2 = OpTypeStruct %int %S1
%_ptr_Function_S2 = OpTypePointer Function %S2
%22 = OpTypeImage %float 2D 0 0 0 1 Unknown
%_ptr_UniformConstant_22 = OpTypePointer UniformConstant %22
%tex = OpVariable %_ptr_UniformConstant_22 UniformConstant
%24 = OpTypeSampledImage %22
%v2float = OpTypeVector %float 2
%float_0_5 = OpConstant %float 0.5
%27 = OpConstantComposite %v2float %float_0_5 %float_0_5
%_ptr_Input_v4float = OpTypePointer Input %v4float
%_ptr_Output_v4float = OpTypePointer Output %v4float
%_entryPointOutput = OpVariable %_ptr_Output_v4float Output
)";
const std::string before =
R"(%main = OpFunction %void None %10
%30 = OpLabel
%31 = OpVariable %_ptr_Function_S1 Function
%32 = OpVariable %_ptr_Function_S2 Function
%33 = OpLoad %16 %samp
%34 = OpLoad %S1 %31
%35 = OpCompositeInsert %S1 %33 %34 1 1
OpStore %31 %35
%36 = OpLoad %S2 %32
%37 = OpCompositeInsert %S2 %35 %36 1
OpStore %32 %37
%38 = OpLoad %22 %tex
%39 = OpCompositeExtract %16 %37 1 1 1
%40 = OpSampledImage %24 %38 %39
%41 = OpImageSampleImplicitLod %v4float %40 %27
OpStore %_entryPointOutput %41
OpReturn
OpFunctionEnd
)";
const std::string after =
R"(%main = OpFunction %void None %10
%30 = OpLabel
%31 = OpVariable %_ptr_Function_S1 Function
%32 = OpVariable %_ptr_Function_S2 Function
%33 = OpLoad %16 %samp
%34 = OpLoad %S1 %31
%35 = OpCompositeInsert %S1 %33 %34 1 1
OpStore %31 %35
%36 = OpLoad %S2 %32
%37 = OpCompositeInsert %S2 %35 %36 1
OpStore %32 %37
%38 = OpLoad %22 %tex
%40 = OpSampledImage %24 %38 %33
%41 = OpImageSampleImplicitLod %v4float %40 %27
OpStore %_entryPointOutput %41
OpReturn
OpFunctionEnd
)";
SinglePassRunAndCheck<SimplificationPass>(predefs + before, predefs + after,
true, true);
}
TEST_F(InsertExtractElimTest, ConflictingInsertPreventsOptimization) {
// Note: The SPIR-V assembly has had store/load elimination
// performed to allow the inserts and extracts to directly
// reference each other.
//
// #version 140
//
// in vec4 BaseColor;
//
// struct S_t {
// vec4 v0;
// vec4 v1;
// };
//
// void main()
// {
// S_t s0;
// s0.v1 = BaseColor;
// s0.v1[2] = 0.0;
// gl_FragColor = s0.v1;
// }
const std::string assembly =
R"(OpCapability Shader
%1 = OpExtInstImport "GLSL.std.450"
OpMemoryModel Logical GLSL450
OpEntryPoint Fragment %main "main" %BaseColor %gl_FragColor
OpExecutionMode %main OriginUpperLeft
OpSource GLSL 140
OpName %main "main"
OpName %S_t "S_t"
OpMemberName %S_t 0 "v0"
OpMemberName %S_t 1 "v1"
OpName %s0 "s0"
OpName %BaseColor "BaseColor"
OpName %gl_FragColor "gl_FragColor"
%void = OpTypeVoid
%8 = OpTypeFunction %void
%float = OpTypeFloat 32
%v4float = OpTypeVector %float 4
%S_t = OpTypeStruct %v4float %v4float
%_ptr_Function_S_t = OpTypePointer Function %S_t
%int = OpTypeInt 32 1
%int_1 = OpConstant %int 1
%float_0 = OpConstant %float 0
%_ptr_Input_v4float = OpTypePointer Input %v4float
%BaseColor = OpVariable %_ptr_Input_v4float Input
%_ptr_Function_v4float = OpTypePointer Function %v4float
%_ptr_Output_v4float = OpTypePointer Output %v4float
%gl_FragColor = OpVariable %_ptr_Output_v4float Output
%main = OpFunction %void None %8
%18 = OpLabel
%s0 = OpVariable %_ptr_Function_S_t Function
%19 = OpLoad %v4float %BaseColor
%20 = OpLoad %S_t %s0
%21 = OpCompositeInsert %S_t %19 %20 1
%22 = OpCompositeInsert %S_t %float_0 %21 1 2
OpStore %s0 %22
%23 = OpCompositeExtract %v4float %22 1
OpStore %gl_FragColor %23
OpReturn
OpFunctionEnd
)";
SinglePassRunAndCheck<SimplificationPass>(assembly, assembly, true, true);
}
TEST_F(InsertExtractElimTest, ConflictingInsertPreventsOptimization2) {
// Note: The SPIR-V assembly has had store/load elimination
// performed to allow the inserts and extracts to directly
// reference each other.
//
// #version 140
//
// in vec4 BaseColor;
//
// struct S_t {
// vec4 v0;
// vec4 v1;
// };
//
// void main()
// {
// S_t s0;
// s0.v1[1] = 1.0; // dead
// s0.v1 = Baseline;
// gl_FragColor = vec4(s0.v1[1], 0.0, 0.0, 0.0);
// }
const std::string before_predefs =
R"(OpCapability Shader
%1 = OpExtInstImport "GLSL.std.450"
OpMemoryModel Logical GLSL450
OpEntryPoint Fragment %main "main" %BaseColor %gl_FragColor
OpExecutionMode %main OriginUpperLeft
OpSource GLSL 140
OpName %main "main"
OpName %S_t "S_t"
OpMemberName %S_t 0 "v0"
OpMemberName %S_t 1 "v1"
OpName %s0 "s0"
OpName %BaseColor "BaseColor"
OpName %gl_FragColor "gl_FragColor"
%void = OpTypeVoid
%8 = OpTypeFunction %void
%float = OpTypeFloat 32
%v4float = OpTypeVector %float 4
%S_t = OpTypeStruct %v4float %v4float
%_ptr_Function_S_t = OpTypePointer Function %S_t
%int = OpTypeInt 32 1
%int_1 = OpConstant %int 1
%float_1 = OpConstant %float 1
%uint = OpTypeInt 32 0
%uint_1 = OpConstant %uint 1
%_ptr_Function_float = OpTypePointer Function %float
%_ptr_Input_v4float = OpTypePointer Input %v4float
%BaseColor = OpVariable %_ptr_Input_v4float Input
%_ptr_Function_v4float = OpTypePointer Function %v4float
%_ptr_Output_v4float = OpTypePointer Output %v4float
%gl_FragColor = OpVariable %_ptr_Output_v4float Output
%float_0 = OpConstant %float 0
)";
const std::string after_predefs =
R"(OpCapability Shader
%1 = OpExtInstImport "GLSL.std.450"
OpMemoryModel Logical GLSL450
OpEntryPoint Fragment %main "main" %BaseColor %gl_FragColor
OpExecutionMode %main OriginUpperLeft
OpSource GLSL 140
OpName %main "main"
OpName %S_t "S_t"
OpMemberName %S_t 0 "v0"
OpMemberName %S_t 1 "v1"
OpName %s0 "s0"
OpName %BaseColor "BaseColor"
OpName %gl_FragColor "gl_FragColor"
%void = OpTypeVoid
%8 = OpTypeFunction %void
%float = OpTypeFloat 32
%v4float = OpTypeVector %float 4
%S_t = OpTypeStruct %v4float %v4float
%_ptr_Function_S_t = OpTypePointer Function %S_t
%int = OpTypeInt 32 1
%int_1 = OpConstant %int 1
%float_1 = OpConstant %float 1
%uint = OpTypeInt 32 0
%uint_1 = OpConstant %uint 1
%_ptr_Function_float = OpTypePointer Function %float
%_ptr_Input_v4float = OpTypePointer Input %v4float
%BaseColor = OpVariable %_ptr_Input_v4float Input
%_ptr_Function_v4float = OpTypePointer Function %v4float
%_ptr_Output_v4float = OpTypePointer Output %v4float
%gl_FragColor = OpVariable %_ptr_Output_v4float Output
%float_0 = OpConstant %float 0
)";
const std::string before =
R"(%main = OpFunction %void None %8
%22 = OpLabel
%s0 = OpVariable %_ptr_Function_S_t Function
%23 = OpLoad %S_t %s0
%24 = OpCompositeInsert %S_t %float_1 %23 1 1
%25 = OpLoad %v4float %BaseColor
%26 = OpCompositeInsert %S_t %25 %24 1
%27 = OpCompositeExtract %float %26 1 1
%28 = OpCompositeConstruct %v4float %27 %float_0 %float_0 %float_0
OpStore %gl_FragColor %28
OpReturn
OpFunctionEnd
)";
const std::string after =
R"(%main = OpFunction %void None %8
%22 = OpLabel
%s0 = OpVariable %_ptr_Function_S_t Function
%23 = OpLoad %S_t %s0
%24 = OpCompositeInsert %S_t %float_1 %23 1 1
%25 = OpLoad %v4float %BaseColor
%26 = OpCompositeInsert %S_t %25 %24 1
%27 = OpCompositeExtract %float %25 1
%28 = OpCompositeConstruct %v4float %27 %float_0 %float_0 %float_0
OpStore %gl_FragColor %28
OpReturn
OpFunctionEnd
)";
SinglePassRunAndCheck<SimplificationPass>(before_predefs + before,
after_predefs + after, true, true);
}
TEST_F(InsertExtractElimTest, MixWithConstants) {
// Extract component of FMix with 0.0 or 1.0 as the a-value.
//
// Note: The SPIR-V assembly has had store/load elimination
// performed to allow the inserts and extracts to directly
// reference each other.
//
// #version 450
//
// layout (location=0) in float bc;
// layout (location=1) in float bc2;
// layout (location=2) in float m;
// layout (location=3) in float m2;
// layout (location=0) out vec4 OutColor;
//
// void main()
// {
// vec4 bcv = vec4(bc, bc2, 0.0, 1.0);
// vec4 bcv2 = vec4(bc2, bc, 1.0, 0.0);
// vec4 v = mix(bcv, bcv2, vec4(0.0,1.0,m,m2));
// OutColor = vec4(v.y);
// }
const std::string predefs =
R"(OpCapability Shader
%1 = OpExtInstImport "GLSL.std.450"
OpMemoryModel Logical GLSL450
OpEntryPoint Fragment %main "main" %bc %bc2 %m %m2 %OutColor
OpExecutionMode %main OriginUpperLeft
OpSource GLSL 450
OpName %main "main"
OpName %bc "bc"
OpName %bc2 "bc2"
OpName %m "m"
OpName %m2 "m2"
OpName %OutColor "OutColor"
OpDecorate %bc Location 0
OpDecorate %bc2 Location 1
OpDecorate %m Location 2
OpDecorate %m2 Location 3
OpDecorate %OutColor Location 0
%void = OpTypeVoid
%9 = OpTypeFunction %void
%float = OpTypeFloat 32
%v4float = OpTypeVector %float 4
%_ptr_Function_v4float = OpTypePointer Function %v4float
%_ptr_Input_float = OpTypePointer Input %float
%bc = OpVariable %_ptr_Input_float Input
%bc2 = OpVariable %_ptr_Input_float Input
%float_0 = OpConstant %float 0
%float_1 = OpConstant %float 1
%m = OpVariable %_ptr_Input_float Input
%m2 = OpVariable %_ptr_Input_float Input
%_ptr_Output_v4float = OpTypePointer Output %v4float
%OutColor = OpVariable %_ptr_Output_v4float Output
%uint = OpTypeInt 32 0
%_ptr_Function_float = OpTypePointer Function %float
)";
const std::string before =
R"(%main = OpFunction %void None %9
%19 = OpLabel
%20 = OpLoad %float %bc
%21 = OpLoad %float %bc2
%22 = OpCompositeConstruct %v4float %20 %21 %float_0 %float_1
%23 = OpLoad %float %bc2
%24 = OpLoad %float %bc
%25 = OpCompositeConstruct %v4float %23 %24 %float_1 %float_0
%26 = OpLoad %float %m
%27 = OpLoad %float %m2
%28 = OpCompositeConstruct %v4float %float_0 %float_1 %26 %27
%29 = OpExtInst %v4float %1 FMix %22 %25 %28
%30 = OpCompositeExtract %float %29 1
%31 = OpCompositeConstruct %v4float %30 %30 %30 %30
OpStore %OutColor %31
OpReturn
OpFunctionEnd
)";
const std::string after =
R"(%main = OpFunction %void None %9
%19 = OpLabel
%20 = OpLoad %float %bc
%21 = OpLoad %float %bc2
%22 = OpCompositeConstruct %v4float %20 %21 %float_0 %float_1
%23 = OpLoad %float %bc2
%24 = OpLoad %float %bc
%25 = OpCompositeConstruct %v4float %23 %24 %float_1 %float_0
%26 = OpLoad %float %m
%27 = OpLoad %float %m2
%28 = OpCompositeConstruct %v4float %float_0 %float_1 %26 %27
%29 = OpExtInst %v4float %1 FMix %22 %25 %28
%31 = OpCompositeConstruct %v4float %24 %24 %24 %24
OpStore %OutColor %31
OpReturn
OpFunctionEnd
)";
SinglePassRunAndCheck<SimplificationPass>(predefs + before, predefs + after,
true, true);
}
TEST_F(InsertExtractElimTest, VectorShuffle1) {
// Extract component from first vector in VectorShuffle
//
// Note: The SPIR-V assembly has had store/load elimination
// performed to allow the inserts and extracts to directly
// reference each other.
//
// #version 450
//
// layout (location=0) in float bc;
// layout (location=1) in float bc2;
// layout (location=0) out vec4 OutColor;
//
// void main()
// {
// vec4 bcv = vec4(bc, bc2, 0.0, 1.0);
// vec4 v = bcv.zwxy;
// OutColor = vec4(v.y);
// }
const std::string predefs_before =
R"(OpCapability Shader
%1 = OpExtInstImport "GLSL.std.450"
OpMemoryModel Logical GLSL450
OpEntryPoint Fragment %main "main" %bc %bc2 %OutColor
OpExecutionMode %main OriginUpperLeft
OpSource GLSL 450
OpName %main "main"
OpName %bc "bc"
OpName %bc2 "bc2"
OpName %OutColor "OutColor"
OpDecorate %bc Location 0
OpDecorate %bc2 Location 1
OpDecorate %OutColor Location 0
%void = OpTypeVoid
%7 = OpTypeFunction %void
%float = OpTypeFloat 32
%v4float = OpTypeVector %float 4
%_ptr_Function_v4float = OpTypePointer Function %v4float
%_ptr_Input_float = OpTypePointer Input %float
%bc = OpVariable %_ptr_Input_float Input
%bc2 = OpVariable %_ptr_Input_float Input
%float_0 = OpConstant %float 0
%float_1 = OpConstant %float 1
%_ptr_Output_v4float = OpTypePointer Output %v4float
%OutColor = OpVariable %_ptr_Output_v4float Output
%uint = OpTypeInt 32 0
%_ptr_Function_float = OpTypePointer Function %float
)";
const std::string predefs_after = predefs_before +
"%24 = OpConstantComposite %v4float "
"%float_1 %float_1 %float_1 %float_1\n";
const std::string before =
R"(%main = OpFunction %void None %7
%17 = OpLabel
%18 = OpLoad %float %bc
%19 = OpLoad %float %bc2
%20 = OpCompositeConstruct %v4float %18 %19 %float_0 %float_1
%21 = OpVectorShuffle %v4float %20 %20 2 3 0 1
%22 = OpCompositeExtract %float %21 1
%23 = OpCompositeConstruct %v4float %22 %22 %22 %22
OpStore %OutColor %23
OpReturn
OpFunctionEnd
)";
const std::string after =
R"(%main = OpFunction %void None %7
%17 = OpLabel
%18 = OpLoad %float %bc
%19 = OpLoad %float %bc2
%20 = OpCompositeConstruct %v4float %18 %19 %float_0 %float_1
%21 = OpVectorShuffle %v4float %20 %20 2 3 0 1
OpStore %OutColor %24
OpReturn
OpFunctionEnd
)";
SinglePassRunAndCheck<SimplificationPass>(predefs_before + before,
predefs_after + after, true, true);
}
TEST_F(InsertExtractElimTest, VectorShuffle2) {
// Extract component from second vector in VectorShuffle
// Identical to test VectorShuffle1 except for the vector
// shuffle index of 7.
//
// Note: The SPIR-V assembly has had store/load elimination
// performed to allow the inserts and extracts to directly
// reference each other.
//
// #version 450
//
// layout (location=0) in float bc;
// layout (location=1) in float bc2;
// layout (location=0) out vec4 OutColor;
//
// void main()
// {
// vec4 bcv = vec4(bc, bc2, 0.0, 1.0);
// vec4 v = bcv.zwxy;
// OutColor = vec4(v.y);
// }
const std::string predefs_before =
R"(OpCapability Shader
%1 = OpExtInstImport "GLSL.std.450"
OpMemoryModel Logical GLSL450
OpEntryPoint Fragment %main "main" %bc %bc2 %OutColor
OpExecutionMode %main OriginUpperLeft
OpSource GLSL 450
OpName %main "main"
OpName %bc "bc"
OpName %bc2 "bc2"
OpName %OutColor "OutColor"
OpDecorate %bc Location 0
OpDecorate %bc2 Location 1
OpDecorate %OutColor Location 0
%void = OpTypeVoid
%7 = OpTypeFunction %void
%float = OpTypeFloat 32
%v4float = OpTypeVector %float 4
%_ptr_Function_v4float = OpTypePointer Function %v4float
%_ptr_Input_float = OpTypePointer Input %float
%bc = OpVariable %_ptr_Input_float Input
%bc2 = OpVariable %_ptr_Input_float Input
%float_0 = OpConstant %float 0
%float_1 = OpConstant %float 1
%_ptr_Output_v4float = OpTypePointer Output %v4float
%OutColor = OpVariable %_ptr_Output_v4float Output
%uint = OpTypeInt 32 0
%_ptr_Function_float = OpTypePointer Function %float
)";
const std::string predefs_after =
R"(OpCapability Shader
%1 = OpExtInstImport "GLSL.std.450"
OpMemoryModel Logical GLSL450
OpEntryPoint Fragment %main "main" %bc %bc2 %OutColor
OpExecutionMode %main OriginUpperLeft
OpSource GLSL 450
OpName %main "main"
OpName %bc "bc"
OpName %bc2 "bc2"
OpName %OutColor "OutColor"
OpDecorate %bc Location 0
OpDecorate %bc2 Location 1
OpDecorate %OutColor Location 0
%void = OpTypeVoid
%7 = OpTypeFunction %void
%float = OpTypeFloat 32
%v4float = OpTypeVector %float 4
%_ptr_Function_v4float = OpTypePointer Function %v4float
%_ptr_Input_float = OpTypePointer Input %float
%bc = OpVariable %_ptr_Input_float Input
%bc2 = OpVariable %_ptr_Input_float Input
%float_0 = OpConstant %float 0
%float_1 = OpConstant %float 1
%_ptr_Output_v4float = OpTypePointer Output %v4float
%OutColor = OpVariable %_ptr_Output_v4float Output
%uint = OpTypeInt 32 0
%_ptr_Function_float = OpTypePointer Function %float
%24 = OpConstantComposite %v4float %float_1 %float_1 %float_1 %float_1
)";
const std::string before =
R"(%main = OpFunction %void None %7
%17 = OpLabel
%18 = OpLoad %float %bc
%19 = OpLoad %float %bc2
%20 = OpCompositeConstruct %v4float %18 %19 %float_0 %float_1
%21 = OpVectorShuffle %v4float %20 %20 2 7 0 1
%22 = OpCompositeExtract %float %21 1
%23 = OpCompositeConstruct %v4float %22 %22 %22 %22
OpStore %OutColor %23
OpReturn
OpFunctionEnd
)";
const std::string after =
R"(%main = OpFunction %void None %7
%17 = OpLabel
%18 = OpLoad %float %bc
%19 = OpLoad %float %bc2
%20 = OpCompositeConstruct %v4float %18 %19 %float_0 %float_1
%21 = OpVectorShuffle %v4float %20 %20 2 7 0 1
OpStore %OutColor %24
OpReturn
OpFunctionEnd
)";
SinglePassRunAndCheck<SimplificationPass>(predefs_before + before,
predefs_after + after, true, true);
}
// TODO(greg-lunarg): Add tests to verify handling of these cases:
//
} // namespace
} // namespace opt
} // namespace spvtools
|