Skip to content
Merged
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
2 changes: 2 additions & 0 deletions clang/include/clang/Basic/DiagnosticDriverKinds.td
Original file line number Diff line number Diff line change
Expand Up @@ -394,6 +394,8 @@ def warn_drv_fraw_string_literals_in_cxx11 : Warning<
"ignoring '-f%select{no-|}0raw-string-literals', which is only valid for C and C++ standards before C++11">,
InGroup<UnusedCommandLineArgument>;

def err_drv_libclc_not_found : Error<"no libclc library '%0' found in the clang resource directory">;

def err_drv_invalid_malign_branch_EQ : Error<
"invalid argument '%0' to -malign-branch=; each element must be one of: %1">;

Expand Down
3 changes: 3 additions & 0 deletions clang/include/clang/Driver/CommonArgs.h
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,9 @@ void addOpenMPDeviceRTL(const Driver &D, const llvm::opt::ArgList &DriverArgs,
StringRef BitcodeSuffix, const llvm::Triple &Triple,
const ToolChain &HostTC);

void addOpenCLBuiltinsLib(const Driver &D, const llvm::opt::ArgList &DriverArgs,
llvm::opt::ArgStringList &CC1Args);

void addOutlineAtomicsArgs(const Driver &D, const ToolChain &TC,
const llvm::opt::ArgList &Args,
llvm::opt::ArgStringList &CmdArgs,
Expand Down
15 changes: 15 additions & 0 deletions clang/include/clang/Driver/Options.td
Original file line number Diff line number Diff line change
Expand Up @@ -1422,6 +1422,21 @@ def fno_hip_emit_relocatable : Flag<["-"], "fno-hip-emit-relocatable">,
HelpText<"Do not override toolchain to compile HIP source to relocatable">;
}

// Clang specific/exclusive options for OpenACC.
def openacc_macro_override
: Separate<["-"], "fexperimental-openacc-macro-override">,
Visibility<[ClangOption, CC1Option]>,
Group<f_Group>,
HelpText<"Overrides the _OPENACC macro value for experimental testing "
"during OpenACC support development">;
def openacc_macro_override_EQ
: Joined<["-"], "fexperimental-openacc-macro-override=">,
Alias<openacc_macro_override>;

// End Clang specific/exclusive options for OpenACC.

def libclc_lib_EQ : Joined<["--"], "libclc-lib=">, Group<opencl_Group>,
HelpText<"Namespec of libclc OpenCL bitcode library to link">;
def libomptarget_amdgpu_bc_path_EQ : Joined<["--"], "libomptarget-amdgpu-bc-path=">, Group<i_Group>,
HelpText<"Path to libomptarget-amdgcn bitcode library">;
def libomptarget_amdgcn_bc_path_EQ : Joined<["--"], "libomptarget-amdgcn-bc-path=">, Group<i_Group>,
Expand Down
3 changes: 3 additions & 0 deletions clang/lib/Driver/ToolChains/AMDGPU.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -857,6 +857,9 @@ void AMDGPUToolChain::addClangTargetOptions(
CC1Args.push_back("-fvisibility=hidden");
CC1Args.push_back("-fapply-global-visibility-to-externs");
}

if (DeviceOffloadingKind == Action::OFK_None)
addOpenCLBuiltinsLib(getDriver(), DriverArgs, CC1Args);
}

void AMDGPUToolChain::addClangWarningOptions(ArgStringList &CC1Args) const {
Expand Down
37 changes: 37 additions & 0 deletions clang/lib/Driver/ToolChains/CommonArgs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2985,6 +2985,43 @@ void tools::addHIPRuntimeLibArgs(const ToolChain &TC, Compilation &C,
}
}

void tools::addOpenCLBuiltinsLib(const Driver &D,
const llvm::opt::ArgList &DriverArgs,
llvm::opt::ArgStringList &CC1Args) {
// Check whether user specifies a libclc bytecode library
const Arg *A = DriverArgs.getLastArg(options::OPT_libclc_lib_EQ);
if (!A)
return;

// Find device libraries in <LLVM_DIR>/lib/clang/<ver>/lib/libclc/
SmallString<128> LibclcPath(D.ResourceDir);
llvm::sys::path::append(LibclcPath, "lib", "libclc");

// If the namespec is of the form :filename, search for that file.
StringRef LibclcNamespec(A->getValue());
bool FilenameSearch = LibclcNamespec.consume_front(":");
SmallString<128> LibclcTargetFile(LibclcNamespec);

if (FilenameSearch && llvm::sys::fs::exists(LibclcTargetFile)) {
CC1Args.push_back("-mlink-builtin-bitcode");
CC1Args.push_back(DriverArgs.MakeArgString(LibclcTargetFile));
} else {
// Search the library paths for the file
if (!FilenameSearch)
LibclcTargetFile += ".bc";

llvm::sys::path::append(LibclcPath, LibclcTargetFile);
if (llvm::sys::fs::exists(LibclcPath)) {
CC1Args.push_back("-mlink-builtin-bitcode");
CC1Args.push_back(DriverArgs.MakeArgString(LibclcPath));
} else {
// Since the user requested a library, if we haven't one then report an
// error.
D.Diag(diag::err_drv_libclc_not_found) << LibclcTargetFile;
}
}
}

void tools::addOutlineAtomicsArgs(const Driver &D, const ToolChain &TC,
const llvm::opt::ArgList &Args,
llvm::opt::ArgStringList &CmdArgs,
Expand Down
Empty file.
Empty file.
9 changes: 9 additions & 0 deletions clang/test/Driver/opencl-libclc.cl
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// RUN: %clang -### -target amdgcn-amd-amdhsa --no-offloadlib --libclc-lib=:%S/Inputs/libclc/libclc.bc %s 2>&1 | FileCheck %s
// RUN: %clang -### -target amdgcn-amd-amdhsa --no-offloadlib --libclc-lib=:%S/Inputs/libclc/subdir/libclc.bc %s 2>&1 | FileCheck %s --check-prefix CHECK-SUBDIR

// RUN: not %clang -### -target amdgcn-amd-amdhsa --no-offloadlib --libclc-lib=:%S/Inputs/libclc/subdir/not-here.bc %s 2>&1 | FileCheck %s --check-prefix CHECK-ERROR

// CHECK: -mlink-builtin-bitcode{{.*}}Inputs{{/|\\\\}}libclc{{/|\\\\}}libclc.bc
// CHECK-SUBDIR: -mlink-builtin-bitcode{{.*}}Inputs{{/|\\\\}}libclc{{/|\\\\}}subdir{{/|\\\\}}libclc.bc

// CHECK-ERROR: no libclc library{{.*}}not-here.bc' found in the clang resource directory
13 changes: 10 additions & 3 deletions libclc/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,9 @@ if( LIBCLC_STANDALONE_BUILD OR CMAKE_SOURCE_DIR STREQUAL CMAKE_CURRENT_SOURCE_DI
set( ${tool}_target )
endforeach()
endif()

# Setup the paths where libclc runtimes should be stored.
set( LIBCLC_OUTPUT_LIBRARY_DIR ${CMAKE_CURRENT_BINARY_DIR} )
else()
# In-tree configuration
set( LIBCLC_STANDALONE_BUILD FALSE )
Expand All @@ -92,10 +95,14 @@ else()
get_host_tool_path( llvm-link LLVM_LINK llvm-link_exe llvm-link_target )
get_host_tool_path( opt OPT opt_exe opt_target )
endif()
endif()

# Setup the paths where libclc runtimes should be stored.
set( LIBCLC_OUTPUT_LIBRARY_DIR ${CMAKE_CURRENT_BINARY_DIR} )
# 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.
get_clang_resource_dir( LIBCLC_OUTPUT_DIR PREFIX ${LLVM_LIBRARY_OUTPUT_INTDIR}/.. )

# Note we do not adhere to LLVM_ENABLE_PER_TARGET_RUNTIME_DIR.
set( LIBCLC_OUTPUT_LIBRARY_DIR ${LIBCLC_OUTPUT_DIR}/lib/libclc )
Copy link
Contributor

@wenju-he wenju-he Aug 8, 2025

Choose a reason for hiding this comment

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

When configuring on windows using cmake msvc generator

cmake -G "Visual Studio 17 2022" -A x64 -Thost=x64 -DLLVM_ENABLE_PROJECTS="llvm;clang;clang-tools-extra;libclc" -DLLVM_INCLUDE_TESTS=OFF -DLLVM_BUILD_TESTS=OFF -DLLVM_ENABLE_ASSERTIONS=ON -DLLVM_TARGETS_TO_BUILD="X86;AMDGPU" -DCMAKE_INSTALL_PREFIX=install -DCMAKE_BUILD_TYPE=Release -DLIBCLC_TARGETS_TO_BUILD="amdgcn--amdhsa" ../llvm

install command in tools\libclc\cmake_install.cmake is not correctly configured as there is a unresolved $(Configuration):

if(CMAKE_INSTALL_COMPONENT STREQUAL "Unspecified" OR NOT CMAKE_INSTALL_COMPONENT)
  file(INSTALL DESTINATION "${CMAKE_INSTALL_PREFIX}/share/clc" TYPE FILE FILES "D:/wenjuhe/llvm-project/build/$(Configuration)/lib/../lib/clang/22/lib/libclc/amdgcn--amdhsa.bc")
endif()

It causes install error in our downstream project and the error is reproducible in llvm-project:

cmake --build . --config Release --target install -j40

  CMake Error at tools/libclc/cmake_install.cmake:45 (file):
    file INSTALL cannot find
    "D:/wenjuhe/llvm-project/build/$(Configuration)/lib/../lib/clang/22/lib/libclc/amdgcn--amdhsa.bc":
    File exists.

@frasercrmck do you have suggestion about why $(Configuration) is unresolved?
cmake jinja generator doesn't seem to have the issue because I can build llvm-project and install with ninja.

If I enable libclc in LLVM_ENABLE_RUNTIMES, there is another build error:

cmake -G "Visual Studio 17 2022" -A x64 -Thost=x64 -DLLVM_ENABLE_PROJECTS="llvm;clang;clang-tools-extra" -DLLVM_ENABLE_RUNTIMES=libclc -DLLVM_INCLUDE_TESTS=OFF -DLLVM_BUILD_TESTS=OFF -DLLVM_ENABLE_ASSERTIONS=ON -DLLVM_TARGETS_TO_BUILD="X86;AMDGPU" -DCMAKE_INSTALL_PREFIX=install -DCMAKE_BUILD_TYPE=Release -DLIBCLC_TARGETS_TO_BUILD="amdgcn--amdhsa" ../llvm
Unknown CMake command "get_clang_resource_dir".

Copy link
Contributor

Choose a reason for hiding this comment

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

will be fixed by #152703

endif()

if( EXISTS ${LIBCLC_CUSTOM_LLVM_TOOLS_BINARY_DIR} )
message( WARNING "Using custom LLVM tools to build libclc: "
Expand Down
Loading