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

Enable precompiled headers #122

Open
wants to merge 1 commit into
base: ocl-open-80
Choose a base branch
from
Open
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
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@ add_public_tablegen_target(CClangCompileOptions)
# Source code
#
set(TARGET_INCLUDE_FILES
exceptions.h
common_clang.h
options.h
binary_result.h
Expand Down
63 changes: 63 additions & 0 deletions cl_headers/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,57 @@ else(USE_PREBUILT_LLVM)
set(OPENCL_HEADERS_DIR "${CLANG_SOURCE_DIR}/lib/Headers")
endif(USE_PREBUILT_LLVM)
copy_file(${OPENCL_HEADERS_DIR}/opencl-c.h opencl-c.h)
copy_file(${CMAKE_CURRENT_SOURCE_DIR}/module.modulemap module.modulemap)

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

function(create_pcm DST MODULE HEADER OPTS DEPS)
add_custom_command (
OUTPUT ${DST}
MAIN_DEPENDENCY ${MODMAP}
DEPENDS ${HEADER} ${DEPS}
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(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()

create_pcm(opencl-c-12-spir.pcm cl12spir opencl-c.h "${SPIR_TRIPLE};${CL12};${OPTS}" "${DEPS}")
create_pcm(opencl-c-20-spir.pcm cl20spir opencl-c.h "${SPIR_TRIPLE};${CL20};${OPTS}" "${DEPS}")
create_pcm(opencl-c-12-spir64.pcm cl12spir64 opencl-c.h "${SPIR64_TRIPLE};${CL12};${OPTS}" "${DEPS}")
create_pcm(opencl-c-20-spir64.pcm cl20spir64 opencl-c.h "${SPIR64_TRIPLE};${CL20};${OPTS}" "${DEPS}")

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

function(pack_to_obj SRC DST TAG)
add_custom_command (
Expand All @@ -41,11 +85,30 @@ else()
pack_to_obj(opencl-c.h opencl-c.h.cpp "PCM_OPENCL_C_H")
list(APPEND CL_HEADERS_SRC
opencl-c.h.cpp

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder why opencl-c-base.h is not here: it was added last year in llvm.org.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

On release-80 branch, clang doesn't have opencl-c-base.h. In the pull request for ocl-open-90, 100, master branch, there is opencl-c-base.h.

opencl-c-12-spir.mod.cpp
opencl-c-20-spir.mod.cpp
opencl-c-12-spir64.mod.cpp
opencl-c-20-spir64.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-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(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)
install(FILES
${CMAKE_CURRENT_BINARY_DIR}/opencl-c.h
${CMAKE_CURRENT_BINARY_DIR}/module.modulemap
DESTINATION include/cclang
)
7 changes: 7 additions & 0 deletions cl_headers/OpenCL.rc
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,10 @@ END
//

OPENCL_C_H PCM "opencl-c.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_12_SPIR64_PCM PCM "opencl-c-12-spir64.pcm"
OPENCL_C_20_SPIR64_PCM PCM "opencl-c-20-spir64.pcm"

OPENCL_C_MODULE_MAP PCM "module.modulemap"
16 changes: 16 additions & 0 deletions cl_headers/module.modulemap
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
module cl12spir {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we re-use the existing module.modulemap from clang/lib/Headers?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Technically yes, but we have the motivation to split out the common built-ins which doesn't relate to cl12/cl20 version, to reduce the duplication in generated pcm. So it's better we have a customized modulemap in our own repo.

header "opencl-c.h"
export *
}
module cl20spir {
header "opencl-c.h"
export *
}
module cl12spir64 {
header "opencl-c.h"
export *
}
module cl20spir64 {
header "opencl-c.h"
export *
}
16 changes: 16 additions & 0 deletions cl_headers/module.mudulemap
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
module cl12spir {
header "opencl-c.h"
export *
}
module cl20spir {
header "opencl-c.h"
export *
}
module cl12spir64 {
header "opencl-c.h"
export *
}
module cl20spir64 {
header "opencl-c.h"
export *
}
5 changes: 5 additions & 0 deletions cl_headers/resource.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,10 @@ Copyright (c) Intel Corporation (2009-2017).
#define __RESOURCE__

#define OPENCL_C_H "OPENCL_C_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_12_SPIR64_PCM "OPENCL_C_12_SPIR64_PCM"
#define OPENCL_C_20_SPIR64_PCM "OPENCL_C_20_SPIR64_PCM"
#define OPENCL_C_MODULE_MAP "OPENCL_C_MODULE_MAP"

#endif /* __RESOURCE__ */
12 changes: 12 additions & 0 deletions common_clang.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ Copyright (c) Intel Corporation (2009-2017).
#include "common_clang.h"
#include "pch_mgr.h"
#include "cl_headers/resource.h"
#include "exceptions.h"
#include "binary_result.h"
#include "options.h"

Expand Down Expand Up @@ -115,6 +116,11 @@ 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_12_SPIR_PCM, "opencl-c-12-spir.pcm"},
{OPENCL_C_20_SPIR_PCM, "opencl-c-20-spir.pcm"},
{OPENCL_C_12_SPIR64_PCM, "opencl-c-12-spir64.pcm"},
{OPENCL_C_20_SPIR64_PCM, "opencl-c-20-spir64.pcm"},
{OPENCL_C_MODULE_MAP, "module.modulemap"}
};

Result.clear();
Expand Down Expand Up @@ -337,5 +343,11 @@ Compile(const char *pszProgramSource, const char **pInputHeaders,
*pBinaryResult = NULL;
}
return CL_OUT_OF_HOST_MEMORY;
} catch (pch_error &) {
if (pBinaryResult) {
*pBinaryResult = NULL;
}
assert(false && " Common_clang. Something wrong with PCHs.");
return CL_COMPILE_PROGRAM_FAILURE;
}
}
5 changes: 5 additions & 0 deletions common_clang.map
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@ global:
Link;
GetKernelArgInfo;
PCM_OPENCL_C_H*;
PCM_OPENCL_C_12_SPIR_PCM*;
PCM_OPENCL_C_20_SPIR_PCM*;
PCM_OPENCL_C_12_SPIR64_PCM*;
PCM_OPENCL_C_20_SPIR64_PCM*;
PCM_OPENCL_C_MODULE_MAP*;
};
local: *;
};
25 changes: 25 additions & 0 deletions exceptions.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/*****************************************************************************\
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure we need this custom exception class, because we never throw it, do we?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We catch this in common_clang.cpp:346 as std::domain_error, do you mean this kind of exception will never be hit?

Copyright (c) Intel Corporation (2009-2020).
INTEL MAKES NO WARRANTY OF ANY KIND REGARDING THE CODE. THIS CODE IS
LICENSED ON AN "AS IS" BASIS AND INTEL WILL NOT PROVIDE ANY SUPPORT,
ASSISTANCE, INSTALLATION, TRAINING OR OTHER SERVICES. INTEL DOES NOT
PROVIDE ANY UPDATES, ENHANCEMENTS OR EXTENSIONS. INTEL SPECIFICALLY
DISCLAIMS ANY WARRANTY OF MERCHANTABILITY, NONINFRINGEMENT, FITNESS FOR ANY
PARTICULAR PURPOSE, OR ANY OTHER WARRANTY. Intel disclaims all liability,
including liability for infringement of any proprietary rights, relating to
use of the code. No license, express or implied, by estoppel or otherwise,
to any intellectual property rights is granted herein.
\file exceptions.h
\*****************************************************************************/

#pragma once
#include <stdexcept>

/// macro for convenient definition of common clang exceptions
#define DEFINE_COMMON_CLANG_EXCEPTION(__name) \
class __name : public std::domain_error { \
public: \
__name(const std::string &str) : std::domain_error(str) {} \
};

DEFINE_COMMON_CLANG_EXCEPTION(pch_error)
15 changes: 15 additions & 0 deletions options_compile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,21 @@ std::string EffectiveOptionsFilter::processOptions(const OpenCLArgList &args,

effectiveArgs.push_back(szTriple);

effectiveArgs.push_back("-fmodules");
if (szTriple.find("spir64") != szTriple.npos) {
if (iCLStdSet <= 120) {
effectiveArgs.push_back("-fmodule-file=opencl-c-12-spir64.pcm");
} else {
effectiveArgs.push_back("-fmodule-file=opencl-c-20-spir64.pcm");
}
} else if (szTriple.find("spir") != szTriple.npos) {
if (iCLStdSet <= 120) {
effectiveArgs.push_back("-fmodule-file=opencl-c-12-spir.pcm");
} else {
effectiveArgs.push_back("-fmodule-file=opencl-c-20-spir.pcm");
}
}

effectiveArgs.push_back("-include");
effectiveArgs.push_back("opencl-c.h");

Expand Down
1 change: 1 addition & 0 deletions pch_mgr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ Copyright (c) Intel Corporation (2009-2017).
\*****************************************************************************/

#include "pch_mgr.h"
#include "exceptions.h"

#include "llvm/Object/ELF.h"
#include "llvm/ADT/Twine.h"
Expand Down