File: tuple_undef.cpp

package info (click to toggle)
halide 21.0.0-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 55,752 kB
  • sloc: cpp: 289,334; ansic: 22,751; python: 7,486; makefile: 4,299; sh: 2,508; java: 1,549; javascript: 282; pascal: 207; xml: 127; asm: 9
file content (153 lines) | stat: -rw-r--r-- 4,286 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
#include "Halide.h"
#include <stdio.h>

using namespace Halide;
using namespace Halide::Internal;

class CountStores : public IRVisitor {
public:
    int count;

    CountStores()
        : count(0) {
    }

protected:
    using IRVisitor::visit;

    void visit(const Store *op) override {
        count++;
    }
};

class CheckStoreCount : public IRMutator {
    int correct;

public:
    CheckStoreCount(int correct)
        : correct(correct) {
    }
    using IRMutator::mutate;

    Stmt mutate(const Stmt &s) override {
        CountStores c;
        s.accept(&c);

        if (c.count != correct) {
            printf("There were %d stores. There were supposed to be %d\n", c.count, correct);
            exit(1);
        }

        return s;
    }
};

int main(int argc, char **argv) {
    Buffer<int> a(1024, 1024), b(1024, 1024);
    const int A = (int)0xdeadbeef;
    const int B = (int)0xf00dcafe;

    printf("Test 1...\n");
    {
        Var x("x"), y("y");
        Func f("f");

        f(x, y) = Tuple(x + y, undef<int32_t>());
        f(x, y) = Tuple(f(x, y)[0] + undef<int32_t>(), f(x, y)[1] + 2);

        // There should be two stores: the undef stores should have been removed.
        f.add_custom_lowering_pass(new CheckStoreCount(2));

        // Pre-fill with unlikely values so we can verify that undef bits are untouched.
        a.fill(A);
        b.fill(B);
        f.realize({a, b});
        for (int y = 0; y < a.height(); y++) {
            for (int x = 0; x < a.width(); x++) {
                int correct_a = x + y;
                int correct_b = B + 2;
                if (a(x, y) != correct_a || b(x, y) != correct_b) {
                    printf("result(%d, %d) = (%d, %d) instead of (%d, %d)\n",
                           x, y, a(x, y), b(x, y), correct_a, correct_b);
                    return 1;
                }
            }
        }
    }

    printf("Test 2...\n");
    {
        Var x("x"), y("y");
        Func f("f");

        f(x, y) = Tuple(x, y);
        f(x, y) = Tuple(undef<int>(), select(x < 20, 20 * f(x, y)[0], undef<int>()));

        // There should be three stores: the undef store to the 1st element of
        // the Tuple in the update definition should have been removed.
        f.add_custom_lowering_pass(new CheckStoreCount(3));

        a.fill(A);
        b.fill(B);
        f.realize({a, b});
        for (int y = 0; y < a.height(); y++) {
            for (int x = 0; x < a.width(); x++) {
                int correct_a = x;
                int correct_b = (x < 20) ? 20 * x : y;
                if (a(x, y) != correct_a || b(x, y) != correct_b) {
                    printf("result(%d, %d) = (%d, %d) instead of (%d, %d)\n",
                           x, y, a(x, y), b(x, y), correct_a, correct_b);
                    return 1;
                }
            }
        }
    }

    printf("Test 3...\n");
    {
        Var x("x"), y("y");
        Func f("f"), g("g");

        f(x, y) = {0, 0};

        RDom r(0, 10);
        Expr arg_0 = clamp(select(r.x < 2, 13, undef<int>()), 0, 100);
        Expr arg_1 = clamp(select(r.x < 2, 23, undef<int>()), 0, 100);
        f(arg_0, arg_1) = {f(arg_0, arg_1)[0] + 10, f(arg_0, arg_1)[1] + 5};

        a.fill(A);
        b.fill(B);
        f.realize({a, b});
        for (int y = 0; y < a.height(); y++) {
            for (int x = 0; x < a.width(); x++) {
                int correct_a = (x == 13) && (y == 23) ? 20 : 0;
                int correct_b = (x == 13) && (y == 23) ? 10 : 0;
                if (a(x, y) != correct_a || b(x, y) != correct_b) {
                    printf("result(%d, %d) = (%d, %d) instead of (%d, %d)\n",
                           x, y, a(x, y), b(x, y), correct_a, correct_b);
                    return 1;
                }
            }
        }
    }

    printf("Test 4...\n");
    {

        Var x("x"), y("y");
        Func f("f");

        f(x, y) = Tuple(undef<int32_t>(), undef<int32_t>());

        // There should be no stores since all Tuple values are undef.
        f.add_custom_lowering_pass(new CheckStoreCount(0));

        // Pre-fill with unlikely values so we can verify that undef bits are untouched.
        a.fill(A);
        b.fill(B);
        f.realize({a, b});
    }

    printf("Success!\n");
    return 0;
}