5 ms·
> As long as you cast it to the correct type, it always works. The new way of doing things thus encourages doing things correctly and makes it harder to do thin
by dahart 7y ago
> As long as you cast it to the correct type, it always works. The new way of doing things thus encourages doing things correctly and makes it harder to do things wrong.
This seems like a really interesting case where removing type information, removing something was presumably thought to be a safety guard, and requiring type casting, makes the type safety stronger by forcing the user to do it explicitly. Maybe it makes sense that dynamic calling mechanisms should require casting (and static type checking is less safe) where static calling mechanisms should use static type checking (and type casting is less safe).
> C specifies that certain types get promoted to wider types when passed as a variadic argument. Integers smaller than int (such as char and short) get promoted to int, and float gets promoted to double. If your method signature includes one of these types, it's not possible for a caller to pass a parameter as that exact type if it's using a variadic prototype.
Holy crap! I’ve used these things for many years without knowing this important tidbit. Does anyone know if it’s the same in C++, or whether GPUs are currently ignoring or following the spec?
- emilfihlman 7y agoI mean, you have seen how printf uses %d for char, short and int as a formatter?
- dahart 7y agoYeah, of course, but that doesn’t by itself lead me to the conclusion that those are all first converted to another type before being passed on the stack. They could be passed as-is and type-cast inside the printf implementation. The more interesting printf example would be %f, since according to what I learned today, it’s not a float, it’s a double. Are you saying you already knew doubles were passed in variadics instead of float, and the reason you knew is printf?
- microtherion 7y ago> They could be passed as-is and type-cast inside the printf implementation. How would you know what to type-cast FROM? It's not like C compilers pass type information around, nor are C values self describing. > Are you saying you already knew doubles were passed in variadics instead of float, and the reason you knew is printf? I can't speak for the person you're answering to, but in pre-ANSI C, these promotions were ubiquitous (since there were no function prototypes yet), so programmers were very likely to have heard about them at some point.
- boulos 7y ago> How would you know what to type-cast FROM? It's not like C compilers pass type information around, nor are C values self describing. That happens to be true, but one could imagine a calling convention that does do this, e.g. something like: fixed0, fixed1, ..., fixedN, numVarArgs, type0, ..., typeM, arg0, ..., argM it's just that it burns tons more registers than the promotion scheme does. And you'd still end up with a mini "interpreter" in the callee to at least switch over types.
- dahart 7y agoI’d be fibbing if I said I’d thought it out that far before commenting... :P But thank you for offering a plausible explanation, Solomon.
- boulos 7y agoNo problem, Dave! I had to do precisely this for PHP’s variadic arguments. I think HHVM made up even more calling conventions. Fwiw, the next optimization is to “compress” the type signatures into a handful of bits, to save registers. But it’s still not that big of a safety win for actual C code because of the clumsy switch inside the callee.
- boulos 7y agoI know from having to implement va_start/va_arg/va_end and it comes down to ABI. The challenge is how you would have either the caller or callee have a consistent piece of code for pushing/popping the stack. The int case is pretty clear, because whatever you go with a 32-bit register like most args in the C-family of ABIs, and it only matters for the register pressure (you're really just zero extending for char and short). I do find the float/double case clumsy, as is the int64 case, but it falls out of the rules for casting as mentioned and the ABI churn for both floating point and AMD64.
- stefan_ 7y agoSounds like undefined behavior, to be honest. No wonder Apple doesn't update GCC, they might just decide to optimize all the msgSend calls one day.
- Kwpolska 7y agoApple has migrated to clang/llvm a long time ago.
- Arcanum-XIII 7y agoApple use Clang - they’re one of the main contributor (also on LLVM if I’m not mistaken). And they’re optimizing the innard of Objective C too.
- microtherion 7y ago> Sounds like undefined behavior, to be honest. Quite the opposite — it's very precisely defined behavior if you bother to read the applicable standards. It's a rather useful exercise, and I'd recommend it to anyone who makes a living using a language.
- microtherion 7y agoYes, it's the same in C++: http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2013/n3797.pdf http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2013/n379... § 5.2.2 7 (pg 99): > When there is no parameter for a given argument [...] If the argument has integral or enumeration type that is subject to the integral promotions (4.5), or a floating point type that is subject to the floating point promotion (4.6), the value of the argument is converted to the promoted type before the call. These promotions are referred to as the default argument promotions. Sections 4.5 and 4.6 spell out in detail what promotions are applied.
- boulos 7y agoWere 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”.