pytorch/test/cpp/tensorexpr/test_cpp_codegen.cpp
Cheng Chang 109bc1047e [NNC] Generate C++ code for Allocate and Free (#51070)
Summary:
This is the initial skeleton for C++ codegen, it includes generations for Allocate and Free.

Pull Request resolved: https://github.com/pytorch/pytorch/pull/51070

Test Plan: New unit tests are added to `test_cpp_codegen.cpp`.

Reviewed By: ZolotukhinM

Differential Revision: D26061818

Pulled By: cheng-chang

fbshipit-source-id: b5256b2dcee6b2583ba73b6c9684994dbe7cdc1f
2021-02-01 13:06:51 -08:00

58 lines
1.5 KiB
C++

#include <gtest/gtest.h>
#include <test/cpp/tensorexpr/test_base.h>
#include <torch/csrc/jit/testing/file_check.h>
#include "torch/csrc/jit/tensorexpr/cpp_codegen.h"
#include "torch/csrc/jit/tensorexpr/mem_arena.h"
#include "torch/csrc/jit/tensorexpr/stmt.h"
namespace torch {
namespace jit {
using namespace torch::jit::tensorexpr;
TEST(CppPrinter, AllocateOnStackThenFree) {
constexpr int dim0 = 2, dim1 = 3;
KernelScope kernel_scope;
VarHandle var("x", kHandle);
Allocate* alloc = Allocate::make(var, kInt, {dim0, dim1});
Free* free = Free::make(var);
Block* block = Block::make({alloc, free});
std::stringstream ss;
CppPrinter printer(&ss);
printer.visit(block);
const std::string expected = R"(
# CHECK: {
# CHECK: int x[6];
# CHECK: }
)";
torch::jit::testing::FileCheck().run(expected, ss.str());
}
TEST(CppPrinter, AllocateOnHeapThenFree) {
constexpr int dim0 = 20, dim1 = 50, dim2 = 3;
KernelScope kernel_scope;
VarHandle var("y", kHandle);
Allocate* alloc = Allocate::make(var, kLong, {dim0, dim1, dim2});
Free* free = Free::make(var);
Block* block = Block::make({alloc, free});
std::stringstream ss;
CppPrinter printer(&ss);
printer.visit(block);
// size(long) = 8;
// dim0 * dim1 * dim2 * size(long) = 24000.
const std::string expected = R"(
# CHECK: {
# CHECK: int64_t* y = static_cast<int64_t*>(malloc(24000));
# CHECK: free(y);
# CHECK: }
)";
torch::jit::testing::FileCheck().run(expected, ss.str());
}
} // namespace jit
} // namespace torch