1326 Commits

Author SHA1 Message Date
Alex MacLean
7daa65a088
Reland "[NVPTX] Use .common linkage for common globals" (#86824)
Switch from `.weak` to `.common` linkage for common global variables
where possible. The `.common` linkage is described in
[PTX ISA 11.6.4. Linking Directives: .common]
(https://docs.nvidia.com/cuda/parallel-thread-execution/index.html#linking-directives-common)
> Declares identifier to be globally visible but “common”.
>
>Common symbols are similar to globally visible symbols. However
multiple object files may declare the same common symbol and they may
have different types and sizes and references to a symbol get resolved
against a common symbol with the largest size.
>
>Only one object file can initialize a common symbol and that must have
the largest size among all other definitions of that common symbol from
different object files.
>
>.common linking directive can be used only on variables with .global
storage. It cannot be used on function symbols or on symbols with opaque
type.

I've updated the logic and tests to only use `.common` for PTX 5.0 or
greater and verified that the new tests now pass with `ptxas`.
2024-03-29 11:58:41 -07:00
Alex MacLean
b1a633bc01
[NFC][NVPTX] remove truncating c-style cast (#85889)
While a stack size large enough to cause this truncation to be a problem
would certainly cause other issues and not produce a valid program
anyway, this cast is triggering our Coverity static analysis. Removing
it seems cleaner.
2024-03-26 16:39:32 -07:00
paperchalice
7ac7d418ac
[NewPM][NVPTX] Add NVPTXPassRegistry.def NFCI (#86246)
Prepare for dag-isel migration.
2024-03-23 11:20:18 +08:00
Alex MacLean
de159aeca9
[NVPTX][DebugInfo] avoid emitting extra .loc directives (#84584)
This change removes an extra, unneeded debug directive emitted in the
PTX at the beginning on non-empty functions:
```nvptx
.visible .func  (.param .b32 func_retval0) foo(
        .param .b32 foo_param_0,
        .param .b32 foo_param_1
)
{
        .reg .b32    %r<4>;
        .loc    1 26 0             <---- unneeded (removed by the PR)
$L__func_begin0:
        .loc    1 26 0
```
2024-03-19 18:32:26 -07:00
Alex MacLean
888e284903
[NVPTX] Use PTX prmt for llvm.bswap (#85545) 2024-03-19 15:18:53 -07:00
Jeremy Morse
b9d83eff25
[NFC][RemoveDIs] Use iterators for insertion at various call-sites (#84736)
These are the last remaining "trivial" changes to passes that use
Instruction pointers for insertion. All of this should be NFC, it's just
changing the spelling of how we identify a position.

In one or two locations, I'm also switching uses of getNextNode etc to
using std::next with iterators. This too should be NFC.

---------

Merged by: Stephen Tozer <stephen.tozer@sony.com>
2024-03-19 16:36:29 +00:00
Sterling Augustine
d4a8e979e4 Revert "[NVPTX] Use .common linkage for common globals (#84416)"
This reverts commit 8f0012d3dc2ae6d40e9f812cae111ca7a6eb2a2d.

The common-linkage.ll test fails with ptxas enabled.
2024-03-15 21:27:46 +00:00
Alex MacLean
89b7b3b995
[NVPTX] support dynamic allocas with PTX alloca instruction (#84585)
Add support for dynamically sized alloca instructions with the PTX
alloca instruction introduced in PTX 7.3 
([9.7.15.3. Stack Manipulation Instructions: alloca]
(https://docs.nvidia.com/cuda/parallel-thread-execution/index.html#stack-manipulation-instructions-alloca))
2024-03-15 11:51:46 -07:00
Adrian Kuegel
4372cab914
Reland "[NVPTX] Add support for atomic add for f16 type" (#85197)
atom.add.noftz.f16 is supported since SM 7.0
2024-03-15 08:13:59 +01:00
Alex MacLean
8f0012d3dc
[NVPTX] Use .common linkage for common globals (#84416)
Switch from `.weak` to `.common` linkage for common global variables
where possible. The `.common` linkage is described in [PTX ISA 11.6.4.
Linking Directives:
.common](https://docs.nvidia.com/cuda/parallel-thread-execution/index.html#linking-directives-common)
> Declares identifier to be globally visible but “common”.
>
>Common symbols are similar to globally visible symbols. However
multiple object files may declare the same common symbol and they may
have different types and sizes and references to a symbol get resolved
against a common symbol with the largest size.
>
>Only one object file can initialize a common symbol and that must have
the largest size among all other definitions of that common symbol from
different object files.
>
>.common linking directive can be used only on variables with .global
storage. It cannot be used on function symbols or on symbols with opaque
type.
2024-03-14 13:30:02 -07:00
Danial Klimkin
afd4758703
Revert "[NVPTX] Add support for atomic add for f16 type" (#84918)
Reverts llvm/llvm-project#84295 due to breakages.
2024-03-12 15:01:18 +01:00
Adrian Kuegel
8e0f4b943f
[NVPTX] Add support for atomic add for f16 type (#84295)
atom.add.noftz.f16 is supported since SM 7.0
2024-03-12 09:12:44 +01:00
Benjamin Kramer
20895965b2 [NVPTX] Remove sub.s16x2 instruction
According to the PTX ISA this doesn't exist (and ptxas rejects it)

See https://github.com/pytorch/pytorch/issues/118589
2024-03-05 12:43:02 +01:00
Rishabh Bali
fe42e72db2
[CodeGen] Port AtomicExpand to new Pass Manager (#71220)
Port the `atomicexpand` pass to the new Pass Manager. 
Fixes #64559
2024-02-25 18:42:22 +05:30
Alex MacLean
590c968e79
[NVPTX] fixup support for unaligned parameters and returns (#82562)
Add support for unaligned parameters and return values. These must be
loaded and stored one byte at a time and then bit manipulation is used
to assemble the correct final result.
2024-02-22 17:27:28 -08:00
David Majnemer
cb1fed3a89 [NVPTX] Correctly guard int -> bf16 on PTX version and SM version 2024-02-21 20:26:07 +00:00
David Majnemer
966b026785 [NVPTX] Simplify handling of ISD::BF16_TO_FP
We only use it to get from BF16 to F32. After that point, we insert
an FP_EXTEND to get the rest of the way.
2024-02-21 18:32:32 +00:00
David Majnemer
6f0e39c423 [NVPTX] Set ISD::FP_{EXTEND,ROUND} to Custom for more types
Sometimes those nodes are queried with the non-bf16. We need to request
to SDAG that we want to handle the non-bf16 side so that the handler can
detect if bf16 is being used on either side.
2024-02-21 18:26:37 +00:00
David Majnemer
cc13f3ba45
Correctly round FP -> BF16 when SDAG expands such nodes (#82399)
We did something pretty naive:
- round FP64 -> BF16 by first rounding to FP32
- skip FP32 -> BF16 rounding entirely
- taking the top 16 bits of a FP32 which will turn some NaNs into
infinities

Let's do this in a more principled way by rounding types with more
precision than FP32 to FP32 using round-inexact-to-odd which will negate
double rounding issues.
2024-02-21 12:37:02 -05:00
Joseph Huber
11fcae69db
[LLVM] Add __builtin_readsteadycounter intrinsic and builtin for realtime clocks (#81331)
Summary:
This patch adds a new intrinsic and builtin function mirroring the
existing `__builtin_readcyclecounter`. The difference is that this
implementation targets a separate counter that some targets have which
returns a fixed frequency clock that can be used to determine elapsed
time, this is different compared to the cycle counter which often has
variable frequency.

This patch only adds support for the NVPTX and AMDGPU targets.

This is done as a new and separate builtin rather than an argument to
`readcyclecounter` to avoid needing to change existing code and to make
the separation more explicit.
2024-02-13 10:06:25 -06:00
Artem Belevich
8799d7143f
[NVPTX] Fix the error in a pattern match in v4i8 comparisons. (#81308)
The replacement should've had BFE() as the arguments for the comparison,
not the source register.

While at that, tighten the patterns a bit, and expand them to cover
variants with immediate arguments. Also change the default lowering of
bfe() to use unsigned variant, so the value of the upper bits is
predictable.
2024-02-12 12:59:03 -08:00
Joseph Huber
2ac8e6b7f5
[NVPTX] Implement __builtin_readcyclecounter on NVPTX (#81344)
Summary:
This patch simply states that `__builtin_readcyclecounter` is legal on
NVPTX and makes it  return the value from the `clock64` sreg. The timer
intrinsics are marked as having side effects, which is desireable for
timing primitives and required to pattern match the instrinic DAG.
2024-02-12 07:07:48 -06:00
Petr
45260bf23b
Fix use after free error in NVVMReflect (#81471)
I have a Triton kernel, which triggered a heap-use-after-free error in
LLVM.

The problem was that the same instruction may be added to the
`ToSimplify` array multiple times. If this duplicate instruction is
trivially dead, it gets deleted on the first pass. Then, on the second
pass, the freed instruction is passed.

To fix this, I'm adding the instructions to the `ToRemove` array and
filter it out for duplicates to avoid possible double frees.
2024-02-12 13:50:00 +01:00
Adrian Kuegel
da9559d69a
Do not use PerformEXTRACTCombine for v8i8 types (#81242)
Same as with v4i8 types, we should not be using PerformEXTRACTCombine
for v8i8 types.
2024-02-12 07:31:31 +01:00
Joseph Huber
07dc85ba0c
[NVVMReflect] Improve folding inside of the NVVMReflect pass (#81253)
Summary:
The previous patch did very simple folding that only worked for driectly
used branches. This patch improves this by traversing the use-def chain
to sipmlify every constant subexpression until it reaches a terminator
we can delete. The support should work for all expected cases now.
2024-02-09 13:39:03 -06:00
Joseph Huber
ffabcbcf8f [NVVMReflect][Reland] Force dead branch elimination in NVVMReflect (#81189)
Summary:
The `__nvvm_reflect` function is used to guard invalid code that varies
between architectures. One problem with this feature is that if it is
used without optimizations, it will leave invalid code in the module
that will then make it to the backend. The `__nvvm_reflect` pass is
already mandatory, so it should do some trivial branch removal to ensure
that constants are handled correctly. This dead branch elimination only
works in the trivial case of a compare on a branch and does not touch
any conditionals that were not realted to the `__nvvm_reflect` call in
order to preserve `O0` semantics as much as possible. This should allow
the following to work on NVPTX targets

```c
int foo() {
  if (__nvvm_reflect("__CUDA_ARCH") >= 700)
    asm("valid;\n");
}
```

Relanding after fixing a bug.
2024-02-08 20:09:44 -06:00
Joseph Huber
0800a36053 Revert "[NVVMReflect] Force dead branch elimination in NVVMReflect (#81189)"
This reverts commit 9211e67da36782db44a46ccb9ac06734ccf2570f.

Summary:
This seemed to crash one one of the CUDA math tests. Revert until it can
be fixed.
2024-02-08 17:32:04 -06:00
Joseph Huber
9211e67da3
[NVVMReflect] Force dead branch elimination in NVVMReflect (#81189)
Summary:
The `__nvvm_reflect` function is used to guard invalid code that varies
between architectures. One problem with this feature is that if it is
used without optimizations, it will leave invalid code in the module
that will then make it to the backend. The `__nvvm_reflect` pass is
already mandatory, so it should do some trivial branch removal to ensure
that constants are handled correctly. This dead branch elimination only
works in the trivial case of a compare on a branch and does not touch
any conditionals that were not realted to the `__nvvm_reflect` call in
order to preserve `O0` semantics as much as possible. This should allow
the following to work on NVPTX targets

```c
int foo() {
  if (__nvvm_reflect("__CUDA_ARCH") >= 700)
    asm("valid;\n");
}
```
2024-02-08 17:16:31 -06:00
Alex MacLean
9affa177b5
[NVPTX] Add support for calling aliases (#81170)
The current implementation of aliases tries to remove all the aliases in
the module to prevent the generic version of `AsmPrinter` from emitting
them incorrectly. Unfortunately, if the aliases are used this will fail.
Instead let's override the function to print aliases directly.

In addition, the declarations of the alias functions must occur before
the uses. To fix this we emit alias declarations as part of
`emitDeclarations` and only emit the `.alias` directives at the end
(where we can assume the aliasee has also already been declared).
2024-02-08 17:14:13 -06:00
Alex MacLean
4b54b474aa
[NVPTX][NFC] cleanup dead vars, use MAKE_CASE (#81161)
Cleanup some dead variables. In addition, switch to a `MAKE_CASE` macro,
similar to other targets, to reduce boilerplate.
2024-02-08 13:53:18 -08:00
Philip Reames
3ff7caea33
[TTI] Use Register in isLoadFromStackSlot and isStoreToStackSlot [nfc] (#80339) 2024-02-01 17:52:35 -08:00
Alex MacLean
5e3ae4c4af
[NVPTX] improve Boolean ISel (#80166)
Add TableGen patterns to convert more instructions to boolean
expressions:

- **mul -> and/or**: i1 multiply instructions currently cannot be
selected causing the compiler to crash. See
https://github.com/llvm/llvm-project/issues/57404
- **select -> and/or**: Converting selects to and/or can enable more
optimizations. `InstCombine` cannot do this as aggressively due to
poison semantics.
2024-01-31 14:37:27 -08:00
Justin Fargnoli
577738a12d
Revert "Disable incorrect peephole optimizations" (#79916)
This reverts commit ff77058141e8026357ca514ad0d45c6c50921290.
2024-01-29 16:22:07 -08:00
Justin Fargnoli
ff77058141
Disable incorrect peephole optimizations 2024-01-29 15:54:40 -08:00
Joseph Huber
e633807a1f
[NVPTX] Add builtin support for 'globaltimer' (#79765)
Summary:
This patch adds support for `globaltimer` to match `clock` and
`clock64`. See the PTX ISA reference for details. This patch does not
implement the `hi` or `lo` variants for brevity as they can be obtained
from this with the cost of an additional register.

https://docs.nvidia.com/cuda/parallel-thread-execution/index.html#special-registers-globaltimer-globaltimer-lo-globaltimer-hi
2024-01-29 14:11:54 -06:00
Joseph Huber
ea8014046c
[NVPTX] Add builtin for 'exit' handling (#79777)
Summary:
The PTX ISA has always supported the 'exit' instruction to terminate
individual threads. This patch adds a builtin to handle it. See the PTX
documentation for further details.

https://docs.nvidia.com/cuda/parallel-thread-execution/index.html#control-flow-instructions-exit
2024-01-29 14:09:34 -06:00
Joseph Huber
5f12cc912a
[NVPTX] Add builtin support for 'nanosleep' PTX instrunction (#79888)
Summary:
This patch adds a builtin for the `nanosleep` PTX function. It takes
either an immediate or a register and sleeps for [0, 2t] nanoseconds
given t. More information at the documentation:

https://docs.nvidia.com/cuda/parallel-thread-execution/index.html#miscellaneous-instructions-nanosleep
2024-01-29 14:07:58 -06:00
Joseph Huber
d492faa7aa
[NVPTX] Add 'activemask' builtin and intrinsic support (#79768)
Summary:
This patch adds support for getting the 'activemask' instruction's value
without needing to use inline assembly. See the relevant PTX reference
for details.


https://docs.nvidia.com/cuda/parallel-thread-execution/index.html#parallel-synchronization-and-communication-instructions-activemask
2024-01-29 14:07:30 -06:00
Alex MacLean
1d5820aafe
[NVPTX] improve identifier renaming for PTX (#79459)
Update `NVPTXAssignValidGlobalNames` to convert all characters which are
illegal in PTX identifiers to `_$_`. ([PTX ISA: 4.4
Identifiers](https://docs.nvidia.com/cuda/parallel-thread-execution/index.html#identifiers)).
2024-01-26 13:49:00 -08:00
Nico Weber
184ca39529
[llvm] Move CodeGenTypes library to its own directory (#79444)
Finally addresses https://reviews.llvm.org/D148769#4311232 :)

No behavior change.
2024-01-25 12:01:31 -05:00
Alex MacLean
3b8539c9dc
[NVPTX] use incomplete aggregate initializers (#79062)
The PTX ISA specifies that initializers may be incomplete ([5.4.4.
Initializers](https://docs.nvidia.com/cuda/parallel-thread-execution/index.html#initializers))
> As in C, array initializers may be incomplete, i.e., the number of
initializer elements may be less than the extent of the corresponding
array dimension, with remaining array locations initialized to the
default value for the specified array type.

Emitting initializers in this form is preferable because it reduces the
size of the PTX, in some cases significantly, and can improve compile
time of ptxas as a result.
2024-01-24 09:24:28 -08:00
Durgadoss R
43531e7196
[LLVM][NVPTX] Add cp.async.bulk.commit/wait intrinsics (#78698)
This patch adds NVVM intrinsics and NVPTX codegen for the bulk variants
of the async-copy commit/wait instructions.
lit tests are added to verify the generated PTX.

PTX Doc link:

https://docs.nvidia.com/cuda/parallel-thread-execution/index.html#data-movement-and-conversion-instructions-cp-async-bulk-commit-group

Signed-off-by: Durgadoss R <durgadossr@nvidia.com>
2024-01-19 10:42:33 -08:00
Alex MacLean
0c195e5096
[NVPTX][NFC] Remove unused parameter of getArgumentAlignment (#78604) 2024-01-18 10:08:56 -08:00
Alex MacLean
430a40d12e
[NVPTX] extend type support for nvvm.{min,max,mulhi,sad} (#78385)
Ensure intrinsics and auto-upgrades support i16, i32, and i64 for for
`nvvm.{min,max,mulhi,sad}`

- `nvvm.min` and `nvvm.max`: These are auto-upgraded to `select`
instructions but it is still nice to support the 16 bit variants just in
case any generators of IR are still trying to use these intrinsics.
- `nvvm.sad` added both the 16 and 64 bit variants, also marked this
instruction as speculateble. These directly correspond to the PTX
`sad.{u16,s16,u64,s64}` instructions.
- `nvvm.mulhi` added the 16 bit variants. These directly correspond to
the PTX `mul.hi.{s,u}16` instructions.
2024-01-17 16:18:39 -08:00
Alex MacLean
da7462a6ae
[NVPTX] Add tex.grad.cube{array} intrinsics (#77693)
Extend IR support for PTX `tex` instruction described in [PTX ISA.
9.7.9.3. Texture Instructions:
tex](https://docs.nvidia.com/cuda/parallel-thread-execution/index.html#texture-instructions-tex).
Add support for unified-move versions of `tex.grad.cube{array}` variants
added in PTX ISA 4.3.
2024-01-17 10:41:11 -08:00
mmoadeli
aa23e493f2
[NVPTX] Fix generating permute bytes from register pair when the initial values are undefined (#74437)
When generating the permute bytes for the prmt instruction, the
existence of an undefined initial value initialises the int32 that holds
the mask with all 1's (0xFFFFFFFF). That initialization subsequently
leads to complications during the subsequent OR operation, leading to
inaccuracies in populating mask values for the following bytes.
Consequently, the final value persists as a constant -1, irrespective of
the actual mask values that succeed the initial set value.
2024-01-16 11:05:41 -08:00
Kazu Hirata
7528cf5ef2 [Target] Use getConstantOperandVal (NFC) 2024-01-14 00:53:29 -08:00
Durgadoss R
8d817f6479
[LLVM][NVPTX]: Add aligned versions of cluster barriers (#77940) 2024-01-13 10:41:19 +01:00
Kazu Hirata
be76f1646f [Target] Use getConstantOperandAPInt (NFC) 2024-01-10 21:06:01 -08:00
Durgadoss R
340cc1702e
[LLVM][NVPTX]: Add intrinsic for setmaxnreg (#77289)
This patch adds an intrinsic for setmaxnreg PTX instruction.
* PTX Doc link for this instruction:
https://docs.nvidia.com/cuda/parallel-thread-execution/index.html#miscellaneous-instructions-setmaxnreg

* The i32 argument, an immediate value, specifies the actual
  absolute register count for the instruction.
* The `setmaxnreg` instruction is available in SM90a.
  So, this patch adds 'hasSM90a' predicate to use in
  the NVPTX backend.
* lit tests are added to verify the lowering of the intrinsic.
* Verifier logic (and tests) are added to test the register
  count range and divisibility-by-8 requirements.

Signed-off-by: Durgadoss R <durgadossr@nvidia.com>
2024-01-09 12:04:13 -08:00