-
Notifications
You must be signed in to change notification settings - Fork 9
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
IR Module #11
Conversation
src/Dialects.jl
Outdated
|
||
for fname in (:xori, :andi, :ori) | ||
@eval function $fname(context, operands, type=IR.get_type(first(operands)); loc=Location(context)) | ||
state = OperationState($(string("arith.", fname)), loc) |
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.
Could we use TableGen
to generate these?
Maybe we can break |
src/IR.jl
Outdated
Location(context, string(lin.file), lin.line) | ||
Location(context::Context, ::Nothing) = Location(context) | ||
|
||
Base.convert(::Type{MlirLocation}, location::Location) = location.location |
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.
Shouldn't all these conversions be unsafe_convert
methods?
src/IR.jl
Outdated
Location(context::Context, lin::Core.LineInfoNode) = | ||
Location(context, string(lin.file), lin.line) | ||
Location(context::Context, lin::LineNumberNode) = | ||
isnothing(lin.file) ? | ||
Location(context) : | ||
Location(context, string(lin.file), lin.line) |
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.
Location(context::Context, lin::Core.LineInfoNode)
and
Location(context::Context, lin::LineNumberNode)
Those seem like something we should leave up to the user, or defined in an Interop module
src/IR.jl
Outdated
operation | ||
end | ||
|
||
get_location(operation) = Location(API.mlirOperationGetLocation(operation)) |
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.
location
, name
/nameof
seem more idiomatic instead of get_
src/IR.jl
Outdated
function lose_ownership!(operation::Operation) | ||
@assert operation.owned | ||
@atomic operation.owned = false | ||
operation | ||
end |
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.
Can you explain the idea behind these ownership semantics a bit more?
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.
Several wrapper structs are made mutable (Region
, Block
, Operation
) so that an associated finalizer can destroy their associated Mlir{Region,Block,Operation}
type (ex using mlirOperationDestroy
). But when the block/region/operation is added to a container (Region to an Operation, Block to a Region, or Operation to a Block), the ownership is transfered to this container and the owned
field is set to false
on the wrapper to prevent running the destroy operation in the finalizer since the container is now in charge of destroy its contained elements.
julia> block = Block();
julia> block = nothing # block finalizer will destroy the MlirBlock
julia> region = Region(); block = Block();
julia> push!(region, block); # block.owned is no longer true since region is now responsible for destroying the MlirBlock
julia> region = nothing # it is unsafe to use the block after this.
Note that this does not model the fact that the Context
struct must outlive all structs created from it. I currently added a context
field only to the MModule
(maybe to rename MLIRModule
) so that one can create a module with a context a keep a reference only to the module. This would not be valid for a Location
for example.
julia> context = Context();
julia> location = Location(context);
julia> context = nothing; # location should not be used after this
Codecov ReportPatch coverage:
❗ Your organization is not using the GitHub App Integration. As a result you may experience degraded service beginning May 15th. Please install the Github App Integration for your organization. Read more. Additional details and impacted files@@ Coverage Diff @@
## main #11 +/- ##
===========================================
- Coverage 83.33% 41.13% -42.20%
===========================================
Files 1 5 +4
Lines 6 581 +575
===========================================
+ Hits 5 239 +234
- Misses 1 342 +341
☔ View full report in Codecov by Sentry. |
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've written a patch to handle context the same way LLVM.jl does (I didn't touch the ownership stuff though). If desirable, I can make a PR to this branch, or include it in a new pr?
Co-authored-by: jumerckx <31353884+jumerckx@users.noreply.github.com>
Co-authored-by: jumerckx <31353884+jumerckx@users.noreply.github.com>
Thanks for the review @jumerckx!
That means something like passing a function to the Context() do ctx
# use ctx
end Can you open a PR targeting this branch? |
Yes, and functions internally call
Opened one here: #17 , but still a bit of work left to finish it completely (see notes in pr). Depending on if there's additional comments, I'll try to finish it up soon :) |
I will land this PR and we can continue working on this on main? |
This PR adds a module to make working with MLIR more convenient. As an example, I added a simple conversion pipeline from Julia SSAIR to MLIR (which we might not want to keep as a test) for very simple scalar code using a subset of julia intrinsics. But is seems to produce valid code which can be tested using the execution engine!
This is a draft because it may be better to make the API look a bit more like LLVM.jl, notably regarding ownership which is currently modeled as an
owned::Bool
field on the relevant structs (Region
,Block
,...).The code for the IR module mostly comes from Coil.jl.