Skip to content

Commit

Permalink
[CIR][CIRGen] Change SignBitOp result type to !cir.bool (#1187)
Browse files Browse the repository at this point in the history
Co-authored-by: Sirui Mu <[email protected]>
  • Loading branch information
PikachuHyA and Lancern authored Dec 2, 2024
1 parent 888f00c commit aa7b5c6
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 15 deletions.
2 changes: 1 addition & 1 deletion clang/include/clang/CIR/Dialect/Builder/CIRBaseBuilder.h
Original file line number Diff line number Diff line change
Expand Up @@ -414,7 +414,7 @@ class CIRBaseBuilderTy : public mlir::OpBuilder {
}

cir::SignBitOp createSignBit(mlir::Location loc, mlir::Value val) {
auto resTy = cir::IntType::get(getContext(), 32, true);
auto resTy = cir::BoolType::get(getContext());
return create<cir::SignBitOp>(loc, resTy, val);
}

Expand Down
6 changes: 3 additions & 3 deletions clang/include/clang/CIR/Dialect/IR/CIROps.td
Original file line number Diff line number Diff line change
Expand Up @@ -5274,11 +5274,11 @@ def AtomicCmpXchg : CIR_Op<"atomic.cmp_xchg",
def SignBitOp : CIR_Op<"signbit", [Pure]> {
let summary = "Checks the sign of a floating-point number";
let description = [{
It returns a non-zero value (true) if the number is negative
and zero (false) if the number is positive or zero.
It returns whether the sign bit (i.e. the highest bit) of the input operand
is set.
}];
let arguments = (ins CIR_AnyFloat:$input);
let results = (outs SInt32:$res);
let results = (outs CIR_BoolType:$res);
let assemblyFormat = [{
$input attr-dict `:` type($input) `->` qualified(type($res))
}];
Expand Down
3 changes: 1 addition & 2 deletions clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVM.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3832,8 +3832,7 @@ mlir::LogicalResult CIRToLLVMSignBitOpLowering::matchAndRewrite(
auto cmpResult = rewriter.create<mlir::LLVM::ICmpOp>(
op.getLoc(), mlir::LLVM::ICmpPredicate::slt, bitcast.getResult(), zero);
auto converted = rewriter.create<mlir::LLVM::ZExtOp>(
op.getLoc(), mlir::IntegerType::get(rewriter.getContext(), 32),
cmpResult);
op.getLoc(), getTypeConverter()->convertType(op.getType()), cmpResult);
rewriter.replaceOp(op, converted);
return mlir::success();
}
Expand Down
15 changes: 6 additions & 9 deletions clang/test/CIR/CodeGen/builtin-signbit.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,31 +5,28 @@

void test_signbit_float(float val) {
// CIR-LABEL: test_signbit_float
// CIR: %{{.+}} = cir.signbit %{{.+}} : !cir.float -> !s32i
// CIR: %{{.+}} = cir.signbit %{{.+}} : !cir.float -> !cir.bool
// LLVM-LABEL: test_signbit_float
// LLVM: [[TMP1:%.*]] = bitcast float %{{.+}} to i32
// LLVM: [[TMP2:%.*]] = icmp slt i32 [[TMP1]], 0
// LLVM: %{{.+}} = zext i1 [[TMP2]] to i32
__builtin_signbit(val);
if (__builtin_signbit(val)) {};
}

void test_signbit_double(double val) {
// CIR-LABEL: test_signbit_double
// CIR: %{{.+}} = cir.signbit %{{.+}} : !cir.float -> !s32i
// CIR: %{{.+}} = cir.signbit %{{.+}} : !cir.float -> !cir.bool
// LLVM-LABEL: test_signbit_double
// LLVM: [[CONV:%.*]] = fptrunc double %{{.+}} to float
// LLVM: [[TMP1:%.*]] = bitcast float [[CONV]] to i32
// LLVM: [[TMP2:%.*]] = icmp slt i32 [[TMP1]], 0
// LLVM: %{{.+}} = zext i1 [[TMP2]] to i32
__builtin_signbitf(val);
if (__builtin_signbitf(val)) {}
}

void test_signbit_long_double(long double val) {
// CIR: test_signbit_long_double
// LLVM: test_signbit_long_double
__builtin_signbitl(val);
// CIR: %{{.+}} = cir.signbit %{{.+}} : !cir.long_double<!cir.f80> -> !s32i
if (__builtin_signbitl(val)) {}
// CIR: %{{.+}} = cir.signbit %{{.+}} : !cir.long_double<!cir.f80> -> !cir.bool
// LLVM: [[TMP1:%.*]] = bitcast x86_fp80 %{{.+}} to i80
// LLVM: [[TMP2:%.*]] = icmp slt i80 [[TMP1]], 0
// LLVM: %{{.+}} = zext i1 [[TMP2]] to i32
}

0 comments on commit aa7b5c6

Please sign in to comment.