Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 8 additions & 6 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -184,17 +184,15 @@ option(TARGET_OPENCL "Include OpenCL-C target" ON)
option(TARGET_OPENGL "Include OpenGL/GLSL target" ON)
option(TARGET_OPENGLCOMPUTE "Include OpenGLCompute target" ON)
option(TARGET_D3D12COMPUTE "Include Direct3D 12 Compute target" ON)
option(HALIDE_SHARED_LIBRARY "Build as a shared library" ON)
option(HALIDE_SHARED_LIBRARY "Build tests+apps using shared library (vs static)" ON)
option(HALIDE_ENABLE_RTTI "Enable RTTI" ${LLVM_ENABLE_RTTI})
option(HALIDE_ENABLE_EXCEPTIONS "Enable exceptions" ${LLVM_ENABLE_EH})
option(HALIDE_USE_CODEMODEL_LARGE "Use the Large LLVM codemodel" OFF)

if (HALIDE_SHARED_LIBRARY)
set(HALIDE_LIBRARY_TYPE SHARED)
message(STATUS "Building Halide as a shared library")
message(STATUS "Build tests+apps using Halide shared library")
else()
set(HALIDE_LIBRARY_TYPE STATIC)
message(STATUS "Building Halide as a static library")
message(STATUS "Build tests+apps using Halide static library")
endif()

if (HALIDE_ENABLE_RTTI AND NOT LLVM_ENABLE_RTTI)
Expand Down Expand Up @@ -291,7 +289,11 @@ endif()
# to specific values, rather than relying on HALIDE_DISTRIB_DIR to be set correctly.
set(HALIDE_INCLUDE_DIR "${CMAKE_BINARY_DIR}/include")
set(HALIDE_TOOLS_DIR "${HALIDE_BASE_DIR}/tools")
set(HALIDE_COMPILER_LIB Halide)
if (HALIDE_SHARED_LIBRARY)
set(HALIDE_COMPILER_LIB HalideShared)
else()
set(HALIDE_COMPILER_LIB HalideStatic)
endif()
set(HALIDE_DISTRIB_DIR "/bad-path")
include(halide.cmake)

Expand Down
179 changes: 103 additions & 76 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -459,7 +459,7 @@ foreach (i ${RUNTIME_HEADER_FILES})
endforeach()

# Keep this list sorted in alphabetical order.
add_library(Halide ${HALIDE_LIBRARY_TYPE}
set(SOURCE_FILES
AddImageChecks.cpp
AddParameterChecks.cpp
AlignLoads.cpp
Expand Down Expand Up @@ -626,160 +626,187 @@ add_library(Halide ${HALIDE_LIBRARY_TYPE}
WasmExecutor.cpp
WrapCalls.cpp
WrapExternStages.cpp
${HEADER_FILES}
${INITIAL_MODULES}
)

# We could expose the /MP flag to all targets, but that might end up saturating the build
# since multiple MSBuild projects might get built in parallel, each of which compiling their
# source files in parallel; the Halide library itself is a "knot" point of the build graph,
# so compiling its files in parallel should not oversubscribe the system
target_compile_options(Halide PUBLIC $<$<CXX_COMPILER_ID:MSVC>:/MP>)

# Define Halide_SHARED or Halide_STATIC depending on library type
target_compile_definitions(Halide PRIVATE "-DHalide_${HALIDE_LIBRARY_TYPE}")
add_dependencies(Halide HalideIncludes)
# The list of -D flags to pass when compiling
set(HALIDE_COMPILE_DEFINITIONS )

# List of LLVM Components required
# This list will be appended to depending on the targets we need to support
# See the output of ``llvm-config --components`` for a list of possible components
set(LLVM_COMPONENTS mcjit;bitwriter;linker)
list(APPEND LLVM_COMPONENTS passes)

# Set definitions and compiler flags

# Note when PUBLIC or INTERFACE scope is used in target_compile_* then targets
# that link against the Halide library inherit those options and definitions
target_include_directories(Halide PRIVATE ${LLVM_INCLUDE_DIRS})
target_include_directories(Halide INTERFACE "${CMAKE_BINARY_DIR}/include")
set(LLVM_COMPONENTS mcjit;bitwriter;linker;passes)

# TODO: For targets we can link against even fewer libraries by specifying
# only the components we **REALLY** need (e.g. x86asmprinter;x86codegen rather than x86)
if (TARGET_X86)
target_compile_definitions(Halide PRIVATE "-DWITH_X86")
list(APPEND HALIDE_COMPILE_DEFINITIONS "-DWITH_X86")
list(APPEND LLVM_COMPONENTS X86)
endif()

if (TARGET_ARM)
target_compile_definitions(Halide PRIVATE "-DWITH_ARM")
list(APPEND HALIDE_COMPILE_DEFINITIONS "-DWITH_ARM")
list(APPEND LLVM_COMPONENTS ARM)
endif()

if (TARGET_AARCH64)
target_compile_definitions(Halide PRIVATE "-DWITH_AARCH64")
list(APPEND HALIDE_COMPILE_DEFINITIONS "-DWITH_AARCH64")
list(APPEND LLVM_COMPONENTS AArch64)
endif()

if (TARGET_HEXAGON)
target_compile_definitions(Halide PRIVATE "-DWITH_HEXAGON")
list(APPEND HALIDE_COMPILE_DEFINITIONS "-DWITH_HEXAGON")
list(APPEND LLVM_COMPONENTS Hexagon)
endif()

if (TARGET_MIPS)
target_compile_definitions(Halide PRIVATE "-DWITH_MIPS")
list(APPEND HALIDE_COMPILE_DEFINITIONS "-DWITH_MIPS")
list(APPEND LLVM_COMPONENTS Mips)
endif()

if (TARGET_POWERPC)
target_compile_definitions(Halide PRIVATE "-DWITH_POWERPC")
list(APPEND HALIDE_COMPILE_DEFINITIONS "-DWITH_POWERPC")
list(APPEND LLVM_COMPONENTS PowerPC)
endif()

if (TARGET_WEBASSEMBLY)
target_compile_definitions(Halide PRIVATE "-DWITH_WEBASSEMBLY")
list(APPEND HALIDE_COMPILE_DEFINITIONS "-DWITH_WEBASSEMBLY")
list(APPEND LLVM_COMPONENTS WebAssembly)
endif()

if (TARGET_PTX)
target_compile_definitions(Halide PRIVATE "-DWITH_PTX")
list(APPEND HALIDE_COMPILE_DEFINITIONS "-DWITH_PTX")
list(APPEND LLVM_COMPONENTS NVPTX)
endif()

if (TARGET_AMDGPU)
target_compile_definitions(Halide PRIVATE "-DWITH_AMDGPU")
list(APPEND HALIDE_COMPILE_DEFINITIONS "-DWITH_AMDGPU")
list(APPEND LLVM_COMPONENTS AMDGPU)
endif()

if (TARGET_RISCV)
target_compile_definitions(Halide PRIVATE "-DWITH_RISCV")
list(APPEND HALIDE_COMPILE_DEFINITIONS "-DWITH_RISCV")
list(APPEND LLVM_COMPONENTS RISCV)
endif()

if (TARGET_OPENCL)
target_compile_definitions(Halide PRIVATE "-DWITH_OPENCL")
list(APPEND HALIDE_COMPILE_DEFINITIONS "-DWITH_OPENCL")
endif()

if (TARGET_OPENGL)
target_compile_definitions(Halide PRIVATE "-DWITH_OPENGL")
list(APPEND HALIDE_COMPILE_DEFINITIONS "-DWITH_OPENGL")
endif()

if (TARGET_METAL)
target_compile_definitions(Halide PRIVATE "-DWITH_METAL")
list(APPEND HALIDE_COMPILE_DEFINITIONS "-DWITH_METAL")
endif()

if (TARGET_D3D12COMPUTE)
target_compile_definitions(Halide PRIVATE "-DWITH_D3D12")
list(APPEND HALIDE_COMPILE_DEFINITIONS "-DWITH_D3D12")
endif()

target_compile_definitions(Halide PRIVATE "-DLLVM_VERSION=${LLVM_VERSION}")
target_compile_definitions(Halide PRIVATE "-DCOMPILING_HALIDE")
target_compile_definitions(Halide PRIVATE ${LLVM_DEFINITIONS})
list(APPEND HALIDE_COMPILE_DEFINITIONS "-DLLVM_VERSION=${LLVM_VERSION}")
list(APPEND HALIDE_COMPILE_DEFINITIONS "-DCOMPILING_HALIDE")

# target_compile_definitions(${OBJ} PRIVATE ${HALIDE_COMPILE_DEFINITIONS} ${LLVM_DEFINITIONS})
if (NOT LLVM_ENABLE_ASSERTIONS)
target_compile_definitions(Halide PRIVATE NDEBUG)
list(APPEND HALIDE_COMPILE_DEFINITIONS NDEBUG)
endif()

# The list of compiler options to pass when compiling
set(HALIDE_COMPILE_OPTIONS )

if (MSVC)
list(APPEND HALIDE_COMPILE_DEFINITIONS "-D_CRT_SECURE_NO_WARNINGS")
list(APPEND HALIDE_COMPILE_DEFINITIONS "-D_SCL_SECURE_NO_WARNINGS")

# Suppress some warnings
# 4244: conversion, possible loss of data
# 4267: conversion, possible loss of data
# 4800: BOOL -> true or false
# 4996: compiler encountered deprecated declaration
target_compile_options(Halide PUBLIC /wd4244 /wd4267 /wd4800 /wd4996)
# Injected from recent LLVM:
target_compile_options(Halide PUBLIC /wd4141) # 'inline' used more than once
target_compile_options(Halide PUBLIC /wd4146) # unary minus applied to unsigned type
target_compile_options(Halide PUBLIC /wd4291) # No matching operator delete found

target_compile_definitions(Halide PUBLIC "-D_CRT_SECURE_NO_WARNINGS" "-D_SCL_SECURE_NO_WARNINGS")
list(APPEND HALIDE_COMPILE_OPTIONS /wd4244) # 4244: conversion, possible loss of data
list(APPEND HALIDE_COMPILE_OPTIONS /wd4267) # 4267: conversion, possible loss of data
list(APPEND HALIDE_COMPILE_OPTIONS /wd4800) # 4800: BOOL -> true or false
list(APPEND HALIDE_COMPILE_OPTIONS /wd4996) # 4996: compiler encountered deprecated declaration
list(APPEND HALIDE_COMPILE_OPTIONS /wd4141) # 'inline' used more than once
list(APPEND HALIDE_COMPILE_OPTIONS /wd4146) # unary minus applied to unsigned type
list(APPEND HALIDE_COMPILE_OPTIONS /wd4291) # No matching operator delete found

# To compile LLVM headers following was taken from LLVM CMake files:
# Disable sized deallocation if the flag is supported. MSVC fails to compile
# the operator new overload in LLVM/IR/Function.h and Instruction.h otherwise.
# See LLVM PR: 23513 (https://llvm.org/bugs/show_bug.cgi?id=23513)
check_cxx_compiler_flag("/WX /Zc:sizedDealloc-" SUPPORTS_SIZED_DEALLOC)
if (SUPPORTS_SIZED_DEALLOC)
target_compile_options(Halide PRIVATE "/Zc:sizedDealloc-")
list(APPEND HALIDE_COMPILE_OPTIONS "/Zc:sizedDealloc-")
endif()
else()
if (NOT HALIDE_ENABLE_RTTI)
if (NOT MSVC)
target_compile_options(Halide PUBLIC "-fno-rtti")
list(APPEND HALIDE_COMPILE_OPTIONS "-fno-rtti")
else()
target_compile_options(Halide PUBLIC "/GR-")
list(APPEND HALIDE_COMPILE_OPTIONS "/GR-")
endif()
endif()
endif()

# Get the LLVM libraries we need
llvm_map_components_to_libnames(LIBS ${LLVM_COMPONENTS})

# When building a shared library the LLVM libraries will be
# embedded in the Halide library. When building a static library
# LLVM is not embedded but CMake knows that when building an executable
# against the Halide static library that it needs to link LLVM too so
# PRIVATE scope is the correct choice here.
target_link_libraries(Halide PRIVATE ${LIBS})

if (NOT MSVC)
set(LLVM_CONFIG ${LLVM_TOOLS_BINARY_DIR}/llvm-config)
execute_process(COMMAND "${LLVM_CONFIG}" --system-libs ${LLVM_COMPONENTS} OUTPUT_VARIABLE EXTRA_LIBS)
string(STRIP EXTRA_LIBS "${EXTRA_LIBS}")
string(REPLACE "-l" ";" EXTRA_LIBS "${EXTRA_LIBS}")
string(REPLACE "\n" "" EXTRA_LIBS "${EXTRA_LIBS}")
string(REPLACE " " "" EXTRA_LIBS "${EXTRA_LIBS}")
target_link_libraries(Halide PUBLIC ${EXTRA_LIBS})
endif()
function(add_halide_library HALIDE_LIBRARY_NAME HALIDE_LIBRARY_TYPE)

add_library(${HALIDE_LIBRARY_NAME} ${HALIDE_LIBRARY_TYPE}
${HEADER_FILES}
${SOURCE_FILES}
${INITIAL_MODULES}
)

if (NOT MSVC)
# HalideShared -> libHalide.so (or libHalide.dylib)
# HalideStatic -> libHalide.a
set_target_properties(${HALIDE_LIBRARY_NAME} PROPERTIES OUTPUT_NAME "Halide")
else()
# HalideShared -> Halide.dll and Halide.lib
# HalideStatic -> HalideStatic.lib
if("${HALIDE_LIBRARY_TYPE}" STREQUAL "SHARED")
set_target_properties(${HALIDE_LIBRARY_NAME} PROPERTIES OUTPUT_NAME "Halide")
endif()
endif()

add_dependencies(${HALIDE_LIBRARY_NAME} HalideIncludes)

target_compile_definitions(${HALIDE_LIBRARY_NAME} PRIVATE ${HALIDE_COMPILE_DEFINITIONS} ${LLVM_DEFINITIONS})
target_compile_options(${HALIDE_LIBRARY_NAME} PRIVATE ${HALIDE_COMPILE_OPTIONS})

# Note when PUBLIC or INTERFACE scope is used in target_compile_* then targets
# that link against the Halide library inherit those options and definitions
target_include_directories(${HALIDE_LIBRARY_NAME} PRIVATE ${LLVM_INCLUDE_DIRS})
target_include_directories(${HALIDE_LIBRARY_NAME} INTERFACE "${CMAKE_BINARY_DIR}/include")

# We could expose the /MP flag to all targets, but that might end up saturating the build
# since multiple MSBuild projects might get built in parallel, each of which compiling their
# source files in parallel; the Halide library itself is a "knot" point of the build graph,
# so compiling its files in parallel should not oversubscribe the system
target_compile_options(${HALIDE_LIBRARY_NAME} PUBLIC $<$<CXX_COMPILER_ID:MSVC>:/MP>)

# Get the LLVM libraries we need
llvm_map_components_to_libnames(LIBS ${LLVM_COMPONENTS})

# When building a shared library the LLVM libraries will be
# embedded in the Halide library. When building a static library
# LLVM is not embedded but CMake knows that when building an executable
# against the Halide static library that it needs to link LLVM too so
# PRIVATE scope is the correct choice here.
target_link_libraries(${HALIDE_LIBRARY_NAME} PRIVATE ${LIBS})

if (NOT MSVC)
set(LLVM_CONFIG ${LLVM_TOOLS_BINARY_DIR}/llvm-config)
execute_process(COMMAND "${LLVM_CONFIG}" --system-libs ${LLVM_COMPONENTS} OUTPUT_VARIABLE EXTRA_LIBS)
string(STRIP EXTRA_LIBS "${EXTRA_LIBS}")
string(REPLACE "-l" ";" EXTRA_LIBS "${EXTRA_LIBS}")
string(REPLACE "\n" "" EXTRA_LIBS "${EXTRA_LIBS}")
string(REPLACE " " "" EXTRA_LIBS "${EXTRA_LIBS}")
target_link_libraries(${HALIDE_LIBRARY_NAME} PUBLIC ${EXTRA_LIBS})
endif()

install(TARGETS ${HALIDE_LIBRARY_NAME}
RUNTIME DESTINATION bin
LIBRARY DESTINATION bin
ARCHIVE DESTINATION lib)
endfunction(add_halide_library)

install(TARGETS Halide
RUNTIME DESTINATION bin
LIBRARY DESTINATION bin
ARCHIVE DESTINATION lib)
add_halide_library(HalideShared SHARED)
add_halide_library(HalideStatic STATIC)
1 change: 1 addition & 0 deletions util/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
halide_project(HalideTraceViz "utils" HalideTraceViz.cpp)

halide_project(HalideTraceDump "utils" HalideTraceDump.cpp HalideTraceUtils.cpp)
halide_use_image_io(HalideTraceDump)