From 16d736ff52d30ffac54d796c2d21d6f6057f6c18 Mon Sep 17 00:00:00 2001 From: 7mile Date: Thu, 13 Jun 2024 02:47:26 +0800 Subject: [PATCH] [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. --- clang/lib/CIR/CodeGen/CIRGenExprScalar.cpp | 5 ++--- clang/test/CIR/CodeGen/address-space-conversion.cpp | 11 +++++++++++ 2 files changed, 13 insertions(+), 3 deletions(-) diff --git a/clang/lib/CIR/CodeGen/CIRGenExprScalar.cpp b/clang/lib/CIR/CodeGen/CIRGenExprScalar.cpp index 85e795d79537..b969bd9ffdbb 100644 --- a/clang/lib/CIR/CodeGen/CIRGenExprScalar.cpp +++ b/clang/lib/CIR/CodeGen/CIRGenExprScalar.cpp @@ -1522,9 +1522,8 @@ mlir::Value ScalarExprEmitter::VisitCastExpr(CastExpr *CE) { // If E has side effect, it is emitted even if its final result is a // null pointer. In that case, a DCE pass should be able to // eliminate the useless instructions emitted during translating E. - if (Result.HasSideEffects) { - llvm_unreachable("NYI"); - } + if (Result.HasSideEffects) + Visit(E); return CGF.CGM.buildNullConstant(DestTy, CGF.getLoc(E->getExprLoc())); } // Since target may map different address spaces in AST to the same address diff --git a/clang/test/CIR/CodeGen/address-space-conversion.cpp b/clang/test/CIR/CodeGen/address-space-conversion.cpp index 84adaa59ac51..1490a174892a 100644 --- a/clang/test/CIR/CodeGen/address-space-conversion.cpp +++ b/clang/test/CIR/CodeGen/address-space-conversion.cpp @@ -55,3 +55,14 @@ void test_nullptr() { // LLVM: store ptr addrspace(1) null, ptr %{{[0-9]+}}, align 8 // LLVM-NEXT: store ptr addrspace(2) null, ptr %{{[0-9]+}}, align 8 } + +void test_side_effect(pi1_t b) { + pi2_t p = (pi2_t)(*b++, (int*)0); + // CIR: %{{[0-9]+}} = cir.ptr_stride(%{{[0-9]+}} : !cir.ptr, %{{[0-9]+}} : !s32i), !cir.ptr + // CIR: %[[#CAST:]] = cir.const #cir.ptr : !cir.ptr + // CIR-NEXT: cir.store %[[#CAST]], %{{[0-9]+}} : !cir.ptr, !cir.ptr> + + // LLVM: %{{[0-9]+}} = getelementptr i32, ptr addrspace(1) %{{[0-9]+}}, i64 1 + // LLVM: store ptr addrspace(2) null, ptr %{{[0-9]+}}, align 8 + +}