Skip to content

Commit d60b5a7

Browse files
vmustyaigcbot
authored andcommitted
Prepare frontend options handling for SYCL input support
The SYCL frontend should require/support a different set of options than the OpenCL C frontend, e.g. no OpenCL extensions or feature/extension macros, and no custom OpenCL headers.
1 parent 8c774c3 commit d60b5a7

2 files changed

Lines changed: 103 additions & 68 deletions

File tree

IGC/OCLFE/igd_fcl_mcl/headers/clang_tb.h

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,8 @@ struct TranslateClangArgs;
4545
static const STB_TranslationCode g_cClangTranslationCodes[] = {
4646
{TB_DATA_FORMAT_OCL_TEXT, TB_DATA_FORMAT_LLVM_TEXT}, {TB_DATA_FORMAT_OCL_TEXT, TB_DATA_FORMAT_LLVM_BINARY},
4747
{TB_DATA_FORMAT_OCL_TEXT, TB_DATA_FORMAT_SPIR_V}, {TB_DATA_FORMAT_ELF, TB_DATA_FORMAT_LLVM_BINARY},
48-
{TB_DATA_FORMAT_ELF, TB_DATA_FORMAT_SPIR_V}, {TB_DATA_FORMAT_ELF, TB_DATA_FORMAT_LLVM_ARCHIVE}};
48+
{TB_DATA_FORMAT_ELF, TB_DATA_FORMAT_SPIR_V}, {TB_DATA_FORMAT_ELF, TB_DATA_FORMAT_LLVM_ARCHIVE},
49+
};
4950

5051
#ifdef _WIN32
5152
#ifdef COMMON_CLANG_LIB_FULL_NAME
@@ -118,6 +119,10 @@ class CClangTranslationBlock : public CTranslationBlock {
118119
bool TranslateClang(const TranslateClangArgs *pInputArgs, STB_TranslateOutputArgs *pOutputArgs,
119120
std::string &exceptString, const char *pInternalOptions);
120121

122+
// Builds the OpenCL-specific clang options (extension list and macro defines)
123+
std::string GetOpenCLOptions(const TranslateClangArgs *pInputArgs, const char *pInternalOptions,
124+
std::string &exceptString) const;
125+
121126
bool TranslateElf(const STB_TranslateInputArgs *pInputArgs, STB_TranslateOutputArgs *pOutputArgs,
122127
std::string &exceptString);
123128

IGC/OCLFE/igd_fcl_mcl/source/clang_tb.cpp

Lines changed: 97 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -298,6 +298,11 @@ static bool AreVMETypesDefined() {
298298
return true;
299299
}
300300

301+
// Returns true if the input is SYCL source, indicated by the '-x sycl' option.
302+
// For SYCL input the OpenCL-specific options (extension/feature macro defines,
303+
// the custom OpenCL header, etc.) must not be passed into the frontend.
304+
static bool IsBuildingForSYCL(const std::string &options) { return options.find("-x sycl") != std::string::npos; }
305+
301306
// Prepares the arguments for the TranslateClang method for the given text input
302307
void CClangTranslationBlock::GetTranslateClangArgs(char *pInput, uint32_t uiInputSize, const char *pOptions,
303308
const char *pInternalOptions, TranslateClangArgs *pClangArgs,
@@ -574,6 +579,10 @@ static std::string GetCDefinesForEnableList(llvm::StringRef enableListStr, unsig
574579
// headers specified
575580
void CClangTranslationBlock::EnsureProperPCH(TranslateClangArgs *pArgs, const char *pInternalOptions,
576581
std::string &exceptString) {
582+
// The custom OpenCL header is not applicable to SYCL input.
583+
if (IsBuildingForSYCL(pArgs->options))
584+
return;
585+
577586
unsigned long CTHeaderSize = 0;
578587
m_cthBuffer = llvm::LoadCharBufferFromResource(IDR_CTH_H, "H", CTHeaderSize);
579588
IGC_ASSERT_MESSAGE(m_cthBuffer, "Error loading Opencl_cth.h");
@@ -860,13 +869,88 @@ static int BuildOptionsAreValid(const std::string &options, std::string &exceptS
860869
return retVal;
861870
}
862871

872+
// Builds the OpenCL-specific clang options: the supported extension list, the
873+
// corresponding extension/feature macro defines and the other OpenCL C macros.
874+
// These options only apply to OpenCL C source and must not be passed for SYCL.
875+
std::string CClangTranslationBlock::GetOpenCLOptions(const TranslateClangArgs *pInputArgs, const char *pInternalOptions,
876+
std::string &exceptString) const {
877+
std::string optionsEx;
878+
std::string extensions;
879+
std::string extensionsFromInternalOptions = GetSubstring(pInternalOptions, "-cl-ext=");
880+
881+
unsigned int oclStd =
882+
GetOclCVersionFromOptions(pInputArgs->options.data(), nullptr, pInputArgs->oclVersion, exceptString);
883+
884+
// if extensions list is passed in via internal options, it will override the default ones.
885+
if (!extensionsFromInternalOptions.empty()) {
886+
extensions = extensionsFromInternalOptions;
887+
} else {
888+
extensions = FormatExtensionsString(m_Extensions);
889+
}
890+
891+
optionsEx.append(extensions);
892+
optionsEx.append(" -I. -D__ENABLE_GENERIC__=1 -D__ENDIAN_LITTLE__");
893+
894+
// Undefine Clang's SPIR/SPIRV macros to prevent automatic extension enablement.
895+
// According to the March 20th OpenCL tooling meeting, Clang's automatic extension
896+
// handling "is trying to do the right thing on a best effort basis, but should not
897+
// be treated as a reference solution, and in its current state appears to be broken
898+
// and doing more harm than good." The recommended solution is to handle all extension
899+
// and feature macro definitions manually.
900+
// By undefining __SPIR__ and __SPIRV__, we prevent Clang from automatically enabling extensions,
901+
// allowing us to explicitly control which extensions are enabled through manual defines.
902+
optionsEx += " -U__SPIR__ -U__SPIRV__";
903+
904+
// get additional -D flags from internal options
905+
optionsEx += " " + GetCDefinesFromInternalOptions(pInternalOptions);
906+
optionsEx += " " + GetCDefinesForEnableList(extensions, oclStd, "-cl-ext=-all,");
907+
908+
// Workaround for Clang issue.
909+
// Clang always defines __IMAGE_SUPPORT__ macro for SPIR target, even if device doesn't support it.
910+
if (optionsEx.find("__IMAGE_SUPPORT__") == std::string::npos) {
911+
optionsEx += " -U__IMAGE_SUPPORT__";
912+
}
913+
914+
// FIXME: do we still support VME?
915+
if (AreVMETypesDefined()) {
916+
optionsEx += " -D__VME_TYPES_DEFINED__";
917+
}
918+
919+
optionsEx += " -D__LLVM_VERSION_MAJOR__=" + to_string(LLVM_VERSION_MAJOR);
920+
921+
return optionsEx;
922+
}
923+
924+
// Builds the '--spirv-ext=' option listing the SPIR-V extensions supported by IGC.
925+
// This allows opencl-clang to enable only relevant extensions when compiling to
926+
// SPIR-V instead of enabling all extensions by default, and prevents generating
927+
// SPIR-V that uses extensions not supported by IGC.
928+
static std::string GetSpirvExtensionsOption() {
929+
const auto &Extensions = IGC::SPIRVExtensionsSupport::getAllExtensions();
930+
if (Extensions.empty())
931+
return {};
932+
933+
std::string option = " --spirv-ext=";
934+
935+
auto it = Extensions.begin();
936+
option += "+";
937+
option += it->Name;
938+
939+
for (++it; it != Extensions.end(); ++it) {
940+
option += ",+";
941+
option += it->Name;
942+
}
943+
944+
return option;
945+
}
946+
863947
// Translates from CL to LL/BC
864948
bool CClangTranslationBlock::TranslateClang(const TranslateClangArgs *pInputArgs, STB_TranslateOutputArgs *pOutputArgs,
865949
std::string &exceptString, const char *pInternalOptions) {
866950
// additional clang options
867951
std::string optionsEx = pInputArgs->optionsEx;
868952
std::string options = pInputArgs->options;
869-
optionsEx.append(" -disable-llvm-optzns -fblocks -I. -D__ENABLE_GENERIC__=1");
953+
bool IsSYCL = IsBuildingForSYCL(options);
870954

871955
switch (m_OutputFormat) {
872956
case TB_DATA_FORMAT_LLVM_TEXT:
@@ -882,10 +966,8 @@ bool CClangTranslationBlock::TranslateClang(const TranslateClangArgs *pInputArgs
882966
break;
883967
}
884968

885-
if (AreVMETypesDefined()) {
886-
optionsEx += " -D__VME_TYPES_DEFINED__";
887-
}
888-
969+
// TODO: do we really need to override the triple? Do we still support 32-bit
970+
// host code for compute?
889971
if (options.find("-triple") == std::string::npos) {
890972
// if target triple not explicitly set
891973
if (pInputArgs->b32bit) {
@@ -896,10 +978,6 @@ bool CClangTranslationBlock::TranslateClang(const TranslateClangArgs *pInputArgs
896978
}
897979
}
898980

899-
if (options.find("-gline-tables-only") != std::string::npos) {
900-
optionsEx += " -debug-info-kind=line-tables-only -dwarf-version=4";
901-
}
902-
903981
#if LLVM_VERSION_MAJOR < 17 && defined(IGC_DEBUG_VARIABLES)
904982
// Allow to dynamically switch from typed to opaque pointers on
905983
// LLVM < 17. Disabling opaque pointers dynamically is not possible.
@@ -913,65 +991,14 @@ bool CClangTranslationBlock::TranslateClang(const TranslateClangArgs *pInputArgs
913991
}
914992
#endif
915993

916-
std::string extensionsFromInternalOptions = GetSubstring(pInternalOptions, "-cl-ext=");
917-
918-
std::string extensions;
919-
920-
// if extensions list is passed in via internal options, it will override the default ones.
921-
if (extensionsFromInternalOptions.size() != 0) {
922-
extensions = extensionsFromInternalOptions;
923-
} else {
924-
extensions = FormatExtensionsString(m_Extensions);
925-
}
926-
927-
optionsEx += " " + extensions;
928-
929-
unsigned int oclStd =
930-
GetOclCVersionFromOptions(pInputArgs->options.data(), nullptr, pInputArgs->oclVersion, exceptString);
931-
932-
// Undefine Clang's SPIR/SPIRV macros to prevent automatic extension enablement.
933-
// According to the March 20th OpenCL tooling meeting, Clang's automatic extension
934-
// handling "is trying to do the right thing on a best effort basis, but should not
935-
// be treated as a reference solution, and in its current state appears to be broken
936-
// and doing more harm than good." The recommended solution is to handle all extension
937-
// and feature macro definitions manually.
938-
// By undefining __SPIR__ and __SPIRV__, we prevent Clang from automatically enabling extensions,
939-
// allowing us to explicitly control which extensions are enabled through manual defines.
940-
optionsEx += " -U__SPIR__";
941-
optionsEx += " -U__SPIRV__";
942-
943-
// get additional -D flags from internal options
944-
optionsEx += " " + GetCDefinesFromInternalOptions(pInternalOptions);
945-
optionsEx += " " + GetCDefinesForEnableList(extensions, oclStd, "-cl-ext=-all,");
946-
947-
optionsEx += " -D__ENDIAN_LITTLE__";
948-
949-
optionsEx += " -D__LLVM_VERSION_MAJOR__=" + to_string(LLVM_VERSION_MAJOR);
950-
951-
// Workaround for Clang issue.
952-
// Clang always defines __IMAGE_SUPPORT__ macro for SPIR target, even if device doesn't support it.
953-
if (optionsEx.find("__IMAGE_SUPPORT__") == std::string::npos) {
954-
optionsEx += " -U__IMAGE_SUPPORT__";
955-
}
956-
957-
// TODO: Workaround - remove after some time to be consistent with LLVM15+ behavior
958-
optionsEx += " -Wno-error=implicit-int";
994+
// SYCL input is compiled without the OpenCL-specific options
995+
// (extension list, feature/extension macro defines, the custom OpenCL
996+
// header, etc.), which only apply to OpenCL C source.
997+
if (!IsSYCL)
998+
optionsEx += GetOpenCLOptions(pInputArgs, pInternalOptions, exceptString);
959999

9601000
// Pass the list of supported SPIR-V extensions from IGC to opencl-clang.
961-
// This allows opencl-clang to enable only relevant extensions when compiling
962-
// OpenCL C to SPIR-V instead of enabling all extensions by default, and
963-
// prevents generating SPIR-V that uses extensions not supported by IGC.
964-
const auto &Extensions = IGC::SPIRVExtensionsSupport::getAllExtensions();
965-
if (!Extensions.empty()) {
966-
optionsEx += " --spirv-ext=";
967-
968-
auto it = Extensions.begin();
969-
optionsEx += "+" + it->Name;
970-
971-
for (++it; it != Extensions.end(); ++it) {
972-
optionsEx += ",+" + it->Name;
973-
}
974-
}
1001+
optionsEx += GetSpirvExtensionsOption();
9751002

9761003
IOCLFEBinaryResult *pResultPtr = NULL;
9771004
int res = 0;
@@ -985,7 +1012,10 @@ bool CClangTranslationBlock::TranslateClang(const TranslateClangArgs *pInputArgs
9851012
(unsigned int)pInputArgs->inputHeaders.size(), (const char **)pInputArgs->inputHeadersNames.data(), NULL, 0,
9861013
options.c_str(), optionsEx.c_str(), pInputArgs->oclVersion.c_str(), &pResultPtr);
9871014
}
988-
if (0 != BuildOptionsAreValid(options, exceptString))
1015+
1016+
// Don't check the options correctness for SYCL after the compilation failure,
1017+
// because SYCL compilation requires a different set of options.
1018+
if (!IsSYCL && 0 != BuildOptionsAreValid(options, exceptString))
9891019
res = -43;
9901020

9911021
Utils::FillOutputArgs(pResultPtr, pOutputArgs, exceptString);

0 commit comments

Comments
 (0)