-
Notifications
You must be signed in to change notification settings - Fork 40
Remove verify
from OperationBuilder
and SameOperandsAndResultType
verification implementation
#516
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
base: next
Are you sure you want to change the base?
Remove verify
from OperationBuilder
and SameOperandsAndResultType
verification implementation
#516
Conversation
Signed-off-by: Tomas Fabrizio Orsi <[email protected]>
Signed-off-by: Tomas Fabrizio Orsi <[email protected]>
…alect Dialect Signed-off-by: Tomas Fabrizio Orsi <[email protected]>
Signed-off-by: Tomas Fabrizio Orsi <[email protected]> Signed-off-by: Tomas Fabrizio Orsi <[email protected]> Signed-off-by: Tomas Fabrizio Orsi <[email protected]>
…explicit dependency Signed-off-by: Tomas Fabrizio Orsi <[email protected]>
Signed-off-by: Tomas Fabrizio Orsi <[email protected]>
Signed-off-by: Tomas Fabrizio Orsi <[email protected]>
…ma separated) Signed-off-by: Tomas Fabrizio Orsi <[email protected]>
Signed-off-by: Tomas Fabrizio Orsi <[email protected]>
Signed-off-by: Tomas Fabrizio Orsi <[email protected]>
Signed-off-by: Tomas Fabrizio Orsi <[email protected]>
…on which matches both patterns Signed-off-by: Tomas Fabrizio Orsi <[email protected]>
Signed-off-by: Tomas Fabrizio Orsi <[email protected]>
Signed-off-by: Tomas Fabrizio Orsi <[email protected]>
Signed-off-by: Tomas Fabrizio Orsi <[email protected]>
Signed-off-by: Tomas Fabrizio Orsi <[email protected]>
Signed-off-by: Tomas Fabrizio Orsi <[email protected]>
Signed-off-by: Tomas Fabrizio Orsi <[email protected]>
…the verifier is enabled Signed-off-by: Tomas Fabrizio Orsi <[email protected]>
Signed-off-by: Tomas Fabrizio Orsi <[email protected]>
Signed-off-by: Tomas Fabrizio Orsi <[email protected]>
Signed-off-by: Tomas Fabrizio Orsi <[email protected]>
…tion. Signed-off-by: Tomas Fabrizio Orsi <[email protected]>
Signed-off-by: Tomas Fabrizio Orsi <[email protected]>
Signed-off-by: Tomas Fabrizio Orsi <[email protected]>
Update: The tests are now failing because I'm working on a fix Edit: This commit upcasts |
…ame type Signed-off-by: Tomas Fabrizio Orsi <[email protected]>
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 work! I've noted a couple of small changes to make, but otherwise it looks good to me!
hir/src/dialects/test/ops/binary.rs
Outdated
traits(BinaryOp, SameTypeOperands, SameOperandsAndResultType), | ||
implements(InferTypeOpInterface) | ||
)] | ||
pub struct InvalidOpsWithReturn { |
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.
You shouldn't need to define a new operation type to test the verifier. I would instead simply construct an Add
operation with mismatched operand types to trigger a verification error.
Since this operation implements InferTypeOpInterface
(and Add
does as well), by definition the result type will be inferred to match the operand type, so we're really just testing that verification fails if the operand types are mismatched, and that's fine for the purpose of the test anyway.
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.
You shouldn't need to define a new operation type to test the verifier. I would instead simply construct an
Add
operation with mismatched operand types to trigger a verification error.
Question, which verifier are you referring to? Wouldn't constructing an Add
operation with mismatched operand types trigger SameTypeOperands
's verification function to error instead of SameOperandsAndResultType
's?
If I'm not mistaken, construction of an Add
op with mismatched operand types is being tested in the derived_op_verifier_test
test, note these lines.
The initial idea behind InvalidOpsWithReturn
was for it to test SameOperandsAndResultType
verification function, hence the new test added here.
Since this operation implements
InferTypeOpInterface
(andAdd
does as well), by definition the result type will be inferred to match the operand type, so we're really just testing that verification fails if the operand types are mismatched, and that's fine for the purpose of the test anyway.
I hardcoded InferTypeOpInterface
implementation for InvalidOpsWithReturn
so it always returned U64
(see here). It was the only way I managed to make InvalidOpsWithReturn
build correctly, but fail during trait validation. Is there a mechanism that I'm missing? As an aside if I remove the InferTypeOpInterface
trait from the operation, then it fails to build the op, stating: invalid operation
Do note that if this test is contrived, then it can be removed all together.
Thanks in advance and apologies if I'm missing something
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.
So in general, SameOperandsAndResultType
, pretty much implies InferTypeOpInterface
(not literally, but if you are defining an op whose result type must be the same as the operands, then implementing InferTypeOpInterface
is trivial, and would guarantee that the result always matches the operand type from which it is inferred.
With that in mind, testing SameOperandsAndResultType
pretty much requires doing so with an op that doesn't implement InferTypeOpInterface
(which would be weird, but is allowed), or overriding the result type after inference. As you found, all of our current test ops that implement SameOperandsAndResultType
also implement InferTypeOpInterface
(and that's the case with all of our other dialects as well), so I think the best way to test the verifier here is to simply override the result type after inference (e.g. with Add
), just to force invalid IR to trip the verifier.
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.
Great! Thanks for the clarification.
With this in mind, what do you think about this refactor: 10728a7
I think I do have to use the set_type
method in this case, right? I tried to do a proper cast via zext
/sext
, but I was not able to use that method. As far as I can tell, that method is implemented for the ArithOpBuilder
trait, which is implemented in the midenc-dialect-arith
crate. So when I try to import it in derive.rs
' Cargo.toml
(hir/Cargo.toml
), I get a cyclic package dependency
error.
Regardless, maybe in this case does merit the use of set_type
use, since it is more of an explicit override rather than a proper cast.
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.
I think I do have to use the set_type method in this case, right?
Yes, correct, we're basically creating invalid IR on purpose, and the only real way to do it is to use low-level APIs like set_type
.
With this in mind, what do you think about this refactor: 10728a7
LGTM!
Signed-off-by: Tomas Fabrizio Orsi <[email protected]> Reported-by: Paul Schoenfelder <[email protected]>
…dResultType do not have operands Signed-off-by: Tomas Fabrizio Orsi <[email protected]>
8b1afd3
to
91ff589
Compare
Signed-off-by: Tomas Fabrizio Orsi <[email protected]>
91ff589
to
72d6df2
Compare
Signed-off-by: Tomas Fabrizio Orsi <[email protected]>
…or size Signed-off-by: Tomas Fabrizio Orsi <[email protected]>
c8afa8c
to
19795d3
Compare
…:add Signed-off-by: Tomas Fabrizio Orsi <[email protected]>
19795d3
to
10728a7
Compare
Closes #456
The issue states that currently
Operation::verify
is called inside theOperationBuilder
which is called before operations are added to blocks, which could lead to failure in operation verification. Because of this,SameTypeOperands
' verification function had been commented out.This PR removes the call to
Operation::verify
from theOperationBuilder
and moves it to the beggining ofPassManager::run
.Operation::verify
was already called inside thePassManager::run
(more precisely, inside theOpToOpPassAdaptor::run_pipeline
). An additionalverify
call was added at the beginning of thePassManager
pipeline to "compensate" its removal in the OperationBuilder and ensureOperation
integrity before applying passes. Now, thePassManger
is responsible forOperation
integrity verification, which is done before passes are applied and also after each pass applies its modifications.With the call to
verify
in theOperationBuilder
now removed,SameTypeOperands
' verification function was re-enabled and so was its test (derived_op_verifier_test
).Additionally, an implementation for
SameOperandsAndResultType
's verification function was added (which was marked here as a TODO item). Following the comment's requirements,SameTypeOperands
was marked as a super-trait.To support this, the
derive!
macro was expanded to support super-traits. The macro supports an arbitrary amount of traits; but it is important to note that the traits must be comma separated instead of the usual "+" separated format. As far as I am aware, it is not possible to use "+" as a separator inside amacro_rules!
context.With the super-trait now added, operations that implemented
SameOperandsAndResultType
but did not implementSameTypeOperands
were updated to now explicitly implement both. This was done explicitly in order to replicate Rust's#[derive]
macro's behavior of explicitly declaring all derived traits.Finally, a unit test was added to test
SameOperandsAndResultType
's verify function. For this, a test operationInvalidOpsWithReturn
was added and registered in the test dialect.Side note: I tried to keep the
SameOperandsAndResultType
's doc comment relatively short in order for it to show up in its entirety when a compilation error shows up, it is currently displayed like so: