  We describe an approach based on topology optimization that enables automatic
discovery of wavelength-scale photonic structures for achieving high-efficiency
second-harmonic generation (SHG). A key distinction from previous formulation
and designs that seek to maximize Purcell factors at individual frequencies is
that our method not only aims to achieve frequency matching (across an entire
octave) and large radiative lifetimes, but also optimizes the equally important
nonlinear--coupling figure of merit $\bar{\beta}$, involving a complicated
spatial overlap-integral between modes. We apply this method to the particular
problem of optimizing micropost and grating-slab cavities (one-dimensional
multilayered structures) and demonstrate that a variety of material platforms
can support modes with the requisite frequencies, large lifetimes $Q > 10^4$,
small modal volumes $\sim (\lambda/n)^3$, and extremely large $\bar{\beta}
\gtrsim 10^{-2}$, leading to orders of magnitude enhancements in SHG efficiency
compared to state of the art photonic designs. Such giant $\bar{\beta}$
alleviate the need for ultra-narrow linewidths and thus pave the way for
wavelength-scale SHG devices with faster operating timescales and higher
tolerance to fabrication imperfections.
