|
| 1 | +#include <gtest/gtest.h> |
| 2 | + |
| 3 | +#include <test/cpp/tensorexpr/test_base.h> |
| 4 | + |
| 5 | +#include <torch/csrc/jit/testing/file_check.h> |
| 6 | +#include "torch/csrc/jit/tensorexpr/cpp_codegen.h" |
| 7 | +#include "torch/csrc/jit/tensorexpr/mem_arena.h" |
| 8 | +#include "torch/csrc/jit/tensorexpr/stmt.h" |
| 9 | + |
| 10 | +namespace torch { |
| 11 | +namespace jit { |
| 12 | + |
| 13 | +using namespace torch::jit::tensorexpr; |
| 14 | + |
| 15 | +TEST(CppPrinter, AllocateOnStackThenFree) { |
| 16 | + constexpr int dim0 = 2, dim1 = 3; |
| 17 | + KernelScope kernel_scope; |
| 18 | + VarHandle var("x", kHandle); |
| 19 | + Allocate* alloc = Allocate::make(var, kInt, {dim0, dim1}); |
| 20 | + Free* free = Free::make(var); |
| 21 | + Block* block = Block::make({alloc, free}); |
| 22 | + |
| 23 | + std::stringstream ss; |
| 24 | + CppPrinter printer(&ss); |
| 25 | + printer.visit(block); |
| 26 | + const std::string expected = R"( |
| 27 | + # CHECK: { |
| 28 | + # CHECK: int x[6]; |
| 29 | + # CHECK: } |
| 30 | + )"; |
| 31 | + torch::jit::testing::FileCheck().run(expected, ss.str()); |
| 32 | +} |
| 33 | + |
| 34 | +TEST(CppPrinter, AllocateOnHeapThenFree) { |
| 35 | + constexpr int dim0 = 20, dim1 = 50, dim2 = 3; |
| 36 | + KernelScope kernel_scope; |
| 37 | + VarHandle var("y", kHandle); |
| 38 | + Allocate* alloc = Allocate::make(var, kLong, {dim0, dim1, dim2}); |
| 39 | + Free* free = Free::make(var); |
| 40 | + Block* block = Block::make({alloc, free}); |
| 41 | + |
| 42 | + std::stringstream ss; |
| 43 | + CppPrinter printer(&ss); |
| 44 | + printer.visit(block); |
| 45 | + // size(long) = 8; |
| 46 | + // dim0 * dim1 * dim2 * size(long) = 24000. |
| 47 | + const std::string expected = R"( |
| 48 | + # CHECK: { |
| 49 | + # CHECK: int64_t* y = static_cast<int64_t*>(malloc(24000)); |
| 50 | + # CHECK: free(y); |
| 51 | + # CHECK: } |
| 52 | + )"; |
| 53 | + torch::jit::testing::FileCheck().run(expected, ss.str()); |
| 54 | +} |
| 55 | + |
| 56 | +} // namespace jit |
| 57 | +} // namespace torch |
0 commit comments