Skip to content

Commit 827f592

Browse files
committed
[CIR][Lowering] Fix Vector Comparison Lowering with -fno-signed-char/unsigned operand
1 parent 6b2ea18 commit 827f592

File tree

2 files changed

+18
-2
lines changed

2 files changed

+18
-2
lines changed

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

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2044,6 +2044,15 @@ mlir::LogicalResult CIRToLLVMVecCreateOpLowering::matchAndRewrite(
20442044
return mlir::success();
20452045
}
20462046

2047+
static bool isCIRZeroVector(mlir::Value value) {
2048+
if (auto constantOp = value.getDefiningOp<cir::ConstantOp>()) {
2049+
if (auto zeroAttr = mlir::dyn_cast<cir::ZeroAttr>(constantOp.getValue())) {
2050+
return true;
2051+
}
2052+
}
2053+
return false;
2054+
}
2055+
20472056
mlir::LogicalResult CIRToLLVMVecCmpOpLowering::matchAndRewrite(
20482057
cir::VecCmpOp op, OpAdaptor adaptor,
20492058
mlir::ConversionPatternRewriter &rewriter) const {
@@ -2052,9 +2061,16 @@ mlir::LogicalResult CIRToLLVMVecCmpOpLowering::matchAndRewrite(
20522061
auto elementType = elementTypeIfVector(op.getLhs().getType());
20532062
mlir::Value bitResult;
20542063
if (auto intType = mlir::dyn_cast<cir::IntType>(elementType)) {
2064+
2065+
bool shouldUseSigned = intType.isSigned();
2066+
// Special treatment For sign-bit extraction patterns (lt comparison with
2067+
// zero), always use signed comparison to preserve the semantic intent
2068+
if (op.getKind() == cir::CmpOpKind::lt && isCIRZeroVector(op.getRhs())) {
2069+
shouldUseSigned = true;
2070+
}
20552071
bitResult = rewriter.create<mlir::LLVM::ICmpOp>(
20562072
op.getLoc(),
2057-
convertCmpKindToICmpPredicate(op.getKind(), intType.isSigned()),
2073+
convertCmpKindToICmpPredicate(op.getKind(), shouldUseSigned),
20582074
adaptor.getLhs(), adaptor.getRhs());
20592075
} else if (mlir::isa<cir::FPTypeInterface>(elementType)) {
20602076
bitResult = rewriter.create<mlir::LLVM::FCmpOp>(

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,4 +26,4 @@ cir.func @vec_cmp_zero(%0: !cir.vector<!u8i x 16>) -> () {
2626

2727
// MLIR: llvm.func @vec_cmp_zero
2828
// MLIR: %{{[0-9]+}} = llvm.icmp "slt" %arg0, %{{[0-9]+}} : vector<16xi8>
29-
// MLIR-NEXT: %{{[0-9]+}} = llvm.bitcast %{{[0-9]+}} : vector<16xi1> to i16
29+
// MLIR-NEXT: %{{[0-9]+}} = llvm.bitcast %{{[0-9]+}} : vector<16xi1> to i16

0 commit comments

Comments
 (0)