<div dir="ltr"><div>In general, it seems as if this can miss several things. For instance, it checks that all the predicessors are valid but never that we have all the predecessors. Same for successors. If we really want to be able to validate a CFG, maybe a stack-based approach like calculate_cfg would work better? Also, did you run this on piglit/shader-db to ensure that everything coming out of calculate_cfg actually passes?<br><br></div>More comments inline<br><div><div><div class="gmail_extra"><br><div class="gmail_quote">On Wed, Nov 5, 2014 at 4:13 PM, Matt Turner <span dir="ltr"><<a href="mailto:mattst88@gmail.com" target="_blank">mattst88@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">---<br>
src/mesa/drivers/dri/i965/test_verify_cfg.cpp | 273 ++++++++++++++++++++++++++<br>
src/mesa/drivers/dri/i965/test_verify_cfg.h | 26 +++<br>
2 files changed, 299 insertions(+)<br>
create mode 100644 src/mesa/drivers/dri/i965/test_verify_cfg.cpp<br>
create mode 100644 src/mesa/drivers/dri/i965/test_verify_cfg.h<br>
<br>
diff --git a/src/mesa/drivers/dri/i965/test_verify_cfg.cpp b/src/mesa/drivers/dri/i965/test_verify_cfg.cpp<br>
new file mode 100644<br>
index 0000000..0aa74c5<br>
--- /dev/null<br>
+++ b/src/mesa/drivers/dri/i965/test_verify_cfg.cpp<br>
@@ -0,0 +1,273 @@<br>
+/*<br>
+ * Copyright © 2014 Intel Corporation<br>
+ *<br>
+ * Permission is hereby granted, free of charge, to any person obtaining a<br>
+ * copy of this software and associated documentation files (the "Software"),<br>
+ * to deal in the Software without restriction, including without limitation<br>
+ * the rights to use, copy, modify, merge, publish, distribute, sublicense,<br>
+ * and/or sell copies of the Software, and to permit persons to whom the<br>
+ * Software is furnished to do so, subject to the following conditions:<br>
+ *<br>
+ * The above copyright notice and this permission notice (including the next<br>
+ * paragraph) shall be included in all copies or substantial portions of the<br>
+ * Software.<br>
+ *<br>
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR<br>
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,<br>
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL<br>
+ * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER<br>
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING<br>
+ * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS<br>
+ * IN THE SOFTWARE.<br>
+ */<br>
+<br>
+#include <gtest/gtest.h><br>
+#include "test_verify_cfg.h"<br>
+#include "brw_cfg.h"<br>
+<br>
+static bool<br>
+is_unconditional_jump(const backend_instruction *inst)<br>
+{<br>
+ return (inst->opcode == BRW_OPCODE_BREAK ||<br>
+ inst->opcode == BRW_OPCODE_CONTINUE ||<br>
+ inst->opcode == BRW_OPCODE_WHILE) &&<br>
+ inst->predicate == BRW_PREDICATE_NONE;<br>
+}<br>
+<br>
+void<br>
+verify_cfg(backend_visitor *v)<br>
+{<br>
+ foreach_block(block, v->cfg) {<br>
+ switch (block->start()->opcode) {<br>
+ case BRW_OPCODE_ENDIF: {<br>
+ /* Has two predecessors:<br>
+ * - the previous block is always a predecessor<br>
+ * - always a predecessor ending in an IF or an ELSE<br>
+ *<br>
+ * Note that if the body of the if block is empty, then the<br>
+ * previous block *is* the block that ends with IF, so the ENDIF<br>
+ * block will have the same predecessor twice.<br>
+ */<br>
+ if (is_unconditional_jump(block->prev()->end())) {<br>
+ EXPECT_EQ(block->parents.length(), 1u);<br>
+ } else {<br>
+ EXPECT_EQ(block->parents.length(), 2u);<br>
+ }<br></blockquote><div><br></div><div>This obviously catches<br><br></div><div>if (foo) {<br></div><div> /* Do stuff */<br></div><div>} else {<br></div><div> break;<br>}<br><br></div><div>but what about<br><br></div><div>if (foo) {<br></div><div> break;<br></div><div>} else {<br> /* Do stuff */<br>}<br><br></div><div>Or worse, what about<br><br></div><div>if (foo) {<br></div><div> break;<br></div><div>} else {<br></div><div> continue;<br>}<br><br></div><div>Maybe there's something I'm missing here.<br></div><div> </div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">
+<br>
+ bool found_prev = false, found_if = false, found_else = false,<br>
+ found_other = false;<br>
+ foreach_list_typed(bblock_link, parent, link, &block->parents) {<br>
+ if (parent->block == block->prev() && !found_prev)<br>
+ found_prev = true;<br>
+ else if (parent->block->end()->opcode == BRW_OPCODE_IF)<br>
+ found_if = true;<br>
+ else if (parent->block->end()->opcode == BRW_OPCODE_ELSE)<br>
+ found_else = true;<br>
+ else<br>
+ found_other = true;<br>
+ }<br></blockquote><div><br></div><div>how do we detect if a parent is simply missing? Or are we trusting in the check above?<br></div><div> </div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">
+ EXPECT_NE(found_prev, is_unconditional_jump(block->prev()->end()));<br>
+ EXPECT_NE(found_if, found_else);<br>
+ EXPECT_FALSE(found_other);<br>
+ break;<br>
+ }<br>
+ case BRW_OPCODE_DO: {<br>
+ /* Has two or more predecessors<br>
+ * - the previous block is always a predecessor<br>
+ * - always a predecessor ending in a WHILE<br>
+ * - some number of predecessors ending in continue<br>
+ */<br>
+ if (is_unconditional_jump(block->prev()->end())) {<br>
+ EXPECT_GE(block->parents.length(), 1u);<br>
+ } else {<br>
+ EXPECT_GE(block->parents.length(), 2u);<br>
+ }<br>
+<br>
+ bool found_prev = false, found_while = false, found_other = false;<br>
+ foreach_list_typed(bblock_link, parent, link, &block->parents) {<br>
+ if (parent->block == block->prev())<br>
+ found_prev = true;<br>
+ else if (parent->block->end()->opcode == BRW_OPCODE_WHILE)<br>
+ found_while = true;<br>
+ else if (parent->block->end()->opcode != BRW_OPCODE_CONTINUE)<br>
+ found_other = true;<br>
+ }<br>
+<br>
+ EXPECT_NE(found_prev, is_unconditional_jump(block->prev()->end()));<br>
+ EXPECT_TRUE(found_while);<br>
+ EXPECT_FALSE(found_other);<br>
+ break;<br>
+ }<br>
+ default: {<br>
+ if (block->num == 0) {<br>
+ EXPECT_EQ(0u, block->parents.length());<br>
+ break;<br>
+ }<br>
+ /* Has one or more predecessors<br>
+ * - the previous block is always a predecessor<br>
+ * - some number of predecessors ending with break<br>
+ */<br>
+ if (!is_unconditional_jump(block->prev()->end())) {<br>
+ EXPECT_GE(block->parents.length(), 1u);<br>
+ }<br>
+<br>
+ bool found_prev = false, found_other = false;<br>
+ foreach_list_typed(bblock_link, parent, link, &block->parents) {<br>
+ if (parent->block == block->prev())<br>
+ found_prev = true;<br>
+ else if (parent->block->end()->opcode != BRW_OPCODE_BREAK)<br>
+ found_other = true;<br>
+ }<br>
+<br>
+ EXPECT_NE(found_prev, is_unconditional_jump(block->prev()->end()));<br>
+ EXPECT_FALSE(found_other);<br>
+ break;<br>
+ }<br>
+ }<br>
+<br>
+ switch (block->end()->opcode) {<br>
+ case BRW_OPCODE_IF: {<br>
+ /* Has two successors:<br>
+ * - the next block is always a successor<br>
+ * - always a successor starting with ENDIF<br>
+ *<br>
+ * Note that if the body of the if block is empty, then the next block<br>
+ * *is* the block that starts with ENDIF, so the IF block will have<br>
+ * the same successor twice.<br>
+ */<br>
+ EXPECT_EQ(block->children.length(), 2u);<br>
+<br>
+ bool found_next = false, found_endif = false, found_other = false;<br>
+ foreach_list_typed(bblock_link, child, link, &block->children) {<br>
+ if (child->block == block->next() && !found_next)<br>
+ found_next = true;<br>
+ else if (child->block->start()->opcode == BRW_OPCODE_ENDIF)<br>
+ found_endif = true;<br>
+ else<br>
+ found_other = true;<br>
+ }<br>
+<br>
+ EXPECT_TRUE(found_next);<br>
+ EXPECT_TRUE(found_endif);<br>
+ EXPECT_FALSE(found_other);<br>
+ break;<br>
+ }<br>
+ case BRW_OPCODE_ELSE: {<br>
+ /* Always a successor starting with ENDIF */<br>
+ EXPECT_EQ(1u, block->children.length());<br></blockquote><div><br></div><div>What about<br><br>...<br></div><div>} else {<br></div><div> break;<br>}<br></div><div> </div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">
+ bool found_endif = false, found_other = false;<br>
+ foreach_list_typed(bblock_link, child, link, &block->children) {<br>
+ if (child->block->start()->opcode == BRW_OPCODE_ENDIF)<br>
+ found_endif = true;<br>
+ else<br>
+ found_other = true;<br>
+ }<br>
+<br>
+ EXPECT_TRUE(found_endif);<br>
+ EXPECT_FALSE(found_other);<br>
+ break;<br>
+ }<br>
+ case BRW_OPCODE_WHILE: {<br>
+ /* Has one or two successors:<br>
+ * - always a successor starting with DO<br>
+ * - if predicated, the next block is a successor<br>
+ */<br>
+ backend_instruction *while_inst = block->end();<br>
+ if (while_inst->predicate == BRW_PREDICATE_NONE) {<br>
+ EXPECT_EQ(1u, block->children.length());<br>
+ } else {<br>
+ EXPECT_EQ(2u, block->children.length());<br>
+ }<br>
+<br>
+ bool found_next = false, found_do = false, found_other = false;<br>
+ foreach_list_typed(bblock_link, child, link, &block->children) {<br>
+ if (child->block == block->next())<br>
+ found_next = true;<br>
+ if (child->block->start()->opcode == BRW_OPCODE_DO)<br>
+ found_do = true;<br>
+ else<br>
+ found_other = true;<br>
+ }<br>
+<br>
+ EXPECT_EQ(while_inst->predicate != BRW_PREDICATE_NONE, found_next);<br>
+ EXPECT_TRUE(found_do);<br>
+ EXPECT_FALSE(found_other);<br>
+ break;<br>
+ }<br>
+ case BRW_OPCODE_BREAK: {<br>
+ /* Has one or two successors:<br>
+ * - always a successor following a WHILE<br>
+ * - if predicated, the next block is a successor<br>
+ */<br>
+ backend_instruction *break_inst = block->end();<br>
+ if (break_inst->predicate == BRW_PREDICATE_NONE) {<br>
+ EXPECT_EQ(1u, block->children.length());<br>
+ } else {<br>
+ EXPECT_EQ(2u, block->children.length());<br>
+ }<br>
+<br>
+ bool found_next = false, found_while = false, found_other = false;<br>
+ foreach_list_typed(bblock_link, child, link, &block->children) {<br>
+ if (child->block == block->next())<br>
+ found_next = true;<br>
+ else if (child->block->prev()->end()->opcode == BRW_OPCODE_WHILE)<br>
+ found_while = true;<br>
+ else<br>
+ found_other = true;<br>
+ }<br>
+<br>
+ EXPECT_EQ(break_inst->predicate != BRW_PREDICATE_NONE, found_next);<br>
+ EXPECT_TRUE(found_while);<br>
+ EXPECT_FALSE(found_other);<br>
+ break;<br>
+ }<br>
+ case BRW_OPCODE_CONTINUE: {<br>
+ /* Has one or two successors:<br>
+ * - always a successor starting with DO<br>
+ * - if predicated, the next block is a successor<br>
+ */<br>
+ backend_instruction *cont_inst = block->end();<br>
+ if (cont_inst->predicate == BRW_PREDICATE_NONE) {<br>
+ EXPECT_EQ(1u, block->children.length());<br>
+ } else {<br>
+ EXPECT_EQ(2u, block->children.length());<br>
+ }<br>
+<br>
+ bool found_next = false, found_do = false, found_other = false;<br>
+ foreach_list_typed(bblock_link, child, link, &block->children) {<br>
+ if (child->block == block->next())<br>
+ found_next = true;<br>
+ if (child->block->start()->opcode == BRW_OPCODE_DO)<br>
+ found_do = true;<br>
+ else<br>
+ found_other = true;<br>
+ }<br>
+<br>
+ EXPECT_EQ(cont_inst->predicate != BRW_PREDICATE_NONE, found_next);<br>
+ EXPECT_TRUE(found_do);<br>
+ EXPECT_FALSE(found_other);<br>
+ break;<br>
+ }<br>
+ default:<br>
+ if (block->num == v->cfg->num_blocks - 1) {<br>
+ EXPECT_EQ(0u, block->children.length());<br>
+ break;<br>
+ }<br>
+ /* The next block is always a successor */<br>
+ EXPECT_EQ(1u, block->children.length());<br>
+ bool found_next = false, found_other = false;<br>
+ foreach_list_typed(bblock_link, child, link, &block->children) {<br>
+ if (child->block == block->next())<br>
+ found_next = true;<br>
+ else<br>
+ found_other = true;<br>
+ }<br>
+<br>
+ EXPECT_TRUE(found_next);<br>
+ EXPECT_FALSE(found_other);<br>
+ break;<br>
+ }<br>
+ }<br>
+}<br>
diff --git a/src/mesa/drivers/dri/i965/test_verify_cfg.h b/src/mesa/drivers/dri/i965/test_verify_cfg.h<br>
new file mode 100644<br>
index 0000000..16c9a24<br>
--- /dev/null<br>
+++ b/src/mesa/drivers/dri/i965/test_verify_cfg.h<br>
@@ -0,0 +1,26 @@<br>
+/*<br>
+ * Copyright © 2014 Intel Corporation<br>
+ *<br>
+ * Permission is hereby granted, free of charge, to any person obtaining a<br>
+ * copy of this software and associated documentation files (the "Software"),<br>
+ * to deal in the Software without restriction, including without limitation<br>
+ * the rights to use, copy, modify, merge, publish, distribute, sublicense,<br>
+ * and/or sell copies of the Software, and to permit persons to whom the<br>
+ * Software is furnished to do so, subject to the following conditions:<br>
+ *<br>
+ * The above copyright notice and this permission notice (including the next<br>
+ * paragraph) shall be included in all copies or substantial portions of the<br>
+ * Software.<br>
+ *<br>
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR<br>
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,<br>
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL<br>
+ * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER<br>
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING<br>
+ * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS<br>
+ * IN THE SOFTWARE.<br>
+ */<br>
+<br>
+#include "brw_shader.h"<br>
+<br>
+void verify_cfg(backend_visitor *v);<br>
<span><font color="#888888">--<br>
2.0.4<br>
<br>
_______________________________________________<br>
mesa-dev mailing list<br>
<a href="mailto:mesa-dev@lists.freedesktop.org" target="_blank">mesa-dev@lists.freedesktop.org</a><br>
<a href="http://lists.freedesktop.org/mailman/listinfo/mesa-dev" target="_blank">http://lists.freedesktop.org/mailman/listinfo/mesa-dev</a><br>
</font></span></blockquote></div><br></div></div></div></div>