705 Commits

Author SHA1 Message Date
Fraser Cormack
06789ccb16
[libclc] Optimize ceil/fabs/floor/rint/trunc (#119596)
These functions all map to the corresponding LLVM intrinsics, but the
vector intrinsics weren't being generated. The intrinsic mapping from
CLC vector function to vector intrinsic was working correctly, but the
mapping from OpenCL builtin to CLC function was suboptimally recursively
splitting vectors in halves.

For example, with this change, `ceil(float16)` calls `llvm.ceil.v16f32`
directly once optimizations are applied.

Now also, instead of generating LLVM intrinsics through `__asm` we now
call clang elementwise builtins for each CLC builtin. This should be a
more standard way of achieving the same result

The CLC versions of each of these builtins are also now built and
enabled for SPIR-V targets. The LLVM -> SPIR-V translator maps the
intrinsics to the appropriate OpExtInst, so there should be no
difference in semantics, despite the newly introduced indirection from
OpenCL builtin through the CLC builtin to the intrinsic.

The AMDGPU targets make use of the same `_CLC_DEFINE_UNARY_BUILTIN`
macro to override `sqrt`, so those functions also appear more optimal
with this change, calling the vector `llvm.sqrt.vXf32` intrinsics
directly.
2024-12-13 08:47:13 +00: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
7387338007 [libclc] Add some include guards to CLC declarations. NFC 2024-11-12 17:25:40 +00: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
a55248789e
[libclc] Avoid using undefined vector3 components (#115857)
Using '.hi' on a vector3 is technically allowed by the spec and is
treated as a 4-element vector with an "undefined" w component. However,
it's more undef/poison code for the compiler to process and remove. We
can easily avoid it with a dedicated macro.
2024-11-12 16:23:52 +00:00
Fraser Cormack
0d2ef7af19
[libclc] Use builtin_convertvector to convert between vector types (#115865)
This keeps values in vectors, rather than scalarizing them and then
reconstituting the vector. The builtin is identical to performing a
C-style cast on each element, which is what we were doing by recursively
splitting the vector down to calling the "base" conversion function on
each element.
2024-11-12 16:18:33 +00:00
Fraser Cormack
6ca50a2593 [libclc] Correct use of CLC macro on two definitions
_CLC_DECL is for declarations and _CLC_DEF for definitions, as the names
imply.

No change to any bitcode module.
2024-11-07 17:47:52 +00:00
Fraser Cormack
b231647475
[libclc] Move relational functions to the CLC library (#115171)
The OpenCL relational functions now call their CLC counterparts, and the
CLC relational functions are defined identically to how the OpenCL
functions were defined.

As usual, clspv and spir-v targets bypass these.

No observable changes to any libclc target (measured with llvm-diff).
2024-11-06 19:28:44 +00:00
Fraser Cormack
b4263ddbe7 [libclc] Use __clc_max in CLC functions 2024-11-06 09:16:36 +00:00
Fraser Cormack
7be30fd533 [libclc] Move abs/abs_diff to CLC library 2024-11-06 09:16:35 +00:00
Fraser Cormack
d2d1b5897e
[libclc] Move clcmacro.h to CLC library. NFC (#114845) 2024-11-04 22:00:01 +00:00
Fraser Cormack
293c78ba0a
[libclc] Move ceil/fabs/floor/rint/trunc to CLC library (#114774)
These functions are all mapped to LLVM intrinsics.

The clspv and spirv targets don't declare or define any of these CLC
functions, and instead map these to their corresponding OpenCL symbols.
2024-11-04 16:35:14 +00:00
Fraser Cormack
b4ef43fc75 [libclc] Format clc_fma.cl. NFC 2024-11-04 11:55:42 +00:00
Fraser Cormack
e28d7f7134 [libclc] Format clc_tan.cl. NFC 2024-11-04 10:52:46 +00:00
Fraser Cormack
f1888e4029 [libclc] Add some include guards and format a file 2024-11-04 10:37:11 +00:00
Fraser Cormack
d12a8da1de
[libclc] Move min/max/clamp into the CLC builtins library (#114386)
These functions are "shared" between integer and floating-point types,
hence the directory name. They are used in several CLC internal
functions such as __clc_ldexp.

Note that clspv and spirv targets don't want to define these functions,
so pre-processor macros replace calls to __clc_min with regular min, for
example. This means they can use as much of the generic CLC source files
as possible, but where CLC functions would usually call out to an
external __clc_min symbol, they call out to an external min symbol. Then
they opt out of defining __clc_min itself in their CLC builtins library.

Preprocessor definitions for these targets have also been changed
somewhat: what used to be CLC_SPIRV (the 32-bit target) is now
CLC_SPIRV32, and CLC_SPIRV now represents either CLC_SPIRV32 or
CLC_SPIRV64. Same goes for CLC_CLSPV.

There are no differences (measured with llvm-diff) in any of the final
builtins libraries for nvptx, amdgpu, or clspv. Neither are there
differences in the SPIR-V targets' LLVM IR before it's actually lowered
to SPIR-V.
2024-10-31 16:45:37 +00:00
Fraser Cormack
86974e15f5 [libclc] Restore header order, which formatting broke 2024-10-31 10:33:47 +00:00
Fraser Cormack
fba9f05ff7 [libclc] Format clc_ldexp.cl and clc_hypot.cl. NFC 2024-10-31 10:18:29 +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
Carl Ritson
076aac59ac
[AMDGPU] Add a new target for gfx1153 (#113138) 2024-10-23 12:56:58 +09:00
David Spickett
a4de127086
[libclc] Give a helpful error when an unknown target is requested (#111528)
I just tried using LLVM backend names here e.g. NVPTX but libclc want's
targets more like triples. This change adds a mesasge to tell you that.

Before you got:
```
 libclc target 'AMDGCN' is enabled
 CMake Error at CMakeLists.txt:253 (list):
   list index: 1 out of range (-1, 0)

 CMake Error at CMakeLists.txt:254 (list):
   list index: 2 out of range (-1, 0)

 Configuring incomplete, errors occurred!
```
Now you get:

```
 CMake Error at CMakeLists.txt:145 (message):
   Unknown target in LIBCLC_TARGETS_TO_BUILD: "AMDGCN"

   Valid targets are:
   amdgcn--;amdgcn--amdhsa;clspv--;clspv64--;r600--;nvptx--;nvptx64--;nvptx--nvidiacl;nvptx64--nvidiacl;amdgcn-mesa-mesa3d
```
Some of the targets are dynamic based on what is installed, so spirv
isn't here for me because I don't have llvm-spirv installed yet.

So this is not perfect but it's an improvement on the current behaviour.
2024-10-09 09:13:26 +01:00
David Spickett
70e0a7e7e6
[libclc] Convert README to Markdown (#111549)
A bit nicer to read on GitHub and with clickable links.

No content changes purely formatting.
2024-10-08 16:58:02 +01:00
David Spickett
64f7e1b697
[libclc] Update build instructions in readme (#111369)
The configure Python script was removed by
d6e0e6d255a7d54a3873b7a5d048eee00ef6bb6d /
https://reviews.llvm.org/D69966.

The readme was never updated with the cmake way to do it. I couldn't
find any dedicated buildbots for this so I'm making an educated guess.
This is what built locally for me.
2024-10-08 16:23:42 +01:00
David Spickett
0e8555d4db
[libclc] Remove mention of BSD license in readme (#111371)
This seems to be an artifact from the intial import in 2012, but even if
not, folks are better off reading the LICENSE.TXT file for the full
details if they need them.

Fixes #109968
2024-10-07 15:26:04 +01: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
Harald van Dijk
903d1c6ee5
[libclc] More cross compilation fixes (#97811)
* Move the setup_host_tool calls to the directories of their tool.
Although it works to call it in libclc, it can only appear in a single
location so it fails the "what if everyone did this?" test and causes
problems for downstream code that also wants to use native versions of
these tools from other projects.
* Correct the TARGET "${${tool}_target}" check. "${${tool}_target}" may
be set to the path to the executable, which works in dependencies but
cannot be tested using if(TARGET). For lack of a better alternative,
just check that "${${tool}_target}" is non-empty and trust that if it
is, it is set to a meaningful value. If somehow it turns out to be a
valid target, its value will still show up in error messages anyway.
* Account for llvm-spirv possibly being provided in-tree. Per
https://github.com/KhronosGroup/SPIRV-LLVM-Translator?tab=readme-ov-file#llvm-in-tree-build
it is possible to drop llvm-spirv into LLVM and have it built as part of
LLVM's build. In this configuration, cross builds of LLVM require a
native version of llvm-spirv to be built.
2024-09-03 17:01:20 +01:00
Romaric Jodin
46223b5eae
libclc: add half version of 'sign' (#99841) 2024-07-22 11:08:56 +01:00
Romaric Jodin
d9cb65ff48
libclc: fix convert with half (#99481)
Fix following update of libclc introducing more fp16 support:
7e6a73959a
2024-07-18 15:28:58 +02:00
Romaric Jodin
7e6a73959a
libclc: increase fp16 support (#98149)
Increase fp16 support to allow clspv to continue to be OpenCL compliant
following the update of the OpenCL-CTS adding more testing on math
functions and conversions with half.

Math functions are implemented by upscaling to fp32 and using the fp32
implementation. It garantees the accuracy required for half-precision
float-point by the CTS.
2024-07-18 12:00:41 +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
Shilei Tian
1ca0055f45
[AMDGPU] Add a new target gfx1152 (#94534) 2024-06-06 12:16:11 -04:00
Romaric Jodin
932ca85680
libclc: remove __attribute__((assume)) for clspv targets (#92126)
Instead add a proper attribute in clang, and add convert it to function
metadata to keep the information in the IR. The goal is to remove the
dependency on __attribute__((assume)) that should have not be there in
the first place.

Ref https://github.com/llvm/llvm-project/pull/84934
2024-05-17 06:13:32 -07:00
Youngsuk Kim
e60b83a645 [libclc] Clarify condition expression (NFC)
Closes #91188
2024-05-14 08:51:56 -05:00
luolent
a98a6e95be
Add clarifying parenthesis around non-trivial conditions in ternary expressions. (#90391)
Fixes [#85868](https://github.com/llvm/llvm-project/issues/85868)

Parenthesis are added as requested on ternary operators with non trivial conditions.

I used this [precedence table](https://en.cppreference.com/w/cpp/language/operator_precedence) for reference, to make sure we get the expected behavior on each change.
2024-05-04 18:38:45 +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
dc74c69f01 [libclc] Provide a more helpful error when tools are missing 2024-04-18 07:01:13 +01:00
Fraser Cormack
0aeeff3059 [libclc] Allow building with pre-built tools
Building the libclc project in-tree with debug tools can be very slow.
This commit adds an option for a user to specify a dierctory from which
to import (e.g., release-built) tools. All tools required by the project
must be imported from the same location, for simplicity.

Original patch downstream authored by @jchlanda.
2024-04-18 07:01:13 +01:00
Fraser Cormack
93d51194b1 [libclc] Clarify option help message
This should make it more obvious it applies only to libclc.
2024-04-18 07:01:13 +01:00
Fraser Cormack
06f54e7c2b [libclc] Convert llvm-spirv to imported executable
This tool now behaves like the others, for consistency.
2024-04-18 07:01:13 +01:00
Fraser Cormack
9d11128632 [libclc] Improve clarity of CMake foreach. NFC.
Should be a bit easier to read.
2024-04-16 16:50:14 +01:00
Fraser Cormack
3d118f9208 [libclc] Fix dependencies between targets
We need file-level - not target-level - dependencies for these custom
commands to re-trigger when their dependencies change.
2024-04-16 15:20:26 +01:00
Fraser Cormack
a0f8191af3 [libclc] Give built bytecode objects a .bc extension. NFC 2024-04-16 15:20:26 +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
b439140e29 [libclc] Fix more spirv build dependencies
The last fix was incomplete.
2024-04-08 16:51:30 +01:00
Fraser Cormack
f46f646506 [libclc] Fix spirv build dependencies
These were accidentally broken in 61efea7.

Thanks to @mgorny and @rjodinchr for spotting this.
2024-04-08 15:48:15 +01:00