File: compile_to_multitarget.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 (250 lines) | stat: -rw-r--r-- 9,382 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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
#include "Halide.h"
#include "halide_test_dirs.h"

#include <cstdio>

using namespace Halide;

// Given a path like /path/to/some/file.ext, return file.ext
// If the path contains no separators (/ or \), just return it as-is
std::string leaf_name(const std::string &path) {
    size_t sep = std::min(path.rfind('/'), path.rfind('\\'));
    return path.substr(sep == std::string::npos ? 0 : sep + 1);
}

std::string get_output_path_prefix(const std::string &base) {
    return Internal::get_test_tmp_dir() + "halide_test_correctness_compile_to_multitarget_" + base;
}

void test_compile_to_static_library(Func j) {
    std::string filename_prefix = get_output_path_prefix("c1");
    const char *a = get_host_target().os == Target::Windows ? ".lib" : ".a";

    std::vector<Target> targets = {
        Target("host-profile-no_bounds_query"),
        Target("host-profile"),
    };

    std::vector<std::string> files;
    files.push_back(filename_prefix + ".h");
    files.push_back(filename_prefix + a);

    for (auto f : files) {
        Internal::ensure_no_file_exists(f);
    }

    j.compile_to_multitarget_static_library(filename_prefix, j.infer_arguments(), targets);

    for (auto f : files) {
        Internal::assert_file_exists(f);
    }

    // TODO: would be nice to examine the contents of the library and verify the
    // sub-objects have the filenames we expect.
}

void test_compile_to_object_files(Func j) {
    std::string filename_prefix = get_output_path_prefix("c2");
    const char *o = get_host_target().os == Target::Windows ? ".obj" : ".o";

    std::vector<std::string> target_strings = {
        "host-profile-no_bounds_query",
        "host-profile",
    };

    std::vector<Target> targets;
    for (auto s : target_strings) {
        targets.emplace_back(s);
    }

    std::vector<std::string> files;
    files.push_back(filename_prefix + ".h");
    files.push_back(filename_prefix + "_runtime" + o);
    files.push_back(filename_prefix + "_wrapper" + o);
    for (auto s : target_strings) {
        files.push_back(filename_prefix + "-" + s + o);
    }

    for (auto f : files) {
        Internal::ensure_no_file_exists(f);
    }

    j.compile_to_multitarget_object_files(filename_prefix, j.infer_arguments(), targets, target_strings);

    for (auto f : files) {
        Internal::assert_file_exists(f);
    }
}

void test_compile_to_object_files_no_runtime(Func j) {
    std::string filename_prefix = get_output_path_prefix("c3");
    const char *o = get_host_target().os == Target::Windows ? ".obj" : ".o";

    std::vector<std::string> target_strings = {
        "host-profile-no_bounds_query-no_runtime",
        "host-profile-no_runtime",
    };

    std::vector<Target> targets;
    for (auto s : target_strings) {
        targets.emplace_back(s);
    }

    std::vector<std::string> files;
    files.push_back(filename_prefix + ".h");
    files.push_back(filename_prefix + "_wrapper" + o);
    for (auto s : target_strings) {
        files.push_back(filename_prefix + "-" + s + o);
    }

    for (auto f : files) {
        Internal::ensure_no_file_exists(f);
    }

    j.compile_to_multitarget_object_files(filename_prefix, j.infer_arguments(), targets, target_strings);

    for (auto f : files) {
        Internal::assert_file_exists(f);
    }
}

void test_compile_to_object_files_single_target(Func j) {
    std::string filename_prefix = get_output_path_prefix("c4");
    const char *o = get_host_target().os == Target::Windows ? ".obj" : ".o";

    std::vector<std::string> target_strings = {
        "host-debug",
    };

    std::vector<Target> targets;
    for (auto s : target_strings) {
        targets.emplace_back(s);
    }

    std::vector<std::string> files;
    files.push_back(filename_prefix + ".h");
    files.push_back(filename_prefix + o);

    for (auto f : files) {
        Internal::ensure_no_file_exists(f);
    }

    j.compile_to_multitarget_object_files(filename_prefix, j.infer_arguments(), targets, target_strings);

    for (auto f : files) {
        Internal::assert_file_exists(f);
    }
}

void test_compile_to_everything(Func j, bool do_object) {
    std::string filename_prefix = get_output_path_prefix(do_object ? "c5" : "c6");
    const char *a = get_host_target().os == Target::Windows ? ".lib" : ".a";
    const char *o = get_host_target().os == Target::Windows ? ".obj" : ".o";

    std::vector<std::string> target_strings = {
        "host-profile-no_bounds_query",
        "host-profile",
    };

    std::vector<Target> targets;
    for (auto s : target_strings) {
        targets.emplace_back(s);
    }

    std::vector<std::string> files;

    // single-file outputs
    for (const char *ext : {".h", ".halide_generated.cpp", ".halide_compiler_log", ".py.cpp", ".pytorch.h", ".registration.cpp", ".schedule.h", a}) {
        if (do_object && !strcmp(ext, a)) continue;
        files.push_back(filename_prefix + ext);
    }
    if (do_object) {
        files.push_back(filename_prefix + "_runtime" + o);
        files.push_back(filename_prefix + "_wrapper" + o);
    }

    // multi-file outputs
    for (const auto &s : target_strings) {
        for (const char *ext : {".s", ".bc", ".featurization", ".ll", ".stmt", ".stmt.html", o}) {
            if (!do_object && !strcmp(ext, o)) continue;
            files.push_back(filename_prefix + "-" + s + ext);
        }
    }

    for (auto f : files) {
        Internal::ensure_no_file_exists(f);
    }

    // There isn't a public API that allows this directly, but Generators allow this
    // via command-line usage, so we'll test the internal API here.
    auto args = j.infer_arguments();
    auto module_producer = [&j, &args](const std::string &name, const Target &target) -> Module {
        return j.compile_to_module(args, name, target);
    };
    std::map<OutputFileType, std::string> outputs = {
        {OutputFileType::assembly, filename_prefix + ".s"},                        // IsMulti
        {OutputFileType::bitcode, filename_prefix + ".bc"},                        // IsMulti
        {OutputFileType::c_header, filename_prefix + ".h"},                        // IsSingle
        {OutputFileType::c_source, filename_prefix + ".halide_generated.cpp"},     // IsSingle
        {OutputFileType::compiler_log, filename_prefix + ".halide_compiler_log"},  // IsSingle
        // Note: compile_multitarget() doesn't produce cpp_stub output,
        // even if you pass this in.
        // {OutputFileType::cpp_stub, filename_prefix + ".stub.h"},  // IsSingle
        {OutputFileType::featurization, filename_prefix + ".featurization"},           // IsMulti
        {OutputFileType::function_info_header, filename_prefix + ".function_info.h"},  // IsSingle
        // Note: compile_multitarget() doesn't produce hlpipe output,
        // even if you pass this in (since the pipeline has already been lowered)
        // {OutputFileType::hlpipe, filename_prefix + ".hlpipe"},  // IsSingle
        {OutputFileType::llvm_assembly, filename_prefix + ".ll"},               // IsMulti
        {OutputFileType::object, filename_prefix + o},                          // IsMulti
        {OutputFileType::python_extension, filename_prefix + ".py.cpp"},        // IsSingle
        {OutputFileType::pytorch_wrapper, filename_prefix + ".pytorch.h"},      // IsSingle
        {OutputFileType::registration, filename_prefix + ".registration.cpp"},  // IsSingle
        {OutputFileType::schedule, filename_prefix + ".schedule.h"},            // IsSingle
        {OutputFileType::static_library, filename_prefix + a},                  // IsSingle
        {OutputFileType::stmt, filename_prefix + ".stmt"},                      // IsMulti
        {OutputFileType::stmt_html, filename_prefix + ".stmt.html"},            // IsMulti
    };
    if (do_object) {
        outputs.erase(OutputFileType::static_library);
    } else {
        outputs.erase(OutputFileType::object);
    }
    const CompilerLoggerFactory compiler_logger_factory =
        [](const std::string &, const Target &) -> std::unique_ptr<Internal::CompilerLogger> {
        // We don't care about the contents of the compiler log - only whether
        // it exists or not --  so just fill in with arbitrary strings.
        return std::unique_ptr<Internal::CompilerLogger>(new Internal::JSONCompilerLogger("generator_name", "function_name", "autoscheduler_name", Target(), "generator_args", false));
    };
    // The first argument to compile_multitarget is *function* name, not filename
    std::string function_name = leaf_name(filename_prefix);
    compile_multitarget(function_name, outputs, targets, target_strings, module_producer, compiler_logger_factory);

    for (auto f : files) {
        Internal::assert_file_exists(f);
    }
}

int main(int argc, char **argv) {
    Param<float> factor("factor");
    Func f, g, h, j;
    Var x, y;
    f(x, y) = x + y;
    g(x, y) = cast<float>(f(x, y) + f(x + 1, y));
    h(x, y) = f(x, y) + g(x, y);
    j(x, y) = h(x, y) * 2 * factor;

    f.compute_root();
    g.compute_root();
    h.compute_root();

    test_compile_to_static_library(j);
    test_compile_to_object_files(j);
    test_compile_to_object_files_no_runtime(j);
    test_compile_to_object_files_single_target(j);
    test_compile_to_everything(j, /*do_object*/ true);
    test_compile_to_everything(j, /*do_object*/ false);

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