Skip to content

Commit 4c0e77f

Browse files
authored
Don't cleanup MLIR module written to file when pass fails (#1005)
1 parent cd21aa0 commit 4c0e77f

File tree

1 file changed

+23
-9
lines changed

1 file changed

+23
-9
lines changed

src/mlir/IR/Pass.jl

+23-9
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,8 @@ function enable_verifier!(pm, enable=true)
6464
return pm
6565
end
6666

67+
const DUMP_MLIR_DIR = Ref{Union{Nothing,String}}(nothing)
68+
6769
"""
6870
run!(passManager, module)
6971
@@ -76,16 +78,28 @@ function run!(pm::PassManager, mod::Module)
7678
API.mlirPassManagerRun(pm, mod)
7779
end)
7880
if isfailure(status)
79-
dir = mktempdir()
80-
path = joinpath(dir, "module.mlir")
81-
open(path, "w") do io
82-
println(io, "// Pass pipeline:")
83-
print(io, "// ")
84-
print_pass_pipeline(io, OpPassManager(pm))
85-
println(io)
86-
show(IOContext(io, :debug => true), mod)
81+
# If `DUMP_MLIR_DIR` is `nothing`, create a persistent new temp
82+
# directory, otherwise use the provided path.
83+
dir = if isnothing(DUMP_MLIR_DIR[])
84+
mktempdir(; prefix="reactant_", cleanup=false)
85+
else
86+
DUMP_MLIR_DIR[]
87+
end
88+
try
89+
# Make sure the directory exists
90+
mkpath(dir)
91+
path = tempname(dir; cleanup=false) * ".mlir"
92+
open(path, "w") do io
93+
println(io, "// Pass pipeline:")
94+
print(io, "// ")
95+
print_pass_pipeline(io, OpPassManager(pm))
96+
println(io)
97+
show(IOContext(io, :debug => true), mod)
98+
end
99+
@error "Dumped module to " * path
100+
catch err
101+
@error "Couldn't save MLIR module" exception = err
87102
end
88-
@error "Dumped module to " * path
89103
throw("failed to run pass manager on module")
90104
end
91105
return mod

0 commit comments

Comments
 (0)