|
67 | 67 | # The SPIRV Tools don't handle Julia's debug info, rejecting DW_LANG_Julia...
|
68 | 68 | strip_debuginfo!(mod)
|
69 | 69 |
|
70 |
| - # write the bitcode to a temporary file (the SPIRV Translator library doesn't have a C API) |
71 |
| - mktemp() do input, input_io |
72 |
| - write(input_io, mod) |
73 |
| - flush(input_io) |
| 70 | + # translate to SPIR-V |
| 71 | + input = tempname(cleanup=false) * ".bc" |
| 72 | + translated = tempname(cleanup=false) * ".spv" |
| 73 | + write(input, mod) |
| 74 | + SPIRV_LLVM_Translator_jll.llvm_spirv() do translator |
| 75 | + proc = run(ignorestatus(`$translator --spirv-debug-info-version=ocl-100 -o $translated $input`)) |
| 76 | + if !success(proc) |
| 77 | + error("""Failed to translate LLVM code to SPIR-V. |
| 78 | + If you think this is a bug, please file an issue and attach $(input).""") |
| 79 | + end |
| 80 | + end |
74 | 81 |
|
75 |
| - # compile to SPIR-V |
76 |
| - mktemp() do translated, translated_io |
77 |
| - SPIRV_LLVM_Translator_jll.llvm_spirv() do translator |
78 |
| - run(`$translator --spirv-debug-info-version=ocl-100 -o $translated $input`) |
| 82 | + # validate |
| 83 | + # XXX: parameterize this on the `validate` driver argument |
| 84 | + # XXX: our code currently doesn't pass the validator |
| 85 | + if Base.JLOptions().debug_level >= 2 && false |
| 86 | + SPIRV_Tools_jll.spirv_val() do validator |
| 87 | + proc = run(ignorestatus(`$validator $translated`)) |
| 88 | + if !success(proc) |
| 89 | + error("""Failed to validate generated SPIR-V. |
| 90 | + If you think this is a bug, please file an issue and attach $(input) and $(translated).""") |
79 | 91 | end
|
| 92 | + end |
| 93 | + end |
80 | 94 |
|
81 |
| - # validate |
82 |
| - # XXX: parameterize this on the `validate` driver argument |
83 |
| - # XXX: our code currently doesn't pass the validator |
84 |
| - if Base.JLOptions().debug_level >= 2 && false |
85 |
| - SPIRV_Tools_jll.spirv_val() do validator |
86 |
| - run(`$validator $translated`) |
87 |
| - end |
| 95 | + # optimize |
| 96 | + # XXX: parameterize this on the `optimize` driver argument |
| 97 | + # XXX: the optimizer segfaults on some of our code |
| 98 | + optimized = tempname(cleanup=false) * ".spv" |
| 99 | + if false |
| 100 | + SPIRV_Tools_jll.spirv_opt() do optimizer |
| 101 | + proc = run(ignorestatus(`$optimizer -O --skip-validation $translated -o $optimized`)) |
| 102 | + if !success(proc) |
| 103 | + error("""Failed to optimize generated SPIR-V. |
| 104 | + If you think this is a bug, please file an issue and attach $(input) and $(translated).""") |
88 | 105 | end
|
| 106 | + end |
| 107 | + end |
89 | 108 |
|
90 |
| - # optimize |
91 |
| - # XXX: parameterize this on the `optimize` driver argument |
92 |
| - mktemp() do optimized, optimized_io |
93 |
| - SPIRV_Tools_jll.spirv_opt() do optimizer |
94 |
| - run(`$optimizer -O --skip-validation $translated -o $optimized`) |
95 |
| - end |
96 |
| - |
97 |
| - if format == LLVM.API.LLVMObjectFile |
98 |
| - read(optimized) |
99 |
| - else |
100 |
| - # disassemble |
101 |
| - SPIRV_Tools_jll.spirv_dis() do disassembler |
102 |
| - read(`$disassembler $optimized`, String) |
103 |
| - end |
104 |
| - end |
105 |
| - end |
| 109 | + output = if format == LLVM.API.LLVMObjectFile |
| 110 | + read(translated) |
| 111 | + else |
| 112 | + # disassemble |
| 113 | + SPIRV_Tools_jll.spirv_dis() do disassembler |
| 114 | + read(`$disassembler $optimized`, String) |
106 | 115 | end
|
107 | 116 | end
|
| 117 | + |
| 118 | + rm(input) |
| 119 | + rm(translated) |
| 120 | + #rm(optimized) |
| 121 | + |
| 122 | + return output |
108 | 123 | end
|
109 | 124 |
|
110 | 125 | # reimplementation that uses `spirv-dis`, giving much more pleasant output
|
|
0 commit comments