Skip to content

Commit

Permalink
[CIR][CIRGen] Builtins: focus on non fast math path
Browse files Browse the repository at this point in the history
The LLVM lowering actually maps to the version without fast math, to add
support for fast math we need to set the proper LLVM attribute on each of those
operations.
  • Loading branch information
bcardosolopes committed Aug 10, 2024
1 parent 90e8feb commit 9c4435d
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 23 deletions.
3 changes: 3 additions & 0 deletions clang/include/clang/CIR/MissingFeatures.h
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,9 @@ struct MissingFeatures {

// Fast math.
static bool fastMathGuard() { return false; }
// Should be implemented with a moduleOp level attribute and directly
// mapped to LLVM - those can be set directly for every relevant LLVM IR
// dialect operation (log10, ...).
static bool fastMathFlags() { return false; }
static bool fastMathFuncAttributes() { return false; }

Expand Down
30 changes: 10 additions & 20 deletions clang/lib/CIR/CodeGen/CIRGenBuiltin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -448,8 +448,7 @@ RValue CIRGenFunction::buildBuiltinExpr(const GlobalDecl GD, unsigned BuiltinID,
case Builtin::BI__builtin_cosf16:
case Builtin::BI__builtin_cosl:
case Builtin::BI__builtin_cosf128:
assert(getContext().getLangOpts().FastMath &&
"cir.cos is only expected under -ffast-math");
assert(!MissingFeatures::fastMathFlags());
return buildUnaryFPBuiltin<mlir::cir::CosOp>(*this, *E);

case Builtin::BIexp:
Expand All @@ -460,8 +459,7 @@ RValue CIRGenFunction::buildBuiltinExpr(const GlobalDecl GD, unsigned BuiltinID,
case Builtin::BI__builtin_expf16:
case Builtin::BI__builtin_expl:
case Builtin::BI__builtin_expf128:
assert(getContext().getLangOpts().FastMath &&
"cir.exp is only expected under -ffast-math");
assert(!MissingFeatures::fastMathFlags());
return buildUnaryFPBuiltin<mlir::cir::ExpOp>(*this, *E);

case Builtin::BIexp2:
Expand All @@ -472,8 +470,7 @@ RValue CIRGenFunction::buildBuiltinExpr(const GlobalDecl GD, unsigned BuiltinID,
case Builtin::BI__builtin_exp2f16:
case Builtin::BI__builtin_exp2l:
case Builtin::BI__builtin_exp2f128:
assert(getContext().getLangOpts().FastMath &&
"cir.exp2 is only expected under -ffast-math");
assert(!MissingFeatures::fastMathFlags());
return buildUnaryFPBuiltin<mlir::cir::Exp2Op>(*this, *E);

case Builtin::BIfabs:
Expand Down Expand Up @@ -540,8 +537,7 @@ RValue CIRGenFunction::buildBuiltinExpr(const GlobalDecl GD, unsigned BuiltinID,
case Builtin::BI__builtin_fmod:
case Builtin::BI__builtin_fmodf:
case Builtin::BI__builtin_fmodl:
assert(getContext().getLangOpts().FastMath &&
"cir.fmod is only expected under -ffast-math");
assert(!MissingFeatures::fastMathFlags());
return buildBinaryFPBuiltin<mlir::cir::FModOp>(*this, *E);

case Builtin::BI__builtin_fmodf16:
Expand All @@ -556,8 +552,7 @@ RValue CIRGenFunction::buildBuiltinExpr(const GlobalDecl GD, unsigned BuiltinID,
case Builtin::BI__builtin_logf16:
case Builtin::BI__builtin_logl:
case Builtin::BI__builtin_logf128:
assert(getContext().getLangOpts().FastMath &&
"cir.log is only expected under -ffast-math");
assert(!MissingFeatures::fastMathFlags());
return buildUnaryFPBuiltin<mlir::cir::LogOp>(*this, *E);

case Builtin::BIlog10:
Expand All @@ -568,8 +563,7 @@ RValue CIRGenFunction::buildBuiltinExpr(const GlobalDecl GD, unsigned BuiltinID,
case Builtin::BI__builtin_log10f16:
case Builtin::BI__builtin_log10l:
case Builtin::BI__builtin_log10f128:
assert(getContext().getLangOpts().FastMath &&
"cir.log10 is only expected under -ffast-math");
assert(!MissingFeatures::fastMathFlags());
return buildUnaryFPBuiltin<mlir::cir::Log10Op>(*this, *E);

case Builtin::BIlog2:
Expand All @@ -580,8 +574,7 @@ RValue CIRGenFunction::buildBuiltinExpr(const GlobalDecl GD, unsigned BuiltinID,
case Builtin::BI__builtin_log2f16:
case Builtin::BI__builtin_log2l:
case Builtin::BI__builtin_log2f128:
assert(getContext().getLangOpts().FastMath &&
"cir.log2 is only expected under -ffast-math");
assert(!MissingFeatures::fastMathFlags());
return buildUnaryFPBuiltin<mlir::cir::Log2Op>(*this, *E);

case Builtin::BInearbyint:
Expand All @@ -599,8 +592,7 @@ RValue CIRGenFunction::buildBuiltinExpr(const GlobalDecl GD, unsigned BuiltinID,
case Builtin::BI__builtin_pow:
case Builtin::BI__builtin_powf:
case Builtin::BI__builtin_powl:
assert(getContext().getLangOpts().FastMath &&
"cir.pow is only expected under -ffast-math");
assert(!MissingFeatures::fastMathFlags());
return RValue::get(
buildBinaryMaybeConstrainedFPBuiltin<mlir::cir::PowOp>(*this, *E));

Expand Down Expand Up @@ -636,8 +628,7 @@ RValue CIRGenFunction::buildBuiltinExpr(const GlobalDecl GD, unsigned BuiltinID,
case Builtin::BI__builtin_sinf16:
case Builtin::BI__builtin_sinl:
case Builtin::BI__builtin_sinf128:
assert(getContext().getLangOpts().FastMath &&
"cir.sin is only expected under -ffast-math");
assert(!MissingFeatures::fastMathFlags());
return buildUnaryFPBuiltin<mlir::cir::SinOp>(*this, *E);

case Builtin::BIsqrt:
Expand All @@ -648,8 +639,7 @@ RValue CIRGenFunction::buildBuiltinExpr(const GlobalDecl GD, unsigned BuiltinID,
case Builtin::BI__builtin_sqrtf16:
case Builtin::BI__builtin_sqrtl:
case Builtin::BI__builtin_sqrtf128:
assert(getContext().getLangOpts().FastMath &&
"cir.sqrt is only expected under -ffast-math");
assert(!MissingFeatures::fastMathFlags());
return buildUnaryFPBuiltin<mlir::cir::SqrtOp>(*this, *E);

case Builtin::BItrunc:
Expand Down
6 changes: 3 additions & 3 deletions clang/test/CIR/CodeGen/builtin-floating-point.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -ffast-math -fclangir -emit-cir -mmlir --mlir-print-ir-before=cir-lowering-prepare %s -o %t1.cir 2>&1 | FileCheck %s
// RUN: %clang_cc1 -triple aarch64-apple-darwin-macho -ffast-math -fclangir -emit-cir -mmlir --mlir-print-ir-before=cir-lowering-prepare %s -o %t1.cir 2>&1 | FileCheck %s --check-prefix=AARCH64
// RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -ffast-math -fclangir -emit-llvm -o %t.ll %s
// RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -fclangir -emit-cir -mmlir --mlir-print-ir-before=cir-lowering-prepare %s -o %t1.cir 2>&1 | FileCheck %s
// RUN: %clang_cc1 -triple aarch64-apple-darwin-macho -fclangir -emit-cir -mmlir --mlir-print-ir-before=cir-lowering-prepare %s -o %t1.cir 2>&1 | FileCheck %s --check-prefix=AARCH64
// RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -fclangir -emit-llvm -o %t.ll %s
// RUN: FileCheck --input-file=%t.ll %s --check-prefix=LLVM

// lround
Expand Down

0 comments on commit 9c4435d

Please sign in to comment.