Skip to content

Commit

Permalink
[CIR][CIRGen] Support __builtin_huge_val for float type (llvm#889)
Browse files Browse the repository at this point in the history
  • Loading branch information
ghehg authored and smeenai committed Oct 9, 2024
1 parent 6c60940 commit ffa2b3c
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 2 deletions.
9 changes: 9 additions & 0 deletions clang/lib/CIR/CodeGen/CIRGenBuilder.h
Original file line number Diff line number Diff line change
Expand Up @@ -536,6 +536,15 @@ class CIRGenBuilderTy : public CIRBaseBuilderTy {

mlir::cir::ConstantOp getConstInt(mlir::Location loc, mlir::Type t,
uint64_t C);

mlir::cir::ConstantOp getConstFP(mlir::Location loc, mlir::Type t,
llvm::APFloat fpVal) {
assert((mlir::isa<mlir::cir::SingleType, mlir::cir::DoubleType>(t)) &&
"expected mlir::cir::SingleType or mlir::cir::DoubleType");
return create<mlir::cir::ConstantOp>(loc, t,
getAttr<mlir::cir::FPAttr>(t, fpVal));
}

/// Create constant nullptr for pointer-to-data-member type ty.
mlir::cir::ConstantOp getNullDataMemberPtr(mlir::cir::DataMemberType ty,
mlir::Location loc) {
Expand Down
12 changes: 10 additions & 2 deletions clang/lib/CIR/CodeGen/CIRGenBuiltin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -313,8 +313,16 @@ RValue CIRGenFunction::buildBuiltinExpr(const GlobalDecl GD, unsigned BuiltinID,
return RValue::get(builder.getConstInt(getLoc(E->getSourceRange()),
Result.Val.getInt()));
}
if (Result.Val.isFloat())
llvm_unreachable("NYI");
if (Result.Val.isFloat()) {
// Note: we are using result type of CallExpr to determine the type of
// the constant. Clang Codegen uses the result value to make judgement
// of the type. We feel it should be Ok to use expression type because
// it is hard to imagine a builtin function evaluates to
// a value that over/underflows its own defined type.
mlir::Type resTy = getCIRType(E->getType());
return RValue::get(builder.getConstFP(getLoc(E->getExprLoc()), resTy,
Result.Val.getFloat()));
}
}

// If current long-double semantics is IEEE 128-bit, replace math builtins
Expand Down
28 changes: 28 additions & 0 deletions clang/test/CIR/CodeGen/builtins.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// RUN: %clang_cc1 -triple aarch64-none-linux-android21 -fclangir -emit-cir %s -o %t.cir
// RUN: FileCheck -check-prefix=CIR --input-file=%t.cir %s
// RUN: %clang_cc1 -triple aarch64-none-linux-android21 -fclangir -emit-llvm %s -o %t.ll
// RUN: FileCheck --check-prefix=LLVM --input-file=%t.ll %s


void test1() {
float f;
double d;
f = __builtin_huge_valf();
d = __builtin_huge_val();
}

// CIR-LABEL: test1
// CIR: [[F:%.*]] = cir.alloca !cir.float, !cir.ptr<!cir.float>, ["f"] {alignment = 4 : i64}
// CIR: [[D:%.*]] = cir.alloca !cir.double, !cir.ptr<!cir.double>, ["d"] {alignment = 8 : i64}
// CIR: [[F_VAL:%.*]] = cir.const #cir.fp<0x7F800000> : !cir.float
// CIR: cir.store [[F_VAL]], [[F]] : !cir.float, !cir.ptr<!cir.float>
// CIR: [[D_VAL:%.*]] = cir.const #cir.fp<0x7FF0000000000000> : !cir.double
// CIR: cir.store [[D_VAL]], [[D]] : !cir.double, !cir.ptr<!cir.double> loc(#loc17)
// CIR: cir.return

// LLVM-LABEL: test1
// [[F:%.*]] = alloca float, align 4
// [[D:%.*]] = alloca double, align 8
// store float 0x7FF0000000000000, ptr [[F]], align 4
// store double 0x7FF0000000000000, ptr[[D]], align 8
// ret void

0 comments on commit ffa2b3c

Please sign in to comment.