diff --git a/CMakeLists.txt b/CMakeLists.txt index 97bda2a0f..da433473b 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -139,17 +139,22 @@ else() endif() use_component(${CLSPV_LIBCLC_SOURCE_DIR}) - set(LLVM_ENABLE_PROJECTS "${LLVM_ENABLE_PROJECTS};libclc") + set(LLVM_ENABLE_RUNTIMES "libclc" CACHE STRING "Semicolon-separated list of runtimes to build, or \"all\".") set(LIBCLC_TARGETS_TO_BUILD clspv--;clspv64--) - set(LLVM_EXTERNAL_LIBCLC_SOURCE_DIR ${CLSPV_LIBCLC_SOURCE_DIR}) + + # Tell LLVM to build the native target (needed to build libclc). + set(CLSPV_LLVM_TARGETS_TO_BUILD "Native") + + # Disabling zlib as build is failing on libclc when enabled + set(LLVM_ENABLE_ZLIB "OFF" CACHE STRING "Use zlib for compression/decompression if available. Can be ON, OFF, or FORCE_ON") endif() # Tell LLVM not to build any targets. - set(LLVM_TARGETS_TO_BUILD "" + set(LLVM_TARGETS_TO_BUILD "${CLSPV_LLVM_TARGETS_TO_BUILD}" CACHE STRING "Semicolon-separated list of targets to build, or \"all\".") # Then pull in LLVM for building. - add_subdirectory(${CLSPV_LLVM_SOURCE_DIR} ${CMAKE_CURRENT_BINARY_DIR}/third_party/llvm EXCLUDE_FROM_ALL) + add_subdirectory(${CLSPV_LLVM_SOURCE_DIR} ${CLSPV_LLVM_BINARY_DIR} EXCLUDE_FROM_ALL) # Ensure clspv and LLVM use the same build options (e.g. -D_FILE_OFFSET_BITS=64). list(APPEND CMAKE_MODULE_PATH ${CLSPV_LLVM_SOURCE_DIR}/cmake/modules/) diff --git a/cmake/CMakeLists.txt b/cmake/CMakeLists.txt index 701f31dec..8d5d0e889 100644 --- a/cmake/CMakeLists.txt +++ b/cmake/CMakeLists.txt @@ -116,15 +116,15 @@ add_custom_target(clspv64_builtin_library DEPENDS ${CLSPV64_LIBRARY_OUTPUT_FILE}) if (NOT DEFINED CLSPV_EXTERNAL_LIBCLC_DIR) - set(CLSPV_LIBRARY_INPUT_FILE ${CLSPV_LLVM_BINARY_DIR}/tools/libclc/clspv--.bc) - set(CLSPV64_LIBRARY_INPUT_FILE ${CLSPV_LLVM_BINARY_DIR}/tools/libclc/clspv64--.bc) - set(CLSPV_LIBRARY_INPUT_DEP prepare-clspv--.bc) - set(CLSPV64_LIBRARY_INPUT_DEP prepare-clspv64--.bc) + set(CLSPV_LIBRARY_INPUT_FILE ${CLSPV_LLVM_BINARY_DIR}/lib/clang/${CLANG_EXECUTABLE_VERSION}/lib/libclc/clspv--.bc) + set(CLSPV64_LIBRARY_INPUT_FILE ${CLSPV_LLVM_BINARY_DIR}/lib/clang/${CLANG_EXECUTABLE_VERSION}/lib/libclc/clspv64--.bc) + set(CLSPV_LIBRARY_DEP libclc) + set(CLSPV64_LIBRARY_DEP libclc) else() set(CLSPV_LIBRARY_INPUT_FILE ${CLSPV_EXTERNAL_LIBCLC_DIR}/clspv--.bc) set(CLSPV64_LIBRARY_INPUT_FILE ${CLSPV_EXTERNAL_LIBCLC_DIR}/clspv64--.bc) - set(CLSPV_LIBRARY_INPUT_DEP ${CLSPV_LIBRARY_INPUT_FILE}) - set(CLSPV64_LIBRARY_INPUT_DEP ${CLSPV64_LIBRARY_INPUT_FILE}) + set(CLSPV_LIBRARY_DEP ${CLSPV_LIBRARY_INPUT_FILE}) + set(CLSPV64_LIBRARY_DEP ${CLSPV64_LIBRARY_INPUT_FILE}) endif() add_custom_command( @@ -135,7 +135,7 @@ add_custom_command( --header-var=${CLSPV_LIBRARY_DATA_VAR_NAME} --header-size=${CLSPV_LIBRARY_SIZE_VAR_NAME} WORKING_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}" - DEPENDS ${BAKE_FILE_PYTHON_FILE} ${CLSPV_LIBRARY_INPUT_DEP} ${CLSPV_LIBRARY_INPUT_FILE} + DEPENDS ${BAKE_FILE_PYTHON_FILE} ${CLSPV_LIBRARY_DEP} ) add_custom_command( @@ -146,5 +146,5 @@ add_custom_command( --header-var=${CLSPV64_LIBRARY_DATA_VAR_NAME} --header-size=${CLSPV64_LIBRARY_SIZE_VAR_NAME} WORKING_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}" - DEPENDS ${BAKE_FILE_PYTHON_FILE} ${CLSPV64_LIBRARY_INPUT_DEP} ${CLSPV64_LIBRARY_INPUT_FILE} + DEPENDS ${BAKE_FILE_PYTHON_FILE} ${CLSPV64_LIBRARY_DEP} ) diff --git a/deps.json b/deps.json index 01fda1ae1..579241f67 100644 --- a/deps.json +++ b/deps.json @@ -6,7 +6,11 @@ "subrepo" : "llvm/llvm-project", "branch" : "main", "subdir" : "third_party/llvm", - "commit" : "e68a20e0b7623738d6af736d3aa02625cba6126a" + "commit" : "5bca8f2f97d23c3562544e959702826eb20696af", + "patches" : [ + {"patch": "patches/0001-libclc-CMake-include-GetClangResourceDir-155836.patch"}, + {"patch": "patches/0002-fix-clang_cmake_builddir.patch"} + ] }, { "name" : "SPIRV-Headers", diff --git a/kokoro/scripts/linux/build-amber.sh b/kokoro/scripts/linux/build-amber.sh index c889dc15e..ed2d91b83 100755 --- a/kokoro/scripts/linux/build-amber.sh +++ b/kokoro/scripts/linux/build-amber.sh @@ -32,7 +32,7 @@ SRC=$PWD/github/clspv AMBER_SRC=$PWD/github/amber cd $SRC -python3 utils/fetch_sources.py +python3 utils/fetch_sources.py --ci cd $BUILD_ROOT/github git clone https://github.com/google/amber.git amber diff --git a/kokoro/scripts/linux/build-clvk.sh b/kokoro/scripts/linux/build-clvk.sh index 678078349..5b4ef4989 100755 --- a/kokoro/scripts/linux/build-clvk.sh +++ b/kokoro/scripts/linux/build-clvk.sh @@ -36,7 +36,7 @@ fi # Get Clspv dependencies. cd $SRC -python3 utils/fetch_sources.py +python3 utils/fetch_sources.py --ci VULKAN_VERSION=v1.3.243 diff --git a/kokoro/scripts/linux/build.sh b/kokoro/scripts/linux/build.sh index 01b1d636f..2076abb2f 100755 --- a/kokoro/scripts/linux/build.sh +++ b/kokoro/scripts/linux/build.sh @@ -31,7 +31,7 @@ BUILD_ROOT=$PWD SRC=$PWD/github/clspv cd $SRC -python3 utils/fetch_sources.py +python3 utils/fetch_sources.py --ci mkdir build && cd $SRC/build diff --git a/kokoro/scripts/macos/build.sh b/kokoro/scripts/macos/build.sh index 8aaff362a..66554c79d 100644 --- a/kokoro/scripts/macos/build.sh +++ b/kokoro/scripts/macos/build.sh @@ -40,7 +40,7 @@ export PATH="$PWD/cmake-$CMAKE_VER-macos-universal/CMake.app/Contents/bin:$PATH" # Get dependencies. cd $SRC -python utils/fetch_sources.py +python utils/fetch_sources.py --ci mkdir build && cd $SRC/build diff --git a/kokoro/scripts/windows/build.bat b/kokoro/scripts/windows/build.bat index 01966fe20..fa51a49d7 100644 --- a/kokoro/scripts/windows/build.bat +++ b/kokoro/scripts/windows/build.bat @@ -26,7 +26,7 @@ set PATH=c:\cmake-3.31.2\bin;%PATH% set PATH=c:\Python312;%PATH% cd %SRC% -python utils/fetch_sources.py +python utils/fetch_sources.py --ci :: ######################################### :: set up msvc build env @@ -50,7 +50,7 @@ if "%KOKORO_GITHUB_COMMIT%." == "." ( set BUILD_SHA=%KOKORO_GITHUB_COMMIT% ) -cmake -G%GENERATOR% -DCMAKE_BUILD_TYPE=%BUILD_TYPE% -DLLVM_TARGETS_TO_BUILD="" .. -Thost=x64 +cmake -G%GENERATOR% -DCMAKE_BUILD_TYPE=%BUILD_TYPE% .. -Thost=x64 if %ERRORLEVEL% GEQ 1 exit /b %ERRORLEVEL% diff --git a/lib/FrontendPlugin.cpp b/lib/FrontendPlugin.cpp index 2802f3789..d308b10be 100644 --- a/lib/FrontendPlugin.cpp +++ b/lib/FrontendPlugin.cpp @@ -116,7 +116,7 @@ struct ExtraValidationConsumer final : public ASTConsumer { } else if (auto *VT = dyn_cast(canonical)) { return ContainsSizedType(VT->getElementType(), width); } else if (auto *RT = dyn_cast(canonical)) { - for (auto field_decl : RT->getDecl()->fields()) { + for (auto field_decl : RT->getOriginalDecl()->fields()) { if (ContainsSizedType(field_decl->getType(), width)) return true; } @@ -132,7 +132,7 @@ struct ExtraValidationConsumer final : public ASTConsumer { } else if (auto *AT = dyn_cast(canonical)) { return ContainsPointerType(AT->getElementType()); } else if (auto *RT = dyn_cast(canonical)) { - for (auto field_decl : RT->getDecl()->fields()) { + for (auto field_decl : RT->getOriginalDecl()->fields()) { if (ContainsPointerType(field_decl->getType())) return true; } @@ -148,7 +148,7 @@ struct ExtraValidationConsumer final : public ASTConsumer { } else if (isa(canonical)) { return true; } else if (auto *RT = dyn_cast(canonical)) { - for (auto field_decl : RT->getDecl()->fields()) { + for (auto field_decl : RT->getOriginalDecl()->fields()) { if (ContainsArrayType(field_decl->getType())) return true; } @@ -169,7 +169,7 @@ struct ExtraValidationConsumer final : public ASTConsumer { } else if (auto *AT = dyn_cast(canonical)) { return IsRecursiveType(AT->getElementType(), seen); } else if (auto *RT = dyn_cast(canonical)) { - for (auto field_decl : RT->getDecl()->fields()) { + for (auto field_decl : RT->getOriginalDecl()->fields()) { if (IsRecursiveType(field_decl->getType(), seen)) return true; } @@ -229,7 +229,7 @@ struct ExtraValidationConsumer final : public ASTConsumer { // To avoid infinite recursion, first verify that the record is not // recursive and then that its fields are supported. - for (auto *field_decl : RT->getDecl()->fields()) { + for (auto *field_decl : RT->getOriginalDecl()->fields()) { if (!IsSupportedType(field_decl->getType(), SR, IsKernelParameter)) { return false; } @@ -462,9 +462,10 @@ struct ExtraValidationConsumer final : public ASTConsumer { return false; } - const auto &record_layout = context.getASTRecordLayout(RT->getDecl()); + const auto &record_layout = + context.getASTRecordLayout(RT->getOriginalDecl()); const FieldDecl *prev = nullptr; - for (auto field_decl : RT->getDecl()->fields()) { + for (auto field_decl : RT->getOriginalDecl()->fields()) { const auto field_type = field_decl->getType(); const unsigned field_no = field_decl->getFieldIndex(); const uint64_t field_offset = @@ -893,7 +894,7 @@ struct ExtraValidationConsumer final : public ASTConsumer { clustered_args->completeDefinition(); if (!clustered_args->field_empty()) { auto record_type = - FD->getASTContext().getRecordType(clustered_args); + FD->getASTContext().getCanonicalTagType(clustered_args); if (!IsSupportedLayout(record_type, 0, SSBO, FD->getASTContext(), FD->getSourceRange(), FD->getSourceRange())) { diff --git a/patches/0001-libclc-CMake-include-GetClangResourceDir-155836.patch b/patches/0001-libclc-CMake-include-GetClangResourceDir-155836.patch new file mode 100644 index 000000000..c000c6957 --- /dev/null +++ b/patches/0001-libclc-CMake-include-GetClangResourceDir-155836.patch @@ -0,0 +1,26 @@ +From 9d96c47f6b922ba02dee62337236f1b87074cfe0 Mon Sep 17 00:00:00 2001 +From: Romaric Jodin +Date: Thu, 28 Aug 2025 18:56:33 +0200 +Subject: [PATCH 1/3] libclc: CMake: include GetClangResourceDir (#155836) + +`get_clang_resource_dir` is not guarantee to be there. Make sure of it +by including `GetClangResourceDir`. +--- + libclc/CMakeLists.txt | 1 + + 1 file changed, 1 insertion(+) + +diff --git a/libclc/CMakeLists.txt b/libclc/CMakeLists.txt +index 756e097dcf12..5c4e12793329 100644 +--- a/libclc/CMakeLists.txt ++++ b/libclc/CMakeLists.txt +@@ -99,6 +99,7 @@ else() + + # Setup the paths where libclc runtimes should be stored. By default, in an + # in-tree build we place the libraries in clang's resource driectory. ++ include(GetClangResourceDir) + get_clang_resource_dir( LIBCLC_OUTPUT_DIR PREFIX ${LLVM_LIBRARY_OUTPUT_INTDIR}/.. ) + + # Note we do not adhere to LLVM_ENABLE_PER_TARGET_RUNTIME_DIR. +-- +2.51.0.384.g4c02a37b29-goog + diff --git a/patches/0002-fix-clang_cmake_builddir.patch b/patches/0002-fix-clang_cmake_builddir.patch new file mode 100644 index 000000000..67542d26e --- /dev/null +++ b/patches/0002-fix-clang_cmake_builddir.patch @@ -0,0 +1,48 @@ +From e4916b6bbd1b4e89d67b6a65e89839942b0be804 Mon Sep 17 00:00:00 2001 +From: Romaric Jodin +Date: Thu, 28 Aug 2025 16:22:17 +0200 +Subject: [PATCH 2/3] fix clang_cmake_builddir + +When building llvm from a subdirectory (like clspv does) +`CMAKE_BINARY_DIR` is at the top of the build directory. + +When building runtimes (libclc for example), the build fails looking +for clang (through `find_package` looking at `LLVM_BINARY_DIR` with +`NO_DEFAULT_PATH` & `NO_CMAKE_FIND_ROOT_PATH`) because clang is not in +`LLVM_BINARY_DIR`. + +Fix that issue by setting `clang_cmake_builddir` the same way we set +`llvm_cmake_builddir` from `LLVM_BINARY_DIR`. + +For default llvm build (using llvm as the main cmake project), it +should not change anything. +--- + clang/cmake/modules/CMakeLists.txt | 7 +++---- + 1 file changed, 3 insertions(+), 4 deletions(-) + +diff --git a/clang/cmake/modules/CMakeLists.txt b/clang/cmake/modules/CMakeLists.txt +index d2d68121371b..b3b4a74f6d47 100644 +--- a/clang/cmake/modules/CMakeLists.txt ++++ b/clang/cmake/modules/CMakeLists.txt +@@ -8,15 +8,14 @@ include(FindPrefixFromConfig) + # the usual CMake convention seems to be ${Project}Targets.cmake. + set(CLANG_INSTALL_PACKAGE_DIR "${CMAKE_INSTALL_PACKAGEDIR}/clang" CACHE STRING + "Path for CMake subdirectory for Clang (defaults to '${CMAKE_INSTALL_PACKAGEDIR}/clang')") +-# CMAKE_INSTALL_PACKAGEDIR might be absolute, so don't reuse below. +-set(clang_cmake_builddir "${CMAKE_BINARY_DIR}/lib${LLVM_LIBDIR_SUFFIX}/cmake/clang") + + # Keep this in sync with llvm/cmake/CMakeLists.txt! + set(LLVM_INSTALL_PACKAGE_DIR "${CMAKE_INSTALL_PACKAGEDIR}/llvm" CACHE STRING + "Path for CMake subdirectory for LLVM (defaults to '${CMAKE_INSTALL_PACKAGEDIR}/llvm')") + # CMAKE_INSTALL_PACKAGEDIR might be absolute, so don't reuse below. +-string(REPLACE "${CMAKE_CFG_INTDIR}" "." llvm_cmake_builddir "${LLVM_LIBRARY_DIR}") +-set(llvm_cmake_builddir "${llvm_cmake_builddir}/cmake/llvm") ++string(REPLACE "${CMAKE_CFG_INTDIR}" "." llvm_builddir "${LLVM_LIBRARY_DIR}") ++set( llvm_cmake_builddir "${llvm_builddir}/cmake/llvm") ++set(clang_cmake_builddir "${llvm_builddir}/cmake/clang") + + get_property(CLANG_EXPORTS GLOBAL PROPERTY CLANG_EXPORTS) + export(TARGETS ${CLANG_EXPORTS} FILE ${clang_cmake_builddir}/ClangTargets.cmake) +-- +2.51.0.384.g4c02a37b29-goog + diff --git a/utils/fetch_sources.py b/utils/fetch_sources.py index a0d6a350e..5d48f3404 100755 --- a/utils/fetch_sources.py +++ b/utils/fetch_sources.py @@ -97,6 +97,7 @@ def __init__(self, json): self.branch = json['branch'] self.subdir = os.path.join(TOP_DIR, json['subdir']) if ('subdir' in json) else TOP_DIR self.commit = json['commit'] + self.patches = json.get('patches', []) def GetUrl(self, style='https'): """Returns the URL for the repository.""" @@ -131,12 +132,18 @@ def Fetch(self, shallow): cmd.append(self.commit if shallow else self.branch) command_output(cmd, self.subdir) - def Checkout(self, shallow): + def Patch(self, patch, ci): + command_output(['git', 'apply' if ci else 'am', os.path.join(TOP_DIR, patch)], self.subdir) + + def Checkout(self, shallow, ci): if not os.path.exists(os.path.join(self.subdir,'.git')): self.InitRepo(shallow) if not self.HasCommit(): self.Fetch(shallow) command_output(['git', 'checkout', self.commit], self.subdir) + for patch in self.patches: + self.Patch(patch['patch'], ci) + def GetGoodCommits(): @@ -162,6 +169,7 @@ def main(): parser.add_argument('--deps', choices=all_deps, nargs='+', default=all_deps, help='A list of dependencies to fetch sources for. ' 'All is the default.') + parser.add_argument('--ci', action='store_true') args = parser.parse_args() @@ -175,7 +183,7 @@ def main(): if c.name not in args.deps: continue print('Get {n}\n'.format(n=c.name)) - c.Checkout(args.shallow) + c.Checkout(args.shallow, args.ci) sys.exit(0)