Skip to content

Commit 27d1318

Browse files
authored
feat: give nice names to dumped mlir files (#1114)
* feat: give nice names to dumped mlir files * fix: make it Atomic
1 parent e734a5f commit 27d1318

File tree

3 files changed

+34
-9
lines changed

3 files changed

+34
-9
lines changed

src/mlir/IR/IR.jl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ export nattrs,
2424
export BlockIterator, RegionIterator, OperationIterator
2525
export @affinemap
2626

27+
using Random: randstring
28+
2729
function mlirIsNull(val)
2830
return val.ptr == C_NULL
2931
end

src/mlir/IR/IntegerSet.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ context(set::IntegerSet) = Context(API.mlirIntegerSetGetContext(set.set))
6969
7070
Checks whether the given set is a canonical empty set, e.g., the set returned by [`mlirIntegerSetEmptyGet`](@ref).
7171
"""
72-
isempty(set::IntegerSet) = API.mlirIntegerSetIsCanonicalEmpty(set)
72+
Base.isempty(set::IntegerSet) = API.mlirIntegerSetIsCanonicalEmpty(set)
7373

7474
"""
7575
ndims(set)

src/mlir/IR/Pass.jl

Lines changed: 31 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -68,22 +68,41 @@ end
6868
const DUMP_MLIR_DIR = Ref{Union{Nothing,String}}(nothing)
6969
# Whether to always dump MLIR, regardless of failure
7070
const DUMP_MLIR_ALWAYS = Ref{Bool}(false)
71+
# Counter for dumping MLIR modules
72+
const MLIR_DUMP_COUNTER = Threads.Atomic{Int}(0)
7173

7274
# Utilities for dumping to a file the module of a failed compilation, useful for
7375
# debugging purposes.
74-
function dump_mlir(mod::Module, pm::Union{Nothing,PassManager}=nothing; failed::Bool=false)
76+
function dump_mlir(
77+
mod::Module, pm::Union{Nothing,PassManager}=nothing, mode::String=""; failed::Bool=false
78+
)
7579
try
7680
# If `DUMP_MLIR_DIR` is `nothing`, create a persistent new temp
7781
# directory, otherwise use the provided path.
7882
dir = if isnothing(DUMP_MLIR_DIR[])
7983
mkpath(tempdir())
80-
mktempdir(; prefix="reactant_", cleanup=false)
84+
# Use the same directory for this session
85+
DUMP_MLIR_DIR[] = mktempdir(; prefix="reactant_", cleanup=false)
8186
else
8287
DUMP_MLIR_DIR[]
8388
end
89+
8490
# Make sure the directory exists
8591
mkpath(dir)
86-
path = tempname(dir; cleanup=false) * ".mlir"
92+
93+
# Attempt to get the name of the module if that exists
94+
module_op = Operation(mod)
95+
mod_name = attr(module_op, String(API.mlirSymbolTableGetSymbolAttributeName()))
96+
fname = mod_name === nothing ? randstring(4) : String(mod_name)
97+
fname = "module_" * lpad(MLIR_DUMP_COUNTER[], 3, "0") * "_$(fname)"
98+
if isempty(mode)
99+
fname *= ".mlir"
100+
else
101+
fname *= "_$(mode).mlir"
102+
end
103+
MLIR_DUMP_COUNTER[] += 1
104+
path = joinpath(dir, fname)
105+
87106
open(path, "w") do io
88107
if !isnothing(pm)
89108
println(io, "// Pass pipeline:")
@@ -93,7 +112,11 @@ function dump_mlir(mod::Module, pm::Union{Nothing,PassManager}=nothing; failed::
93112
end
94113
show(IOContext(io, :debug => true), mod)
95114
end
96-
failed && @error "Compilation failed, MLIR module written to $(path)"
115+
if failed
116+
@error "Compilation failed, MLIR module written to $(path)"
117+
else
118+
@debug "MLIR module written to $(path)"
119+
end
97120
catch err
98121
@error "Couldn't save MLIR module" exception = err
99122
end
@@ -103,15 +126,15 @@ function try_compile_dump_mlir(f, mod::Module, pm=nothing)
103126
failed = false
104127
# Dump MLIR before calling `f`. We set `pm` to nothing because the pass
105128
# manager isn't called yet here.
106-
DUMP_MLIR_ALWAYS[] && dump_mlir(mod, nothing)
129+
DUMP_MLIR_ALWAYS[] && dump_mlir(mod, nothing, "pre_xla_compile")
107130
try
108131
f()
109132
catch
110133
failed = true
111134
rethrow()
112135
finally
113136
if failed || DUMP_MLIR_ALWAYS[]
114-
dump_mlir(mod, pm; failed)
137+
dump_mlir(mod, pm, "post_xla_compile"; failed)
115138
end
116139
end
117140
end
@@ -124,15 +147,15 @@ Run the provided `passManager` on the given `module`.
124147
function run!(pm::PassManager, mod::Module)
125148
# Dump MLIR before running the pass manager. We set `pm` to nothing because
126149
# the pass manager isn't called yet here.
127-
DUMP_MLIR_ALWAYS[] && dump_mlir(mod, nothing)
150+
DUMP_MLIR_ALWAYS[] && dump_mlir(mod, nothing, "pre_pm")
128151
status = LogicalResult(@static if isdefined(API, :mlirPassManagerRunOnOp)
129152
API.mlirPassManagerRunOnOp(pm, Operation(mod))
130153
else
131154
API.mlirPassManagerRun(pm, mod)
132155
end)
133156
failed = isfailure(status)
134157
if failed || DUMP_MLIR_ALWAYS[]
135-
dump_mlir(mod, pm; failed)
158+
dump_mlir(mod, pm, "post_pm"; failed)
136159
end
137160
if failed
138161
throw("failed to run pass manager on module")

0 commit comments

Comments
 (0)