Hash :
94ee620d
Author :
Date :
2025-05-22T10:07:05
Metal: Allow optimization of simple loops
Reimplement the feature to avoid undefined behavior of infinite loops.
Add EnsureLoopForwardProgress rewrite pass that inserts a volatile
variable access to all loops that it cannot analyze as being finite.
Detect loops of form `for (; i <op> x; ++i)` as being finite.
The <op> can be any of <,<=,>,>=,==, != operator.
The i can be int or uint.
The ++i can be -- or ++, -=1, +=1.
This assumes that backends using the feature emit signed int arithmetic
with defined wraparound semantics.
Uses volatile write instead of asm("") due to asm not forcing the
behavior in some compiler versions. The volatile variable access is
defined in C++ as forward progress, and by inheritance this works in
MSL.
Later commits may remove injectAsmStatementIntoLoopBodies if
ensureLoopForwardProgress is appropriate for all use-cases.
Bug: angleproject:418918522
Change-Id: Ic9c29f57044b792195386483208632354d24c854
Reviewed-on: https://chromium-review.googlesource.com/c/angle/angle/+/6575051
Reviewed-by: Shahbaz Youssefi <syoussefi@chromium.org>
Auto-Submit: Kimmo Kinnunen <kkinnunen@apple.com>
Reviewed-by: Geoff Lang <geofflang@chromium.org>
Commit-Queue: Kimmo Kinnunen <kkinnunen@apple.com>
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
//
// Copyright 2025 The ANGLE Project Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
//
// EnsureLoopForwardProgress_test.cpp:
// Tests that loops make forward progress.
//
#include "GLSLANG/ShaderLang.h"
#include "angle_gl.h"
#include "gmock/gmock.h"
#include "gtest/gtest.h"
#include "tests/test_utils/compiler_test.h"
namespace
{
using namespace sh;
using namespace testing;
class EnsureLoopForwardProgressTest : public MatchOutputCodeTest
{
public:
EnsureLoopForwardProgressTest() : MatchOutputCodeTest(GL_FRAGMENT_SHADER, SH_ESSL_OUTPUT)
{
ShCompileOptions defaultCompileOptions = {};
defaultCompileOptions.ensureLoopForwardProgress = true;
defaultCompileOptions.validateAST = true;
setDefaultCompileOptions(defaultCompileOptions);
}
};
TEST_F(EnsureLoopForwardProgressTest, FiniteForInitLessThanConstantPlusPlus)
{
const char kShader[] = R"(#version 300 es
void main() {
for (highp int i = 0; i < 100; ++i) { }
})";
const char kExpected[] = R"(#version 300 es
void main(){
for (highp int _ui = 0; (_ui < 100); (++_ui))
{
}
}
)";
compile(kShader);
EXPECT_EQ(kExpected, outputCode(SH_ESSL_OUTPUT));
}
TEST_F(EnsureLoopForwardProgressTest, InfiniteForExample)
{
const char kShader[] = R"(#version 300 es
void main() {
for (highp int i = 0; i < 100; i++) { i = 0; }
})";
const char kExpected[] = R"(#version 300 es
void main(){
for (highp int _ui = 0; (_ui < 100); (_ui++))
{
loopForwardProgress();
{
(_ui = 0);
}
}
}
)";
compile(kShader);
EXPECT_EQ(kExpected, outputCode(SH_ESSL_OUTPUT));
}
TEST_F(EnsureLoopForwardProgressTest, InfiniteNestedForExample)
{
const char kShader[] = R"(#version 300 es
void main() {
for (highp int i = 0; i < 100; i++) { for (highp int j = 0; j < 100; j++) { j = 0; } i = 0; }
})";
const char kExpected[] = R"(#version 300 es
void main(){
for (highp int _ui = 0; (_ui < 100); (_ui++))
{
loopForwardProgress();
{
for (highp int _uj = 0; (_uj < 100); (_uj++))
{
loopForwardProgress();
{
(_uj = 0);
}
}
(_ui = 0);
}
}
}
)";
compile(kShader);
EXPECT_EQ(kExpected, outputCode(SH_ESSL_OUTPUT));
}
TEST_F(EnsureLoopForwardProgressTest, FiniteFors)
{
const char kShaderPrefix[] = R"(#version 300 es
precision highp int;
uniform int a;
uniform uint b;
void main() {
)";
const char kShaderSuffix[] = "}\n";
const char *tests[]{"int i = 101; for (; i < 10; i++) { }",
"int i = 101; for (; i < 10; i+=1) { }",
"int i = 101; for (; i < 10; i-=1) { }",
"for (int i = 0; i < 10; i++) { }",
"for (int i = 0; i < a; i++) { }",
"for (int i = 0; i < 100000/2; ++i) { }",
"for (uint i = 0u; i < 10u; i++) { }",
"for (uint i = 0u; i < b; i++) { }",
"for (uint i = 0u; i < 100000u/2u; ++i) { }",
"for (uint i = 0u; i < 4294967295u; ++i) { }",
"for (uint i = 10u; i > 1u+3u ; --i) { }",
"const int z = 7; for (int i = 0; i < z; i++) { }",
"for (int i = 0; i < 10; i++) { for (int j = 0; j < 1000; ++j) { }}"};
for (auto &test : tests)
{
std::string shader = (std::stringstream() << kShaderPrefix << test << kShaderSuffix).str();
compile(shader.c_str());
std::string output = outputCode(SH_ESSL_OUTPUT);
EXPECT_THAT(output, HasSubstr("void main(){"));
EXPECT_THAT(output, Not(HasSubstr("loopForwardProgress();")))
<< "input: " << test << "output: " << output;
}
}
TEST_F(EnsureLoopForwardProgressTest, InfiniteFors)
{
const char kShaderPrefix[] = R"(#version 300 es
precision highp int;
uniform int a;
uniform uint b;
void main() {
)";
const char kShaderSuffix[] = "}\n";
const char *tests[]{"for (;;) { }",
"for (bool b = true; b; b = false) { }",
"for (int i = 0; i < 10;) { }",
"int i = 101; for (; i < 10; i+=2) { }",
"int i = 101; for (; i < 10; i-=2) { }",
"int z = 7; for (int i = 0; i < z; i++) { }",
"for (int i = 0; i < 10; i++) { i++; }",
"for (int i = 0; i < 10;) { i++; }",
"for (int i = 0; i < a/2; i++) { }",
"for (int i = 0; float(i) < 10e10; ++i) { }",
"for (int i = 0; i < 10; i++) { for (int j = 0; j < 1000; ++i) { }}",
"for (int i = 0; i != 1; i+=2) { }"};
for (auto &test : tests)
{
std::string shader = (std::stringstream() << kShaderPrefix << test << kShaderSuffix).str();
compile(shader.c_str());
std::string output = outputCode(SH_ESSL_OUTPUT);
EXPECT_THAT(output, HasSubstr("void main(){"));
EXPECT_THAT(output, HasSubstr("loopForwardProgress();"))
<< "input: " << test << "output: " << output;
}
}
} // namespace