Skip to content

Commit fbbbc6a

Browse files
authored
[CIR][CIRGen] Support __builtin_huge_val for float type (#889)
as title. The test cases are from [clang codegen test case](https://github.com/llvm/clangir/blob/52323c17c6a3708b3eb72651465f7d4b82f057e7/clang/test/CodeGen/builtins.c#L37)
1 parent 599dc51 commit fbbbc6a

File tree

3 files changed

+47
-2
lines changed

3 files changed

+47
-2
lines changed

clang/lib/CIR/CodeGen/CIRGenBuilder.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -536,6 +536,15 @@ class CIRGenBuilderTy : public CIRBaseBuilderTy {
536536

537537
mlir::cir::ConstantOp getConstInt(mlir::Location loc, mlir::Type t,
538538
uint64_t C);
539+
540+
mlir::cir::ConstantOp getConstFP(mlir::Location loc, mlir::Type t,
541+
llvm::APFloat fpVal) {
542+
assert((mlir::isa<mlir::cir::SingleType, mlir::cir::DoubleType>(t)) &&
543+
"expected mlir::cir::SingleType or mlir::cir::DoubleType");
544+
return create<mlir::cir::ConstantOp>(loc, t,
545+
getAttr<mlir::cir::FPAttr>(t, fpVal));
546+
}
547+
539548
/// Create constant nullptr for pointer-to-data-member type ty.
540549
mlir::cir::ConstantOp getNullDataMemberPtr(mlir::cir::DataMemberType ty,
541550
mlir::Location loc) {

clang/lib/CIR/CodeGen/CIRGenBuiltin.cpp

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -313,8 +313,16 @@ RValue CIRGenFunction::buildBuiltinExpr(const GlobalDecl GD, unsigned BuiltinID,
313313
return RValue::get(builder.getConstInt(getLoc(E->getSourceRange()),
314314
Result.Val.getInt()));
315315
}
316-
if (Result.Val.isFloat())
317-
llvm_unreachable("NYI");
316+
if (Result.Val.isFloat()) {
317+
// Note: we are using result type of CallExpr to determine the type of
318+
// the constant. Clang Codegen uses the result value to make judgement
319+
// of the type. We feel it should be Ok to use expression type because
320+
// it is hard to imagine a builtin function evaluates to
321+
// a value that over/underflows its own defined type.
322+
mlir::Type resTy = getCIRType(E->getType());
323+
return RValue::get(builder.getConstFP(getLoc(E->getExprLoc()), resTy,
324+
Result.Val.getFloat()));
325+
}
318326
}
319327

320328
// If current long-double semantics is IEEE 128-bit, replace math builtins

clang/test/CIR/CodeGen/builtins.c

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// RUN: %clang_cc1 -triple aarch64-none-linux-android21 -fclangir -emit-cir %s -o %t.cir
2+
// RUN: FileCheck -check-prefix=CIR --input-file=%t.cir %s
3+
// RUN: %clang_cc1 -triple aarch64-none-linux-android21 -fclangir -emit-llvm %s -o %t.ll
4+
// RUN: FileCheck --check-prefix=LLVM --input-file=%t.ll %s
5+
6+
7+
void test1() {
8+
float f;
9+
double d;
10+
f = __builtin_huge_valf();
11+
d = __builtin_huge_val();
12+
}
13+
14+
// CIR-LABEL: test1
15+
// CIR: [[F:%.*]] = cir.alloca !cir.float, !cir.ptr<!cir.float>, ["f"] {alignment = 4 : i64}
16+
// CIR: [[D:%.*]] = cir.alloca !cir.double, !cir.ptr<!cir.double>, ["d"] {alignment = 8 : i64}
17+
// CIR: [[F_VAL:%.*]] = cir.const #cir.fp<0x7F800000> : !cir.float
18+
// CIR: cir.store [[F_VAL]], [[F]] : !cir.float, !cir.ptr<!cir.float>
19+
// CIR: [[D_VAL:%.*]] = cir.const #cir.fp<0x7FF0000000000000> : !cir.double
20+
// CIR: cir.store [[D_VAL]], [[D]] : !cir.double, !cir.ptr<!cir.double> loc(#loc17)
21+
// CIR: cir.return
22+
23+
// LLVM-LABEL: test1
24+
// [[F:%.*]] = alloca float, align 4
25+
// [[D:%.*]] = alloca double, align 8
26+
// store float 0x7FF0000000000000, ptr [[F]], align 4
27+
// store double 0x7FF0000000000000, ptr[[D]], align 8
28+
// ret void

0 commit comments

Comments
 (0)