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

[CIR][CIRGen] Add support for __fp16 type #950

Merged
merged 1 commit into from
Oct 9, 2024

Conversation

Lancern
Copy link
Member

@Lancern Lancern commented Oct 8, 2024

This PR adds support for the __fp16 type. CIRGen and LLVM lowering is included.

Resolve #900 .

Copy link
Member

@bcardosolopes bcardosolopes left a comment

Choose a reason for hiding this comment

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

Thanks for addressing this, pretty cool!

Have you thought about making fp16 having an extra argument that tells whether it's soft? Seems like it would make CIRGen more simple and offset the intrinsic calls to lowering time, but maybe this is just my not-so-informed opinion?

a single- or double-precision floating point value before performing any
arithmetic operations.

Additionally, `!cir.f16.storage` is lowered to the `i16` LLVM type. The
Copy link
Member

Choose a reason for hiding this comment

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

In the LLVM lowering pipeline ...


Additionally, `!cir.f16.storage` is lowered to the `i16` LLVM type. The
promotion and un-promotion of `!cir.f16.storage` values are lowered to
calls to `llvm.convert.from.f16` and `llvm.convert.to.f16` LLVM intrinsic
Copy link
Member

Choose a reason for hiding this comment

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

... calls to cir.llvm.intrinsic convert.from.f16 and ...

def CIR_StorageOnlyFP16 : CIR_FloatType<"StorageOnlyFP16", "f16.storage"> {
let summary = "CIR type that represents a storage-only fp16 type";
let description = [{
Floating-point type that represents a storage-only fp16 type.
Copy link
Member

Choose a reason for hiding this comment

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

Should we call this fp16.soft? This comes from the usual semantics we get for more regular floats when -msoft-float is used, which seems to be similar here.

} else {
value = Builder.createCast(CGF.getLoc(E->getExprLoc()),
mlir::cir::CastKind::floating, input,
CGF.CGM.FloatTy);
Copy link
Collaborator

Choose a reason for hiding this comment

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

Do we have a real world case for this else branch to test?
I'm thinking about how we lower this CastOp in current LLVM Lowering.
it looks like it may exercise this lowering code
rewriter.replaceOpWithNewOp<mlir::LLVM::FPExtOp>
And mlir::LLVM::FPExtOp is expecting floating type arg,
But your type converter converts cir.f16.storage into MLIR::IntegerType.....

Copy link
Member Author

@Lancern Lancern Oct 9, 2024

Choose a reason for hiding this comment

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

This else branch is the normal case and it is hit when input is of type !cir.f16. When input is of type !cir.f16.storage, the then branch would be taken instead.

Do we have a real world case for this else branch to test?

Well since the else branch is the normal one, let's consider instead the then branch. I searched in the upstream clang tests with llvm.convert.from.fp16 and remarkably I found nothing. For most targets the useFP16ConversionIntrinsics() function call would return false, which indicates for most targets the else branch would be taken. But there are indeed some exceptions, for example mips-unknown-linux-gnu: https://godbolt.org/z/feGvPYGxE , unfortunately CIR now supports none of these exceptions.

Anyway for now I think I'm going to remove the !cir.f16.storage stuff and make it assert on relevant paths, including the then branch here.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Yep, you are right, the else branch is the most common case, and it should takes existing cir.f16 type as input. And lowering supports it without problem.

@Lancern
Copy link
Member Author

Lancern commented Oct 9, 2024

Have you thought about making fp16 having an extra argument that tells whether it's soft?

Yes I implemented this like what you said in the first attempt, but I eventually decided to invent a new type for soft fp16 since I feel we should not add soft fp16 type to the CIR_AnyFloat constraint.

For now, as said in comment #950 (comment), I'm going to remove this new type from this PR since CIR yet supports none of the targets that generate this new type.

@Lancern Lancern changed the title [CIR] Add support for __fp16 type [CIR][CIRGen] Add support for __fp16 type Oct 9, 2024
@Lancern Lancern marked this pull request as ready for review October 9, 2024 12:18
@Lancern Lancern requested a review from lanza as a code owner October 9, 2024 12:18
@ghehg
Copy link
Collaborator

ghehg commented Oct 9, 2024

LGTM, especially like the new update that focuses on supporting the common case. We can also add FP16Storage type and intrinsics when we have a need for it.

@bcardosolopes
Copy link
Member

Yes I implemented this like what you said in the first attempt, but I eventually decided to invent a new type for soft fp16 since I feel we should not add soft fp16 type to the CIR_AnyFloat constraint.

Just a quick reminder that CIR_AnyFloat and friends are shortcuts for providing reliable constraints, if we ever need to specially this further, e.g. CIR_AnyNonSoftFloat, it should be fine. Simpler CIRGen is more welcome than having to change constraints to be more specific into what gets accepted by operations.

@bcardosolopes bcardosolopes merged commit a621989 into llvm:main Oct 9, 2024
8 checks passed
smeenai pushed a commit to smeenai/clangir that referenced this pull request Oct 9, 2024
This PR adds support for the `__fp16` type. CIRGen and LLVM lowering is
included.

Resolve llvm#900 .
@Lancern Lancern deleted the fp16-support branch October 10, 2024 02:04
keryell pushed a commit to keryell/clangir that referenced this pull request Oct 19, 2024
This PR adds support for the `__fp16` type. CIRGen and LLVM lowering is
included.

Resolve llvm#900 .
lanza pushed a commit that referenced this pull request Nov 5, 2024
This PR adds support for the `__fp16` type. CIRGen and LLVM lowering is
included.

Resolve #900 .
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Lack of support on __fp16 type
3 participants