Index: lib/CodeGen/CGBuiltin.cpp =================================================================== --- lib/CodeGen/CGBuiltin.cpp +++ lib/CodeGen/CGBuiltin.cpp @@ -3339,7 +3339,16 @@ // for the block. \p First is the position of the first size argument. auto CreateArrayForSizeVar = [=](unsigned First) { auto *AT = llvm::ArrayType::get(SizeTy, NumArgs - First); + // Always insert the alloca in the entry block so it remains static in + // the SelectionDAG. + BasicBlock *Begin = nullptr; + if (Instruction *Entry = CurFn->getEntryBlock().getTerminator()) { + Begin = Builder.GetInsertBlock(); + Builder.SetInsertPoint(Entry); + } auto *Arr = Builder.CreateAlloca(AT); + if (Begin) + Builder.SetInsertPoint(Begin); llvm::Value *Ptr; // Each of the following arguments specifies the size of the corresponding // argument passed to the enqueued block. Index: test/CodeGenOpenCL/enqueue-kernel-non-entry-block.cl =================================================================== --- /dev/null +++ test/CodeGenOpenCL/enqueue-kernel-non-entry-block.cl @@ -0,0 +1,21 @@ +// RUN: %clang_cc1 %s -cl-std=CL2.0 -O0 -emit-llvm -o - -triple amdgcn | FileCheck %s --check-prefixes=COMMON,AMDGPU +// RUN: %clang_cc1 %s -cl-std=CL2.0 -O0 -emit-llvm -o - -triple "spir-unknown-unknown" | FileCheck %s --check-prefixes=COMMON,SPIR32 +// RUN: %clang_cc1 %s -cl-std=CL2.0 -O0 -emit-llvm -o - -triple "spir64-unknown-unknown" | FileCheck %s --check-prefixes=COMMON,SPIR64 + +typedef struct {int a;} ndrange_t; + +kernel void test(int i) { +// COMMON-LABEL: define {{.*}} void @test +// COMMON-LABEL: entry: +// AMDGPU: alloca [1 x i64] +// SPIR32: alloca [1 x i32] +// SPIR64: alloca [1 x i64] +// COMMON-LABEL: if.then: +// COMMON-NOT: alloca +// COMMON-LABEL: if.end + queue_t default_queue; + unsigned flags = 0; + ndrange_t ndrange; + if (i) + enqueue_kernel(default_queue, flags, ndrange, ^(local void *a) { }, 32); +}