-
Notifications
You must be signed in to change notification settings - Fork 13.5k
[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; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Related to the signed vs unsigned: this is where things I think get confusing. int16_t would almost always be sign extended, not zero extended. But doing a sign extension here doesn't make sense as you'll count 16 extra bits for any negative int16_t. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sorry if I'm being confusing - I am not saying you have the wrong code here, I'm just bringing it up that this is where my thought drifted towards the topic because of this zero extension (which I think is correct) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The extension should only be on the return value which is always unsigned. Is this code wrong with that in mind? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Oh, I see. This is overriding the cast op below. Is a zero/sign extension needed? I notice the return type is "int16_t". There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Disregard. I see you actually updated that correctly! (the whole point of this PR haha) |
||
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.
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.