Skip to content

Commit 0b9e699

Browse files
committed
[CIR][CIRGen] Support __builtin_isinf_sign
1 parent 637f2f3 commit 0b9e699

File tree

3 files changed

+47
-3
lines changed

3 files changed

+47
-3
lines changed

clang/lib/CIR/CodeGen/CIRGenBuiltin.cpp

+17
Original file line numberDiff line numberDiff line change
@@ -1424,6 +1424,23 @@ RValue CIRGenFunction::emitBuiltinExpr(const GlobalDecl GD, unsigned BuiltinID,
14241424
case Builtin::BI__builtin_matrix_column_major_store:
14251425
llvm_unreachable("BI__builtin_matrix_column_major_store NYI");
14261426

1427+
case Builtin::BI__builtin_isinf_sign: {
1428+
CIRGenFunction::CIRGenFPOptionsRAII FPOptsRAII(*this, E);
1429+
mlir::Location Loc = getLoc(E->getBeginLoc());
1430+
mlir::Value Arg = emitScalarExpr(E->getArg(0));
1431+
mlir::Value AbsArg = builder.create<cir::FAbsOp>(Loc, Arg.getType(), Arg);
1432+
mlir::Value IsInf =
1433+
builder.createIsFPClass(Loc, AbsArg, FPClassTest::fcInf);
1434+
mlir::Value IsNeg = emitSignBit(Loc, *this, Arg);
1435+
auto IntTy = convertType(E->getType());
1436+
auto Zero = builder.getNullValue(IntTy, Loc);
1437+
auto One = builder.getConstant(Loc, cir::IntAttr::get(IntTy, 1));
1438+
auto NegativeOne = builder.getConstant(Loc, cir::IntAttr::get(IntTy, -1));
1439+
auto SignResult = builder.createSelect(Loc, IsNeg, NegativeOne, One);
1440+
auto Result = builder.createSelect(Loc, IsInf, SignResult, Zero);
1441+
return RValue::get(Result);
1442+
}
1443+
14271444
case Builtin::BI__builtin_flt_rounds:
14281445
llvm_unreachable("BI__builtin_flt_rounds NYI");
14291446

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

+1-3
Original file line numberDiff line numberDiff line change
@@ -4067,9 +4067,7 @@ mlir::LogicalResult CIRToLLVMSignBitOpLowering::matchAndRewrite(
40674067
auto zero = rewriter.create<mlir::LLVM::ConstantOp>(op->getLoc(), intTy, 0);
40684068
auto cmpResult = rewriter.create<mlir::LLVM::ICmpOp>(
40694069
op.getLoc(), mlir::LLVM::ICmpPredicate::slt, bitcast.getResult(), zero);
4070-
auto converted = rewriter.create<mlir::LLVM::ZExtOp>(
4071-
op.getLoc(), getTypeConverter()->convertType(op.getType()), cmpResult);
4072-
rewriter.replaceOp(op, converted);
4070+
rewriter.replaceOp(op, cmpResult);
40734071
return mlir::success();
40744072
}
40754073

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -fclangir -emit-cir %s -o %t.cir
2+
// RUN: FileCheck --check-prefix=CIR --input-file=%t.cir %s
3+
// RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -fclangir -emit-llvm %s -o %t.ll
4+
// RUN: FileCheck --check-prefix=LLVM --input-file=%t.ll %s
5+
6+
int test_float_isinf_sign(float x) {
7+
// CIR-LABEL: test_float_isinf_sign
8+
// CIR: %[[TMP0:.*]] = cir.load %{{.*}} : !cir.ptr<!cir.float>, !cir.float
9+
// CIR: %[[TMP1:.*]] = cir.fabs %[[TMP0]] : !cir.float
10+
// CIR: %[[IS_INF:.*]] = cir.is_fp_class %[[TMP1]], 516 : (!cir.float) -> !cir.bool
11+
// CIR: %[[IS_NEG:.*]] = cir.signbit %[[TMP0]] : !cir.float -> !cir.bool
12+
// CIR: %[[C_0:.*]] = cir.const #cir.int<0> : !s32i
13+
// CIR: %[[C_1:.*]] = cir.const #cir.int<1> : !s32i
14+
// CIR: %[[C_m1:.*]] = cir.const #cir.int<-1> : !s32i
15+
// CIR: %[[TMP4:.*]] = cir.select if %[[IS_NEG]] then %[[C_m1]] else %[[C_1]] : (!cir.bool, !s32i, !s32i) -> !s32i
16+
// CIR: %[[RET:.*]] = cir.select if %[[IS_INF]] then %[[TMP4]] else %[[C_0]] : (!cir.bool, !s32i, !s32i) -> !s32i
17+
// CIR: cir.store %[[RET]], %{{.*}} : !s32i, !cir.ptr<!s32i>
18+
19+
// LLVM-LABEL: test_float_isinf_sign
20+
// LLVM: %[[TMP0:.*]] = load float, ptr %{{.*}}
21+
// LLVM: %[[TMP1:.*]] = call float @llvm.fabs.f32(float %[[TMP0]])
22+
// LLVM: %[[IS_INF:.*]] = call i1 @llvm.is.fpclass.f32(float %[[TMP1]], i32 516)
23+
// LLVM: %[[TMP1:.*]] = bitcast float %[[TMP0]] to i32
24+
// LLVM: %[[IS_NEG:.*]] = icmp slt i32 %[[TMP1]], 0
25+
// LLVM: %[[TMP2:.*]] = select i1 %[[IS_NEG]], i32 -1, i32 1
26+
// LLVM: %[[RET:.*]] = select i1 %[[IS_INF]], i32 %[[TMP2]], i32 0
27+
// LLVM: store i32 %[[RET]], ptr %{{.*}}, align 4
28+
return __builtin_isinf_sign(x);
29+
}

0 commit comments

Comments
 (0)