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

IR Module #11

Merged
merged 9 commits into from
Sep 29, 2023
Merged

IR Module #11

merged 9 commits into from
Sep 29, 2023

Conversation

Pangoraw
Copy link
Collaborator

@Pangoraw Pangoraw commented May 16, 2023

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.

lib/14/libMLIR_h.jl Outdated Show resolved Hide resolved
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)
Copy link
Collaborator

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?

src/IR.jl Outdated Show resolved Hide resolved
src/IR.jl Outdated Show resolved Hide resolved
@vchuravy
Copy link
Collaborator

@Pangoraw thanks! That looks like a great starting point.

@femtomc do you have some time to take a look if this jams with your understanding of things?

@vchuravy
Copy link
Collaborator

Maybe we can break src/IR.jl into multiple files? An mirror the include/mlir-c?

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
Copy link
Collaborator

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 Show resolved Hide resolved
src/IR.jl Outdated Show resolved Hide resolved
src/IR.jl Outdated
Comment on lines 155 to 160
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)
Copy link
Collaborator

@vchuravy vchuravy May 17, 2023

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))
Copy link
Collaborator

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
Comment on lines 624 to 628
function lose_ownership!(operation::Operation)
@assert operation.owned
@atomic operation.owned = false
operation
end
Copy link
Collaborator

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?

Copy link
Collaborator Author

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-commenter
Copy link

codecov-commenter commented May 19, 2023

Codecov Report

Patch coverage: 40.59% and project coverage change: -42.20% ⚠️

Comparison is base (75776fa) 83.33% compared to head (812f803) 41.13%.

❗ 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     
Files Changed Coverage Δ
src/MLIR.jl 85.71% <ø> (+2.38%) ⬆️
src/IR/Pass.jl 26.25% <26.25%> (ø)
src/IR/IR.jl 34.64% <34.64%> (ø)
src/Dialects.jl 76.92% <76.92%> (ø)
src/IR/Support.jl 83.60% <83.60%> (ø)

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

Copy link
Collaborator

@jumerckx jumerckx left a 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?

src/IR.jl Outdated Show resolved Hide resolved
src/IR.jl Outdated Show resolved Hide resolved
Pangoraw and others added 3 commits August 13, 2023 11:55
Co-authored-by: jumerckx <31353884+jumerckx@users.noreply.github.com>
Co-authored-by: jumerckx <31353884+jumerckx@users.noreply.github.com>
@Pangoraw
Copy link
Collaborator Author

Thanks for the review @jumerckx!

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?

That means something like passing a function to the Context constructor like so (I am unfortunately not really familiar with LLVM.jl) ?

Context() do ctx
  # use ctx
end

Can you open a PR targeting this branch?

@jumerckx
Copy link
Collaborator

That means something like passing a function to the Context constructor like so (I am unfortunately not really familiar with LLVM.jl) ?

Context() do ctx
  # use ctx
end

Yes, and functions internally call context() to get the most recent context on a stack. (See here in LLVM.jl)

Can you open a PR targeting this branch?

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 :)

@vchuravy
Copy link
Collaborator

I will land this PR and we can continue working on this on main?

@vchuravy vchuravy marked this pull request as ready for review September 29, 2023 13:38
@vchuravy vchuravy merged commit 0a447e6 into main Sep 29, 2023
@vchuravy vchuravy deleted the pb/ir_api branch September 29, 2023 13:39
@vchuravy vchuravy restored the pb/ir_api branch September 29, 2023 13:40
@Pangoraw Pangoraw deleted the pb/ir_api branch January 12, 2024 13:08
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.

5 participants