codegen.cpp 2.9 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
/*******************************************************************************
* Copyright 2017-2018 Intel Corporation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
*     http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*******************************************************************************/
16 17 18 19 20 21 22 23

#include <sstream>
#include <string>
#include <vector>

#include "gtest/gtest.h"

#include "ngraph/codegen/compiler.hpp"
Robert Kimball's avatar
Robert Kimball committed
24
#include "ngraph/codegen/execution_engine.hpp"
25 26

using namespace std;
Robert Kimball's avatar
Robert Kimball committed
27
using namespace ngraph;
28

29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
TEST(codegen, eigen_gpl_test)
{
    // In order for this test to pass the in-memory compiler must define EIGEN_MPL2_ONLY
    constexpr auto source = R"(
#if not defined(EIGEN_MPL2_ONLY)
#error("must define flag")
#endif
)";

    codegen::Compiler compiler;
    codegen::ExecutionEngine execution_engine;

    auto module = compiler.compile(source);
    ASSERT_NE(nullptr, module);
}

45
TEST(DISABLED_codegen, simple_return)
46 47 48
{
    constexpr auto source = R"(extern "C" int test() { return 2+5; })";

Robert Kimball's avatar
Robert Kimball committed
49 50 51 52
    codegen::Compiler compiler;
    codegen::ExecutionEngine execution_engine;

    auto module = compiler.compile(source);
53 54
    ASSERT_NE(nullptr, module);

Robert Kimball's avatar
Robert Kimball committed
55
    execution_engine.add_module(module);
56

Robert Kimball's avatar
Robert Kimball committed
57
    execution_engine.finalize();
58

Robert Kimball's avatar
Robert Kimball committed
59
    auto func = execution_engine.find_function<int()>("test");
60 61 62 63 64 65
    ASSERT_NE(nullptr, func);

    int result = func();
    EXPECT_EQ(7, result);
}

66
TEST(DISABLED_codegen, pass_args)
67 68 69
{
    constexpr auto source = R"(extern "C" int test(int a, int b) { return a+b; })";

Robert Kimball's avatar
Robert Kimball committed
70 71 72 73
    codegen::Compiler compiler;
    codegen::ExecutionEngine execution_engine;

    auto module = compiler.compile(source);
74 75
    ASSERT_NE(nullptr, module);

Robert Kimball's avatar
Robert Kimball committed
76
    execution_engine.add_module(module);
77

Robert Kimball's avatar
Robert Kimball committed
78
    execution_engine.finalize();
79

Robert Kimball's avatar
Robert Kimball committed
80
    auto func = execution_engine.find_function<int(int, int)>("test");
81 82 83 84 85 86
    ASSERT_NE(nullptr, func);

    int result = func(20, 22);
    EXPECT_EQ(42, result);
}

87
TEST(DISABLED_codegen, include)
88 89 90 91 92 93 94 95 96 97
{
    constexpr auto source =
        R"(
        #include <cmath>
        extern "C" int test(int a, int b)
        {
            return (int)pow((double)a,(double)b);
        }
    )";

Robert Kimball's avatar
Robert Kimball committed
98 99 100 101
    codegen::Compiler compiler;
    codegen::ExecutionEngine execution_engine;

    auto module = compiler.compile(source);
102 103
    ASSERT_NE(nullptr, module);

Robert Kimball's avatar
Robert Kimball committed
104
    execution_engine.add_module(module);
105

Robert Kimball's avatar
Robert Kimball committed
106
    execution_engine.finalize();
107

Robert Kimball's avatar
Robert Kimball committed
108
    auto func = execution_engine.find_function<int(int, int)>("test");
109 110 111 112 113
    ASSERT_NE(nullptr, func);

    int result = func(20, 2);
    EXPECT_EQ(400, result);
}