From 4a6412436b54167037daec2ce3c825f1293fc22f Mon Sep 17 00:00:00 2001 From: Shoaib Meenai Date: Mon, 7 Oct 2024 17:44:07 -0700 Subject: [PATCH] fixup! [CIR][LowerToLLVM] Fix crash in PtrStrideOp lowering After recent rebases, the LLVMIR stride operand can be generated by an unrealized_conversion_cast whereas the CIR operand is coming directly from a block argument (and thus has no defining op), so we need to separately check the CIR operand for the existence of a defining op. Fixes https://github.com/llvm/clangir/issues/912 --- clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVM.cpp | 4 ++-- clang/test/CIR/Lowering/ptrstride.cir | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVM.cpp b/clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVM.cpp index 0db060472d28..94d889d535d0 100644 --- a/clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVM.cpp +++ b/clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVM.cpp @@ -761,8 +761,8 @@ class CIRPtrStrideOpLowering // before it. To achieve that, look at unary minus, which already got // lowered to "sub 0, x". auto sub = dyn_cast(indexOp); - auto unary = - dyn_cast(ptrStrideOp.getStride().getDefiningOp()); + auto unary = dyn_cast_if_present( + ptrStrideOp.getStride().getDefiningOp()); bool rewriteSub = unary && unary.getKind() == mlir::cir::UnaryOpKind::Minus && sub; if (rewriteSub) diff --git a/clang/test/CIR/Lowering/ptrstride.cir b/clang/test/CIR/Lowering/ptrstride.cir index 344647e3dfd3..b5df897d2b0e 100644 --- a/clang/test/CIR/Lowering/ptrstride.cir +++ b/clang/test/CIR/Lowering/ptrstride.cir @@ -1,6 +1,5 @@ // RUN: cir-opt %s -cir-to-llvm -o %t.mlir // RUN: FileCheck %s --input-file=%t.mlir -check-prefix=MLIR -// XFAIL:* !s32i = !cir.int module { @@ -31,4 +30,5 @@ module { // MLIR: llvm.return // MLIR-LABEL: @g -// MLIR: llvm.getelementptr %arg0[%arg1] : (!llvm.ptr, i32) -> !llvm.ptr, i32 +// MLIR: %0 = llvm.sext %arg1 : i32 to i64 +// MLIR-NEXT: llvm.getelementptr %arg0[%0] : (!llvm.ptr, i64) -> !llvm.ptr, i32