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

vla5924-practice / compiler-project / 13749764672

09 Mar 2025 03:11PM UTC coverage: 82.491% (-0.03%) from 82.521%
13749764672

push

github

web-flow
Invoke callback functions safely in `OptBuilder::Notifier` (#204)

16 of 21 new or added lines in 2 files covered. (76.19%)

3312 of 4015 relevant lines covered (82.49%)

279.42 hits per line

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

72.88
/compiler/lib/backend/optree/optimizer/opt_builder.cpp
1
#include "optimizer/opt_builder.hpp"
2

3
#include <functional>
4

5
#include "compiler/optree/builder.hpp"
6
#include "compiler/optree/operation.hpp"
7
#include "compiler/utils/debug.hpp"
8
#include "compiler/utils/helpers.hpp"
9

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

13
using dbg = utils::DebugPrinter;
14

15
namespace {
16

17
void notifyInsertRecursively(const Operation::Ptr &op, const OptBuilder::Notifier &notifier) {
×
18
    for (const auto &nestedOp : op->body) {
×
19
        notifyInsertRecursively(nestedOp, notifier);
×
20
        notifier.onInsert(nestedOp);
×
21
    }
22
}
×
23

24
} // namespace
25

NEW
26
OptBuilder::Notifier::Notifier(const Callback &onInsert, const Callback &onUpdate, const Callback &onErase)
×
NEW
27
    : onInsert(onInsert), onUpdate(onUpdate), onErase(onErase) {
×
NEW
28
}
×
29

30
void OptBuilder::Notifier::notifyInsert(const Operation::Ptr &op) const {
32✔
31
    if (onInsert)
32✔
32
        onInsert(op);
32✔
33
}
32✔
34

35
void OptBuilder::Notifier::notifyUpdate(const Operation::Ptr &op) const {
2✔
36
    if (onUpdate)
2✔
37
        onUpdate(op);
2✔
38
}
2✔
39

40
void OptBuilder::Notifier::notifyErase(const Operation::Ptr &op) const {
63✔
41
    if (onErase)
63✔
42
        onErase(op);
63✔
43
}
63✔
44

45
void OptBuilder::insert(const Operation::Ptr &op) {
32✔
46
    COMPILER_DEBUG(dbg::get() << "  Insert " << op->name << '\n');
32✔
47
    Builder::insert(op);
32✔
48
    notifier.notifyInsert(op);
32✔
49
}
32✔
50

51
Operation::Ptr OptBuilder::clone(const Operation::Ptr &op) {
×
NEW
52
    COMPILER_DEBUG(dbg::get() << "  Clone " << op->name << "{\n");
×
53
    auto newOp = op->clone();
×
54
    notifyInsertRecursively(newOp, notifier);
×
55
    insert(newOp);
×
NEW
56
    COMPILER_DEBUG(dbg::get() << "  }\n");
×
57
    return newOp;
×
58
}
×
59

60
void OptBuilder::erase(const Operation::Ptr &op) {
63✔
61
    if (op->parent)
63✔
62
        setInsertPointAfter(op);
63✔
63
    while (!op->body.empty()) {
81✔
64
        erase(op->body.back());
18✔
65
    }
66
    COMPILER_DEBUG(dbg::get() << "  Erase " << op->name << '\n');
63✔
67
    notifier.notifyErase(op);
63✔
68
    op->erase();
63✔
69
}
63✔
70

71
void OptBuilder::update(const Operation::Ptr &op, const std::function<void()> &actor) {
2✔
72
    COMPILER_DEBUG(dbg::get() << "  Update " << op->name << '\n');
2✔
73
    if (actor)
2✔
74
        actor();
2✔
75
    notifier.notifyUpdate(op);
2✔
76
}
2✔
77

78
void OptBuilder::replace(const Operation::Ptr &op, const Operation::Ptr &newOp) {
32✔
79
    COMPILER_DEBUG(dbg::get() << "  Replace " << op->name << '\n');
32✔
80
    for (const auto &[oldResult, newResult] : utils::zip(op->results, newOp->results)) {
64✔
81
        for (const auto &use : oldResult->uses) {
34✔
82
            auto user = use.lock();
2✔
83
            update(user, [&] { user->operand(use.operandNumber) = newResult; });
4✔
84
        }
2✔
85
        newResult->uses.splice_after(newResult->uses.before_begin(), oldResult->uses);
32✔
86
    }
32✔
87
    erase(op);
32✔
88
}
32✔
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