28 Commits

Author SHA1 Message Date
Fraser Cormack
4cb1803ff9 [libclc][NFC] Fix typo in comment 2025-04-14 14:38:58 +01:00
Wenju He
0c21d6b4c8
[libclc] Fix commands in compile_to_bc are executed sequentially (#130755)
In libclc, we observe that compiling OpenCL source files to bitcode is
executed sequentially on Windows, which increases debug build time by
about an hour.
add_custom_command may introduce additional implicit dependencies, see
https://gitlab.kitware.com/cmake/cmake/-/issues/17097
This PR adds a target for each command, enabling parallel builds of
OpenCL source files.
CMake 3.27 has fixed above issue with DEPENDS_EXPLICIT_ONLY. When LLVM
upgrades cmake vertion to 3.7, we can switch to DEPENDS_EXPLICIT_ONLY.
2025-04-14 14:11:04 +01:00
Fraser Cormack
0a74cbfac4
[libclc] Pass -fapprox-func when compiling 'native' builtins (#133119)
The libclc build system isn't well set up to pass arbitrary options to
arbitrary source files in a non-intrusive way. There isn't currently any
other motivating example to warrant rewriting the build system just to
satisfy this requirement. So this commit uses a filename-based approach
to inserting this option into the list of compile flags.
2025-03-28 08:22:19 +00:00
Wenju He
735d7c1539
[libclc] link_bc target should depends on target builtins.link.clc-arch_suffix (#132338)
Currently link_bc command depends on the bitcode file that is associated
with custom target builtins.link.clc-arch_suffix.
On windows we randomly see following error:
`
  Generating builtins.link.clc-${ARCH}--.bc
  Generating builtins.link.libspirv-${ARCH}.bc
error : The requested operation cannot be performed on a file with a
user-mapped section open.
`
I suspect that builtins.link.clc-${ARCH}--.bc file is being generated
while it is being used in link_bc.
This PR adds target-level dependency to ensure
builtins.link.clc-${ARCH}--.bc is generated first.
2025-03-24 10:09:19 +00:00
Wenju He
cb1e91c18d
[libclc] add --only-needed to llvm-link when INTERNALIZE flag is set (#130871)
When -internalize flag is passed to llvm-link, we only need to link in
needed symbols. This PR reduces size of linked bitcode, e.g. by removing
following symbols:

    _Z12__clc_sw_fmaDv16_fS_S_
    _Z12__clc_sw_fmaDv2_fS_S_
    _Z12__clc_sw_fmaDv3_fS_S_
    _Z12__clc_sw_fmaDv4_fS_S_
    _Z12__clc_sw_fmaDv8_fS_S_
    _Z12__clc_sw_fmafff
2025-03-20 13:25:55 +00:00
Fraser Cormack
73d067977b
[libclc] Clean up directory search procedure (#127783)
During a recent change, the build system accidentally dropped the
(theoretical) support for the CLC builtins library to build
target-specific builtins from the 'amdgpu' directory, due to a change in
variable names. This functionality wasn't being used but was spotted
during another code review.

This commit takes the opportunity to clean up and better document the
code that manages the list of directories to search for builtin
implementations.

While fixing this, some references to now-removed SOURCES files were
discovered which have been cleaned up.
2025-02-19 12:21:18 +00:00
Fraser Cormack
1c6cecdbdd
[libclc] Suppress data-layout warnings during linking (#127532)
libclc uses llvm-link to link together all of the individually built
libclc builtins files into one module. Some of these builtins files are
compiled from source by clang whilst others are converted from LLVM IR
directly to bytecode.

When llvm-link links a 'source' module into a 'destination' module, it
warns if the two modules have differing data layouts.

The LLVM IR files libclc links either have no data layout (shared
submodule files) or an explicit data layout in the case of certain
amdgcn/r600 files.

The warnings are very noisy and largely inconsequential. We can suppress
them exploiting a specific behaviours exhibited by llvm-link. When the
destination module has no data layout, it is given the source module's
data layout. Thus, if we link together all IR files first, followed by
the clang-compiled modules, 99% of the warnings are suppressed as they
arose from linking an empty data layout into a non-empty one.

The remaining warnings came from the amdgcn and r600 targets. Some of
these were because the data layouts were out of date compared with what
clang currently produced, so those could have been updated.

However, even with those changes and by grouping the IR files together,
the linker may still link explicit data layouts with empty ones
depending on the order the IR files are processed.

As it happens, the data layouts aren't essential. With the changes to
the link line we can rely on those IR files receiving the correct data
layout from the clang-compiled modules later in the link line. This also
makes the previously AMDGPU-specific IR files available to be used by
all targets in a generic capacity in the future.
2025-02-18 12:06:14 +00:00
Fraser Cormack
9fec0a0942
[libclc] Disable external-calls testing for clspv targets (#127529)
These targets don't include all OpenCL builtins, so there will always be
external calls in the final bytecode module.

Fixes #127316.
2025-02-18 09:14:04 +00:00
Fraser Cormack
15c2d1b328
[libclc] Fix dependencies on generated convert builtins (#127515)
In #127378 it was reported that builds without clspv targets enabled
were failing after #124727, as all targets had a dependency on a file
that only clspv targets generated.

A quick fix was merged in #127315 which wasn't correct. It moved the
dependency on those generated files to the spirv targets, instead of
onto the clspv targets. This means a build with spirv targets and
without clspv targets would see the same problems as #127378 reported.

I tried simply removing the requirement to explicitly add dependencies
to the custom command, relying instead on the file-level dependencies.
This didn't seem reliable enough; in some cases on a Makefiles build,
the clang command compiling (e.g.,) convert.cl would begin before the
file was fully written.

Instead, we keep the target-level dependency but automatically infer it
based on the generated file name, to avoid manual book-keeping of pairs
of files and targets.

This commit also fixes what looks like an unintended bug where, when
ENABLE_RUNTIME_SUBNORMAL was enabled, the OpenCL conversions weren't
being compiled.
2025-02-17 17:36:02 +00:00
Michał Górny
dbc98cfa46
[libclc] [cmake] Fix per-target *_convert.cl dependencies (#127315)
Fix `add_libclc_builtin_set` to add an appropriate dependency to either
`clspv-generate_convert.cl` or `generate_convert.cl` based on the `ARCH`
argument, rather than to both unconditionally. This fixes build failures
due to missing dependencies when `clspv*` targets are not enabled.

The added check mirrors the one from `libclc/CMakeLists.txt`.

Fixes: #127378
2025-02-16 08:48:52 +01:00
Fraser Cormack
76befc86de
Reland "[libclc] Create aliases with custom_command (#115885)" (#116025)
This relands commit 2c980310f67c13dd89c8702d40abeab47a4a2b4b after
fixing an issue.
2024-11-13 11:44:21 +00:00
Sylvestre Ledru
2c980310f6 Revert "[libclc] Create aliases with custom_command (#115885)"
for causing: https://github.com/llvm/llvm-project/issues/115942

This reverts commit 584d1a632f3af0daca4db02f7f3b2c7f48ab0ddf.
2024-11-13 10:05:37 +01:00
Fraser Cormack
584d1a632f
[libclc] Create aliases with custom_command (#115885)
This in conjunction with a custom target prevents them from being
rebuilt if there are no changes.
2024-11-12 17:22:30 +00:00
Fraser Cormack
b2bdd8bd39 [libclc] Create an internal 'clc' builtins library
Some libclc builtins currently use internal builtins prefixed with
'__clc_' for various reasons, e.g., to avoid naming clashes.

This commit formalizes this concept by starting to isolate the
definitions of these internal clc builtins into a separate
self-contained bytecode library, which is linked into each target's
libclc OpenCL builtins before optimization takes place.

The goal of this step is to allow additional libraries of builtins
that provide entry points (or bindings) that are not written in OpenCL C
but still wish to expose OpenCL-compatible builtins. By moving the
implementations into a separate self-contained library, entry points can
share as much code as possible without going through OpenCL C.

The overall structure of the internal clc library is similar to the
current OpenCL structure, with SOURCES files and targets being able to
override the definitions of builtins as needed. The idea is that the
OpenCL builtins will begin to need fewer target-specific overrides, as
those will slowly move over to the clc builtins instead.

Another advantage of having a separate bytecode library with the CLC
implementations is that we can internalize the symbols when linking it
(separately), whereas currently the CLC symbols make it into the final
builtins library (and perhaps even the final compiled binary).

This patch starts of with 'dot' as it's relatively self-contained, as
opposed to most of the maths builtins which tend to pull in other
builtins.

We can also start to clang-format the builtins as we go, which should
help to modernize the codebase.
2024-10-29 13:09:56 +00:00
Fraser Cormack
183b38eb22 [libclc] Split off library build system into helpers
This splits off several key parts of the build system into utility
methods. This will be used in upcoming patches to help provide
additional sets of target-specific builtin libraries.

Running llvm-diff on the resulting LLVM bytecode binaries, and regular
diff on SPIR-V binaries, shows no differences before and after this
patch.
2024-10-29 13:09:56 +00:00
Fraser Cormack
9f3728d157
[libclc] Fix installation w/ ENABLE_RUNTIME_SUBNORMAL (#109926)
The `ARCHIVE` artifact kind is not valid for `install(FILES ...)`.

Additionally, install wasn't resolving the target's `TARGET_FILE`
properly and was trying to find it in the top-level build directory, rather than
in the libclc binary directory. This is because our `TARGET_FILE`
properties were being set to relative paths. The cmake behaviour they
are trying to mimic - `$<TARGET_FILE:$tgt>` - provides an absolute path.

As such this patch updates instances where we set the `TARGET_FILE`
property to return an absolute path.
2024-09-30 10:48:30 +01:00
Michael Kruse
6838c7afb2
[libclc] Revise IDE folder structure (#89746)
Reviewers of #89153 suggested to break up the patch into per-subproject
patches. This is the libclc part. See #89153 for the entire series and
motivation.

Update the folder titles for targets in the monorepository that have not
seen taken care of for some time. These are the folders that targets are
organized in Visual Studio and XCode
(`set_property(TARGET <target> PROPERTY FOLDER "<title>")`)
when using the respective CMake's IDE generator.

 * Ensure that every target is in a folder
 * Use a folder hierarchy with each LLVM subproject as a top-level folder
 * Use consistent folder names between subprojects
 * When using target-creating functions from AddLLVM.cmake, automatically
deduce the folder. This reduces the number of
`set_property`/`set_target_property`, but are still necessary when
`add_custom_target`, `add_executable`, `add_library`, etc. are used. A
LLVM_SUBPROJECT_TITLE definition is used for that in each subproject's
root CMakeLists.txt.
2024-07-18 12:40:55 +02:00
Harald van Dijk
5ee53d417f
[libclc] Fix cross in-tree builds (#97392)
When performing cross in-tree builds, we need native versions of various
tools, we cannot assume the cross builds that are part of the current
build are executable. LLVM provides the setup_host_tool function to
handle this, either picking up versions of tools from
LLVM_NATIVE_TOOL_DIR, or implicitly building native versions as needed.
Use it for libclc too.

LLVM's setup_host_tool function assumes the project is LLVM, so this
also needs libclc's project() to be conditional on it being built
standalone. Luckily, the only change this needs is using
CMAKE_CURRENT_SOURCE_DIR instead of PROJECT_SOURCE_DIR.
2024-07-02 16:26:58 +01:00
Tim Creech
64796044f4
[CMake][libclc] Improve dependencies to avoid build errors (#95018)
With the Makefile generator and particularly high build parallelism some
intermediate dependencies may be generated redundantly and concurrently,
leading to build failures.

To fix this, arrange for libclc's add_custom_commands to depend on
targets in addition to files.

This follows CMake documentation's[^1] guidance on add_custom_command:

> Do not list the output in more than one independent target that may
> build in parallel or the instances of the rule may conflict. Instead,
> use the add_custom_target() command to drive the command and make the
> other targets depend on that one.

Eliminating the redundant commands also improves build times.

[^1]: https://cmake.org/cmake/help/v3.29/command/add_custom_command.html
2024-06-24 09:51:34 +01:00
Fraser Cormack
effb2f1917
[libclc] Use a response file when building on Windows (#89756)
We've recently seen the libclc llvm-link invocations become so long that
they exceed the character limits on certain platforms.

Using a 'response file' should solve this by offloading the list of
inputs into a separate file, and using special syntax to pass it to
llvm-link. Note that neither the response file nor syntax aren't
specific to Windows but we restrict it to that platform regardless. We
have the option of expanding it to other platforms in the future.
2024-04-24 10:11:26 +01:00
Fraser Cormack
e614e037f3
[libclc] Fix build with Unix Makefiles (#89147)
Commit #87622 broke the build. Ninja was happy with creating the output
directories as necessary, but Unix Makefiles isn't. Ensure they are
always created.

Fixes #88626.
2024-04-22 09:50:39 +01:00
Fraser Cormack
72f9881c3f
[libclc] Refactor build system to allow in-tree builds (#87622)
The previous build system was adding custom "OpenCL" and "LLVM IR"
languages in CMake to build the builtin libraries. This was making it
harder to build in-tree because the tool binaries needed to be present
at configure time.

This commit refactors the build system to use custom commands to build
the bytecode files one by one, and link them all together into the final
bytecode library. It also enables in-tree builds by aliasing the
clang/llvm-link/etc. tool targets to internal targets, which are
imported from the LLVM installation directory when building out of tree.

Diffing (with llvm-diff) all of the final bytecode libraries in an
out-of-tree configuration against those built using the current tip
system shows no changes. Note that there are textual changes to metadata
IDs which confuse regular diff, and that llvm-diff 14 and below may show
false-positives.

This commit also removes a file listed in one of the SOURCEs which
didn't exist and which was preventing the use of
ENABLE_RUNTIME_SUBNORMAL when configuring CMake.
2024-04-11 17:09:07 +01:00
Fraser Cormack
688d71ea88
[libclc] Track dependencies through dependency files (#86965)
This commit fixes the problem of missing build dependencies between
libclc source files and their various includes (namely headers and .inc
files).

We would like to do this with compiler-generated dependency files
because then the dependencies are accurate and there are no false
positives, leading to unnecessary rebuilds. This is how regular C/C++
dependencies are usually tracked by CMake.

Note that this variable is an internal API so is not guaranteed to work,
but then again *all* of CMake's support for new languages (which we use
for CLC/LL languages) is an internal API. On balance this change is
probably worth it due to how minimally invasive it is. It should work
with all supported compilers and CMake generators.
2024-03-28 20:29:25 +00:00
Fraser Cormack
9253950ec1
[libclc] Convert tabs to spaces in CMake (#85634)
Having a mix of tabs and spaces makes the diff of any changes to the
build system noisier than necessary. This commit unifies them to two
spaces.

This includes some minor cosmetic changes such as with joining things on
one line where appropriate.

There are other files in libclc which have tabs but those haven't been
touched at this time. Those could come at another time if desired,
though they might be more contentious as the project isn't
clang-formatted at all and so that might invite larger discussions
around formatting.
2024-03-18 14:37:04 +00:00
Daniel Stone
24fad7278a libclc: Use temporary files rather than a pipe
This is required for using the Ninja backend on Windows, as it passes
commands directly to CreateProcess, and does not allow the shell to
interpret them: https://ninja-build.org/manual.html#ref_rule_command

Using the Visual Studio backend is not possible as attempting to create
a static library target comprised entirely of novel languages not known
to the Visual Studio backend built in to CMake's C++ source will
generate nothing at all.

reviewer: jvesely
Differential Revision: https://reviews.llvm.org/D77165
2020-04-14 10:03:27 -04:00
Daniel Stone
cccdd0579b libclc: Don't pass linker flags to CLC/LLAsm
We don't want the regular linker flags for these invocations, since
we're not compiling to the target machine anyway. This fixes things like
'/machine:x64' being unknown when invoked under Windows.

reviewer: jvesely
Differential Revision: https://reviews.llvm.org/D77164
2020-04-14 10:03:27 -04:00
Daniel Stone
acf079006e libclc: Use echo rather than true for try_compile
When providing a fake compiler, libclc currently uses 'true' which does
not exist on Windows. Use echo instead as the no-op.

reviewer: jvesely
Differential Revision: https://reviews.llvm.org/D77163
2020-04-14 10:03:27 -04:00
Jan Vesely
2ce1d090c2 Add cmake build system
Add cmake support for CLC and ll asm language,
the latter includes clang preprocessing stage.
Add ctests to check for external function calls.

v2: fix typos, style

Signed-off-by: Jan Vesely <jan.vesely@rutgers.edu>
Acked-by: Aaron Watry <awatry@gmail.com>
Tested-by: Aaron Watry <awatry@gmail.com>
Acked-by: Vedran Miletić <vedran@miletic.net>
llvm-svn: 347667
2018-11-27 16:07:19 +00:00