|
35 | 35 | ret i64 %value""" |
36 | 36 | return :(Base.llvmcall($llvmcall_str, T, Tuple{T}, i)) |
37 | 37 | end |
| 38 | + |
| 39 | +# filecheck utils |
| 40 | + |
| 41 | +module FileCheck |
| 42 | + import LLVM_jll |
| 43 | + import IOCapture |
| 44 | + using GPUCompiler, LLVM |
| 45 | + using Test |
| 46 | + |
| 47 | + export filecheck, @filecheck, @check_str |
| 48 | + |
| 49 | + global filecheck_path::String |
| 50 | + function __init__() |
| 51 | + # TODO: Windows |
| 52 | + global filecheck_path = joinpath(LLVM_jll.artifact_dir, "tools", "FileCheck") |
| 53 | + end |
| 54 | + |
| 55 | + function filecheck_exe(; adjust_PATH::Bool=true, adjust_LIBPATH::Bool=true) |
| 56 | + env = Base.invokelatest( |
| 57 | + LLVM_jll.JLLWrappers.adjust_ENV!, |
| 58 | + copy(ENV), |
| 59 | + LLVM_jll.PATH[], |
| 60 | + LLVM_jll.LIBPATH[], |
| 61 | + adjust_PATH, |
| 62 | + adjust_LIBPATH |
| 63 | + ) |
| 64 | + |
| 65 | + return Cmd(Cmd([filecheck_path]); env) |
| 66 | + end |
| 67 | + |
| 68 | + const julia_typed_pointers = JuliaContext() do ctx |
| 69 | + supports_typed_pointers(ctx) |
| 70 | + end |
| 71 | + |
| 72 | + function filecheck(f, input) |
| 73 | + # FileCheck assumes that the input is available as a file |
| 74 | + mktemp() do path, input_io |
| 75 | + write(input_io, input) |
| 76 | + close(input_io) |
| 77 | + |
| 78 | + # capture the output of `f` and write it into a temporary buffer |
| 79 | + result = IOCapture.capture(rethrow=Union{}) do |
| 80 | + f(input) |
| 81 | + end |
| 82 | + output_io = IOBuffer() |
| 83 | + write(output_io, result.output) |
| 84 | + println(output_io) |
| 85 | + |
| 86 | + if result.error |
| 87 | + # if the function errored, also render the exception and backtrace |
| 88 | + showerror(output_io, result.value, result.backtrace) |
| 89 | + elseif result.value !== nothing |
| 90 | + # also show the returned value; some APIs don't print |
| 91 | + write(output_io, string(result.value)) |
| 92 | + end |
| 93 | + |
| 94 | + # determine some useful prefixes for FileCheck |
| 95 | + prefixes = ["CHECK", |
| 96 | + "JULIA$(VERSION.major)_$(VERSION.minor)", |
| 97 | + "LLVM$(Base.libllvm_version.major)"] |
| 98 | + ## whether we use typed pointers or opaque pointers |
| 99 | + if julia_typed_pointers |
| 100 | + push!(prefixes, "TYPED") |
| 101 | + else |
| 102 | + push!(prefixes, "OPAQUE") |
| 103 | + end |
| 104 | + ## whether we pass pointers as integers or as actual pointers |
| 105 | + if VERSION >= v"1.12.0-DEV.225" |
| 106 | + push!(prefixes, "PTR_ABI") |
| 107 | + else |
| 108 | + push!(prefixes, "INTPTR_ABI") |
| 109 | + end |
| 110 | + |
| 111 | + # now pass the collected output to FileCheck |
| 112 | + seekstart(output_io) |
| 113 | + filecheck_io = Pipe() |
| 114 | + cmd = ```$(filecheck_exe()) |
| 115 | + --color |
| 116 | + --allow-unused-prefixes |
| 117 | + --check-prefixes $(join(prefixes, ',')) |
| 118 | + $path``` |
| 119 | + proc = run(pipeline(ignorestatus(cmd); stdin=output_io, stdout=filecheck_io, stderr=filecheck_io); wait=false) |
| 120 | + close(filecheck_io.in) |
| 121 | + |
| 122 | + # collect the output of FileCheck |
| 123 | + reader = Threads.@spawn String(read(filecheck_io)) |
| 124 | + Base.wait(proc) |
| 125 | + log = strip(fetch(reader)) |
| 126 | + |
| 127 | + # error out if FileCheck did not succeed. |
| 128 | + # otherwise, return true so that `@test @filecheck` works as expected. |
| 129 | + if !success(proc) |
| 130 | + error(log) |
| 131 | + end |
| 132 | + return true |
| 133 | + end |
| 134 | + end |
| 135 | + |
| 136 | + # collect checks used in the @filecheck block by piggybacking on macro expansion |
| 137 | + const checks = String[] |
| 138 | + macro check_str(str) |
| 139 | + push!(checks, str) |
| 140 | + nothing |
| 141 | + end |
| 142 | + |
| 143 | + macro filecheck(ex) |
| 144 | + ex = Base.macroexpand(__module__, ex) |
| 145 | + if isempty(checks) |
| 146 | + error("No checks provided within the @filecheck macro block") |
| 147 | + end |
| 148 | + check_str = join(checks, "\n") |
| 149 | + empty!(checks) |
| 150 | + |
| 151 | + esc(quote |
| 152 | + filecheck($check_str) do _ |
| 153 | + $ex |
| 154 | + end |
| 155 | + end) |
| 156 | + end |
| 157 | +end |
0 commit comments