Index: lib/CodeGen/CGDecl.cpp =================================================================== --- lib/CodeGen/CGDecl.cpp +++ lib/CodeGen/CGDecl.cpp @@ -158,6 +158,13 @@ // Don't emit it now, allow it to be emitted lazily on its first use. return; + // Samplers in constant address space are handled as a special case + // unlike other variables they are not generated as globals + // and their initialiser will be used everywhere it is being referenced. + if (D.getType().getAddressSpace() == LangAS::opencl_constant && + D.getType().getTypePtr()->isSamplerT()) + return; + // Some function-scope variable does not have static storage but still // needs to be emitted like a static variable, e.g. a function-scope // variable in constant address space in OpenCL. Index: test/CodeGenOpenCL/sampler.cl =================================================================== --- test/CodeGenOpenCL/sampler.cl +++ test/CodeGenOpenCL/sampler.cl @@ -33,6 +33,10 @@ // CHECK: [[SAMP:%[0-9]+]] = call %opencl.sampler_t addrspace(2)* @__translate_sampler_initializer(i32 19) // CHECK: store %opencl.sampler_t addrspace(2)* [[SAMP]], %opencl.sampler_t addrspace(2)** [[smp_ptr]] + // Initialising constant AS sampler will be handled as global (we won't generate local alloca for it) + // CHECK-NOT: alloca %opencl.sampler_t addrspace(2)* + constant sampler_t smp_const_as = 11; + // Case 1b fnc4smp(smp); // CHECK-NOT: call %opencl.sampler_t addrspace(2)* @__translate_sampler_initializer(i32 19) @@ -58,4 +62,8 @@ fnc4smp(5); // CHECK: [[SAMP:%[0-9]+]] = call %opencl.sampler_t addrspace(2)* @__translate_sampler_initializer(i32 5) // CHECK: call spir_func void @fnc4smp(%opencl.sampler_t addrspace(2)* [[SAMP]]) + + fnc4smp(smp_const_as); + // CHECK: [[SAMP:%[0-9]+]] = call %opencl.sampler_t addrspace(2)* @__translate_sampler_initializer(i32 11) + // CHECK: call spir_func void @fnc4smp(%opencl.sampler_t addrspace(2)* [[SAMP]]) }