-
Notifications
You must be signed in to change notification settings - Fork 15.1k
[SPIRV] Implement translation for llvm.modf.* intrinsics #147556
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 2 commits
5bfcf33
7d87ed3
e07f915
cdc2571
4a17c21
e803ef6
4f91ce4
435d636
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 | ||||
|---|---|---|---|---|---|---|
|
|
@@ -296,6 +296,8 @@ class SPIRVInstructionSelector : public InstructionSelector { | |||||
| bool selectImageWriteIntrinsic(MachineInstr &I) const; | ||||||
| bool selectResourceGetPointer(Register &ResVReg, const SPIRVType *ResType, | ||||||
| MachineInstr &I) const; | ||||||
| bool selectModf(Register ResVReg, const SPIRVType *ResType, | ||||||
| MachineInstr &I) const; | ||||||
|
|
||||||
| // Utilities | ||||||
| std::pair<Register, bool> | ||||||
|
|
@@ -3207,6 +3209,9 @@ bool SPIRVInstructionSelector::selectIntrinsic(Register ResVReg, | |||||
| case Intrinsic::spv_discard: { | ||||||
| return selectDiscard(ResVReg, ResType, I); | ||||||
| } | ||||||
| case Intrinsic::modf: { | ||||||
| return selectModf(ResVReg, ResType, I); | ||||||
| } | ||||||
| default: { | ||||||
| std::string DiagMsg; | ||||||
| raw_string_ostream OS(DiagMsg); | ||||||
|
|
@@ -3990,6 +3995,77 @@ bool SPIRVInstructionSelector::selectLog10(Register ResVReg, | |||||
| .constrainAllUses(TII, TRI, RBI); | ||||||
| } | ||||||
|
|
||||||
| bool SPIRVInstructionSelector::selectModf(Register ResVReg, | ||||||
| const SPIRVType *ResType, | ||||||
| MachineInstr &I) const { | ||||||
| // llvm.modf has a single arg --the number to be decomposed-- and returns a | ||||||
| // struct { restype, restype }, while OpenCLLIB::modf has two args --the | ||||||
| // number to be decomposed and a pointer--, returns the fractional part and | ||||||
| // the integral part is stored in the pointer argument. Therefore, we can't | ||||||
| // use directly the OpenCLLIB::modf intrinsic. However, we can do some | ||||||
| // scaffolding to make it work. The idea is to create an alloca instruction | ||||||
| // to get a ptr, pass this ptr to OpenCL::modf, and then load the value | ||||||
| // from this ptr to place it in the struct. llvm.modf returns the fractional | ||||||
| // part as the first element of the result, and the integral part as the | ||||||
| // second element of the result. | ||||||
|
|
||||||
| // At this point, the return type is not a struct anymore, but rather two | ||||||
| // independent elements of SPIRVResType. We can get each independent element | ||||||
| // from I.getDefs() or I.getOperands(). | ||||||
| ExtInstList ExtInsts = {{SPIRV::InstructionSet::OpenCL_std, CL::modf}, | ||||||
| {SPIRV::InstructionSet::GLSL_std_450, GL::Modf}}; | ||||||
| for (const auto &Ex : ExtInsts) { | ||||||
| SPIRV::InstructionSet::InstructionSet Set = Ex.first; | ||||||
| uint32_t Opcode = Ex.second; | ||||||
| if (STI.canUseExtInstSet(Set)) { | ||||||
| MachineIRBuilder MIRBuilder(I); | ||||||
| // Get pointer type for alloca variable. | ||||||
| const SPIRVType *PtrType = GR.getOrCreateSPIRVPointerType( | ||||||
| ResType, MIRBuilder, SPIRV::StorageClass::Input); | ||||||
|
||||||
| ResType, MIRBuilder, SPIRV::StorageClass::Input); | |
| ResType, MIRBuilder, SPIRV::StorageClass::Function); |
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.
Thanks for catching this!
Outdated
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.
| MIRBuilder.getMRI()->setType(NewRegister, LLT::pointer(0, 64)); | |
| MIRBuilder.getMRI()->setType(NewRegister, LLT::pointer(storageClassToAddressSpace(SPIRV::StorageClass::Function), GR->getPointerSize())); |
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.
Done in last commit.
Outdated
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.
I think this works because you have 1 call to the intrinsics in your test, this is the first thing the function does
Module *M = MIRBuilder.getMF().getFunction().getParent();
GVar = M->getGlobalVariable(Name);
if (GVar == nullptr) {
const Type *Ty = getTypeForSPIRVType(BaseType); // TODO: check type.
// Module takes ownership of the global var.
GVar = new GlobalVariable(*M, const_cast<Type *>(Ty), false,
GlobalValue::ExternalLinkage, nullptr,
Twine(Name));
}You probably going to end up with strange things if you have 2 calls in 2 distinct functions.
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.
I changed the approach to use variables local to function instead of global.
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.
Also, worth to note I extended the test to have a second function calling llvm.modf and it works well.
maarquitos14 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
Outdated
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.
nit: Not necessary OpenCLLIB:: as the code also handles GLSL_std_450
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.
I have now moved to if/else approach, so I kept this as is for the OpenCL branch. Let me know if that's okay with you.
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.
why a loop and no a
if / elseto initOpcodeandSet? Looks a bit convoluted for no reasonUh oh!
There was an error while loading. Please reload this page.
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.
Agree with Victor, that it's probably better to use if/else here, but for a different reason. From https://registry.khronos.org/SPIR-V/specs/unified1/GLSL.std.450.html :
Modf is deprecated, use ModfStruct instead.andModfStructhas a different semantic. I don't have strong opinion whether to generateModfStructin this patch right away (up to you to decide), but obviously handling forModfStructwill be different.BTW, please leave
FIXMEcomment in case if you don't want to generateModfStructright away.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.
well spotted,
ModfStructwas added in version 0.99, so IMO there is no reason to useGL::Modfat allThere 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.
Honestly, I just copied the structure from
SPIRVInstructionSelector::selectExtInstand adapted it for this particular case, but I do agree it seems convoluted. Also, ifGL::Modfis deprecated there's no reason to keep this. I'll simplify it, thanks for catching this.