Skip to content

Commit

Permalink
Rename MModule to Module and add constructor from module operat…
Browse files Browse the repository at this point in the history
…ion (#50)

Co-authored-by: Paul Berg <[email protected]>
  • Loading branch information
jumerckx and Pangoraw authored Feb 2, 2024
1 parent 9938596 commit 26ea09b
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 15 deletions.
2 changes: 1 addition & 1 deletion examples/brutus.jl
Original file line number Diff line number Diff line change
Expand Up @@ -276,7 +276,7 @@ using MLIR.IR, MLIR
fptr = IR.context!(IR.Context()) do
op = Brutus.code_mlir(pow, Tuple{Int,Int})

mod = MModule(Location())
mod = IR.Module(Location())
body = IR.get_body(mod)
push!(body, op)

Expand Down
24 changes: 12 additions & 12 deletions src/IR/IR.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ export
OperationState,
Location,
Context,
MModule,
Value,
MLIRType,
Region,
Expand Down Expand Up @@ -749,33 +748,34 @@ Base.unsafe_convert(::Type{MlirRegion}, region::Region) = region.region

### Module

mutable struct MModule
mutable struct Module
module_::MlirModule

MModule(module_) = begin
@assert !mlirIsNull(module_) "cannot create MModule with null MlirModule"
Module(module_) = begin
@assert !mlirIsNull(module_) "cannot create Module with null MlirModule"
finalizer(API.mlirModuleDestroy, new(module_))
end
end

MModule(loc::Location=Location()) =
MModule(API.mlirModuleCreateEmpty(loc))
Module(loc::Location=Location()) =
Module(API.mlirModuleCreateEmpty(loc))
Module(op::Operation) = Module(API.mlirModuleFromOperation(lose_ownership!(op)))
get_operation(module_) = Operation(API.mlirModuleGetOperation(module_), false)
get_body(module_) = Block(API.mlirModuleGetBody(module_), false)
get_first_child_op(mod::MModule) = get_first_child_op(get_operation(mod))
get_first_child_op(mod::Module) = get_first_child_op(get_operation(mod))

Base.convert(::Type{MlirModule}, module_::MModule) = module_.module_
Base.parse(::Type{MModule}, module_) = MModule(API.mlirModuleCreateParse(context(), module_), context())
Base.convert(::Type{MlirModule}, module_::Module) = module_.module_
Base.parse(::Type{Module}, module_) = Module(API.mlirModuleCreateParse(context(), module_), context())

macro mlir_str(code)
quote
ctx = Context()
parse(MModule, code)
parse(Module, code)
end
end

function Base.show(io::IO, module_::MModule)
println(io, "MModule:")
function Base.show(io::IO, module_::Module)
println(io, "Module:")
show(io, get_operation(module_))
end

Expand Down
2 changes: 1 addition & 1 deletion src/IR/Support.jl
Original file line number Diff line number Diff line change
Expand Up @@ -129,5 +129,5 @@ function verifyall(operation::Operation; debug=false)
end
end
end
verifyall(module_::MModule) = get_operation(module_) |> verifyall
verifyall(module_::IR.Module) = get_operation(module_) |> verifyall

19 changes: 18 additions & 1 deletion test/ir.jl
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using MLIR.Dialects: arith
using MLIR.Dialects: arith, builtin
using MLIR.IR, LLVM

@testset "operation introspection" begin
Expand All @@ -11,3 +11,20 @@ using MLIR.IR, LLVM
@test IR.get_attribute_by_name(op, "value") |> IR.bool_value
end
end

@testset "Module construction from operation" begin
IR.context!(IR.Context()) do
if LLVM.version() >= v"15"
op = builtin.module_(bodyRegion=IR.Region())
else
op = builtin.module_(body=IR.Region())
end
mod = IR.Module(op)
op = IR.get_operation(mod)

@test IR.name(op) == "builtin.module"

# Only a `module` operation can be used to create a module.
@test_throws AssertionError IR.Module(arith.constant(; value=true, result=MLIRType(Bool)))
end
end

0 comments on commit 26ea09b

Please sign in to comment.