Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Spirv][HLSL] Add OpAll lowering and float vec support #87952

Merged
merged 10 commits into from
Apr 10, 2024

Conversation

farzonl
Copy link
Member

@farzonl farzonl commented Apr 8, 2024

The main point of this change was to add support for HLSL's all intrinsic.
In the process of doing that I found a few issues around creating an OpConstantComposite via buildZerosVal.

First the current code didn't support floats so the process of adding buildZerosValF meant I needed a
float version of getOrCreateIntConstVector. After doing so I renamed both versions to getOrCreateConstVector. That meant I needed to create a float type version of getOrCreateIntCompositeOrNull. Luckily the type information was low for this function so was able to split it out into a helpwe and rename getOrCreateIntCompositeOrNull to getOrCreateCompositeOrNull With the exception of type handling differences of the code and Null vs 0 Constant Op codes these functions should be identical.

To handle scalar floats I could not use buildConstantFP like this PR did: 0a2aaab5aba46#diff-733a189c5a8c3211f3a04fd6e719952a3fa231eadd8a7f11e6ecf1e584d57411R1603 because that would create too many superfluous registers (that causes problems in the validator), I had to create a float version of getOrCreateConstInt which I called getOrCreateConstFP.
similar problems with doing it like this: https://github.com/llvm/llvm-project/blob/main/llvm/lib/Target/SPIRV/SPIRVBuiltins.cpp#L1540.

buildZerosValF also has a use of a function getZeroFP. This is because half, float, and double scalar values of 0 would collide in SPIRVDuplicatesTracker<Constant> CT if you use APFloat(0.0f).

getORCreateConstFP needed its own version of getOrCreateConstIntReg which I called getOrCreateConstFloatReg The one difference in this function is getOrCreateConstFloatReg returns a bit width so we don't have to call getScalarOrVectorBitWidth twice ie when it is used again in getOrCreateConstFP for OpConstantF addNumImm.

getOrCreateConstFloatReg needed an assignFloatTypeToVReg helper which called a getOrCreateSPIRVFloatType helper. There was no equivalent IntegerType::get for floats so I handled this with a switch statement on bit widths to get the right LLVM float type.

Finally, there is the use of bool ZeroAsNull = STI.isOpenCLEnv(); This is partly a cosmetic change. When Zeros are treated as nulls, we don't create OpConstantComposite vectors which is something we do in the DXCs SPIRV backend. The DXC SPIRV backend also does not use OpConstantNull. Finally, I needed a means to test the behavior of the OpConstantNull and OpConstantComposite changes and this was one way I could do that via the same tests.

llvm/lib/Target/SPIRV/SPIRVGlobalRegistry.cpp Show resolved Hide resolved
llvm/lib/Target/SPIRV/SPIRVGlobalRegistry.cpp Show resolved Hide resolved
llvm/lib/Target/SPIRV/SPIRVGlobalRegistry.cpp Outdated Show resolved Hide resolved
llvm/test/CodeGen/SPIRV/hlsl-intrinsics/all.ll Outdated Show resolved Hide resolved
llvm/test/CodeGen/SPIRV/hlsl-intrinsics/all.ll Outdated Show resolved Hide resolved
llvm/test/CodeGen/SPIRV/hlsl-intrinsics/all.ll Outdated Show resolved Hide resolved
@farzonl farzonl marked this pull request as ready for review April 9, 2024 14:09
@VyacheslavLevytskyy
Copy link
Contributor

LGTM, thank you @farzonl

Copy link
Contributor

@coopp coopp left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me. I am still getting used to seeing 'unsigned' being used without a type being specified.

@farzonl farzonl merged commit 05093e2 into llvm:main Apr 10, 2024
7 checks passed
@farzonl farzonl deleted the spirv-opall-lowering-and-float-vec-support branch April 10, 2024 20:27
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Status: Done
Development

Successfully merging this pull request may close these issues.

[SPIRV] Add Float and Float Vector Constant Support [HLSL][SPIR-V] Add spv all intrinsic lowering
5 participants