-
Couldn't load subscription status.
- Fork 15k
[clang][SPIR][SPIRV] Materialize non-generic null pointers via addrspacecast #161773
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
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 | ||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -61,6 +61,9 @@ class CommonSPIRTargetCodeGenInfo : public TargetCodeGenInfo { | |||||||||||||||
| QualType SampledType, CodeGenModule &CGM) const; | ||||||||||||||||
| void | ||||||||||||||||
| setOCLKernelStubCallingConvention(const FunctionType *&FT) const override; | ||||||||||||||||
| llvm::Constant *getNullPointer(const CodeGen::CodeGenModule &CGM, | ||||||||||||||||
| llvm::PointerType *T, | ||||||||||||||||
| QualType QT) const override; | ||||||||||||||||
| }; | ||||||||||||||||
| class SPIRVTargetCodeGenInfo : public CommonSPIRTargetCodeGenInfo { | ||||||||||||||||
| public: | ||||||||||||||||
|
|
@@ -240,6 +243,31 @@ void CommonSPIRTargetCodeGenInfo::setOCLKernelStubCallingConvention( | |||||||||||||||
| FT, FT->getExtInfo().withCallingConv(CC_SpirFunction)); | ||||||||||||||||
| } | ||||||||||||||||
|
|
||||||||||||||||
| // LLVM currently assumes a null pointer has the bit pattern 0, but some GPU | ||||||||||||||||
| // targets use a non-zero encoding for null in certain address spaces. | ||||||||||||||||
| // Because SPIR(-V) is a virtual target and the bit pattern of a non-generic | ||||||||||||||||
| // null is unspecified, materialize non-generic null via an addrspacecast from | ||||||||||||||||
| // the generic null. | ||||||||||||||||
| // This allows later lowering to substitute the target’s real sentinel value. | ||||||||||||||||
| llvm::Constant * | ||||||||||||||||
| CommonSPIRTargetCodeGenInfo::getNullPointer(const CodeGen::CodeGenModule &CGM, | ||||||||||||||||
|
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. Can you directly copy the AMDGPU implementation, which checks against getTargetNullPointerValue 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.
llvm-project/clang/lib/Basic/Targets/AMDGPU.h Lines 424 to 430 in 441f0c7
However, we don't know the return value of getNullPointerValue in BaseSPIRTargetInfo except for generic address space, right? 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. Hm, seems like getNullPointerValue should be able to fail. My main worry here is special casing generic address space, as opposed to default address space 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.
thanks @arsenm, you're right that the default AS is not checked. I copied the logic from ASTContext::getTargetNullPointerValue into CommonSPIRTargetCodeGenInfo::getNullPointer in 5c5e13e to avoid changing return type of both getNullPointerValue and getTargetNullPointerValue to std::optional<uint64_t>. Since getTargetNullPointerValue is used in many places, changing its return type looks like unnecessary churn. |
||||||||||||||||
| llvm::PointerType *PT, | ||||||||||||||||
| QualType QT) const { | ||||||||||||||||
| LangAS AS; | ||||||||||||||||
| if (QT->getUnqualifiedDesugaredType()->isNullPtrType()) | ||||||||||||||||
| AS = LangAS::Default; | ||||||||||||||||
| else | ||||||||||||||||
| AS = QT->getPointeeType().getAddressSpace(); | ||||||||||||||||
|
||||||||||||||||
| if (AS == LangAS::Default || AS == LangAS::opencl_generic) | ||||||||||||||||
| return llvm::ConstantPointerNull::get(PT); | ||||||||||||||||
|
|
||||||||||||||||
| auto &Ctx = CGM.getContext(); | ||||||||||||||||
| auto NPT = llvm::PointerType::get( | ||||||||||||||||
| PT->getContext(), Ctx.getTargetAddressSpace(LangAS::opencl_generic)); | ||||||||||||||||
| return llvm::ConstantExpr::getAddrSpaceCast( | ||||||||||||||||
| llvm::ConstantPointerNull::get(NPT), PT); | ||||||||||||||||
| } | ||||||||||||||||
|
|
||||||||||||||||
| LangAS | ||||||||||||||||
| SPIRVTargetCodeGenInfo::getGlobalVarAddressSpace(CodeGenModule &CGM, | ||||||||||||||||
| const VarDecl *D) const { | ||||||||||||||||
|
|
||||||||||||||||
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.
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 b95e77a