Skip to content

Commit

Permalink
LightMetal - Add Flatbuffers into cmake infra/build as cpm package (#…
Browse files Browse the repository at this point in the history
…17039)

 - Mostly unused here, used by light metal capture/replay coming in follow up PR
 - Add CMAKE_POSITION_INDEPENDENT_CODE=ON globally (-fPIC) following discussion
   (needed for flatbuffer to avoid linking errors when lifting to tt-mlir project.
 - Use -Wno-restrict for flatbuffers compile to supress warning in g++12 build,
   though don't know why they were fine previously with flatbuffers from UMD
 - Misc PR feedback about PUBLIC vs PRIVATE, etc.
  • Loading branch information
kmabeeTT authored and yieldthought committed Jan 31, 2025
1 parent 61b7cd9 commit 0a91e75
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 2 deletions.
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ include(clang-tidy)
set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_EXTENSIONS OFF)
set(CMAKE_POSITION_INDEPENDENT_CODE TRUE)

if(NOT ENABLE_LIBCXX)
# required when linking with libstdc++ with clang and gcc
Expand Down
17 changes: 17 additions & 0 deletions cmake/flatbuffers.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Function to generate FlatBuffers C++ headers from schema files
function(GENERATE_FBS_HEADER FBS_FILE)
get_filename_component(FBS_FILE_NAME ${FBS_FILE} NAME_WE)
set(FBS_GENERATED_HEADER_DIR "${CMAKE_CURRENT_BINARY_DIR}/flatbuffers")
set(FBS_GENERATED_HEADER_FILE "${FBS_GENERATED_HEADER_DIR}/${FBS_FILE_NAME}_generated.h")
add_custom_command(
OUTPUT
${FBS_GENERATED_HEADER_FILE}
COMMAND
flatc --cpp --scoped-enums -I ${CMAKE_CURRENT_SOURCE_DIR} -o "${FBS_GENERATED_HEADER_DIR}" ${FBS_FILE}
DEPENDS
flatc
${FBS_FILE}
COMMENT "Building C++ header for ${FBS_FILE}"
)
set(FBS_GENERATED_HEADER_FILE ${FBS_GENERATED_HEADER_FILE} PARENT_SCOPE)
endfunction()
2 changes: 0 additions & 2 deletions cmake/tracy.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,6 @@ set_target_properties(
"${PROJECT_BINARY_DIR}/lib"
ARCHIVE_OUTPUT_DIRECTORY
"${PROJECT_BINARY_DIR}/lib"
POSITION_INDEPENDENT_CODE
ON # this is equivalent to adding -fPIC
OUTPUT_NAME
"tracy"
)
Expand Down
21 changes: 21 additions & 0 deletions dependencies/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -173,3 +173,24 @@ CPMAddPackage(
"CMAKE_MESSAGE_LOG_LEVEL NOTICE" # Taskflow's CMakeLists.txt is super noisy
)
add_library(Taskflow::Taskflow ALIAS Taskflow)

############################################################################################################################
# flatbuffers : https://github.com/google/flatbuffers
############################################################################################################################

CPMAddPackage(
NAME flatbuffers
GITHUB_REPOSITORY google/flatbuffers
GIT_TAG v24.3.25
OPTIONS
"FLATBUFFERS_BUILD_FLATC ON"
"FLATBUFFERS_BUILD_TESTS OFF"
"FLATBUFFERS_SKIP_MONSTER_EXTRA ON"
"FLATBUFFERS_STRICT_MODE ON"
)

if(flatbuffers_ADDED)
# Few files including idl_gen_dart.cpp:175:18, Possibly related: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=105329
target_compile_options(flatc PRIVATE -Wno-restrict)
target_compile_options(flatbuffers PRIVATE -Wno-restrict)
endif()
13 changes: 13 additions & 0 deletions tt_metal/impl/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,16 @@ set(IMPL_SRC
${CMAKE_CURRENT_SOURCE_DIR}/event/event.cpp
)

# Include helper functions and generate headers from flatbuffer schemas
include(flatbuffers)

set(FLATBUFFER_SCHEMAS) # Empty to start, coming soon.

foreach(FBS_FILE ${FLATBUFFER_SCHEMAS})
GENERATE_FBS_HEADER(${FBS_FILE})
list(APPEND IMPL_SRC ${FBS_GENERATED_HEADER_FILE})
endforeach()

add_library(impl OBJECT ${IMPL_SRC})
add_library(Metalium::Metal::Impl ALIAS impl)

Expand All @@ -60,6 +70,7 @@ target_link_libraries(
Taskflow::Taskflow
TT::Metalium::HostDevCommon
Metalium::Metal::Hardware
FlatBuffers::FlatBuffers
)

target_include_directories(
Expand All @@ -68,6 +79,8 @@ target_include_directories(
${PROJECT_SOURCE_DIR}/tt_metal
${CMAKE_CURRENT_SOURCE_DIR}
${PROJECT_SOURCE_DIR}/tt_metal/include
PRIVATE
${CMAKE_CURRENT_BINARY_DIR}/flatbuffers
)

target_compile_options(impl PUBLIC -Wno-int-to-pointer-cast)

0 comments on commit 0a91e75

Please sign in to comment.