Skip to content

Commit 99aef47

Browse files
seven-milelanza
authored andcommittedOct 14, 2024
[CIR][CodeGen] Fix address space of result pointer type of array decay cast op (#812)
There are two occurrences of `cir.cast(array_to_ptrdecay, ...)` that drop address spaces unexpectedly for its result pointer type. This PR fixes them with the source address space. ```mlir // Before %1 = cir.cast(array_to_ptrdecay, %0 : !cir.ptr<!cir.array<!s32i x 32>, addrspace(offload_local)>), !cir.ptr<!s32i> // After %1 = cir.cast(array_to_ptrdecay, %0 : !cir.ptr<!cir.array<!s32i x 32>, addrspace(offload_local)>), !cir.ptr<!s32i, addrspace(offload_local)> ```

File tree

5 files changed

+50
-3
lines changed

5 files changed

+50
-3
lines changed
 

‎clang/lib/CIR/CodeGen/CIRGenBuilder.cpp

+3-1
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,10 @@ mlir::Value CIRGenBuilderTy::maybeBuildArrayDecay(mlir::Location loc,
1919
::mlir::dyn_cast<::mlir::cir::ArrayType>(arrayPtrTy.getPointee());
2020

2121
if (arrayTy) {
22+
auto addrSpace = ::mlir::cast_if_present<::mlir::cir::AddressSpaceAttr>(
23+
arrayPtrTy.getAddrSpace());
2224
mlir::cir::PointerType flatPtrTy =
23-
mlir::cir::PointerType::get(getContext(), arrayTy.getEltType());
25+
getPointerTo(arrayTy.getEltType(), addrSpace);
2426
return create<mlir::cir::CastOp>(
2527
loc, flatPtrTy, mlir::cir::CastKind::array_to_ptrdecay, arrayPtr);
2628
}

‎clang/lib/CIR/CodeGen/CIRGenExprAgg.cpp

+4-2
Original file line numberDiff line numberDiff line change
@@ -488,8 +488,10 @@ void AggExprEmitter::buildArrayInit(Address DestPtr, mlir::cir::ArrayType AType,
488488
QualType elementPtrType = CGF.getContext().getPointerType(elementType);
489489

490490
auto cirElementType = CGF.convertType(elementType);
491-
auto cirElementPtrType = mlir::cir::PointerType::get(
492-
CGF.getBuilder().getContext(), cirElementType);
491+
auto cirAddrSpace = mlir::cast_if_present<mlir::cir::AddressSpaceAttr>(
492+
DestPtr.getType().getAddrSpace());
493+
auto cirElementPtrType =
494+
CGF.getBuilder().getPointerTo(cirElementType, cirAddrSpace);
493495
auto loc = CGF.getLoc(ExprToVisit->getSourceRange());
494496

495497
// Cast from cir.ptr<cir.array<elementType> to cir.ptr<elementType>

‎clang/lib/CIR/Dialect/IR/CIRDialect.cpp

+5
Original file line numberDiff line numberDiff line change
@@ -490,6 +490,11 @@ LogicalResult CastOp::verify() {
490490
if (!arrayPtrTy || !flatPtrTy)
491491
return emitOpError() << "requires !cir.ptr type for source and result";
492492

493+
if (arrayPtrTy.getAddrSpace() != flatPtrTy.getAddrSpace()) {
494+
return emitOpError()
495+
<< "requires same address space for source and result";
496+
}
497+
493498
auto arrayTy =
494499
mlir::dyn_cast<mlir::cir::ArrayType>(arrayPtrTy.getPointee());
495500
if (!arrayTy)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
// RUN: %clang_cc1 -cl-std=CL3.0 -O0 -fclangir -emit-cir -triple spirv64-unknown-unknown %s -o %t.cir
2+
// RUN: FileCheck --input-file=%t.cir %s --check-prefix=CIR
3+
// RUN: %clang_cc1 -cl-std=CL3.0 -O0 -fclangir -emit-llvm -triple spirv64-unknown-unknown %s -o %t.ll
4+
// RUN: FileCheck --input-file=%t.ll %s --check-prefix=LLVM
5+
6+
// CIR: @func1
7+
// LLVM: @func1
8+
kernel void func1(global int *data) {
9+
local int arr[32];
10+
11+
local int *ptr = arr;
12+
// CIR: cir.cast(array_to_ptrdecay, %{{[0-9]+}} : !cir.ptr<!cir.array<!s32i x 32>, addrspace(offload_local)>), !cir.ptr<!s32i, addrspace(offload_local)>
13+
// CIR-NEXT: cir.store %{{[0-9]+}}, %{{[0-9]+}} : !cir.ptr<!s32i, addrspace(offload_local)>, !cir.ptr<!cir.ptr<!s32i, addrspace(offload_local)>, addrspace(offload_private)>
14+
15+
// LLVM: store ptr addrspace(3) @func1.arr, ptr %{{[0-9]+}}
16+
}
17+
18+
// CIR: @func2
19+
// LLVM: @func2
20+
kernel void func2(global int *data) {
21+
private int arr[32] = {data[2]};
22+
// CIR: %{{[0-9]+}} = cir.cast(array_to_ptrdecay, %{{[0-9]+}} : !cir.ptr<!cir.array<!s32i x 32>, addrspace(offload_private)>), !cir.ptr<!s32i, addrspace(offload_private)>
23+
24+
// LLVM: %{{[0-9]+}} = getelementptr i32, ptr %3, i32 0
25+
}

‎clang/test/CIR/IR/invalid.cir

+13
Original file line numberDiff line numberDiff line change
@@ -1285,3 +1285,16 @@ module {
12851285
cir.return
12861286
}
12871287
}
1288+
1289+
// -----
1290+
1291+
!s32i = !cir.int<s, 32>
1292+
1293+
module {
1294+
cir.func @array_to_ptrdecay_addrspace() {
1295+
%0 = cir.alloca !cir.array<!s32i x 32>, !cir.ptr<!cir.array<!s32i x 32>, addrspace(offload_private)>, ["x", init]
1296+
// expected-error@+1 {{requires same address space for source and result}}
1297+
%1 = cir.cast(array_to_ptrdecay, %0 : !cir.ptr<!cir.array<!s32i x 32>, addrspace(offload_private)>), !cir.ptr<!s32i>
1298+
cir.return
1299+
}
1300+
}

0 commit comments

Comments
 (0)
Please sign in to comment.