• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

vla5924-practice / compiler-project / 14006022465

22 Mar 2025 06:25AM UTC coverage: 82.886% (+0.8%) from 82.067%
14006022465

push

github

web-flow
Implement fold control flow optimization (#212)

Such conditions as `if False`, `if True`, `while False` could be
optimized by hoisting the corresponding block's body and removing blocks
that will be never executed. Implement this.

35 of 37 new or added lines in 1 file covered. (94.59%)

4722 of 5697 relevant lines covered (82.89%)

270.6 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

94.59
/compiler/lib/backend/optree/optimizer/transforms/fold_control_flow_ops.cpp
1
#include "optimizer/transform.hpp"
2

3
#include <memory>
4
#include <string_view>
5

6
#include "compiler/optree/adaptors.hpp"
7
#include "compiler/optree/helpers.hpp"
8
#include "compiler/optree/operation.hpp"
9
#include "compiler/optree/types.hpp"
10
#include "compiler/utils/helpers.hpp"
11

12
#include "optimizer/opt_builder.hpp"
13

14
using namespace optree;
15
using namespace optree::optimizer;
16

17
namespace {
18

19
struct FoldControlFlowOps : public Transform<IfOp, WhileOp> {
20
    using Transform::Transform;
21

22
    std::string_view name() const override {
4✔
23
        return "FoldControlFlowOps";
4✔
24
    }
25

26
    static void hoistBody(const Operation::Ptr &op, OptBuilder &builder) {
2✔
27
        if (!op)
2✔
NEW
28
            return;
×
29
        builder.setInsertPointBefore(op->parent);
2✔
30
        for (const auto &childOp : utils::advanceEarly(op->body)) {
6✔
31
            auto cloned = builder.clone(childOp);
4✔
32
            builder.replace(childOp, cloned);
4✔
33
            builder.setInsertPointAfter(cloned);
4✔
34
        }
4✔
35
    }
36

37
    static void processIfOp(const IfOp &op, OptBuilder &builder) {
2✔
38
        auto conditionOp = getValueOwnerAs<ConstantOp>(op.cond());
2✔
39
        if (!conditionOp)
2✔
NEW
40
            return;
×
41
        bool condition = conditionOp.value().as<NativeBool>();
2✔
42
        if (condition) {
2✔
43
            hoistBody(op.thenOp(), builder);
1✔
44
        } else {
45
            hoistBody(op.elseOp(), builder);
1✔
46
        }
47
        builder.erase(op);
2✔
48
    }
2✔
49

50
    static void processWhileOp(const WhileOp &op, OptBuilder &builder) {
2✔
51
        auto conditionOp = getValueOwnerAs<ConstantOp>(op.conditionOp().terminator());
4✔
52
        if (!conditionOp)
2✔
53
            return;
1✔
54
        bool condition = conditionOp.value().as<NativeBool>();
1✔
55
        if (!condition) {
1✔
56
            builder.erase(op);
1✔
57
        }
58
    }
2✔
59

60
    void run(const Operation::Ptr &op, OptBuilder &builder) const override {
4✔
61
        if (auto ifOp = op->as<IfOp>())
4✔
62
            processIfOp(ifOp, builder);
2✔
63
        else if (auto whileOp = op->as<WhileOp>())
2✔
64
            processWhileOp(whileOp, builder);
6✔
65
    }
4✔
66
};
67

68
} // namespace
69

70
namespace optree {
71
namespace optimizer {
72

73
BaseTransform::Ptr createFoldControlFlowOps() {
4✔
74
    return std::make_shared<FoldControlFlowOps>();
4✔
75
}
76

77
} // namespace optimizer
78
} // namespace optree
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc