Add intrinsic to force-monomorphize; or some other solution for Index

ef1e6dc
Opened by Manish Goregaokar at 2023-09-14 04:32:25

For gdb to be able to index things like hashmaps, we need to ensure that the index implementation is monomorphized correctly.

I would like to have an intrinsic, which when found in a monomorphized function, will force monomorphization of the provided argument. For example, in Vec::new(), we can include force_monomorphization(<Vec<T> as Index>::index).

Alternatively, we could specially mark methods (index and deref? all of std::ops? has to be parametric over self but not parametric over other non-Self args.) that are always monomorphized if the corresponding concrete self type already exists. Perhaps only in debug mode. This may bloat binary size, but eh, debug mode.

cc @rust-lang/compiler @tromey

  1. The monomorphization can be triggered easily (e.g. cast to a function pointer). The tricky bit here is marking the monomorphized function so that it gets exported.

    Eduard-Mihai Burtescu at 2016-06-28 15:31:43

  2. Somewhat of a prior art: SPECIALISE pragma in haskell.

    Simonas Kazlauskas at 2016-06-28 17:53:50

  3. I don't think an intrinsic is appropriate here. An attribute or some other way to "eagerly" monomorphize makes more sense. As @eddyb says, the problem isn't the monomorphisation, it's ensuring the monomorphised function isn't removed as dead-code.

    James Miller at 2016-06-29 04:20:13

  4. Yeah, so the issue is instructing the compiler which monomorphizations should not be removed as dead code. An attribute on a method or trait method works, but what are the heuristics on it being included? The intrinsic lets us define these heuristic per-type, e.g. "only export this function if this other function monomorphization gets called". But I prefer the attribute solution myself -- just concerned about the binary size.

    Manish Goregaokar at 2016-06-29 05:40:30