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

vla5924-practice / compiler-project / 13874105804

15 Mar 2025 03:15PM UTC coverage: 77.3% (+0.7%) from 76.638%
13874105804

Pull #212

github

web-flow
Merge eeeac26cd into 57989f91f
Pull Request #212: Implement fold control flow optimization

26 of 38 new or added lines in 1 file covered. (68.42%)

4311 of 5577 relevant lines covered (77.3%)

221.92 hits per line

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

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

3
#include <memory>
4

5
#include "compiler/optree/adaptors.hpp"
6
#include "compiler/optree/helpers.hpp"
7
#include "compiler/optree/operation.hpp"
8

9
#include "optimizer/opt_builder.hpp"
10

11
using namespace optree;
12
using namespace optree::optimizer;
13

14
namespace {
15

16
struct FoldControlFlowOps : public Transform<IfOp, WhileOp> {
17
    using Transform::Transform;
18

19
    std::string_view name() const override {
1✔
20
        return "FoldControlFlowOps";
1✔
21
    }
22

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

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

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

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

66
} // namespace
67

68
namespace optree {
69
namespace optimizer {
70

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

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