|
66 | 66 |
|
67 | 67 | const DUMP_MLIR_DIR = Ref{Union{Nothing,String}}(nothing)
|
68 | 68 |
|
69 |
| -""" |
70 |
| - run!(passManager, module) |
71 |
| -
|
72 |
| -Run the provided `passManager` on the given `module`. |
73 |
| -""" |
74 |
| -function run!(pm::PassManager, mod::Module) |
75 |
| - status = LogicalResult(@static if isdefined(API, :mlirPassManagerRunOnOp) |
76 |
| - API.mlirPassManagerRunOnOp(pm, Operation(mod)) |
77 |
| - else |
78 |
| - API.mlirPassManagerRun(pm, mod) |
79 |
| - end) |
80 |
| - if isfailure(status) |
| 69 | +# Utilities for dumping to a file the module of a failed compilation, useful for |
| 70 | +# debugging purposes. |
| 71 | +function compilation_failed_dump_mlir(mod::Module, pm::Union{Nothing,PassManager}=nothing) |
| 72 | + try |
81 | 73 | # If `DUMP_MLIR_DIR` is `nothing`, create a persistent new temp
|
82 | 74 | # directory, otherwise use the provided path.
|
83 | 75 | dir = if isnothing(DUMP_MLIR_DIR[])
|
| 76 | + mkpath(tempdir()) |
84 | 77 | mktempdir(; prefix="reactant_", cleanup=false)
|
85 | 78 | else
|
86 | 79 | DUMP_MLIR_DIR[]
|
87 | 80 | 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 |
| 81 | + # Make sure the directory exists |
| 82 | + mkpath(dir) |
| 83 | + path = tempname(dir; cleanup=false) * ".mlir" |
| 84 | + open(path, "w") do io |
| 85 | + if !isnothing(pm) |
93 | 86 | println(io, "// Pass pipeline:")
|
94 | 87 | print(io, "// ")
|
95 | 88 | print_pass_pipeline(io, OpPassManager(pm))
|
96 | 89 | println(io)
|
97 |
| - show(IOContext(io, :debug => true), mod) |
98 | 90 | end
|
99 |
| - @error "Dumped module to " * path |
100 |
| - catch err |
101 |
| - @error "Couldn't save MLIR module" exception = err |
| 91 | + show(IOContext(io, :debug => true), mod) |
102 | 92 | end
|
| 93 | + @error "Compilation failed, MLIR module written to $(path)" |
| 94 | + catch err |
| 95 | + @error "Couldn't save MLIR module" exception = err |
| 96 | + end |
| 97 | +end |
| 98 | + |
| 99 | +function try_compile_dump_mlir(f, mod::Module, pm=nothing) |
| 100 | + try |
| 101 | + f() |
| 102 | + catch |
| 103 | + compilation_failed_dump_mlir(mod, pm) |
| 104 | + rethrow() |
| 105 | + end |
| 106 | +end |
| 107 | + |
| 108 | +""" |
| 109 | + run!(passManager, module) |
| 110 | +
|
| 111 | +Run the provided `passManager` on the given `module`. |
| 112 | +""" |
| 113 | +function run!(pm::PassManager, mod::Module) |
| 114 | + status = LogicalResult(@static if isdefined(API, :mlirPassManagerRunOnOp) |
| 115 | + API.mlirPassManagerRunOnOp(pm, Operation(mod)) |
| 116 | + else |
| 117 | + API.mlirPassManagerRun(pm, mod) |
| 118 | + end) |
| 119 | + if isfailure(status) |
| 120 | + compilation_failed_dump_mlir(mod, pm) |
103 | 121 | throw("failed to run pass manager on module")
|
104 | 122 | end
|
105 | 123 | return mod
|
|
0 commit comments