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

vla5924-practice / compiler-project / 13926013161

18 Mar 2025 02:40PM UTC coverage: 82.886% (+6.2%) from 76.638%
13926013161

Pull #212

github

web-flow
Merge fde87dfba into 993c9a6a4
Pull Request #212: Implement fold control flow optimization

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

234 existing lines in 10 files now uncovered.

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/utils/helpers.hpp"
10

11
#include "optimizer/opt_builder.hpp"
12

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

16
namespace {
17

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

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

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

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

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

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

67
} // namespace
68

69
namespace optree {
70
namespace optimizer {
71

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

76
} // namespace optimizer
77
} // 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