Skip to content

Commit

Permalink
Enable precompiled headers
Browse files Browse the repository at this point in the history
  • Loading branch information
woshiyifei committed May 28, 2020
1 parent d4ce529 commit 2638c80
Show file tree
Hide file tree
Showing 7 changed files with 203 additions and 2 deletions.
83 changes: 83 additions & 0 deletions cl_headers/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,68 @@ 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}
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()

set(OPTS -cl-ext=-cl_khr_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-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}")
set(OPTS -cl-ext=+cl_khr_fp64)
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-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}")

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
opencl-c-12-spir-fp64.pcm
opencl-c-20-spir-fp64.pcm
opencl-c-12-spir64-fp64.pcm
opencl-c-20-spir64-fp64.pcm
)

function(pack_to_obj SRC DST TAG)
add_custom_command (
Expand All @@ -40,16 +94,45 @@ endfunction(pack_to_obj)
if(WIN32)
list(APPEND CL_HEADERS_SRC OpenCL.rc)
else()

pack_to_obj(opencl-c.h opencl-c.h.cpp "PCM_OPENCL_C_H")
pack_to_obj(opencl-c-base.h opencl-c-base.h.cpp "PCM_OPENCL_C_BASE_H")
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-12-spir64.mod.cpp
opencl-c-20-spir64.mod.cpp
opencl-c-12-spir-fp64.mod.cpp
opencl-c-20-spir-fp64.mod.cpp
opencl-c-12-spir64-fp64.mod.cpp
opencl-c-20-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-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-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-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(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}/opencl-c-base.h
${CMAKE_CURRENT_BINARY_DIR}/module.modulemap
DESTINATION include/cclang
)
13 changes: 12 additions & 1 deletion cl_headers/OpenCL.rc
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,15 @@ END
//

OPENCL_C_H PCM "opencl-c.h"
OPENCL_C_BASE_H PCM "opencl-c-base.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_12_SPIR64_PCM PCM "opencl-c-12-spir64.pcm"
OPENCL_C_20_SPIR64_PCM PCM "opencl-c-20-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_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_MODULE_MAP PCM "module.modulemap"
40 changes: 40 additions & 0 deletions cl_headers/module.modulemap
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
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 cl12spir64 {
header "opencl-c.h"
header "opencl-c-base.h"
export *
}
module cl20spir64 {
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 cl12spir64fp64 {
header "opencl-c.h"
header "opencl-c-base.h"
export *
}
module cl20spir64fp64 {
header "opencl-c.h"
header "opencl-c-base.h"
export *
}
10 changes: 10 additions & 0 deletions cl_headers/resource.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,14 @@ Copyright (c) Intel Corporation (2009-2017).
#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_12_SPIR64_PCM "OPENCL_C_12_SPIR64_PCM"
#define OPENCL_C_20_SPIR64_PCM "OPENCL_C_20_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_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_MODULE_MAP "OPENCL_C_MODULE_MAP"

#endif /* __RESOURCE__ */
9 changes: 9 additions & 0 deletions common_clang.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,15 @@ 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"},
{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_12_SPIR_FP64_PCM, "opencl-c-12-spir-fp64.pcm"},
{OPENCL_C_20_SPIR_FP64_PCM, "opencl-c-20-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_MODULE_MAP, "module.modulemap"}
};

Result.clear();
Expand Down
9 changes: 9 additions & 0 deletions common_clang.map
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,15 @@ 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_12_SPIR64_PCM*;
PCM_OPENCL_C_20_SPIR64_PCM*;
PCM_OPENCL_C_12_SPIR_FP64_PCM*;
PCM_OPENCL_C_20_SPIR_FP64_PCM*;
PCM_OPENCL_C_12_SPIR64_FP64_PCM*;
PCM_OPENCL_C_20_SPIR64_FP64_PCM*;
PCM_OPENCL_C_MODULE_MAP*;
};
local: *;
};
41 changes: 40 additions & 1 deletion options_compile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ std::string EffectiveOptionsFilter::processOptions(const OpenCLArgList &args,
ArgsVector &effectiveArgs) {
// Reset args
int iCLStdSet = 0;
int fp64Enable = 0;
std::string szTriple;
std::string sourceName(llvm::Twine(s_progID++).str());

Expand Down Expand Up @@ -191,7 +192,6 @@ std::string EffectiveOptionsFilter::processOptions(const OpenCLArgList &args,
}

effectiveArgs.push_back(szTriple);

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

Expand Down Expand Up @@ -220,6 +220,45 @@ std::string EffectiveOptionsFilter::processOptions(const OpenCLArgList &args,
std::back_insert_iterator<ArgsVector> it(std::back_inserter(effectiveArgs));
quoted_tokenize(it, pszOptionsEx, " \t", '"', '\x00');

for (ArgsVector::iterator it = effectiveArgs.begin(),
end = effectiveArgs.end();
it != end; ++it) {
if (it->compare("-Dcl_khr_fp64") == 0) {
fp64Enable = true;
}
}
effectiveArgs.push_back("-fmodules");
if (fp64Enable == 0) {
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");
}
}
}
else if (fp64Enable == 1) {
if (szTriple.find("spir64") != szTriple.npos) {
if (iCLStdSet <= 120) {
effectiveArgs.push_back("-fmodule-file=opencl-c-12-spir64-fp64.pcm");
} else {
effectiveArgs.push_back("-fmodule-file=opencl-c-20-spir64-fp64.pcm");
}
} else if (szTriple.find("spir") != szTriple.npos) {
if (iCLStdSet <= 120) {
effectiveArgs.push_back("-fmodule-file=opencl-c-12-spir-fp64.pcm");
} else {
effectiveArgs.push_back("-fmodule-file=opencl-c-20-spir-fp64.pcm");
}
}
}

// add source name to options as an input file
assert(!sourceName.empty() && "Empty source name.");
effectiveArgs.push_back(sourceName);
Expand Down

0 comments on commit 2638c80

Please sign in to comment.