Skip to content
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

Implementation of tanh lowering. #9

Closed
wants to merge 1 commit into from
Closed
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
40 changes: 39 additions & 1 deletion mlir/lib/Conversion/StandardToLLVM/StandardToLLVM.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1368,6 +1368,44 @@ struct ReOpLowering : public ConvertOpToLLVMPattern<ReOp> {
}
};

struct TanhOpLowering : public ConvertOpToLLVMPattern<TanhOp> {
using ConvertOpToLLVMPattern<TanhOp>::ConvertOpToLLVMPattern;

LogicalResult
matchAndRewrite(Operation *op, ArrayRef<Value> operands,
ConversionPatternRewriter &rewriter) const override {
if (operands.size() != 1)
return failure();
Type resultType = op->getResult(0).getType();
const char* funcName;
LLVM::LLVMType llvmResultType;
if (resultType.isF32()) {
funcName = static_cast<const char*>("tanhf");
llvmResultType = LLVM::LLVMType::getFloatTy(&getDialect());
}
else if (resultType.isF64()) {
funcName = static_cast<const char*>("tanh");
llvmResultType = LLVM::LLVMType::getDoubleTy(&getDialect());
}
else
return failure();

// Insert the appropriate tanh declaration if it is not already present.
auto tanhFunc =
op->getParentOfType<ModuleOp>().lookupSymbol<LLVM::LLVMFuncOp>(funcName);
if (!tanhFunc) {
OpBuilder moduleBuilder(op->getParentOfType<ModuleOp>().getBodyRegion());
tanhFunc = moduleBuilder.create<LLVM::LLVMFuncOp>(
op->getLoc(), funcName,
LLVM::LLVMType::getFunctionTy(llvmResultType, {llvmResultType},
/*isVarArg=*/false));
}
rewriter.replaceOpWithNewOp<LLVM::CallOp>(
op, ArrayRef<Type>(llvmResultType), rewriter.getSymbolRefAttr(tanhFunc), operands);
return success();
}
};

struct ImOpLowering : public ConvertOpToLLVMPattern<ImOp> {
using ConvertOpToLLVMPattern<ImOp>::ConvertOpToLLVMPattern;

Expand Down Expand Up @@ -2977,6 +3015,7 @@ void mlir::populateStdToLLVMNonMemoryConversionPatterns(
SubCFOpLowering,
SubFOpLowering,
SubIOpLowering,
TanhOpLowering,
TruncateIOpLowering,
UnsignedDivIOpLowering,
UnsignedRemIOpLowering,
Expand Down Expand Up @@ -3147,7 +3186,6 @@ mlir::LLVMConversionTarget::LLVMConversionTarget(MLIRContext &ctx)
: ConversionTarget(ctx) {
this->addLegalDialect<LLVM::LLVMDialect>();
this->addIllegalOp<LLVM::DialectCastOp>();
this->addIllegalOp<TanhOp>();
}

std::unique_ptr<OperationPass<ModuleOp>>
Expand Down
20 changes: 20 additions & 0 deletions mlir/test/Conversion/StandardToLLVM/standard-to-llvm.mlir
Original file line number Diff line number Diff line change
Expand Up @@ -87,3 +87,23 @@ func @unknown_source() -> i32 {
// expected-error@+1 {{must be LLVM dialect type}}
return %1 : i32
}

// -----

// CHECK-LABEL: @tanh_float
func @tanh_float() {
%c0 = constant 1.0 : f32
// CHECK: %[[.*]] = llvm.call @tanh(%[[.*]]) : (!llvm.float) -> !llvm.float
%1 = tanh %c0 : f32
return
}

// -----

// CHECK-LABEL: @tanh_double
func @tanh_double() {
%c0 = constant 1.0 : f64
// CHECK: %[[.*]] = llvm.call @tanh(%[[.*]]) : (!llvm.double) -> !llvm.double
%1 = tanh %c0 : f64
return
}