[llvm] [NVPTX] Check Before inserting AddrSpaceCastInst in NVPTXLoweringAlloca (PR #106127)

Artem Belevich via llvm-commits llvm-commits at lists.llvm.org
Fri Aug 30 10:11:05 PDT 2024


================
@@ -72,12 +72,21 @@ bool NVPTXLowerAlloca::runOnFunction(Function &F) {
         Changed = true;
         auto ETy = allocaInst->getAllocatedType();
         auto LocalAddrTy = PointerType::get(ETy, ADDRESS_SPACE_LOCAL);
-        auto NewASCToLocal = new AddrSpaceCastInst(allocaInst, LocalAddrTy, "");
-        auto GenericAddrTy = PointerType::get(ETy, ADDRESS_SPACE_GENERIC);
-        auto NewASCToGeneric =
-            new AddrSpaceCastInst(NewASCToLocal, GenericAddrTy, "");
-        NewASCToLocal->insertAfter(allocaInst);
-        NewASCToGeneric->insertAfter(NewASCToLocal);
+        PointerType *AllocInstPtrTy =
+            cast<PointerType>(allocaInst->getType()->getScalarType());
+        Instruction *NewASCToGeneric = allocaInst;
+        if (AllocInstPtrTy->getAddressSpace() != ADDRESS_SPACE_LOCAL) {
----------------
Artem-B wrote:

> Do you mean that an explicit assert should be added in NVPTXLowerAlloca.cpp instead of rely on the constructor of AddrSpaceCastInst 

Yes. It's better to catch it early, rather than add more wrong things, and wait for something to break later on, and then back-track.

By default, IR operates on generic pointers. Specific address spaces are target-dependent and are not guaranteed to be supported, even if IR remains syntactically valid. For NVPTX, we can only support allocas in the default AS or AS(5).

https://github.com/llvm/llvm-project/pull/106127


More information about the llvm-commits mailing list