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>
//
// 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 is an AST traverser that ensures that loops are either finite or
// invoke side-effects.
//
#ifndef COMPILER_TRANSLATOR_TREEOPS_MSL_ENSURELOOPFORWARDPROGRESS_H_
#define COMPILER_TRANSLATOR_TREEOPS_MSL_ENSURELOOPFORWARDPROGRESS_H_
#include "common/angleutils.h"
#include "common/debug.h"
namespace sh
{
class TCompiler;
class TIntermNode;
#ifdef ANGLE_ENABLE_METAL
[[nodiscard]] bool EnsureLoopForwardProgress(TCompiler *compiler, TIntermNode *root);
#else
[[nodiscard]] ANGLE_INLINE bool EnsureLoopForwardProgress(TCompiler *compiler, TIntermNode *root)
{
UNREACHABLE();
return false;
}
#endif
} // namespace sh
#endif // COMPILER_TRANSLATOR_TREEOPS_MSL_ENSURELOOPFORWARDPROGRESS_H_