47 Commits

Author SHA1 Message Date
Kazu Hirata
8f71650baa
[clang] Use a new constructor of ArrayRef (NFC) (#146007)
ArrayRef now has a new constructor that takes a parameter whose type
has data() and size().  This patch migrates:

  ArrayRef<T>(X.data(), X.size()

to:

  ArrayRef<T>(X)
2025-06-26 23:38:05 -07:00
Kazu Hirata
9adcb4fe12
[clang] Use llvm::replace (NFC) (#140264) 2025-05-16 09:06:31 -07:00
Yaxun (Sam) Liu
035dcf6a2a
[HIP] fix bundle ID for amdgcnspirv (#139112)
Currently ROCm 6.4.0 only recognize spirv64-amd-amdhsa- in bundle ID.
spirv64-amd-amdhsa-unknown causes all HIP apps compiled for amdgcnspirv
to fail.

Previously we fixed a similar issue for
amdgcn-amd-amdhsa-unknown. This patch extends that to
spirv64-amd-amdhsa-unknown.
2025-05-08 22:08:31 -04:00
Juan Manuel Martinez Caamaño
614d8557dc
[OffloadBundler] Expose function to parse compressed bundle headers (#130284)
In COMGR we hash the header of compressed bundles. For this we take the
first bytes of the buffer (according to the maximum header size) and
hash them.

To have a more stable API, and to be able to pick only the hash field (which is
the only one we are actually interested in) of the header, we propose
a version independent header version that is common to all versions.
2025-03-19 09:10:40 +01:00
Shilei Tian
d85a81b4e4
[OffloadBundler] Rework the ctor of OffloadTargetInfo to support AMDGPU's generic target (#122629)
The current parsing logic for the target string assumes it follows the
format `<kind>-<triple>-<target id>:<feature>`, such as
`hipv4-amdgcn-amd-amdhsa-gfx1030:+xnack`.
Specifically, it assumes that `<target id>` does not contain any `-`,
relying on `rsplit` for parsing.
However, this assumption breaks for AMDGPU's generic targets, which may
contain one or more `-`, such as `gfx10-3-generic` or `gfx12-generic`.
As a result, the existing approach using `rstrip` is no longer reliable.

This patch reworks the parsing logic to handle target strings more
robustly, including support for generic targets.
The bundler now strictly requires a 4-field target triple.
Additionally, a new Python helper function has been added to `config.py`
to normalize the target triple into the 4-field format when it is not,
ensuring tests pass reliably.
2025-03-18 10:09:37 -04:00
Yaxun (Sam) Liu
e87b843811 Reland [OffloadBundler] Compress bundles over 4GB (#122307)
Reland the patch after fixing the lit test.
2025-01-20 21:17:21 -05:00
Yaxun (Sam) Liu
72c560da4b Revert "[OffloadBundler] Compress bundles over 4GB (#122307)"
revert due to failure in buildbot

 https://lab.llvm.org/buildbot/#/builders/144/builds/16114

This reverts commit 4e2efc3bd500836d0fa977d6e257ffee2c92e178.
2025-01-20 20:58:05 -05:00
Yaxun (Sam) Liu
4e2efc3bd5
[OffloadBundler] Compress bundles over 4GB (#122307)
Added initial support for version 3 of the compressed offload bundle
format, which uses 64-bit fields for Total File Size and Uncompressed
Binary Size. This enables support for files larger than 4GB. The support
is currently experimental and can be enabled by setting the environment
variable `COMPRESSED_BUNDLE_FORMAT_VERSION=3`.
2025-01-20 20:17:30 -05:00
macurtis-amd
52c338daec
[llvm][NFC] Rework Timer.cpp globals to ensure valid lifetimes (#121663)
This is intended to help with flang `-ftime-report` support:
- #107270.

With this change, I was able to cherry-pick #107270, uncomment
`llvm::TimePassesIsEnabled = true;` and compile with `-ftime-report`.

I also noticed that `clang/lib/Driver/OffloadBundler.cpp` was statically
constructing a `TimerGroup` and changed it to lazily construct via
ManagedStatic.
2025-01-09 06:32:48 -06:00
Kazu Hirata
4d6a5fc702
[Driver] Remove unused includes (NFC) (#116316)
Identified with misc-include-cleaner.
2024-11-15 07:26:05 -08:00
Abhina Sree
74dcf0b595
[SystemZ][z/OS] Open text files in text mode (#109972)
This patch continues the work that was started here
https://reviews.llvm.org/D99426 to correctly open text files in text
mode.
2024-09-25 10:49:45 -04:00
Kazu Hirata
d8a8cb9a72
[Driver] Avoid repeated hash lookups (NFC) (#108676) 2024-09-13 22:52:23 -07:00
Jakub Chlanda
ab20086422
[CUDA][NFC] CudaArch to OffloadArch rename (#97028)
Rename `CudaArch` to `OffloadArch` to better reflect its content and the
use.
Apply a similar rename to helpers handling the enum.
2024-06-30 07:56:07 +02:00
Yaxun (Sam) Liu
ca3917538d
[ClangOffloadBundler] make hipv4 and hip compatible (#91637)
The distinction between the hip and hipv4 offload kinds is historically
based. Originally, these designations might have indicated different
versions of the code object ABI (Application Binary Interface). However,
as the system has evolved, the ABI version is now embedded directly
within the code object itself, making these historical distinctions
irrelevant during the unbundling process. Consequently, hip and hipv4
are treated as compatible in current implementations, facilitating
interchangeable handling of code objects without differentiation based
on offload kind. This change streamlines code management within the
ecosystem.
2024-05-09 17:14:43 -04:00
Yaxun (Sam) Liu
78dca4af5a
[ClangOffloadBundler] Add file size to header (#88827)
__hipRegisterFatBinary only accepts one pointer argument. It is expected
to get the fat binary size from the header.

This patch adds a file size field to the header of the compressed
bundle.
2024-04-19 10:40:16 -04:00
Yaxun (Sam) Liu
124d0b787b
[HIP] add --offload-compression-level= option (#83605)
Added --offload-compression-level= option to clang and
-compression-level=
option to clang-offload-bundler for controlling compression level.

Added support of long distance matching (LDM) for llvm::zstd which is
off
by default. Enable it for clang-offload-bundler by default since it
improves compression rate in general.

Change default compression level to 3 for zstd for clang-offload-bundler
since it works well for bundle entry size from 1KB to 32MB, which should
cover most of the clang-offload-bundler usage. Users can still specify
compression level by -compression-level= option if necessary.
2024-03-09 09:45:48 -05:00
Yaxun (Sam) Liu
61b13e0dfe
[ClangOffloadBundler] fix unbundling archive (#84195)
When unbundling an archive, need to save the content of each object file
to a temporary file before passing it to llvm-objcopy, instead of
passing the original input archive file to llvm-objcopy.

Also allows extracting host bundles for archives.

Fixes: https://github.com/llvm/llvm-project/issues/83509
2024-03-07 09:26:16 -05:00
Yaxun (Sam) Liu
33a6ce1837
[HIP] Allow partial linking for -fgpu-rdc (#81700)
`-fgpu-rdc` mode allows device functions call device functions in
different TU. However, currently all device objects have to be linked
together since only one fat binary is supported. This is time consuming
for AMDGPU backend since it only supports LTO.

There are use cases that objects can be divided into groups in which
device functions are self-contained but host functions are not. It is
desirable to link/optimize/codegen the device code and generate a fatbin
for each group, whereas partially link the host code with `ld -r` or
generate a static library by using the `--emit-static-lib` option of
clang. This avoids linking all device code together, therefore decreases
the linking time for `-fgpu-rdc`.

Previously, clang emits an external symbol `__hip_fatbin` for all
objects for `-fgpu-rdc`. With this patch, clang emits an unique external
symbol `__hip_fatbin_{cuid}` for the fat binary for each object. When a
group of objects are linked together to generate a fatbin, the symbols
are merged by alias and point to the same fat binary. Each group has its
own fat binary. One executable or shared library can have multiple fat
binaries. Device linking is done for undefined fab binary symbols only
to avoid repeated linking. `__hip_gpubin_handle` is also uniquefied and
merged to avoid repeated registering. Symbol `__hip_cuid_{cuid}` is
introduced to facilitate debugging and tooling.

Fixes: https://github.com/llvm/llvm-project/issues/77018
2024-02-22 13:51:31 -05:00
Jacob Lambert
3cf190974a
[clang-offload-bundler] Add support for -check-input-archive (#73709)
In this patch, we add support for checking a heterogeneous archive. We
also significantly improve the clang-offload-bundler documentation.
2023-11-29 18:39:17 -08:00
Kazu Hirata
b8885926f8 Use llvm::endianness::{big,little,native} (NFC)
Note that llvm::support::endianness has been renamed to
llvm::endianness while becoming an enum class as opposed to an enum.
This patch replaces llvm::support::{big,little,native} with
llvm::endianness::{big,little,native}.
2023-10-10 22:54:51 -07:00
Yaxun (Sam) Liu
7e2823438e Reland "[HIP] Support compressing device binary"
Original PR: https://github.com/llvm/llvm-project/pull/67162

The commit was reverted due to UB detected by santizer:

https://lab.llvm.org/buildbot/#/builders/238/builds/5955

clang/lib/Driver/OffloadBundler.cpp:1012:25: runtime error:
 load of misaligned address 0xaaaae2d90e7c for type
 'const uint64_t' (aka 'const unsigned long'), which
 requires 8 byte alignment

It was fixed by using memcpy instead of dereferencing int*
casted from unaligned char*.
2023-10-05 11:20:46 -04:00
Yaxun (Sam) Liu
c6ed5a6125 Revert "[HIP] Support compressing device binary (#67162)"
This reverts commit a1e81d2ead02e041471ec2299d7382f80c4dbba6.

Revert "Fix test hip-offload-compress-zlib.hip"

This reverts commit ba01ce60665848478ba4e76190907153a8c26fe9.

Revert due to sanity fail at

https://lab.llvm.org/buildbot/#/builders/5/builds/37188

https://lab.llvm.org/buildbot/#/builders/238/builds/5955

/b/sanitizer-aarch64-linux-bootstrap-ubsan/build/llvm-project/clang/lib/Driver/OffloadBundler.cpp:1012:25: runtime error: load of misaligned address 0xaaaae2d90e7c for type 'const uint64_t' (aka 'const unsigned long'), which requires 8 byte alignment
0xaaaae2d90e7c: note: pointer points here
  bc 00 00 00 94 dc 29 9a  89 fb ca 2b 78 9c 8b 8f  77 f6 71 f4 73 8f f7 77  73 f3 f1 77 74 89 77 0a
              ^
    #0 0xaaaaba125f70 in clang::CompressedOffloadBundle::decompress(llvm::MemoryBuffer const&, bool) /b/sanitizer-aarch64-linux-bootstrap-ubsan/build/llvm-project/clang/lib/Driver/OffloadBundler.cpp:1012:25
    #1 0xaaaaba126150 in clang::OffloadBundler::ListBundleIDsInFile(llvm::StringRef, clang::OffloadBundlerConfig const&) /b/sanitizer-aarch64-linux-bootstrap-ubsan/build/llvm-project/clang/lib/Driver/OffloadBundler.cpp:1089:7

Will reland after fixing it.
2023-10-05 00:29:42 -04:00
Yaxun (Sam) Liu
a1e81d2ead
[HIP] Support compressing device binary (#67162)
Add option -f[no-]offload-compress to clang to enable/disable
compression of device binary for HIP. By default it is disabled.

Add option -compress to clang-offload-bundler to enable compression of
offload bundle. By default it is disabled.

When enabled, zstd or zlib is used for compression when available.

When disabled, it is NFC compared to previous behavior. The same offload
bundle format is used as before.

Clang-offload-bundler automatically detects whether the input file to be
unbundled is compressed and the compression method and decompress if
necessary.
2023-10-04 09:32:56 -04:00
zhijian
f740bcb370 [AIX] supporting -X options for llvm-ranlib in AIX OS
Summary:

llvm-ar is symlinked as llvm-ranlib and will act as ranlib when invoked in that mode. llvm-ar since [[ 4f2cfbe531 | compiler/llvm-project@4f2cfbe ]] supports the -X options, but doesn't seem to accept them when running as llvm-ranlib.

In AIX OS , according to https://www.ibm.com/docs/en/aix/7.2?topic=r-ranlib-command

-X mode 	Specifies the type of object file ranlib should examine. The mode must be one of the following:

32
    Processes only 32-bit object files
64
    Processes only 64-bit object files
32_64, any
    Processes both 32-bit and 64-bit object files

The default is to process 32-bit object files (ignore 64-bit objects). The mode can also be set with the OBJECT_MODE environment variable. For example, OBJECT_MODE=64 causes ranlib to process any 64-bit objects and ignore 32-bit objects. The -X flag overrides the OBJECT_MODE variable.

Reviewers: James Henderson, MaskRay, Stephen Peckham
Differential Revision: https://reviews.llvm.org/D142660
2023-08-22 09:41:33 -04:00
Kazu Hirata
ed1539c6ad Migrate {starts,ends}with_insensitive to {starts,ends}_with_insensitive (NFC)
This patch migrates uses of StringRef::{starts,ends}with_insensitive
to StringRef::{starts,ends}_with_insensitive so that we can use names
similar to those used in std::string_view.

Note that the llvm/ directory has migrated in commit
6c3ea866e93003e16fc55d3b5cedd3bc371d1fde.

I'll post a separate patch to deprecate
StringRef::{starts,ends}with_insensitive.

Differential Revision: https://reviews.llvm.org/D150506
2023-05-16 10:12:42 -07:00
Kazu Hirata
7eaa7b0553 [clang] Use *{Map,Set}::contains (NFC) 2023-03-15 18:06:34 -07:00
Kazu Hirata
ea9d404032 [clang] Use *{Set,Map}::contains (NFC) 2023-03-14 19:17:18 -07:00
Jacob Lambert
e48ae0dbd8 [clang-offload-bundler] Standardize TargetID field for bundler
The bundler accepts both of the following for the --target option:
  hip-amdgcn-amd-amdhsa-gfx900    (no env field)
  hip-amdgcn-amd-amdhsa--gfx900   (blank env field)

The environment field is defined as optional for Triples
in Triple.h. However, in this patch we update the bundler to
internally standardize to include the env field. While users
aren't required to specify an env field when listing targets on
the commandline, bundles generated by the offload-bundler will
include the ABI field.

This standardization simplifies things for APIs that deal with
bundles generated by the clang-offload-bundler tool.

Differential Revision: https://reviews.llvm.org/D145770
2023-03-14 14:12:31 -07:00
Archibald Elliott
d768bf994f [NFC][TargetParser] Replace uses of llvm/Support/Host.h
The forwarding header is left in place because of its use in
`polly/lib/External/isl/interface/extract_interface.cc`, but I have
added a GCC warning about the fact it is deprecated, because it is used
in `isl` from where it is included by Polly.
2023-02-10 09:59:46 +00:00
Archibald Elliott
62c7f035b4 [NFC][TargetParser] Remove llvm/ADT/Triple.h
I also ran `git clang-format` to get the headers in the right order for
the new location, which has changed the order of other headers in two
files.
2023-02-07 12:39:46 +00:00
Yaxun (Sam) Liu
3006cb2aa8 [HIP] Unbundler allows missing host entry
Reviewed by: Artem Belevich

Differential Revision: https://reviews.llvm.org/D142118
2023-01-19 22:13:17 -05:00
Yaxun (Sam) Liu
e8f41fdb5c [HIP] Use .hipi as preprocessor output extension
so that clang can recognize it and handle it automatically
without -x hip-cpp-output.

Reviewed by: Artem Belevich

Differential Revision: https://reviews.llvm.org/D141437
2023-01-11 18:39:58 -05:00
Benjamin Kramer
e68fc86b22 [NFCI][Offload Bundler] Replace hand-rolled endian conversion with llvm::support 2023-01-08 17:37:02 +01:00
Benjamin Kramer
b6942a2880 [NFC] Hide implementation details in anonymous namespaces 2023-01-08 17:37:02 +01:00
Gregory Alfonso
d22f050e15 Remove redundant .c_str() and .get() calls
Reviewed By: MaskRay

Differential Revision: https://reviews.llvm.org/D139485
2022-12-18 00:33:53 +00:00
Kazu Hirata
37a3e98c84 [clang] Use std::nullopt instead of None in comments (NFC)
This is part of an effort to migrate from llvm::Optional to
std::optional:

https://discourse.llvm.org/t/deprecating-llvm-optional-x-hasvalue-getvalue-getvalueor/63716
2022-12-09 18:39:01 -08:00
Fangrui Song
2c5d49cffc [Driver] llvm::Optional => std::optional
and change a few referenced Basic and llvm/lib/WindowsDriver API
2022-12-03 20:17:05 +00:00
Fangrui Song
0c2f6e36f9 [Driver] llvm::None => std::nullopt. NFC 2022-12-03 19:43:25 +00:00
raghavmedicherla
5b11caa826 [NFC][Offload Bundler] Formatting clang offloadbundler source file.
Differential Revision: https://reviews.llvm.org/D136715
2022-10-26 15:12:47 -04:00
Yaxun (Sam) Liu
844b84af20 [clang-offload-bundler] extracting compatible bundle entry
In HIP a library is usually compiled with default target ID e.g. gfx906 so that
it can be used in all GPU configurations. The bitcode is saved in bundled
bitcode with gfx906 in entry ID.

In runtime compilation, a HIP program is compiled with a target ID matching
the GPU configuration, e.g. gfx906:xnack-. This program needs to link with
a library bundled bitcode with target ID gfx906.

For example:

  clang --offload-arch=gfx906 -o lib.o lib.hip
  clang --offload-arch=gfx906:xnack- program.hip lib.o

This common use case requires that clang-offlod-bundler to be able to extract
entry with compatible target ID, e.g. extracting an gfx906 entry when requesting
gfx906:xnack-.

Currently clang-offload-bundler only allow extracting entry with exact match
of target ID. This patch relaxes that so that it can extract entries with compatible
target ID.

Reviewed by: Artem Belevich, Saiyedul Islam

Differential Revision: https://reviews.llvm.org/D134546
2022-10-05 19:44:38 -04:00
Kazu Hirata
981cbfb592 [clang] Don't include StringSwitch.h (NFC)
These files don't seem to use StringSwitch.
2022-09-18 22:21:32 -07:00
Simon Pilgrim
b978fa2844 OffloadBundler.cpp - fix Wdocumentation warnings. NFC.
Fix param list instead of embedding \p tag
2022-08-01 15:24:47 +01:00
Kazu Hirata
a948117088 [clang] Use has_value instead of value (NFC) 2022-07-29 21:18:39 -07:00
Fangrui Song
7430894a65 Replace Optional::hasValue with has_value or operator bool. NFC 2022-07-29 10:57:25 -07:00
Jacob Lambert
0f3f357e26 [clang-offload-bundler] Library-ize ClangOffloadBundler
Lifting the core functionalities of the clang-offload-bundler into a
user-facing library/API. This will allow online and JIT compilers to
bundle and unbundle files without spawning a new process.

This patch lifts the classes and functions used to implement
the clang-offload-bundler into a separate OffloadBundler.cpp,
and defines three top-level API functions in OfflaodBundler.h.
        BundleFiles()
        UnbundleFiles()
        UnbundleArchives()

This patch also introduces a Config class that locally stores the
previously global cl::opt options and arrays to allow users to call
the APIs in a multi-threaded context, and introduces an
OffloadBundler class to encapsulate the top-level API functions.

We also  lift the BundlerExecutable variable, which is specific
to the clang-offload-bundler tool, from the API, and replace
its use with an ObjcopyPath variable. This variable must be set
in order to internally call llvm-objcopy.

Finally, we move the API files from
clang/tools/clang-offload-bundler into clang/lib/Driver and
clang/include/clang/Driver.

Differential Revision: https://reviews.llvm.org/D129873
2022-07-27 11:54:38 -07:00
Lambert, Jacob
4638d7a28f Revert "[clang-offload-bundler] Library-ize ClangOffloadBundler"
This reverts commit 8348c4095600ec2c0beee293267832799d2ebee3.
2022-07-26 11:22:31 -07:00
Jacob Lambert
8348c40956 [clang-offload-bundler] Library-ize ClangOffloadBundler
Lifting the core functionalities of the clang-offload-bundler into a
user-facing library/API. This will allow online and JIT compilers to
bundle and unbundle files without spawning a new process.

This patch lifts the classes and functions used to implement
the clang-offload-bundler into a separate OffloadBundler.cpp,
and defines three top-level API functions in OfflaodBundler.h.
        BundleFiles()
        UnbundleFiles()
        UnbundleArchives()

This patch also introduces a Config class that locally stores the
previously global cl::opt options and arrays to allow users to call
the APIs in a multi-threaded context, and introduces an
OffloadBundler class to encapsulate the top-level API functions.

We also  lift the BundlerExecutable variable, which is specific
to the clang-offload-bundler tool, from the API, and replace
its use with an ObjcopyPath variable. This variable must be set
in order to internally call llvm-objcopy.

Finally, we move the API files from
clang/tools/clang-offload-bundler into clang/lib/Driver and
clang/include/clang/Driver.

Differential Revision: https://reviews.llvm.org/D129873
2022-07-26 10:05:22 -07:00