| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019 |
- // Copyright (c) 2017 Google 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 <array>
- #include <memory>
- #include <string>
- #include <vector>
- #include "gmock/gmock.h"
- #include "source/opt/dominator_analysis.h"
- #include "source/opt/iterator.h"
- #include "source/opt/pass.h"
- #include "test/opt/assembly_builder.h"
- #include "test/opt/function_utils.h"
- #include "test/opt/pass_fixture.h"
- #include "test/opt/pass_utils.h"
- namespace spvtools {
- namespace opt {
- namespace {
- using ::testing::UnorderedElementsAre;
- using PassClassTest = PassTest<::testing::Test>;
- // Check that x dominates y, and
- // if x != y then
- // x strictly dominates y and
- // y does not dominate x and
- // y does not strictly dominate x
- // if x == x then
- // x does not strictly dominate itself
- void check_dominance(const DominatorAnalysisBase& dom_tree, const Function* fn,
- uint32_t x, uint32_t y) {
- SCOPED_TRACE("Check dominance properties for Basic Block " +
- std::to_string(x) + " and " + std::to_string(y));
- EXPECT_TRUE(dom_tree.Dominates(spvtest::GetBasicBlock(fn, x),
- spvtest::GetBasicBlock(fn, y)));
- EXPECT_TRUE(dom_tree.Dominates(x, y));
- if (x == y) {
- EXPECT_FALSE(dom_tree.StrictlyDominates(x, x));
- } else {
- EXPECT_TRUE(dom_tree.StrictlyDominates(x, y));
- EXPECT_FALSE(dom_tree.Dominates(y, x));
- EXPECT_FALSE(dom_tree.StrictlyDominates(y, x));
- }
- }
- // Check that x does not dominates y and vice versa
- void check_no_dominance(const DominatorAnalysisBase& dom_tree,
- const Function* fn, uint32_t x, uint32_t y) {
- SCOPED_TRACE("Check no domination for Basic Block " + std::to_string(x) +
- " and " + std::to_string(y));
- EXPECT_FALSE(dom_tree.Dominates(spvtest::GetBasicBlock(fn, x),
- spvtest::GetBasicBlock(fn, y)));
- EXPECT_FALSE(dom_tree.Dominates(x, y));
- EXPECT_FALSE(dom_tree.StrictlyDominates(spvtest::GetBasicBlock(fn, x),
- spvtest::GetBasicBlock(fn, y)));
- EXPECT_FALSE(dom_tree.StrictlyDominates(x, y));
- EXPECT_FALSE(dom_tree.Dominates(spvtest::GetBasicBlock(fn, y),
- spvtest::GetBasicBlock(fn, x)));
- EXPECT_FALSE(dom_tree.Dominates(y, x));
- EXPECT_FALSE(dom_tree.StrictlyDominates(spvtest::GetBasicBlock(fn, y),
- spvtest::GetBasicBlock(fn, x)));
- EXPECT_FALSE(dom_tree.StrictlyDominates(y, x));
- }
- TEST_F(PassClassTest, DominatorSimpleCFG) {
- const std::string text = R"(
- OpCapability Addresses
- OpCapability Kernel
- OpMemoryModel Physical64 OpenCL
- OpEntryPoint Kernel %1 "main"
- %2 = OpTypeVoid
- %3 = OpTypeFunction %2
- %4 = OpTypeBool
- %5 = OpTypeInt 32 0
- %6 = OpConstant %5 0
- %7 = OpConstantFalse %4
- %8 = OpConstantTrue %4
- %9 = OpConstant %5 1
- %1 = OpFunction %2 None %3
- %10 = OpLabel
- OpBranch %11
- %11 = OpLabel
- OpSwitch %6 %12 1 %13
- %12 = OpLabel
- OpBranch %14
- %13 = OpLabel
- OpBranch %14
- %14 = OpLabel
- OpBranchConditional %8 %11 %15
- %15 = OpLabel
- OpReturn
- OpFunctionEnd
- )";
- // clang-format on
- std::unique_ptr<IRContext> context =
- BuildModule(SPV_ENV_UNIVERSAL_1_0, nullptr, text,
- SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS);
- Module* module = context->module();
- EXPECT_NE(nullptr, module) << "Assembling failed for shader:\n"
- << text << std::endl;
- const Function* fn = spvtest::GetFunction(module, 1);
- const BasicBlock* entry = spvtest::GetBasicBlock(fn, 10);
- EXPECT_EQ(entry, fn->entry().get())
- << "The entry node is not the expected one";
- // Test normal dominator tree
- {
- DominatorAnalysis dom_tree;
- const CFG& cfg = *context->cfg();
- dom_tree.InitializeTree(cfg, fn);
- // Inspect the actual tree
- DominatorTree& tree = dom_tree.GetDomTree();
- EXPECT_EQ(tree.GetRoot()->bb_, cfg.pseudo_entry_block());
- EXPECT_TRUE(
- dom_tree.Dominates(cfg.pseudo_entry_block()->id(), entry->id()));
- // (strict) dominance checks
- for (uint32_t id : {10, 11, 12, 13, 14, 15})
- check_dominance(dom_tree, fn, id, id);
- check_dominance(dom_tree, fn, 10, 11);
- check_dominance(dom_tree, fn, 10, 12);
- check_dominance(dom_tree, fn, 10, 13);
- check_dominance(dom_tree, fn, 10, 14);
- check_dominance(dom_tree, fn, 10, 15);
- check_dominance(dom_tree, fn, 11, 12);
- check_dominance(dom_tree, fn, 11, 13);
- check_dominance(dom_tree, fn, 11, 14);
- check_dominance(dom_tree, fn, 11, 15);
- check_dominance(dom_tree, fn, 14, 15);
- check_no_dominance(dom_tree, fn, 12, 13);
- check_no_dominance(dom_tree, fn, 12, 14);
- check_no_dominance(dom_tree, fn, 13, 14);
- // check with some invalid inputs
- EXPECT_FALSE(dom_tree.Dominates(nullptr, entry));
- EXPECT_FALSE(dom_tree.Dominates(entry, nullptr));
- EXPECT_FALSE(dom_tree.Dominates(static_cast<BasicBlock*>(nullptr),
- static_cast<BasicBlock*>(nullptr)));
- EXPECT_FALSE(dom_tree.Dominates(10, 1));
- EXPECT_FALSE(dom_tree.Dominates(1, 10));
- EXPECT_FALSE(dom_tree.Dominates(1, 1));
- EXPECT_FALSE(dom_tree.StrictlyDominates(nullptr, entry));
- EXPECT_FALSE(dom_tree.StrictlyDominates(entry, nullptr));
- EXPECT_FALSE(dom_tree.StrictlyDominates(nullptr, nullptr));
- EXPECT_FALSE(dom_tree.StrictlyDominates(10, 1));
- EXPECT_FALSE(dom_tree.StrictlyDominates(1, 10));
- EXPECT_FALSE(dom_tree.StrictlyDominates(1, 1));
- EXPECT_EQ(dom_tree.ImmediateDominator(cfg.pseudo_entry_block()), nullptr);
- EXPECT_EQ(dom_tree.ImmediateDominator(entry), cfg.pseudo_entry_block());
- EXPECT_EQ(dom_tree.ImmediateDominator(nullptr), nullptr);
- EXPECT_EQ(dom_tree.ImmediateDominator(spvtest::GetBasicBlock(fn, 11)),
- spvtest::GetBasicBlock(fn, 10));
- EXPECT_EQ(dom_tree.ImmediateDominator(spvtest::GetBasicBlock(fn, 12)),
- spvtest::GetBasicBlock(fn, 11));
- EXPECT_EQ(dom_tree.ImmediateDominator(spvtest::GetBasicBlock(fn, 13)),
- spvtest::GetBasicBlock(fn, 11));
- EXPECT_EQ(dom_tree.ImmediateDominator(spvtest::GetBasicBlock(fn, 14)),
- spvtest::GetBasicBlock(fn, 11));
- EXPECT_EQ(dom_tree.ImmediateDominator(spvtest::GetBasicBlock(fn, 15)),
- spvtest::GetBasicBlock(fn, 14));
- }
- // Test post dominator tree
- {
- PostDominatorAnalysis dom_tree;
- const CFG& cfg = *context->cfg();
- dom_tree.InitializeTree(cfg, fn);
- // Inspect the actual tree
- DominatorTree& tree = dom_tree.GetDomTree();
- EXPECT_EQ(tree.GetRoot()->bb_, cfg.pseudo_exit_block());
- EXPECT_TRUE(dom_tree.Dominates(cfg.pseudo_exit_block()->id(), 15));
- // (strict) dominance checks
- for (uint32_t id : {10, 11, 12, 13, 14, 15})
- check_dominance(dom_tree, fn, id, id);
- check_dominance(dom_tree, fn, 14, 10);
- check_dominance(dom_tree, fn, 14, 11);
- check_dominance(dom_tree, fn, 14, 12);
- check_dominance(dom_tree, fn, 14, 13);
- check_dominance(dom_tree, fn, 15, 10);
- check_dominance(dom_tree, fn, 15, 11);
- check_dominance(dom_tree, fn, 15, 12);
- check_dominance(dom_tree, fn, 15, 13);
- check_dominance(dom_tree, fn, 15, 14);
- check_no_dominance(dom_tree, fn, 13, 12);
- check_no_dominance(dom_tree, fn, 12, 11);
- check_no_dominance(dom_tree, fn, 13, 11);
- // check with some invalid inputs
- EXPECT_FALSE(dom_tree.Dominates(nullptr, entry));
- EXPECT_FALSE(dom_tree.Dominates(entry, nullptr));
- EXPECT_FALSE(dom_tree.Dominates(static_cast<BasicBlock*>(nullptr),
- static_cast<BasicBlock*>(nullptr)));
- EXPECT_FALSE(dom_tree.Dominates(10, 1));
- EXPECT_FALSE(dom_tree.Dominates(1, 10));
- EXPECT_FALSE(dom_tree.Dominates(1, 1));
- EXPECT_FALSE(dom_tree.StrictlyDominates(nullptr, entry));
- EXPECT_FALSE(dom_tree.StrictlyDominates(entry, nullptr));
- EXPECT_FALSE(dom_tree.StrictlyDominates(nullptr, nullptr));
- EXPECT_FALSE(dom_tree.StrictlyDominates(10, 1));
- EXPECT_FALSE(dom_tree.StrictlyDominates(1, 10));
- EXPECT_FALSE(dom_tree.StrictlyDominates(1, 1));
- EXPECT_EQ(dom_tree.ImmediateDominator(nullptr), nullptr);
- EXPECT_EQ(dom_tree.ImmediateDominator(spvtest::GetBasicBlock(fn, 11)),
- spvtest::GetBasicBlock(fn, 14));
- EXPECT_EQ(dom_tree.ImmediateDominator(spvtest::GetBasicBlock(fn, 12)),
- spvtest::GetBasicBlock(fn, 14));
- EXPECT_EQ(dom_tree.ImmediateDominator(spvtest::GetBasicBlock(fn, 13)),
- spvtest::GetBasicBlock(fn, 14));
- EXPECT_EQ(dom_tree.ImmediateDominator(spvtest::GetBasicBlock(fn, 14)),
- spvtest::GetBasicBlock(fn, 15));
- EXPECT_EQ(dom_tree.ImmediateDominator(spvtest::GetBasicBlock(fn, 15)),
- cfg.pseudo_exit_block());
- EXPECT_EQ(dom_tree.ImmediateDominator(cfg.pseudo_exit_block()), nullptr);
- }
- }
- TEST_F(PassClassTest, DominatorIrreducibleCFG) {
- const std::string text = R"(
- OpCapability Addresses
- OpCapability Kernel
- OpMemoryModel Physical64 OpenCL
- OpEntryPoint Kernel %1 "main"
- %2 = OpTypeVoid
- %3 = OpTypeFunction %2
- %4 = OpTypeBool
- %5 = OpTypeInt 32 0
- %6 = OpConstantFalse %4
- %7 = OpConstantTrue %4
- %1 = OpFunction %2 None %3
- %8 = OpLabel
- OpBranch %9
- %9 = OpLabel
- OpBranchConditional %7 %10 %11
- %10 = OpLabel
- OpBranch %11
- %11 = OpLabel
- OpBranchConditional %7 %10 %12
- %12 = OpLabel
- OpReturn
- OpFunctionEnd
- )";
- // clang-format on
- std::unique_ptr<IRContext> context =
- BuildModule(SPV_ENV_UNIVERSAL_1_0, nullptr, text,
- SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS);
- Module* module = context->module();
- EXPECT_NE(nullptr, module) << "Assembling failed for shader:\n"
- << text << std::endl;
- const Function* fn = spvtest::GetFunction(module, 1);
- const BasicBlock* entry = spvtest::GetBasicBlock(fn, 8);
- EXPECT_EQ(entry, fn->entry().get())
- << "The entry node is not the expected one";
- // Check normal dominator tree
- {
- DominatorAnalysis dom_tree;
- const CFG& cfg = *context->cfg();
- dom_tree.InitializeTree(cfg, fn);
- // Inspect the actual tree
- DominatorTree& tree = dom_tree.GetDomTree();
- EXPECT_EQ(tree.GetRoot()->bb_, cfg.pseudo_entry_block());
- EXPECT_TRUE(
- dom_tree.Dominates(cfg.pseudo_entry_block()->id(), entry->id()));
- // (strict) dominance checks
- for (uint32_t id : {8, 9, 10, 11, 12})
- check_dominance(dom_tree, fn, id, id);
- check_dominance(dom_tree, fn, 8, 9);
- check_dominance(dom_tree, fn, 8, 10);
- check_dominance(dom_tree, fn, 8, 11);
- check_dominance(dom_tree, fn, 8, 12);
- check_dominance(dom_tree, fn, 9, 10);
- check_dominance(dom_tree, fn, 9, 11);
- check_dominance(dom_tree, fn, 9, 12);
- check_dominance(dom_tree, fn, 11, 12);
- check_no_dominance(dom_tree, fn, 10, 11);
- EXPECT_EQ(dom_tree.ImmediateDominator(cfg.pseudo_entry_block()), nullptr);
- EXPECT_EQ(dom_tree.ImmediateDominator(entry), cfg.pseudo_entry_block());
- EXPECT_EQ(dom_tree.ImmediateDominator(spvtest::GetBasicBlock(fn, 9)),
- spvtest::GetBasicBlock(fn, 8));
- EXPECT_EQ(dom_tree.ImmediateDominator(spvtest::GetBasicBlock(fn, 10)),
- spvtest::GetBasicBlock(fn, 9));
- EXPECT_EQ(dom_tree.ImmediateDominator(spvtest::GetBasicBlock(fn, 11)),
- spvtest::GetBasicBlock(fn, 9));
- EXPECT_EQ(dom_tree.ImmediateDominator(spvtest::GetBasicBlock(fn, 12)),
- spvtest::GetBasicBlock(fn, 11));
- }
- // Check post dominator tree
- {
- PostDominatorAnalysis dom_tree;
- const CFG& cfg = *context->cfg();
- dom_tree.InitializeTree(cfg, fn);
- // Inspect the actual tree
- DominatorTree& tree = dom_tree.GetDomTree();
- EXPECT_EQ(tree.GetRoot()->bb_, cfg.pseudo_exit_block());
- EXPECT_TRUE(dom_tree.Dominates(cfg.pseudo_exit_block()->id(), 12));
- // (strict) dominance checks
- for (uint32_t id : {8, 9, 10, 11, 12})
- check_dominance(dom_tree, fn, id, id);
- check_dominance(dom_tree, fn, 12, 8);
- check_dominance(dom_tree, fn, 12, 10);
- check_dominance(dom_tree, fn, 12, 11);
- check_dominance(dom_tree, fn, 12, 12);
- check_dominance(dom_tree, fn, 11, 8);
- check_dominance(dom_tree, fn, 11, 9);
- check_dominance(dom_tree, fn, 11, 10);
- check_dominance(dom_tree, fn, 9, 8);
- EXPECT_EQ(dom_tree.ImmediateDominator(entry),
- spvtest::GetBasicBlock(fn, 9));
- EXPECT_EQ(dom_tree.ImmediateDominator(spvtest::GetBasicBlock(fn, 9)),
- spvtest::GetBasicBlock(fn, 11));
- EXPECT_EQ(dom_tree.ImmediateDominator(spvtest::GetBasicBlock(fn, 10)),
- spvtest::GetBasicBlock(fn, 11));
- EXPECT_EQ(dom_tree.ImmediateDominator(spvtest::GetBasicBlock(fn, 11)),
- spvtest::GetBasicBlock(fn, 12));
- EXPECT_EQ(dom_tree.ImmediateDominator(spvtest::GetBasicBlock(fn, 12)),
- cfg.pseudo_exit_block());
- EXPECT_EQ(dom_tree.ImmediateDominator(cfg.pseudo_exit_block()), nullptr);
- }
- }
- TEST_F(PassClassTest, DominatorLoopToSelf) {
- const std::string text = R"(
- OpCapability Addresses
- OpCapability Kernel
- OpMemoryModel Physical64 OpenCL
- OpEntryPoint Kernel %1 "main"
- %2 = OpTypeVoid
- %3 = OpTypeFunction %2
- %4 = OpTypeBool
- %5 = OpTypeInt 32 0
- %6 = OpConstant %5 0
- %7 = OpConstantFalse %4
- %8 = OpConstantTrue %4
- %9 = OpConstant %5 1
- %1 = OpFunction %2 None %3
- %10 = OpLabel
- OpBranch %11
- %11 = OpLabel
- OpSwitch %6 %12 1 %11
- %12 = OpLabel
- OpReturn
- OpFunctionEnd
- )";
- // clang-format on
- std::unique_ptr<IRContext> context =
- BuildModule(SPV_ENV_UNIVERSAL_1_0, nullptr, text,
- SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS);
- Module* module = context->module();
- EXPECT_NE(nullptr, module) << "Assembling failed for shader:\n"
- << text << std::endl;
- const Function* fn = spvtest::GetFunction(module, 1);
- const BasicBlock* entry = spvtest::GetBasicBlock(fn, 10);
- EXPECT_EQ(entry, fn->entry().get())
- << "The entry node is not the expected one";
- // Check normal dominator tree
- {
- DominatorAnalysis dom_tree;
- const CFG& cfg = *context->cfg();
- dom_tree.InitializeTree(cfg, fn);
- // Inspect the actual tree
- DominatorTree& tree = dom_tree.GetDomTree();
- EXPECT_EQ(tree.GetRoot()->bb_, cfg.pseudo_entry_block());
- EXPECT_TRUE(
- dom_tree.Dominates(cfg.pseudo_entry_block()->id(), entry->id()));
- // (strict) dominance checks
- for (uint32_t id : {10, 11, 12}) check_dominance(dom_tree, fn, id, id);
- check_dominance(dom_tree, fn, 10, 11);
- check_dominance(dom_tree, fn, 10, 12);
- check_dominance(dom_tree, fn, 11, 12);
- EXPECT_EQ(dom_tree.ImmediateDominator(cfg.pseudo_entry_block()), nullptr);
- EXPECT_EQ(dom_tree.ImmediateDominator(entry), cfg.pseudo_entry_block());
- EXPECT_EQ(dom_tree.ImmediateDominator(spvtest::GetBasicBlock(fn, 11)),
- spvtest::GetBasicBlock(fn, 10));
- EXPECT_EQ(dom_tree.ImmediateDominator(spvtest::GetBasicBlock(fn, 12)),
- spvtest::GetBasicBlock(fn, 11));
- std::array<uint32_t, 3> node_order = {{10, 11, 12}};
- {
- // Test dominator tree iteration order.
- DominatorTree::iterator node_it = dom_tree.GetDomTree().begin();
- DominatorTree::iterator node_end = dom_tree.GetDomTree().end();
- for (uint32_t id : node_order) {
- EXPECT_NE(node_it, node_end);
- EXPECT_EQ(node_it->id(), id);
- node_it++;
- }
- EXPECT_EQ(node_it, node_end);
- }
- {
- // Same as above, but with const iterators.
- DominatorTree::const_iterator node_it = dom_tree.GetDomTree().cbegin();
- DominatorTree::const_iterator node_end = dom_tree.GetDomTree().cend();
- for (uint32_t id : node_order) {
- EXPECT_NE(node_it, node_end);
- EXPECT_EQ(node_it->id(), id);
- node_it++;
- }
- EXPECT_EQ(node_it, node_end);
- }
- {
- // Test dominator tree iteration order.
- DominatorTree::post_iterator node_it = dom_tree.GetDomTree().post_begin();
- DominatorTree::post_iterator node_end = dom_tree.GetDomTree().post_end();
- for (uint32_t id : make_range(node_order.rbegin(), node_order.rend())) {
- EXPECT_NE(node_it, node_end);
- EXPECT_EQ(node_it->id(), id);
- node_it++;
- }
- EXPECT_EQ(node_it, node_end);
- }
- {
- // Same as above, but with const iterators.
- DominatorTree::const_post_iterator node_it =
- dom_tree.GetDomTree().post_cbegin();
- DominatorTree::const_post_iterator node_end =
- dom_tree.GetDomTree().post_cend();
- for (uint32_t id : make_range(node_order.rbegin(), node_order.rend())) {
- EXPECT_NE(node_it, node_end);
- EXPECT_EQ(node_it->id(), id);
- node_it++;
- }
- EXPECT_EQ(node_it, node_end);
- }
- }
- // Check post dominator tree
- {
- PostDominatorAnalysis dom_tree;
- const CFG& cfg = *context->cfg();
- dom_tree.InitializeTree(cfg, fn);
- // Inspect the actual tree
- DominatorTree& tree = dom_tree.GetDomTree();
- EXPECT_EQ(tree.GetRoot()->bb_, cfg.pseudo_exit_block());
- EXPECT_TRUE(dom_tree.Dominates(cfg.pseudo_exit_block()->id(), 12));
- // (strict) dominance checks
- for (uint32_t id : {10, 11, 12}) check_dominance(dom_tree, fn, id, id);
- check_dominance(dom_tree, fn, 12, 10);
- check_dominance(dom_tree, fn, 12, 11);
- check_dominance(dom_tree, fn, 12, 12);
- EXPECT_EQ(dom_tree.ImmediateDominator(entry),
- spvtest::GetBasicBlock(fn, 11));
- EXPECT_EQ(dom_tree.ImmediateDominator(spvtest::GetBasicBlock(fn, 11)),
- spvtest::GetBasicBlock(fn, 12));
- EXPECT_EQ(dom_tree.ImmediateDominator(cfg.pseudo_exit_block()), nullptr);
- EXPECT_EQ(dom_tree.ImmediateDominator(spvtest::GetBasicBlock(fn, 12)),
- cfg.pseudo_exit_block());
- std::array<uint32_t, 3> node_order = {{12, 11, 10}};
- {
- // Test dominator tree iteration order.
- DominatorTree::iterator node_it = tree.begin();
- DominatorTree::iterator node_end = tree.end();
- for (uint32_t id : node_order) {
- EXPECT_NE(node_it, node_end);
- EXPECT_EQ(node_it->id(), id);
- node_it++;
- }
- EXPECT_EQ(node_it, node_end);
- }
- {
- // Same as above, but with const iterators.
- DominatorTree::const_iterator node_it = tree.cbegin();
- DominatorTree::const_iterator node_end = tree.cend();
- for (uint32_t id : node_order) {
- EXPECT_NE(node_it, node_end);
- EXPECT_EQ(node_it->id(), id);
- node_it++;
- }
- EXPECT_EQ(node_it, node_end);
- }
- {
- // Test dominator tree iteration order.
- DominatorTree::post_iterator node_it = dom_tree.GetDomTree().post_begin();
- DominatorTree::post_iterator node_end = dom_tree.GetDomTree().post_end();
- for (uint32_t id : make_range(node_order.rbegin(), node_order.rend())) {
- EXPECT_NE(node_it, node_end);
- EXPECT_EQ(node_it->id(), id);
- node_it++;
- }
- EXPECT_EQ(node_it, node_end);
- }
- {
- // Same as above, but with const iterators.
- DominatorTree::const_post_iterator node_it =
- dom_tree.GetDomTree().post_cbegin();
- DominatorTree::const_post_iterator node_end =
- dom_tree.GetDomTree().post_cend();
- for (uint32_t id : make_range(node_order.rbegin(), node_order.rend())) {
- EXPECT_NE(node_it, node_end);
- EXPECT_EQ(node_it->id(), id);
- node_it++;
- }
- EXPECT_EQ(node_it, node_end);
- }
- }
- }
- TEST_F(PassClassTest, DominatorUnreachableInLoop) {
- const std::string text = R"(
- OpCapability Addresses
- OpCapability Kernel
- OpMemoryModel Physical64 OpenCL
- OpEntryPoint Kernel %1 "main"
- %2 = OpTypeVoid
- %3 = OpTypeFunction %2
- %4 = OpTypeBool
- %5 = OpTypeInt 32 0
- %6 = OpConstant %5 0
- %7 = OpConstantFalse %4
- %8 = OpConstantTrue %4
- %9 = OpConstant %5 1
- %1 = OpFunction %2 None %3
- %10 = OpLabel
- OpBranch %11
- %11 = OpLabel
- OpSwitch %6 %12 1 %13
- %12 = OpLabel
- OpBranch %14
- %13 = OpLabel
- OpUnreachable
- %14 = OpLabel
- OpBranchConditional %8 %11 %15
- %15 = OpLabel
- OpReturn
- OpFunctionEnd
- )";
- // clang-format on
- std::unique_ptr<IRContext> context =
- BuildModule(SPV_ENV_UNIVERSAL_1_0, nullptr, text,
- SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS);
- Module* module = context->module();
- EXPECT_NE(nullptr, module) << "Assembling failed for shader:\n"
- << text << std::endl;
- const Function* fn = spvtest::GetFunction(module, 1);
- const BasicBlock* entry = spvtest::GetBasicBlock(fn, 10);
- EXPECT_EQ(entry, fn->entry().get())
- << "The entry node is not the expected one";
- // Check normal dominator tree
- {
- DominatorAnalysis dom_tree;
- const CFG& cfg = *context->cfg();
- dom_tree.InitializeTree(cfg, fn);
- // Inspect the actual tree
- DominatorTree& tree = dom_tree.GetDomTree();
- EXPECT_EQ(tree.GetRoot()->bb_, cfg.pseudo_entry_block());
- EXPECT_TRUE(
- dom_tree.Dominates(cfg.pseudo_entry_block()->id(), entry->id()));
- // (strict) dominance checks
- for (uint32_t id : {10, 11, 12, 13, 14, 15})
- check_dominance(dom_tree, fn, id, id);
- check_dominance(dom_tree, fn, 10, 11);
- check_dominance(dom_tree, fn, 10, 13);
- check_dominance(dom_tree, fn, 10, 12);
- check_dominance(dom_tree, fn, 10, 14);
- check_dominance(dom_tree, fn, 10, 15);
- check_dominance(dom_tree, fn, 11, 12);
- check_dominance(dom_tree, fn, 11, 13);
- check_dominance(dom_tree, fn, 11, 14);
- check_dominance(dom_tree, fn, 11, 15);
- check_dominance(dom_tree, fn, 12, 14);
- check_dominance(dom_tree, fn, 12, 15);
- check_dominance(dom_tree, fn, 14, 15);
- check_no_dominance(dom_tree, fn, 13, 12);
- check_no_dominance(dom_tree, fn, 13, 14);
- check_no_dominance(dom_tree, fn, 13, 15);
- EXPECT_EQ(dom_tree.ImmediateDominator(cfg.pseudo_entry_block()), nullptr);
- EXPECT_EQ(dom_tree.ImmediateDominator(entry), cfg.pseudo_entry_block());
- EXPECT_EQ(dom_tree.ImmediateDominator(spvtest::GetBasicBlock(fn, 11)),
- spvtest::GetBasicBlock(fn, 10));
- EXPECT_EQ(dom_tree.ImmediateDominator(spvtest::GetBasicBlock(fn, 12)),
- spvtest::GetBasicBlock(fn, 11));
- EXPECT_EQ(dom_tree.ImmediateDominator(spvtest::GetBasicBlock(fn, 13)),
- spvtest::GetBasicBlock(fn, 11));
- EXPECT_EQ(dom_tree.ImmediateDominator(spvtest::GetBasicBlock(fn, 14)),
- spvtest::GetBasicBlock(fn, 12));
- EXPECT_EQ(dom_tree.ImmediateDominator(spvtest::GetBasicBlock(fn, 15)),
- spvtest::GetBasicBlock(fn, 14));
- }
- // Check post dominator tree.
- {
- PostDominatorAnalysis dom_tree;
- const CFG& cfg = *context->cfg();
- dom_tree.InitializeTree(cfg, fn);
- // (strict) dominance checks.
- for (uint32_t id : {10, 11, 12, 13, 14, 15})
- check_dominance(dom_tree, fn, id, id);
- check_no_dominance(dom_tree, fn, 15, 10);
- check_no_dominance(dom_tree, fn, 15, 11);
- check_no_dominance(dom_tree, fn, 15, 12);
- check_no_dominance(dom_tree, fn, 15, 13);
- check_no_dominance(dom_tree, fn, 15, 14);
- check_dominance(dom_tree, fn, 14, 12);
- check_no_dominance(dom_tree, fn, 13, 10);
- check_no_dominance(dom_tree, fn, 13, 11);
- check_no_dominance(dom_tree, fn, 13, 12);
- check_no_dominance(dom_tree, fn, 13, 14);
- check_no_dominance(dom_tree, fn, 13, 15);
- EXPECT_EQ(dom_tree.ImmediateDominator(spvtest::GetBasicBlock(fn, 10)),
- spvtest::GetBasicBlock(fn, 11));
- EXPECT_EQ(dom_tree.ImmediateDominator(spvtest::GetBasicBlock(fn, 12)),
- spvtest::GetBasicBlock(fn, 14));
- EXPECT_EQ(dom_tree.ImmediateDominator(cfg.pseudo_exit_block()), nullptr);
- EXPECT_EQ(dom_tree.ImmediateDominator(spvtest::GetBasicBlock(fn, 15)),
- cfg.pseudo_exit_block());
- EXPECT_EQ(dom_tree.ImmediateDominator(spvtest::GetBasicBlock(fn, 13)),
- cfg.pseudo_exit_block());
- EXPECT_EQ(dom_tree.ImmediateDominator(spvtest::GetBasicBlock(fn, 14)),
- cfg.pseudo_exit_block());
- EXPECT_EQ(dom_tree.ImmediateDominator(spvtest::GetBasicBlock(fn, 11)),
- cfg.pseudo_exit_block());
- }
- }
- TEST_F(PassClassTest, DominatorInfinitLoop) {
- const std::string text = R"(
- OpCapability Addresses
- OpCapability Kernel
- OpMemoryModel Physical64 OpenCL
- OpEntryPoint Kernel %1 "main"
- %2 = OpTypeVoid
- %3 = OpTypeFunction %2
- %4 = OpTypeBool
- %5 = OpTypeInt 32 0
- %6 = OpConstant %5 0
- %7 = OpConstantFalse %4
- %8 = OpConstantTrue %4
- %9 = OpConstant %5 1
- %1 = OpFunction %2 None %3
- %10 = OpLabel
- OpBranch %11
- %11 = OpLabel
- OpSwitch %6 %12 1 %13
- %12 = OpLabel
- OpReturn
- %13 = OpLabel
- OpBranch %13
- OpFunctionEnd
- )";
- // clang-format on
- std::unique_ptr<IRContext> context =
- BuildModule(SPV_ENV_UNIVERSAL_1_0, nullptr, text,
- SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS);
- Module* module = context->module();
- EXPECT_NE(nullptr, module) << "Assembling failed for shader:\n"
- << text << std::endl;
- const Function* fn = spvtest::GetFunction(module, 1);
- const BasicBlock* entry = spvtest::GetBasicBlock(fn, 10);
- EXPECT_EQ(entry, fn->entry().get())
- << "The entry node is not the expected one";
- // Check normal dominator tree
- {
- DominatorAnalysis dom_tree;
- const CFG& cfg = *context->cfg();
- dom_tree.InitializeTree(cfg, fn);
- // Inspect the actual tree
- DominatorTree& tree = dom_tree.GetDomTree();
- EXPECT_EQ(tree.GetRoot()->bb_, cfg.pseudo_entry_block());
- EXPECT_TRUE(
- dom_tree.Dominates(cfg.pseudo_entry_block()->id(), entry->id()));
- // (strict) dominance checks
- for (uint32_t id : {10, 11, 12, 13}) check_dominance(dom_tree, fn, id, id);
- check_dominance(dom_tree, fn, 10, 11);
- check_dominance(dom_tree, fn, 10, 12);
- check_dominance(dom_tree, fn, 10, 13);
- check_dominance(dom_tree, fn, 11, 12);
- check_dominance(dom_tree, fn, 11, 13);
- check_no_dominance(dom_tree, fn, 13, 12);
- EXPECT_EQ(dom_tree.ImmediateDominator(cfg.pseudo_entry_block()), nullptr);
- EXPECT_EQ(dom_tree.ImmediateDominator(entry), cfg.pseudo_entry_block());
- EXPECT_EQ(dom_tree.ImmediateDominator(spvtest::GetBasicBlock(fn, 11)),
- spvtest::GetBasicBlock(fn, 10));
- EXPECT_EQ(dom_tree.ImmediateDominator(spvtest::GetBasicBlock(fn, 12)),
- spvtest::GetBasicBlock(fn, 11));
- EXPECT_EQ(dom_tree.ImmediateDominator(spvtest::GetBasicBlock(fn, 13)),
- spvtest::GetBasicBlock(fn, 11));
- }
- // Check post dominator tree
- {
- PostDominatorAnalysis dom_tree;
- const CFG& cfg = *context->cfg();
- dom_tree.InitializeTree(cfg, fn);
- // Inspect the actual tree
- DominatorTree& tree = dom_tree.GetDomTree();
- EXPECT_EQ(tree.GetRoot()->bb_, cfg.pseudo_exit_block());
- EXPECT_TRUE(dom_tree.Dominates(cfg.pseudo_exit_block()->id(), 12));
- // (strict) dominance checks
- for (uint32_t id : {10, 11, 12}) check_dominance(dom_tree, fn, id, id);
- check_dominance(dom_tree, fn, 12, 11);
- check_dominance(dom_tree, fn, 12, 10);
- // 13 should be completely out of tree as it's unreachable from exit nodes
- check_no_dominance(dom_tree, fn, 12, 13);
- check_no_dominance(dom_tree, fn, 11, 13);
- check_no_dominance(dom_tree, fn, 10, 13);
- EXPECT_EQ(dom_tree.ImmediateDominator(cfg.pseudo_exit_block()), nullptr);
- EXPECT_EQ(dom_tree.ImmediateDominator(spvtest::GetBasicBlock(fn, 12)),
- cfg.pseudo_exit_block());
- EXPECT_EQ(dom_tree.ImmediateDominator(spvtest::GetBasicBlock(fn, 10)),
- spvtest::GetBasicBlock(fn, 11));
- EXPECT_EQ(dom_tree.ImmediateDominator(spvtest::GetBasicBlock(fn, 11)),
- spvtest::GetBasicBlock(fn, 12));
- }
- }
- TEST_F(PassClassTest, DominatorUnreachableFromEntry) {
- const std::string text = R"(
- OpCapability Addresses
- OpCapability Addresses
- OpCapability Kernel
- OpMemoryModel Physical64 OpenCL
- OpEntryPoint Kernel %1 "main"
- %2 = OpTypeVoid
- %3 = OpTypeFunction %2
- %4 = OpTypeBool
- %5 = OpTypeInt 32 0
- %6 = OpConstantFalse %4
- %7 = OpConstantTrue %4
- %1 = OpFunction %2 None %3
- %8 = OpLabel
- OpBranch %9
- %9 = OpLabel
- OpReturn
- %10 = OpLabel
- OpBranch %9
- OpFunctionEnd
- )";
- // clang-format on
- std::unique_ptr<IRContext> context =
- BuildModule(SPV_ENV_UNIVERSAL_1_0, nullptr, text,
- SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS);
- Module* module = context->module();
- EXPECT_NE(nullptr, module) << "Assembling failed for shader:\n"
- << text << std::endl;
- const Function* fn = spvtest::GetFunction(module, 1);
- const BasicBlock* entry = spvtest::GetBasicBlock(fn, 8);
- EXPECT_EQ(entry, fn->entry().get())
- << "The entry node is not the expected one";
- // Check dominator tree
- {
- DominatorAnalysis dom_tree;
- const CFG& cfg = *context->cfg();
- dom_tree.InitializeTree(cfg, fn);
- // Inspect the actual tree
- DominatorTree& tree = dom_tree.GetDomTree();
- EXPECT_EQ(tree.GetRoot()->bb_, cfg.pseudo_entry_block());
- EXPECT_TRUE(
- dom_tree.Dominates(cfg.pseudo_entry_block()->id(), entry->id()));
- // (strict) dominance checks
- for (uint32_t id : {8, 9}) check_dominance(dom_tree, fn, id, id);
- check_dominance(dom_tree, fn, 8, 9);
- check_no_dominance(dom_tree, fn, 10, 8);
- check_no_dominance(dom_tree, fn, 10, 9);
- EXPECT_EQ(dom_tree.ImmediateDominator(cfg.pseudo_entry_block()), nullptr);
- EXPECT_EQ(dom_tree.ImmediateDominator(entry), cfg.pseudo_entry_block());
- EXPECT_EQ(dom_tree.ImmediateDominator(spvtest::GetBasicBlock(fn, 9)),
- spvtest::GetBasicBlock(fn, 8));
- EXPECT_EQ(dom_tree.ImmediateDominator(spvtest::GetBasicBlock(fn, 10)),
- nullptr);
- }
- // Check post dominator tree
- {
- PostDominatorAnalysis dom_tree;
- const CFG& cfg = *context->cfg();
- dom_tree.InitializeTree(cfg, fn);
- // Inspect the actual tree
- DominatorTree& tree = dom_tree.GetDomTree();
- EXPECT_EQ(tree.GetRoot()->bb_, cfg.pseudo_exit_block());
- EXPECT_TRUE(dom_tree.Dominates(cfg.pseudo_exit_block()->id(), 9));
- // (strict) dominance checks
- for (uint32_t id : {8, 9, 10}) check_dominance(dom_tree, fn, id, id);
- check_dominance(dom_tree, fn, 9, 8);
- check_dominance(dom_tree, fn, 9, 10);
- EXPECT_EQ(dom_tree.ImmediateDominator(entry),
- spvtest::GetBasicBlock(fn, 9));
- EXPECT_EQ(dom_tree.ImmediateDominator(cfg.pseudo_exit_block()), nullptr);
- EXPECT_EQ(dom_tree.ImmediateDominator(spvtest::GetBasicBlock(fn, 9)),
- cfg.pseudo_exit_block());
- EXPECT_EQ(dom_tree.ImmediateDominator(spvtest::GetBasicBlock(fn, 10)),
- spvtest::GetBasicBlock(fn, 9));
- }
- }
- TEST_F(PassClassTest, DominationForInstructions) {
- const std::string text = R"(
- OpCapability Shader
- %1 = OpExtInstImport "GLSL.std.450"
- OpMemoryModel Logical GLSL450
- OpEntryPoint Fragment %4 "main"
- OpExecutionMode %4 OriginUpperLeft
- OpSource ESSL 310
- %2 = OpTypeVoid
- %3 = OpTypeFunction %2
- %6 = OpTypeInt 32 1
- %7 = OpTypeBool
- %8 = OpConstantTrue %7
- %9 = OpConstant %6 37
- %10 = OpConstant %6 3
- %13 = OpConstant %6 5
- %4 = OpFunction %2 None %3
- %5 = OpLabel
- %12 = OpIAdd %6 %9 %10
- %15 = OpISub %6 %12 %13
- OpSelectionMerge %18 None
- OpBranchConditional %8 %16 %17
- %16 = OpLabel
- %20 = OpISub %6 %12 %13
- OpBranch %18
- %17 = OpLabel
- %21 = OpISub %6 %12 %13
- OpBranch %18
- %18 = OpLabel
- %22 = OpISub %6 %12 %13
- OpReturn
- OpFunctionEnd
- )";
- // clang-format on
- std::unique_ptr<IRContext> context =
- BuildModule(SPV_ENV_UNIVERSAL_1_0, nullptr, text,
- SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS);
- EXPECT_NE(nullptr, context->module()) << "Assembling failed for shader:\n"
- << text << std::endl;
- {
- const DominatorAnalysis* dominator_analysis = context->GetDominatorAnalysis(
- spvtest::GetFunction(context->module(), 4));
- EXPECT_TRUE(
- dominator_analysis->Dominates(context->get_def_use_mgr()->GetDef(12),
- context->get_def_use_mgr()->GetDef(15)));
- EXPECT_FALSE(
- dominator_analysis->Dominates(context->get_def_use_mgr()->GetDef(15),
- context->get_def_use_mgr()->GetDef(12)));
- EXPECT_TRUE(
- dominator_analysis->Dominates(context->get_def_use_mgr()->GetDef(5),
- context->get_def_use_mgr()->GetDef(12)));
- EXPECT_FALSE(
- dominator_analysis->Dominates(context->get_def_use_mgr()->GetDef(12),
- context->get_def_use_mgr()->GetDef(5)));
- EXPECT_TRUE(
- dominator_analysis->Dominates(context->get_def_use_mgr()->GetDef(15),
- context->get_def_use_mgr()->GetDef(16)));
- EXPECT_TRUE(
- dominator_analysis->Dominates(context->get_def_use_mgr()->GetDef(15),
- context->get_def_use_mgr()->GetDef(21)));
- EXPECT_TRUE(
- dominator_analysis->Dominates(context->get_def_use_mgr()->GetDef(15),
- context->get_def_use_mgr()->GetDef(18)));
- EXPECT_TRUE(
- dominator_analysis->Dominates(context->get_def_use_mgr()->GetDef(15),
- context->get_def_use_mgr()->GetDef(22)));
- EXPECT_FALSE(
- dominator_analysis->Dominates(context->get_def_use_mgr()->GetDef(20),
- context->get_def_use_mgr()->GetDef(22)));
- EXPECT_FALSE(
- dominator_analysis->Dominates(context->get_def_use_mgr()->GetDef(21),
- context->get_def_use_mgr()->GetDef(22)));
- EXPECT_TRUE(
- dominator_analysis->Dominates(context->get_def_use_mgr()->GetDef(15),
- context->get_def_use_mgr()->GetDef(15)));
- }
- {
- const PostDominatorAnalysis* post_dominator_analysis =
- context->GetPostDominatorAnalysis(
- spvtest::GetFunction(context->module(), 4));
- EXPECT_TRUE(post_dominator_analysis->Dominates(
- context->get_def_use_mgr()->GetDef(15),
- context->get_def_use_mgr()->GetDef(12)));
- EXPECT_FALSE(post_dominator_analysis->Dominates(
- context->get_def_use_mgr()->GetDef(12),
- context->get_def_use_mgr()->GetDef(15)));
- EXPECT_TRUE(post_dominator_analysis->Dominates(
- context->get_def_use_mgr()->GetDef(12),
- context->get_def_use_mgr()->GetDef(5)));
- EXPECT_FALSE(post_dominator_analysis->Dominates(
- context->get_def_use_mgr()->GetDef(5),
- context->get_def_use_mgr()->GetDef(12)));
- EXPECT_FALSE(post_dominator_analysis->Dominates(
- context->get_def_use_mgr()->GetDef(16),
- context->get_def_use_mgr()->GetDef(15)));
- EXPECT_FALSE(post_dominator_analysis->Dominates(
- context->get_def_use_mgr()->GetDef(21),
- context->get_def_use_mgr()->GetDef(15)));
- EXPECT_TRUE(post_dominator_analysis->Dominates(
- context->get_def_use_mgr()->GetDef(18),
- context->get_def_use_mgr()->GetDef(15)));
- EXPECT_TRUE(post_dominator_analysis->Dominates(
- context->get_def_use_mgr()->GetDef(22),
- context->get_def_use_mgr()->GetDef(15)));
- EXPECT_TRUE(post_dominator_analysis->Dominates(
- context->get_def_use_mgr()->GetDef(22),
- context->get_def_use_mgr()->GetDef(20)));
- EXPECT_TRUE(post_dominator_analysis->Dominates(
- context->get_def_use_mgr()->GetDef(22),
- context->get_def_use_mgr()->GetDef(21)));
- EXPECT_TRUE(post_dominator_analysis->Dominates(
- context->get_def_use_mgr()->GetDef(15),
- context->get_def_use_mgr()->GetDef(15)));
- }
- }
- } // namespace
- } // namespace opt
- } // namespace spvtools
|