3 ms·
Were you reading "variadic arguments" as for templates or function arguments? (Because I think this only applies to functions, as C lacks templates, and I haven
by boulos 7y ago
Were you reading "variadic arguments" as for templates or function arguments? (Because I think this only applies to functions, as C lacks templates, and I haven't ever seen varargs in CUDA (is that a thing?).
- dahart 7y agoHey again! I was thinking only functions, not templates, just wondering if C++ had changed things to not require the type promotion. CUDA has printf, but I suspect it’s implemented differently, not according to the C variadic function spec, reflecting on some of the weird ways that I’ve seen printf fail in CUDA code...
- boulos 7y agoWhoa, this is quite the rabbit hole. This commit [1] and it’s partner [2] suggest that at least around 2016, there is some really special case “yeah, but we really want printf”. I couldn’t find the CUDA runtime implementation on my phone, but now I’m curious what the PTX for a device function that calls printf looks like (like perhaps it does a bunch of stuff to emit a call to the host’s C printf, except it has/had to do so in a sort of strange way of building up buffers of things to print / exiting and coming back afterwards). [1] https://reviews.llvm.org/D16484 https://reviews.llvm.org/D16484 [2] https://reviews.llvm.org/D16559 https://reviews.llvm.org/D16559
- dahart 7y agoI’m going to look it up Monday, pretty sure there is something surprising in the printf impl, and IIRC there’s like no type safety at all. I vaguely remember writing buggy code that printed out data that I didn’t pass to printf.
- boulos 7y agoThat’s common though for variadic functions, particularly printf! Many printf implementations will blindly do a va_arg (popping the stack pointer by 4 or 8 bytes) if the format string says so. If your call to printf looks like: printf(“%d %d %d %d”, x, y); then you’re going to “read” two more ints from the stack at the call site. clang (and later gcc, I think) thus have a semantic analysis pass that understands/parses printf-style formatting strings so it can tell “hey, you didn’t pass enough arguments” or just as frequently “you passed too few”.