Skip to content

[CIR][CIRGen] Support __builtin_huge_val for float type #889

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Sep 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
14 changes: 11 additions & 3 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 Expand Up @@ -1627,4 +1635,4 @@ mlir::cir::FuncOp CIRGenModule::getBuiltinLibFunction(const FunctionDecl *FD,

auto Ty = getTypes().ConvertType(FD->getType());
return GetOrCreateCIRFunction(Name, Ty, D, /*ForVTable=*/false);
}
}
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