-
Notifications
You must be signed in to change notification settings - Fork 258
[spirv] Add binary arithmetic operations. #54
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is nice, thanks @denis0x0D! I just have two small comments. :)
@@ -252,7 +255,100 @@ def SPV_ExecutionModeOp : SPV_Op<"ExecutionMode", [ModuleOnly]> { | |||
|
|||
// ----- | |||
|
|||
def SPV_FMulOp : SPV_Op<"FMul", [NoSideEffect, SameOperandsAndResultType]> { | |||
def SPV_FAddOp : SPV_ArithmeticOp<"FAdd", SPV_Float> { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This op should also be marked as Commutative
. We should add another template parameter to SPV_ArithmeticOp
to allow additional traits on certain ops. (You can use !listconcat(traits, [NoSideEffect, SameOperandsAndResultType])
to concatenate them.)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for mention this! Fixed.
|
||
// ----- | ||
|
||
def SPV_FMulOp : SPV_ArithmeticOp<"FMul", SPV_Float> { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same here. Also need to mark as Commutative
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks, fixed.
|
||
// ----- | ||
|
||
def SPV_IAddOp : SPV_ArithmeticOp<"IAdd", SPV_Integer> { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed.
|
||
// ----- | ||
|
||
def SPV_IMulOp : SPV_ArithmeticOp<"IMul", SPV_Integer> { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed.
@@ -0,0 +1,61 @@ | |||
// RUN: mlir-translate -serialize-spirv %s | mlir-translate -deserialize-spirv | FileCheck %s | |||
// CHECK: func {{@.*}}([[ARG1:%.*]]: f32, [[ARG2:%.*]]: f32) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No need to check the func
s; just need to check the arithmetic ops. Checks in the test should focus on what we are testing. Checking additional stuff will make the test difficult to maintain in the future (say if we change how func
are printed then we need to update everywhere, even in this test case, which is not related to testing func
anyway.)
Besides, we typically add the // CHECK
to be directly above the line to be checked for easy contrast. :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the explanation, fixed.
Integer signedness is a common confusing point in SPIR-V, which even led a dedicated section in the spec to explain: https://www.khronos.org/registry/spir-v/specs/unified1/SPIRV.html#_unsigned_versus_signed_integers. Quote the spec:
So |
28e7040
to
3f3ad82
Compare
@antiagainst thanks a lot for review, updated regarding to your comments. Can you please take a look.
Thanks for the explanation, I was thinking OpU*, OpS* requires additional checks, because of this:
Thanks. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice, thanks!
func @spirv_bin_ops() -> () { | ||
spv.module "Logical" "VulkanKHR" { | ||
func @fmul(%arg0 : f32, %arg1 : f32) { | ||
// CHECK: [[VALUE:%.*]] = spv.FMul [[ARG1:%.*]], [[ARG2:%.*]] : f32 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nit: This doesnt need to be [[..:%.*]]
. Just use {{%.*}}
since these values arent matched anywhere else. Same for everything below as well
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks, fixed!
@@ -66,8 +66,11 @@ def SPV_AccessChainOp : SPV_Op<"AccessChain", [NoSideEffect]> { | |||
no remaining (unused) indexes. | |||
|
|||
Each index in Indexes | |||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nit : Why the extra line here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's a autofix by define_inst.sh script, which fixes my previous mistake, I've removed those lines by hands.
Maybe a simple test in |
In the SPIR-V dialect, ideally I'd prefer to go with the first way of thinking from the spec, i.e., only have |
c4b9cf6
to
ff3689c
Compare
Add binary operations such as: OpIAdd, OpFAdd, OpISub, OpFSub, OpIMul, OpFDiv, OpFRem, OpFMod.
@MaheshRavishankar thanks for review, updated and added some tests to ops.mlir. |
After changing serialization and deserialization to only interact with |
@antiagainst thanks for the detailed explanations, seems clear to me. |
Add binary operations such as: OpIAdd, OpFAdd, OpISub, OpFSub, OpIMul, OpFDiv, OpFRem, OpFMod. Closes #54 COPYBARA_INTEGRATE_REVIEW=tensorflow/mlir#54 from denis0x0D:sandbox/bin_ops ff3689c441a6a181dbe52c410562346993163013 PiperOrigin-RevId: 260734166
Hello, this patch implements subset of arithmetic binary operations such as: OpIAdd, OpFAdd, OpISub, OpFSub, OpIMul, OpFDiv, OpFRem, OpFMod. The ops reuse same parser, printer and verifier, so I've created a base class for them - ArithmeticOp. Tests added to check serialization and deserialization. I did not add tests into ops.mlir file, because all ops are reusing the same implementation and FMullOp already has tests.
As far as I understood, other binary arithmetic operation such as OpUMod, OpUDiv, OpSDiv, OpSRem, OpSMod require to check signess of the spirv integer type, in this case IntegerTypeOp should be fully implemented, and this affects a serialization and deserialization. If it's ok, I can implement them in next patch.
@antiagainst can you please take a look.
Thanks.