Skip to content

Commit 4397440

Browse files
seven-milelanza
authored andcommitted
[CIR][CodeGen] Support side effects in address space casting (#673)
Continue the work of #652 . Test the branch of null pointer expressions with side effects.
1 parent ef1d41a commit 4397440

File tree

2 files changed

+13
-3
lines changed

2 files changed

+13
-3
lines changed

clang/lib/CIR/CodeGen/CIRGenExprScalar.cpp

+2-3
Original file line numberDiff line numberDiff line change
@@ -1522,9 +1522,8 @@ mlir::Value ScalarExprEmitter::VisitCastExpr(CastExpr *CE) {
15221522
// If E has side effect, it is emitted even if its final result is a
15231523
// null pointer. In that case, a DCE pass should be able to
15241524
// eliminate the useless instructions emitted during translating E.
1525-
if (Result.HasSideEffects) {
1526-
llvm_unreachable("NYI");
1527-
}
1525+
if (Result.HasSideEffects)
1526+
Visit(E);
15281527
return CGF.CGM.buildNullConstant(DestTy, CGF.getLoc(E->getExprLoc()));
15291528
}
15301529
// Since target may map different address spaces in AST to the same address

clang/test/CIR/CodeGen/address-space-conversion.cpp

+11
Original file line numberDiff line numberDiff line change
@@ -55,3 +55,14 @@ void test_nullptr() {
5555
// LLVM: store ptr addrspace(1) null, ptr %{{[0-9]+}}, align 8
5656
// LLVM-NEXT: store ptr addrspace(2) null, ptr %{{[0-9]+}}, align 8
5757
}
58+
59+
void test_side_effect(pi1_t b) {
60+
pi2_t p = (pi2_t)(*b++, (int*)0);
61+
// CIR: %{{[0-9]+}} = cir.ptr_stride(%{{[0-9]+}} : !cir.ptr<!s32i, addrspace(1)>, %{{[0-9]+}} : !s32i), !cir.ptr<!s32i, addrspace(1)>
62+
// CIR: %[[#CAST:]] = cir.const #cir.ptr<null> : !cir.ptr<!s32i, addrspace(2)>
63+
// CIR-NEXT: cir.store %[[#CAST]], %{{[0-9]+}} : !cir.ptr<!s32i, addrspace(2)>, !cir.ptr<!cir.ptr<!s32i, addrspace(2)>>
64+
65+
// LLVM: %{{[0-9]+}} = getelementptr i32, ptr addrspace(1) %{{[0-9]+}}, i64 1
66+
// LLVM: store ptr addrspace(2) null, ptr %{{[0-9]+}}, align 8
67+
68+
}

0 commit comments

Comments
 (0)