Skip to content
This repository has been archived by the owner on Apr 23, 2021. It is now read-only.

Added LLVM ops and lowering phases from standard dialect. #300

Closed
wants to merge 1 commit into from

Conversation

dfki-jugr
Copy link
Contributor

Added LLVM ops and lowering phases from standard dialect for FAbs, FCeil, Cos, FNeg, CopySign and Tanh.

Added test cases for the newly added LLVM operations and lowering features.

@pifon2a pifon2a self-requested a review December 6, 2019 14:17
@@ -793,12 +790,33 @@ struct SubFOpLowering : public BinaryOpLLVMOpLowering<SubFOp, LLVM::FSubOp> {
struct MulFOpLowering : public BinaryOpLLVMOpLowering<MulFOp, LLVM::FMulOp> {
using Super::Super;
};
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe have unary ops defs before binary ops sorted and also sort them?

test/Target/llvmir-intrinsics.mlir Show resolved Hide resolved
}];
}

def LLVM_TanhOp : LLVM_Op<"tanh", [NoSideEffect]>,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@River707, @ftynse, @joker-eph
What would be the right way to test values of Tanh? It is quite easy to introduce a bug.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We need a more detailed explanation of where these values come from and what is computed in the end.

}];
}

def LLVM_CosOp : LLVM_Op<"intr.cos", [NoSideEffect]>,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: sort defs (CosOp should be after Copysign)

Copy link
Contributor

@ftynse ftynse left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LLVM dialect should only contain LLVM instructions or intrinsics. There's no tanh in LLVM IR, so it should not be in the dialect. Consider implementing what you did in the builder as a conversion pattern from tanh to a combination of LLVM dialect operations.

Arguments<(ins LLVM_Type:$in)>,
Results<(outs LLVM_Type:$res)> {
let llvmBuilder = [{
llvm::Module *module = builder.GetInsertBlock()->getModule();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As we keep adding more intrinsics, I am more and more interesting in generalizing these. Have you considered it?

}];
}

def LLVM_FCeilOp : LLVM_Op<"intr.ceil", [NoSideEffect]>,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's a mismatch between op name (fceil) and intrinsic name (ceil), please fix.

def LLVM_TanhOp : LLVM_Op<"tanh", [NoSideEffect]>,
Arguments<(ins LLVM_Type:$in)>,
Results<(outs LLVM_Type:$res)> {
let llvmBuilder = [{
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is way too long to be inline code in Tablegen. Please extract into a function and just call it here.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@dfki-jugr Let's remove Tanh from the current PR. This logic be added to legalization from STD TanhOp to LLVM dialect, not from LLVM dialect to LLVM IR.

llvm::Module *module = builder.GetInsertBlock()->getModule();

llvm::Type *type = $in->getType();
llvm::Value *max_value = llvm::ConstantFP::get(type, 20.0);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

llvm::Value *input_clamped =
builder.CreateSelect(cmp_lower_bound, clamp_min, select_res);

static constexpr std::array<float, 7> numerator_coeffs{
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The must be accompanied by a detailed comment of what you are doing here and why it should work.

}];
}

def LLVM_TanhOp : LLVM_Op<"tanh", [NoSideEffect]>,
Copy link
Contributor

@ftynse ftynse Dec 6, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If there is no tanh op in LLVM, it must not be an LLVM Op. Create it in some dialect and implement a lowering from that dialect to the LLVM dialect instead, so the translation from the LLVM dialect to LLVM IR is as simple as possible. Having magic constants in the translation wouldn't cut it.

If you implement your non-trivial conversion in as a conversion pattern, it is significantly easier to replace.

@ftynse
Copy link
Contributor

ftynse commented Dec 6, 2019

please do not merge this until the concerns on translation are addressed.

Copy link
Contributor

@pifon2a pifon2a left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can submit this CL, after the TanhOp lowering is excluded and the comments are addressed.

test/Target/llvmir-intrinsics.mlir Show resolved Hide resolved
def LLVM_CosOp : LLVM_UnaryIntrinsicOp<"cos">;

def LLVM_CopySignOp : LLVM_BinaryIntrinsicOp<"copysign">;

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: remove empty line

…eil, Cos, FNeg, CopySign and Tanh.

Added test cases for the newly added LLVM operations and lowering features.
tensorflow-copybara pushed a commit to tensorflow/tensorflow that referenced this pull request Dec 18, 2019
…eil, Cos, FNeg, CopySign.

Added test cases for the newly added LLVM operations and lowering features.

Closes #300

COPYBARA_INTEGRATE_REVIEW=tensorflow/mlir#300 from dfki-jugr:std_to_llvm da6168bbc1a369ae2e99ad3881fdddd82f075dd4
PiperOrigin-RevId: 286231169
Change-Id: I364bda2adad4054052a3a53b4cb1c3de1102864f
swift-ci pushed a commit to swiftlang/llvm-project that referenced this pull request Dec 24, 2019
…eil, Cos, FNeg, CopySign.

Added test cases for the newly added LLVM operations and lowering features.

Closes tensorflow/mlir#300

COPYBARA_INTEGRATE_REVIEW=tensorflow/mlir#300 from dfki-jugr:std_to_llvm da6168bbc1a369ae2e99ad3881fdddd82f075dd4
PiperOrigin-RevId: 286231169
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants