Skip to content

Add EmitC support for f32 arithmetic ops - #5872

Merged
ScottTodd merged 1 commit into
iree-org:mainfrom
marbre:vm.arithmetic_ops_f32
May 18, 2021
Merged

ScottTodd merged 1 commit into
iree-org:mainfrom
marbre:vm.arithmetic_ops_f32

Conversation

@marbre

@marbre marbre commented May 13, 2021

Copy link
Copy Markdown
Member

No description provided.

@marbre
marbre requested a review from benvanik May 13, 2021 15:35
@google-cla google-cla Bot added the cla: yes label May 13, 2021
@marbre
marbre force-pushed the vm.arithmetic_ops_f32 branch 3 times, most recently from 416239b to 769785d Compare May 17, 2021 09:57
@marbre

marbre commented May 17, 2021

Copy link
Copy Markdown
Member Author

Rebased and added support for vm.fma.f32.

@marbre
marbre force-pushed the vm.arithmetic_ops_f32 branch from 769785d to e108b5d Compare May 17, 2021 10:03
@marbre
marbre requested a review from ScottTodd May 17, 2021 19:26
Comment on lines +725 to +728
// ExtF32: Native floating-point arithmetic
patterns.insert<CallOpConversion<IREE::VM::AddF32Op>>(context, "vm_add_f32");
patterns.insert<CallOpConversion<IREE::VM::SubF32Op>>(context, "vm_sub_f32");
patterns.insert<CallOpConversion<IREE::VM::MulF32Op>>(context, "vm_mul_f32");

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

200 lines of boilerplate code... this could be generated or assisted with a macro. WDYT?

(fine to land as-is, just wondering how we could make this easier to work on in the future)

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Definitely worth to think about a better solution. I think it could be possible to send a follow up where we have a single patterns.intert<> call. However, we still need to pass all the VM ops that can be converted to EmitC via the pattern. So I don't know if this removes the boilerplate much, which we also have in other places. For example see https://github.com/google/iree/blob/739a596b52226cd0539752ffa7bcce514234afa1/iree/compiler/Dialect/VM/Conversion/StandardToVM/ConvertStandardToVM.cpp#L778-L798

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

We discussed this internally and it should be indeed doable to have a single patterns.insert call. The C functions name to call could be either generated via tablegen or one could move the mapping into the CallOpConversion. However, this somehow moves the boilerplate, but does not really reduce it. Your thoughts @ScottTodd?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I don't really have much of an opinion on how the code is set up other than

  • modifying this set of conversions should be easy (the current structure is easy to edit but risks missing conversions)
  • if the set of conversions is 1:1 with no special cases then it could be fully generated, which would make it impossible to miss conversions

We have many other conversion patterns in the project, but these look particularly mechanical.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

I don't really have much of an opinion on how the code is set up other than

* modifying this set of conversions should be easy (the current structure is easy to edit but risks missing conversions)

That is true, but ...

* if the set of conversions is 1:1 with no special cases then it could be fully generated, which would make it impossible to miss conversions

... unfortunately, this is not a full 1:1 set. E.g. constants
https://github.com/google/iree/blob/5de65494a0edea0db932ac5f16a883c1bb8e8cb9/iree/compiler/Dialect/VM/Conversion/VMToEmitC/ConvertVMToEmitC.cpp#L652-L654

are not converted via a CallOpConversion and also lists vm.listas well as other ops need different conversions. For those we do not call functions in iree/vm/ops.h and generate more complex code instead. An (old) example showing the vm.listallocation is vizualized on slide 17 - 20 in the EmitC slides presented at the ODM last month. So at least for now I think we should land as is.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

An idea for the straightforward ones is as mentioned use tablegen - we already have a custom tblgen for handling the bytecode encoding, and could extend that to also support the c functions. This makes things declarative in all cases but those requiring extra lowering work. I'm not sure it's the most scalable solution though (especially if we add direct LLVM lowering, or something like an emitc# output, etc), so it may not be worth doing.

In general though I'm fine with having lots of one line pattern instantiations like this - it makes it really easy to find/replace them in the code, easy to add new ones (no ambiguity over whether to do it in tablegen or as a pattern or as an instantiation of a templated pattern), and scales linearly with the number of ops (which is fairly bounded).

Comment on lines +725 to +728
// ExtF32: Native floating-point arithmetic
patterns.insert<CallOpConversion<IREE::VM::AddF32Op>>(context, "vm_add_f32");
patterns.insert<CallOpConversion<IREE::VM::SubF32Op>>(context, "vm_sub_f32");
patterns.insert<CallOpConversion<IREE::VM::MulF32Op>>(context, "vm_mul_f32");

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I don't really have much of an opinion on how the code is set up other than

  • modifying this set of conversions should be easy (the current structure is easy to edit but risks missing conversions)
  • if the set of conversions is 1:1 with no special cases then it could be fully generated, which would make it impossible to miss conversions

We have many other conversion patterns in the project, but these look particularly mechanical.

@ScottTodd
ScottTodd merged commit 9b3e709 into iree-org:main May 18, 2021
@hanhanW hanhanW mentioned this pull request May 18, 2021
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.

3 participants