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 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
//
// 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.
//
#include "test_utils/ANGLETest.h"
#include "test_utils/gl_raii.h"
#include "util/shader_utils.h"
namespace
{
// To inspect current behavior, run the tests in following manner:
// clang-format off
// ANGLE_FEATURE_OVERRIDES_DISABLED=injectAsmStatementIntoLoopBodies GMD_STDOUT=1 ./out/Debug/angle_end2end_tests --gtest_also_run_disabled_tests --gtest_filter=TimeoutDrawTest.DISABLED_DynamicInfiniteLoop2VS/ES3_Metal
// GMD_STDOUT=1 ./out/Debug/angle_end2end_tests --gtest_also_run_disabled_tests --gtest_filter=TimeoutDrawTest.DISABLED_DynamicInfiniteLoop2VS/ES3_Metal_EnsureLoopForwardProgress
// clang-format on
using namespace angle;
class TimeoutDrawTest : public ANGLETest<>
{
protected:
TimeoutDrawTest()
{
setWindowWidth(128);
setWindowHeight(128);
setConfigRedBits(8);
setConfigGreenBits(8);
setConfigBlueBits(8);
setConfigAlphaBits(8);
// Tests should skip if robustness not supported, but this can be done only after
// Metal supports robustness.
if (IsEGLClientExtensionEnabled("EGL_EXT_create_context_robustness"))
{
setContextResetStrategy(EGL_LOSE_CONTEXT_ON_RESET_EXT);
}
else
{
setContextResetStrategy(EGL_NO_RESET_NOTIFICATION_EXT);
}
}
void testSetUp() override
{
glClear(GL_COLOR_BUFFER_BIT);
glFinish();
}
};
// Tests that trivial infinite loops in vertex shaders hang instead of progress.
TEST_P(TimeoutDrawTest, DISABLED_TrivialInfiniteLoopVS)
{
constexpr char kVS[] = R"(precision highp float;
attribute vec4 a_position;
void main()
{
for (;;) {}
gl_Position = a_position;
})";
ANGLE_GL_PROGRAM(program, kVS, essl1_shaders::fs::Red());
drawQuad(program, essl1_shaders::PositionAttrib(), 0.5f);
glFinish();
if (glGetError() != GL_CONTEXT_LOST)
{
FAIL();
EXPECT_PIXEL_COLOR_EQ(0, 0, GLColor::transparentBlack);
}
}
// Tests that trivial infinite loops in fragment shaders hang instead of progress.
TEST_P(TimeoutDrawTest, DISABLED_TrivialInfiniteLoopFS)
{
constexpr char kFS[] = R"(precision mediump float;
void main()
{
for (;;) {}
gl_FragColor = vec4(1, 0, 0, 1);
})";
ANGLE_GL_PROGRAM(program, essl1_shaders::vs::Simple(), kFS);
drawQuad(program, essl1_shaders::PositionAttrib(), 0.5f);
glFinish();
if (glGetError() != GL_CONTEXT_LOST)
{
FAIL();
EXPECT_PIXEL_COLOR_EQ(0, 0, GLColor::transparentBlack);
}
}
// Tests that infinite loops based on user-supplied values in vertex shaders hang instead of
// progress. Otherwise optimizer would be able to assume something about the domain of the
// user-supplied value.
TEST_P(TimeoutDrawTest, DISABLED_DynamicInfiniteLoopVS)
{
constexpr char kVS[] = R"(precision highp float;
attribute vec4 a_position;
uniform int f;
void main()
{
for (;f != 0;) {}
gl_Position = a_position;
})";
ANGLE_GL_PROGRAM(program, kVS, essl1_shaders::fs::Red());
glUseProgram(program);
GLint uniformLocation = glGetUniformLocation(program, "f");
glUniform1i(uniformLocation, 77);
drawQuad(program, essl1_shaders::PositionAttrib(), 0.5f);
glFinish();
if (glGetError() != GL_CONTEXT_LOST)
{
FAIL();
EXPECT_PIXEL_COLOR_EQ(0, 0, GLColor::transparentBlack);
}
}
// Tests that infinite loops based on user-supplied values in fragment shaders hang instead of
// progress. Otherwise optimizer would be able to assume something about the domain of the
// user-supplied value.
TEST_P(TimeoutDrawTest, DISABLED_DynamicInfiniteLoopFS)
{
constexpr char kFS[] = R"(precision mediump float;
uniform int f;
void main()
{
for (;f != 0;) {}
gl_FragColor = vec4(1, 0, 0, 1);
})";
ANGLE_GL_PROGRAM(program, essl1_shaders::vs::Simple(), kFS);
glUseProgram(program);
GLint uniformLocation = glGetUniformLocation(program, "f");
glUniform1i(uniformLocation, 88);
EXPECT_GL_NO_ERROR();
drawQuad(program, essl1_shaders::PositionAttrib(), 0.5f);
glFinish();
if (glGetError() != GL_CONTEXT_LOST)
{
FAIL();
EXPECT_PIXEL_COLOR_EQ(0, 0, GLColor::transparentBlack);
}
}
// Tests that infinite loops based on user-supplied values in vertex shaders hang instead of
// progress. Otherwise optimizer would be able to assume something about the domain of the
// user-supplied value. Explicit value break variant.
TEST_P(TimeoutDrawTest, DISABLED_DynamicInfiniteLoop2VS)
{
constexpr char kVS[] = R"(precision highp float;
attribute vec4 a_position;
uniform int f;
void main()
{
for (;;) { if (f <= 1) break; }
gl_Position = a_position;
})";
ANGLE_GL_PROGRAM(program, kVS, essl1_shaders::fs::Red());
glUseProgram(program);
GLint uniformLocation = glGetUniformLocation(program, "f");
glUniform1i(uniformLocation, 66);
EXPECT_GL_NO_ERROR();
drawQuad(program, essl1_shaders::PositionAttrib(), 0.5f);
glFinish();
if (glGetError() != GL_CONTEXT_LOST)
{
FAIL();
EXPECT_PIXEL_COLOR_EQ(0, 0, GLColor::transparentBlack);
}
}
// Tests that infinite loops based on user-supplied values in fragment shaders hang instead of
// progress. Otherwise optimizer would be able to assume something about the domain of the
// user-supplied value. Explicit value break variant.
TEST_P(TimeoutDrawTest, DISABLED_DynamicInfiniteLoop2FS)
{
constexpr char kFS[] = R"(precision mediump float;
uniform float f;
void main()
{
for (;;) { if (f < 0.1) break; }
gl_FragColor = vec4(1, 0, f, 1);
})";
ANGLE_GL_PROGRAM(program, essl1_shaders::vs::Simple(), kFS);
glUseProgram(program);
GLint uniformLocation = glGetUniformLocation(program, "f");
glUniform1f(uniformLocation, .5f);
EXPECT_GL_NO_ERROR();
drawQuad(program, essl1_shaders::PositionAttrib(), 0.5f);
glFinish();
if (glGetError() != GL_CONTEXT_LOST)
{
FAIL();
EXPECT_PIXEL_COLOR_EQ(0, 0, GLColor::transparentBlack);
}
}
} // namespace
GTEST_ALLOW_UNINSTANTIATED_PARAMETERIZED_TEST(TimeoutDrawTest);
ANGLE_INSTANTIATE_TEST(
TimeoutDrawTest,
WithRobustness(ES2_METAL()),
WithRobustness(ES3_METAL()),
WithRobustness(ES2_METAL().enable(Feature::InjectAsmStatementIntoLoopBodies)),
WithRobustness(ES3_METAL().enable(Feature::InjectAsmStatementIntoLoopBodies)),
WithRobustness(ES2_METAL().enable(Feature::EnsureLoopForwardProgress)),
WithRobustness(ES3_METAL().enable(Feature::EnsureLoopForwardProgress)));