Skip to content

Commit 6c35a8f

Browse files
committed
[CIR][Lowering][MLIR] Lower cir.cast(bitcast) between !cir.ptr
1 parent 1097f87 commit 6c35a8f

File tree

1 file changed

+20
-1
lines changed

1 file changed

+20
-1
lines changed

clang/lib/CIR/Lowering/ThroughMLIR/LowerCIRToMLIR.cpp

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -207,9 +207,11 @@ static bool findBaseAndIndices(mlir::Value addr, mlir::Value &base,
207207
while (mlir::Operation *addrOp = addr.getDefiningOp()) {
208208
if (!isa<mlir::memref::ReinterpretCastOp>(addrOp))
209209
break;
210-
indices.push_back(addrOp->getOperand(1));
211210
addr = addrOp->getOperand(0);
212211
eraseList.push_back(addrOp);
212+
// If there is another operand, assume it is the lowered index
213+
if (addrOp->getNumOperands() == 2)
214+
indices.push_back(addrOp->getOperand(1));
213215
}
214216
base = addr;
215217
if (indices.size() == 0)
@@ -1150,6 +1152,23 @@ class CIRCastOpLowering : public mlir::OpConversionPattern<cir::CastOp> {
11501152
op, newDstType, src, 0, std::nullopt, std::nullopt);
11511153
return mlir::success();
11521154
}
1155+
case CIR::bitcast: {
1156+
// clang-format off
1157+
// %7 = cir.cast(bitcast, %6 : !cir.ptr<!s32i>), !cir.ptr<!cir.array<!s32i x 8192>>
1158+
// Is lowered as
1159+
// memref<i32> → memref.reinterpret_cast → memref<8192xi32>
1160+
// clang-format on
1161+
auto newDstType = convertTy(dstType);
1162+
if (!(mlir::isa<mlir::MemRefType>(adaptor.getSrc().getType()) &&
1163+
mlir::isa<mlir::MemRefType>(newDstType)))
1164+
return op.emitError() << "NYI bitcast from " << op.getSrc().getType()
1165+
<< " to " << dstType;
1166+
auto dstMR = mlir::cast<mlir::MemRefType>(newDstType);
1167+
auto [strides, offset] = dstMR.getStridesAndOffset();
1168+
rewriter.replaceOpWithNewOp<mlir::memref::ReinterpretCastOp>(
1169+
op, dstMR, src, offset, dstMR.getShape(), strides);
1170+
return mlir::success();
1171+
}
11531172
case CIR::int_to_bool: {
11541173
auto zero = rewriter.create<cir::ConstantOp>(
11551174
src.getLoc(), op.getSrc().getType(),

0 commit comments

Comments
 (0)