-
Notifications
You must be signed in to change notification settings - Fork 15.2k
[HLSL] Re-implement countbits with the correct return type #113189
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
Changes from all commits
23d6202
e29f401
5e40350
74f0925
d160782
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -505,6 +505,73 @@ class OpLowerer { | |
| }); | ||
| } | ||
|
|
||
| [[nodiscard]] bool lowerCtpopToCountBits(Function &F) { | ||
| IRBuilder<> &IRB = OpBuilder.getIRB(); | ||
| Type *Int32Ty = IRB.getInt32Ty(); | ||
|
|
||
| return replaceFunction(F, [&](CallInst *CI) -> Error { | ||
| IRB.SetInsertPoint(CI); | ||
| SmallVector<Value *> Args; | ||
| Args.append(CI->arg_begin(), CI->arg_end()); | ||
|
|
||
| Type *RetTy = Int32Ty; | ||
| Type *FRT = F.getReturnType(); | ||
| if (const auto *VT = dyn_cast<VectorType>(FRT)) | ||
| RetTy = VectorType::get(RetTy, VT); | ||
|
|
||
| Expected<CallInst *> OpCall = OpBuilder.tryCreateOp( | ||
| dxil::OpCode::CountBits, Args, CI->getName(), RetTy); | ||
| if (Error E = OpCall.takeError()) | ||
| return E; | ||
|
|
||
| // If the result type is 32 bits we can do a direct replacement. | ||
| if (FRT->isIntOrIntVectorTy(32)) { | ||
| CI->replaceAllUsesWith(*OpCall); | ||
| CI->eraseFromParent(); | ||
| return Error::success(); | ||
| } | ||
|
|
||
| unsigned CastOp; | ||
| unsigned CastOp2; | ||
| if (FRT->isIntOrIntVectorTy(16)) { | ||
| CastOp = Instruction::ZExt; | ||
|
||
| CastOp2 = Instruction::SExt; | ||
| } else { // must be 64 bits | ||
| assert(FRT->isIntOrIntVectorTy(64) && | ||
| "Currently only lowering 16, 32, or 64 bit ctpop to CountBits \ | ||
| is supported."); | ||
| CastOp = Instruction::Trunc; | ||
spall marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| CastOp2 = Instruction::Trunc; | ||
| } | ||
|
|
||
| // It is correct to replace the ctpop with the dxil op and | ||
| // remove all casts to i32 | ||
| bool NeedsCast = false; | ||
| for (User *User : make_early_inc_range(CI->users())) { | ||
| Instruction *I = dyn_cast<Instruction>(User); | ||
| if (I && (I->getOpcode() == CastOp || I->getOpcode() == CastOp2) && | ||
| I->getType() == RetTy) { | ||
| I->replaceAllUsesWith(*OpCall); | ||
| I->eraseFromParent(); | ||
| } else | ||
| NeedsCast = true; | ||
| } | ||
|
|
||
| // It is correct to replace a ctpop with the dxil op and | ||
| // a cast from i32 to the return type of the ctpop | ||
| // the cast is emitted here if there is a non-cast to i32 | ||
| // instr which uses the ctpop | ||
| if (NeedsCast) { | ||
| Value *Cast = | ||
| IRB.CreateZExtOrTrunc(*OpCall, F.getReturnType(), "ctpop.cast"); | ||
| CI->replaceAllUsesWith(Cast); | ||
| } | ||
|
|
||
| CI->eraseFromParent(); | ||
| return Error::success(); | ||
| }); | ||
| } | ||
|
|
||
| bool lowerIntrinsics() { | ||
| bool Updated = false; | ||
| bool HasErrors = false; | ||
|
|
@@ -543,6 +610,9 @@ class OpLowerer { | |
| return replaceSplitDoubleCallUsages(CI, Op); | ||
| }); | ||
| break; | ||
| case Intrinsic::ctpop: | ||
| HasErrors |= lowerCtpopToCountBits(F); | ||
| break; | ||
| } | ||
| Updated = true; | ||
| } | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry I missed the earlier PR where this was added, but I don't know how signed integers work here. We only support unsigned in HLSL. Is that a change for clang?
Missing tests as well, if so.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When I asked Justin about this he pointed to this link:
https://github.com/microsoft/DirectXShaderCompiler/blob/main/utils/hct/gen_intrin_main.txt#L114
and suggested it meant both signed and unsigned were supported.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah, of course our documentation is wrong. countbits of signed is kind of illogical, but definitely should add tests regardless.