Skip to content

Commit d65a0b6

Browse files
committed
Add support for custom IR validation passes.
1 parent 73f9deb commit d65a0b6

File tree

3 files changed

+21
-0
lines changed

3 files changed

+21
-0
lines changed

src/interface.jl

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -243,6 +243,9 @@ end
243243
# post-Julia optimization processing of the module
244244
optimize_module!(@nospecialize(job::CompilerJob), mod::LLVM.Module) = return
245245

246+
# whether an LLVM function is valid for this back-end
247+
validate_module(@nospecialize(job::CompilerJob), mod::LLVM.Module) = IRError[]
248+
246249
# finalization of the module, before deferred codegen and optimization
247250
function finish_module!(@nospecialize(job::CompilerJob), mod::LLVM.Module, entry::LLVM.Function)
248251
return entry

src/metal.jl

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,21 @@ function process_entry!(job::CompilerJob{MetalCompilerTarget}, mod::LLVM.Module,
6363
return entry
6464
end
6565

66+
function validate_module(job::CompilerJob{MetalCompilerTarget}, mod::LLVM.Module)
67+
errors = IRError[]
68+
69+
T_double = LLVM.DoubleType(context(mod))
70+
71+
for fun in functions(mod), bb in blocks(fun), inst in instructions(bb)
72+
if llvmtype(inst) == T_double || any(param->llvmtype(param) == T_double, operands(inst))
73+
bt = backtrace(inst)
74+
push!(errors, ("use of double floating-point value", bt, inst))
75+
end
76+
end
77+
78+
return errors
79+
end
80+
6681
# TODO: why is this done in finish_module? maybe just in process_entry?
6782
function finish_module!(@nospecialize(job::CompilerJob{MetalCompilerTarget}), mod::LLVM.Module, entry::LLVM.Function)
6883
entry = invoke(finish_module!, Tuple{CompilerJob, LLVM.Module, LLVM.Function}, job, mod, entry)

src/validation.jl

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,9 @@ function check_ir!(job, errors::Vector{IRError}, mod::LLVM.Module)
147147
check_ir!(job, errors, f)
148148
end
149149

150+
# custom validation
151+
append!(errors, validate_module(job, mod))
152+
150153
return errors
151154
end
152155

0 commit comments

Comments
 (0)