Skip to content

Commit 7c97db0

Browse files
authored
Implement a version getter for the graph compiler (#40)
Add version getter to the graph compiler API Signed-off-by: Dmitry Chigarev <[email protected]>
1 parent f53a112 commit 7c97db0

File tree

5 files changed

+78
-3
lines changed

5 files changed

+78
-3
lines changed

CMakeLists.txt

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
cmake_minimum_required(VERSION 3.20)
2-
project(GraphCompiler LANGUAGES C CXX)
3-
2+
project(GraphCompiler VERSION "0.1.0" LANGUAGES C CXX)
43

54
set(CMAKE_CXX_STANDARD 17)
65
set(CMAKE_CXX_STANDARD_REQUIRED ON)
@@ -42,6 +41,8 @@ set(GC_LIB_SOURCES CACHE INTERNAL "The graph_compiler library source paths")
4241
set(GC_LIB_INCLUDES CACHE INTERNAL "The graph_compiler library include paths")
4342

4443
add_definitions(${LLVM_DEFINITIONS})
44+
include("cmake/version.cmake")
45+
4546
add_subdirectory(include)
4647
add_subdirectory(lib)
4748
add_subdirectory(src)

cmake/version.cmake

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
include_guard()
2+
3+
string(REPLACE "." ";" VERSION_LIST ${PROJECT_VERSION})
4+
list(GET VERSION_LIST 0 GC_VERSION_MAJOR)
5+
list(GET VERSION_LIST 1 GC_VERSION_MINOR)
6+
list(GET VERSION_LIST 2 GC_VERSION_PATCH)
7+
8+
find_package(Git)
9+
if(GIT_FOUND)
10+
execute_process(COMMAND ${GIT_EXECUTABLE} -c log.showSignature=false log --no-abbrev-commit --oneline -1 --format=%H
11+
WORKING_DIRECTORY ${PROJECT_SOURCE_DIR}
12+
RESULT_VARIABLE RESULT
13+
OUTPUT_VARIABLE GC_VERSION_HASH
14+
OUTPUT_STRIP_TRAILING_WHITESPACE)
15+
endif()
16+
17+
if(NOT GIT_FOUND OR RESULT)
18+
set(GC_VERSION_HASH "N/A")
19+
endif()
20+
21+
add_compile_definitions(
22+
GC_VERSION_MAJOR=${GC_VERSION_MAJOR}
23+
GC_VERSION_MINOR=${GC_VERSION_MINOR}
24+
GC_VERSION_PATCH=${GC_VERSION_PATCH}
25+
GC_VERSION_HASH="${GC_VERSION_HASH}")

include/gc_version.h

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#ifndef GC_VERSION_H
2+
#define GC_VERSION_H
3+
4+
#if !defined(GC_VERSION_MAJOR) || !defined(GC_VERSION_MINOR) || !defined(GC_VERSION_PATCH)
5+
// define an invalid version if it wasn't defined by CMake
6+
#include <limits>
7+
#define GC_VERSION_MAJOR std::numeric_limits<uint8_t>::max()
8+
#define GC_VERSION_MINOR std::numeric_limits<uint8_t>::max()
9+
#define GC_VERSION_PATCH std::numeric_limits<uint8_t>::max()
10+
#endif
11+
#ifndef GC_VERSION_HASH
12+
#define GC_VERSION_HASH "N/A"
13+
#endif
14+
15+
#endif

src/dnnl/dnnl_graph_compiler.cpp

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,20 @@
11
#include "dnnl_graph_compiler.hpp"
2+
#include "gc_version.h"
23
#include <new>
34

45
// dnnl_graph_compiler.h interface implementation.
5-
// TODO: Implement
6+
// TODO: Implement.
7+
8+
const dnnl_graph_compiler_version *dnnl_graph_compiler_get_version(void) {
9+
static const dnnl_graph_compiler_version ver = {
10+
.api_version = {DNNL_GC_API_V_MAJOR, DNNL_GC_API_V_MINOR,
11+
DNNL_GC_API_V_PATCH,
12+
DNNL_GC_API_V_HASH}, // version defined by oneDNN
13+
.gc_version = {
14+
GC_VERSION_MAJOR, GC_VERSION_MINOR, GC_VERSION_PATCH,
15+
GC_VERSION_HASH}}; // version defined by graph compiler itself
16+
return &ver;
17+
}
618

719
dnnl_status_t
820
dnnl_graph_compiler_create(const struct dnnl_graph_compiler_context *ctx,

test/dnnl/test_dnnl_c_interface.cpp

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#include "dnnl_graph_compiler.h"
22
#include "dnnl_test_utils.hpp"
3+
#include "gc_version.h"
34
#include <gtest/gtest.h>
45

56
TEST(dnnl_graph_compiler, c_interface) {
@@ -29,6 +30,27 @@ TEST(dnnl_graph_compiler, c_interface) {
2930
dnnl_graph_compiler_destroy(gc);
3031
}
3132

33+
TEST(dnnl_graph_compiler, get_version) {
34+
auto v = dnnl_graph_compiler_get_version();
35+
36+
ASSERT_NE(v, nullptr);
37+
38+
ASSERT_EQ(v->api_version.major, DNNL_GC_API_V_MAJOR);
39+
ASSERT_EQ(v->api_version.minor, DNNL_GC_API_V_MINOR);
40+
ASSERT_EQ(v->api_version.patch, DNNL_GC_API_V_PATCH);
41+
ASSERT_STREQ(v->api_version.hash, DNNL_GC_API_V_HASH);
42+
43+
// check if the version is valid
44+
ASSERT_NE(v->gc_version.major, std::numeric_limits<uint8_t>::max());
45+
ASSERT_NE(v->gc_version.minor, std::numeric_limits<uint8_t>::max());
46+
ASSERT_NE(v->gc_version.patch, std::numeric_limits<uint8_t>::max());
47+
48+
ASSERT_EQ(v->gc_version.major, GC_VERSION_MAJOR);
49+
ASSERT_EQ(v->gc_version.minor, GC_VERSION_MINOR);
50+
ASSERT_EQ(v->gc_version.patch, GC_VERSION_PATCH);
51+
ASSERT_STREQ(v->gc_version.hash, GC_VERSION_HASH);
52+
}
53+
3254
int main(int argc, char **argv) {
3355
::testing::InitGoogleTest(&argc, argv);
3456
return RUN_ALL_TESTS();

0 commit comments

Comments
 (0)