31 Commits

Author SHA1 Message Date
Pierre van Houtryve
b79ba02479
[AMDGPU][GFX12.5] Reimplement monitor load as an atomic operation (#177343)
Load monitor operations make more sense as atomic operations, as
non-atomic operations cannot be used for inter-thread communication w/o
additional synchronization.
The previous built-in made it work because one could just override the
CPol bits, but that bypasses the memory model and forces the user to learn
about ISA bits encoding.

Making load monitor an atomic operation has a couple of advantages.
First, the memory model foundation for it is stronger. We just lean on the
existing rules for atomic operations. Second, the CPol bits are abstracted away
from the user, which avoids leaking ISA details into the API.

This patch also adds supporting memory model and intrinsics
documentation to AMDGPUUsage.

Solves SWDEV-516398.
2026-02-09 09:57:27 +01:00
Matt Arsenault
0c583e784e
AMDGPU: Add llvm.amdgcn.s.wait.event intrinsic (#180170)
Exactly match the s_wait_event instruction. For some reason we already
had this instruction used through llvm.amdgcn.s.wait.event.export.ready,
but that hardcodes a specific value. This should really be a bitmask
that
can combine multiple wait types.

gfx11 -> gfx12 broke compatabilty in a weird way, by inverting the
interpretation of the bit but also shifting the used bit by 1. Simplify
the selection of the old intrinsic by just using the magic number 2,
which should satisfy both cases.
2026-02-09 08:45:13 +01:00
Shilei Tian
4efbe98659
[Clang][AMDGPU] Add a Sema check for the imm argument of __builtin_amdgcn_s_setreg (#176838)
Our backend cannot select the corresponding intrinsic if the imm
argument is not a `int16_t` or `uint16_t`, which is not really helpful.
2026-01-20 11:48:52 -05:00
Yaxun (Sam) Liu
c752e12b76
[AMDGPU] Fix builtin crash with template parameter size argument (#175767)
## Summary

`__builtin_amdgcn_global_load_lds` and related builtins crash when the
size argument is a template parameter. The semantic checker calls
`VerifyIntegerConstantExpression` without first checking if the
expression is value-dependent, causing an assertion failure.

**Affected builtins:**
- `__builtin_amdgcn_global_load_lds`
- `__builtin_amdgcn_raw_ptr_buffer_load_lds`
- `__builtin_amdgcn_struct_ptr_buffer_load_lds`
- `__builtin_amdgcn_load_to_lds`

**Reproducer:**
```cpp
template<int N>
__device__ void load(void* src, __shared__ void* dst) {
    __builtin_amdgcn_global_load_lds(src, dst, N, 0, 0);  // Crashes
}
template __device__ void load<16>(void*, __shared__ void*);
```

**Error before fix:**
```
Assertion `!isValueDependent() && "Expression evaluator can't be called on a dependent expression."' failed
```

**Fix:** Add `isValueDependent()` check before evaluating the constant
expression. Value-dependent expressions are accepted during template
definition and properly checked during template instantiation.

## Test plan

- [x] New lit test
`clang/test/SemaHIP/amdgpu-global-load-lds-template.hip`
  - Tests valid template parameter sizes compile without errors
  - Tests invalid sizes are caught at template instantiation
- [x] All existing SemaHIP tests pass (17/17)
- [x] Related builtin tests pass
2026-01-13 13:10:02 -05:00
Shilei Tian
5a63367b15
Reapply "[AMDGPU] Rework the clamp support for WMMA instructions" (#174674) (#174697)
This reverts commit 0b2f3cfb72a76fa90f3ec2a234caabe0d0712590.
2026-01-07 06:12:19 +00:00
dyung
0b2f3cfb72
Revert "[AMDGPU] Rework the clamp support for WMMA instructions" (#174674)
Reverts llvm/llvm-project#174310

This change is causing 2 cross-project-test failures on
https://lab.llvm.org/buildbot/#/builders/174/builds/29695
2026-01-07 01:18:23 +00:00
Shilei Tian
ccca3b8c67
[AMDGPU] Rework the clamp support for WMMA instructions (#174310)
Fixes #166989.
2026-01-06 15:46:40 -05:00
Shilei Tian
c97de4387b
Revert "[AMDGPU] add clamp immediate operand to WMMA iu8 intrinsic (#171069)" (#174303)
This reverts commit 2c376ffeca490a5732e4fd6e98e5351fcf6d692a because it
breaks assembler.

```
$ llvm-mc -triple=amdgcn -mcpu=gfx1250 -show-encoding <<< "v_wmma_i32_16x16x64_iu8 v[16:23], v[0:7], v[8:15], v[16:23] matrix_b_reuse"
  v_wmma_i32_16x16x64_iu8 v[16:23], v[0:7], v[8:15], v[16:23] clamp ; encoding: [0x10,0x80,0x72,0xcc,0x00,0x11,0x42,0x1c]
```

We have a fundamental issue in the clamp support in VOP3P instructions,
which will need more changes.
2026-01-04 02:13:21 +00:00
Muhammad Abdul
2c376ffeca
[AMDGPU] add clamp immediate operand to WMMA iu8 intrinsic (#171069)
Fixes #166989 

- Adds a clamp immediate operand to the AMDGPU WMMA iu8 intrinsic and
threads it through LLVM IR, MIR lowering, Clang builtins/tests, and MLIR
ROCDL dialect so all layers agree on the new operand
- Updates AMDGPUWmmaIntrinsicModsAB so the clamp attribute is emitted,
teaches VOP3P encoding to accept the immediate, and adjusts Clang
codegen/builtin headers plus MLIR op definitions and tests to match
- Documents what the WMMA clamp operand do
- Implement bitcode AutoUpgrade for source compatibility on WMMA IU8
Intrinsic op

Possible future enhancements:
- infer clamping as an optimization fold based on the use context

---------

Co-authored-by: Matt Arsenault <arsenm2@gmail.com>
2025-12-27 12:51:29 -05:00
Matheus Izvekov
d2f75f2fe3
[clang] SFINAE context refactor (#164703)
This teases the SFINAE handling bits out of the CodeSynthesisContext,
and moves that functionality into SFINAETrap and a new class.

There is also a small performance benefit here:
<img width="1460" height="20" alt="image"
src="https://github.com/user-attachments/assets/aeb446e3-04c3-418e-83de-c80904c83574"
/>
2025-11-06 09:39:18 -10:00
Rana Pratap Reddy
24c75a21b8
[AMDGPU][Clang] Support for type inferring extended image builtins for AMDGPU (#164358)
Introduces the builtins for extended image insts for amdgcn.
2025-10-30 22:20:28 +05:30
Jordan Rupprecht
5391c6849f
[AMDGPU][NFC] Fix clang frontend<->sema layering issue (#162865)
#140210 added `#include "clang/Frontend/FrontendDiagnostic.h"` to
clang/lib/Sema/SemaAMDGPU.cpp, but Frontend itself has a dependency on
Sema. This creates a layering issue as described in
https://llvm.org/docs/CodingStandards.html#library-layering.

Fortunately, d076608d58d1ec55016eb747a995511e3a3f72aa made this easy to
fix, as clang/Frontend/FrontendDiagnostic.h just forwards to
clang/Basic/DiagnosticFrontend.h, so it's trivial to make this depend on
basic instead of frontend.
2025-10-10 15:44:51 +00:00
Rana Pratap Reddy
f2ffb4d20b
[AMDGPU] Support for type inferring image load/store builtins for AMDGPU (#140210)
Introduces the builtins for amdgcn_image_load/store/sample.
2025-10-10 15:26:08 +05:30
Juan Manuel Martinez Caamaño
efa907e899
[Clang][AMDGPU] Defer amdgcn.*to_lds builtins size diagnostics (#160140)
Before, the diagnostic was emitted immediately, as soon as the error was
detected. This is problematic during the host compilation, since
the compiler performs semantic analysis of `__device__` functions with
the host's
target attributes.

A solution for this is to use `SemaRef.targetDiag` to defer the
diagnostic. The diagnostic will then be printed only if the function is
emitted.

The test included in this patch highlights a second problem: we cannot
compile a file having a call to `__builtin_amdgcn_load_to_lds` on a
`__device__` function since we typecheck the signature.
The issue is that, `__shared__ void*` on X86 doesn't translate to
`addrspace(3) void*`, so the compilation fails.

Fixes: SWDEV-555501
2025-09-25 09:12:37 +02:00
Juan Manuel Martinez Caamaño
9fa23e43fe
[NFC][Clang] Fix typo memody->memory (#160141)
Fix typo memody->memory
2025-09-22 19:23:20 +02:00
Shilei Tian
158eeb344b
[AMDGPU] Change scale_sel to be 4 bits (#157900)
The latest SP changes updated it to use `OP_SEL[0:3]` instead of
`OP_SEL[0:2]`.

Fixes SWDEV-554472.
2025-09-16 15:36:45 -04:00
Pierre van Houtryve
e2bd10cf16
[AMDGPU][gfx1250] Add 128B cooperative atomics (#156418)
- Add clang built-ins + sema/codegen
- Add IR Intrinsic + verifier
- Add DAG/GlobalISel codegen for the intrinsics
- Add lowering in SIMemoryLegalizer using a MMO flag.
2025-09-04 09:19:25 +00:00
Piotr Sobczak
19803d8249
[AMDGPU] Fix nested cases (#151918)
The Incorrect nesting was added in #151616 in what looks like a merge
error.
2025-08-04 10:58:19 +02:00
Stanislav Mekhanoshin
7598c25b5a
[AMDGPU] v_cvt_scale_pk16 gfx1250 instructions (#151804) 2025-08-02 10:45:02 -07:00
Stanislav Mekhanoshin
c7bb105e97
[AMDGPU] Add v_cvt_scale_pk8_* gfx1250 instructions (#151616) 2025-07-31 18:55:59 -07:00
zGoldthorpe
85349b4936
[clang][amdgpu] Add builtin for struct buffer lds load (#148950)
This is essentially just a revision of #137678 which only exposes a
builtin for the intrinsic `llvm.amdgcn.struct.ptr.buffer.load.lds`,
which expects an `__amdgpu_buffer_rsrc_t` rather than a `v4i32` as its
first argument.

The reason for excluding the other intrinsics exposed by the cited PR is
because the intrinsics taking a `v4i32` are legacy and should be
deprecated.
2025-07-16 07:23:09 -06:00
Krzysztof Drewniak
4bdd116b80
[AMDGPU] Add a new amdgcn.load.to.lds intrinsic (#137425)
This PR adds a amdgns_load_to_lds intrinsic that abstracts over loads to
LDS from global (address space 1) pointers and buffer fat pointers
(address space 7), since they use the same API and "gather from a
pointer to LDS" is something of an abstract operation.

This commit adds the intrinsic and its lowerings for addrspaces 1 and 7,
and updates the MLIR wrappers to use it (loosening up the restrictions
on loads to LDS along the way to match the ground truth from target
features).

It also plumbs the intrinsic through to clang.
2025-05-19 07:15:04 -07:00
Juan Manuel Martinez Caamaño
d995b2ebdc
[Clang][AMDGPU] Accept builtins in lambda declarations (#135027)
`Sema::getCurFunctionDecl(AllowLambda = false)` returns a nullptr when
the lambda declaration is outside a function (for example, when
assigning a lambda to a static constexpr variable).

This triggered an assertion in
`SemaAMDGPU::CheckAMDGCNBuiltinFunctionCall`.

Using `Sema::getCurFunctionDecl(AllowLambda = true)` returns the
declaration of the enclosing lambda.

Stumbled with this issue when refactoring some code in CK.
2025-04-11 19:11:46 +02:00
Juan Manuel Martinez Caamaño
041e84261a
[Clang][AMDGPU] Expose buffer load lds as a clang builtin (#132048)
CK is using either inline assembly or inline LLVM-IR builtins to
generate buffer_load_dword lds instructions.

This patch exposes this instruction as a Clang builtin available on gfx9 and gfx10.

Related to SWDEV-519702 and SWDEV-518861
2025-04-03 09:22:38 +02:00
Matt Arsenault
a2c3e0c4cb
AMDGPU/clang: Add global_load_lds size check support for gfx950 (#117825)
Co-authored-by: Shilei Tian <shilei.tian@amd.com>
2024-11-26 23:41:09 -05:00
Stanislav Mekhanoshin
ba1a09da8d
[AMDGPU] Allow overload of __builtin_amdgcn_mov_dpp8 (#113610)
The same handling as for __builtin_amdgcn_mov_dpp.
2024-10-31 02:19:20 -07:00
Stanislav Mekhanoshin
03fef62b84
[AMDGPU] Relax __builtin_amdgcn_update_dpp sema check (#113341)
Recent change applied too strict check for old and src operands match.
These shall be compatible, but not necessarily exactly the same.

Fixes: SWDEV-493072
2024-10-22 12:32:08 -07:00
Stanislav Mekhanoshin
622e398d88
[AMDGPU] Allow overload of __builtin_amdgcn_mov/update_dpp (#112447)
We need to support 64-bit data types (intrinsics do support it). We are
also silently converting FP to integer argument now, also fixed.
2024-10-21 11:57:18 -07:00
Haojian Wu
79393124ff Fix -Wunused-variable in SemaAMDGPU.cpp in release build, NFC 2024-06-06 15:48:23 +02:00
Shilei Tian
170d45c0eb
[Clang][AMDGPU] Use I to decorate imm argument for __builtin_amdgcn_global_load_lds (#94376) 2024-06-06 09:29:59 -04:00
Vlad Serebrennikov
ed35a92c40
[clang] Introduce target-specific Sema components (#93179)
This patch introduces `SemaAMDGPU`, `SemaARM`, `SemaBPF`, `SemaHexagon`,
`SemaLoongArch`, `SemaMIPS`, `SemaNVPTX`, `SemaPPC`, `SemaSystemZ`,
`SemaWasm`. This continues previous efforts to split Sema up. Additional
context can be found in #84184 and #92682.

I decided to bundle target-specific components together because of their
low impact on `Sema`. That said, their impact on `SemaChecking.cpp` is
far from low, and I consider it a success.

Somewhat accidentally, I also moved Wasm- and AMDGPU-specific function
from `SemaDeclAttr.cpp`, because they were exposed in `Sema`. That went
well, and I consider it a success, too. I'd like to move the rest of
static target-specific functions out of `SemaDeclAttr.cpp` like we're
doing with built-ins in `SemaChecking.cpp` .
2024-05-30 19:59:59 +04:00