Skip to content

Commit 15fcc6e

Browse files
committed
[CIR][Lowering] Fix inconditional sign extension on vec.cmp
1 parent fe8b9ca commit 15fcc6e

File tree

2 files changed

+26
-2
lines changed

2 files changed

+26
-2
lines changed

clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVM.cpp

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2004,8 +2004,15 @@ mlir::LogicalResult CIRToLLVMVecCmpOpLowering::matchAndRewrite(
20042004
} else {
20052005
return op.emitError() << "unsupported type for VecCmpOp: " << elementType;
20062006
}
2007-
rewriter.replaceOpWithNewOp<mlir::LLVM::SExtOp>(
2008-
op, typeConverter->convertType(op.getType()), bitResult);
2007+
2008+
// Check if the types are the same before generating SExtOp
2009+
auto targetType = typeConverter->convertType(op.getType());
2010+
if (bitResult.getType() == targetType) {
2011+
rewriter.replaceOp(op, bitResult);
2012+
} else {
2013+
rewriter.replaceOpWithNewOp<mlir::LLVM::SExtOp>(op, targetType, bitResult);
2014+
}
2015+
20092016
return mlir::success();
20102017
}
20112018

clang/test/CIR/Lowering/vec-cmp.cir

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// RUN: cir-opt %s -cir-to-llvm -o %t.cir
2+
// RUN: FileCheck --input-file=%t.cir %s -check-prefix=MLIR
3+
4+
!s16i = !cir.int<s, 16>
5+
!u16i = !cir.int<u, 16>
6+
!s1i = !cir.int<s, 1>
7+
8+
cir.func @vec_cmp(%0: !cir.vector<!s16i x 16>, %1: !cir.vector<!s16i x 16>) -> () {
9+
%2 = cir.vec.cmp(lt, %0, %1) : !cir.vector<!s16i x 16>, !cir.vector<!cir.int<u, 1> x 16>
10+
%3 = cir.cast(bitcast, %2 : !cir.vector<!cir.int<u, 1> x 16>), !u16i
11+
cir.return
12+
}
13+
14+
// MLIR: llvm.func @vec_cmp
15+
// MLIR-NEXT: %{{[0-9]+}} = llvm.icmp "slt" %arg0, %arg1 : vector<16xi16>
16+
// MLIR-NEXT: %{{[0-9]+}} = llvm.bitcast %{{[0-9]+}} : vector<16xi1> to i16
17+
// MLIR-NEXT: llvm.return

0 commit comments

Comments
 (0)