AlexVlx created this revision.
AlexVlx added reviewers: aaron.ballman, yaxunl.
Herald added subscribers: arichardson, tpr.
Herald added a project: All.
AlexVlx requested review of this revision.
Herald added a project: clang.
Herald added a subscriber: cfe-commits.

When lowering from a HLL that does not use explicit generic address spaces 
(e.g. HIP) to target specific IR for a target that uses explicit address spaces 
(e.g. AMDGPU), it is possible for an explicitly placed pointer to be passed as 
an argument to a function taking a generic pointer. An example of this 
situation is when GEPs are formed into VTTs. This flares in Debug builds (since 
a bitcast would be invalid) and, possibly, at runtime on targets where the 
bitwise representation of pointers in different address spaces is not identical.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D150746

Files:
  clang/lib/CodeGen/CGCall.cpp


Index: clang/lib/CodeGen/CGCall.cpp
===================================================================
--- clang/lib/CodeGen/CGCall.cpp
+++ clang/lib/CodeGen/CGCall.cpp
@@ -5183,11 +5183,17 @@
             V->getType()->isIntegerTy())
           V = Builder.CreateZExt(V, ArgInfo.getCoerceToType());
 
-        // If the argument doesn't match, perform a bitcast to coerce it.  This
-        // can happen due to trivial type mismatches.
+        // If the argument doesn't match, perform a either a bitcast or an
+        // address space cast to coerce it. This can happen either due to
+        // trivial type mismatches or valid address space mismatches
+        // (e.g. global -> generic; GEPs into VTTs are an example).
         if (FirstIRArg < IRFuncTy->getNumParams() &&
             V->getType() != IRFuncTy->getParamType(FirstIRArg))
-          V = Builder.CreateBitCast(V, IRFuncTy->getParamType(FirstIRArg));
+          if (V->getType()->isPointerTy())
+            V = Builder.CreateAddrSpaceCast(V,
+                                            
IRFuncTy->getParamType(FirstIrArg));
+          else
+            V = Builder.CreateBitCast(V, IRFuncTy->getParamType(FirstIRArg));
 
         if (ArgHasMaybeUndefAttr)
           V = Builder.CreateFreeze(V);


Index: clang/lib/CodeGen/CGCall.cpp
===================================================================
--- clang/lib/CodeGen/CGCall.cpp
+++ clang/lib/CodeGen/CGCall.cpp
@@ -5183,11 +5183,17 @@
             V->getType()->isIntegerTy())
           V = Builder.CreateZExt(V, ArgInfo.getCoerceToType());
 
-        // If the argument doesn't match, perform a bitcast to coerce it.  This
-        // can happen due to trivial type mismatches.
+        // If the argument doesn't match, perform a either a bitcast or an
+        // address space cast to coerce it. This can happen either due to
+        // trivial type mismatches or valid address space mismatches
+        // (e.g. global -> generic; GEPs into VTTs are an example).
         if (FirstIRArg < IRFuncTy->getNumParams() &&
             V->getType() != IRFuncTy->getParamType(FirstIRArg))
-          V = Builder.CreateBitCast(V, IRFuncTy->getParamType(FirstIRArg));
+          if (V->getType()->isPointerTy())
+            V = Builder.CreateAddrSpaceCast(V,
+                                            IRFuncTy->getParamType(FirstIrArg));
+          else
+            V = Builder.CreateBitCast(V, IRFuncTy->getParamType(FirstIRArg));
 
         if (ArgHasMaybeUndefAttr)
           V = Builder.CreateFreeze(V);
_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to