Author: Nikita Popov
Date: 2022-02-16T15:17:08+01:00
New Revision: fe3407a91b6dcc1100c0b30187aa8c9587c73cad

URL: 
https://github.com/llvm/llvm-project/commit/fe3407a91b6dcc1100c0b30187aa8c9587c73cad
DIFF: 
https://github.com/llvm/llvm-project/commit/fe3407a91b6dcc1100c0b30187aa8c9587c73cad.diff

LOG: [CGBuilder] Assert that CreateAddrSpaceCast does not change element type

Address space casts in general may change the element type, but
don't allow it in the method working on Address, so we can
preserve the element type.

CreatePointerBitCastOrAddrSpaceCast() still needs to be addressed.

Added: 
    

Modified: 
    clang/lib/CodeGen/CGBuilder.h
    clang/lib/CodeGen/CGExpr.cpp

Removed: 
    


################################################################################
diff  --git a/clang/lib/CodeGen/CGBuilder.h b/clang/lib/CodeGen/CGBuilder.h
index 2855649472ec..eaf563a436ca 100644
--- a/clang/lib/CodeGen/CGBuilder.h
+++ b/clang/lib/CodeGen/CGBuilder.h
@@ -154,8 +154,10 @@ class CGBuilderTy : public CGBuilderBaseTy {
   using CGBuilderBaseTy::CreateAddrSpaceCast;
   Address CreateAddrSpaceCast(Address Addr, llvm::Type *Ty,
                               const llvm::Twine &Name = "") {
-    return Address(CreateAddrSpaceCast(Addr.getPointer(), Ty, Name),
-                   Addr.getAlignment());
+    assert(cast<llvm::PointerType>(Ty)->isOpaqueOrPointeeTypeMatches(
+               Addr.getElementType()) &&
+           "Should not change the element type");
+    return Addr.withPointer(CreateAddrSpaceCast(Addr.getPointer(), Ty, Name));
   }
 
   /// Cast the element type of the given address to a 
diff erent type,

diff  --git a/clang/lib/CodeGen/CGExpr.cpp b/clang/lib/CodeGen/CGExpr.cpp
index e0e1dd5df586..7fc4c4f35796 100644
--- a/clang/lib/CodeGen/CGExpr.cpp
+++ b/clang/lib/CodeGen/CGExpr.cpp
@@ -1114,11 +1114,11 @@ Address CodeGenFunction::EmitPointerWithAlignment(const 
Expr *E,
                                       CE->getBeginLoc());
         }
 
-        if (CE->getCastKind() == CK_AddressSpaceConversion)
-         return Builder.CreateAddrSpaceCast(Addr, ConvertType(E->getType()));
-
         llvm::Type *ElemTy = ConvertTypeForMem(E->getType()->getPointeeType());
-        return Builder.CreateElementBitCast(Addr, ElemTy);
+        Addr = Builder.CreateElementBitCast(Addr, ElemTy);
+        if (CE->getCastKind() == CK_AddressSpaceConversion)
+          Addr = Builder.CreateAddrSpaceCast(Addr, ConvertType(E->getType()));
+        return Addr;
       }
       break;
 


        
_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to