Repository: llvm/llvm-project
File: clang/lib/CIR/CodeGen/CIRGenBuiltin.cpp (C++)

Code before:
  case Builtin::BI__builtin_reduce_add:
  case Builtin::BI__builtin_reduce_mul:
  case Builtin::BI__builtin_reduce_xor:
  case Builtin::BI__builtin_reduce_or:
  case Builtin::BI__builtin_reduce_and:
  case Builtin::BI__builtin_reduce_assoc_fadd:
  case Builtin::BI__builtin_reduce_in_order_fadd:
  case Builtin::BI__builtin_reduce_maximum:
  case Builtin::BI__builtin_reduce_minimum:
  case Builtin::BI__builtin_matrix_transpose:
  case Builtin::BI__builtin_matrix_column_major_load:
  case Builtin::BI__builtin_matrix_column_major_store:
  case Builtin::BI__builtin_masked_load:
  case Builtin::BI__builtin_masked_expand_load:
  case Builtin::BI__builtin_masked_gather:
  case Builtin::BI__builtin_masked_store:
  case Builtin::BI__builtin_masked_compress_store:
  case Builtin::BI__builtin_masked_scatter:
    return errorBuiltinNYI(*this, e, builtinID);
  case Builtin::BI__builtin_isinf_sign: {
    CIRGenFunction::CIRGenFPOptionsRAII FPOptsRAII(*this, e);
    mlir::Location loc = getLoc(e->getBeginLoc());
    mlir::Value arg = emitScalarExpr(e->getArg(0));
    mlir::Value absArg = cir::FAbsOp::create(builder, loc, arg.getType(), arg);
    mlir::Value isInf =
        builder.createIsFPClass(loc, absArg, cir::FPClassTest::Infinity);
    mlir::Value isNeg = emitSignBit(loc, *this, arg);
    mlir::Type intTy = convertType(e->getType());
    cir::ConstantOp zero = builder.getNullValue(intTy, loc);
    cir::ConstantOp one = builder.getConstant(loc, cir::IntAttr::get(intTy, 1));
    cir::ConstantOp negativeOne =
        builder.getConstant(loc, cir::IntAttr::get(intTy, -1));
    mlir::Value signResult = builder.createSelect(loc, isNeg, negativeOne, one);
    mlir::Value result = builder.createSelect(loc, isInf, signResult, zero);
    return RValue::get(result);
  }
  case Builtin::BI__builtin_flt_rounds:
  case Builtin::BI__builtin_set_flt_rounds:
  case Builtin::BI__builtin_fpclassify:
    return errorBuiltinNYI(*this, e, builtinID);
  case Builtin::BIalloca:
  case Builtin::BI_alloca:
  case Builtin::BI__builtin_alloca_uninitialized:
  case Builtin::BI__builtin_alloca:
    return emitBuiltinAlloca(*this, e, builtinID);
  case Builtin::BI__builtin_alloca_with_align_uninitialized:
  case Builtin::BI__builtin_alloca_with_align:
  case Builtin::BI__builtin_infer_alloc_token:
  case Builtin::BIbzero:
  case Builtin::BI__builtin_bzero:
  case Builtin::BIbcopy:

Review comment (suggestion):
There's no need to do both `fabs` and `isFPClass`. Classic codegen uses `fabs` so that it can compare against positive infinity. If you're going to use `isFPClass` that alone is sufficient (and it will be canonicalized to `fabs` + compare to +INF).

Code after:
  case Builtin::BI__builtin_reduce_add:
  case Builtin::BI__builtin_reduce_mul:
  case Builtin::BI__builtin_reduce_xor:
  case Builtin::BI__builtin_reduce_or:
  case Builtin::BI__builtin_reduce_and:
  case Builtin::BI__builtin_reduce_assoc_fadd:
  case Builtin::BI__builtin_reduce_in_order_fadd:
  case Builtin::BI__builtin_reduce_maximum:
  case Builtin::BI__builtin_reduce_minimum:
  case Builtin::BI__builtin_matrix_transpose:
  case Builtin::BI__builtin_matrix_column_major_load:
  case Builtin::BI__builtin_matrix_column_major_store:
  case Builtin::BI__builtin_masked_load:
  case Builtin::BI__builtin_masked_expand_load:
  case Builtin::BI__builtin_masked_gather:
  case Builtin::BI__builtin_masked_store:
  case Builtin::BI__builtin_masked_compress_store:
  case Builtin::BI__builtin_masked_scatter:
    return errorBuiltinNYI(*this, e, builtinID);
  case Builtin::BI__builtin_isinf_sign: {
    CIRGenFunction::CIRGenFPOptionsRAII FPOptsRAII(*this, e);
    mlir::Location loc = getLoc(e->getBeginLoc());
    mlir::Value arg = emitScalarExpr(e->getArg(0));
    mlir::Value isInf =
        builder.createIsFPClass(loc, arg, cir::FPClassTest::Infinity);
    mlir::Value isNeg = emitSignBit(loc, *this, arg);
    mlir::Type intTy = convertType(e->getType());
    cir::ConstantOp zero = builder.getNullValue(intTy, loc);
    cir::ConstantOp one = builder.getConstant(loc, cir::IntAttr::get(intTy, 1));
    cir::ConstantOp negativeOne =
        builder.getConstant(loc, cir::IntAttr::get(intTy, -1));
    mlir::Value signResult = builder.createSelect(loc, isNeg, negativeOne, one);
    mlir::Value result = builder.createSelect(loc, isInf, signResult, zero);
    return RValue::get(result);
  }
  case Builtin::BI__builtin_flt_rounds:
  case Builtin::BI__builtin_set_flt_rounds:
  case Builtin::BI__builtin_fpclassify:
    return errorBuiltinNYI(*this, e, builtinID);
  case Builtin::BIalloca:
  case Builtin::BI_alloca:
  case Builtin::BI__builtin_alloca_uninitialized:
  case Builtin::BI__builtin_alloca:
    return emitBuiltinAlloca(*this, e, builtinID);
  case Builtin::BI__builtin_alloca_with_align_uninitialized:
  case Builtin::BI__builtin_alloca_with_align:
  case Builtin::BI__builtin_infer_alloc_token:
  case Builtin::BIbzero:
  case Builtin::BI__builtin_bzero:
  case Builtin::BIbcopy:
  case Builtin::BI__builtin_bcopy: