Index: test/CodeGenOpenCL/struct.cl
===================================================================
--- test/CodeGenOpenCL/struct.cl	(revision 0)
+++ test/CodeGenOpenCL/struct.cl	(revision 0)
@@ -0,0 +1,17 @@
+// RUN: %clang_cc1 %s -ffake-address-space-map -emit-llvm -o %t
+
+typedef struct {
+  float a;
+  int s;
+  float loc;
+} AStruct;
+
+void foo(AStruct s)
+{
+  s.s = s.a + s.loc;
+}
+
+kernel void StructTest(__global AStruct *s_in )
+{
+  foo(s_in[0]);
+}
Index: lib/CodeGen/CGCall.cpp
===================================================================
--- lib/CodeGen/CGCall.cpp	(revision 158365)
+++ lib/CodeGen/CGCall.cpp	(working copy)
@@ -1726,10 +1726,21 @@
       cast<CastExpr>(E)->getCastKind() == CK_LValueToRValue) {
     LValue L = EmitLValue(cast<CastExpr>(E)->getSubExpr());
     assert(L.isSimple());
-    args.add(L.asAggregateRValue(), type, /*NeedsCopy*/true);
+    
+    // Convert to the correct address space. This can only
+    // occur, in the indirect byval case; any other case of mismatched
+    // address spaces is caught in Sema.
+    if (L.getType().getAddressSpace() != type.getAddressSpace()) {
+      llvm::Type *Cast = ConvertType(getContext().getPointerType(type));
+      llvm::Value *V = Builder.CreateBitCast(L.getAddress(), Cast);
+      args.add(RValue::getAggregate(V, L.isVolatileQualified()),
+               type, /*NeedsCopy*/true);
+    }
+    else
+      args.add(L.asAggregateRValue(), type, /*NeedsCopy*/true);
+    
     return;
   }
-
   args.add(EmitAnyExprToTemp(E), type);
 }
 
