Skip to content

Commit 9b03d3f

Browse files
[CIR][CodeGen] Replace errorNYI with assert for address space in emitAutoVarAlloca (llvm#197506)
Auto variables can only be in the default address space, or opencl_private when compiling OpenCL. Replace the errorNYI with an assert matching OG codegen (CGDecl.cpp). Fixes part of llvm#160386 Co-authored-by: Andy Kaylor <akaylor@nvidia.com>
1 parent 3c193d4 commit 9b03d3f

2 files changed

Lines changed: 51 additions & 2 deletions

File tree

clang/lib/CIR/CodeGen/CIRGenDecl.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,9 @@ CIRGenFunction::AutoVarEmission
3232
CIRGenFunction::emitAutoVarAlloca(const VarDecl &d,
3333
mlir::OpBuilder::InsertPoint ip) {
3434
QualType ty = d.getType();
35-
if (ty.getAddressSpace() != LangAS::Default)
36-
cgm.errorNYI(d.getSourceRange(), "emitAutoVarAlloca: address space");
35+
assert(
36+
ty.getAddressSpace() == LangAS::Default ||
37+
(ty.getAddressSpace() == LangAS::opencl_private && getLangOpts().OpenCL));
3738

3839
mlir::Location loc = getLoc(d.getSourceRange());
3940
bool nrvo =
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
// RUN: %clang_cc1 -x clcpp -triple spir64 -cl-std=clc++ -fclangir -emit-cir %s -o %t.cir
2+
// RUN: FileCheck --input-file=%t.cir %s -check-prefix=CIR
3+
// RUN: %clang_cc1 -x clcpp -triple spir64 -cl-std=clc++ -fclangir -emit-llvm -O0 %s -o %t.ll
4+
// RUN: FileCheck --input-file=%t.ll %s -check-prefix=LLVM
5+
// RUN: %clang_cc1 -x clcpp -triple spir64 -cl-std=clc++ -emit-llvm -O0 %s -o %t.ll
6+
// RUN: FileCheck --input-file=%t.ll %s -check-prefix=OGCG
7+
8+
// Test that local variable allocation works correctly in OpenCL C++,
9+
// where auto variables have the opencl_private address space.
10+
11+
// CIR: cir.func {{.*}} @k(%arg0: !cir.ptr<!s32i>
12+
// CIR: %[[GP:.*]] = cir.alloca !cir.ptr<!s32i>, !cir.ptr<!cir.ptr<!s32i>>, ["gp", init]
13+
// CIR: %[[GR_ALLOCA:.*]] = cir.alloca !cir.ptr<!s32i>, !cir.ptr<!cir.ptr<!s32i>>, ["gr", init, const]
14+
// CIR: %[[R_ALLOCA:.*]] = cir.alloca !cir.ptr<!s32i>, !cir.ptr<!cir.ptr<!s32i>>, ["r", init, const]
15+
// CIR: %[[R:.*]] = cir.cast address_space %[[R_ALLOCA]] : !cir.ptr<!cir.ptr<!s32i>> -> !cir.ptr<!cir.ptr<!s32i>>
16+
// CIR: %[[GR:.*]] = cir.cast address_space %[[GR_ALLOCA]] : !cir.ptr<!cir.ptr<!s32i>> -> !cir.ptr<!cir.ptr<!s32i>>
17+
// CIR: cir.store %arg0, %[[GP]] : !cir.ptr<!s32i>, !cir.ptr<!cir.ptr<!s32i>>
18+
// CIR: %[[DEREF:.*]] = cir.load deref {{.*}} %[[GP]] : !cir.ptr<!cir.ptr<!s32i>>, !cir.ptr<!s32i>
19+
// CIR: cir.store {{.*}} %[[DEREF]], %[[GR]] : !cir.ptr<!s32i>, !cir.ptr<!cir.ptr<!s32i>>
20+
// CIR: %[[GR_VAL:.*]] = cir.load %[[GR]] : !cir.ptr<!cir.ptr<!s32i>>, !cir.ptr<!s32i>
21+
// CIR: %[[CAST:.*]] = cir.cast address_space %[[GR_VAL]] : !cir.ptr<!s32i> -> !cir.ptr<!s32i>
22+
// CIR: cir.store {{.*}} %[[CAST]], %[[R]] : !cir.ptr<!s32i>, !cir.ptr<!cir.ptr<!s32i>>
23+
24+
// LLVM: define dso_local void @k(ptr noundef %[[ARG:.*]])
25+
// LLVM: %[[GP_ADDR:.*]] = alloca ptr
26+
// LLVM: %[[GR_ADDR:.*]] = alloca ptr
27+
// LLVM: %[[R_ADDR:.*]] = alloca ptr
28+
// LLVM: store ptr %[[ARG]], ptr %[[GP_ADDR]]
29+
// LLVM: %[[V1:.*]] = load ptr, ptr %[[GP_ADDR]]
30+
// LLVM: store ptr %[[V1]], ptr %[[GR_ADDR]]
31+
// LLVM: %[[V2:.*]] = load ptr, ptr %[[GR_ADDR]]
32+
// LLVM: store ptr %[[V2]], ptr %[[R_ADDR]]
33+
34+
// OGCG: define dso_local spir_func void @__clang_ocl_kern_imp_k(ptr addrspace(1) noundef align 4 %gp)
35+
// OGCG: %gp.addr = alloca ptr addrspace(1)
36+
// OGCG: %gr = alloca ptr addrspace(1)
37+
// OGCG: %r = alloca ptr addrspace(4)
38+
// OGCG: store ptr addrspace(1) %gp, ptr %gp.addr
39+
// OGCG: %[[V1:.*]] = load ptr addrspace(1), ptr %gp.addr
40+
// OGCG: store ptr addrspace(1) %[[V1]], ptr %gr
41+
// OGCG: %[[V2:.*]] = load ptr addrspace(1), ptr %gr
42+
// OGCG: %[[V3:.*]] = addrspacecast ptr addrspace(1) %[[V2]] to ptr addrspace(4)
43+
// OGCG: store ptr addrspace(4) %[[V3]], ptr %r
44+
__kernel void k(__global int *gp) {
45+
__global int &gr = *gp;
46+
int &r = gr;
47+
(void)r;
48+
}

0 commit comments

Comments
 (0)