Skip to content

Commit 26ea09b

Browse files
jumerckxPangoraw
andauthored
Rename MModule to Module and add constructor from module operation (#50)
Co-authored-by: Paul Berg <[email protected]>
1 parent 9938596 commit 26ea09b

File tree

4 files changed

+32
-15
lines changed

4 files changed

+32
-15
lines changed

examples/brutus.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -276,7 +276,7 @@ using MLIR.IR, MLIR
276276
fptr = IR.context!(IR.Context()) do
277277
op = Brutus.code_mlir(pow, Tuple{Int,Int})
278278

279-
mod = MModule(Location())
279+
mod = IR.Module(Location())
280280
body = IR.get_body(mod)
281281
push!(body, op)
282282

src/IR/IR.jl

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ export
33
OperationState,
44
Location,
55
Context,
6-
MModule,
76
Value,
87
MLIRType,
98
Region,
@@ -749,33 +748,34 @@ Base.unsafe_convert(::Type{MlirRegion}, region::Region) = region.region
749748

750749
### Module
751750

752-
mutable struct MModule
751+
mutable struct Module
753752
module_::MlirModule
754753

755-
MModule(module_) = begin
756-
@assert !mlirIsNull(module_) "cannot create MModule with null MlirModule"
754+
Module(module_) = begin
755+
@assert !mlirIsNull(module_) "cannot create Module with null MlirModule"
757756
finalizer(API.mlirModuleDestroy, new(module_))
758757
end
759758
end
760759

761-
MModule(loc::Location=Location()) =
762-
MModule(API.mlirModuleCreateEmpty(loc))
760+
Module(loc::Location=Location()) =
761+
Module(API.mlirModuleCreateEmpty(loc))
762+
Module(op::Operation) = Module(API.mlirModuleFromOperation(lose_ownership!(op)))
763763
get_operation(module_) = Operation(API.mlirModuleGetOperation(module_), false)
764764
get_body(module_) = Block(API.mlirModuleGetBody(module_), false)
765-
get_first_child_op(mod::MModule) = get_first_child_op(get_operation(mod))
765+
get_first_child_op(mod::Module) = get_first_child_op(get_operation(mod))
766766

767-
Base.convert(::Type{MlirModule}, module_::MModule) = module_.module_
768-
Base.parse(::Type{MModule}, module_) = MModule(API.mlirModuleCreateParse(context(), module_), context())
767+
Base.convert(::Type{MlirModule}, module_::Module) = module_.module_
768+
Base.parse(::Type{Module}, module_) = Module(API.mlirModuleCreateParse(context(), module_), context())
769769

770770
macro mlir_str(code)
771771
quote
772772
ctx = Context()
773-
parse(MModule, code)
773+
parse(Module, code)
774774
end
775775
end
776776

777-
function Base.show(io::IO, module_::MModule)
778-
println(io, "MModule:")
777+
function Base.show(io::IO, module_::Module)
778+
println(io, "Module:")
779779
show(io, get_operation(module_))
780780
end
781781

src/IR/Support.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,5 +129,5 @@ function verifyall(operation::Operation; debug=false)
129129
end
130130
end
131131
end
132-
verifyall(module_::MModule) = get_operation(module_) |> verifyall
132+
verifyall(module_::IR.Module) = get_operation(module_) |> verifyall
133133

test/ir.jl

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
using MLIR.Dialects: arith
1+
using MLIR.Dialects: arith, builtin
22
using MLIR.IR, LLVM
33

44
@testset "operation introspection" begin
@@ -11,3 +11,20 @@ using MLIR.IR, LLVM
1111
@test IR.get_attribute_by_name(op, "value") |> IR.bool_value
1212
end
1313
end
14+
15+
@testset "Module construction from operation" begin
16+
IR.context!(IR.Context()) do
17+
if LLVM.version() >= v"15"
18+
op = builtin.module_(bodyRegion=IR.Region())
19+
else
20+
op = builtin.module_(body=IR.Region())
21+
end
22+
mod = IR.Module(op)
23+
op = IR.get_operation(mod)
24+
25+
@test IR.name(op) == "builtin.module"
26+
27+
# Only a `module` operation can be used to create a module.
28+
@test_throws AssertionError IR.Module(arith.constant(; value=true, result=MLIRType(Bool)))
29+
end
30+
end

0 commit comments

Comments
 (0)