Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Backmarge - Enable precompiled headers (#297) (#379) #433

Merged
merged 1 commit into from
May 16, 2023
Merged
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
27 changes: 24 additions & 3 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,27 @@
project( common_clang )
cmake_minimum_required(VERSION 3.4.3)

if(NOT DEFINED BASE_LLVM_VERSION)
set(BASE_LLVM_VERSION 14.0.0)
endif(NOT DEFINED BASE_LLVM_VERSION)
set(OPENCL_CLANG_VERSION ${BASE_LLVM_VERSION}.0)

if(NOT DEFINED OPENCL_CLANG_BUILD_EXTERNAL)
# check if we build inside llvm or not
if(CMAKE_SOURCE_DIR STREQUAL CMAKE_CURRENT_SOURCE_DIR)
set(OPENCL_CLANG_BUILD_EXTERNAL YES)
endif(CMAKE_SOURCE_DIR STREQUAL CMAKE_CURRENT_SOURCE_DIR)
endif(NOT DEFINED OPENCL_CLANG_BUILD_EXTERNAL)

if(OPENCL_CLANG_BUILD_EXTERNAL)
project(OPENCL_CLANG
VERSION
${OPENCL_CLANG_VERSION}
LANGUAGES
CXX
C
)
endif()

# Do not omit TARGET_OBJECTS expression from the SOURCES target
# property
# `cmake --help-policy CMP0051` for details.
Expand All @@ -14,7 +35,7 @@ set(CMAKE_MODULE_PATH

include(CMakeFunctions)

if(LLVM_USE_HOST_TOOLS)
if(LLVM_USE_HOST_TOOLS AND OPENCL_CLANG_BUILD_EXTERNAL)
include(CrossCompile)
llvm_create_cross_target(${PROJECT_NAME} NATIVE "" Release)
endif()
Expand Down Expand Up @@ -60,7 +81,7 @@ include(AddLLVM)
include(TableGen)

if (NOT WIN32)
add_subdirectory( linux_linker bin )
add_subdirectory( linux_linker bin)
endif()

if (CMAKE_SIZEOF_VOID_P EQUAL 4)
Expand Down
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ To build opencl-clang as a standalone project, you need to obtain pre-built LLVM
and SPIR-V Translator libraries. **Note:** currently this kind of build is
supported on Linux only.

If opencl-clang is used as part of another CMake project, you will need to define `OPENCL_CLANG_BUILD_EXTERNAL`.

Integration with pre-built LLVM is done using standard `find_package` way as
documented in [Embedding LLVM in your project](https://llvm.org/docs/CMake.html#embedding-llvm-in-your-project).

Expand Down
110 changes: 108 additions & 2 deletions cl_headers/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
set(CL_HEADERS_LIB cl_headers)
set(CLANG_COMMAND clang)
set(CLANG_COMMAND $<TARGET_FILE:clang> )
if(LLVM_USE_HOST_TOOLS AND NOT OPENCL_CLANG_BUILD_EXTERNAL)
build_native_tool(clang CLANG_COMMAND)
endif()
set(LINUX_RESOURCE_LINKER_COMMAND linux_resource_linker)

function(copy_file SRC DST)
add_custom_command(
Expand All @@ -18,14 +22,81 @@ else(USE_PREBUILT_LLVM)
endif(USE_PREBUILT_LLVM)
copy_file(${OPENCL_HEADERS_DIR}/opencl-c.h opencl-c.h)
copy_file(${OPENCL_HEADERS_DIR}/opencl-c-base.h opencl-c-base.h)
copy_file(${CMAKE_CURRENT_SOURCE_DIR}/module.modulemap module.modulemap)

add_custom_target (
opencl.headers.target
DEPENDS
module.modulemap
opencl-c.h
opencl-c-base.h
)

function(create_pcm DST MODULE HEADER OPTS DEPS)
add_custom_command (
OUTPUT ${DST}
MAIN_DEPENDENCY ${MODMAP}
DEPENDS ${HEADER} ${DEPS} ${CLANG_COMMAND}
COMMAND
${CLANG_COMMAND} -cc1 -x cl
-I. -O0 ${OPTS}
-fmodules -fmodule-name=${MODULE} -fmodule-map-file-home-is-cwd
-emit-module "module.modulemap"
-fno-validate-pch
-o ${DST}
VERBATIM
COMMENT "Generating ${DST}"
)
endfunction(create_pcm)

set(CL12 "-cl-std=CL1.2")
set(CL20 "-cl-std=CL2.0")
set(CL30 "-cl-std=CL3.0")
# Add OpenCL C 3.0 Optional features
set(OPTS30 "-cl-ext=+__opencl_c_3d_image_writes,+__opencl_c_atomic_order_acq_rel,+__opencl_c_atomic_order_seq_cst,+__opencl_c_atomic_scope_device,+__opencl_c_atomic_scope_all_devices,+__opencl_c_device_enqueue,+__opencl_c_generic_address_space,+__opencl_c_images,+__opencl_c_int64,+__opencl_c_pipes,+__opencl_c_program_scope_global_variables,+__opencl_c_read_write_images,+__opencl_c_subgroups,+__opencl_c_work_group_collective_functions")
set(OPTS30_FP64 "-D__opencl_c_fp64=1")

set(SPIR_TRIPLE "-triple;spir-unknown-unknown")
set(SPIR64_TRIPLE "-triple;spir64-unknown-unknown")

if (BUILD_X64)
set(HOST_TRIPLE "${SPIR64_TRIPLE}")
else()
set(HOST_TRIPLE "${SPIR_TRIPLE}")
endif()

set(OPTS -cl-ext=+all,-cl_khr_fp64,-__opencl_c_fp64)
create_pcm(opencl-c-12-spir.pcm cl12spir opencl-c-base.h "${SPIR_TRIPLE};${CL12};${OPTS}" "${DEPS}")
create_pcm(opencl-c-20-spir.pcm cl20spir opencl-c-base.h "${SPIR_TRIPLE};${CL20};${OPTS}" "${DEPS}")
create_pcm(opencl-c-30-spir.pcm cl30spir opencl-c-base.h "${SPIR_TRIPLE};${CL30};${OPTS};${OPTS30}" "${DEPS}")
create_pcm(opencl-c-12-spir64.pcm cl12spir64 opencl-c-base.h "${SPIR64_TRIPLE};${CL12};${OPTS}" "${DEPS}")
create_pcm(opencl-c-20-spir64.pcm cl20spir64 opencl-c-base.h "${SPIR64_TRIPLE};${CL20};${OPTS}" "${DEPS}")
create_pcm(opencl-c-30-spir64.pcm cl30spir64 opencl-c-base.h "${SPIR64_TRIPLE};${CL30};${OPTS};${OPTS30}" "${DEPS}")
set(OPTS -cl-ext=+all)
create_pcm(opencl-c-12-spir-fp64.pcm cl12spirfp64 opencl-c-base.h "${SPIR_TRIPLE};${CL12};${OPTS}" "${DEPS}")
create_pcm(opencl-c-20-spir-fp64.pcm cl20spirfp64 opencl-c-base.h "${SPIR_TRIPLE};${CL20};${OPTS}" "${DEPS}")
create_pcm(opencl-c-30-spir-fp64.pcm cl30spirfp64 opencl-c-base.h "${SPIR_TRIPLE};${CL30};${OPTS};${OPTS30};${OPTS30_FP64}" "${DEPS}")
create_pcm(opencl-c-12-spir64-fp64.pcm cl12spir64fp64 opencl-c-base.h "${SPIR64_TRIPLE};${CL12};${OPTS}" "${DEPS}")
create_pcm(opencl-c-20-spir64-fp64.pcm cl20spir64fp64 opencl-c-base.h "${SPIR64_TRIPLE};${CL20};${OPTS}" "${DEPS}")
create_pcm(opencl-c-30-spir64-fp64.pcm cl30spir64fp64 opencl-c-base.h "${SPIR64_TRIPLE};${CL30};${OPTS};${OPTS30};${OPTS30_FP64}" "${DEPS}")

add_custom_target (
opencl.pcm.target
DEPENDS
opencl.headers.target
opencl-c-12-spir.pcm
opencl-c-20-spir.pcm
opencl-c-30-spir.pcm
opencl-c-12-spir64.pcm
opencl-c-20-spir64.pcm
opencl-c-30-spir64.pcm
opencl-c-12-spir-fp64.pcm
opencl-c-20-spir-fp64.pcm
opencl-c-30-spir-fp64.pcm
opencl-c-12-spir64-fp64.pcm
opencl-c-20-spir64-fp64.pcm
opencl-c-30-spir64-fp64.pcm
)

function(pack_to_obj SRC DST TAG)
add_custom_command (
Expand All @@ -44,11 +115,46 @@ else()
list(APPEND CL_HEADERS_SRC
opencl-c.h.cpp
opencl-c-base.h.cpp
opencl-c-12-spir.mod.cpp
opencl-c-20-spir.mod.cpp
opencl-c-30-spir.mod.cpp
opencl-c-12-spir64.mod.cpp
opencl-c-20-spir64.mod.cpp
opencl-c-30-spir64.mod.cpp
opencl-c-12-spir-fp64.mod.cpp
opencl-c-20-spir-fp64.mod.cpp
opencl-c-30-spir-fp64.mod.cpp
opencl-c-12-spir64-fp64.mod.cpp
opencl-c-20-spir64-fp64.mod.cpp
opencl-c-30-spir64-fp64.mod.cpp
module.modulemap.cpp
)
# note the .pcm -> .mod extension change
# this is a workaround for CMake bug that caused
# dependency cycle in generated build rules
pack_to_obj(opencl-c-12-spir.pcm opencl-c-12-spir.mod.cpp "PCM_OPENCL_C_12_SPIR_PCM")
pack_to_obj(opencl-c-20-spir.pcm opencl-c-20-spir.mod.cpp "PCM_OPENCL_C_20_SPIR_PCM")
pack_to_obj(opencl-c-30-spir.pcm opencl-c-30-spir.mod.cpp "PCM_OPENCL_C_30_SPIR_PCM")
pack_to_obj(opencl-c-12-spir64.pcm opencl-c-12-spir64.mod.cpp "PCM_OPENCL_C_12_SPIR64_PCM")
pack_to_obj(opencl-c-20-spir64.pcm opencl-c-20-spir64.mod.cpp "PCM_OPENCL_C_20_SPIR64_PCM")
pack_to_obj(opencl-c-30-spir64.pcm opencl-c-30-spir64.mod.cpp "PCM_OPENCL_C_30_SPIR64_PCM")
pack_to_obj(opencl-c-12-spir-fp64.pcm opencl-c-12-spir-fp64.mod.cpp "PCM_OPENCL_C_12_SPIR_FP64_PCM")
pack_to_obj(opencl-c-20-spir-fp64.pcm opencl-c-20-spir-fp64.mod.cpp "PCM_OPENCL_C_20_SPIR_FP64_PCM")
pack_to_obj(opencl-c-30-spir-fp64.pcm opencl-c-30-spir-fp64.mod.cpp "PCM_OPENCL_C_30_SPIR_FP64_PCM")
pack_to_obj(opencl-c-12-spir64-fp64.pcm opencl-c-12-spir64-fp64.mod.cpp "PCM_OPENCL_C_12_SPIR64_FP64_PCM")
pack_to_obj(opencl-c-20-spir64-fp64.pcm opencl-c-20-spir64-fp64.mod.cpp "PCM_OPENCL_C_20_SPIR64_FP64_PCM")
pack_to_obj(opencl-c-30-spir64-fp64.pcm opencl-c-30-spir64-fp64.mod.cpp "PCM_OPENCL_C_30_SPIR64_FP64_PCM")
pack_to_obj(module.modulemap module.modulemap.cpp "PCM_OPENCL_C_MODULE_MAP")
endif()

add_library(${CL_HEADERS_LIB} OBJECT
${CL_HEADERS_SRC}
)

add_dependencies(${CL_HEADERS_LIB} opencl.headers.target)
add_dependencies(${CL_HEADERS_LIB} opencl.pcm.target)
install(FILES
${CMAKE_CURRENT_BINARY_DIR}/opencl-c.h
${CMAKE_CURRENT_BINARY_DIR}/opencl-c-base.h
${CMAKE_CURRENT_BINARY_DIR}/module.modulemap
DESTINATION include/cclang
)
17 changes: 15 additions & 2 deletions cl_headers/OpenCL.rc
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,18 @@ END
// Module with OpenCL C declarations with corresponding headers
//

OPENCL_C_H PCM "opencl-c.h"
OPENCL_C_BASE_H PCM "opencl-c-base.h"
OPENCL_C_H PCM "opencl-c.h"
OPENCL_C_BASE_H PCM "opencl-c-base.h"
OPENCL_C_12_SPIR_PCM PCM "opencl-c-12-spir.pcm"
OPENCL_C_20_SPIR_PCM PCM "opencl-c-20-spir.pcm"
OPENCL_C_30_SPIR_PCM PCM "opencl-c-30-spir.pcm"
OPENCL_C_12_SPIR64_PCM PCM "opencl-c-12-spir64.pcm"
OPENCL_C_20_SPIR64_PCM PCM "opencl-c-20-spir64.pcm"
OPENCL_C_30_SPIR64_PCM PCM "opencl-c-30-spir64.pcm"
OPENCL_C_12_SPIR_FP64_PCM PCM "opencl-c-12-spir-fp64.pcm"
OPENCL_C_20_SPIR_FP64_PCM PCM "opencl-c-20-spir-fp64.pcm"
OPENCL_C_30_SPIR_FP64_PCM PCM "opencl-c-30-spir-fp64.pcm"
OPENCL_C_12_SPIR64_FP64_PCM PCM "opencl-c-12-spir64-fp64.pcm"
OPENCL_C_20_SPIR64_FP64_PCM PCM "opencl-c-20-spir64-fp64.pcm"
OPENCL_C_30_SPIR64_FP64_PCM PCM "opencl-c-30-spir64-fp64.pcm"
OPENCL_C_MODULE_MAP PCM "module.modulemap"
60 changes: 60 additions & 0 deletions cl_headers/module.modulemap
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
module cl12spir {
header "opencl-c.h"
header "opencl-c-base.h"
export *
}
module cl20spir {
header "opencl-c.h"
header "opencl-c-base.h"
export *
}
module cl30spir {
header "opencl-c.h"
header "opencl-c-base.h"
export *
}
module cl12spir64 {
header "opencl-c.h"
header "opencl-c-base.h"
export *
}
module cl20spir64 {
header "opencl-c.h"
header "opencl-c-base.h"
export *
}
module cl30spir64 {
header "opencl-c.h"
header "opencl-c-base.h"
export *
}
module cl12spirfp64 {
header "opencl-c.h"
header "opencl-c-base.h"
export *
}
module cl20spirfp64 {
header "opencl-c.h"
header "opencl-c-base.h"
export *
}
module cl30spirfp64 {
header "opencl-c.h"
header "opencl-c-base.h"
export *
}
module cl12spir64fp64 {
header "opencl-c.h"
header "opencl-c-base.h"
export *
}
module cl20spir64fp64 {
header "opencl-c.h"
header "opencl-c-base.h"
export *
}
module cl30spir64fp64 {
header "opencl-c.h"
header "opencl-c-base.h"
export *
}
17 changes: 15 additions & 2 deletions cl_headers/resource.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,20 @@ Copyright (c) Intel Corporation (2009-2017).
#ifndef __RESOURCE__
#define __RESOURCE__

#define OPENCL_C_H "OPENCL_C_H"
#define OPENCL_C_BASE_H "OPENCL_C_BASE_H"
#define OPENCL_C_H "OPENCL_C_H"
#define OPENCL_C_BASE_H "OPENCL_C_BASE_H"
#define OPENCL_C_12_SPIR_PCM "OPENCL_C_12_SPIR_PCM"
#define OPENCL_C_20_SPIR_PCM "OPENCL_C_20_SPIR_PCM"
#define OPENCL_C_30_SPIR_PCM "OPENCL_C_30_SPIR_PCM"
#define OPENCL_C_12_SPIR64_PCM "OPENCL_C_12_SPIR64_PCM"
#define OPENCL_C_20_SPIR64_PCM "OPENCL_C_20_SPIR64_PCM"
#define OPENCL_C_30_SPIR64_PCM "OPENCL_C_30_SPIR64_PCM"
#define OPENCL_C_12_SPIR_FP64_PCM "OPENCL_C_12_SPIR_FP64_PCM"
#define OPENCL_C_20_SPIR_FP64_PCM "OPENCL_C_20_SPIR_FP64_PCM"
#define OPENCL_C_30_SPIR_FP64_PCM "OPENCL_C_30_SPIR_FP64_PCM"
#define OPENCL_C_12_SPIR64_FP64_PCM "OPENCL_C_12_SPIR64_FP64_PCM"
#define OPENCL_C_20_SPIR64_FP64_PCM "OPENCL_C_20_SPIR64_FP64_PCM"
#define OPENCL_C_30_SPIR64_FP64_PCM "OPENCL_C_30_SPIR64_FP64_PCM"
#define OPENCL_C_MODULE_MAP "OPENCL_C_MODULE_MAP"

#endif /* __RESOURCE__ */
25 changes: 19 additions & 6 deletions common_clang.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -113,17 +113,30 @@ void CommonClangInitialize() {
}

static bool GetHeaders(std::vector<Resource> &Result) {
struct {const char *ID; const char *Name;} Headers[] = {
{OPENCL_C_H, "opencl-c.h"},
{OPENCL_C_BASE_H, "opencl-c-base.h"},
};
struct {
const char *ID;
const char *Name;
} Headers[] = {{OPENCL_C_H, "opencl-c.h"},
{OPENCL_C_BASE_H, "opencl-c-base.h"},
{OPENCL_C_12_SPIR_PCM, "opencl-c-12-spir.pcm"},
{OPENCL_C_20_SPIR_PCM, "opencl-c-20-spir.pcm"},
{OPENCL_C_30_SPIR_PCM, "opencl-c-30-spir.pcm"},
{OPENCL_C_12_SPIR64_PCM, "opencl-c-12-spir64.pcm"},
{OPENCL_C_20_SPIR64_PCM, "opencl-c-20-spir64.pcm"},
{OPENCL_C_30_SPIR64_PCM, "opencl-c-30-spir64.pcm"},
{OPENCL_C_12_SPIR_FP64_PCM, "opencl-c-12-spir-fp64.pcm"},
{OPENCL_C_20_SPIR_FP64_PCM, "opencl-c-20-spir-fp64.pcm"},
{OPENCL_C_30_SPIR_FP64_PCM, "opencl-c-30-spir-fp64.pcm"},
{OPENCL_C_12_SPIR64_FP64_PCM, "opencl-c-12-spir64-fp64.pcm"},
{OPENCL_C_20_SPIR64_FP64_PCM, "opencl-c-20-spir64-fp64.pcm"},
{OPENCL_C_30_SPIR64_FP64_PCM, "opencl-c-30-spir64-fp64.pcm"},
{OPENCL_C_MODULE_MAP, "module.modulemap"}};

Result.clear();
Result.reserve(sizeof(Headers) / sizeof(*Headers));

ResourceManager &RM = ResourceManager::instance();

for (auto Header:Headers) {
for (auto Header : Headers) {
Resource R = RM.get_resource(Header.Name, Header.ID, "PCM", true);
if (!R) {
assert(0 && "Resource not found");
Expand Down
13 changes: 13 additions & 0 deletions common_clang.map
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,19 @@ global:
GetKernelArgInfo;
PCM_OPENCL_C_H*;
PCM_OPENCL_C_BASE_H*;
PCM_OPENCL_C_12_SPIR_PCM*;
PCM_OPENCL_C_20_SPIR_PCM*;
PCM_OPENCL_C_30_SPIR_PCM*;
PCM_OPENCL_C_12_SPIR64_PCM*;
PCM_OPENCL_C_20_SPIR64_PCM*;
PCM_OPENCL_C_30_SPIR64_PCM*;
PCM_OPENCL_C_12_SPIR_FP64_PCM*;
PCM_OPENCL_C_20_SPIR_FP64_PCM*;
PCM_OPENCL_C_30_SPIR_FP64_PCM*;
PCM_OPENCL_C_12_SPIR64_FP64_PCM*;
PCM_OPENCL_C_20_SPIR64_FP64_PCM*;
PCM_OPENCL_C_30_SPIR64_FP64_PCM*;
PCM_OPENCL_C_MODULE_MAP*;
};
local: *;
};
Loading