I would like to use macros to generate functions with different bodies based on the function’s type parameters. @generated is not an option since ultimately these need to work with CUDA.jl (as far as I can tell, those are incompatible).
Consider this example, where I have a statement/block returned by my_body and I want it to be inline repeated.
macro repeat_inline(N, expr)
# Yes, this is terrible but it works
tail(expr, N) = N <= 0 ? :() : :($expr; $(tail(expr, N - 1)))
return esc(tail(expr, N))
@repeat_inline 3 @my_body
Macros operate on syntax, never on values. Even if those values are types and are known at compile time, they’re still values. The macro receives the symbol :N with no meaning or value attached to it.
Can you tell us a little more about what you’re actually trying to do and why it requires metaprogramming at all? Your minimal example is so minimal that it can (and definitely should) use a for loop instead of any macro trickery.
Mostly this was an ‘educational’ question to better understand macros. I’m writing kernels using CUDA.jl, specifically warp reductions. Since the warp size of mine and most other hardware is 32, I can safely hard-code 5 = log2(32). Explicit iteration costs precious registers. Putting that block in a function doesn’t seem like an option since the iteration mutates multiple variables, and I can’t return tuples on a gpu. This seemed nicer than trying to use pointers, if that is even possible.
The macro thing would be helpful for the next level of reductions, which span multiple warps, because then the number of times I could repeat might be limited if I’m trying to move larger objects through shared memory (and each iteration needs twice as much shared memory as the previous).
I think I might go the way of generating my own functions like:
macro make(name, arg, body)
esc(:($name($arg) = $body))
@make f x x*2
f (generic function with 1 method)