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

vla5924-practice / compiler-project / 13874376834

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

Pull #212

github

web-flow
Merge 6e1fbe25d 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
#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 {
1✔
22
        return "FoldControlFlowOps";
1✔
23
    }
24

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

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

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

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

68
} // namespace
69

70
namespace optree {
71
namespace optimizer {
72

73
BaseTransform::Ptr createFoldControlFlowOps() {
2✔
74
    return std::make_shared<FoldControlFlowOps>();
2✔
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