Skip to content

Commit aa7b5c6

Browse files
PikachuHyALancern
andauthored
[CIR][CIRGen] Change SignBitOp result type to !cir.bool (#1187)
Co-authored-by: Sirui Mu <[email protected]>
1 parent 888f00c commit aa7b5c6

File tree

4 files changed

+11
-15
lines changed

4 files changed

+11
-15
lines changed

clang/include/clang/CIR/Dialect/Builder/CIRBaseBuilder.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -414,7 +414,7 @@ class CIRBaseBuilderTy : public mlir::OpBuilder {
414414
}
415415

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

clang/include/clang/CIR/Dialect/IR/CIROps.td

+3-3
Original file line numberDiff line numberDiff line change
@@ -5274,11 +5274,11 @@ def AtomicCmpXchg : CIR_Op<"atomic.cmp_xchg",
52745274
def SignBitOp : CIR_Op<"signbit", [Pure]> {
52755275
let summary = "Checks the sign of a floating-point number";
52765276
let description = [{
5277-
It returns a non-zero value (true) if the number is negative
5278-
and zero (false) if the number is positive or zero.
5277+
It returns whether the sign bit (i.e. the highest bit) of the input operand
5278+
is set.
52795279
}];
52805280
let arguments = (ins CIR_AnyFloat:$input);
5281-
let results = (outs SInt32:$res);
5281+
let results = (outs CIR_BoolType:$res);
52825282
let assemblyFormat = [{
52835283
$input attr-dict `:` type($input) `->` qualified(type($res))
52845284
}];

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

+1-2
Original file line numberDiff line numberDiff line change
@@ -3832,8 +3832,7 @@ mlir::LogicalResult CIRToLLVMSignBitOpLowering::matchAndRewrite(
38323832
auto cmpResult = rewriter.create<mlir::LLVM::ICmpOp>(
38333833
op.getLoc(), mlir::LLVM::ICmpPredicate::slt, bitcast.getResult(), zero);
38343834
auto converted = rewriter.create<mlir::LLVM::ZExtOp>(
3835-
op.getLoc(), mlir::IntegerType::get(rewriter.getContext(), 32),
3836-
cmpResult);
3835+
op.getLoc(), getTypeConverter()->convertType(op.getType()), cmpResult);
38373836
rewriter.replaceOp(op, converted);
38383837
return mlir::success();
38393838
}

clang/test/CIR/CodeGen/builtin-signbit.c

+6-9
Original file line numberDiff line numberDiff line change
@@ -5,31 +5,28 @@
55

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

1615
void test_signbit_double(double val) {
1716
// CIR-LABEL: test_signbit_double
18-
// CIR: %{{.+}} = cir.signbit %{{.+}} : !cir.float -> !s32i
17+
// CIR: %{{.+}} = cir.signbit %{{.+}} : !cir.float -> !cir.bool
1918
// LLVM-LABEL: test_signbit_double
2019
// LLVM: [[CONV:%.*]] = fptrunc double %{{.+}} to float
2120
// LLVM: [[TMP1:%.*]] = bitcast float [[CONV]] to i32
2221
// LLVM: [[TMP2:%.*]] = icmp slt i32 [[TMP1]], 0
23-
// LLVM: %{{.+}} = zext i1 [[TMP2]] to i32
24-
__builtin_signbitf(val);
22+
if (__builtin_signbitf(val)) {}
2523
}
2624

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

0 commit comments

Comments
 (0)