[flang-commits] [flang] [flang][Transforms] Add LiftSCFWhileToSCFFor pass (PR #213273)
via flang-commits
flang-commits at lists.llvm.org
Fri Jul 31 06:52:21 PDT 2026
github-actions[bot] wrote:
<!--LLVM CODE FORMAT COMMENT: {clang-format}-->
:warning: C/C++ code formatter, clang-format found issues in your code. :warning:
<details>
<summary>
You can test this locally with the following command:
</summary>
``````````bash
git-clang-format --diff origin/main HEAD --extensions cpp -- flang/lib/Optimizer/Transforms/LiftSCFWhileToSCFFor.cpp --diff_from_common_commit
``````````
:warning:
The reproduction instructions above might return results for more than one PR
in a stack if you are using a stacked PR workflow. You can limit the results by
changing `origin/main` to the base branch/commit you want to compare against.
:warning:
</details>
<details>
<summary>
View the diff from clang-format here.
</summary>
``````````diff
diff --git a/flang/lib/Optimizer/Transforms/LiftSCFWhileToSCFFor.cpp b/flang/lib/Optimizer/Transforms/LiftSCFWhileToSCFFor.cpp
index 0978e8ae8..920909d92 100644
--- a/flang/lib/Optimizer/Transforms/LiftSCFWhileToSCFFor.cpp
+++ b/flang/lib/Optimizer/Transforms/LiftSCFWhileToSCFFor.cpp
@@ -117,8 +117,8 @@ struct AffineRecurrence {
// loop-invariant w.r.t. `whileOp`), return the recurrence op, the step value,
// and whether the recurrence is a subtraction. The returned step is the raw
// RHS; for arith.subi the caller should interpret it as a negative step.
-static AffineRecurrence matchAffineRecurrence(
- Value v, Value beforeArg, scf::WhileOp whileOp) {
+static AffineRecurrence matchAffineRecurrence(Value v, Value beforeArg,
+ scf::WhileOp whileOp) {
if (auto add = v.getDefiningOp<arith::AddIOp>()) {
if (add.getLhs() == beforeArg && isLoopInvariant(add.getRhs(), whileOp)) {
return {add.getOperation(), add.getRhs(), /*isSub=*/false};
@@ -218,8 +218,7 @@ struct LoopInfo {
LoopInfo(scf::WhileOp whileOp, Block *continuesBlock, Value tripInit,
Operation *tripRecurrence, unsigned ivArgIdx, Value ivInit,
- Value ivStep, Operation *ivRecurrence,
- SmallVector<IterArg> iterArgs)
+ Value ivStep, Operation *ivRecurrence, SmallVector<IterArg> iterArgs)
: whileOp(whileOp), continuesBlock(continuesBlock), tripInit(tripInit),
tripRecurrence(tripRecurrence), ivArgIdx(ivArgIdx), ivInit(ivInit),
ivStep(ivStep), ivRecurrence(ivRecurrence),
@@ -228,8 +227,8 @@ struct LoopInfo {
// Try to build a LoopInfo for `whileOp`. On failure, emit a one-line stderr
// diagnostic explaining why and return std::nullopt.
-static std::optional<LoopInfo> tryBuildLoopInfo(
- scf::WhileOp whileOp, llvm::raw_ostream &os) {
+static std::optional<LoopInfo> tryBuildLoopInfo(scf::WhileOp whileOp,
+ llvm::raw_ostream &os) {
auto bail = [&](StringRef reason) -> std::optional<LoopInfo> {
os << " [skip] " << reason << "\n";
return std::nullopt;
@@ -289,7 +288,7 @@ static std::optional<LoopInfo> tryBuildLoopInfo(
auto contYield = cast<scf::YieldOp>(cont->getTerminator());
for (auto [argIdx, beforeArg] :
- llvm::enumerate(whileOp.getBeforeArguments())) {
+ llvm::enumerate(whileOp.getBeforeArguments())) {
Value nextInBefore = getNextIterValueInBefore(whileOp, (unsigned)argIdx);
// In the canonical lift-cf-to-scf shape every iteration value flows
@@ -420,10 +419,11 @@ static void rewriteToSCFFor(const LoopInfo &info, llvm::raw_ostream &os) {
// ub = arith.addi ivInit, scaled
IntegerAttr ivStepAttr;
bool ivStepIsOne = matchPattern(info.ivStep, m_Constant(&ivStepAttr)) &&
- ivStepAttr.getInt() == 1;
- Value scaled = ivStepIsOne
- ? info.tripInit
- : arith::MulIOp::create(builder, loc, info.tripInit, info.ivStep);
+ ivStepAttr.getInt() == 1;
+ Value scaled =
+ ivStepIsOne
+ ? info.tripInit
+ : arith::MulIOp::create(builder, loc, info.tripInit, info.ivStep);
Value ub = arith::AddIOp::create(builder, loc, info.ivInit, scaled);
Value lbIdx = castTo(info.ivInit, indexType);
``````````
</details>
https://github.com/llvm/llvm-project/pull/213273
More information about the flang-commits
mailing list