Skip to content

Commit

Permalink
[flang] Support late math lowering for intrinsics from the llvm table.
Browse files Browse the repository at this point in the history
mathOperations should now support all intrinsics that are handled
by the llvmIntrinsics table + `tan` lowered as Math dialect operation +
f128 flavor of abs.

I am going to flip the default to late math lowering after this change,
but still keep the fallback via pgmath. This will allow getting rid
of the llvmIntrinsics table and continue populating
only the mathOperations table, otherwise, updating both tables
seems to be inconvenient.

Differential Revision: https://reviews.llvm.org/D130048
  • Loading branch information
vzakhari committed Jul 19, 2022
1 parent 0bc1009 commit fd4afa7
Show file tree
Hide file tree
Showing 4 changed files with 481 additions and 0 deletions.
18 changes: 18 additions & 0 deletions flang/lib/Lower/IntrinsicCall.cpp
Expand Up @@ -1044,6 +1044,11 @@ static mlir::FunctionType genF64F64FuncType(mlir::MLIRContext *context) {
return mlir::FunctionType::get(context, {t}, {t});
}

static mlir::FunctionType genF128F128FuncType(mlir::MLIRContext *context) {
mlir::Type t = mlir::FloatType::getF128(context);
return mlir::FunctionType::get(context, {t}, {t});
}

static mlir::FunctionType genF32F32F32FuncType(mlir::MLIRContext *context) {
auto t = mlir::FloatType::getF32(context);
return mlir::FunctionType::get(context, {t, t}, {t});
Expand Down Expand Up @@ -1179,6 +1184,8 @@ static mlir::Value genMathOp(fir::FirOpBuilder &builder, mlir::Location loc,
static constexpr MathOperation mathOperations[] = {
{"abs", "fabsf", genF32F32FuncType, genMathOp<mlir::math::AbsOp>},
{"abs", "fabs", genF64F64FuncType, genMathOp<mlir::math::AbsOp>},
{"abs", "llvm.fabs.f128", genF128F128FuncType,
genMathOp<mlir::math::AbsOp>},
// llvm.trunc behaves the same way as libm's trunc.
{"aint", "llvm.trunc.f32", genF32F32FuncType, genLibCall},
{"aint", "llvm.trunc.f64", genF64F64FuncType, genLibCall},
Expand All @@ -1196,6 +1203,8 @@ static constexpr MathOperation mathOperations[] = {
{"ceil", "ceil", genF64F64FuncType, genMathOp<mlir::math::CeilOp>},
{"cos", "cosf", genF32F32FuncType, genMathOp<mlir::math::CosOp>},
{"cos", "cos", genF64F64FuncType, genMathOp<mlir::math::CosOp>},
{"cosh", "coshf", genF32F32FuncType, genLibCall},
{"cosh", "cosh", genF64F64FuncType, genLibCall},
{"erf", "erff", genF32F32FuncType, genMathOp<mlir::math::ErfOp>},
{"erf", "erf", genF64F64FuncType, genMathOp<mlir::math::ErfOp>},
{"exp", "expf", genF32F32FuncType, genMathOp<mlir::math::ExpOp>},
Expand Down Expand Up @@ -1223,10 +1232,18 @@ static constexpr MathOperation mathOperations[] = {
genMathOp<mlir::math::CopySignOp>},
{"sign", "copysign", genF64F64F64FuncType,
genMathOp<mlir::math::CopySignOp>},
{"sign", "copysignl", genF80F80F80FuncType,
genMathOp<mlir::math::CopySignOp>},
{"sign", "llvm.copysign.f128", genF128F128F128FuncType,
genMathOp<mlir::math::CopySignOp>},
{"sin", "sinf", genF32F32FuncType, genMathOp<mlir::math::SinOp>},
{"sin", "sin", genF64F64FuncType, genMathOp<mlir::math::SinOp>},
{"sinh", "sinhf", genF32F32FuncType, genLibCall},
{"sinh", "sinh", genF64F64FuncType, genLibCall},
{"sqrt", "sqrtf", genF32F32FuncType, genMathOp<mlir::math::SqrtOp>},
{"sqrt", "sqrt", genF64F64FuncType, genMathOp<mlir::math::SqrtOp>},
{"tan", "tanf", genF32F32FuncType, genMathOp<mlir::math::TanOp>},
{"tan", "tan", genF64F64FuncType, genMathOp<mlir::math::TanOp>},
{"tanh", "tanhf", genF32F32FuncType, genMathOp<mlir::math::TanhOp>},
{"tanh", "tanh", genF64F64FuncType, genMathOp<mlir::math::TanhOp>},
};
Expand All @@ -1243,6 +1260,7 @@ static constexpr MathOperation mathOperations[] = {
static constexpr RuntimeFunction llvmIntrinsics[] = {
{"abs", "llvm.fabs.f32", genF32F32FuncType},
{"abs", "llvm.fabs.f64", genF64F64FuncType},
{"abs", "llvm.fabs.f128", genF128F128FuncType},
{"aint", "llvm.trunc.f32", genF32F32FuncType},
{"aint", "llvm.trunc.f64", genF64F64FuncType},
{"anint", "llvm.round.f32", genF32F32FuncType},
Expand Down

0 comments on commit fd4afa7

Please sign in to comment.