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
|
#include "Halide.h"
#include "halide_test_dirs.h"
#include <cstdio>
#include <fstream>
using namespace Halide;
bool error_occurred = false;
void my_error_handler(JITUserContext *user_context, const char *msg) {
// printf("%s\n", msg);
error_occurred = true;
}
int basic_constraints() {
Func f, g;
Var x, y;
ImageParam param(Int(32), 2);
Buffer<int> image1(128, 73);
Buffer<int> image2(144, 23);
f(x, y) = param(x, y) * 2;
param.dim(0).set_bounds(0, 128);
f.jit_handlers().custom_error = my_error_handler;
// This should be fine
param.set(image1);
error_occurred = false;
f.realize({20, 20});
if (error_occurred) {
printf("Error incorrectly raised\n");
return 1;
}
// This should be an error, because dimension 0 of image 2 is not from 0 to 128 like we promised
param.set(image2);
error_occurred = false;
f.realize({20, 20});
if (!error_occurred) {
printf("Error incorrectly not raised\n");
return 1;
}
// Now try constraining the output buffer of a function
g(x, y) = x * y;
g.jit_handlers().custom_error = my_error_handler;
g.output_buffer().dim(0).set_stride(2);
error_occurred = false;
g.realize(image1);
if (!error_occurred) {
printf("Error incorrectly not raised when constraining output buffer\n");
return 1;
}
Func h;
h(x, y) = x * y;
h.jit_handlers().custom_error = my_error_handler;
h.output_buffer()
.dim(0)
.set_stride(1)
.set_bounds(0, ((h.output_buffer().dim(0).extent()) / 8) * 8)
.dim(1)
.set_bounds(0, image1.dim(1).extent());
error_occurred = false;
h.realize(image1);
std::string assembly_file = Internal::get_test_tmp_dir() + "h.s";
Internal::ensure_no_file_exists(assembly_file);
// Also check it compiles ok without an inferred argument list
h.compile_to_assembly(assembly_file, {image1}, "h");
if (error_occurred) {
printf("Error incorrectly raised when constraining output buffer\n");
return 1;
}
Internal::assert_file_exists(assembly_file);
return 0;
}
std::string load_file_to_string(const std::string &filename) {
std::stringstream contents;
std::ifstream file(filename);
std::string line;
while (std::getline(file, line)) {
contents << line << "\n";
}
return contents.str();
}
int alignment_constraints() {
Var x, y;
ImageParam p_aligned(Float(32), 2);
ImageParam p_unaligned(Float(32), 2);
const int alignment = 4;
p_aligned.set_host_alignment(alignment * sizeof(float));
p_aligned.dim(0).set_min((p_aligned.dim(0).min() / alignment) * alignment);
p_aligned.dim(0).set_extent((p_aligned.dim(0).extent() / alignment) * alignment);
p_aligned.dim(1).set_stride((p_aligned.dim(1).stride() / alignment) * alignment);
Func aligned, unaligned;
aligned(x, y) = p_aligned(x, y);
unaligned(x, y) = p_unaligned(x, y);
aligned.vectorize(x, 4);
unaligned.vectorize(x, 4);
aligned.output_buffer().dim(0).set_min(0);
unaligned.output_buffer().dim(0).set_min(0);
Target target = get_jit_target_from_environment();
target.set_feature(Target::NoRuntime);
std::string unaligned_ll_file = Internal::get_test_tmp_dir() + "unaligned.ll";
Internal::ensure_no_file_exists(unaligned_ll_file);
unaligned.compile_to_llvm_assembly(unaligned_ll_file, {p_unaligned}, "unaligned", target);
std::string unaligned_code = load_file_to_string(unaligned_ll_file);
if (unaligned_code.find("align 16") != std::string::npos) {
printf("Found aligned load from unaligned buffer!\n");
return 1;
}
std::string aligned_ll_file = Internal::get_test_tmp_dir() + "aligned.ll";
Internal::ensure_no_file_exists(aligned_ll_file);
aligned.compile_to_llvm_assembly(aligned_ll_file, {p_aligned}, "aligned", target);
std::string aligned_code = load_file_to_string(aligned_ll_file);
if (aligned_code.find("align 16") == std::string::npos) {
printf("Did not find aligned load from aligned buffer!\n");
return 1;
}
return 0;
}
int unstructured_constraints() {
Func f, g;
Var x, y;
ImageParam param(Int(32), 2);
Buffer<int> image1(128, 73);
Buffer<int> image2(144, 23);
f(x, y) = param(x, y) * 2;
Param<int> required_min, required_extent;
required_min.set(0);
required_extent.set(128);
Pipeline pf(f);
pf.add_requirement(param.dim(0).min() == required_min && param.dim(0).extent() == required_extent,
"Custom message:", param.dim(0).min(), param.dim(0).max());
pf.jit_handlers().custom_error = my_error_handler;
// This should be fine
param.set(image1);
error_occurred = false;
pf.realize({20, 20});
if (error_occurred) {
printf("Error incorrectly raised\n");
return 1;
}
// This should be an error, because dimension 0 of image 2 is not from 0 to 128 like we promised
param.set(image2);
error_occurred = false;
pf.realize({20, 20});
if (!error_occurred) {
printf("Error incorrectly not raised\n");
return 1;
}
// Now try constraining the output buffer of a function
g(x, y) = x * y;
Pipeline pg(g);
Param<int> required_stride;
required_stride.set(2);
pg.add_requirement(g.output_buffer().dim(0).stride() == required_stride);
pg.jit_handlers().custom_error = my_error_handler;
error_occurred = false;
pg.realize(image1);
if (!error_occurred) {
printf("Error incorrectly not raised when constraining output buffer\n");
return 1;
}
Func h;
h(x, y) = x * y;
Pipeline ph(h);
ph.jit_handlers().custom_error = my_error_handler;
ph.add_requirement(h.output_buffer().dim(0).stride() == 1);
ph.add_requirement(h.output_buffer().dim(0).min() == 0);
ph.add_requirement(h.output_buffer().dim(0).extent() % 8 == 0);
ph.add_requirement(h.output_buffer().dim(1).min() == 0);
ph.add_requirement(h.output_buffer().dim(1).extent() == image1.dim(1).extent());
error_occurred = false;
h.realize(image1);
if (error_occurred) {
printf("Error incorrectly raised when constraining output buffer\n");
return 1;
}
return 0;
}
int main(int argc, char **argv) {
int result;
result = basic_constraints();
if (result != 0) return result;
result = alignment_constraints();
if (result != 0) return result;
result = unstructured_constraints();
if (result != 0) return result;
printf("Success!\n");
return 0;
}
|