18 Commits

Author SHA1 Message Date
Paul Walker
96d2cb4145
[LLVM][CodeGen][DirectX] Fix scalarisation when vector ConstantFP is used. (#172684)
When using -use-constant-fp-for-fixed-length-splat `splat (float C)`
becomes ConstantFP(C) rather than ConstantVector(C, C, C...).
2026-02-05 13:18:38 +00:00
Jameson Nash
1721b9ff61
[DirectX] remove getAllocatedType in DXILDataScalarization (#179067)
Update dynamicallyLoadArray to take the allocated type as a parameter
instead of querying getAllocatedType. This is to facilitate removing
other incorrect uses of getAllocatedType, and eventually possibly even
getAllocatedType itself.

Co-authored-by: Claude Sonnet 4.5 <noreply@anthropic.com>
2026-02-01 14:46:30 -05:00
Deric C.
f5e228b32a
[DirectX] Simplify DXIL data scalarization, and data scalarize whole GEP chains (#168096)
- The DXIL data scalarizer only needs to change vectors into arrays. It
does not need to change the types of GEPs to match the pointer type.
This PR simplifies the `visitGetElementPtrInst` method to do just that
while also accounting for nested GEPs from ConstantExprs. (Before this
PR, there were still vector types lingering in nested GEPs with
ConstantExprs.)
- The `equivalentArrayTypeFromVector` function was awkwardly placed near
the top of the file and away from the other helper functions. The
function is now moved next to the other helper functions.
- Removed an unnecessary `||` condition from `isVectorOrArrayOfVectors`

Related tests have also been cleaned up, and the test CHECKs have been
modified to account for the new simplified behavior.
2025-11-24 10:56:20 -08:00
Finn Plummer
75c09b7924
[DirectX] Let data scalarizer pass account for sub-types when updating GEP type (#166200)
This pr lets the `dxil-data-scalarization` account for a GEP with a
source type that is a sub-type of the pointer operand type.

The pass is updated so that the replaced GEP introduces zero indices
such that the result type remains the same (with the vector -> array
transform).

Please see resolved issue for an annotated example.

Resolves: https://github.com/llvm/llvm-project/issues/165473
2025-11-06 09:14:50 -08:00
Deric C.
13a634281f
[DirectX] Fix the writing of ConstantExpr GEPs to DXIL bitcode (#154446)
Fixes #153304

Changes:
- When writing `ConstantExpr` GEPs to DXIL bitcode, the bitcode writer
will use the old Constant Code `CST_CODE_CE_GEP_OLD = 12` instead of the
newer `CST_CODE_CE_GEP = 32` which is interpreted as an undef in DXIL.
Additional context: [CST_CODE_CE_GEP = 12 in
DXC](0c9e75e7e9/include/llvm/Bitcode/LLVMBitCodes.h (L187))
while the same constant code is labeled [CST_CODE_CE_GEP_OLD in
LLVM](65de318d18/llvm/include/llvm/Bitcode/LLVMBitCodes.h (L411))
- Modifies the `PointerTypeAnalysis` to be able to analyze pointer-typed
constants that appear in the operands of instructions so that the
correct type of the `ConstantExpr` GEP is determined and written into
the DXIL bitcode.
- Adds a `PointerTypeAnalysis` test and dxil-dis test to ensure that the
pointer type of `ConstantExpr` GEPs are resolved and `ConstantExpr` GEPs
are written to DXIL bitcode correctly

In addition, this PR also adds a missing call to
`GV.removeDeadConstantUsers()` in the DXILFinalizeLinkage pass, and
removes an unnecessary manual removal of a ConstantExpr in the
DXILFlattenArrays pass.
2025-08-26 10:01:59 -07:00
Farzon Lotfi
c73c19c60e
[DirectX] Support ConstExpr GEPs (#150082)
- Fixes #150050
- Address the issue of many nested geps
- Check for ConstantExpr GEP if we see it check if it needs a global
replacement with a new type. Create the new constExpr Gep and set the
pointer operand to it. Finally cleanup and remove the old nested geps.
2025-07-23 16:38:14 -04:00
Deric C.
a4dab509d3
[DirectX] Replace getNextNonDebugInstruction with getNextNode (#148890)
Fixes the DirectX backend build failure due to #144383
2025-07-15 09:58:56 -07:00
Deric C.
27b3b4a665
[DirectX] Move the scalarizer pass to before dxil-flatten-arrays (#146800)
Fixes #145924 and #140416
Depends on #146173 being merged first.

This PR moves the scalarizer pass to immediately before the
dxil-flatten-arrays pass to allow the dxil-flatten-arrays pass to turn
scalar GEPs (including i8 GEPs) into flattened array GEPs where
applicable.

A number of LLVM DirectX CodeGen tests have been edited to remove scalar
GEPs and also correct previously uncaught incorrectly-transformed GEPs.

No more validation errors of the form `Access to out-of-bounds memory is
disallowed` or `TGSM pointers must originate from an unambiguous TGSM
global variable` appear anymore after this PR when compiling DML
shaders.
2025-07-14 18:13:42 -07:00
Farzon Lotfi
778b6a21ec
[DirectX] Allow vector Allocas to be transformed into arrays (#145972)
fixes #145782

This change modifies `isArrayOfVectors` into `isVectorOrArrayOfVectors`.
The previous implementation did not support vector to array
transformations. Further it was too simplistic and didn't assume allocas
would create multidimensional arrays.
2025-06-27 13:12:47 -04:00
Farzon Lotfi
38aec4f1f4
[DirectX] Limit GEP transformations to Arrays (#145758)
fixes #145408

Before we see the GEP we already have transformed Allocas that get
passed the `isArrayOfVectors`.
The bug is because we are trying to transform a gep for struct of arrays
when we should only be transforming arrays.

The problem with our `visitGetElementPtrInst` is that it was doing
transformations for all allocas when it should be limiting it to the
alloca of array cases. Technically we would have liked to make sure it
was an array of vectors cases but by the time we see the GEP the type
has been changed by replace all uses. There should not be a problem with
looking at all Arrays since DXILDataScalarization does not change any
indicies.
2025-06-25 17:17:52 -04:00
Farzon Lotfi
2a4207e732
[DirectX] Don't limit visitGetElementPtrInst to global ptrs (#144959)
fixes #144608
- there is a getPointerOperandIndex function so we don't need to iterate
the operands trying to find the pointer. This resulted in a small
cleanup to visitStoreInst and visitLoadInst.

- The meat of this change was in visitGetElementPtrInst to account for
allocas and not bail when we don't find a global.
2025-06-20 15:23:20 -04:00
Deric C.
3f42c6bddd
[DirectX] Scalarize extractelement and insertelement with dynamic indices (#141676)
Fixes #141136

- Implement `visitExtractElementInst` and `visitInsertElementInst` in
`DXILDataScalarizerVisitor` to scalarize `extractelement` and
`insertelement` instructions whose index operand is not a `ConstantInt`
by converting the vector to an array and then loading from the array
- Rename the `replaceVectorWithArray` helper function to
`equivalentArrayTypeFromVector`, relocate the function toward the top of
the file, and remove the unused `Ctx` parameter
2025-06-20 11:20:30 -07:00
Farzon Lotfi
909212feec
[DirectX] Scalarize Allocas as part of data scalarization (#140165)
- DXILDataScalarization should not just be limited to global data
- Add a scalarization for alloca
- Add ReversePostOrderTraversal of functions and iterate over basic
blocks and run DataScalarizerVisitor.
- fixes #140143
2025-05-27 14:23:29 -04:00
Kazu Hirata
ad1ba15ea8
[Target] Use llvm::append_range (NFC) (#133606) 2025-03-29 18:47:47 -07:00
S. Bharadwaj Yadavalli
41745a2005
[NFC][DirectX] Change deprecated insertBefore(Instruction*) API (#125308)
This fixes build errors due to deprecation of this API in commit
79499f010d2bfe809187a9a5f042d4e4ee1f1bcc

All tests in llvm/test/CodeGen/DirectX - including those added in commit
5ac624c8234fe0a62cbf0447dbf7035ea29d062e that added the original code
verified to pass.
2025-01-31 17:45:16 -05:00
Farzon Lotfi
6457aee5b7
[DirectX] Bug fix for Data Scalarization crash (#118426)
Two bugs here. First calling `Inst->getFunction()` has undefined
behavior if the instruction is not tracked to a function. I suspect the
`replaceAllUsesWith` was leaving the GEPs in a weird ghost parent
situation. I switched up the visitor to be able to `eraseFromParent` as
part of visiting and then everything started working.

The second bug was in `DXILFlattenArrays.cpp`. I was unaware that you
can have multidimensional arrays of `zeroinitializer`, and `undef` so
fixed up the initializer to handle these two cases.

fixes #117273
2024-12-18 16:33:49 -05:00
Justin Bogner
3eca15cbb9
[DirectX] Split resource info into type and binding info. NFC (#119773)
This splits the DXILResourceAnalysis pass into TypeAnalysis and
BindingAnalysis passes. The type analysis pass is made immutable and
populated lazily so that it can be used earlier in the pipeline without
needing to carefully maintain the invariants of the binding analysis.

Fixes #118400
2024-12-18 09:02:28 -07:00
Farzon Lotfi
324bdd662d
[DirectX] Data Scalarization of Vectors in Global Scope (#110029)
This change adds a pass to scalarize vectors in global scope into
arrays.

There are three distinct parts
1. find the globals that need to be updated and define what the new type
should be
2. initialize that new type and copy over all the right attributes over
from the old type.
3. Use the instruction visitor pattern to update the loads, stores, and
geps for the layout of the new data structure.

resolves https://github.com/llvm/llvm-project/issues/107920
2024-09-26 17:16:29 -04:00