Skip to content

Commit 0a0686f

Browse files
authored
[Comgr][OpenCL] Replace opencl-c.pch in favour of opencl-c-base.h + -fdeclare-opencl-builtins (#111)
2 parents a0426f8 + b754bb1 commit 0a0686f

13 files changed

+106
-153
lines changed

amd/comgr/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -229,7 +229,7 @@ endif()
229229
list(APPEND AMD_COMGR_PRIVATE_COMPILE_DEFINITIONS AMD_COMGR_EXPORT)
230230

231231
include(bc2h)
232-
include(opencl_pch)
232+
include(opencl_header)
233233
include(DeviceLibs)
234234

235235
# Add major version to the name on windows, including Win64

amd/comgr/cmake/DeviceLibs.cmake

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -65,20 +65,17 @@ endforeach()
6565
list(JOIN TARGETS_INCLUDES "\n" TARGETS_INCLUDES)
6666
file(GENERATE OUTPUT ${GEN_LIBRARY_INC_FILE} CONTENT "${TARGETS_INCLUDES}")
6767

68-
foreach(OPENCL_VERSION 1.2 2.0)
69-
string(REPLACE . _ OPENCL_UNDERSCORE_VERSION ${OPENCL_VERSION})
70-
add_custom_command(OUTPUT ${INC_DIR}/opencl${OPENCL_VERSION}-c.inc
71-
COMMAND bc2h ${CMAKE_CURRENT_BINARY_DIR}/opencl${OPENCL_VERSION}-c.pch
72-
${INC_DIR}/opencl${OPENCL_VERSION}-c.inc
73-
opencl${OPENCL_UNDERSCORE_VERSION}_c
74-
DEPENDS bc2h ${CMAKE_CURRENT_BINARY_DIR}/opencl${OPENCL_VERSION}-c.pch
75-
COMMENT "Generating opencl${OPENCL_VERSION}-c.inc"
76-
)
77-
set_property(DIRECTORY APPEND PROPERTY
78-
ADDITIONAL_MAKE_CLEAN_FILES ${INC_DIR}/opencl${OPENCL_VERSION}-c.inc)
79-
add_custom_target(opencl${OPENCL_VERSION}-c.inc_target DEPENDS ${INC_DIR}/opencl${OPENCL_VERSION}-c.inc)
80-
add_dependencies(amd_comgr opencl${OPENCL_VERSION}-c.inc_target)
81-
endforeach()
68+
add_custom_command(OUTPUT ${INC_DIR}/opencl-c-base.inc
69+
COMMAND bc2h ${OPENCL_C_H}
70+
${INC_DIR}/opencl-c-base.inc
71+
opencl_c_base
72+
DEPENDS bc2h clang ${OPENCL_C_H}
73+
COMMENT "Generating opencl-c-base.inc"
74+
)
75+
set_property(DIRECTORY APPEND PROPERTY
76+
ADDITIONAL_MAKE_CLEAN_FILES ${INC_DIR}/opencl-c-base.inc)
77+
add_custom_target(opencl-c-base.inc_target DEPENDS ${INC_DIR}/opencl-c-base.inc)
78+
add_dependencies(amd_comgr opencl-c-base.inc_target)
8279

8380
set(TARGETS_DEFS "")
8481
list(APPEND TARGETS_DEFS "#ifndef AMD_DEVICE_LIBS_TARGET\n#define AMD_DEVICE_LIBS_TARGET(t)\n#endif")

amd/comgr/cmake/opencl_header.cmake

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
if(CMAKE_SOURCE_DIR STREQUAL CMAKE_CURRENT_SOURCE_DIR)
2+
find_package(Clang REQUIRED CONFIG)
3+
4+
# FIXME: CLANG_CMAKE_DIR seems like the most stable way to find this, but
5+
# really there is no way to reliably discover this header.
6+
#
7+
# We effectively back up to the Clang output directory (for the case of a build
8+
# tree) or install prefix (for the case of an installed copy), and then search
9+
# for a file named opencl-c-base.h anywhere below that. We take the first result in
10+
# the case where there are multiple (e.g. if there is an installed copy nested
11+
# in a build directory). This is a bit imprecise, but it covers cases like MSVC
12+
# adding some additional configuration-specific subdirectories to the build
13+
# tree but not to an installed copy.
14+
file(GLOB_RECURSE OPENCL_C_H_LIST "${CLANG_CMAKE_DIR}/../../../*/opencl-c-base.h")
15+
16+
list(GET OPENCL_C_H_LIST 0 OPENCL_C_H)
17+
18+
if (NOT EXISTS "${OPENCL_C_H}" OR IS_DIRECTORY "${OPENCL_C_H}")
19+
message(FATAL_ERROR "Unable to locate opencl-c-base.h from the supplied Clang. The path '${CLANG_CMAKE_DIR}/../../../*' was searched.")
20+
endif()
21+
else()
22+
get_target_property(clang_build_header_dir clang-resource-headers RUNTIME_OUTPUT_DIRECTORY)
23+
set(OPENCL_C_H "${clang_build_header_dir}/opencl-c-base.h")
24+
endif()

amd/comgr/cmake/opencl_pch.cmake

Lines changed: 0 additions & 52 deletions
This file was deleted.

amd/comgr/include/amd_comgr.h.in

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1612,17 +1612,18 @@ typedef enum amd_comgr_action_kind_s {
16121612
*/
16131613
AMD_COMGR_ACTION_SOURCE_TO_PREPROCESSOR = 0x0,
16141614
/**
1615-
* Copy all existing data objects in @p input to @p output, then add the
1616-
* device-specific and language-specific precompiled headers required for
1617-
* compilation.
1615+
* Copy all existing data objects in @p input to @p output.
16181616
*
1619-
* Currently the only supported languages are @p AMD_COMGR_LANGUAGE_OPENCL_1_2
1620-
* and @p AMD_COMGR_LANGUAGE_OPENCL_2_0.
1617+
* Currently the action is a no-op, as the OpenCL pre-compiled headers
1618+
* are no longer used.
16211619
*
1622-
* Return @p AMD_COMGR_STATUS_ERROR_INVALID_ARGUMENT if isa name or language
1623-
* is not set in @p info, or the language is not supported.
1620+
* Return @p AMD_COMGR_STATUS_ERROR_INVALID_ARGUMENT if any of the
1621+
* input or output are not initialized.
16241622
*/
1625-
AMD_COMGR_ACTION_ADD_PRECOMPILED_HEADERS = 0x1,
1623+
AMD_COMGR_ACTION_ADD_PRECOMPILED_HEADERS
1624+
AMD_COMGR_DEPRECATED("Will be removed in Comgr v4.0. Currently the action\
1625+
is a no-op, as the OpenCL pre-compiled headers are no longer used.")
1626+
= 0x1,
16261627
/**
16271628
* Compile each source data object in @p input in order. For each
16281629
* successful compilation add a bc data object to @p result. Resolve

amd/comgr/src/comgr-clang-command.cpp

Lines changed: 4 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -54,9 +54,11 @@ bool skipProblematicFlag(IteratorTy &It, const IteratorTy &End) {
5454
// Skip include paths, these should have been handled by preprocessing the
5555
// source first. Sadly, these are passed also to the middle-end commands. Skip
5656
// debug related flags (they should be ignored) like -dumpdir (used for
57-
// profiling/coverage/split-dwarf)
57+
// profiling/coverage/split-dwarf).
58+
// Skip flags related to opencl-c headers or device-libs builtins.
5859
StringRef Arg = *It;
59-
static const StringSet<> FlagsWithPathArg = {"-I", "-dumpdir"};
60+
static const StringSet<> FlagsWithPathArg = {"-I", "-dumpdir", "-include",
61+
"-mlink-builtin-bitcode"};
6062
bool IsFlagWithPathArg = It + 1 != End && FlagsWithPathArg.contains(Arg);
6163
if (IsFlagWithPathArg) {
6264
++It;
@@ -119,19 +121,6 @@ void ClangCommand::addOptionsIdentifier(HashAlgorithm &H) const {
119121
continue;
120122

121123
StringRef Arg = *It;
122-
static const StringSet<> FlagsWithFileArgEmbededInComgr = {
123-
"-include-pch", "-mlink-builtin-bitcode"};
124-
if (FlagsWithFileArgEmbededInComgr.contains(Arg)) {
125-
// The next argument is a path to a "secondary" input-file (pre-compiled
126-
// header or device-libs builtin)
127-
// These two files kinds of files are embedded in comgr at compile time,
128-
// and in normally their remain constant with comgr's build. The user is
129-
// not able to change them.
130-
++It;
131-
if (It == End)
132-
break;
133-
continue;
134-
}
135124

136125
// input files are considered by their content
137126
// output files should not be considered at all

amd/comgr/src/comgr-compiler.cpp

Lines changed: 39 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -311,6 +311,14 @@ bool AssemblerInvocation::createFromArgs(AssemblerInvocation &Opts,
311311
}
312312

313313
namespace {
314+
bool needsPreprocessing(DataObject *O) {
315+
if (O->DataKind != AMD_COMGR_DATA_KIND_SOURCE)
316+
return false;
317+
StringRef Ext = path::extension(O->Name);
318+
bool IsPreprocessedSource = Ext == ".i";
319+
return !IsPreprocessedSource;
320+
}
321+
314322
std::unique_ptr<raw_fd_ostream> getOutputStream(AssemblerInvocation &Opts,
315323
DiagnosticsEngine &Diags,
316324
bool Binary) {
@@ -941,7 +949,10 @@ AMDGPUCompiler::processFiles(amd_comgr_data_kind_t OutputKind,
941949
ScopedDataObjectReleaser SDOR(OutputT);
942950

943951
DataObject *Output = DataObject::convert(OutputT);
944-
Output->setName(std::string(Input->Name) + OutputSuffix);
952+
953+
SmallString<128> OutputName(Input->Name);
954+
sys::path::replace_extension(OutputName, OutputSuffix);
955+
Output->setName(OutputName);
945956

946957
auto OutputFilePath = getFilePath(Output, OutputDir);
947958

@@ -963,6 +974,28 @@ AMDGPUCompiler::processFiles(amd_comgr_data_kind_t OutputKind,
963974
}
964975

965976
amd_comgr_status_t AMDGPUCompiler::addIncludeFlags() {
977+
if (none_of(InSet->DataObjects, needsPreprocessing))
978+
return AMD_COMGR_STATUS_SUCCESS;
979+
980+
switch (ActionInfo->Language) {
981+
case AMD_COMGR_LANGUAGE_OPENCL_1_2:
982+
case AMD_COMGR_LANGUAGE_OPENCL_2_0: {
983+
SmallString<128> OpenCLCBasePath = IncludeDir;
984+
sys::path::append(OpenCLCBasePath, "opencl-c-base.h");
985+
if (auto Status =
986+
outputToFile(getOpenCLCBaseHeaderContents(), OpenCLCBasePath)) {
987+
return Status;
988+
}
989+
Args.push_back("-include");
990+
Args.push_back(Saver.save(OpenCLCBasePath.c_str()).data());
991+
Args.push_back("-Xclang");
992+
Args.push_back("-fdeclare-opencl-builtins");
993+
break;
994+
}
995+
default:
996+
break;
997+
}
998+
966999
if (ActionInfo->Path) {
9671000
Args.push_back("-I");
9681001
Args.push_back(ActionInfo->Path);
@@ -1023,22 +1056,24 @@ amd_comgr_status_t AMDGPUCompiler::addCompilationFlags() {
10231056

10241057
Args.push_back("-x");
10251058

1059+
bool NeedsPreprocessing = any_of(InSet->DataObjects, needsPreprocessing);
1060+
10261061
switch (ActionInfo->Language) {
10271062
case AMD_COMGR_LANGUAGE_LLVM_IR:
10281063
Args.push_back("ir");
10291064
break;
10301065
case AMD_COMGR_LANGUAGE_OPENCL_1_2:
1031-
Args.push_back("cl");
1066+
Args.push_back(NeedsPreprocessing ? "cl" : "cl-cpp-output");
10321067
Args.push_back("-std=cl1.2");
10331068
Args.push_back("-cl-no-stdinc");
10341069
break;
10351070
case AMD_COMGR_LANGUAGE_OPENCL_2_0:
1036-
Args.push_back("cl");
1071+
Args.push_back(NeedsPreprocessing ? "cl" : "cl-cpp-output");
10371072
Args.push_back("-std=cl2.0");
10381073
Args.push_back("-cl-no-stdinc");
10391074
break;
10401075
case AMD_COMGR_LANGUAGE_HIP:
1041-
Args.push_back("hip");
1076+
Args.push_back(NeedsPreprocessing ? "hip" : "hip-cpp-output");
10421077
Args.push_back("--offload-device-only");
10431078
// Pass a cuid that depends on the input files
10441079
// Otherwise, a random (which depends on the /tmp/comgr-xxxxx path) cuid is

amd/comgr/src/comgr-device-libs.cpp

Lines changed: 4 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -25,45 +25,18 @@ using namespace llvm;
2525
namespace COMGR {
2626

2727
namespace {
28-
amd_comgr_status_t addObject(DataSet *DataSet, amd_comgr_data_kind_t Kind,
29-
const char *Name, const void *Data, size_t Size) {
30-
DataObject *Obj = DataObject::allocate(Kind);
31-
if (!Obj) {
32-
return AMD_COMGR_STATUS_ERROR_OUT_OF_RESOURCES;
33-
}
34-
if (auto Status = Obj->setName(Name)) {
35-
return Status;
36-
}
37-
if (auto Status =
38-
Obj->setData(StringRef(reinterpret_cast<const char *>(Data), Size))) {
39-
return Status;
40-
}
41-
DataSet->DataObjects.insert(Obj);
42-
return AMD_COMGR_STATUS_SUCCESS;
43-
}
44-
4528
#include "libraries.inc"
4629
#include "libraries_sha.inc"
47-
#include "opencl1.2-c.inc"
48-
#include "opencl2.0-c.inc"
30+
#include "opencl-c-base.inc"
4931
} // namespace
5032

5133
ArrayRef<unsigned char> getDeviceLibrariesIdentifier() {
5234
return DEVICE_LIBS_ID;
5335
}
5436

55-
amd_comgr_status_t addPrecompiledHeaders(DataAction *ActionInfo,
56-
DataSet *ResultSet) {
57-
switch (ActionInfo->Language) {
58-
case AMD_COMGR_LANGUAGE_OPENCL_1_2:
59-
return addObject(ResultSet, AMD_COMGR_DATA_KIND_PRECOMPILED_HEADER,
60-
"opencl1.2-c.pch", opencl1_2_c, opencl1_2_c_size);
61-
case AMD_COMGR_LANGUAGE_OPENCL_2_0:
62-
return addObject(ResultSet, AMD_COMGR_DATA_KIND_PRECOMPILED_HEADER,
63-
"opencl2.0-c.pch", opencl2_0_c, opencl2_0_c_size);
64-
default:
65-
return AMD_COMGR_STATUS_ERROR_INVALID_ARGUMENT;
66-
}
37+
StringRef getOpenCLCBaseHeaderContents() {
38+
return StringRef(reinterpret_cast<const char *>(opencl_c_base),
39+
opencl_c_base_size);
6740
}
6841

6942
llvm::ArrayRef<std::tuple<llvm::StringRef, llvm::StringRef>>

amd/comgr/src/comgr-device-libs.h

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
#ifndef COMGR_DEVICE_LIBS_H
1010
#define COMGR_DEVICE_LIBS_H
1111

12-
#include "amd_comgr.h"
1312
#include "llvm/ADT/ArrayRef.h"
1413
#include "llvm/ADT/StringRef.h"
1514
#include <tuple>
@@ -19,11 +18,8 @@ namespace COMGR {
1918
struct DataAction;
2019
struct DataSet;
2120

22-
amd_comgr_status_t addPrecompiledHeaders(DataAction *ActionInfo,
23-
DataSet *ResultSet);
24-
2521
llvm::ArrayRef<unsigned char> getDeviceLibrariesIdentifier();
26-
22+
llvm::StringRef getOpenCLCBaseHeaderContents();
2723
llvm::ArrayRef<std::tuple<llvm::StringRef, llvm::StringRef>>
2824
getDeviceLibraries();
2925

amd/comgr/src/comgr.cpp

Lines changed: 7 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -176,21 +176,6 @@ amd_comgr_status_t dispatchCompilerAction(amd_comgr_action_kind_t ActionKind,
176176
}
177177
}
178178

179-
amd_comgr_status_t dispatchAddAction(amd_comgr_action_kind_t ActionKind,
180-
DataAction *ActionInfo, DataSet *InputSet,
181-
DataSet *ResultSet) {
182-
for (DataObject *Data : InputSet->DataObjects) {
183-
Data->RefCount++;
184-
ResultSet->DataObjects.insert(Data);
185-
}
186-
switch (ActionKind) {
187-
case AMD_COMGR_ACTION_ADD_PRECOMPILED_HEADERS:
188-
return addPrecompiledHeaders(ActionInfo, ResultSet);
189-
default:
190-
return AMD_COMGR_STATUS_ERROR_INVALID_ARGUMENT;
191-
}
192-
}
193-
194179
StringRef getLanguageName(amd_comgr_language_t Language) {
195180
switch (Language) {
196181
case AMD_COMGR_LANGUAGE_NONE:
@@ -1381,8 +1366,13 @@ amd_comgr_status_t AMD_COMGR_API
13811366
ResultSetP, *LogP);
13821367
break;
13831368
case AMD_COMGR_ACTION_ADD_PRECOMPILED_HEADERS:
1384-
ActionStatus =
1385-
dispatchAddAction(ActionKind, ActionInfoP, InputSetP, ResultSetP);
1369+
// Redirect the input to the output.
1370+
// Deprecate and remove this action.
1371+
for (DataObject *Data : InputSetP->DataObjects) {
1372+
Data->RefCount++;
1373+
ResultSetP->DataObjects.insert(Data);
1374+
}
1375+
ActionStatus = AMD_COMGR_STATUS_SUCCESS;
13861376
break;
13871377
default:
13881378
ActionStatus = AMD_COMGR_STATUS_ERROR_INVALID_ARGUMENT;

amd/comgr/test-lit/comgr-sources/source-to-bc-with-dev-libs.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,9 +55,9 @@ int main(int argc, char *argv[]) {
5555
amd_comgr_(action_data_count(DataSetPch,
5656
AMD_COMGR_DATA_KIND_PRECOMPILED_HEADER, &Count));
5757

58-
if (Count != 1) {
58+
if (Count != 0) {
5959
printf("AMD_COMGR_ACTION_ADD_PRECOMPILED_HEADERS Failed: "
60-
"produced %zu precompiled header objects (expected 1)\n",
60+
"produced %zu precompiled header objects (expected 0)\n",
6161
Count);
6262
exit(1);
6363
}

amd/comgr/test/compile_source_with_device_libs_to_bc_test.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,9 +60,9 @@ int main(int argc, char *argv[]) {
6060
DataSetPch, AMD_COMGR_DATA_KIND_PRECOMPILED_HEADER, &Count);
6161
checkError(Status, "amd_comgr_action_data_count");
6262

63-
if (Count != 1) {
63+
if (Count != 0) {
6464
printf("AMD_COMGR_ACTION_ADD_PRECOMPILED_HEADERS Failed: "
65-
"produced %zu precompiled header objects (expected 1)\n",
65+
"produced %zu precompiled header objects (expected 0)\n",
6666
Count);
6767
exit(1);
6868
}

0 commit comments

Comments
 (0)